Remove legacy AccountUtils

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
This commit is contained in:
Chris Narkiewicz 2019-06-05 22:54:07 +01:00
parent 0a10bc06de
commit 23f7c2f46b
No known key found for this signature in database
GPG key ID: 30D28CA4CCC665C6
29 changed files with 151 additions and 200 deletions

View file

@ -14,6 +14,8 @@ USER=$1
PASS=$2 PASS=$2
TYPE=$4 TYPE=$4
set -e
if [ $TYPE = "IT" ]; then if [ $TYPE = "IT" ]; then
cd build/reports/androidTests/connected/flavors/GPLAY cd build/reports/androidTests/connected/flavors/GPLAY
else else

View file

@ -39,13 +39,13 @@ public class UserAccountManagerImplTest extends AbstractIT {
accountManager.setUserData(account, AccountUtils.Constants.KEY_USER_ID, null); accountManager.setUserData(account, AccountUtils.Constants.KEY_USER_ID, null);
assertNull(accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID)); assertNull(accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID));
sut.migrateUserId(); boolean success = sut.migrateUserId();
assertTrue(success);
Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments(); Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
String userId = arguments.getString("TEST_SERVER_USERNAME"); String userId = arguments.getString("TEST_SERVER_USERNAME");
// assume that userId == loginname (as we manually set it) // assume that userId == loginname (as we manually set it)
assertEquals(userId, accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID)); assertEquals(userId, accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID));
assertTrue(appPreferences.isUserIdMigrated());
} }
} }

View file

