FileActivity.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2011 Bartek Przybylski
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import android.accounts.Account;
  23. import android.accounts.AccountManager;
  24. import android.accounts.AccountManagerCallback;
  25. import android.accounts.AccountManagerFuture;
  26. import android.accounts.AuthenticatorException;
  27. import android.accounts.OperationCanceledException;
  28. import android.content.ComponentName;
  29. import android.content.Context;
  30. import android.content.Intent;
  31. import android.content.ServiceConnection;
  32. import android.content.res.Configuration;
  33. import android.os.Bundle;
  34. import android.os.Handler;
  35. import android.os.IBinder;
  36. import android.support.v4.app.ActionBarDrawerToggle;
  37. import android.support.v4.app.Fragment;
  38. import android.support.v4.app.FragmentManager;
  39. import android.support.v4.app.FragmentTransaction;
  40. import android.support.v4.widget.DrawerLayout;
  41. import android.support.v7.app.ActionBar;
  42. import android.support.v7.app.ActionBarActivity;
  43. import android.view.View;
  44. import android.widget.AdapterView;
  45. import android.widget.LinearLayout;
  46. import android.widget.ListView;
  47. import android.widget.Toast;
  48. import com.owncloud.android.BuildConfig;
  49. import com.owncloud.android.MainApp;
  50. import com.owncloud.android.R;
  51. import com.owncloud.android.authentication.AccountUtils;
  52. import com.owncloud.android.authentication.AuthenticatorActivity;
  53. import com.owncloud.android.datamodel.FileDataStorageManager;
  54. import com.owncloud.android.datamodel.OCFile;
  55. import com.owncloud.android.files.FileOperationsHelper;
  56. import com.owncloud.android.files.services.FileDownloader;
  57. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  58. import com.owncloud.android.files.services.FileUploader;
  59. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  60. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  61. import com.owncloud.android.lib.common.operations.RemoteOperation;
  62. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  63. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  64. import com.owncloud.android.lib.common.utils.Log_OC;
  65. import com.owncloud.android.operations.CreateShareOperation;
  66. import com.owncloud.android.operations.SynchronizeFolderOperation;
  67. import com.owncloud.android.operations.UnshareLinkOperation;
  68. import com.owncloud.android.services.OperationsService;
  69. import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
  70. import com.owncloud.android.ui.NavigationDrawerItem;
  71. import com.owncloud.android.ui.adapter.NavigationDrawerListAdapter;
  72. import com.owncloud.android.ui.dialog.LoadingDialog;
  73. import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
  74. import com.owncloud.android.utils.ErrorMessageAdapter;
  75. import java.util.ArrayList;
  76. /**
  77. * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud
  78. * {@link Account}s .
  79. */
  80. public class FileActivity extends ActionBarActivity
  81. implements OnRemoteOperationListener, ComponentsGetter {
  82. public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
  83. public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
  84. public static final String EXTRA_WAITING_TO_PREVIEW =
  85. "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
  86. public static final String EXTRA_FROM_NOTIFICATION =
  87. "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
  88. public static final String TAG = FileActivity.class.getSimpleName();
  89. private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
  90. private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";
  91. private static final String DIALOG_SHARE_PASSWORD = "DIALOG_SHARE_PASSWORD";
  92. private static final String KEY_TRY_SHARE_AGAIN = "TRY_SHARE_AGAIN";
  93. private static final String KEY_ACTION_BAR_TITLE = "ACTION_BAR_TITLE";
  94. protected static final long DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS = 200;
  95. /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.*/
  96. private Account mAccount;
  97. /** Main {@link OCFile} handled by the activity.*/
  98. private OCFile mFile;
  99. /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud
  100. * {@link Account} */
  101. private boolean mRedirectingToSetupAccount = false;
  102. /** Flag to signal when the value of mAccount was set */
  103. protected boolean mAccountWasSet;
  104. /** Flag to signal when the value of mAccount was restored from a saved state */
  105. protected boolean mAccountWasRestored;
  106. /** Flag to signal if the activity is launched by a notification */
  107. private boolean mFromNotification;
  108. /** Messages handler associated to the main thread and the life cycle of the activity */
  109. private Handler mHandler;
  110. /** Access point to the cached database for the current ownCloud {@link Account} */
  111. private FileDataStorageManager mStorageManager = null;
  112. private FileOperationsHelper mFileOperationsHelper;
  113. private ServiceConnection mOperationsServiceConnection = null;
  114. private OperationsServiceBinder mOperationsServiceBinder = null;
  115. protected FileDownloaderBinder mDownloaderBinder = null;
  116. protected FileUploaderBinder mUploaderBinder = null;
  117. private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
  118. private boolean mTryShareAgain = false;
  119. // Navigation Drawer
  120. protected DrawerLayout mDrawerLayout;
  121. protected ActionBarDrawerToggle mDrawerToggle;
  122. protected ListView mDrawerList;
  123. // Slide menu items
  124. protected String[] mDrawerTitles;
  125. protected String[] mDrawerContentDescriptions;
  126. protected ArrayList<NavigationDrawerItem> mDrawerItems;
  127. protected NavigationDrawerListAdapter mNavigationDrawerAdapter = null;
  128. // TODO re-enable when "Accounts" is available in Navigation Drawer
  129. // protected boolean mShowAccounts = false;
  130. /**
  131. * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
  132. * the {@link FileActivity}.
  133. *
  134. * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
  135. * is requested to create a new one.
  136. */
  137. @Override
  138. protected void onCreate(Bundle savedInstanceState) {
  139. super.onCreate(savedInstanceState);
  140. mHandler = new Handler();
  141. mFileOperationsHelper = new FileOperationsHelper(this);
  142. Account account = null;
  143. if(savedInstanceState != null) {
  144. mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
  145. mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
  146. mFileOperationsHelper.setOpIdWaitingFor(
  147. savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID, Long.MAX_VALUE)
  148. );
  149. mTryShareAgain = savedInstanceState.getBoolean(KEY_TRY_SHARE_AGAIN);
  150. getSupportActionBar().setTitle(savedInstanceState.getString(KEY_ACTION_BAR_TITLE));
  151. } else {
  152. account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
  153. mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
  154. mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION,
  155. false);
  156. }
  157. AccountUtils.updateAccountVersion(this); // best place, before any access to AccountManager
  158. // or database
  159. setAccount(account, savedInstanceState != null);
  160. mOperationsServiceConnection = new OperationsServiceConnection();
  161. bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection,
  162. Context.BIND_AUTO_CREATE);
  163. mDownloadServiceConnection = newTransferenceServiceConnection();
  164. if (mDownloadServiceConnection != null) {
  165. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  166. Context.BIND_AUTO_CREATE);
  167. }
  168. mUploadServiceConnection = newTransferenceServiceConnection();
  169. if (mUploadServiceConnection != null) {
  170. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  171. Context.BIND_AUTO_CREATE);
  172. }
  173. }
  174. @Override
  175. protected void onNewIntent (Intent intent) {
  176. Log_OC.v(TAG, "onNewIntent() start");
  177. Account current = AccountUtils.getCurrentOwnCloudAccount(this);
  178. if (current != null && mAccount != null && !mAccount.name.equals(current.name)) {
  179. mAccount = current;
  180. }
  181. Log_OC.v(TAG, "onNewIntent() stop");
  182. }
  183. /**
  184. * Since ownCloud {@link Account}s can be managed from the system setting menu,
  185. * the existence of the {@link Account} associated to the instance must be checked
  186. * every time it is restarted.
  187. */
  188. @Override
  189. protected void onRestart() {
  190. Log_OC.v(TAG, "onRestart() start");
  191. super.onRestart();
  192. boolean validAccount = (mAccount != null && AccountUtils.exists(mAccount, this));
  193. if (!validAccount) {
  194. swapToDefaultAccount();
  195. }
  196. Log_OC.v(TAG, "onRestart() end");
  197. }
  198. @Override
  199. protected void onStart() {
  200. super.onStart();
  201. if (mAccountWasSet) {
  202. onAccountSet(mAccountWasRestored);
  203. }
  204. }
  205. @Override
  206. protected void onResume() {
  207. super.onResume();
  208. if (mOperationsServiceBinder != null) {
  209. doOnResumeAndBound();
  210. }
  211. }
  212. @Override
  213. protected void onPause() {
  214. if (mOperationsServiceBinder != null) {
  215. mOperationsServiceBinder.removeOperationListener(this);
  216. }
  217. super.onPause();
  218. }
  219. @Override
  220. protected void onDestroy() {
  221. if (mOperationsServiceConnection != null) {
  222. unbindService(mOperationsServiceConnection);
  223. mOperationsServiceBinder = null;
  224. }
  225. if (mDownloadServiceConnection != null) {
  226. unbindService(mDownloadServiceConnection);
  227. mDownloadServiceConnection = null;
  228. }
  229. if (mUploadServiceConnection != null) {
  230. unbindService(mUploadServiceConnection);
  231. mUploadServiceConnection = null;
  232. }
  233. super.onDestroy();
  234. }
  235. @Override
  236. protected void onPostCreate(Bundle savedInstanceState) {
  237. super.onPostCreate(savedInstanceState);
  238. // Sync the toggle state after onRestoreInstanceState has occurred.
  239. if (mDrawerToggle != null) {
  240. mDrawerToggle.syncState();
  241. }
  242. }
  243. @Override
  244. public void onConfigurationChanged(Configuration newConfig) {
  245. super.onConfigurationChanged(newConfig);
  246. if (mDrawerToggle != null) {
  247. mDrawerToggle.onConfigurationChanged(newConfig);
  248. }
  249. }
  250. protected void initDrawer(){
  251. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  252. // Notification Drawer
  253. LinearLayout navigationDrawerLayout = (LinearLayout) findViewById(R.id.left_drawer);
  254. mDrawerList = (ListView) navigationDrawerLayout.findViewById(R.id.drawer_list);
  255. // TODO re-enable when "Accounts" is available in Navigation Drawer
  256. // // load Account in the Drawer Title
  257. // // User-Icon
  258. // ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon);
  259. // userIcon.setImageResource(DisplayUtils.getSeasonalIconId());
  260. //
  261. // // Username
  262. // TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
  263. // Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  264. //
  265. // if (account != null) {
  266. // int lastAtPos = account.name.lastIndexOf("@");
  267. // username.setText(account.name.substring(0, lastAtPos));
  268. // }
  269. // load slide menu items
  270. mDrawerTitles = getResources().getStringArray(R.array.drawer_items);
  271. // nav drawer content description from resources
  272. mDrawerContentDescriptions = getResources().
  273. getStringArray(R.array.drawer_content_descriptions);
  274. // nav drawer items
  275. mDrawerItems = new ArrayList<NavigationDrawerItem>();
  276. // adding nav drawer items to array
  277. // TODO re-enable when "Accounts" is available in Navigation Drawer
  278. // Accounts
  279. // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0],
  280. // mDrawerContentDescriptions[0]));
  281. // All Files
  282. mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0], mDrawerContentDescriptions[0]));
  283. // TODO Enable when "On Device" is recovered
  284. // On Device
  285. //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
  286. // mDrawerContentDescriptions[2]));
  287. // Settings
  288. mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[1], mDrawerContentDescriptions[1]));
  289. // Logs
  290. if (BuildConfig.DEBUG) {
  291. mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2],
  292. mDrawerContentDescriptions[2]));
  293. }
  294. // setting the nav drawer list adapter
  295. mNavigationDrawerAdapter = new NavigationDrawerListAdapter(getApplicationContext(), this,
  296. mDrawerItems);
  297. mDrawerList.setAdapter(mNavigationDrawerAdapter);
  298. mDrawerToggle = new ActionBarDrawerToggle(
  299. this,
  300. mDrawerLayout,
  301. R.drawable.ic_drawer,
  302. R.string.drawer_open,
  303. R.string.drawer_close) {
  304. /** Called when a drawer has settled in a completely closed state. */
  305. public void onDrawerClosed(View view) {
  306. super.onDrawerClosed(view);
  307. updateActionBarTitle();
  308. getSupportActionBar().setDisplayShowTitleEnabled(true);
  309. getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  310. invalidateOptionsMenu();
  311. }
  312. /** Called when a drawer has settled in a completely open state. */
  313. public void onDrawerOpened(View drawerView) {
  314. super.onDrawerOpened(drawerView);
  315. getSupportActionBar().setTitle(R.string.drawer_open);
  316. getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  317. mDrawerToggle.setDrawerIndicatorEnabled(true);
  318. invalidateOptionsMenu();
  319. }
  320. };
  321. mDrawerToggle.setDrawerIndicatorEnabled(true);
  322. // Set the list's click listener
  323. mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  324. // Set the drawer toggle as the DrawerListener
  325. mDrawerLayout.setDrawerListener(mDrawerToggle);
  326. }
  327. protected void updateActionBarTitle(){
  328. if (mFile.getParentId() == 0) {
  329. getSupportActionBar().setTitle(getString(
  330. R.string.default_display_name_for_root_folder));
  331. mDrawerToggle.setDrawerIndicatorEnabled(true);
  332. } else {
  333. getSupportActionBar().setTitle(mFile.getFileName().toString());
  334. mDrawerToggle.setDrawerIndicatorEnabled(false);
  335. }
  336. }
  337. /**
  338. * Sets and validates the ownCloud {@link Account} associated to the Activity.
  339. *
  340. * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
  341. *
  342. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  343. *
  344. * @param account New {@link Account} to set.
  345. * @param savedAccount When 'true', account was retrieved from a saved instance state.
  346. */
  347. protected void setAccount(Account account, boolean savedAccount) {
  348. Account oldAccount = mAccount;
  349. boolean validAccount =
  350. (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(),
  351. account.name));
  352. if (validAccount) {
  353. mAccount = account;
  354. mAccountWasSet = true;
  355. mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
  356. } else {
  357. swapToDefaultAccount();
  358. }
  359. }
  360. /**
  361. * Tries to swap the current ownCloud {@link Account} for other valid and existing.
  362. *
  363. * If no valid ownCloud {@link Account} exists, the the user is requested
  364. * to create a new ownCloud {@link Account}.
  365. *
  366. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  367. */
  368. private void swapToDefaultAccount() {
  369. // default to the most recently used account
  370. Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  371. if (newAccount == null) {
  372. /// no account available: force account creation
  373. createFirstAccount();
  374. mRedirectingToSetupAccount = true;
  375. mAccountWasSet = false;
  376. mAccountWasRestored = false;
  377. } else {
  378. mAccountWasSet = true;
  379. mAccountWasRestored = (newAccount.equals(mAccount));
  380. mAccount = newAccount;
  381. }
  382. }
  383. /**
  384. * Launches the account creation activity. To use when no ownCloud account is available
  385. */
  386. private void createFirstAccount() {
  387. AccountManager am = AccountManager.get(getApplicationContext());
  388. am.addAccount(MainApp.getAccountType(),
  389. null,
  390. null,
  391. null,
  392. this,
  393. new AccountCreationCallback(),
  394. null);
  395. }
  396. /**
  397. * {@inheritDoc}
  398. */
  399. @Override
  400. protected void onSaveInstanceState(Bundle outState) {
  401. super.onSaveInstanceState(outState);
  402. outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
  403. outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
  404. outState.putLong(KEY_WAITING_FOR_OP_ID, mFileOperationsHelper.getOpIdWaitingFor());
  405. outState.putBoolean(KEY_TRY_SHARE_AGAIN, mTryShareAgain);
  406. outState.putString(KEY_ACTION_BAR_TITLE, getSupportActionBar().getTitle().toString());
  407. }
  408. /**
  409. * Getter for the main {@link OCFile} handled by the activity.
  410. *
  411. * @return Main {@link OCFile} handled by the activity.
  412. */
  413. public OCFile getFile() {
  414. return mFile;
  415. }
  416. /**
  417. * Setter for the main {@link OCFile} handled by the activity.
  418. *
  419. * @param file Main {@link OCFile} to be handled by the activity.
  420. */
  421. public void setFile(OCFile file) {
  422. mFile = file;
  423. }
  424. /**
  425. * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
  426. * is located.
  427. *
  428. * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
  429. * is located.
  430. */
  431. public Account getAccount() {
  432. return mAccount;
  433. }
  434. protected void setAccount(Account account) {
  435. mAccount = account;
  436. }
  437. /**
  438. * @return Value of mFromNotification: True if the Activity is launched by a notification
  439. */
  440. public boolean fromNotification() {
  441. return mFromNotification;
  442. }
  443. /**
  444. * @return 'True' when the Activity is finishing to enforce the setup of a new account.
  445. */
  446. protected boolean isRedirectingToSetupAccount() {
  447. return mRedirectingToSetupAccount;
  448. }
  449. public boolean isTryShareAgain(){
  450. return mTryShareAgain;
  451. }
  452. public void setTryShareAgain(boolean tryShareAgain) {
  453. mTryShareAgain = tryShareAgain;
  454. }
  455. public OperationsServiceBinder getOperationsServiceBinder() {
  456. return mOperationsServiceBinder;
  457. }
  458. protected ServiceConnection newTransferenceServiceConnection() {
  459. return null;
  460. }
  461. /**
  462. * Helper class handling a callback from the {@link AccountManager} after the creation of
  463. * a new ownCloud {@link Account} finished, successfully or not.
  464. *
  465. * At this moment, only called after the creation of the first account.
  466. */
  467. public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
  468. @Override
  469. public void run(AccountManagerFuture<Bundle> future) {
  470. FileActivity.this.mRedirectingToSetupAccount = false;
  471. boolean accountWasSet = false;
  472. if (future != null) {
  473. try {
  474. Bundle result;
  475. result = future.getResult();
  476. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  477. String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
  478. if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
  479. setAccount(new Account(name, type), false);
  480. accountWasSet = true;
  481. }
  482. } catch (OperationCanceledException e) {
  483. Log_OC.d(TAG, "Account creation canceled");
  484. } catch (Exception e) {
  485. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  486. }
  487. } else {
  488. Log_OC.e(TAG, "Account creation callback with null bundle");
  489. }
  490. if (!accountWasSet) {
  491. moveTaskToBack(true);
  492. }
  493. }
  494. }
  495. /**
  496. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  497. *
  498. * Child classes must grant that state depending on the {@link Account} is updated.
  499. */
  500. protected void onAccountSet(boolean stateWasRecovered) {
  501. if (getAccount() != null) {
  502. mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
  503. } else {
  504. Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
  505. }
  506. }
  507. public FileDataStorageManager getStorageManager() {
  508. return mStorageManager;
  509. }
  510. public OnRemoteOperationListener getRemoteOperationListener() {
  511. return this;
  512. }
  513. public Handler getHandler() {
  514. return mHandler;
  515. }
  516. public FileOperationsHelper getFileOperationsHelper() {
  517. return mFileOperationsHelper;
  518. }
  519. /**
  520. *
  521. * @param operation Removal operation performed.
  522. * @param result Result of the removal.
  523. */
  524. @Override
  525. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  526. Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the "
  527. + "FileActivities ");
  528. mFileOperationsHelper.setOpIdWaitingFor(Long.MAX_VALUE);
  529. if (!result.isSuccess() && (
  530. result.getCode() == ResultCode.UNAUTHORIZED ||
  531. result.isIdPRedirection() ||
  532. (result.isException() && result.getException() instanceof AuthenticatorException)
  533. )) {
  534. requestCredentialsUpdate();
  535. if (result.getCode() == ResultCode.UNAUTHORIZED) {
  536. dismissLoadingDialog();
  537. Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
  538. operation, getResources()),
  539. Toast.LENGTH_LONG);
  540. t.show();
  541. }
  542. mTryShareAgain = false;
  543. } else if (operation instanceof CreateShareOperation) {
  544. onCreateShareOperationFinish((CreateShareOperation) operation, result);
  545. } else if (operation instanceof UnshareLinkOperation) {
  546. onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
  547. } else if (operation instanceof SynchronizeFolderOperation) {
  548. onSynchronizeFolderOperationFinish((SynchronizeFolderOperation)operation, result);
  549. }
  550. }
  551. protected void requestCredentialsUpdate() {
  552. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  553. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
  554. updateAccountCredentials.putExtra(
  555. AuthenticatorActivity.EXTRA_ACTION,
  556. AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
  557. updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  558. startActivity(updateAccountCredentials);
  559. }
  560. private void onCreateShareOperationFinish(CreateShareOperation operation,
  561. RemoteOperationResult result) {
  562. dismissLoadingDialog();
  563. if (result.isSuccess()) {
  564. mTryShareAgain = false;
  565. updateFileFromDB();
  566. Intent sendIntent = operation.getSendIntent();
  567. startActivity(sendIntent);
  568. } else {
  569. // Detect Failure (403) --> needs Password
  570. if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  571. if (!isTryShareAgain()) {
  572. SharePasswordDialogFragment dialog =
  573. SharePasswordDialogFragment.newInstance(new OCFile(operation.getPath()),
  574. operation.getSendIntent());
  575. dialog.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD);
  576. } else {
  577. Toast t = Toast.makeText(this,
  578. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  579. Toast.LENGTH_LONG);
  580. t.show();
  581. mTryShareAgain = false;
  582. }
  583. } else {
  584. Toast t = Toast.makeText(this,
  585. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  586. Toast.LENGTH_LONG);
  587. t.show();
  588. }
  589. }
  590. }
  591. private void onUnshareLinkOperationFinish(UnshareLinkOperation operation,
  592. RemoteOperationResult result) {
  593. dismissLoadingDialog();
  594. if (result.isSuccess()){
  595. updateFileFromDB();
  596. } else {
  597. Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
  598. operation, getResources()), Toast.LENGTH_LONG);
  599. t.show();
  600. }
  601. }
  602. private void onSynchronizeFolderOperationFinish(
  603. SynchronizeFolderOperation operation, RemoteOperationResult result
  604. ) {
  605. if (!result.isSuccess() && result.getCode() != ResultCode.CANCELLED){
  606. Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result,
  607. operation, getResources()), Toast.LENGTH_LONG);
  608. t.show();
  609. }
  610. }
  611. protected void updateFileFromDB(){
  612. OCFile file = getFile();
  613. if (file != null) {
  614. file = getStorageManager().getFileByPath(file.getRemotePath());
  615. setFile(file);
  616. }
  617. }
  618. /**
  619. * Show loading dialog
  620. */
  621. public void showLoadingDialog() {
  622. // Construct dialog
  623. LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
  624. FragmentManager fm = getSupportFragmentManager();
  625. FragmentTransaction ft = fm.beginTransaction();
  626. loading.show(ft, DIALOG_WAIT_TAG);
  627. }
  628. /**
  629. * Dismiss loading dialog
  630. */
  631. public void dismissLoadingDialog(){
  632. Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
  633. if (frag != null) {
  634. LoadingDialog loading = (LoadingDialog) frag;
  635. loading.dismiss();
  636. }
  637. }
  638. private void doOnResumeAndBound() {
  639. mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler);
  640. long waitingForOpId = mFileOperationsHelper.getOpIdWaitingFor();
  641. if (waitingForOpId <= Integer.MAX_VALUE) {
  642. boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int)waitingForOpId,
  643. this);
  644. if (!wait ) {
  645. dismissLoadingDialog();
  646. }
  647. }
  648. }
  649. /**
  650. * Implements callback methods for service binding. Passed as a parameter to {
  651. */
  652. private class OperationsServiceConnection implements ServiceConnection {
  653. @Override
  654. public void onServiceConnected(ComponentName component, IBinder service) {
  655. if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
  656. Log_OC.d(TAG, "Operations service connected");
  657. mOperationsServiceBinder = (OperationsServiceBinder) service;
  658. /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
  659. dismissLoadingDialog();
  660. }*/
  661. doOnResumeAndBound();
  662. } else {
  663. return;
  664. }
  665. }
  666. @Override
  667. public void onServiceDisconnected(ComponentName component) {
  668. if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
  669. Log_OC.d(TAG, "Operations service disconnected");
  670. mOperationsServiceBinder = null;
  671. // TODO whatever could be waiting for the service is unbound
  672. }
  673. }
  674. }
  675. @Override
  676. public FileDownloaderBinder getFileDownloaderBinder() {
  677. return mDownloaderBinder;
  678. }
  679. @Override
  680. public FileUploaderBinder getFileUploaderBinder() {
  681. return mUploaderBinder;
  682. }
  683. public void restart(){
  684. Intent i = new Intent(this, FileDisplayActivity.class);
  685. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  686. startActivity(i);
  687. }
  688. // TODO re-enable when "Accounts" is available in Navigation Drawer
  689. // public void closeDrawer() {
  690. // mDrawerLayout.closeDrawers();
  691. // }
  692. public void allFilesOption(){
  693. restart();
  694. }
  695. private class DrawerItemClickListener implements ListView.OnItemClickListener {
  696. @Override
  697. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  698. // TODO re-enable when "Accounts" is available in Navigation Drawer
  699. // if (mShowAccounts && position > 0){
  700. // position = position - 1;
  701. // }
  702. switch (position){
  703. // TODO re-enable when "Accounts" is available in Navigation Drawer
  704. // case 0: // Accounts
  705. // mShowAccounts = !mShowAccounts;
  706. // mNavigationDrawerAdapter.setShowAccounts(mShowAccounts);
  707. // mNavigationDrawerAdapter.notifyDataSetChanged();
  708. // break;
  709. case 0: // All Files
  710. allFilesOption();
  711. mDrawerLayout.closeDrawers();
  712. break;
  713. // TODO Enable when "On Device" is recovered ?
  714. // case 2:
  715. // MainApp.showOnlyFilesOnDevice(true);
  716. // mDrawerLayout.closeDrawers();
  717. // break;
  718. case 1: // Settings
  719. Intent settingsIntent = new Intent(getApplicationContext(),
  720. Preferences.class);
  721. startActivity(settingsIntent);
  722. break;
  723. case 2: // Logs
  724. Intent loggerIntent = new Intent(getApplicationContext(),
  725. LogHistoryActivity.class);
  726. startActivity(loggerIntent);
  727. break;
  728. }
  729. }
  730. }
  731. }