diff --git a/src/main/java/com/owncloud/android/authentication/AccountUtils.java b/src/main/java/com/owncloud/android/authentication/AccountUtils.java index 2dd480feb6b7..0ae1db823d7b 100644 --- a/src/main/java/com/owncloud/android/authentication/AccountUtils.java +++ b/src/main/java/com/owncloud/android/authentication/AccountUtils.java @@ -28,12 +28,14 @@ import android.preference.PreferenceManager; import com.owncloud.android.MainApp; +import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.lib.common.accounts.AccountTypeUtils; import com.owncloud.android.lib.common.accounts.AccountUtils.Constants; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.status.OwnCloudVersion; +import com.owncloud.android.ui.activity.ManageAccountsActivity; import com.owncloud.android.operations.GetCapabilitiesOperarion; import java.util.Locale; @@ -63,10 +65,10 @@ public static Account getCurrentOwnCloudAccount(Context context) { Account[] ocAccounts = getAccounts(context); Account defaultAccount = null; - SharedPreferences appPreferences = PreferenceManager - .getDefaultSharedPreferences(context); - String accountName = appPreferences - .getString("select_oc_account", null); + ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver()); + + SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context); + String accountName = appPreferences.getString("select_oc_account", null); // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager if (accountName != null) { @@ -77,10 +79,18 @@ public static Account getCurrentOwnCloudAccount(Context context) { } } } - - if (defaultAccount == null && ocAccounts.length != 0) { - // take first account as fallback - defaultAccount = ocAccounts[0]; + + if (defaultAccount == null && ocAccounts.length > 0) { + // take first which is not pending for removal account as fallback + for (Account account: ocAccounts) { + boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account, + ManageAccountsActivity.PENDING_FOR_REMOVAL); + + if (!pendingForRemoval) { + defaultAccount = account; + break; + } + } } return defaultAccount; @@ -155,8 +165,7 @@ public static boolean setCurrentOwnCloudAccount(final Context context, String ac for (final Account account : getAccounts(context)) { found = (account.name.equals(accountName)); if (found) { - SharedPreferences.Editor appPrefs = PreferenceManager - .getDefaultSharedPreferences(context).edit(); + SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit(); appPrefs.putString("select_oc_account", accountName); // update credentials @@ -182,6 +191,13 @@ public void run() { return result; } + public static void resetOwnCloudAccount(Context context) { + SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit(); + appPrefs.putString("select_oc_account", null); + + appPrefs.apply(); + } + /** * Returns the proper URL path to access the WebDAV interface of an ownCloud server, * according to its version and the authorization method used. diff --git a/src/main/java/com/owncloud/android/services/AccountRemovalJob.java b/src/main/java/com/owncloud/android/services/AccountRemovalJob.java new file mode 100644 index 000000000000..0414f6b59043 --- /dev/null +++ b/src/main/java/com/owncloud/android/services/AccountRemovalJob.java @@ -0,0 +1,93 @@ +/* + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2017 Tobias Kaminsky + * Copyright (C) 2017 Nextcloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.services; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.accounts.AccountManagerCallback; +import android.accounts.AccountManagerFuture; +import android.content.Context; +import android.support.annotation.NonNull; + +import com.evernote.android.job.Job; +import com.evernote.android.job.util.support.PersistableBundleCompat; +import com.owncloud.android.MainApp; +import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.datamodel.ArbitraryDataProvider; +import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.ui.events.AccountRemovedEvent; +import com.owncloud.android.utils.FileStorageUtils; + +import org.greenrobot.eventbus.EventBus; + +import java.io.File; + +import static android.content.Context.ACCOUNT_SERVICE; +import static com.owncloud.android.ui.activity.ManageAccountsActivity.PENDING_FOR_REMOVAL; + +/** + * Removes account and all local files + */ + +public class AccountRemovalJob extends Job implements AccountManagerCallback { + public static final String TAG = "AccountRemovalJob"; + public static final String ACCOUNT = "account"; + + @NonNull + @Override + protected Result onRunJob(Params params) { + Context context = MainApp.getAppContext(); + PersistableBundleCompat bundle = params.getExtras(); + Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT, "")); + + if (account != null ) { + AccountManager am = (AccountManager) context.getSystemService(ACCOUNT_SERVICE); + am.removeAccount(account, this, null); + + FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver()); + + File tempDir = new File(FileStorageUtils.getTemporalPath(account.name)); + File saveDir = new File(FileStorageUtils.getSavePath(account.name)); + + FileStorageUtils.deleteRecursively(tempDir, storageManager); + FileStorageUtils.deleteRecursively(saveDir, storageManager); + + // delete all database entries + storageManager.deleteAllFiles(); + + // remove pending account removal + ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver()); + arbitraryDataProvider.deleteKeyForAccount(account, PENDING_FOR_REMOVAL); + + return Result.SUCCESS; + } else { + return Result.FAILURE; + } + } + + @Override + public void run(AccountManagerFuture future) { + if (future.isDone()) { + EventBus.getDefault().post(new AccountRemovedEvent()); + } + } +} diff --git a/src/main/java/com/owncloud/android/services/NCJobCreator.java b/src/main/java/com/owncloud/android/services/NCJobCreator.java index db9ef4dbb148..c3d2cd418256 100644 --- a/src/main/java/com/owncloud/android/services/NCJobCreator.java +++ b/src/main/java/com/owncloud/android/services/NCJobCreator.java @@ -37,6 +37,8 @@ public Job create(String tag) { return new ContactsBackupJob(); case ContactsImportJob.TAG: return new ContactsImportJob(); + case AccountRemovalJob.TAG: + return new AccountRemovalJob(); default: return null; } diff --git a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 8ad598e3ebdc..e217d9b627d2 100644 --- a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -54,6 +54,7 @@ import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ExternalLinksProvider; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.common.ExternalLink; @@ -69,6 +70,7 @@ import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; import com.owncloud.android.operations.GetCapabilitiesOperarion; import com.owncloud.android.ui.TextDrawable; +import com.owncloud.android.ui.events.AccountRemovedEvent; import com.owncloud.android.ui.events.ChangeMenuEvent; import com.owncloud.android.ui.events.DummyDrawerEvent; import com.owncloud.android.ui.events.MenuItemClickEvent; @@ -182,6 +184,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU private ExternalLinksProvider externalLinksProvider; private SharedPreferences sharedPreferences; + private ArbitraryDataProvider arbitraryDataProvider; /** * Initializes the drawer, its content and highlights the menu item with the given id. @@ -605,7 +608,7 @@ public void setDrawerLockMode(int lockMode) { /** * Enable or disable the drawer indicator. * - * @param enable true to enable, false to disable + * @param enable true to enable, false to disable */ public void setDrawerIndicatorEnabled(boolean enable) { if (mDrawerToggle != null) { @@ -618,9 +621,21 @@ public void setDrawerIndicatorEnabled(boolean enable) { */ public void updateAccountList() { Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType()); + + ArrayList persistingAccounts = new ArrayList<>(); + + for (Account acc: accounts) { + boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(acc, + ManageAccountsActivity.PENDING_FOR_REMOVAL); + + if (!pendingForRemoval) { + persistingAccounts.add(acc); + } + } + if (mNavigationView != null && mDrawerLayout != null) { - if (accounts.length > 0) { - repopulateAccountList(accounts); + if (persistingAccounts.size() > 0) { + repopulateAccountList(persistingAccounts); setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this)); populateDrawerOwnCloudAccounts(); @@ -655,25 +670,24 @@ mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(), * * @param accounts list of accounts */ - private void repopulateAccountList(Account[] accounts) { + private void repopulateAccountList(ArrayList accounts) { // remove all accounts from list mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts); // add all accounts to list - for (int i = 0; i < accounts.length; i++) { + for (Account account: accounts) { try { - // show all accounts except the currently active one - if (!getAccount().name.equals(accounts[i].name)) { + // show all accounts except the currently active one and those pending for removal + + if (!getAccount().name.equals(account.name)) { MenuItem accountMenuItem = mNavigationView.getMenu().add( R.id.drawer_menu_accounts, Menu.NONE, MENU_ORDER_ACCOUNT, - accounts[i].name) - .setIcon(TextDrawable.createAvatar( - accounts[i].name, - mMenuAccountAvatarRadiusDimension) - ); - DisplayUtils.setAvatar(accounts[i], this, mMenuAccountAvatarRadiusDimension, getResources(), getStorageManager(), accountMenuItem); + account.name) + .setIcon(TextDrawable.createAvatar(account.name, mMenuAccountAvatarRadiusDimension)); + DisplayUtils.setAvatar(account, this, mMenuAccountAvatarRadiusDimension, getResources(), + getStorageManager(), accountMenuItem); } } catch (Exception e) { Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e); @@ -681,7 +695,7 @@ private void repopulateAccountList(Account[] accounts) { R.id.drawer_menu_accounts, Menu.NONE, MENU_ORDER_ACCOUNT, - accounts[i].name) + account.name) .setIcon(R.drawable.ic_user); } } @@ -1056,6 +1070,7 @@ protected void onCreate(Bundle savedInstanceState) { .getDimension(R.dimen.nav_drawer_menu_avatar_radius); externalLinksProvider = new ExternalLinksProvider(MainApp.getAppContext().getContentResolver()); + arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver()); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); } @@ -1130,13 +1145,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { // update Account list and active account if Manage Account activity replies with // - ACCOUNT_LIST_CHANGED = true // - RESULT_OK - if (requestCode == ACTION_MANAGE_ACCOUNTS - && resultCode == RESULT_OK + if (requestCode == ACTION_MANAGE_ACCOUNTS && resultCode == RESULT_OK && data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) { // current account has changed if (data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) { setAccount(AccountUtils.getCurrentOwnCloudAccount(this)); + updateAccountList(); restart(); } else { updateAccountList(); @@ -1194,15 +1209,26 @@ protected void onAccountCreationSuccessful(AccountManagerFuture future) */ private void populateDrawerOwnCloudAccounts() { mAvatars = new Account[3]; - Account[] accountsAll = AccountManager.get(this).getAccountsByType - (MainApp.getAccountType()); + Account[] accountsAll = AccountManager.get(this).getAccountsByType(MainApp.getAccountType()); + + ArrayList persistingAccounts = new ArrayList<>(); + + for (Account acc: accountsAll) { + boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(acc, + ManageAccountsActivity.PENDING_FOR_REMOVAL); + + if (!pendingForRemoval) { + persistingAccounts.add(acc); + } + } + Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this); mAvatars[0] = currentAccount; int j = 0; - for (int i = 1; i <= 2 && i < accountsAll.length && j < accountsAll.length; j++) { - if (!currentAccount.equals(accountsAll[j])) { - mAvatars[i] = accountsAll[j]; + for (int i = 1; i <= 2 && i < persistingAccounts.size() && j < persistingAccounts.size(); j++) { + if (!currentAccount.equals(persistingAccounts.get(j))) { + mAvatars[i] = persistingAccounts.get(j); i++; } } @@ -1260,6 +1286,11 @@ protected void onStop() { super.onStop(); } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onAccountRemovedEvent(AccountRemovedEvent event) { + updateAccountList(); + } + /** * Retrieves external links via api from 'external' app */ diff --git a/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java b/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java index 690defa764f4..a60af3c7974b 100644 --- a/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java @@ -1,4 +1,4 @@ -/** +/* * ownCloud Android client application * * @author Andy Scherzinger @@ -29,7 +29,6 @@ import android.content.Intent; import android.content.ServiceConnection; import android.graphics.drawable.Drawable; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -40,27 +39,34 @@ import android.widget.AdapterView; import android.widget.ListView; +import com.evernote.android.job.JobRequest; +import com.evernote.android.job.util.support.PersistableBundleCompat; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.services.AccountRemovalJob; +import com.owncloud.android.services.AutoUploadJob; import com.owncloud.android.services.OperationsService; import com.owncloud.android.ui.adapter.AccountListAdapter; import com.owncloud.android.ui.adapter.AccountListItem; +import com.owncloud.android.ui.events.AccountRemovedEvent; import com.owncloud.android.ui.helpers.FileOperationsHelper; import com.owncloud.android.utils.AnalyticsUtils; import com.owncloud.android.utils.DisplayUtils; -import com.owncloud.android.utils.FileStorageUtils; import com.owncloud.android.utils.ThemeUtils; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; import org.parceler.Parcels; -import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Set; /** @@ -72,6 +78,8 @@ public class ManageAccountsActivity extends FileActivity public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"; public static final String KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"; + public static final String PENDING_FOR_REMOVAL = "PENDING_FOR_REMOVAL"; + private static final String KEY_ACCOUNT = "ACCOUNT"; private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME"; @@ -88,6 +96,7 @@ public class ManageAccountsActivity extends FileActivity private Drawable mTintedCheck; private static final String SCREEN_NAME = "Logs"; + private ArbitraryDataProvider arbitraryDataProvider; @Override protected void onCreate(Bundle savedInstanceState) { @@ -105,12 +114,14 @@ protected void onCreate(Bundle savedInstanceState) { updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts)); Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType()); - mOriginalAccounts = DisplayUtils.toAccountNameSet(accountList); + mOriginalAccounts = DisplayUtils.toAccountNameSet(Arrays.asList(accountList)); mOriginalCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(this).name; setAccount(AccountUtils.getCurrentOwnCloudAccount(this)); onAccountSet(false); + arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver()); + mAccountListAdapter = new AccountListAdapter(this, getAccountListItems(), mTintedCheck); mListView.setAdapter(mAccountListAdapter); @@ -120,16 +131,20 @@ protected void onCreate(Bundle savedInstanceState) { mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - Account account = mAccountListAdapter.getItem(position).getAccount(); - intent.putExtra(KEY_ACCOUNT, Parcels.wrap(account)); - try { - OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext()); - intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName()); - } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) { - Log_OC.d(TAG, "Failed to find NC account"); - } + AccountListItem item = mAccountListAdapter.getItem(position); + + if (item != null && item.isEnabled()) { + Account account = item.getAccount(); + intent.putExtra(KEY_ACCOUNT, Parcels.wrap(account)); + try { + OwnCloudAccount oca = new OwnCloudAccount(account, MainApp.getAppContext()); + intent.putExtra(KEY_DISPLAY_NAME, oca.getDisplayName()); + } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) { + Log_OC.d(TAG, "Failed to find NC account"); + } - startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE); + startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE); + } } }); @@ -174,18 +189,28 @@ public void onBackPressed() { /** * checks the set of actual accounts against the set of original accounts when the activity has been started. * - * @return true if aacount list has changed, false if not + * @return true if account list has changed, false if not */ private boolean hasAccountListChanged() { Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType()); - Set actualAccounts = DisplayUtils.toAccountNameSet(accountList); + + ArrayList newList = new ArrayList<>(); + for (Account account : accountList) { + boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account, PENDING_FOR_REMOVAL); + + if (!pendingForRemoval) { + newList.add(account); + } + } + + Set actualAccounts = DisplayUtils.toAccountNameSet(newList); return !mOriginalAccounts.equals(actualAccounts); } /** * checks actual current account against current accounts when the activity has been started. * - * @return true if aacount list has changed, false if not + * @return true if account list has changed, false if not */ private boolean hasCurrentAccountChanged() { Account account = AccountUtils.getCurrentOwnCloudAccount(this); @@ -221,7 +246,8 @@ private ArrayList getAccountListItems() { Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType()); ArrayList adapterAccountList = new ArrayList<>(accountList.length); for (Account account : accountList) { - adapterAccountList.add(new AccountListItem(account)); + boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account, PENDING_FOR_REMOVAL); + adapterAccountList.add(new AccountListItem(account, !pendingForRemoval)); } // Add Create Account item at the end of account list if multi-account is enabled @@ -284,6 +310,14 @@ public void run() { }, mHandler); } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onAccountRemovedEvent(AccountRemovedEvent event) { + ArrayList accountListItemArray = getAccountListItems(); + mAccountListAdapter.clear(); + mAccountListAdapter.addAll(accountListItemArray); + mAccountListAdapter.notifyDataSetChanged(); + } + @Override public void run(AccountManagerFuture future) { if (future.isDone()) { @@ -359,32 +393,70 @@ protected ServiceConnection newTransferenceServiceConnection() { } private void performAccountRemoval(Account account) { - AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE); - am.removeAccount(account, this, this.getHandler()); + // disable account in list view + for (int i = 0; i < mAccountListAdapter.getCount(); i++) { + AccountListItem item = mAccountListAdapter.getItem(i); - deleteAccountFiles(account); - } - - private void deleteAccountFiles(final Account account) { - AsyncTask removalTask = new AsyncTask() { - @Override - protected Object doInBackground(Object[] params) { - FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver()); + if (item != null && item.getAccount().equals(account)) { + item.setEnabled(false); + break; + } - File tempDir = new File(FileStorageUtils.getTemporalPath(account.name)); - File saveDir = new File(FileStorageUtils.getSavePath(account.name)); + mAccountListAdapter.notifyDataSetChanged(); + } - FileStorageUtils.deleteRecursively(tempDir, storageManager); - FileStorageUtils.deleteRecursively(saveDir, storageManager); + // store pending account removal + ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver()); + arbitraryDataProvider.storeOrUpdateKeyValue(account, PENDING_FOR_REMOVAL, String.valueOf(true)); - // delete all database entries - storageManager.deleteAllFiles(); + // Cancel transfers + if (mUploaderBinder != null) { + mUploaderBinder.cancel(account); + } + if (mDownloaderBinder != null) { + mDownloaderBinder.cancel(account); + } - return true; + // schedule job + PersistableBundleCompat bundle = new PersistableBundleCompat(); + bundle.putString(AutoUploadJob.ACCOUNT, account.name); + + new JobRequest.Builder(AccountRemovalJob.TAG) + .setExecutionWindow(1_000L, 10_000L) + .setExtras(bundle) + .setPersisted(false) + .setUpdateCurrent(false) + .build() + .schedule(); + + // immediately select a new account + Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType()); + + String newAccountName = ""; + for (Account acc: accounts) { + if (!account.name.equalsIgnoreCase(acc.name)) { + newAccountName = acc.name; + break; } - }; + } - removalTask.execute(); + if (newAccountName.isEmpty()) { + Log_OC.d(TAG, "new account set to null"); + AccountUtils.resetOwnCloudAccount(this); + } else { + Log_OC.d(TAG, "new account set to: " + newAccountName); + AccountUtils.setCurrentOwnCloudAccount(this, newAccountName); + } + + // only one to be (deleted) account remaining + if (accounts.length < 2) { + Intent resultIntent = new Intent(); + resultIntent.putExtra(KEY_ACCOUNT_LIST_CHANGED, true); + resultIntent.putExtra(KEY_CURRENT_ACCOUNT_CHANGED, true); + setResult(RESULT_OK, resultIntent); + + super.onBackPressed(); + } } /** diff --git a/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java index 55080efddb60..42e652e39c03 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java @@ -20,6 +20,7 @@ package com.owncloud.android.ui.adapter; import android.accounts.Account; +import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.view.LayoutInflater; @@ -93,6 +94,17 @@ public View getView(final int position, View convertView, ViewGroup parent) { setAvatar(viewHolder, account); setCurrentlyActiveState(viewHolder, account); + TextView usernameView = viewHolder.usernameViewItem; + TextView accountView = viewHolder.accountViewItem; + + if (!accountListItem.isEnabled()) { + usernameView.setPaintFlags(usernameView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); + accountView.setPaintFlags(accountView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); + } else { + usernameView.setPaintFlags(usernameView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); + accountView.setPaintFlags(accountView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); + } + } // create add account action item else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && mListener != null) { return setupAddAccountListItem(parent); diff --git a/src/main/java/com/owncloud/android/ui/adapter/AccountListItem.java b/src/main/java/com/owncloud/android/ui/adapter/AccountListItem.java index bd7c28ec5d7d..9b9bff86bf43 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/AccountListItem.java +++ b/src/main/java/com/owncloud/android/ui/adapter/AccountListItem.java @@ -30,6 +30,7 @@ public class AccountListItem { private Account mAccount; private int mType; + private boolean mEnabled; /** * creates an account list item containing an {@link Account}. @@ -39,6 +40,13 @@ public class AccountListItem { public AccountListItem(Account account) { mAccount = account; mType = TYPE_ACCOUNT; + mEnabled = true; + } + + public AccountListItem(Account account, boolean enabled) { + mAccount = account; + mType = TYPE_ACCOUNT; + mEnabled = enabled; } /** @@ -55,4 +63,12 @@ public Account getAccount() { public int getType() { return mType; } + + public void setEnabled(boolean bool) { + mEnabled = bool; + } + + public boolean isEnabled() { + return mEnabled; + } } diff --git a/src/main/java/com/owncloud/android/ui/events/AccountRemovedEvent.java b/src/main/java/com/owncloud/android/ui/events/AccountRemovedEvent.java new file mode 100644 index 000000000000..bde7870b7ba3 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/events/AccountRemovedEvent.java @@ -0,0 +1,30 @@ +/* + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2017 Tobias Kaminsky + * Copyright (C) 2017 Nextcloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.ui.events; + +/** + * Event that notifies that an account was removed + */ + +public class AccountRemovedEvent { + +} \ No newline at end of file diff --git a/src/main/java/com/owncloud/android/ui/events/TokenPushEvent.java b/src/main/java/com/owncloud/android/ui/events/TokenPushEvent.java index 4eae0187a236..8dc53ef7b4a7 100644 --- a/src/main/java/com/owncloud/android/ui/events/TokenPushEvent.java +++ b/src/main/java/com/owncloud/android/ui/events/TokenPushEvent.java @@ -1,4 +1,4 @@ -/** +/* * Nextcloud Android client application * * @author Mario Danic diff --git a/src/main/java/com/owncloud/android/utils/DisplayUtils.java b/src/main/java/com/owncloud/android/utils/DisplayUtils.java index cb260c268d5e..915fd049eb20 100644 --- a/src/main/java/com/owncloud/android/utils/DisplayUtils.java +++ b/src/main/java/com/owncloud/android/utils/DisplayUtils.java @@ -81,6 +81,7 @@ import java.math.BigDecimal; import java.net.IDN; import java.text.DateFormat; +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -278,8 +279,8 @@ public static String getAccountNameDisplayText(Context context, Account savedAcc * @param accountList the account array * @return set of account names */ - public static Set toAccountNameSet(Account[] accountList) { - Set actualAccounts = new HashSet<>(accountList.length); + public static Set toAccountNameSet(Collection accountList) { + Set actualAccounts = new HashSet<>(accountList.size()); for (Account account : accountList) { actualAccounts.add(account.name); }