@ -58,14 +58,14 @@ public abstract class AbstractIT {
AccountManager platformAccountManager = AccountManager.get(targetContext); AccountManager platformAccountManager = AccountManager.get(targetContext);
platformAccountManager.addAccountExplicitly(temp, password, null); platformAccountManager.addAccountExplicitly(temp, password, null);
platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
Integer.toString(com.owncloud.android.authentication.AccountUtils.ACCOUNT_VERSION)); Integer.toString(UserAccountManager.ACCOUNT_VERSION));
platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0"); platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0");
platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString()); platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString());
platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId
} }
account = com.owncloud.android.authentication.AccountUtils.getOwnCloudAccountByName(targetContext, final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
loginName + "@" + baseUrl); account = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
if (account == null) { if (account == null) {
throw new ActivityNotFoundException(); throw new ActivityNotFoundException();

View file

@ -3,10 +3,10 @@ package com.owncloud.android;
import android.content.ContentResolver; import android.content.ContentResolver;
import com.evernote.android.job.JobRequest; import com.evernote.android.job.JobRequest;
import com.nextcloud.client.account.CurrentAccountProvider; import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.account.UserAccountManagerImpl;
import com.nextcloud.client.device.PowerManagementService; import com.nextcloud.client.device.PowerManagementService;
import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.client.network.ConnectivityService;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.UploadsStorageManager; import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.db.OCUpload; import com.owncloud.android.db.OCUpload;
import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.files.services.FileUploader;
@ -69,8 +69,8 @@ public class UploadIT extends AbstractIT {
@Before @Before
public void setUp() { public void setUp() {
final ContentResolver contentResolver = targetContext.getContentResolver(); final ContentResolver contentResolver = targetContext.getContentResolver();
final CurrentAccountProvider currentAccountProvider = () -> AccountUtils.getCurrentOwnCloudAccount(targetContext); final UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
storageManager = new UploadsStorageManager(currentAccountProvider, contentResolver); storageManager = new UploadsStorageManager(accountManager, contentResolver);
} }
@Test @Test

View file

@ -36,7 +36,6 @@ import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl; import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.PushConfigurationState; import com.owncloud.android.datamodel.PushConfigurationState;
import com.owncloud.android.datamodel.SignatureVerification; import com.owncloud.android.datamodel.SignatureVerification;
@ -262,7 +261,7 @@ public final class PushUtils {
} else if (remoteOperationResult.getCode() == } else if (remoteOperationResult.getCode() ==
RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD) { RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD) {
arbitraryDataProvider.storeOrUpdateKeyValue(account.name, arbitraryDataProvider.storeOrUpdateKeyValue(account.name,
AccountUtils.ACCOUNT_USES_STANDARD_PASSWORD, "true"); UserAccountManager.ACCOUNT_USES_STANDARD_PASSWORD, "true");
} }
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) { } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
Log_OC.d(TAG, "Failed to find an account"); Log_OC.d(TAG, "Failed to find an account");

View file

@ -30,6 +30,11 @@ import androidx.annotation.Nullable;
public interface UserAccountManager extends CurrentAccountProvider { public interface UserAccountManager extends CurrentAccountProvider {
int ACCOUNT_VERSION = 1;
int ACCOUNT_VERSION_WITH_PROPER_ID = 2;
String ACCOUNT_USES_STANDARD_PASSWORD = "ACCOUNT_USES_STANDARD_PASSWORD";
String PENDING_FOR_REMOVAL = "PENDING_FOR_REMOVAL";
@Nullable @Nullable
OwnCloudAccount getCurrentOwnCloudAccount(); OwnCloudAccount getCurrentOwnCloudAccount();
@ -55,9 +60,9 @@ public interface UserAccountManager extends CurrentAccountProvider {
boolean exists(Account account); boolean exists(Account account);
/** /**
* Verifies that every account has userId set. * Verifies that every account has userId set
*/ */
void migrateUserId(); boolean migrateUserId();
@Nullable @Nullable
Account getAccountByName(String name); Account getAccountByName(String name);

View file

@ -27,11 +27,9 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
@ -49,7 +47,7 @@ import androidx.annotation.Nullable;
public class UserAccountManagerImpl implements UserAccountManager { public class UserAccountManagerImpl implements UserAccountManager {
private static final String TAG = AccountUtils.class.getSimpleName(); private static final String TAG = UserAccountManagerImpl.class.getSimpleName();
private static final String PREF_SELECT_OC_ACCOUNT = "select_oc_account"; private static final String PREF_SELECT_OC_ACCOUNT = "select_oc_account";
private Context context; private Context context;
@ -107,7 +105,38 @@ public class UserAccountManagerImpl implements UserAccountManager {
@Nullable @Nullable
public Account getCurrentAccount() { public Account getCurrentAccount() {
return AccountUtils.getCurrentOwnCloudAccount(context); Account[] ocAccounts = getAccounts();
Account defaultAccount = null;
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String accountName = appPreferences.getString(PREF_SELECT_OC_ACCOUNT, null);
// account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
if (accountName != null) {
for (Account account : ocAccounts) {
if (account.name.equals(accountName)) {
defaultAccount = account;
break;
}
}
}
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,
PENDING_FOR_REMOVAL);
if (!pendingForRemoval) {
defaultAccount = account;
break;
}
}
}
return defaultAccount;
} }
@Override @Override
@ -206,13 +235,7 @@ public class UserAccountManagerImpl implements UserAccountManager {
return !TextUtils.isEmpty(file.getOwnerId()) && account.name.split("@")[0].equals(file.getOwnerId()); return !TextUtils.isEmpty(file.getOwnerId()) && account.name.split("@")[0].equals(file.getOwnerId());
} }
public void migrateUserId() { public boolean migrateUserId() {
AppPreferences appPreferences = AppPreferencesImpl.fromContext(context);
if (appPreferences.isUserIdMigrated()) {
// migration done
return;
}
boolean success = false; boolean success = false;
Account[] ocAccounts = accountManager.getAccountsByType(MainApp.getAccountType(context)); Account[] ocAccounts = accountManager.getAccountsByType(MainApp.getAccountType(context));
String userId; String userId;
@ -258,9 +281,7 @@ public class UserAccountManagerImpl implements UserAccountManager {
success = true; success = true;
} }
if (success) { return success;
appPreferences.setMigratedUserId(true);
}
} }
private String getAccountType() { private String getAccountType() {

View file

@ -26,6 +26,7 @@ import com.nextcloud.client.appinfo.AppInfoModule;
import com.nextcloud.client.device.DeviceModule; import com.nextcloud.client.device.DeviceModule;
import com.nextcloud.client.network.NetworkModule; import com.nextcloud.client.network.NetworkModule;
import com.nextcloud.client.onboarding.OnboardingModule; import com.nextcloud.client.onboarding.OnboardingModule;
import com.nextcloud.client.preferences.PreferencesModule;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -37,6 +38,7 @@ import dagger.android.support.AndroidSupportInjectionModule;
@Component(modules = { @Component(modules = {
AndroidSupportInjectionModule.class, AndroidSupportInjectionModule.class,
AppModule.class, AppModule.class,
PreferencesModule.class,
AppInfoModule.class, AppInfoModule.class,
NetworkModule.class, NetworkModule.class,
DeviceModule.class, DeviceModule.class,

View file

@ -63,11 +63,6 @@ class AppModule {
return application.getResources(); return application.getResources();
} }
@Provides
AppPreferences preferences(Application application) {
return AppPreferencesImpl.fromContext(application);
}
@Provides @Provides
UserAccountManager userAccountManager( UserAccountManager userAccountManager(
Context context, Context context,

View file

@ -26,7 +26,6 @@ package com.nextcloud.client.onboarding;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
@ -44,7 +43,6 @@ import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.AppPreferences;
import com.owncloud.android.BuildConfig; import com.owncloud.android.BuildConfig;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.authentication.AuthenticatorActivity; import com.owncloud.android.authentication.AuthenticatorActivity;
import com.owncloud.android.features.FeatureItem; import com.owncloud.android.features.FeatureItem;
import com.owncloud.android.ui.activity.BaseActivity; import com.owncloud.android.ui.activity.BaseActivity;
@ -211,7 +209,8 @@ public class FirstRunActivity extends BaseActivity implements ViewPager.OnPageCh
if (FIRST_RUN_RESULT_CODE == requestCode && RESULT_OK == resultCode) { if (FIRST_RUN_RESULT_CODE == requestCode && RESULT_OK == resultCode) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
Account account = AccountUtils.getOwnCloudAccountByName(this, accountName); Account account = userAccountManager.getAccountByName(accountName);
if (account == null) { if (account == null) {
DisplayUtils.showSnackMessage(this, R.string.account_creation_failed); DisplayUtils.showSnackMessage(this, R.string.account_creation_failed);

View file

@ -26,7 +26,8 @@ import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import com.owncloud.android.authentication.AccountUtils; import com.nextcloud.client.account.CurrentAccountProvider;
import com.nextcloud.client.account.UserAccountManagerImpl;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
@ -74,15 +75,18 @@ public final class AppPreferencesImpl implements AppPreferences {
private final Context context; private final Context context;
private final SharedPreferences preferences; private final SharedPreferences preferences;
private final CurrentAccountProvider currentAccountProvider;
public static AppPreferences fromContext(Context context) { public static AppPreferences fromContext(Context context) {
SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(context); final CurrentAccountProvider currentAccountProvider = UserAccountManagerImpl.fromContext(context);
return new AppPreferencesImpl(context, prefs); final SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
return new AppPreferencesImpl(context, prefs, currentAccountProvider);
} }
AppPreferencesImpl(Context appContext, SharedPreferences preferences) { AppPreferencesImpl(Context appContext, SharedPreferences preferences, CurrentAccountProvider currentAccountProvider) {
this.context = appContext; this.context = appContext;
this.preferences = preferences; this.preferences = preferences;
this.currentAccountProvider = currentAccountProvider;
} }
@Override @Override
@ -203,23 +207,38 @@ public final class AppPreferencesImpl implements AppPreferences {
@Override @Override
public String getFolderLayout(OCFile folder) { public String getFolderLayout(OCFile folder) {
return getFolderPreference(context, PREF__FOLDER_LAYOUT, folder, FOLDER_LAYOUT_LIST); return getFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
PREF__FOLDER_LAYOUT,
folder,
FOLDER_LAYOUT_LIST);
} }
@Override @Override
public void setFolderLayout(OCFile folder, String layout_name) { public void setFolderLayout(OCFile folder, String layout_name) {
setFolderPreference(context, PREF__FOLDER_LAYOUT, folder, layout_name); setFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
PREF__FOLDER_LAYOUT,
folder,
layout_name);
} }
@Override @Override
public FileSortOrder getSortOrderByFolder(OCFile folder) { public FileSortOrder getSortOrderByFolder(OCFile folder) {
return FileSortOrder.sortOrders.get(getFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder, return FileSortOrder.sortOrders.get(getFolderPreference(context,
FileSortOrder.sort_a_to_z.name)); currentAccountProvider.getCurrentAccount(),
PREF__FOLDER_SORT_ORDER,
folder,
FileSortOrder.sort_a_to_z.name));
} }
@Override @Override
public void setSortOrder(OCFile folder, FileSortOrder sortOrder) { public void setSortOrder(OCFile folder, FileSortOrder sortOrder) {
setFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder, sortOrder.name); setFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
PREF__FOLDER_SORT_ORDER,
folder,
sortOrder.name);
} }
@Override @Override
@ -229,8 +248,7 @@ public final class AppPreferencesImpl implements AppPreferences {
@Override @Override
public FileSortOrder getSortOrderByType(FileSortOrder.Type type, FileSortOrder defaultOrder) { public FileSortOrder getSortOrderByType(FileSortOrder.Type type, FileSortOrder defaultOrder) {
Account account = AccountUtils.getCurrentOwnCloudAccount(context); Account account = currentAccountProvider.getCurrentAccount();
if (account == null) { if (account == null) {
return defaultOrder; return defaultOrder;
} }
@ -244,7 +262,7 @@ public final class AppPreferencesImpl implements AppPreferences {
@Override @Override
public void setSortOrder(FileSortOrder.Type type, FileSortOrder sortOrder) { public void setSortOrder(FileSortOrder.Type type, FileSortOrder sortOrder) {
Account account = AccountUtils.getCurrentOwnCloudAccount(context); Account account = currentAccountProvider.getCurrentAccount();
if (account == null) { if (account == null) {
throw new IllegalArgumentException("Account may not be null!"); throw new IllegalArgumentException("Account may not be null!");
@ -457,10 +475,11 @@ public final class AppPreferencesImpl implements AppPreferences {
* @param defaultValue Fallback value in case no ancestor is set. * @param defaultValue Fallback value in case no ancestor is set.
* @return Preference value * @return Preference value
*/ */
private static String getFolderPreference(Context context, String preferenceName, OCFile folder, private static String getFolderPreference(final Context context,
String defaultValue) { final Account account,
Account account = AccountUtils.getCurrentOwnCloudAccount(context); final String preferenceName,
final OCFile folder,
final String defaultValue) {
if (account == null) { if (account == null) {
return defaultValue; return defaultValue;
} }
@ -469,9 +488,10 @@ public final class AppPreferencesImpl implements AppPreferences {
FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver()); FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder)); String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
while (folder != null && value.isEmpty()) { OCFile prefFolder = folder;
folder = storageManager.getFileById(folder.getParentId()); while (prefFolder != null && value.isEmpty()) {
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder)); prefFolder = storageManager.getFileById(prefFolder.getParentId());
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, prefFolder));
} }
return value.isEmpty() ? defaultValue : value; return value.isEmpty() ? defaultValue : value;
} }
@ -484,9 +504,11 @@ public final class AppPreferencesImpl implements AppPreferences {
* @param folder Folder. * @param folder Folder.
* @param value Preference value to set. * @param value Preference value to set.
*/ */
private static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) { private static void setFolderPreference(final Context context,
Account account = AccountUtils.getCurrentOwnCloudAccount(context); final Account account,
final String preferenceName,
final OCFile folder,
final String value) {
if (account == null) { if (account == null) {
throw new IllegalArgumentException("Account may not be null!"); throw new IllegalArgumentException("Account may not be null!");
} }

View file

@ -0,0 +1,29 @@
package com.nextcloud.client.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import com.nextcloud.client.account.CurrentAccountProvider;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class PreferencesModule {
@Provides
@Singleton
public SharedPreferences sharedPreferences(Context context) {
return android.preference.PreferenceManager.getDefaultSharedPreferences(context);
}
@Provides
@Singleton
public AppPreferences appPreferences(Context context,
SharedPreferences sharedPreferences,
CurrentAccountProvider currentAccountProvider) {
return new AppPreferencesImpl(context, sharedPreferences, currentAccountProvider);
}
}

View file

@ -210,7 +210,10 @@ public class MainApp extends MultiDexApplication implements
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
// best place, before any access to AccountManager or database // best place, before any access to AccountManager or database
accountManager.migrateUserId(); if (!preferences.isUserIdMigrated()) {
final boolean migrated = accountManager.migrateUserId();
preferences.setMigratedUserId(migrated);
}
}); });
t.start(); t.start();

View file

@ -1,118 +0,0 @@
/*
* ownCloud Android client application
*
* Copyright (C) 2012 Bartek Przybylski
* Copyright (C) 2016 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.authentication;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
import com.owncloud.android.ui.activity.ManageAccountsActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Helper class for dealing with accounts.
*/
@Deprecated
public final class AccountUtils {
private static final String PREF_SELECT_OC_ACCOUNT = "select_oc_account";
public static final int ACCOUNT_VERSION = 1;
public static final int ACCOUNT_VERSION_WITH_PROPER_ID = 2;
public static final String ACCOUNT_USES_STANDARD_PASSWORD = "ACCOUNT_USES_STANDARD_PASSWORD";
private AccountUtils() {
// Required empty constructor
}
/**
* Can be used to get the currently selected ownCloud {@link Account} in the
* application preferences.
*
* @param context The current application {@link Context}
* @return The ownCloud {@link Account} currently saved in preferences, or the first
* {@link Account} available, if valid (still registered in the system as ownCloud
* account). If none is available and valid, returns null.
*/
public static @Nullable Account getCurrentOwnCloudAccount(Context context) {
Account[] ocAccounts = getAccounts(context);
Account defaultAccount = null;
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String accountName = appPreferences.getString(PREF_SELECT_OC_ACCOUNT, null);
// account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
if (accountName != null) {
for (Account account : ocAccounts) {
if (account.name.equals(accountName)) {
defaultAccount = account;
break;
}
}
}
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;
}
private static Account[] getAccounts(Context context) {
AccountManager accountManager = AccountManager.get(context);
return accountManager.getAccountsByType(MainApp.getAccountType(context));
}
/**
* Returns owncloud account identified by accountName or null if it does not exist.
* @param context the context
* @param accountName name of account to be returned
* @return owncloud account named accountName
*/
public static Account getOwnCloudAccountByName(Context context, String accountName) {
Account[] ocAccounts = AccountManager.get(context).getAccountsByType(MainApp.getAccountType(context));
for (Account account : ocAccounts) {
if(account.name.equals(accountName)) {
return account;
}
}
return null;
}
}

View file

@ -1719,7 +1719,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
mAccountMgr.setUserData(mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName()); mAccountMgr.setUserData(mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
mAccountMgr.setUserData(mAccount, Constants.KEY_USER_ID, userInfo.getId()); mAccountMgr.setUserData(mAccount, Constants.KEY_USER_ID, userInfo.getId());
mAccountMgr.setUserData(mAccount, Constants.KEY_OC_ACCOUNT_VERSION, mAccountMgr.setUserData(mAccount, Constants.KEY_OC_ACCOUNT_VERSION,
Integer.toString(AccountUtils.ACCOUNT_VERSION_WITH_PROPER_ID)); Integer.toString(UserAccountManager.ACCOUNT_VERSION_WITH_PROPER_ID));
setAccountAuthenticatorResult(intent.getExtras()); setAccountAuthenticatorResult(intent.getExtras());

View file

@ -24,12 +24,10 @@
package com.owncloud.android.db; package com.owncloud.android.db;
import android.accounts.Account; import android.accounts.Account;
import android.content.Context;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.UploadsStorageManager; import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus; import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;

View file

@ -40,7 +40,6 @@ import android.util.Pair;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.authentication.AuthenticatorActivity; import com.owncloud.android.authentication.AuthenticatorActivity;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;

View file

@ -35,7 +35,6 @@ import com.evernote.android.job.util.support.PersistableBundleCompat;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.preferences.AppPreferencesImpl; import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.FilesystemDataProvider; import com.owncloud.android.datamodel.FilesystemDataProvider;

View file

@ -23,7 +23,6 @@ package com.owncloud.android.jobs;
import android.accounts.Account; import android.accounts.Account;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.database.Cursor; import android.database.Cursor;
@ -35,9 +34,7 @@ import android.text.format.DateFormat;
import com.evernote.android.job.Job; import com.evernote.android.job.Job;
import com.evernote.android.job.util.support.PersistableBundleCompat; import com.evernote.android.job.util.support.PersistableBundleCompat;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;

View file

@ -31,7 +31,6 @@ import android.os.AsyncTask;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;

View file

@ -31,7 +31,6 @@ import android.os.AsyncTask;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;

View file

@ -12,7 +12,6 @@ import android.os.Handler;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.di.Injectable; import com.nextcloud.client.di.Injectable;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;

View file

@ -83,7 +83,7 @@ public class ManageAccountsActivity extends FileActivity
public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"; 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 KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED";
public static final String PENDING_FOR_REMOVAL = "PENDING_FOR_REMOVAL"; public static final String PENDING_FOR_REMOVAL = UserAccountManager.PENDING_FOR_REMOVAL;
private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME"; private static final String KEY_DISPLAY_NAME = "DISPLAY_NAME";

View file

@ -40,8 +40,8 @@ import android.widget.TextView;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.jobs.NotificationJob; import com.owncloud.android.jobs.NotificationJob;
import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.OwnCloudAccount;
@ -178,8 +178,8 @@ public class NotificationsActivity extends FileActivity implements Notifications
} else { } else {
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver()); ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
boolean usesOldLogin = arbitraryDataProvider.getBooleanValue(currentAccount.name, boolean usesOldLogin = arbitraryDataProvider.getBooleanValue(currentAccount.name,
AccountUtils.ACCOUNT_USES_STANDARD_PASSWORD); UserAccountManager.ACCOUNT_USES_STANDARD_PASSWORD);
if (usesOldLogin) { if (usesOldLogin) {
snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_old_login, snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_old_login,

View file

@ -47,7 +47,6 @@ import com.nextcloud.client.preferences.AppPreferences;
import com.owncloud.android.BuildConfig; import com.owncloud.android.BuildConfig;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.MediaFolder; import com.owncloud.android.datamodel.MediaFolder;
import com.owncloud.android.datamodel.MediaFolderType; import com.owncloud.android.datamodel.MediaFolderType;

View file

@ -41,12 +41,10 @@ import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputEditText;
import com.nextcloud.client.account.CurrentAccountProvider;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.di.Injectable; import com.nextcloud.client.di.Injectable;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.OwnCloudAccount;

View file

@ -26,7 +26,6 @@ import android.view.Menu;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.lib.resources.status.OCCapability; import com.owncloud.android.lib.resources.status.OCCapability;
import com.owncloud.android.lib.resources.status.OwnCloudVersion; import com.owncloud.android.lib.resources.status.OwnCloudVersion;

View file

@ -46,9 +46,9 @@ import com.google.android.material.button.MaterialButton;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import com.nextcloud.client.account.UserAccountManagerImpl;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.status.OCCapability; import com.owncloud.android.lib.resources.status.OCCapability;
@ -559,7 +559,8 @@ public final class ThemeUtils {
if (acc != null) { if (acc != null) {
account = acc; account = acc;
} else if (context != null) { } else if (context != null) {
account = AccountUtils.getCurrentOwnCloudAccount(context); // TODO: refactor when dark theme work is completed
account = UserAccountManagerImpl.fromContext(context).getCurrentAccount();
} }
if (account != null) { if (account != null) {

View file

@ -3,6 +3,8 @@ package com.nextcloud.client.preferences;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import com.nextcloud.client.account.CurrentAccountProvider;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -23,13 +25,16 @@ public class TestPreferenceManager {
@Mock @Mock
private SharedPreferences.Editor editor; private SharedPreferences.Editor editor;
@Mock
private CurrentAccountProvider accountProvider;
private AppPreferencesImpl appPreferences; private AppPreferencesImpl appPreferences;
@Before @Before
public void setUp() { public void setUp() {
when(editor.remove(anyString())).thenReturn(editor); when(editor.remove(anyString())).thenReturn(editor);
when(sharedPreferences.edit()).thenReturn(editor); when(sharedPreferences.edit()).thenReturn(editor);
appPreferences = new AppPreferencesImpl(testContext, sharedPreferences); appPreferences = new AppPreferencesImpl(testContext, sharedPreferences, accountProvider);
} }
@Test @Test