ManageAccountsActivity.java 15 KB

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