From ee4808213b095d1177324fe75aae0503a0e308aa Mon Sep 17 00:00:00 2001 From: Chris Narkiewicz Date: Thu, 7 Oct 2021 22:13:00 +0100 Subject: [PATCH] Deprecate FileDataStorageManager constructors depending on legacy Account This commit deprecates constructors using legacy platform Account type and wraps them using new API accepting User model. All calls to FileDataStorageManager constructors that were trivially portable without are updated as well. Places where passing new type requires more risku work were left out for now. FileDataStorageManager still uses Account internally - modifying this requires getting rid of all legacy constructors first. Signed-off-by: Chris Narkiewicz --- .../java/com/owncloud/android/AbstractIT.java | 2 +- .../FileDataStorageManagerContentResolverIT.java | 2 +- .../owncloud/android/datamodel/OCCapabilityIT.kt | 4 ++-- .../android/util/ErrorMessageAdapterIT.java | 8 +++++--- src/debug/java/com/nextcloud/client/TestActivity.kt | 2 +- .../client/files/downloader/DownloadTask.kt | 2 +- .../com/nextcloud/client/jobs/AccountRemovalWork.kt | 2 +- .../com/nextcloud/client/jobs/ContactsBackupWork.kt | 2 +- .../com/nextcloud/client/jobs/OfflineSyncWork.kt | 2 +- .../client/preferences/AppPreferencesImpl.java | 2 +- .../com/nextcloud/ui/ChooseAccountDialogFragment.kt | 2 +- .../android/datamodel/FileDataStorageManager.java | 13 +++++++++---- .../providers/DiskLruImageCacheFileProvider.java | 2 +- .../providers/UsersAndGroupsSearchProvider.java | 2 +- .../android/services/OperationsService.java | 2 +- .../android/ui/activity/DrawerActivity.java | 2 +- .../android/ui/activity/FileDisplayActivity.java | 2 +- .../android/ui/activity/StorageMigration.java | 3 ++- .../android/ui/adapter/OCFileListAdapter.java | 2 +- .../ChooseRichDocumentsTemplateDialogFragment.java | 2 +- .../ui/dialog/ChooseTemplateDialogFragment.java | 2 +- .../android/ui/dialog/ConflictsResolveDialog.java | 2 +- .../ui/fragment/FileDetailActivitiesFragment.java | 2 +- .../android/ui/helpers/FileOperationsHelper.java | 4 ++-- 24 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/androidTest/java/com/owncloud/android/AbstractIT.java b/src/androidTest/java/com/owncloud/android/AbstractIT.java index 8edc0a1950..d7d96a6e8a 100644 --- a/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -88,7 +88,7 @@ public abstract class AbstractIT { protected Activity currentActivity; protected FileDataStorageManager fileDataStorageManager = - new FileDataStorageManager(account, targetContext.getContentResolver()); + new FileDataStorageManager(user, targetContext.getContentResolver()); @BeforeClass public static void beforeAll() { diff --git a/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentResolverIT.java b/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentResolverIT.java index 4ea5d05d4d..76543fec0d 100644 --- a/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentResolverIT.java +++ b/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentResolverIT.java @@ -25,7 +25,7 @@ package com.owncloud.android.datamodel; public class FileDataStorageManagerContentResolverIT extends FileDataStorageManagerIT { @Override public void before() { - sut = new FileDataStorageManager(account, targetContext.getContentResolver()); + sut = new FileDataStorageManager(user, targetContext.getContentResolver()); super.before(); } diff --git a/src/androidTest/java/com/owncloud/android/datamodel/OCCapabilityIT.kt b/src/androidTest/java/com/owncloud/android/datamodel/OCCapabilityIT.kt index 1b5c1b2f4d..7487fb404b 100644 --- a/src/androidTest/java/com/owncloud/android/datamodel/OCCapabilityIT.kt +++ b/src/androidTest/java/com/owncloud/android/datamodel/OCCapabilityIT.kt @@ -31,7 +31,7 @@ import org.junit.Test class OCCapabilityIT : AbstractIT() { @Test fun saveCapability() { - val fileDataStorageManager = FileDataStorageManager(account, targetContext.contentResolver) + val fileDataStorageManager = FileDataStorageManager(user, targetContext.contentResolver) val capability = OCCapability() capability.etag = "123" @@ -40,7 +40,7 @@ class OCCapabilityIT : AbstractIT() { fileDataStorageManager.saveCapabilities(capability) - val newCapability = fileDataStorageManager.getCapability(account.name) + val newCapability = fileDataStorageManager.getCapability(user.accountName) assertEquals(capability.etag, newCapability.etag) assertEquals(capability.userStatus, newCapability.userStatus) diff --git a/src/androidTest/java/com/owncloud/android/util/ErrorMessageAdapterIT.java b/src/androidTest/java/com/owncloud/android/util/ErrorMessageAdapterIT.java index c2888db390..0100e0daee 100644 --- a/src/androidTest/java/com/owncloud/android/util/ErrorMessageAdapterIT.java +++ b/src/androidTest/java/com/owncloud/android/util/ErrorMessageAdapterIT.java @@ -25,6 +25,8 @@ import android.accounts.Account; import android.content.Context; import android.content.res.Resources; +import com.nextcloud.client.account.MockUser; +import com.nextcloud.client.account.User; import com.owncloud.android.MainApp; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; @@ -49,17 +51,17 @@ public class ErrorMessageAdapterIT { @Test public void getErrorCauseMessageForForbiddenRemoval() { Resources resources = InstrumentationRegistry.getInstrumentation().getTargetContext().getResources(); - Account account = new Account("name", ACCOUNT_TYPE); + User user = new MockUser("name", ACCOUNT_TYPE); Context context = MainApp.getAppContext(); String errorMessage = ErrorMessageAdapter.getErrorCauseMessage( new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN), new RemoveFileOperation(new OCFile(PATH_TO_DELETE), false, - account, + user.toPlatformAccount(), false, context, - new FileDataStorageManager(account, context.getContentResolver())), + new FileDataStorageManager(user, context.getContentResolver())), resources ); diff --git a/src/debug/java/com/nextcloud/client/TestActivity.kt b/src/debug/java/com/nextcloud/client/TestActivity.kt index b72229c434..dca0328d3a 100644 --- a/src/debug/java/com/nextcloud/client/TestActivity.kt +++ b/src/debug/java/com/nextcloud/client/TestActivity.kt @@ -101,7 +101,7 @@ class TestActivity : override fun getStorageManager(): FileDataStorageManager { if (!this::storage.isInitialized) { - storage = FileDataStorageManager(account, contentResolver) + storage = FileDataStorageManager(user.get(), contentResolver) if (!storage.capabilityExistsForAccount(account.name)) { val ocCapability = OCCapability() diff --git a/src/main/java/com/nextcloud/client/files/downloader/DownloadTask.kt b/src/main/java/com/nextcloud/client/files/downloader/DownloadTask.kt index 4a8588eb66..2d1f181677 100644 --- a/src/main/java/com/nextcloud/client/files/downloader/DownloadTask.kt +++ b/src/main/java/com/nextcloud/client/files/downloader/DownloadTask.kt @@ -68,7 +68,7 @@ class DownloadTask( val result = op.execute(client) if (result.isSuccess) { val storageManager = FileDataStorageManager( - request.user.toPlatformAccount(), + request.user, contentResolver ) val file = saveDownloadedFile(op, storageManager) diff --git a/src/main/java/com/nextcloud/client/jobs/AccountRemovalWork.kt b/src/main/java/com/nextcloud/client/jobs/AccountRemovalWork.kt index 3dea3a3e13..d5ffe486b6 100644 --- a/src/main/java/com/nextcloud/client/jobs/AccountRemovalWork.kt +++ b/src/main/java/com/nextcloud/client/jobs/AccountRemovalWork.kt @@ -94,7 +94,7 @@ class AccountRemovalWork( val user = optionalUser.get() backgroundJobManager.cancelPeriodicContactsBackup(user) val userRemoved = userAccountManager.removeUser(user) - val storageManager = FileDataStorageManager(user.toPlatformAccount(), context.contentResolver) + val storageManager = FileDataStorageManager(user, context.contentResolver) // disable daily backup arbitraryDataProvider.storeOrUpdateKeyValue( diff --git a/src/main/java/com/nextcloud/client/jobs/ContactsBackupWork.kt b/src/main/java/com/nextcloud/client/jobs/ContactsBackupWork.kt index 307168f9c6..beea91736b 100644 --- a/src/main/java/com/nextcloud/client/jobs/ContactsBackupWork.kt +++ b/src/main/java/com/nextcloud/client/jobs/ContactsBackupWork.kt @@ -179,7 +179,7 @@ class ContactsBackupWork( private fun expireFiles(daysToExpire: Int, backupFolderString: String, user: User) { // -1 disables expiration if (daysToExpire > -1) { val storageManager = FileDataStorageManager( - user.toPlatformAccount(), + user, applicationContext.getContentResolver() ) val backupFolder: OCFile = storageManager.getFileByPath(backupFolderString) diff --git a/src/main/java/com/nextcloud/client/jobs/OfflineSyncWork.kt b/src/main/java/com/nextcloud/client/jobs/OfflineSyncWork.kt index 80d614769f..66723e178b 100644 --- a/src/main/java/com/nextcloud/client/jobs/OfflineSyncWork.kt +++ b/src/main/java/com/nextcloud/client/jobs/OfflineSyncWork.kt @@ -60,7 +60,7 @@ class OfflineSyncWork constructor( if (!powerManagementService.isPowerSavingEnabled && !connectivityService.isInternetWalled) { val users = userAccountManager.allUsers for (user in users) { - val storageManager = FileDataStorageManager(user.toPlatformAccount(), contentResolver) + val storageManager = FileDataStorageManager(user, contentResolver) val ocRoot = storageManager.getFileByPath(OCFile.ROOT_PATH) if (ocRoot.storagePath == null) { break diff --git a/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java b/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java index 5233b4c51e..bbfca9d072 100644 --- a/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java +++ b/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java @@ -599,7 +599,7 @@ public final class AppPreferencesImpl implements AppPreferences { } ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver()); - FileDataStorageManager storageManager = new FileDataStorageManager(user.toPlatformAccount(), context.getContentResolver()); + FileDataStorageManager storageManager = new FileDataStorageManager(user, context.getContentResolver()); String value = dataProvider.getValue(user.getAccountName(), getKeyFromFolder(preferenceName, folder)); OCFile prefFolder = folder; diff --git a/src/main/java/com/nextcloud/ui/ChooseAccountDialogFragment.kt b/src/main/java/com/nextcloud/ui/ChooseAccountDialogFragment.kt index ee1a45788c..6e76022c44 100644 --- a/src/main/java/com/nextcloud/ui/ChooseAccountDialogFragment.kt +++ b/src/main/java/com/nextcloud/ui/ChooseAccountDialogFragment.kt @@ -148,7 +148,7 @@ class ChooseAccountDialogFragment : dismiss() } - val capability = FileDataStorageManager(user.toPlatformAccount(), context?.contentResolver) + val capability = FileDataStorageManager(user, context?.contentResolver) .getCapability(user) if (capability.userStatus.isTrue) { diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index a4c4d19cf8..c8f7434039 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -87,18 +87,27 @@ public class FileDataStorageManager { private ContentProviderClient contentProviderClient; private Account account; + @Deprecated public FileDataStorageManager(Account account, ContentResolver contentResolver) { this.contentProviderClient = null; this.contentResolver = contentResolver; this.account = account; } + public FileDataStorageManager(User user, ContentResolver contentResolver) { + this(user.toPlatformAccount(), contentResolver); + } + + @Deprecated public FileDataStorageManager(Account account, ContentProviderClient contentProviderClient) { this.contentProviderClient = contentProviderClient; this.contentResolver = null; this.account = account; } + public FileDataStorageManager(User user, ContentProviderClient contentProviderClient) { + this(user.toPlatformAccount(), contentProviderClient); + } /** * Use getFileByEncryptedRemotePath() or getFileByDecryptedRemotePath() @@ -2403,8 +2412,4 @@ public class FileDataStorageManager { public Account getAccount() { return this.account; } - - public void setAccount(Account account) { - this.account = account; - } } diff --git a/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java b/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java index 974c1afb81..9a7c38bdaf 100644 --- a/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java +++ b/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java @@ -62,7 +62,7 @@ public class DiskLruImageCacheFileProvider extends ContentProvider { private OCFile getFile(Uri uri) { User user = accountManager.getUser(); - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user.toPlatformAccount(), + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, MainApp.getAppContext().getContentResolver()); return fileDataStorageManager.getFileByPath(uri.getPath()); diff --git a/src/main/java/com/owncloud/android/providers/UsersAndGroupsSearchProvider.java b/src/main/java/com/owncloud/android/providers/UsersAndGroupsSearchProvider.java index a02df6be10..024358670a 100644 --- a/src/main/java/com/owncloud/android/providers/UsersAndGroupsSearchProvider.java +++ b/src/main/java/com/owncloud/android/providers/UsersAndGroupsSearchProvider.java @@ -234,7 +234,7 @@ public class UsersAndGroupsSearchProvider extends ContentProvider { Uri emailBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_EMAIL).build(); Uri circleBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_CIRCLE).build(); - FileDataStorageManager manager = new FileDataStorageManager(user.toPlatformAccount(), + FileDataStorageManager manager = new FileDataStorageManager(user, getContext().getContentResolver()); boolean federatedShareAllowed = manager.getCapability(user.getAccountName()) .getFilesSharingFederationOutgoing() diff --git a/src/main/java/com/owncloud/android/services/OperationsService.java b/src/main/java/com/owncloud/android/services/OperationsService.java index 318c5db37f..97d1776648 100644 --- a/src/main/java/com/owncloud/android/services/OperationsService.java +++ b/src/main/java/com/owncloud/android/services/OperationsService.java @@ -507,7 +507,7 @@ public class OperationsService extends Service { String newParentPath; long shareId; - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account, + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, getContentResolver()); switch (action) { diff --git a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 819196c1b3..926c71e56e 100644 --- a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -390,7 +390,7 @@ public abstract class DrawerActivity extends ToolbarActivity } private void filterDrawerMenu(final Menu menu, @NonNull final User user) { - FileDataStorageManager storageManager = new FileDataStorageManager(user.toPlatformAccount(), + FileDataStorageManager storageManager = new FileDataStorageManager(user, getContentResolver()); OCCapability capability = storageManager.getCapability(user.getAccountName()); diff --git a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index e2e4970f4c..0a721492a6 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -2503,7 +2503,7 @@ public class FileDisplayActivity extends FileActivity FileDataStorageManager storageManager = getStorageManager(); if (storageManager == null) { - storageManager = new FileDataStorageManager(user.toPlatformAccount(), getContentResolver()); + storageManager = new FileDataStorageManager(user, getContentResolver()); } FetchRemoteFileTask fetchRemoteFileTask = new FetchRemoteFileTask(user, diff --git a/src/main/java/com/owncloud/android/ui/activity/StorageMigration.java b/src/main/java/com/owncloud/android/ui/activity/StorageMigration.java index cb774e13fc..9c22028271 100644 --- a/src/main/java/com/owncloud/android/ui/activity/StorageMigration.java +++ b/src/main/java/com/owncloud/android/ui/activity/StorageMigration.java @@ -436,7 +436,8 @@ public class StorageMigration { } private void updateIndex(Context context) throws MigrationException { - FileDataStorageManager manager = new FileDataStorageManager(null, context.getContentResolver()); + final Account nullAccount = null; + FileDataStorageManager manager = new FileDataStorageManager(nullAccount, context.getContentResolver()); try { manager.migrateStoredFiles(mStorageSource, mStorageTarget); diff --git a/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java index 805aea2eda..a5c8fd6668 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java @@ -857,7 +857,7 @@ public class OCFileListAdapter extends RecyclerView.Adapter { User user = fileActivity.getUser().orElseThrow(RuntimeException::new); - FileDataStorageManager storageManager = new FileDataStorageManager(fileActivity.getAccount(), + FileDataStorageManager storageManager = new FileDataStorageManager(user, fileActivity.getContentResolver()); // check if file is in conflict (this is known due to latest folder refresh) @@ -314,7 +314,7 @@ public class FileOperationsHelper { public void run() { User user = currentAccount.getUser(); FileDataStorageManager storageManager = - new FileDataStorageManager(user.toPlatformAccount(), fileActivity.getContentResolver()); + new FileDataStorageManager(user, fileActivity.getContentResolver()); // a fresh object is needed; many things could have occurred to the file // since it was registered to observe again, assuming that local files // are linked to a remote file AT MOST, SOMETHING TO BE DONE;