BaseActivity.java 7.6 KB

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