BaseActivity.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.owncloud.android.ui.activity;
  2. import android.accounts.Account;
  3. import android.accounts.AccountManager;
  4. import android.accounts.AccountManagerCallback;
  5. import android.accounts.AccountManagerFuture;
  6. import android.accounts.OperationCanceledException;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.support.v7.app.AppCompatActivity;
  11. import com.owncloud.android.MainApp;
  12. import com.owncloud.android.authentication.AccountUtils;
  13. import com.owncloud.android.datamodel.FileDataStorageManager;
  14. import com.owncloud.android.datamodel.OCFile;
  15. import com.owncloud.android.lib.common.utils.Log_OC;
  16. import com.owncloud.android.lib.resources.status.OCCapability;
  17. /**
  18. * Base activity with common behaviour for activities dealing with ownCloud {@link Account}s .
  19. */
  20. public abstract class BaseActivity extends AppCompatActivity {
  21. private static final String TAG = BaseActivity.class.getSimpleName();
  22. /**
  23. * ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  24. */
  25. private Account mCurrentAccount;
  26. /**
  27. * Capabilities of the server where {@link #mCurrentAccount} lives.
  28. */
  29. private OCCapability mCapabilities;
  30. /**
  31. * Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account}.
  32. */
  33. private boolean mRedirectingToSetupAccount = false;
  34. /**
  35. * Flag to signal when the value of mAccount was set.
  36. */
  37. protected boolean mAccountWasSet;
  38. /**
  39. * Flag to signal when the value of mAccount was restored from a saved state.
  40. */
  41. protected boolean mAccountWasRestored;
  42. /**
  43. * Access point to the cached database for the current ownCloud {@link Account}.
  44. */
  45. private FileDataStorageManager mStorageManager = null;
  46. @Override
  47. protected void onNewIntent (Intent intent) {
  48. Log_OC.v(TAG, "onNewIntent() start");
  49. Account current = AccountUtils.getCurrentOwnCloudAccount(this);
  50. if (current != null && mCurrentAccount != null && !mCurrentAccount.name.equals(current.name)) {
  51. mCurrentAccount = current;
  52. }
  53. Log_OC.v(TAG, "onNewIntent() stop");
  54. }
  55. /**
  56. * Since ownCloud {@link Account}s can be managed from the system setting menu, the existence of the {@link
  57. * Account} associated to the instance must be checked every time it is restarted.
  58. */
  59. @Override
  60. protected void onRestart() {
  61. Log_OC.v(TAG, "onRestart() start");
  62. super.onRestart();
  63. boolean validAccount = (mCurrentAccount != null && AccountUtils.exists(mCurrentAccount, this));
  64. if (!validAccount) {
  65. swapToDefaultAccount();
  66. }
  67. Log_OC.v(TAG, "onRestart() end");
  68. }
  69. /**
  70. * Sets and validates the ownCloud {@link Account} associated to the Activity.
  71. *
  72. * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
  73. *
  74. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  75. *
  76. * @param account New {@link Account} to set.
  77. * @param savedAccount When 'true', account was retrieved from a saved instance state.
  78. */
  79. protected void setAccount(Account account, boolean savedAccount) {
  80. Account oldAccount = mCurrentAccount;
  81. boolean validAccount =
  82. (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(),
  83. account.name));
  84. if (validAccount) {
  85. mCurrentAccount = account;
  86. mAccountWasSet = true;
  87. mAccountWasRestored = (savedAccount || mCurrentAccount.equals(oldAccount));
  88. } else {
  89. swapToDefaultAccount();
  90. }
  91. }
  92. /**
  93. * Tries to swap the current ownCloud {@link Account} for other valid and existing.
  94. *
  95. * If no valid ownCloud {@link Account} exists, the the user is requested
  96. * to create a new ownCloud {@link Account}.
  97. *
  98. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  99. */
  100. protected void swapToDefaultAccount() {
  101. // default to the most recently used account
  102. Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  103. if (newAccount == null) {
  104. /// no account available: force account creation
  105. createAccount(true);
  106. mRedirectingToSetupAccount = true;
  107. mAccountWasSet = false;
  108. mAccountWasRestored = false;
  109. } else {
  110. mAccountWasSet = true;
  111. mAccountWasRestored = (newAccount.equals(mCurrentAccount));
  112. mCurrentAccount = newAccount;
  113. }
  114. }
  115. /**
  116. * Launches the account creation activity.
  117. *
  118. * @param mandatoryCreation When 'true', if an account is not created by the user, the app will be closed.
  119. * To use when no ownCloud account is available.
  120. */
  121. protected void createAccount(boolean mandatoryCreation) {
  122. AccountManager am = AccountManager.get(getApplicationContext());
  123. am.addAccount(MainApp.getAccountType(),
  124. null,
  125. null,
  126. null,
  127. this,
  128. new AccountCreationCallback(mandatoryCreation),
  129. new Handler());
  130. }
  131. /**
  132. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  133. *
  134. * Child classes must grant that state depending on the {@link Account} is updated.
  135. */
  136. protected void onAccountSet(boolean stateWasRecovered) {
  137. if (getAccount() != null) {
  138. mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
  139. mCapabilities = mStorageManager.getCapability(mCurrentAccount.name);
  140. } else {
  141. Log_OC.e(TAG, "onAccountChanged was called with NULL account associated!");
  142. }
  143. }
  144. protected void setAccount(Account account) {
  145. mCurrentAccount = account;
  146. }
  147. /**
  148. * Getter for the capabilities of the server where the current OC account lives.
  149. *
  150. * @return Capabilities of the server where the current OC account lives. Null if the account is not
  151. * set yet.
  152. */
  153. public OCCapability getCapabilities() {
  154. return mCapabilities;
  155. }
  156. /**
  157. * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity
  158. * is located.
  159. *
  160. * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity
  161. * is located.
  162. */
  163. public Account getAccount() {
  164. return mCurrentAccount;
  165. }
  166. @Override
  167. protected void onStart() {
  168. super.onStart();
  169. if (mAccountWasSet) {
  170. onAccountSet(mAccountWasRestored);
  171. }
  172. }
  173. /**
  174. * @return 'True' when the Activity is finishing to enforce the setup of a new account.
  175. */
  176. protected boolean isRedirectingToSetupAccount() {
  177. return mRedirectingToSetupAccount;
  178. }
  179. public FileDataStorageManager getStorageManager() {
  180. return mStorageManager;
  181. }
  182. /**
  183. * Method that gets called when a new account has been successfully created.
  184. *
  185. * @param future
  186. */
  187. protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> future) {
  188. // no special handling in base activity
  189. }
  190. /**
  191. * Helper class handling a callback from the {@link AccountManager} after the creation of
  192. * a new ownCloud {@link Account} finished, successfully or not.
  193. */
  194. public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
  195. boolean mMandatoryCreation;
  196. /**
  197. * Constuctor
  198. *
  199. * @param mandatoryCreation When 'true', if an account was not created, the app is closed.
  200. */
  201. public AccountCreationCallback(boolean mandatoryCreation) {
  202. mMandatoryCreation = mandatoryCreation;
  203. }
  204. @Override
  205. public void run(AccountManagerFuture<Bundle> future) {
  206. BaseActivity.this.mRedirectingToSetupAccount = false;
  207. boolean accountWasSet = false;
  208. if (future != null) {
  209. try {
  210. Bundle result;
  211. result = future.getResult();
  212. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  213. String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
  214. if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
  215. setAccount(new Account(name, type), false);
  216. accountWasSet = true;
  217. }
  218. onAccountCreationSuccessful(future);
  219. } catch (OperationCanceledException e) {
  220. Log_OC.d(TAG, "Account creation canceled");
  221. } catch (Exception e) {
  222. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  223. }
  224. } else {
  225. Log_OC.e(TAG, "Account creation callback with null bundle");
  226. }
  227. if (mMandatoryCreation && !accountWasSet) {
  228. moveTaskToBack(true);
  229. }
  230. }
  231. }
  232. }