ManageAccountsActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.support.v4.content.ContextCompat;
  34. import android.support.v4.graphics.drawable.DrawableCompat;
  35. import android.view.MenuItem;
  36. import android.view.View;
  37. import android.widget.AdapterView;
  38. import android.widget.ListView;
  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.FileDataStorageManager;
  43. import com.owncloud.android.files.services.FileDownloader;
  44. import com.owncloud.android.files.services.FileUploader;
  45. import com.owncloud.android.lib.common.OwnCloudAccount;
  46. import com.owncloud.android.lib.common.utils.Log_OC;
  47. import com.owncloud.android.services.OperationsService;
  48. import com.owncloud.android.ui.adapter.AccountListAdapter;
  49. import com.owncloud.android.ui.adapter.AccountListItem;
  50. import com.owncloud.android.ui.helpers.FileOperationsHelper;
  51. import com.owncloud.android.utils.AnalyticsUtils;
  52. import com.owncloud.android.utils.DisplayUtils;
  53. import org.parceler.Parcels;
  54. import java.util.ArrayList;
  55. import java.util.Set;
  56. /**
  57. * An Activity that allows the user to manage accounts.
  58. */
  59. public class ManageAccountsActivity extends FileActivity
  60. implements AccountListAdapter.AccountListAdapterListener, AccountManagerCallback<Boolean>, ComponentsGetter {
  61. private static final String TAG = ManageAccountsActivity.class.getSimpleName();
  62. public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED";
  63. public static final String KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED";
  64. private static final String KEY_ACCOUNT = "ACCOUNT";
  65. private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
  66. private static final int KEY_USER_INFO_REQUEST_CODE = 13;
  67. private static final int KEY_DELETE_CODE = 101;
  68. private ListView mListView;
  69. private final Handler mHandler = new Handler();
  70. private String mAccountName;
  71. private AccountListAdapter mAccountListAdapter;
  72. private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
  73. Set<String> mOriginalAccounts;
  74. String mOriginalCurrentAccount;
  75. private Drawable mTintedCheck;
  76. private static final String SCREEN_NAME = "Logs";
  77. @Override
  78. protected void onCreate(Bundle savedInstanceState) {
  79. super.onCreate(savedInstanceState);
  80. mTintedCheck = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.ic_account_circle_white_18dp));
  81. int tint = ContextCompat.getColor(this, R.color.primary);
  82. DrawableCompat.setTint(mTintedCheck, tint);
  83. setContentView(R.layout.accounts_layout);
  84. mListView = (ListView) findViewById(R.id.account_list);
  85. setupToolbar();
  86. updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts));
  87. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  88. mOriginalAccounts = DisplayUtils.toAccountNameSet(accountList);
  89. mOriginalCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(this).name;
  90. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  91. onAccountSet(false);
  92. mAccountListAdapter = new AccountListAdapter(this, getAccountListItems(), mTintedCheck);
  93. mListView.setAdapter(mAccountListAdapter);
  94. final Intent intent = new Intent(this, UserInfoActivity.class);
  95. mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  96. @Override
  97. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  98. Account account = mAccountListAdapter.getItem(position).getAccount();
  99. intent.putExtra(KEY_ACCOUNT, Parcels.wrap(account));
  100. try {
  101. OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext());
  102. intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName());
  103. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  104. Log_OC.d(TAG, "Failed to find NC account");
  105. }
  106. startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
  107. }
  108. });
  109. initializeComponentGetters();
  110. }
  111. @Override
  112. protected void onResume() {
  113. super.onResume();
  114. AnalyticsUtils.setCurrentScreenName(this, SCREEN_NAME, TAG);
  115. }
  116. @Override
  117. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  118. super.onActivityResult(requestCode, resultCode, data);
  119. switch (resultCode) {
  120. case KEY_DELETE_CODE:
  121. if (data != null) {
  122. Bundle bundle = data.getExtras();
  123. if (bundle.containsKey(KEY_ACCOUNT)) {
  124. Account account = Parcels.unwrap(bundle.getParcelable(KEY_ACCOUNT));
  125. mAccountName = account.name;
  126. performAccountRemoval(account);
  127. }
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. @Override
  135. public void onBackPressed() {
  136. Intent resultIntent = new Intent();
  137. resultIntent.putExtra(KEY_ACCOUNT_LIST_CHANGED, hasAccountListChanged());
  138. resultIntent.putExtra(KEY_CURRENT_ACCOUNT_CHANGED, hasCurrentAccountChanged());
  139. setResult(RESULT_OK, resultIntent);
  140. super.onBackPressed();
  141. }
  142. /**
  143. * checks the set of actual accounts against the set of original accounts when the activity has been started.
  144. *
  145. * @return <code>true</code> if aacount list has changed, <code>false</code> if not
  146. */
  147. private boolean hasAccountListChanged() {
  148. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  149. Set<String> actualAccounts = DisplayUtils.toAccountNameSet(accountList);
  150. return !mOriginalAccounts.equals(actualAccounts);
  151. }
  152. /**
  153. * checks actual current account against current accounts when the activity has been started.
  154. *
  155. * @return <code>true</code> if aacount list has changed, <code>false</code> if not
  156. */
  157. private boolean hasCurrentAccountChanged() {
  158. Account account = AccountUtils.getCurrentOwnCloudAccount(this);
  159. if (account == null) {
  160. return true;
  161. } else {
  162. return !mOriginalCurrentAccount.equals(account.name);
  163. }
  164. }
  165. /**
  166. * Initialize ComponentsGetters.
  167. */
  168. private void initializeComponentGetters() {
  169. mDownloadServiceConnection = newTransferenceServiceConnection();
  170. if (mDownloadServiceConnection != null) {
  171. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  172. Context.BIND_AUTO_CREATE);
  173. }
  174. mUploadServiceConnection = newTransferenceServiceConnection();
  175. if (mUploadServiceConnection != null) {
  176. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  177. Context.BIND_AUTO_CREATE);
  178. }
  179. }
  180. /**
  181. * creates the account list items list including the add-account action in case multiaccount_support is enabled.
  182. *
  183. * @return list of account list items
  184. */
  185. private ArrayList<AccountListItem> getAccountListItems() {
  186. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  187. ArrayList<AccountListItem> adapterAccountList = new ArrayList<>(accountList.length);
  188. for (Account account : accountList) {
  189. adapterAccountList.add(new AccountListItem(account));
  190. }
  191. // Add Create Account item at the end of account list if multi-account is enabled
  192. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  193. adapterAccountList.add(new AccountListItem());
  194. }
  195. return adapterAccountList;
  196. }
  197. @Override
  198. public boolean onOptionsItemSelected(MenuItem item) {
  199. boolean retval = true;
  200. switch (item.getItemId()) {
  201. case android.R.id.home:
  202. onBackPressed();
  203. break;
  204. default:
  205. retval = super.onOptionsItemSelected(item);
  206. break;
  207. }
  208. return retval;
  209. }
  210. @Override
  211. public void createAccount() {
  212. AccountManager am = AccountManager.get(getApplicationContext());
  213. am.addAccount(MainApp.getAccountType(),
  214. null,
  215. null,
  216. null,
  217. this,
  218. new AccountManagerCallback<Bundle>() {
  219. @Override
  220. public void run(AccountManagerFuture<Bundle> future) {
  221. if (future != null) {
  222. try {
  223. Bundle result = future.getResult();
  224. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  225. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
  226. mAccountListAdapter = new AccountListAdapter(
  227. ManageAccountsActivity.this,
  228. getAccountListItems(),
  229. mTintedCheck
  230. );
  231. mListView.setAdapter(mAccountListAdapter);
  232. runOnUiThread(new Runnable() {
  233. @Override
  234. public void run() {
  235. mAccountListAdapter.notifyDataSetChanged();
  236. }
  237. });
  238. } catch (OperationCanceledException e) {
  239. Log_OC.d(TAG, "Account creation canceled");
  240. } catch (Exception e) {
  241. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  242. }
  243. }
  244. }
  245. }, mHandler);
  246. }
  247. @Override
  248. public void run(AccountManagerFuture<Boolean> future) {
  249. if (future.isDone()) {
  250. // after remove account
  251. Account account = new Account(mAccountName, MainApp.getAccountType());
  252. if (!AccountUtils.exists(account, MainApp.getAppContext())) {
  253. // Cancel transfers of the removed account
  254. if (mUploaderBinder != null) {
  255. mUploaderBinder.cancel(account);
  256. }
  257. if (mDownloaderBinder != null) {
  258. mDownloaderBinder.cancel(account);
  259. }
  260. }
  261. if (AccountUtils.getCurrentOwnCloudAccount(this) == null) {
  262. String accountName = "";
  263. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  264. if (accounts.length != 0) {
  265. accountName = accounts[0].name;
  266. }
  267. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  268. }
  269. ArrayList<AccountListItem> accountListItemArray = getAccountListItems();
  270. if (accountListItemArray.size() > 1) {
  271. mAccountListAdapter = new AccountListAdapter(this, accountListItemArray, mTintedCheck);
  272. mListView.setAdapter(mAccountListAdapter);
  273. } else {
  274. onBackPressed();
  275. }
  276. }
  277. }
  278. @Override
  279. protected void onDestroy() {
  280. if (mDownloadServiceConnection != null) {
  281. unbindService(mDownloadServiceConnection);
  282. mDownloadServiceConnection = null;
  283. }
  284. if (mUploadServiceConnection != null) {
  285. unbindService(mUploadServiceConnection);
  286. mUploadServiceConnection = null;
  287. }
  288. super.onDestroy();
  289. }
  290. public Handler getHandler() { return mHandler; }
  291. @Override
  292. public FileUploader.FileUploaderBinder getFileUploaderBinder() {
  293. return mUploaderBinder;
  294. }
  295. @Override
  296. public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
  297. return null;
  298. }
  299. @Override
  300. public FileDataStorageManager getStorageManager() {
  301. return super.getStorageManager();
  302. }
  303. @Override
  304. public FileOperationsHelper getFileOperationsHelper() {
  305. return null;
  306. }
  307. protected ServiceConnection newTransferenceServiceConnection() {
  308. return new ManageAccountsServiceConnection();
  309. }
  310. private void performAccountRemoval(Account account) {
  311. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  312. am.removeAccount(account, this, this.getHandler());
  313. }
  314. /**
  315. * Defines callbacks for service binding, passed to bindService()
  316. */
  317. private class ManageAccountsServiceConnection implements ServiceConnection {
  318. @Override
  319. public void onServiceConnected(ComponentName component, IBinder service) {
  320. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  321. mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
  322. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  323. Log_OC.d(TAG, "Upload service connected");
  324. mUploaderBinder = (FileUploader.FileUploaderBinder) service;
  325. }
  326. }
  327. @Override
  328. public void onServiceDisconnected(ComponentName component) {
  329. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  330. Log_OC.d(TAG, "Download service suddenly disconnected");
  331. mDownloaderBinder = null;
  332. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  333. Log_OC.d(TAG, "Upload service suddenly disconnected");
  334. mUploaderBinder = null;
  335. }
  336. }
  337. }
  338. }