ManageAccountsActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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.util.Log;
  36. import android.view.MenuItem;
  37. import android.view.View;
  38. import android.widget.AdapterView;
  39. import android.widget.ListView;
  40. import com.owncloud.android.MainApp;
  41. import com.owncloud.android.R;
  42. import com.owncloud.android.authentication.AccountUtils;
  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.lib.common.OwnCloudAccount;
  47. import com.owncloud.android.lib.common.utils.Log_OC;
  48. import com.owncloud.android.services.OperationsService;
  49. import com.owncloud.android.ui.adapter.AccountListAdapter;
  50. import com.owncloud.android.ui.adapter.AccountListItem;
  51. import com.owncloud.android.ui.helpers.FileOperationsHelper;
  52. import com.owncloud.android.utils.DisplayUtils;
  53. import org.parceler.Parcels;
  54. import java.util.ArrayList;
  55. import java.util.Set;
  56. import butterknife.OnItemClick;
  57. /**
  58. * An Activity that allows the user to manage accounts.
  59. */
  60. public class ManageAccountsActivity extends FileActivity
  61. implements AccountListAdapter.AccountListAdapterListener, AccountManagerCallback<Boolean>, ComponentsGetter {
  62. private static final String TAG = ManageAccountsActivity.class.getSimpleName();
  63. public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED";
  64. public static final String KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED";
  65. private static final String KEY_ACCOUNT = "ACCOUNT";
  66. private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";
  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. startActivity(intent);
  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. }
  125. }
  126. @Override
  127. public void onBackPressed() {
  128. Intent resultIntent = new Intent();
  129. resultIntent.putExtra(KEY_ACCOUNT_LIST_CHANGED, hasAccountListChanged());
  130. resultIntent.putExtra(KEY_CURRENT_ACCOUNT_CHANGED, hasCurrentAccountChanged());
  131. setResult(RESULT_OK, resultIntent);
  132. super.onBackPressed();
  133. }
  134. /**
  135. * checks the set of actual accounts against the set of original accounts when the activity has been started.
  136. *
  137. * @return <code>true</code> if aacount list has changed, <code>false</code> if not
  138. */
  139. private boolean hasAccountListChanged() {
  140. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  141. Set<String> actualAccounts = DisplayUtils.toAccountNameSet(accountList);
  142. return !mOriginalAccounts.equals(actualAccounts);
  143. }
  144. /**
  145. * checks actual current account against current accounts when the activity has been started.
  146. *
  147. * @return <code>true</code> if aacount list has changed, <code>false</code> if not
  148. */
  149. private boolean hasCurrentAccountChanged() {
  150. Account account = AccountUtils.getCurrentOwnCloudAccount(this);
  151. if (account == null) {
  152. return true;
  153. } else {
  154. return !mOriginalCurrentAccount.equals(account.name);
  155. }
  156. }
  157. /**
  158. * Initialize ComponentsGetters.
  159. */
  160. private void initializeComponentGetters() {
  161. mDownloadServiceConnection = newTransferenceServiceConnection();
  162. if (mDownloadServiceConnection != null) {
  163. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  164. Context.BIND_AUTO_CREATE);
  165. }
  166. mUploadServiceConnection = newTransferenceServiceConnection();
  167. if (mUploadServiceConnection != null) {
  168. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  169. Context.BIND_AUTO_CREATE);
  170. }
  171. }
  172. /**
  173. * creates the account list items list including the add-account action in case multiaccount_support is enabled.
  174. *
  175. * @return list of account list items
  176. */
  177. private ArrayList<AccountListItem> getAccountListItems() {
  178. Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  179. ArrayList<AccountListItem> adapterAccountList = new ArrayList<>(accountList.length);
  180. for (Account account : accountList) {
  181. adapterAccountList.add(new AccountListItem(account));
  182. }
  183. // Add Create Account item at the end of account list if multi-account is enabled
  184. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  185. adapterAccountList.add(new AccountListItem());
  186. }
  187. return adapterAccountList;
  188. }
  189. @Override
  190. public boolean onOptionsItemSelected(MenuItem item) {
  191. boolean retval = true;
  192. switch (item.getItemId()) {
  193. case android.R.id.home:
  194. onBackPressed();
  195. break;
  196. default:
  197. retval = super.onOptionsItemSelected(item);
  198. }
  199. return retval;
  200. }
  201. @Override
  202. public void createAccount() {
  203. AccountManager am = AccountManager.get(getApplicationContext());
  204. am.addAccount(MainApp.getAccountType(),
  205. null,
  206. null,
  207. null,
  208. this,
  209. new AccountManagerCallback<Bundle>() {
  210. @Override
  211. public void run(AccountManagerFuture<Bundle> future) {
  212. if (future != null) {
  213. try {
  214. Bundle result = future.getResult();
  215. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  216. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
  217. mAccountListAdapter = new AccountListAdapter(
  218. ManageAccountsActivity.this,
  219. getAccountListItems(),
  220. mTintedCheck
  221. );
  222. mListView.setAdapter(mAccountListAdapter);
  223. runOnUiThread(new Runnable() {
  224. @Override
  225. public void run() {
  226. mAccountListAdapter.notifyDataSetChanged();
  227. }
  228. });
  229. } catch (OperationCanceledException e) {
  230. Log_OC.d(TAG, "Account creation canceled");
  231. } catch (Exception e) {
  232. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  233. }
  234. }
  235. }
  236. }, mHandler);
  237. }
  238. @Override
  239. public void run(AccountManagerFuture<Boolean> future) {
  240. if (future.isDone()) {
  241. // after remove account
  242. Account account = new Account(mAccountName, MainApp.getAccountType());
  243. if (!AccountUtils.exists(account, MainApp.getAppContext())) {
  244. // Cancel transfers of the removed account
  245. if (mUploaderBinder != null) {
  246. mUploaderBinder.cancel(account);
  247. }
  248. if (mDownloaderBinder != null) {
  249. mDownloaderBinder.cancel(account);
  250. }
  251. }
  252. if (AccountUtils.getCurrentOwnCloudAccount(this) == null) {
  253. String accountName = "";
  254. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  255. if (accounts.length != 0) {
  256. accountName = accounts[0].name;
  257. }
  258. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  259. }
  260. ArrayList<AccountListItem> accountListItemArray = getAccountListItems();
  261. if (accountListItemArray.size() > 1) {
  262. mAccountListAdapter = new AccountListAdapter(this, accountListItemArray, mTintedCheck);
  263. mListView.setAdapter(mAccountListAdapter);
  264. } else {
  265. onBackPressed();
  266. }
  267. }
  268. }
  269. @Override
  270. protected void onDestroy() {
  271. if (mDownloadServiceConnection != null) {
  272. unbindService(mDownloadServiceConnection);
  273. mDownloadServiceConnection = null;
  274. }
  275. if (mUploadServiceConnection != null) {
  276. unbindService(mUploadServiceConnection);
  277. mUploadServiceConnection = null;
  278. }
  279. super.onDestroy();
  280. }
  281. public Handler getHandler() { return mHandler; }
  282. @Override
  283. public FileUploader.FileUploaderBinder getFileUploaderBinder() {
  284. return mUploaderBinder;
  285. }
  286. @Override
  287. public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
  288. return null;
  289. }
  290. @Override
  291. public FileDataStorageManager getStorageManager() {
  292. return super.getStorageManager();
  293. }
  294. @Override
  295. public FileOperationsHelper getFileOperationsHelper() {
  296. return null;
  297. }
  298. protected ServiceConnection newTransferenceServiceConnection() {
  299. return new ManageAccountsServiceConnection();
  300. }
  301. private void performAccountRemoval(Account account) {
  302. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  303. am.removeAccount(
  304. account,
  305. this,
  306. this.getHandler());
  307. }
  308. /**
  309. * Defines callbacks for service binding, passed to bindService()
  310. */
  311. private class ManageAccountsServiceConnection implements ServiceConnection {
  312. @Override
  313. public void onServiceConnected(ComponentName component, IBinder service) {
  314. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  315. mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
  316. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  317. Log_OC.d(TAG, "Upload service connected");
  318. mUploaderBinder = (FileUploader.FileUploaderBinder) service;
  319. }
  320. }
  321. @Override
  322. public void onServiceDisconnected(ComponentName component) {
  323. if (component.equals(new ComponentName(ManageAccountsActivity.this, FileDownloader.class))) {
  324. Log_OC.d(TAG, "Download service suddenly disconnected");
  325. mDownloaderBinder = null;
  326. } else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
  327. Log_OC.d(TAG, "Upload service suddenly disconnected");
  328. mUploaderBinder = null;
  329. }
  330. }
  331. }
  332. }