FileActivity.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.activity;
  19. import android.accounts.Account;
  20. import android.accounts.AccountManager;
  21. import android.accounts.AccountManagerCallback;
  22. import android.accounts.AccountManagerFuture;
  23. import android.accounts.OperationCanceledException;
  24. import android.content.ComponentName;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.content.ServiceConnection;
  28. import android.os.Bundle;
  29. import android.os.Handler;
  30. import android.os.IBinder;
  31. import android.support.v4.app.Fragment;
  32. import android.support.v4.app.FragmentManager;
  33. import android.support.v4.app.FragmentTransaction;
  34. import android.widget.Toast;
  35. import com.actionbarsherlock.app.SherlockFragmentActivity;
  36. import com.owncloud.android.MainApp;
  37. import com.owncloud.android.R;
  38. import com.owncloud.android.authentication.AccountUtils;
  39. import com.owncloud.android.datamodel.FileDataStorageManager;
  40. import com.owncloud.android.datamodel.OCFile;
  41. import com.owncloud.android.files.FileOperationsHelper;
  42. import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
  43. import com.owncloud.android.lib.operations.common.RemoteOperation;
  44. import com.owncloud.android.lib.operations.common.RemoteOperationResult;
  45. import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
  46. import com.owncloud.android.operations.CreateShareOperation;
  47. import com.owncloud.android.operations.UnshareLinkOperation;
  48. import com.owncloud.android.services.OperationsService;
  49. import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
  50. import com.owncloud.android.ui.dialog.LoadingDialog;
  51. import com.owncloud.android.utils.Log_OC;
  52. /**
  53. * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
  54. *
  55. * @author David A. Velasco
  56. */
  57. public class FileActivity extends SherlockFragmentActivity implements OnRemoteOperationListener {
  58. public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
  59. public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
  60. public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
  61. public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
  62. public static final String TAG = FileActivity.class.getSimpleName();
  63. private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
  64. /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
  65. private Account mAccount;
  66. /** Main {@link OCFile} handled by the activity.*/
  67. private OCFile mFile;
  68. /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
  69. private boolean mRedirectingToSetupAccount = false;
  70. /** Flag to signal when the value of mAccount was set */
  71. private boolean mAccountWasSet;
  72. /** Flag to signal when the value of mAccount was restored from a saved state */
  73. private boolean mAccountWasRestored;
  74. /** Flag to signal if the activity is launched by a notification */
  75. private boolean mFromNotification;
  76. /** Messages handler associated to the main thread and the life cycle of the activity */
  77. private Handler mHandler;
  78. /** Access point to the cached database for the current ownCloud {@link Account} */
  79. private FileDataStorageManager mStorageManager = null;
  80. private FileOperationsHelper mFileOperationsHelper;
  81. private ServiceConnection mOperationsServiceConnection = null;
  82. private OperationsServiceBinder mOperationsServiceBinder = null;
  83. /**
  84. * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
  85. * the {@link FileActivity}.
  86. *
  87. * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
  88. * is requested to create a new one.
  89. */
  90. @Override
  91. protected void onCreate(Bundle savedInstanceState) {
  92. super.onCreate(savedInstanceState);
  93. mHandler = new Handler();
  94. mFileOperationsHelper = new FileOperationsHelper();
  95. Account account;
  96. if(savedInstanceState != null) {
  97. account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  98. mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
  99. mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
  100. } else {
  101. account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
  102. mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
  103. mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
  104. }
  105. setAccount(account, savedInstanceState != null);
  106. mOperationsServiceConnection = new OperationsServiceConnection();
  107. bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection, Context.BIND_AUTO_CREATE);
  108. }
  109. /**
  110. * Since ownCloud {@link Account}s can be managed from the system setting menu,
  111. * the existence of the {@link Account} associated to the instance must be checked
  112. * every time it is restarted.
  113. */
  114. @Override
  115. protected void onRestart() {
  116. super.onRestart();
  117. boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
  118. if (!validAccount) {
  119. swapToDefaultAccount();
  120. }
  121. }
  122. @Override
  123. protected void onStart() {
  124. super.onStart();
  125. if (mAccountWasSet) {
  126. onAccountSet(mAccountWasRestored);
  127. }
  128. }
  129. @Override
  130. protected void onStop() {
  131. super.onStop();
  132. }
  133. @Override
  134. protected void onDestroy() {
  135. super.onDestroy();
  136. if (mOperationsServiceConnection != null) {
  137. if (mOperationsServiceBinder != null) {
  138. mOperationsServiceBinder.removeOperationListener(this);
  139. mOperationsServiceBinder = null;
  140. }
  141. unbindService(mOperationsServiceConnection);
  142. }
  143. }
  144. /**
  145. * Sets and validates the ownCloud {@link Account} associated to the Activity.
  146. *
  147. * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
  148. *
  149. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  150. *
  151. * @param account New {@link Account} to set.
  152. * @param savedAccount When 'true', account was retrieved from a saved instance state.
  153. */
  154. private void setAccount(Account account, boolean savedAccount) {
  155. Account oldAccount = mAccount;
  156. boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
  157. if (validAccount) {
  158. mAccount = account;
  159. mAccountWasSet = true;
  160. mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
  161. } else {
  162. swapToDefaultAccount();
  163. }
  164. }
  165. /**
  166. * Tries to swap the current ownCloud {@link Account} for other valid and existing.
  167. *
  168. * If no valid ownCloud {@link Account} exists, the the user is requested
  169. * to create a new ownCloud {@link Account}.
  170. *
  171. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  172. *
  173. * @return 'True' if the checked {@link Account} was valid.
  174. */
  175. private void swapToDefaultAccount() {
  176. // default to the most recently used account
  177. Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  178. if (newAccount == null) {
  179. /// no account available: force account creation
  180. createFirstAccount();
  181. mRedirectingToSetupAccount = true;
  182. mAccountWasSet = false;
  183. mAccountWasRestored = false;
  184. } else {
  185. mAccountWasSet = true;
  186. mAccountWasRestored = (newAccount.equals(mAccount));
  187. mAccount = newAccount;
  188. }
  189. }
  190. /**
  191. * Launches the account creation activity. To use when no ownCloud account is available
  192. */
  193. private void createFirstAccount() {
  194. AccountManager am = AccountManager.get(getApplicationContext());
  195. am.addAccount(MainApp.getAccountType(),
  196. null,
  197. null,
  198. null,
  199. this,
  200. new AccountCreationCallback(),
  201. null);
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. @Override
  207. protected void onSaveInstanceState(Bundle outState) {
  208. super.onSaveInstanceState(outState);
  209. outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
  210. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  211. outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
  212. }
  213. /**
  214. * Getter for the main {@link OCFile} handled by the activity.
  215. *
  216. * @return Main {@link OCFile} handled by the activity.
  217. */
  218. public OCFile getFile() {
  219. return mFile;
  220. }
  221. /**
  222. * Setter for the main {@link OCFile} handled by the activity.
  223. *
  224. * @param file Main {@link OCFile} to be handled by the activity.
  225. */
  226. public void setFile(OCFile file) {
  227. mFile = file;
  228. }
  229. /**
  230. * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  231. *
  232. * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  233. */
  234. public Account getAccount() {
  235. return mAccount;
  236. }
  237. /**
  238. * @return Value of mFromNotification: True if the Activity is launched by a notification
  239. */
  240. public boolean fromNotification() {
  241. return mFromNotification;
  242. }
  243. /**
  244. * @return 'True' when the Activity is finishing to enforce the setup of a new account.
  245. */
  246. protected boolean isRedirectingToSetupAccount() {
  247. return mRedirectingToSetupAccount;
  248. }
  249. /**
  250. * Helper class handling a callback from the {@link AccountManager} after the creation of
  251. * a new ownCloud {@link Account} finished, successfully or not.
  252. *
  253. * At this moment, only called after the creation of the first account.
  254. *
  255. * @author David A. Velasco
  256. */
  257. public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
  258. @Override
  259. public void run(AccountManagerFuture<Bundle> future) {
  260. FileActivity.this.mRedirectingToSetupAccount = false;
  261. boolean accountWasSet = false;
  262. if (future != null) {
  263. try {
  264. Bundle result;
  265. result = future.getResult();
  266. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  267. String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
  268. if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
  269. setAccount(new Account(name, type), false);
  270. accountWasSet = true;
  271. }
  272. } catch (OperationCanceledException e) {
  273. Log_OC.d(TAG, "Account creation canceled");
  274. } catch (Exception e) {
  275. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  276. }
  277. } else {
  278. Log_OC.e(TAG, "Account creation callback with null bundle");
  279. }
  280. if (!accountWasSet) {
  281. moveTaskToBack(true);
  282. }
  283. }
  284. }
  285. /**
  286. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  287. *
  288. * Child classes must grant that state depending on the {@link Account} is updated.
  289. */
  290. protected void onAccountSet(boolean stateWasRecovered) {
  291. if (getAccount() != null) {
  292. mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
  293. } else {
  294. Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
  295. }
  296. }
  297. public FileDataStorageManager getStorageManager() {
  298. return mStorageManager;
  299. }
  300. public OnRemoteOperationListener getRemoteOperationListener() {
  301. return this;
  302. }
  303. public Handler getHandler() {
  304. return mHandler;
  305. }
  306. public FileOperationsHelper getFileOperationsHelper() {
  307. return mFileOperationsHelper;
  308. }
  309. /**
  310. *
  311. * @param operation Removal operation performed.
  312. * @param result Result of the removal.
  313. */
  314. @Override
  315. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  316. Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the FileActivities ");
  317. if (operation instanceof CreateShareOperation) {
  318. onCreateShareOperationFinish((CreateShareOperation) operation, result);
  319. } else if (operation instanceof UnshareLinkOperation) {
  320. onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
  321. }
  322. }
  323. private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) {
  324. dismissLoadingDialog();
  325. if (result.isSuccess()) {
  326. updateFileFromDB();
  327. Intent sendIntent = operation.getSendIntent();
  328. startActivity(sendIntent);
  329. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  330. Toast t = Toast.makeText(this, getString(R.string.share_link_file_no_exist), Toast.LENGTH_LONG);
  331. t.show();
  332. } else { // Generic error
  333. // Show a Message, operation finished without success
  334. Toast t = Toast.makeText(this, getString(R.string.share_link_file_error), Toast.LENGTH_LONG);
  335. t.show();
  336. }
  337. }
  338. private void onUnshareLinkOperationFinish(UnshareLinkOperation operation, RemoteOperationResult result) {
  339. dismissLoadingDialog();
  340. if (result.isSuccess()){
  341. updateFileFromDB();
  342. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  343. Toast t = Toast.makeText(this, getString(R.string.unshare_link_file_no_exist), Toast.LENGTH_LONG);
  344. t.show();
  345. } else { // Generic error
  346. // Show a Message, operation finished without success
  347. Toast t = Toast.makeText(this, getString(R.string.unshare_link_file_error), Toast.LENGTH_LONG);
  348. t.show();
  349. }
  350. }
  351. private void updateFileFromDB(){
  352. OCFile file = getStorageManager().getFileByPath(getFile().getRemotePath());
  353. if (file != null) {
  354. setFile(file);
  355. }
  356. }
  357. /**
  358. * Show loading dialog
  359. */
  360. public void showLoadingDialog() {
  361. // Construct dialog
  362. LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
  363. FragmentManager fm = getSupportFragmentManager();
  364. FragmentTransaction ft = fm.beginTransaction();
  365. loading.show(ft, DIALOG_WAIT_TAG);
  366. }
  367. /**
  368. * Dismiss loading dialog
  369. */
  370. public void dismissLoadingDialog(){
  371. Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
  372. if (frag != null) {
  373. LoadingDialog loading = (LoadingDialog) frag;
  374. loading.dismiss();
  375. }
  376. }
  377. /**
  378. * Implements callback methods for service binding. Passed as a parameter to {
  379. */
  380. private class OperationsServiceConnection implements ServiceConnection {
  381. @Override
  382. public void onServiceConnected(ComponentName component, IBinder service) {
  383. if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
  384. Log_OC.d(TAG, "Operations service connected");
  385. mOperationsServiceBinder = (OperationsServiceBinder) service;
  386. mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler);
  387. if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
  388. dismissLoadingDialog();
  389. }
  390. } else {
  391. return;
  392. }
  393. }
  394. @Override
  395. public void onServiceDisconnected(ComponentName component) {
  396. if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
  397. Log_OC.d(TAG, "Operations service disconnected");
  398. mOperationsServiceBinder = null;
  399. // TODO whatever could be waiting for the service is unbound
  400. }
  401. }
  402. };
  403. }