Skip to content

Commit

Permalink
- strike out the account name (aka, you can't click it)
Browse files Browse the repository at this point in the history
- switch to other acc if it exists, if not show login screen
- run a job with all the delete magic
- prevent adding the same account while there is pending delete for that account
  • Loading branch information
tobiasKaminsky committed Jul 5, 2017
1 parent 8b1ed6d commit 5e4f458
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 72 deletions.
38 changes: 27 additions & 11 deletions src/main/java/com/owncloud/android/authentication/AccountUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import android.preference.PreferenceManager;

import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
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 java.util.Locale;

Expand Down Expand Up @@ -60,10 +62,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) {
Expand All @@ -74,10 +76,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;
Expand Down Expand Up @@ -152,10 +162,9 @@ public static boolean setCurrentOwnCloudAccount(Context context, String accountN
for (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);

appPrefs.apply();
result = true;
break;
Expand All @@ -165,6 +174,13 @@ public static boolean setCurrentOwnCloudAccount(Context context, String accountN
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.
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/com/owncloud/android/services/AccountRemovalJob.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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<Boolean> {
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<Boolean> future) {
if (future.isDone()) {
EventBus.getDefault().post(new AccountRemovedEvent());
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/owncloud/android/services/NCJobCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
73 changes: 52 additions & 21 deletions src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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;
Expand All @@ -65,6 +66,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;
Expand Down Expand Up @@ -177,6 +179,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.
Expand Down Expand Up @@ -623,7 +626,7 @@ public void setDrawerLockMode(int lockMode) {
/**
* Enable or disable the drawer indicator.
*
* @param enable <code>true</code> to enable, <code>false</code> to disable
* @param enable true to enable, false to disable
*/
public void setDrawerIndicatorEnabled(boolean enable) {
if (mDrawerToggle != null) {
Expand All @@ -636,9 +639,21 @@ public void setDrawerIndicatorEnabled(boolean enable) {
*/
public void updateAccountList() {
Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());

ArrayList<Account> 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();

Expand Down Expand Up @@ -673,33 +688,32 @@ mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager(),
*
* @param accounts list of accounts
*/
private void repopulateAccountList(Account[] accounts) {
private void repopulateAccountList(ArrayList<Account> 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);
mNavigationView.getMenu().add(
R.id.drawer_menu_accounts,
Menu.NONE,
MENU_ORDER_ACCOUNT,
accounts[i].name)
account.name)
.setIcon(R.drawable.ic_user);
}
}
Expand Down Expand Up @@ -1017,6 +1031,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);
}
Expand Down Expand Up @@ -1090,13 +1105,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();
Expand Down Expand Up @@ -1148,15 +1163,26 @@ protected void onAccountCreationSuccessful(AccountManagerFuture<Bundle> 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<Account> 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++;
}
}
Expand Down Expand Up @@ -1214,6 +1240,11 @@ protected void onStop() {
super.onStop();
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onAccountRemovedEvent(AccountRemovedEvent event) {
updateAccountList();
}

/**
* Retrieves external links via api from 'external' app
*/
Expand Down
Loading

0 comments on commit 5e4f458

Please sign in to comment.