BaseActivity.java 8.0 KB

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