ManageAccountsActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.os.Bundle;
  30. import android.os.Handler;
  31. import android.os.IBinder;
  32. import android.support.annotation.NonNull;
  33. import android.view.MenuItem;
  34. import android.view.View;
  35. import android.widget.AdapterView;
  36. import android.widget.ListView;
  37. import com.owncloud.android.MainApp;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.authentication.AccountUtils;
  40. import com.owncloud.android.authentication.AuthenticatorActivity;
  41. import com.owncloud.android.datamodel.FileDataStorageManager;
  42. import com.owncloud.android.files.FileOperationsHelper;
  43. import com.owncloud.android.files.services.FileDownloader;
  44. import com.owncloud.android.files.services.FileUploader;
  45. import com.owncloud.android.lib.common.utils.Log_OC;
  46. import com.owncloud.android.services.OperationsService;
  47. import com.owncloud.android.ui.adapter.AccountListAdapter;
  48. import com.owncloud.android.ui.adapter.AccountListItem;
  49. import java.util.ArrayList;
  50. /**
  51. * Managing the accounts.
  52. */
  53. public class ManageAccountsActivity extends ToolbarActivity
  54. implements AccountListAdapter.AccountListAdapterListener, AccountManagerCallback<Boolean>, ComponentsGetter {
  55. private static final String TAG = ManageAccountsActivity.class.getSimpleName();
  56. private ListView mListView;
  57. private final Handler mHandler = new Handler();
  58. private String mAccountName;
  59. private AccountListAdapter mAccountListAdapter;
  60. protected FileUploader.FileUploaderBinder mUploaderBinder = null;
  61. protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
  62. private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.accounts_layout);
  67. mListView = (ListView) findViewById(R.id.account_list);
  68. setupToolbar();
  69. updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts));
  70. mAccountListAdapter = new AccountListAdapter(this, getAccountListItems());
  71. mListView.setAdapter(mAccountListAdapter);
  72. initializeComponentGetters();
  73. }
  74. /**
  75. * Initialize ComponentsGetters.
  76. */
  77. private void initializeComponentGetters() {
  78. mDownloadServiceConnection = newTransferenceServiceConnection();
  79. if (mDownloadServiceConnection != null) {
  80. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  81. Context.BIND_AUTO_CREATE);
  82. }
  83. mUploadServiceConnection = newTransferenceServiceConnection();
  84. if (mUploadServiceConnection != null) {
  85. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  86. Context.BIND_AUTO_CREATE);
  87. }
  88. }
  89. /**
  90. * creates the account list items list including the add-account action in case multiaccount_support is enabled.
  91. *
  92. * @return list of account list items
  93. */
  94. @NonNull
  95. private ArrayList<AccountListItem> getAccountListItems() {
  96. AccountManager am = (AccountManager) this.getSystemService(this.ACCOUNT_SERVICE);
  97. Account[] accountList = am.getAccountsByType(MainApp.getAccountType());
  98. ArrayList<AccountListItem> adapterAccountList = new ArrayList<AccountListItem>(accountList.length);
  99. for (Account account : accountList) {
  100. adapterAccountList.add(new AccountListItem(account));
  101. }
  102. // Add Create Account item at the end of account list if multi-account is enabled
  103. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  104. adapterAccountList.add(new AccountListItem());
  105. }
  106. return adapterAccountList;
  107. }
  108. @Override
  109. public boolean onOptionsItemSelected(MenuItem item) {
  110. boolean retval = true;
  111. switch (item.getItemId()) {
  112. case android.R.id.home:
  113. onBackPressed();
  114. break;
  115. default:
  116. retval = super.onOptionsItemSelected(item);
  117. }
  118. return retval;
  119. }
  120. @Override
  121. public void removeAccount(Account account) {
  122. AccountManager am = (AccountManager) this.getSystemService(ACCOUNT_SERVICE);
  123. mAccountName = account.name;
  124. am.removeAccount(account, ManageAccountsActivity.this, mHandler);
  125. Log_OC.d(TAG, "Remove an account " + account.name);
  126. }
  127. @Override
  128. public void changePasswordOfAccount(Account account) {
  129. Intent updateAccountCredentials = new Intent(ManageAccountsActivity.this, AuthenticatorActivity.class);
  130. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
  131. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  132. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  133. startActivity(updateAccountCredentials);
  134. }
  135. @Override
  136. public void createAccount() {
  137. AccountManager am = AccountManager.get(getApplicationContext());
  138. final AccountManagerFuture<Bundle> future = am.addAccount(MainApp.getAccountType(),
  139. null,
  140. null,
  141. null,
  142. this,
  143. new AccountManagerCallback<Bundle>() {
  144. @Override
  145. public void run(AccountManagerFuture<Bundle> future) {
  146. if (future != null) {
  147. try {
  148. Bundle result = future.getResult();
  149. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  150. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
  151. ArrayList<AccountListItem> accounts = getAccountListItems();
  152. mAccountListAdapter.clear();
  153. mAccountListAdapter.addAll(accounts);
  154. runOnUiThread(new Runnable() {
  155. @Override
  156. public void run() {
  157. mAccountListAdapter.notifyDataSetChanged();
  158. }
  159. });
  160. } catch (OperationCanceledException e) {
  161. Log_OC.d(TAG, "Account creation canceled");
  162. } catch (Exception e) {
  163. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  164. }
  165. }
  166. }
  167. }, mHandler);
  168. }
  169. @Override
  170. public void run(AccountManagerFuture<Boolean> future) {
  171. if (future.isDone()) {
  172. // after remove account
  173. Account account = new Account(mAccountName, MainApp.getAccountType());
  174. if (!AccountUtils.exists(account, MainApp.getAppContext())) {
  175. // Cancel transfers of the removed account
  176. if (mUploaderBinder != null) {
  177. mUploaderBinder.cancel(account);
  178. }
  179. if (mDownloaderBinder != null) {
  180. mDownloaderBinder.cancel(account);
  181. }
  182. }
  183. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  184. String accountName = "";
  185. if (a == null) {
  186. Account[] accounts = AccountManager.get(this)
  187. .getAccountsByType(MainApp.getAccountType());
  188. if (accounts.length != 0)
  189. accountName = accounts[0].name;
  190. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  191. }
  192. mAccountListAdapter = new AccountListAdapter(this, getAccountListItems());
  193. mAccountListAdapter.notifyDataSetChanged();
  194. }
  195. }
  196. /**
  197. * Helper class handling a callback from the {@link AccountManager} after the creation of
  198. * a new ownCloud {@link Account} finished, successfully or not.
  199. * <p/>
  200. * At this moment, only called after the creation of the first account.
  201. */
  202. public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
  203. @Override
  204. public void run(AccountManagerFuture<Bundle> future) {
  205. if (future != null) {
  206. try {
  207. Bundle result = future.getResult();
  208. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  209. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
  210. ArrayList<AccountListItem> accounts = getAccountListItems();
  211. ManageAccountsActivity.this.mAccountListAdapter = new AccountListAdapter(ManageAccountsActivity
  212. .this, accounts);
  213. ManageAccountsActivity.this.mAccountListAdapter.notifyDataSetChanged();
  214. } catch (OperationCanceledException e) {
  215. Log_OC.d(TAG, "Account creation canceled");
  216. } catch (Exception e) {
  217. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  218. }
  219. } else {
  220. Log_OC.e(TAG, "Account creation callback with null bundle");
  221. }
  222. }
  223. }
  224. @Override
  225. protected void onDestroy() {
  226. if (mDownloadServiceConnection != null) {
  227. unbindService(mDownloadServiceConnection);
  228. mDownloadServiceConnection = null;
  229. }
  230. if (mUploadServiceConnection != null) {
  231. unbindService(mUploadServiceConnection);
  232. mUploadServiceConnection = null;
  233. }
  234. super.onDestroy();
  235. }
  236. // Methods for ComponentsGetter
  237. @Override
  238. public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
  239. return mDownloaderBinder;
  240. }
  241. @Override
  242. public FileUploader.FileUploaderBinder getFileUploaderBinder() {
  243. return mUploaderBinder;
  244. }
  245. @Override
  246. public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
  247. return null;
  248. }
  249. @Override
  250. public FileDataStorageManager getStorageManager() {
  251. return null;
  252. }
  253. @Override
  254. public FileOperationsHelper getFileOperationsHelper() {
  255. return null;
  256. }
  257. protected ServiceConnection newTransferenceServiceConnection() {
  258. return new ManageAccountsServiceConnection();
  259. }
  260. /**
  261. * Defines callbacks for service binding, passed to bindService()
  262. */
  263. private class ManageAccountsServiceConnection implements ServiceConnection {
  264. @Override
  265. public void onServiceConnected(ComponentName component, IBinder service) {
  266. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  267. mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
  268. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  269. Log_OC.d(TAG, "Upload service connected");
  270. mUploaderBinder = (FileUploader.FileUploaderBinder) service;
  271. } else {
  272. return;
  273. }
  274. }
  275. @Override
  276. public void onServiceDisconnected(ComponentName component) {
  277. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  278. Log_OC.d(TAG, "Download service suddenly disconnected");
  279. mDownloaderBinder = null;
  280. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  281. Log_OC.d(TAG, "Upload service suddenly disconnected");
  282. mUploaderBinder = null;
  283. }
  284. }
  285. }
  286. ;
  287. }