BaseActivity.java 9.5 KB

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