mirror of
https://github.com/nextcloud/android.git
synced 2024-11-26 23:28:42 +03:00
Remove legacy AccountUtils
Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
This commit is contained in:
parent
0a10bc06de
commit
23f7c2f46b
29 changed files with 151 additions and 200 deletions
|
@ -14,6 +14,8 @@ USER=$1
|
|||
PASS=$2
|
||||
TYPE=$4
|
||||
|
||||
set -e
|
||||
|
||||
if [ $TYPE = "IT" ]; then
|
||||
cd build/reports/androidTests/connected/flavors/GPLAY
|
||||
else
|
||||
|
|
|
@ -39,13 +39,13 @@ public class UserAccountManagerImplTest extends AbstractIT {
|
|||
accountManager.setUserData(account, AccountUtils.Constants.KEY_USER_ID, null);
|
||||
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();
|
||||
String userId = arguments.getString("TEST_SERVER_USERNAME");
|
||||
|
||||
// assume that userId == loginname (as we manually set it)
|
||||
assertEquals(userId, accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID));
|
||||
assertTrue(appPreferences.isUserIdMigrated());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,14 +58,14 @@ public abstract class AbstractIT {
|
|||
AccountManager platformAccountManager = AccountManager.get(targetContext);
|
||||
platformAccountManager.addAccountExplicitly(temp, password, null);
|
||||
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_BASE_URL, baseUrl.toString());
|
||||
platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId
|
||||
}
|
||||
|
||||
account = com.owncloud.android.authentication.AccountUtils.getOwnCloudAccountByName(targetContext,
|
||||
loginName + "@" + baseUrl);
|
||||
final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
|
||||
account = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
|
||||
|
||||
if (account == null) {
|
||||
throw new ActivityNotFoundException();
|
||||
|
|
|
@ -3,10 +3,10 @@ package com.owncloud.android;
|
|||
import android.content.ContentResolver;
|
||||
|
||||
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.network.ConnectivityService;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.UploadsStorageManager;
|
||||
import com.owncloud.android.db.OCUpload;
|
||||
import com.owncloud.android.files.services.FileUploader;
|
||||
|
@ -69,8 +69,8 @@ public class UploadIT extends AbstractIT {
|
|||
@Before
|
||||
public void setUp() {
|
||||
final ContentResolver contentResolver = targetContext.getContentResolver();
|
||||
final CurrentAccountProvider currentAccountProvider = () -> AccountUtils.getCurrentOwnCloudAccount(targetContext);
|
||||
storageManager = new UploadsStorageManager(currentAccountProvider, contentResolver);
|
||||
final UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
|
||||
storageManager = new UploadsStorageManager(accountManager, contentResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -36,7 +36,6 @@ import com.nextcloud.client.preferences.AppPreferences;
|
|||
import com.nextcloud.client.preferences.AppPreferencesImpl;
|
||||
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.PushConfigurationState;
|
||||
import com.owncloud.android.datamodel.SignatureVerification;
|
||||
|
@ -262,7 +261,7 @@ public final class PushUtils {
|
|||
} else if (remoteOperationResult.getCode() ==
|
||||
RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD) {
|
||||
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) {
|
||||
Log_OC.d(TAG, "Failed to find an account");
|
||||
|
|
|
@ -30,6 +30,11 @@ import androidx.annotation.Nullable;
|
|||
|
||||
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
|
||||
OwnCloudAccount getCurrentOwnCloudAccount();
|
||||
|
||||
|
@ -55,9 +60,9 @@ public interface UserAccountManager extends CurrentAccountProvider {
|
|||
boolean exists(Account account);
|
||||
|
||||
/**
|
||||
* Verifies that every account has userId set.
|
||||
* Verifies that every account has userId set
|
||||
*/
|
||||
void migrateUserId();
|
||||
boolean migrateUserId();
|
||||
|
||||
@Nullable
|
||||
Account getAccountByName(String name);
|
||||
|
|
|
@ -27,11 +27,9 @@ import android.content.SharedPreferences;
|
|||
import android.preference.PreferenceManager;
|
||||
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.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.ArbitraryDataProvider;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
import com.owncloud.android.lib.common.OwnCloudAccount;
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
|
@ -49,7 +47,7 @@ import androidx.annotation.Nullable;
|
|||
|
||||
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 Context context;
|
||||
|
@ -107,7 +105,38 @@ public class UserAccountManagerImpl implements UserAccountManager {
|
|||
|
||||
@Nullable
|
||||
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
|
||||
|
@ -206,13 +235,7 @@ public class UserAccountManagerImpl implements UserAccountManager {
|
|||
return !TextUtils.isEmpty(file.getOwnerId()) && account.name.split("@")[0].equals(file.getOwnerId());
|
||||
}
|
||||
|
||||
public void migrateUserId() {
|
||||
AppPreferences appPreferences = AppPreferencesImpl.fromContext(context);
|
||||
if (appPreferences.isUserIdMigrated()) {
|
||||
// migration done
|
||||
return;
|
||||
}
|
||||
|
||||
public boolean migrateUserId() {
|
||||
boolean success = false;
|
||||
Account[] ocAccounts = accountManager.getAccountsByType(MainApp.getAccountType(context));
|
||||
String userId;
|
||||
|
@ -258,9 +281,7 @@ public class UserAccountManagerImpl implements UserAccountManager {
|
|||
success = true;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
appPreferences.setMigratedUserId(true);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
private String getAccountType() {
|
||||
|
|
|
@ -26,6 +26,7 @@ import com.nextcloud.client.appinfo.AppInfoModule;
|
|||
import com.nextcloud.client.device.DeviceModule;
|
||||
import com.nextcloud.client.network.NetworkModule;
|
||||
import com.nextcloud.client.onboarding.OnboardingModule;
|
||||
import com.nextcloud.client.preferences.PreferencesModule;
|
||||
import com.owncloud.android.MainApp;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
@ -37,6 +38,7 @@ import dagger.android.support.AndroidSupportInjectionModule;
|
|||
@Component(modules = {
|
||||
AndroidSupportInjectionModule.class,
|
||||
AppModule.class,
|
||||
PreferencesModule.class,
|
||||
AppInfoModule.class,
|
||||
NetworkModule.class,
|
||||
DeviceModule.class,
|
||||
|
|
|
@ -63,11 +63,6 @@ class AppModule {
|
|||
return application.getResources();
|
||||
}
|
||||
|
||||
@Provides
|
||||
AppPreferences preferences(Application application) {
|
||||
return AppPreferencesImpl.fromContext(application);
|
||||
}
|
||||
|
||||
@Provides
|
||||
UserAccountManager userAccountManager(
|
||||
Context context,
|
||||
|
|
|
@ -26,7 +26,6 @@ package com.nextcloud.client.onboarding;
|
|||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Color;
|
||||
|
@ -44,7 +43,6 @@ import com.nextcloud.client.di.Injectable;
|
|||
import com.nextcloud.client.preferences.AppPreferences;
|
||||
import com.owncloud.android.BuildConfig;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.authentication.AuthenticatorActivity;
|
||||
import com.owncloud.android.features.FeatureItem;
|
||||
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) {
|
||||
|
||||
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
|
||||
Account account = AccountUtils.getOwnCloudAccountByName(this, accountName);
|
||||
Account account = userAccountManager.getAccountByName(accountName);
|
||||
|
||||
|
||||
if (account == null) {
|
||||
DisplayUtils.showSnackMessage(this, R.string.account_creation_failed);
|
||||
|
|
|
@ -26,7 +26,8 @@ import android.annotation.SuppressLint;
|
|||
import android.content.Context;
|
||||
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.FileDataStorageManager;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
|
@ -74,15 +75,18 @@ public final class AppPreferencesImpl implements AppPreferences {
|
|||
|
||||
private final Context context;
|
||||
private final SharedPreferences preferences;
|
||||
private final CurrentAccountProvider currentAccountProvider;
|
||||
|
||||
public static AppPreferences fromContext(Context context) {
|
||||
SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return new AppPreferencesImpl(context, prefs);
|
||||
final CurrentAccountProvider currentAccountProvider = UserAccountManagerImpl.fromContext(context);
|
||||
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.preferences = preferences;
|
||||
this.currentAccountProvider = currentAccountProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -203,23 +207,38 @@ public final class AppPreferencesImpl implements AppPreferences {
|
|||
|
||||
@Override
|
||||
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
|
||||
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
|
||||
public FileSortOrder getSortOrderByFolder(OCFile folder) {
|
||||
return FileSortOrder.sortOrders.get(getFolderPreference(context, PREF__FOLDER_SORT_ORDER, folder,
|
||||
FileSortOrder.sort_a_to_z.name));
|
||||
return FileSortOrder.sortOrders.get(getFolderPreference(context,
|
||||
currentAccountProvider.getCurrentAccount(),
|
||||
PREF__FOLDER_SORT_ORDER,
|
||||
folder,
|
||||
FileSortOrder.sort_a_to_z.name));
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
|
@ -229,8 +248,7 @@ public final class AppPreferencesImpl implements AppPreferences {
|
|||
|
||||
@Override
|
||||
public FileSortOrder getSortOrderByType(FileSortOrder.Type type, FileSortOrder defaultOrder) {
|
||||
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
|
||||
|
||||
Account account = currentAccountProvider.getCurrentAccount();
|
||||
if (account == null) {
|
||||
return defaultOrder;
|
||||
}
|
||||
|
@ -244,7 +262,7 @@ public final class AppPreferencesImpl implements AppPreferences {
|
|||
|
||||
@Override
|
||||
public void setSortOrder(FileSortOrder.Type type, FileSortOrder sortOrder) {
|
||||
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
|
||||
Account account = currentAccountProvider.getCurrentAccount();
|
||||
|
||||
if (account == 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.
|
||||
* @return Preference value
|
||||
*/
|
||||
private static String getFolderPreference(Context context, String preferenceName, OCFile folder,
|
||||
String defaultValue) {
|
||||
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
|
||||
|
||||
private static String getFolderPreference(final Context context,
|
||||
final Account account,
|
||||
final String preferenceName,
|
||||
final OCFile folder,
|
||||
final String defaultValue) {
|
||||
if (account == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -469,9 +488,10 @@ public final class AppPreferencesImpl implements AppPreferences {
|
|||
FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
|
||||
|
||||
String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
|
||||
while (folder != null && value.isEmpty()) {
|
||||
folder = storageManager.getFileById(folder.getParentId());
|
||||
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
|
||||
OCFile prefFolder = folder;
|
||||
while (prefFolder != null && value.isEmpty()) {
|
||||
prefFolder = storageManager.getFileById(prefFolder.getParentId());
|
||||
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, prefFolder));
|
||||
}
|
||||
return value.isEmpty() ? defaultValue : value;
|
||||
}
|
||||
|
@ -484,9 +504,11 @@ public final class AppPreferencesImpl implements AppPreferences {
|
|||
* @param folder Folder.
|
||||
* @param value Preference value to set.
|
||||
*/
|
||||
private static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
|
||||
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
|
||||
|
||||
private static void setFolderPreference(final Context context,
|
||||
final Account account,
|
||||
final String preferenceName,
|
||||
final OCFile folder,
|
||||
final String value) {
|
||||
if (account == null) {
|
||||
throw new IllegalArgumentException("Account may not be null!");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -210,7 +210,10 @@ public class MainApp extends MultiDexApplication implements
|
|||
|
||||
Thread t = new Thread(() -> {
|
||||
// best place, before any access to AccountManager or database
|
||||
accountManager.migrateUserId();
|
||||
if (!preferences.isUserIdMigrated()) {
|
||||
final boolean migrated = accountManager.migrateUserId();
|
||||
preferences.setMigratedUserId(migrated);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1719,7 +1719,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
|
|||
mAccountMgr.setUserData(mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
|
||||
mAccountMgr.setUserData(mAccount, Constants.KEY_USER_ID, userInfo.getId());
|
||||
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());
|
||||
|
|
|
@ -24,12 +24,10 @@
|
|||
package com.owncloud.android.db;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
import com.owncloud.android.datamodel.UploadsStorageManager;
|
||||
import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
|
||||
|
|
|
@ -40,7 +40,6 @@ import android.util.Pair;
|
|||
|
||||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.authentication.AuthenticatorActivity;
|
||||
import com.owncloud.android.datamodel.FileDataStorageManager;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
|
|
|
@ -35,7 +35,6 @@ import com.evernote.android.job.util.support.PersistableBundleCompat;
|
|||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.nextcloud.client.preferences.AppPreferencesImpl;
|
||||
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.datamodel.FilesystemDataProvider;
|
||||
|
|
|
@ -23,7 +23,6 @@ package com.owncloud.android.jobs;
|
|||
|
||||
import android.accounts.Account;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.database.Cursor;
|
||||
|
@ -35,9 +34,7 @@ import android.text.format.DateFormat;
|
|||
import com.evernote.android.job.Job;
|
||||
import com.evernote.android.job.util.support.PersistableBundleCompat;
|
||||
import com.nextcloud.client.account.UserAccountManager;
|
||||
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.datamodel.OCFile;
|
||||
|
|
|
@ -31,7 +31,6 @@ import android.os.AsyncTask;
|
|||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.owncloud.android.MainApp;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.lib.common.OwnCloudAccount;
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
|
||||
|
|
|
@ -31,7 +31,6 @@ import android.os.AsyncTask;
|
|||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.owncloud.android.MainApp;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
import com.owncloud.android.lib.common.OwnCloudAccount;
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
|
|
|
@ -12,7 +12,6 @@ import android.os.Handler;
|
|||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.nextcloud.client.di.Injectable;
|
||||
import com.owncloud.android.MainApp;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.FileDataStorageManager;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
|
|
@ -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_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";
|
||||
|
||||
|
|
|
@ -40,8 +40,8 @@ import android.widget.TextView;
|
|||
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.ArbitraryDataProvider;
|
||||
import com.owncloud.android.jobs.NotificationJob;
|
||||
import com.owncloud.android.lib.common.OwnCloudAccount;
|
||||
|
@ -178,8 +178,8 @@ public class NotificationsActivity extends FileActivity implements Notifications
|
|||
} else {
|
||||
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
|
||||
|
||||
boolean usesOldLogin = arbitraryDataProvider.getBooleanValue(currentAccount.name,
|
||||
AccountUtils.ACCOUNT_USES_STANDARD_PASSWORD);
|
||||
boolean usesOldLogin = arbitraryDataProvider.getBooleanValue(currentAccount.name,
|
||||
UserAccountManager.ACCOUNT_USES_STANDARD_PASSWORD);
|
||||
|
||||
if (usesOldLogin) {
|
||||
snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_old_login,
|
||||
|
|
|
@ -47,7 +47,6 @@ import com.nextcloud.client.preferences.AppPreferences;
|
|||
import com.owncloud.android.BuildConfig;
|
||||
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.MediaFolder;
|
||||
import com.owncloud.android.datamodel.MediaFolderType;
|
||||
|
|
|
@ -41,12 +41,10 @@ import android.widget.TextView;
|
|||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.nextcloud.client.account.CurrentAccountProvider;
|
||||
import com.nextcloud.client.account.UserAccountManager;
|
||||
import com.nextcloud.client.di.Injectable;
|
||||
import com.owncloud.android.MainApp;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.FileDataStorageManager;
|
||||
import com.owncloud.android.datamodel.OCFile;
|
||||
import com.owncloud.android.lib.common.OwnCloudAccount;
|
||||
|
|
|
@ -26,7 +26,6 @@ import android.view.Menu;
|
|||
|
||||
import com.nextcloud.client.account.UserAccountManager;
|
||||
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.OwnCloudVersion;
|
||||
|
||||
|
|
|
@ -46,9 +46,9 @@ import com.google.android.material.button.MaterialButton;
|
|||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
import com.nextcloud.client.account.UserAccountManagerImpl;
|
||||
import com.owncloud.android.MainApp;
|
||||
import com.owncloud.android.R;
|
||||
import com.owncloud.android.authentication.AccountUtils;
|
||||
import com.owncloud.android.datamodel.FileDataStorageManager;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
import com.owncloud.android.lib.resources.status.OCCapability;
|
||||
|
@ -559,7 +559,8 @@ public final class ThemeUtils {
|
|||
if (acc != null) {
|
||||
account = acc;
|
||||
} else if (context != null) {
|
||||
account = AccountUtils.getCurrentOwnCloudAccount(context);
|
||||
// TODO: refactor when dark theme work is completed
|
||||
account = UserAccountManagerImpl.fromContext(context).getCurrentAccount();
|
||||
}
|
||||
|
||||
if (account != null) {
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.nextcloud.client.preferences;
|
|||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import com.nextcloud.client.account.CurrentAccountProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -23,13 +25,16 @@ public class TestPreferenceManager {
|
|||
@Mock
|
||||
private SharedPreferences.Editor editor;
|
||||
|
||||
@Mock
|
||||
private CurrentAccountProvider accountProvider;
|
||||
|
||||
private AppPreferencesImpl appPreferences;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
when(editor.remove(anyString())).thenReturn(editor);
|
||||
when(sharedPreferences.edit()).thenReturn(editor);
|
||||
appPreferences = new AppPreferencesImpl(testContext, sharedPreferences);
|
||||
appPreferences = new AppPreferencesImpl(testContext, sharedPreferences, accountProvider);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue