From 23f7c2f46bede08d252c389e9950fc826cf0270e Mon Sep 17 00:00:00 2001 From: Chris Narkiewicz Date: Wed, 5 Jun 2019 22:54:07 +0100 Subject: [PATCH] Remove legacy AccountUtils Signed-off-by: Chris Narkiewicz --- scripts/uploadReport.sh | 2 + .../account/UserAccountManagerImplTest.java | 6 +- .../java/com/owncloud/android/AbstractIT.java | 6 +- .../java/com/owncloud/android/UploadIT.java | 8 +- .../com/owncloud/android/utils/PushUtils.java | 3 +- .../client/account/UserAccountManager.java | 9 +- .../account/UserAccountManagerImpl.java | 51 +++++--- .../com/nextcloud/client/di/AppComponent.java | 2 + .../com/nextcloud/client/di/AppModule.java | 5 - .../client/onboarding/FirstRunActivity.java | 5 +- .../preferences/AppPreferencesImpl.java | 66 ++++++---- .../client/preferences/PreferencesModule.java | 29 +++++ .../java/com/owncloud/android/MainApp.java | 5 +- .../android/authentication/AccountUtils.java | 118 ------------------ .../authentication/AuthenticatorActivity.java | 2 +- .../com/owncloud/android/db/OCUpload.java | 2 - .../files/services/FileDownloader.java | 1 - .../android/jobs/AccountRemovalJob.java | 1 - .../android/jobs/ContactsBackupJob.java | 3 - .../activities/ActivitiesServiceApiImpl.java | 1 - .../data/files/FilesServiceApiImpl.java | 1 - .../android/ui/activity/BaseActivity.java | 1 - .../ui/activity/ManageAccountsActivity.java | 2 +- .../ui/activity/NotificationsActivity.java | 6 +- .../ui/activity/SyncedFoldersActivity.java | 1 - .../FileDetailActivitiesFragment.java | 2 - .../android/utils/DrawerMenuUtil.java | 1 - .../owncloud/android/utils/ThemeUtils.java | 5 +- .../preferences/TestPreferenceManager.java | 7 +- 29 files changed, 151 insertions(+), 200 deletions(-) create mode 100644 src/main/java/com/nextcloud/client/preferences/PreferencesModule.java delete mode 100644 src/main/java/com/owncloud/android/authentication/AccountUtils.java diff --git a/scripts/uploadReport.sh b/scripts/uploadReport.sh index 88b84961c1..fb1fdb0355 100755 --- a/scripts/uploadReport.sh +++ b/scripts/uploadReport.sh @@ -14,6 +14,8 @@ USER=$1 PASS=$2 TYPE=$4 +set -e + if [ $TYPE = "IT" ]; then cd build/reports/androidTests/connected/flavors/GPLAY else diff --git a/src/androidTest/java/com/nextcloud/client/account/UserAccountManagerImplTest.java b/src/androidTest/java/com/nextcloud/client/account/UserAccountManagerImplTest.java index 0c60d44c4f..bc2a32f189 100644 --- a/src/androidTest/java/com/nextcloud/client/account/UserAccountManagerImplTest.java +++ b/src/androidTest/java/com/nextcloud/client/account/UserAccountManagerImplTest.java @@ -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()); } } diff --git a/src/androidTest/java/com/owncloud/android/AbstractIT.java b/src/androidTest/java/com/owncloud/android/AbstractIT.java index f3460cbfe5..25fc902922 100644 --- a/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -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(); diff --git a/src/androidTest/java/com/owncloud/android/UploadIT.java b/src/androidTest/java/com/owncloud/android/UploadIT.java index cc901f551a..1d468e7162 100644 --- a/src/androidTest/java/com/owncloud/android/UploadIT.java +++ b/src/androidTest/java/com/owncloud/android/UploadIT.java @@ -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 diff --git a/src/gplay/java/com/owncloud/android/utils/PushUtils.java b/src/gplay/java/com/owncloud/android/utils/PushUtils.java index fefb666f9d..49a601edd2 100644 --- a/src/gplay/java/com/owncloud/android/utils/PushUtils.java +++ b/src/gplay/java/com/owncloud/android/utils/PushUtils.java @@ -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"); diff --git a/src/main/java/com/nextcloud/client/account/UserAccountManager.java b/src/main/java/com/nextcloud/client/account/UserAccountManager.java index c2ea1c0081..5584541c49 100644 --- a/src/main/java/com/nextcloud/client/account/UserAccountManager.java +++ b/src/main/java/com/nextcloud/client/account/UserAccountManager.java @@ -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); diff --git a/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java b/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java index 6b1a875573..c9689a6e1a 100644 --- a/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java +++ b/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java @@ -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() { diff --git a/src/main/java/com/nextcloud/client/di/AppComponent.java b/src/main/java/com/nextcloud/client/di/AppComponent.java index 34e4f6ca4f..818b974170 100644 --- a/src/main/java/com/nextcloud/client/di/AppComponent.java +++ b/src/main/java/com/nextcloud/client/di/AppComponent.java @@ -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, diff --git a/src/main/java/com/nextcloud/client/di/AppModule.java b/src/main/java/com/nextcloud/client/di/AppModule.java index 0ea0beac94..15eb0c40ab 100644 --- a/src/main/java/com/nextcloud/client/di/AppModule.java +++ b/src/main/java/com/nextcloud/client/di/AppModule.java @@ -63,11 +63,6 @@ class AppModule { return application.getResources(); } - @Provides - AppPreferences preferences(Application application) { - return AppPreferencesImpl.fromContext(application); - } - @Provides UserAccountManager userAccountManager( Context context, diff --git a/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.java b/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.java index 01301864d8..36a4040489 100644 --- a/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.java +++ b/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.java @@ -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); diff --git a/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java b/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java index 8cd55564c4..855b185b44 100644 --- a/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java +++ b/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java @@ -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!"); } diff --git a/src/main/java/com/nextcloud/client/preferences/PreferencesModule.java b/src/main/java/com/nextcloud/client/preferences/PreferencesModule.java new file mode 100644 index 0000000000..6d3338fb37 --- /dev/null +++ b/src/main/java/com/nextcloud/client/preferences/PreferencesModule.java @@ -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); + } +} diff --git a/src/main/java/com/owncloud/android/MainApp.java b/src/main/java/com/owncloud/android/MainApp.java index a04d9017ea..2e7f118d27 100644 --- a/src/main/java/com/owncloud/android/MainApp.java +++ b/src/main/java/com/owncloud/android/MainApp.java @@ -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(); diff --git a/src/main/java/com/owncloud/android/authentication/AccountUtils.java b/src/main/java/com/owncloud/android/authentication/AccountUtils.java deleted file mode 100644 index 586fb69682..0000000000 --- a/src/main/java/com/owncloud/android/authentication/AccountUtils.java +++ /dev/null @@ -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 . - */ - -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; - } -} diff --git a/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java index ddc365e37b..114471d03a 100644 --- a/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -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()); diff --git a/src/main/java/com/owncloud/android/db/OCUpload.java b/src/main/java/com/owncloud/android/db/OCUpload.java index 2f28047e9e..ee8d8b3c05 100644 --- a/src/main/java/com/owncloud/android/db/OCUpload.java +++ b/src/main/java/com/owncloud/android/db/OCUpload.java @@ -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; diff --git a/src/main/java/com/owncloud/android/files/services/FileDownloader.java b/src/main/java/com/owncloud/android/files/services/FileDownloader.java index f02152f407..0b8e66767c 100644 --- a/src/main/java/com/owncloud/android/files/services/FileDownloader.java +++ b/src/main/java/com/owncloud/android/files/services/FileDownloader.java @@ -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; diff --git a/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java b/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java index 1d8bf81259..a0ed06552e 100644 --- a/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java +++ b/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java @@ -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; diff --git a/src/main/java/com/owncloud/android/jobs/ContactsBackupJob.java b/src/main/java/com/owncloud/android/jobs/ContactsBackupJob.java index 2fa9d2e501..5ef97e3009 100644 --- a/src/main/java/com/owncloud/android/jobs/ContactsBackupJob.java +++ b/src/main/java/com/owncloud/android/jobs/ContactsBackupJob.java @@ -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; diff --git a/src/main/java/com/owncloud/android/ui/activities/data/activities/ActivitiesServiceApiImpl.java b/src/main/java/com/owncloud/android/ui/activities/data/activities/ActivitiesServiceApiImpl.java index f3b4c9c551..b43d594565 100644 --- a/src/main/java/com/owncloud/android/ui/activities/data/activities/ActivitiesServiceApiImpl.java +++ b/src/main/java/com/owncloud/android/ui/activities/data/activities/ActivitiesServiceApiImpl.java @@ -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; diff --git a/src/main/java/com/owncloud/android/ui/activities/data/files/FilesServiceApiImpl.java b/src/main/java/com/owncloud/android/ui/activities/data/files/FilesServiceApiImpl.java index b1baeb0bc8..892d05a4cf 100644 --- a/src/main/java/com/owncloud/android/ui/activities/data/files/FilesServiceApiImpl.java +++ b/src/main/java/com/owncloud/android/ui/activities/data/files/FilesServiceApiImpl.java @@ -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; diff --git a/src/main/java/com/owncloud/android/ui/activity/BaseActivity.java b/src/main/java/com/owncloud/android/ui/activity/BaseActivity.java index e2a5fa3111..571ce32220 100644 --- a/src/main/java/com/owncloud/android/ui/activity/BaseActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/BaseActivity.java @@ -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; 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 2c1596cbd9..b96152e188 100644 --- a/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/ManageAccountsActivity.java @@ -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"; diff --git a/src/main/java/com/owncloud/android/ui/activity/NotificationsActivity.java b/src/main/java/com/owncloud/android/ui/activity/NotificationsActivity.java index a5e02cc490..bee713d03b 100644 --- a/src/main/java/com/owncloud/android/ui/activity/NotificationsActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/NotificationsActivity.java @@ -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, diff --git a/src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.java b/src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.java index 60e1b0fedf..ac5af6f602 100644 --- a/src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.java @@ -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; diff --git a/src/main/java/com/owncloud/android/ui/fragment/FileDetailActivitiesFragment.java b/src/main/java/com/owncloud/android/ui/fragment/FileDetailActivitiesFragment.java index 773978bef5..9760e6f756 100644 --- a/src/main/java/com/owncloud/android/ui/fragment/FileDetailActivitiesFragment.java +++ b/src/main/java/com/owncloud/android/ui/fragment/FileDetailActivitiesFragment.java @@ -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; diff --git a/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java b/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java index 53d68bf151..ba16ddbe71 100644 --- a/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java +++ b/src/main/java/com/owncloud/android/utils/DrawerMenuUtil.java @@ -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; diff --git a/src/main/java/com/owncloud/android/utils/ThemeUtils.java b/src/main/java/com/owncloud/android/utils/ThemeUtils.java index ee816c418a..b57c48d491 100644 --- a/src/main/java/com/owncloud/android/utils/ThemeUtils.java +++ b/src/main/java/com/owncloud/android/utils/ThemeUtils.java @@ -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) { diff --git a/src/test/java/com/nextcloud/client/preferences/TestPreferenceManager.java b/src/test/java/com/nextcloud/client/preferences/TestPreferenceManager.java index 713adb908f..64bdca3e5e 100644 --- a/src/test/java/com/nextcloud/client/preferences/TestPreferenceManager.java +++ b/src/test/java/com/nextcloud/client/preferences/TestPreferenceManager.java @@ -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