BaseActivity.java 8.6 KB

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