ManageAccountsActivity.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 ownCloud Inc.
  6. * <p/>
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. * <p/>
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * <p/>
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.ui.activity;
  20. import android.accounts.Account;
  21. import android.accounts.AccountManager;
  22. import android.accounts.AccountManagerCallback;
  23. import android.accounts.AccountManagerFuture;
  24. import android.accounts.OperationCanceledException;
  25. import android.content.ComponentName;
  26. import android.content.Context;
  27. import android.content.Intent;
  28. import android.content.ServiceConnection;
  29. import android.graphics.drawable.Drawable;
  30. import android.os.Bundle;
  31. import android.os.Handler;
  32. import android.os.IBinder;
  33. import android.view.MenuItem;
  34. import android.view.View;
  35. import android.widget.AdapterView;
  36. import android.widget.ListView;
  37. import com.evernote.android.job.JobRequest;
  38. import com.evernote.android.job.util.support.PersistableBundleCompat;
  39. import com.owncloud.android.MainApp;
  40. import com.owncloud.android.R;
  41. import com.owncloud.android.authentication.AccountUtils;
  42. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  43. import com.owncloud.android.datamodel.FileDataStorageManager;
  44. import com.owncloud.android.files.services.FileDownloader;
  45. import com.owncloud.android.files.services.FileUploader;
  46. import com.owncloud.android.jobs.AccountRemovalJob;
  47. import com.owncloud.android.lib.common.OwnCloudAccount;
  48. import com.owncloud.android.lib.common.utils.Log_OC;
  49. import com.owncloud.android.services.OperationsService;
  50. import com.owncloud.android.ui.adapter.AccountListAdapter;
  51. import com.owncloud.android.ui.adapter.AccountListItem;
  52. import com.owncloud.android.ui.events.AccountRemovedEvent;
  53. import com.owncloud.android.ui.helpers.FileOperationsHelper;
  54. import com.owncloud.android.utils.DisplayUtils;
  55. import com.owncloud.android.utils.ThemeUtils;
  56. import org.greenrobot.eventbus.Subscribe;
  57. import org.greenrobot.eventbus.ThreadMode;
  58. import org.parceler.Parcels;
  59. import java.util.ArrayList;
  60. import java.util.Arrays;
  61. import java.util.List;
  62. import java.util.Set;
  63. import androidx.core.content.ContextCompat;
  64. import androidx.core.graphics.drawable.DrawableCompat;
  65. /**
  66. * An Activity that allows the user to manage accounts.
  67. */
  68. public class ManageAccountsActivity extends FileActivity
  69. implements AccountListAdapter.AccountListAdapterListener, AccountManagerCallback<Boolean>, ComponentsGetter {
  70. private static final String TAG = ManageAccountsActivity.class.getSimpleName();
  71. public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED";
  72. public static final String KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED";
  73. public static final String PENDING_FOR_REMOVAL = "PENDING_FOR_REMOVAL";
  74. private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
  75. private static final int KEY_USER_INFO_REQUEST_CODE = 13;
  76. private static final int KEY_DELETE_CODE = 101;
  77. private static final int SINGLE_ACCOUNT = 1;
  78. private static final int MIN_MULTI_ACCOUNT_SIZE = 2;
  79. private ListView mListView;
  80. private final Handler mHandler = new Handler();
  81. private String mAccountName;
  82. private AccountListAdapter mAccountListAdapter;
  83. private ServiceConnection mDownloadServiceConnection;
  84. private ServiceConnection mUploadServiceConnection;
  85. private Set<String> mOriginalAccounts;
  86. private String mOriginalCurrentAccount;
  87. private Drawable mTintedCheck;
  88. private ArbitraryDataProvider arbitraryDataProvider;
  89. @Override
  90. protected void onCreate(Bundle savedInstanceState) {
  91. super.onCreate(savedInstanceState);
  92. mTintedCheck = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.account_circle_white));
  93. int tint = ThemeUtils.elementColor(this);
  94. DrawableCompat.setTint(mTintedCheck, tint);
  95. setContentView(R.layout.accounts_layout);
  96. mListView = findViewById(R.id.account_list);
  97. setupToolbar();
  98. updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts));
  99. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  100. mOriginalAccounts = DisplayUtils.toAccountNameSet(Arrays.asList(accountList));
  101. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  102. if (currentAccount != null) {
  103. mOriginalCurrentAccount = currentAccount.name;
  104. }
  105. setAccount(currentAccount);
  106. onAccountSet(false);
  107. arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  108. mAccountListAdapter = new AccountListAdapter(this, getAccountListItems(), mTintedCheck);
  109. mListView.setAdapter(mAccountListAdapter);
  110. final Intent intent = new Intent(this, UserInfoActivity.class);
  111. mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  112. @Override
  113. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  114. AccountListItem item = mAccountListAdapter.getItem(position);
  115. if (item != null && item.isEnabled()) {
  116. Account account = item.getAccount();
  117. intent.putExtra(UserInfoActivity.KEY_ACCOUNT, Parcels.wrap(account));
  118. try {
  119. OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext());
  120. intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName());
  121. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  122. Log_OC.d(TAG, "Failed to find NC account");
  123. }
  124. startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
  125. }
  126. }
  127. });
  128. initializeComponentGetters();
  129. }
  130. @Override
  131. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  132. super.onActivityResult(requestCode, resultCode, data);
  133. switch (resultCode) {
  134. case KEY_DELETE_CODE:
  135. if (data != null) {
  136. Bundle bundle = data.getExtras();
  137. if (bundle.containsKey(UserInfoActivity.KEY_ACCOUNT)) {
  138. Account account = Parcels.unwrap(bundle.getParcelable(UserInfoActivity.KEY_ACCOUNT));
  139. mAccountName = account.name;
  140. performAccountRemoval(account);
  141. }
  142. }
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. @Override
  149. public void onBackPressed() {
  150. Intent resultIntent = new Intent();
  151. resultIntent.putExtra(KEY_ACCOUNT_LIST_CHANGED, hasAccountListChanged());
  152. resultIntent.putExtra(KEY_CURRENT_ACCOUNT_CHANGED, hasCurrentAccountChanged());
  153. setResult(RESULT_OK, resultIntent);
  154. super.onBackPressed();
  155. }
  156. /**
  157. * checks the set of actual accounts against the set of original accounts when the activity has been started.
  158. *
  159. * @return true if account list has changed, false if not
  160. */
  161. private boolean hasAccountListChanged() {
  162. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  163. ArrayList<Account> newList = new ArrayList<>();
  164. for (Account account : accountList) {
  165. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account, PENDING_FOR_REMOVAL);
  166. if (!pendingForRemoval) {
  167. newList.add(account);
  168. }
  169. }
  170. Set<String> actualAccounts = DisplayUtils.toAccountNameSet(newList);
  171. return !mOriginalAccounts.equals(actualAccounts);
  172. }
  173. /**
  174. * checks actual current account against current accounts when the activity has been started.
  175. *
  176. * @return true if account list has changed, false if not
  177. */
  178. private boolean hasCurrentAccountChanged() {
  179. Account account = AccountUtils.getCurrentOwnCloudAccount(this);
  180. if (account == null) {
  181. return true;
  182. } else {
  183. return !account.name.equals(mOriginalCurrentAccount);
  184. }
  185. }
  186. /**
  187. * Initialize ComponentsGetters.
  188. */
  189. private void initializeComponentGetters() {
  190. mDownloadServiceConnection = newTransferenceServiceConnection();
  191. if (mDownloadServiceConnection != null) {
  192. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  193. Context.BIND_AUTO_CREATE);
  194. }
  195. mUploadServiceConnection = newTransferenceServiceConnection();
  196. if (mUploadServiceConnection != null) {
  197. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  198. Context.BIND_AUTO_CREATE);
  199. }
  200. }
  201. /**
  202. * creates the account list items list including the add-account action in case multiaccount_support is enabled.
  203. *
  204. * @return list of account list items
  205. */
  206. private List<AccountListItem> getAccountListItems() {
  207. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  208. List<AccountListItem> adapterAccountList = new ArrayList<>(accountList.length);
  209. for (Account account : accountList) {
  210. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account, PENDING_FOR_REMOVAL);
  211. adapterAccountList.add(new AccountListItem(account, !pendingForRemoval));
  212. }
  213. // Add Create Account item at the end of account list if multi-account is enabled
  214. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  215. adapterAccountList.add(new AccountListItem());
  216. }
  217. return adapterAccountList;
  218. }
  219. @Override
  220. public boolean onOptionsItemSelected(MenuItem item) {
  221. boolean retval = true;
  222. switch (item.getItemId()) {
  223. case android.R.id.home:
  224. onBackPressed();
  225. break;
  226. default:
  227. retval = super.onOptionsItemSelected(item);
  228. break;
  229. }
  230. return retval;
  231. }
  232. @Override
  233. public void showFirstRunActivity() {
  234. Intent firstRunIntent = new Intent(getApplicationContext(), FirstRunActivity.class);
  235. firstRunIntent.putExtra(FirstRunActivity.EXTRA_ALLOW_CLOSE, true);
  236. startActivity(firstRunIntent);
  237. }
  238. @Override
  239. public void createAccount() {
  240. AccountManager am = AccountManager.get(getApplicationContext());
  241. am.addAccount(MainApp.getAccountType(this),
  242. null,
  243. null,
  244. null,
  245. this,
  246. new AccountManagerCallback<Bundle>() {
  247. @Override
  248. public void run(AccountManagerFuture<Bundle> future) {
  249. if (future != null) {
  250. try {
  251. Bundle result = future.getResult();
  252. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  253. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
  254. mAccountListAdapter = new AccountListAdapter(
  255. ManageAccountsActivity.this,
  256. getAccountListItems(),
  257. mTintedCheck
  258. );
  259. mListView.setAdapter(mAccountListAdapter);
  260. runOnUiThread(new Runnable() {
  261. @Override
  262. public void run() {
  263. mAccountListAdapter.notifyDataSetChanged();
  264. }
  265. });
  266. } catch (OperationCanceledException e) {
  267. Log_OC.d(TAG, "Account creation canceled");
  268. } catch (Exception e) {
  269. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  270. }
  271. }
  272. }
  273. }, mHandler);
  274. }
  275. @Subscribe(threadMode = ThreadMode.MAIN)
  276. public void onAccountRemovedEvent(AccountRemovedEvent event) {
  277. List<AccountListItem> accountListItemArray = getAccountListItems();
  278. mAccountListAdapter.clear();
  279. mAccountListAdapter.addAll(accountListItemArray);
  280. mAccountListAdapter.notifyDataSetChanged();
  281. }
  282. @Override
  283. public void run(AccountManagerFuture<Boolean> future) {
  284. if (future.isDone()) {
  285. // after remove account
  286. Account account = new Account(mAccountName, MainApp.getAccountType(this));
  287. if (!AccountUtils.exists(account, MainApp.getAppContext())) {
  288. // Cancel transfers of the removed account
  289. if (mUploaderBinder != null) {
  290. mUploaderBinder.cancel(account);
  291. }
  292. if (mDownloaderBinder != null) {
  293. mDownloaderBinder.cancel(account);
  294. }
  295. }
  296. if (AccountUtils.getCurrentOwnCloudAccount(this) == null) {
  297. String accountName = "";
  298. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  299. if (accounts.length != 0) {
  300. accountName = accounts[0].name;
  301. }
  302. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  303. }
  304. List<AccountListItem> accountListItemArray = getAccountListItems();
  305. if (accountListItemArray.size() > SINGLE_ACCOUNT) {
  306. mAccountListAdapter = new AccountListAdapter(this, accountListItemArray, mTintedCheck);
  307. mListView.setAdapter(mAccountListAdapter);
  308. } else {
  309. onBackPressed();
  310. }
  311. }
  312. }
  313. @Override
  314. protected void onDestroy() {
  315. if (mDownloadServiceConnection != null) {
  316. unbindService(mDownloadServiceConnection);
  317. mDownloadServiceConnection = null;
  318. }
  319. if (mUploadServiceConnection != null) {
  320. unbindService(mUploadServiceConnection);
  321. mUploadServiceConnection = null;
  322. }
  323. super.onDestroy();
  324. }
  325. public Handler getHandler() { return mHandler; }
  326. @Override
  327. public FileUploader.FileUploaderBinder getFileUploaderBinder() {
  328. return mUploaderBinder;
  329. }
  330. @Override
  331. public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
  332. return null;
  333. }
  334. @Override
  335. public FileDataStorageManager getStorageManager() {
  336. return super.getStorageManager();
  337. }
  338. @Override
  339. public FileOperationsHelper getFileOperationsHelper() {
  340. return null;
  341. }
  342. protected ServiceConnection newTransferenceServiceConnection() {
  343. return new ManageAccountsServiceConnection();
  344. }
  345. private void performAccountRemoval(Account account) {
  346. // disable account in list view
  347. for (int i = 0; i < mAccountListAdapter.getCount(); i++) {
  348. AccountListItem item = mAccountListAdapter.getItem(i);
  349. if (item != null && item.getAccount().equals(account)) {
  350. item.setEnabled(false);
  351. break;
  352. }
  353. mAccountListAdapter.notifyDataSetChanged();
  354. }
  355. // store pending account removal
  356. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  357. arbitraryDataProvider.storeOrUpdateKeyValue(account.name, PENDING_FOR_REMOVAL, String.valueOf(true));
  358. // Cancel transfers
  359. if (mUploaderBinder != null) {
  360. mUploaderBinder.cancel(account);
  361. }
  362. if (mDownloaderBinder != null) {
  363. mDownloaderBinder.cancel(account);
  364. }
  365. // schedule job
  366. PersistableBundleCompat bundle = new PersistableBundleCompat();
  367. bundle.putString(AccountRemovalJob.ACCOUNT, account.name);
  368. new JobRequest.Builder(AccountRemovalJob.TAG)
  369. .startNow()
  370. .setExtras(bundle)
  371. .setUpdateCurrent(false)
  372. .build()
  373. .schedule();
  374. // immediately select a new account
  375. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType(this));
  376. String newAccountName = "";
  377. for (Account acc: accounts) {
  378. if (!account.name.equalsIgnoreCase(acc.name)) {
  379. newAccountName = acc.name;
  380. break;
  381. }
  382. }
  383. if (newAccountName.isEmpty()) {
  384. Log_OC.d(TAG, "new account set to null");
  385. AccountUtils.resetOwnCloudAccount(this);
  386. } else {
  387. Log_OC.d(TAG, "new account set to: " + newAccountName);
  388. AccountUtils.setCurrentOwnCloudAccount(this, newAccountName);
  389. }
  390. // only one to be (deleted) account remaining
  391. if (accounts.length < MIN_MULTI_ACCOUNT_SIZE) {
  392. Intent resultIntent = new Intent();
  393. resultIntent.putExtra(KEY_ACCOUNT_LIST_CHANGED, true);
  394. resultIntent.putExtra(KEY_CURRENT_ACCOUNT_CHANGED, true);
  395. setResult(RESULT_OK, resultIntent);
  396. super.onBackPressed();
  397. }
  398. }
  399. /**
  400. * Defines callbacks for service binding, passed to bindService()
  401. */
  402. private class ManageAccountsServiceConnection implements ServiceConnection {
  403. @Override
  404. public void onServiceConnected(ComponentName component, IBinder service) {
  405. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  406. mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
  407. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  408. Log_OC.d(TAG, "Upload service connected");
  409. mUploaderBinder = (FileUploader.FileUploaderBinder) service;
  410. }
  411. }
  412. @Override
  413. public void onServiceDisconnected(ComponentName component) {
  414. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  415. Log_OC.d(TAG, "Download service suddenly disconnected");
  416. mDownloaderBinder = null;
  417. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  418. Log_OC.d(TAG, "Upload service suddenly disconnected");
  419. mUploaderBinder = null;
  420. }
  421. }
  422. }
  423. }