From 53eea6d08e3d8d17ab16c9aca715c8c580e11ea2 Mon Sep 17 00:00:00 2001 From: Chris Narkiewicz Date: Thu, 7 Oct 2021 22:13:00 +0100 Subject: [PATCH 1/2] 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 --- src/debug/java/com/nextcloud/client/TestActivity.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/debug/java/com/nextcloud/client/TestActivity.kt b/src/debug/java/com/nextcloud/client/TestActivity.kt index dca0328d3a..fbb1d1ce3e 100644 --- a/src/debug/java/com/nextcloud/client/TestActivity.kt +++ b/src/debug/java/com/nextcloud/client/TestActivity.kt @@ -38,6 +38,7 @@ import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.activity.OnEnforceableRefreshListener import com.owncloud.android.ui.fragment.FileFragment import com.owncloud.android.ui.helpers.FileOperationsHelper +import java.lang.RuntimeException class TestActivity : FileActivity(), From 57b2a8ef1a60fbfa8331b045ca49f0e60d34eb28 Mon Sep 17 00:00:00 2001 From: Chris Narkiewicz Date: Thu, 7 Oct 2021 22:58:00 +0100 Subject: [PATCH 2/2] Removed deprecated FileDataStorageManager constructors entirely Signed-off-by: Chris Narkiewicz --- .../nextcloud/client/EndToEndRandomIT.java | 2 +- .../nextcloud/client/jobs/ContactsBackupIT.kt | 2 +- .../java/com/owncloud/android/AbstractIT.java | 2 +- .../java/com/owncloud/android/DownloadIT.java | 4 +- ...StorageManagerContentProviderClientIT.java | 2 +- .../datamodel/FileDataStorageManagerIT.java | 2 +- .../providers/DocumentsStorageProviderIT.kt | 2 +- .../activity/ConflictsResolveActivityIT.java | 42 +++---- .../android/ui/dialog/DialogFragmentIT.java | 12 +- .../java/com/nextcloud/client/TestActivity.kt | 1 - .../com/nextcloud/client/di/AppModule.java | 2 +- .../client/files/downloader/DownloadTask.kt | 2 +- .../nextcloud/client/mixins/SessionMixin.kt | 2 +- .../datamodel/FileDataStorageManager.java | 117 +++++++++--------- .../files/services/FileDownloader.java | 25 ++-- .../android/files/services/FileUploader.java | 6 +- .../operations/DownloadFileOperation.java | 35 ++++-- .../providers/DocumentsStorageProvider.java | 9 +- .../android/syncadapter/FileSyncAdapter.java | 4 +- .../ErrorsWhileCopyingHandlerActivity.java | 16 +-- .../android/ui/activity/SettingsActivity.java | 3 +- .../android/ui/activity/StorageMigration.java | 20 ++- .../android/utils/theme/CapabilityUtils.java | 21 ++-- .../android/utils/theme/ThemeColorUtils.java | 13 +- 24 files changed, 178 insertions(+), 168 deletions(-) diff --git a/src/androidTest/java/com/nextcloud/client/EndToEndRandomIT.java b/src/androidTest/java/com/nextcloud/client/EndToEndRandomIT.java index 49b024c4b8..231dfdba00 100644 --- a/src/androidTest/java/com/nextcloud/client/EndToEndRandomIT.java +++ b/src/androidTest/java/com/nextcloud/client/EndToEndRandomIT.java @@ -366,7 +366,7 @@ public class EndToEndRandomIT extends AbstractOnServerIT { "[" + i + "/" + actionCount + "] " + "Download file: " + currentFolder.getDecryptedRemotePath() + fileToDownload.getDecryptedFileName()); - assertTrue(new DownloadFileOperation(account, fileToDownload, targetContext) + assertTrue(new DownloadFileOperation(user, fileToDownload, targetContext) .execute(client) .isSuccess()); diff --git a/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt b/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt index f06950f7af..708ffbb6c7 100644 --- a/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt +++ b/src/androidTest/java/com/nextcloud/client/jobs/ContactsBackupIT.kt @@ -79,7 +79,7 @@ class ContactsBackupIT : AbstractOnServerIT() { false )[0] - assertTrue(DownloadFileOperation(account, backupOCFile, AbstractIT.targetContext).execute(client).isSuccess) + assertTrue(DownloadFileOperation(user, backupOCFile, AbstractIT.targetContext).execute(client).isSuccess) val backupFile = File(backupOCFile.storagePath) diff --git a/src/androidTest/java/com/owncloud/android/AbstractIT.java b/src/androidTest/java/com/owncloud/android/AbstractIT.java index d7d96a6e8a..c03912eb86 100644 --- a/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -134,7 +134,7 @@ public abstract class AbstractIT { // color String colorParameter = arguments.getString("COLOR"); if (!TextUtils.isEmpty(colorParameter)) { - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account, + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, targetContext.getContentResolver()); String colorHex = null; diff --git a/src/androidTest/java/com/owncloud/android/DownloadIT.java b/src/androidTest/java/com/owncloud/android/DownloadIT.java index 0ac3f1b138..a2ae4fa974 100644 --- a/src/androidTest/java/com/owncloud/android/DownloadIT.java +++ b/src/androidTest/java/com/owncloud/android/DownloadIT.java @@ -92,8 +92,8 @@ public class DownloadIT extends AbstractOnServerIT { OCFile file2 = fileDataStorageManager.getFileByDecryptedRemotePath(FOLDER + "nonEmpty2.txt"); verifyDownload(file1, file2); - assertTrue(new DownloadFileOperation(account, file1, targetContext).execute(client).isSuccess()); - assertTrue(new DownloadFileOperation(account, file2, targetContext).execute(client).isSuccess()); + assertTrue(new DownloadFileOperation(user, file1, targetContext).execute(client).isSuccess()); + assertTrue(new DownloadFileOperation(user, file2, targetContext).execute(client).isSuccess()); refreshFolder(FOLDER); diff --git a/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentProviderClientIT.java b/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentProviderClientIT.java index 2aca01dc7e..6c6e8bb28e 100644 --- a/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentProviderClientIT.java +++ b/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerContentProviderClientIT.java @@ -28,7 +28,7 @@ public class FileDataStorageManagerContentProviderClientIT extends FileDataStora @Override public void before() { - sut = new FileDataStorageManager(account, + sut = new FileDataStorageManager(user, targetContext .getContentResolver() .acquireContentProviderClient(ProviderMeta.ProviderTableMeta.CONTENT_URI) diff --git a/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerIT.java b/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerIT.java index 05883ed165..5a57f96cec 100644 --- a/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerIT.java +++ b/src/androidTest/java/com/owncloud/android/datamodel/FileDataStorageManagerIT.java @@ -217,7 +217,7 @@ abstract public class FileDataStorageManagerIT extends AbstractOnServerIT { */ @Test public void testGallerySearch() throws IOException { - sut = new FileDataStorageManager(account, + sut = new FileDataStorageManager(user, targetContext .getContentResolver() .acquireContentProviderClient(ProviderMeta.ProviderTableMeta.CONTENT_URI) diff --git a/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt b/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt index 0cb8633ed2..dcb00fddcf 100644 --- a/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt +++ b/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt @@ -38,7 +38,7 @@ class DocumentsStorageProviderIT : AbstractOnServerIT() { private val authority = context.getString(R.string.document_provider_authority) private val rootFileId = storageManager.getFileByEncryptedRemotePath(ROOT_PATH).fileId - private val documentId = "${account.hashCode()}${DOCUMENTID_SEPARATOR}$rootFileId" + private val documentId = "${user.hashCode()}${DOCUMENTID_SEPARATOR}$rootFileId" private val uri = DocumentsContract.buildTreeDocumentUri(authority, documentId) private val rootDir get() = DocumentFile.fromTreeUri(context, uri)!! diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java b/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java index 235a293926..b3dee08926 100644 --- a/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java +++ b/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java @@ -62,13 +62,13 @@ public class ConflictsResolveActivityIT extends AbstractIT { OCFile newFile = new OCFile("/newFile.txt"); newFile.setFileLength(56000); newFile.setModificationTimestamp(1522019340); - newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); + newFile.setStoragePath(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt"); OCFile existingFile = new OCFile("/newFile.txt"); existingFile.setFileLength(1024000); existingFile.setModificationTimestamp(1582019340); - FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver()); + FileDataStorageManager storageManager = new FileDataStorageManager(user, targetContext.getContentResolver()); storageManager.saveNewFile(existingFile); Intent intent = new Intent(targetContext, ConflictsResolveActivity.class); @@ -98,13 +98,13 @@ public class ConflictsResolveActivityIT extends AbstractIT { // @Test // @ScreenshotTest // todo run without real server // public void screenshotImages() throws IOException { -// FileDataStorageManager storageManager = new FileDataStorageManager(account, +// FileDataStorageManager storageManager = new FileDataStorageManager(user, // targetContext.getContentResolver()); // // OCFile newFile = new OCFile("/newFile.txt"); // newFile.setFileLength(56000); // newFile.setModificationTimestamp(1522019340); -// newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); +// newFile.setStoragePath(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt"); // // File image = getFile("image.jpg"); // @@ -118,7 +118,7 @@ public class ConflictsResolveActivityIT extends AbstractIT { // false, // true, // storageManager, -// account, +// user.toPlatformAccount(), // targetContext // ).execute(client).isSuccess()); // @@ -153,9 +153,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { public void cancel() { returnCode = false; - OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt", + OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt", "/newFile.txt", - account.name); + user.getAccountName()); OCFile existingFile = new OCFile("/newFile.txt"); existingFile.setFileLength(1024000); @@ -164,9 +164,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { OCFile newFile = new OCFile("/newFile.txt"); newFile.setFileLength(56000); newFile.setModificationTimestamp(1522019340); - newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); + newFile.setStoragePath(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt"); - FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver()); + FileDataStorageManager storageManager = new FileDataStorageManager(user, targetContext.getContentResolver()); storageManager.saveNewFile(existingFile); Intent intent = new Intent(targetContext, ConflictsResolveActivity.class); @@ -194,9 +194,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { public void keepExisting() { returnCode = false; - OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt", + OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt", "/newFile.txt", - account.name); + user.getAccountName()); OCFile existingFile = new OCFile("/newFile.txt"); existingFile.setFileLength(1024000); @@ -205,9 +205,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { OCFile newFile = new OCFile("/newFile.txt"); newFile.setFileLength(56000); newFile.setModificationTimestamp(1522019340); - newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); + newFile.setStoragePath(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt"); - FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver()); + FileDataStorageManager storageManager = new FileDataStorageManager(user, targetContext.getContentResolver()); storageManager.saveNewFile(existingFile); Intent intent = new Intent(targetContext, ConflictsResolveActivity.class); @@ -239,9 +239,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { public void keepNew() { returnCode = false; - OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt", + OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt", "/newFile.txt", - account.name); + user.getAccountName()); OCFile existingFile = new OCFile("/newFile.txt"); existingFile.setFileLength(1024000); @@ -251,9 +251,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { OCFile newFile = new OCFile("/newFile.txt"); newFile.setFileLength(56000); newFile.setModificationTimestamp(1522019340); - newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); + newFile.setStoragePath(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt"); - FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver()); + FileDataStorageManager storageManager = new FileDataStorageManager(user, targetContext.getContentResolver()); storageManager.saveNewFile(existingFile); Intent intent = new Intent(targetContext, ConflictsResolveActivity.class); @@ -285,9 +285,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { public void keepBoth() { returnCode = false; - OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt", + OCUpload newUpload = new OCUpload(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt", "/newFile.txt", - account.name); + user.getAccountName()); OCFile existingFile = new OCFile("/newFile.txt"); existingFile.setFileLength(1024000); @@ -296,9 +296,9 @@ public class ConflictsResolveActivityIT extends AbstractIT { OCFile newFile = new OCFile("/newFile.txt"); newFile.setFileLength(56000); newFile.setModificationTimestamp(1522019340); - newFile.setStoragePath(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt"); + newFile.setStoragePath(FileStorageUtils.getSavePath(user.getAccountName()) + "/nonEmpty.txt"); - FileDataStorageManager storageManager = new FileDataStorageManager(account, targetContext.getContentResolver()); + FileDataStorageManager storageManager = new FileDataStorageManager(user, targetContext.getContentResolver()); storageManager.saveNewFile(existingFile); Intent intent = new Intent(targetContext, ConflictsResolveActivity.class); diff --git a/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java b/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java index 1b5bccf8a4..5db5828268 100644 --- a/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java +++ b/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java @@ -33,6 +33,8 @@ import android.widget.TextView; import com.google.gson.Gson; import com.nextcloud.client.account.RegisteredUser; import com.nextcloud.client.account.Server; +import com.nextcloud.client.account.User; +import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.device.DeviceInfo; import com.nextcloud.ui.ChooseAccountDialogFragment; import com.owncloud.android.AbstractIT; @@ -171,6 +173,7 @@ public class DialogFragmentIT extends AbstractIT { @Test @ScreenshotTest public void testAccountChooserDialog() throws AccountUtils.AccountNotFoundException { + UserAccountManager userAccountManager = activityRule.getActivity().getUserAccountManager(); AccountManager accountManager = AccountManager.get(targetContext); for (Account account : accountManager.getAccountsByType(MainApp.getAccountType(targetContext))) { accountManager.removeAccountExplicitly(account); @@ -181,7 +184,7 @@ public class DialogFragmentIT extends AbstractIT { accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, SERVER_URL); accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, "test"); accountManager.setAuthToken(newAccount, AccountTypeUtils.getAuthTokenTypePass(newAccount.type), "password"); - + User newUser = userAccountManager.getUser(newAccount.name).orElseThrow(RuntimeException::new); Account newAccount2 = new Account("user1@nextcloud.localhost", MainApp.getAccountType(targetContext)); accountManager.addAccountExplicitly(newAccount2, "password", null); @@ -190,8 +193,7 @@ public class DialogFragmentIT extends AbstractIT { accountManager.setUserData(newAccount2, AccountUtils.Constants.KEY_OC_VERSION, "20.0.0"); accountManager.setAuthToken(newAccount2, AccountTypeUtils.getAuthTokenTypePass(newAccount.type), "password"); - - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newAccount, + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newUser, targetContext.getContentResolver()); OCCapability capability = new OCCapability(); @@ -253,7 +255,9 @@ public class DialogFragmentIT extends AbstractIT { accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, "test"); accountManager.setAuthToken(newAccount, AccountTypeUtils.getAuthTokenTypePass(newAccount.type), "password"); - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newAccount, + UserAccountManager userAccountManager = activityRule.getActivity().getUserAccountManager(); + User newUser = userAccountManager.getUser(newAccount.name).get(); + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newUser, targetContext.getContentResolver()); OCCapability capability = new OCCapability(); diff --git a/src/debug/java/com/nextcloud/client/TestActivity.kt b/src/debug/java/com/nextcloud/client/TestActivity.kt index fbb1d1ce3e..dca0328d3a 100644 --- a/src/debug/java/com/nextcloud/client/TestActivity.kt +++ b/src/debug/java/com/nextcloud/client/TestActivity.kt @@ -38,7 +38,6 @@ import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.activity.OnEnforceableRefreshListener import com.owncloud.android.ui.fragment.FileFragment import com.owncloud.android.ui.helpers.FileOperationsHelper -import java.lang.RuntimeException class TestActivity : FileActivity(), diff --git a/src/main/java/com/nextcloud/client/di/AppModule.java b/src/main/java/com/nextcloud/client/di/AppModule.java index c17ae66f54..44faf419c7 100644 --- a/src/main/java/com/nextcloud/client/di/AppModule.java +++ b/src/main/java/com/nextcloud/client/di/AppModule.java @@ -139,7 +139,7 @@ class AppModule { @Provides FileDataStorageManager fileDataStorageManager(CurrentAccountProvider currentAccountProvider, Context context) { - return new FileDataStorageManager(currentAccountProvider.getCurrentAccount(), context.getContentResolver()); + return new FileDataStorageManager(currentAccountProvider.getUser(), context.getContentResolver()); } @Provides 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 2d1f181677..b8f61045a9 100644 --- a/src/main/java/com/nextcloud/client/files/downloader/DownloadTask.kt +++ b/src/main/java/com/nextcloud/client/files/downloader/DownloadTask.kt @@ -63,7 +63,7 @@ class DownloadTask( } fun download(request: DownloadRequest, progress: (Int) -> Unit, isCancelled: IsCancelled): Result { - val op = DownloadFileOperation(request.user.toPlatformAccount(), request.file, context) + val op = DownloadFileOperation(request.user, request.file, context) val client = clientProvider.invoke() val result = op.execute(client) if (result.isSuccess) { diff --git a/src/main/java/com/nextcloud/client/mixins/SessionMixin.kt b/src/main/java/com/nextcloud/client/mixins/SessionMixin.kt index b69688d795..6319e7515e 100644 --- a/src/main/java/com/nextcloud/client/mixins/SessionMixin.kt +++ b/src/main/java/com/nextcloud/client/mixins/SessionMixin.kt @@ -65,7 +65,7 @@ class SessionMixin constructor( } currentAccount?.let { - val storageManager = FileDataStorageManager(currentAccount, contentResolver) + val storageManager = FileDataStorageManager(getUser().get(), contentResolver) this.storageManager = storageManager this.capabilities = storageManager.getCapability(it.name) } diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index c8f7434039..2daa597cfc 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -85,28 +85,18 @@ public class FileDataStorageManager { private ContentResolver contentResolver; private ContentProviderClient contentProviderClient; - private Account account; - - @Deprecated - public FileDataStorageManager(Account account, ContentResolver contentResolver) { - this.contentProviderClient = null; - this.contentResolver = contentResolver; - this.account = account; - } + private User user; 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; + this.contentProviderClient = null; + this.contentResolver = contentResolver; + this.user = user; } public FileDataStorageManager(User user, ContentProviderClient contentProviderClient) { - this(user.toPlatformAccount(), contentProviderClient); + this.contentProviderClient = contentProviderClient; + this.contentResolver = null; + this.user = user; } /** @@ -235,7 +225,7 @@ public class FileDataStorageManager { if (!ocFile.isFolder()) { cv.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); } - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, user.getAccountName()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); @@ -402,7 +392,7 @@ public class FileDataStorageManager { // prepare operations to remove files in the given folder String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[2]; - whereArgs[0] = account.name; + whereArgs[0] = user.getAccountName(); for (OCFile ocFile : filesToRemove) { if (ocFile.getParentId() == folder.getFileId()) { whereArgs[1] = ocFile.getRemotePath(); @@ -411,7 +401,7 @@ public class FileDataStorageManager { ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, ocFile.getFileId())) .withSelection(where, whereArgs).build()); - File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(account.name, ocFile)); + File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile)); if (localFolder.exists()) { removeLocalFolder(localFolder); } @@ -489,7 +479,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath()); cv.put(ProviderTableMeta.FILE_PATH_DECRYPTED, folder.getDecryptedRemotePath()); - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, user.getAccountName()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag()); @@ -523,7 +513,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); cv.put(ProviderTableMeta.FILE_PATH_DECRYPTED, file.getDecryptedRemotePath()); cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, user.getAccountName()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); @@ -562,7 +552,7 @@ public class FileDataStorageManager { // ""+file.getFileId()); Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, ocFile.getFileId()); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; - String[] whereArgs = new String[]{account.name, ocFile.getRemotePath()}; + String[] whereArgs = new String[]{user.getAccountName(), ocFile.getRemotePath()}; int deleted = 0; if (getContentProviderClient() != null) { try { @@ -619,7 +609,7 @@ public class FileDataStorageManager { Uri folderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(folder.getFileId())); // for recursive deletion String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; - String[] whereArgs = new String[]{account.name, folder.getRemotePath()}; + String[] whereArgs = new String[]{user.getAccountName(), folder.getRemotePath()}; int deleted = 0; if (getContentProviderClient() != null) { try { @@ -635,7 +625,7 @@ public class FileDataStorageManager { private boolean removeLocalFolder(OCFile folder) { boolean success = true; - String localFolderPath = FileStorageUtils.getDefaultSavePathFor(account.name, folder); + String localFolderPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), folder); File localFolder = new File(localFolderPath); if (localFolder.exists()) { @@ -704,7 +694,7 @@ public class FileDataStorageManager { ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[]{account.name, ocFile.getRemotePath() + "%"}, + new String[]{user.getAccountName(), ocFile.getRemotePath() + "%"}, ProviderTableMeta.FILE_PATH + " ASC " ); } catch (RemoteException e) { @@ -716,14 +706,14 @@ public class FileDataStorageManager { ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[]{account.name, ocFile.getRemotePath() + "%"}, + new String[]{user.getAccountName(), ocFile.getRemotePath() + "%"}, ProviderTableMeta.FILE_PATH + " ASC " ); } /// 2. prepare a batch of update operations to change all the descendants ArrayList operations = new ArrayList<>(cursor.getCount()); - String defaultSavePath = FileStorageUtils.getSavePath(account.name); + String defaultSavePath = FileStorageUtils.getSavePath(user.getAccountName()); List originalPathsToTriggerMediaScan = new ArrayList<>(); List newPathsToTriggerMediaScan = new ArrayList<>(); @@ -779,7 +769,7 @@ public class FileDataStorageManager { } /// 4. move in local file system - String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile); + String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile); String targetLocalPath = defaultSavePath + targetPath; File localFile = new File(originalLocalPath); boolean renamed = false; @@ -811,10 +801,10 @@ public class FileDataStorageManager { public void copyLocalFile(OCFile ocFile, String targetPath) { if (ocFile.fileExists() && !OCFile.ROOT_PATH.equals(ocFile.getFileName())) { - String localPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile); + String localPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile); File localFile = new File(localPath); boolean copied = false; - String defaultSavePath = FileStorageUtils.getSavePath(account.name); + String defaultSavePath = FileStorageUtils.getSavePath(user.getAccountName()); if (localFile.exists()) { File targetFile = new File(defaultSavePath + targetPath); File targetFolder = targetFile.getParentFile(); @@ -827,6 +817,10 @@ public class FileDataStorageManager { } } + /** + * This method does not require {@link FileDataStorageManager} being initialized + * with any specific user. Migration can be performed with {@link com.nextcloud.client.account.AnonymousUser}. + */ public void migrateStoredFiles(String sourcePath, String destinationPath) throws RemoteException, OperationApplicationException { Cursor cursor; @@ -961,14 +955,14 @@ public class FileDataStorageManager { key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[]{value, account.name}, null); + new String[]{value, user.getAccountName()}, null); } else { try { cursor = getContentProviderClient().query( ProviderTableMeta.CONTENT_URI, null, key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", new String[]{value, account.name}, + + "=?", new String[]{value, user.getAccountName()}, null); } catch (RemoteException e) { Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e); @@ -998,7 +992,7 @@ public class FileDataStorageManager { // try to find existing file and bind it with current account; // with the current update of SynchronizeFolderOperation, this won't be // necessary anymore after a full synchronization of the account - File file = new File(FileStorageUtils.getDefaultSavePathFor(account.name, ocFile)); + File file = new File(FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile)); if (file.exists()) { ocFile.setStoragePath(file.getAbsolutePath()); ocFile.setLastSyncDateForData(file.lastModified()); @@ -1159,7 +1153,7 @@ public class FileDataStorageManager { null, key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[]{value, account.name}, + new String[]{value, user.getAccountName()}, null ); } else { @@ -1168,7 +1162,7 @@ public class FileDataStorageManager { ProviderTableMeta.CONTENT_URI_SHARE, null, key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[]{value, account.name}, + new String[]{value, user.getAccountName()}, null ); } catch (RemoteException e) { @@ -1204,21 +1198,21 @@ public class FileDataStorageManager { selectionArgs = new String[]{ path, Integer.toString(type.getValue()), - account.name + user.getAccountName() }; } else { if (shareWith == null) { selectionArgs = new String[]{ path, Integer.toString(type.getValue()), - account.name, + user.getAccountName(), "" }; } else { selectionArgs = new String[]{ path, Integer.toString(type.getValue()), - account.name, + user.getAccountName(), shareWith }; } @@ -1273,7 +1267,7 @@ public class FileDataStorageManager { contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); - contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, user.getAccountName()); contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); @@ -1312,7 +1306,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; - String[] whereArgs = new String[]{account.name}; + String[] whereArgs = new String[]{user.getAccountName()}; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); @@ -1331,7 +1325,7 @@ public class FileDataStorageManager { contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + " = ?"; - String[] whereArgs = new String[]{account.name, String.valueOf(folder.getFileId())}; + String[] whereArgs = new String[]{user.getAccountName(), String.valueOf(folder.getFileId())}; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); @@ -1350,7 +1344,7 @@ public class FileDataStorageManager { contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; - String[] whereArgs = new String[]{account.name, filePath}; + String[] whereArgs = new String[]{user.getAccountName(), filePath}; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); @@ -1367,7 +1361,7 @@ public class FileDataStorageManager { @VisibleForTesting public void cleanShares() { String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; - String[] whereArgs = new String[]{account.name}; + String[] whereArgs = new String[]{user.getAccountName()}; if (getContentResolver() != null) { getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs); @@ -1450,7 +1444,7 @@ public class FileDataStorageManager { if (!file.isFolder()) { cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); } - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, user.getAccountName()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put( ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, @@ -1511,7 +1505,7 @@ public class FileDataStorageManager { Uri contentUriShare = ProviderTableMeta.CONTENT_URI_SHARE; String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + ProviderTableMeta._ID + " = ?"; - String[] whereArgs = {account.name, Long.toString(share.getId())}; + String[] whereArgs = {user.getAccountName(), Long.toString(share.getId())}; if (getContentProviderClient() != null) { try { @@ -1634,7 +1628,7 @@ public class FileDataStorageManager { if (folder != null) { String where = ProviderTableMeta.OCSHARES_PATH + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; - String[] whereArgs = new String[]{"", account.name}; + String[] whereArgs = new String[]{"", user.getAccountName()}; List files = getFolderContent(folder, false); @@ -1655,7 +1649,7 @@ public class FileDataStorageManager { String where = ProviderTableMeta.OCSHARES_PATH + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?"; - String[] whereArgs = new String[]{filePath, account.name}; + String[] whereArgs = new String[]{filePath, user.getAccountName()}; preparedOperations.add( ContentProviderOperation @@ -1888,7 +1882,7 @@ public class FileDataStorageManager { ProviderTableMeta.CONTENT_URI_FILE, projection, whereForDescencentsInConflict, - new String[]{account.name, parentPath + "%"}, + new String[]{user.getAccountName(), parentPath + "%"}, null ); } else { @@ -1897,7 +1891,7 @@ public class FileDataStorageManager { ProviderTableMeta.CONTENT_URI_FILE, projection, whereForDescencentsInConflict, - new String[]{account.name, parentPath + "%"}, + new String[]{user.getAccountName(), parentPath + "%"}, null ); } catch (RemoteException e) { @@ -1913,7 +1907,7 @@ public class FileDataStorageManager { cv, ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?", - new String[]{account.name, parentPath} + new String[]{user.getAccountName(), parentPath} ); } else { try { @@ -1922,7 +1916,7 @@ public class FileDataStorageManager { cv, ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?" - , new String[]{account.name, parentPath} + , new String[]{user.getAccountName(), parentPath} ); } catch (RemoteException e) { Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); @@ -1947,18 +1941,18 @@ public class FileDataStorageManager { public void saveCapabilities(OCCapability capability) { // Prepare capabilities data - ContentValues contentValues = createContentValues(account.name, capability); + ContentValues contentValues = createContentValues(user.getAccountName(), capability); - if (capabilityExists(account.name)) { + if (capabilityExists(user.getAccountName())) { if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, contentValues, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?", - new String[]{account.name}); + new String[]{user.getAccountName()}); } else { try { getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, contentValues, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?", - new String[]{account.name}); + new String[]{user.getAccountName()}); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } @@ -1980,7 +1974,7 @@ public class FileDataStorageManager { long new_id = Long.parseLong(result_uri.getPathSegments() .get(1)); capability.setId(new_id); - capability.setAccountName(account.name); + capability.setAccountName(user.getAccountName()); } } } @@ -2340,7 +2334,7 @@ public class FileDataStorageManager { public void deleteAllFiles() { String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "= ? AND " + ProviderTableMeta.FILE_PATH + "= ?"; - String[] whereArgs = new String[]{account.name, OCFile.ROOT_PATH}; + String[] whereArgs = new String[]{user.getAccountName(), OCFile.ROOT_PATH}; if (getContentResolver() != null) { getContentResolver().delete(ProviderTableMeta.CONTENT_URI_DIR, where, whereArgs); @@ -2348,14 +2342,14 @@ public class FileDataStorageManager { try { getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_DIR, where, whereArgs); } catch (RemoteException e) { - Log_OC.e(TAG, "Exception in deleteAllFiles for account " + account.name + ": " + e.getMessage(), e); + Log_OC.e(TAG, "Exception in deleteAllFiles for account " + user.getAccountName() + ": " + e.getMessage(), e); } } } public List getAllFiles() { String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER + "= ? "; - String[] selectionArgs = new String[]{account.name}; + String[] selectionArgs = new String[]{user.getAccountName()}; List folderContent = new ArrayList<>(); @@ -2409,7 +2403,8 @@ public class FileDataStorageManager { return this.contentProviderClient; } + @Deprecated public Account getAccount() { - return this.account; + return this.user.toPlatformAccount(); } } 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 cccd75af44..53fae01d95 100644 --- a/src/main/java/com/owncloud/android/files/services/FileDownloader.java +++ b/src/main/java/com/owncloud/android/files/services/FileDownloader.java @@ -41,6 +41,7 @@ import android.util.Pair; import com.nextcloud.client.account.User; import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.files.downloader.DownloadTask; +import com.nextcloud.java.util.Optional; import com.owncloud.android.R; import com.owncloud.android.authentication.AuthenticatorActivity; import com.owncloud.android.datamodel.FileDataStorageManager; @@ -104,7 +105,7 @@ public class FileDownloader extends Service private ServiceHandler mServiceHandler; private IBinder mBinder; private OwnCloudClient mDownloadClient; - private Account mCurrentAccount; + private Optional currentUser; private FileDataStorageManager mStorageManager; private IndexedForest mPendingDownloads = new IndexedForest<>(); @@ -210,7 +211,7 @@ public class FileDownloader extends Service conflictUploadId = intent.getLongExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, -1); AbstractList requestedDownloads = new Vector(); try { - DownloadFileOperation newDownload = new DownloadFileOperation(user.toPlatformAccount(), + DownloadFileOperation newDownload = new DownloadFileOperation(user, file, behaviour, activityName, @@ -303,9 +304,9 @@ public class FileDownloader extends Service if (download != null) { download.cancel(); } else { - if (mCurrentDownload != null && mCurrentAccount != null && + if (mCurrentDownload != null && currentUser.isPresent() && mCurrentDownload.getRemotePath().startsWith(file.getRemotePath()) && - account.name.equals(mCurrentAccount.name)) { + account.name.equals(currentUser.get().getAccountName())) { mCurrentDownload.cancel(); } } @@ -456,18 +457,16 @@ public class FileDownloader extends Service RemoteOperationResult downloadResult = null; try { /// prepare client object to send the request to the ownCloud server - if (mCurrentAccount == null || - !mCurrentAccount.equals(mCurrentDownload.getAccount())) { - mCurrentAccount = mCurrentDownload.getAccount(); - mStorageManager = new FileDataStorageManager( - mCurrentAccount, - getContentResolver() - ); + Account currentDownloadAccount = mCurrentDownload.getAccount(); + Optional currentDownloadUser = accountManager.getUser(currentDownloadAccount.name); + if (!currentUser.equals(currentDownloadUser)) { + currentUser = currentDownloadUser; + mStorageManager = new FileDataStorageManager(currentUser.get(), getContentResolver()); } // else, reuse storage manager from previous operation // always get client from client manager, to get fresh credentials in case // of update - OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this); + OwnCloudAccount ocAccount = currentDownloadUser.get().toOwnCloudAccount(); mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(ocAccount, this); @@ -484,7 +483,7 @@ public class FileDownloader extends Service } finally { Pair removeResult = mPendingDownloads.removePayload( - mCurrentAccount.name, mCurrentDownload.getRemotePath()); + currentUser.get().getAccountName(), mCurrentDownload.getRemotePath()); if (downloadResult == null) { downloadResult = new RemoteOperationResult(new RuntimeException("Error downloading…")); diff --git a/src/main/java/com/owncloud/android/files/services/FileUploader.java b/src/main/java/com/owncloud/android/files/services/FileUploader.java index 4835a1a275..7d13fc717e 100644 --- a/src/main/java/com/owncloud/android/files/services/FileUploader.java +++ b/src/main/java/com/owncloud/android/files/services/FileUploader.java @@ -491,7 +491,7 @@ public class FileUploader extends Service onWifiOnly, whileChargingOnly, disableRetries, - new FileDataStorageManager(user.toPlatformAccount(), getContentResolver()) + new FileDataStorageManager(user, getContentResolver()) ); newUpload.setCreatedBy(createdBy); if (isCreateRemoteFolder) { @@ -541,7 +541,7 @@ public class FileUploader extends Service onWifiOnly, whileChargingOnly, true, - new FileDataStorageManager(user.toPlatformAccount(), getContentResolver()) + new FileDataStorageManager(user, getContentResolver()) ); newUpload.addDataTransferProgressListener(this); @@ -623,7 +623,7 @@ public class FileUploader extends Service /// prepare client object to send the request to the ownCloud server if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) { mCurrentAccount = mCurrentUpload.getAccount(); - mStorageManager = new FileDataStorageManager(mCurrentAccount, getContentResolver()); + mStorageManager = new FileDataStorageManager(getCurrentUser().get(), getContentResolver()); } // else, reuse storage manager from previous operation // always get client from client manager, to get fresh credentials in case of update diff --git a/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java b/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java index ec2dfe39f7..4b3df0e523 100644 --- a/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java +++ b/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java @@ -26,6 +26,7 @@ import android.content.Context; import android.text.TextUtils; import android.webkit.MimeTypeMap; +import com.nextcloud.client.account.User; import com.owncloud.android.datamodel.DecryptedFolderMetadata; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; @@ -52,7 +53,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class DownloadFileOperation extends RemoteOperation { private static final String TAG = DownloadFileOperation.class.getSimpleName(); - private Account account; + private User user; private OCFile file; private String behaviour; private String etag = ""; @@ -66,10 +67,10 @@ public class DownloadFileOperation extends RemoteOperation { private final AtomicBoolean cancellationRequested = new AtomicBoolean(false); - public DownloadFileOperation(Account account, OCFile file, String behaviour, String activityName, + public DownloadFileOperation(User user, OCFile file, String behaviour, String activityName, String packageName, Context context) { - if (account == null) { - throw new IllegalArgumentException("Illegal null account in DownloadFileOperation " + + if (user == null) { + throw new IllegalArgumentException("Illegal null user in DownloadFileOperation " + "creation"); } if (file == null) { @@ -77,7 +78,7 @@ public class DownloadFileOperation extends RemoteOperation { "creation"); } - this.account = account; + this.user = user; this.file = file; this.behaviour = behaviour; this.activityName = activityName; @@ -85,8 +86,8 @@ public class DownloadFileOperation extends RemoteOperation { this.context = context; } - public DownloadFileOperation(Account account, OCFile file, Context context) { - this(account, file, null, null, null, context); + public DownloadFileOperation(User user, OCFile file, Context context) { + this(user, file, null, null, null, context); } public String getSavePath() { @@ -100,15 +101,15 @@ public class DownloadFileOperation extends RemoteOperation { return path.getAbsolutePath(); } } - return FileStorageUtils.getDefaultSavePathFor(account.name, file); + return FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), file); } public String getTmpPath() { - return FileStorageUtils.getTemporalPath(account.name) + file.getRemotePath(); + return FileStorageUtils.getTemporalPath(user.getAccountName()) + file.getRemotePath(); } public String getTmpFolder() { - return FileStorageUtils.getTemporalPath(account.name); + return FileStorageUtils.getTemporalPath(user.getAccountName()); } public String getRemotePath() { @@ -177,11 +178,14 @@ public class DownloadFileOperation extends RemoteOperation { // decrypt file if (file.isEncrypted()) { - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account, context.getContentResolver()); + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.getContentResolver()); OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath()); - DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, context, account); + DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, + client, + context, + user.toPlatformAccount()); if (metadata == null) { return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND); @@ -235,8 +239,13 @@ public class DownloadFileOperation extends RemoteOperation { } } + @Deprecated public Account getAccount() { - return this.account; + return this.user.toPlatformAccount(); + } + + public User getUser() { + return this.user; } public OCFile getFile() { diff --git a/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java b/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java index aa07a13ac7..d6803b733c 100644 --- a/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java +++ b/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java @@ -198,6 +198,7 @@ public class DocumentsStorageProvider extends DocumentsProvider { OCFile ocFile = document.getFile(); Account account = document.getAccount(); + User user = accountManager.getUser(account.name).orElseGet(accountManager::getAnonymousUser); int accessMode = ParcelFileDescriptor.parseMode(mode); boolean writeOnly = (accessMode & MODE_WRITE_ONLY) != 0; @@ -208,7 +209,7 @@ public class DocumentsStorageProvider extends DocumentsProvider { // TODO show a conflict notification with a pending intent that shows a ConflictResolveDialog Log_OC.w(TAG, "Conflict found!"); } else { - DownloadFileOperation downloadFileOperation = new DownloadFileOperation(account, ocFile, context); + DownloadFileOperation downloadFileOperation = new DownloadFileOperation(user, ocFile, context); RemoteOperationResult result = downloadFileOperation.execute(document.getClient()); if (!result.isSuccess()) { if (ocFile.isDown()) { @@ -674,9 +675,9 @@ public class DocumentsStorageProvider extends DocumentsProvider { ContentResolver contentResolver = getContext().getContentResolver(); - for (Account account : accountManager.getAccounts()) { - final FileDataStorageManager storageManager = new FileDataStorageManager(account, contentResolver); - rootIdToStorageManager.put(account.hashCode(), storageManager); + for (User user : accountManager.getAllUsers()) { + final FileDataStorageManager storageManager = new FileDataStorageManager(user, contentResolver); + rootIdToStorageManager.put(user.hashCode(), storageManager); } } diff --git a/src/main/java/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/main/java/com/owncloud/android/syncadapter/FileSyncAdapter.java index 91ef7b725d..5ffe218bf1 100644 --- a/src/main/java/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/main/java/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -160,7 +160,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { this.setAccount(account); this.setContentProviderClient(providerClient); - this.setStorageManager(new FileDataStorageManager(account, providerClient)); + this.setStorageManager(new FileDataStorageManager(getUser(), providerClient)); try { this.initClientForCurrentAccount(); @@ -480,7 +480,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { /// includes a pending intent in the notification showing a more detailed explanation Intent explanationIntent = new Intent(getContext(), ErrorsWhileCopyingHandlerActivity.class); - explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_ACCOUNT, getAccount()); + explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_USER, getUser()); ArrayList remotePaths = new ArrayList(); ArrayList localPaths = new ArrayList(); remotePaths.addAll(mForgottenLocalFiles.keySet()); diff --git a/src/main/java/com/owncloud/android/ui/activity/ErrorsWhileCopyingHandlerActivity.java b/src/main/java/com/owncloud/android/ui/activity/ErrorsWhileCopyingHandlerActivity.java index 9111a17741..b796cbc832 100644 --- a/src/main/java/com/owncloud/android/ui/activity/ErrorsWhileCopyingHandlerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/ErrorsWhileCopyingHandlerActivity.java @@ -20,7 +20,6 @@ package com.owncloud.android.ui.activity; -import android.accounts.Account; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; @@ -36,6 +35,7 @@ import android.widget.Button; import android.widget.ListView; import android.widget.TextView; +import com.nextcloud.client.account.User; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; @@ -65,8 +65,8 @@ public class ErrorsWhileCopyingHandlerActivity extends AppCompatActivity implem private static final String TAG = ErrorsWhileCopyingHandlerActivity.class.getSimpleName(); - public static final String EXTRA_ACCOUNT = - ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_ACCOUNT"; + public static final String EXTRA_USER = + ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_ACCOUNT"; public static final String EXTRA_LOCAL_PATHS = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_LOCAL_PATHS"; public static final String EXTRA_REMOTE_PATHS = @@ -74,7 +74,7 @@ public class ErrorsWhileCopyingHandlerActivity extends AppCompatActivity implem private static final String WAIT_DIALOG_TAG = "WAIT_DIALOG"; - protected Account mAccount; + protected User user; protected FileDataStorageManager mStorageManager; protected List mLocalPaths; protected List mRemotePaths; @@ -91,10 +91,10 @@ public class ErrorsWhileCopyingHandlerActivity extends AppCompatActivity implem /// read extra parameters in intent Intent intent = getIntent(); - mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); + user = intent.getParcelableExtra(EXTRA_USER); mRemotePaths = intent.getStringArrayListExtra(EXTRA_REMOTE_PATHS); mLocalPaths = intent.getStringArrayListExtra(EXTRA_LOCAL_PATHS); - mStorageManager = new FileDataStorageManager(mAccount, getContentResolver()); + mStorageManager = new FileDataStorageManager(user, getContentResolver()); mHandler = new Handler(); if (mCurrentDialog != null) { mCurrentDialog.dismiss(); @@ -108,7 +108,7 @@ public class ErrorsWhileCopyingHandlerActivity extends AppCompatActivity implem TextView textView = findViewById(R.id.message); String appName = getString(R.string.app_name); String message = String.format(getString(R.string.sync_foreign_files_forgotten_explanation), - appName, appName, appName, appName, mAccount.name); + appName, appName, appName, appName, user.getAccountName()); textView.setText(message); textView.setMovementMethod(new ScrollingMovementMethod()); @@ -230,7 +230,7 @@ public class ErrorsWhileCopyingHandlerActivity extends AppCompatActivity implem while (!mLocalPaths.isEmpty()) { String currentPath = mLocalPaths.get(0); File currentFile = new File(currentPath); - String expectedPath = FileStorageUtils.getSavePath(mAccount.name) + mRemotePaths.get(0); + String expectedPath = FileStorageUtils.getSavePath(user.getAccountName()) + mRemotePaths.get(0); File expectedFile = new File(expectedPath); if (expectedFile.equals(currentFile) || currentFile.renameTo(expectedFile)) { diff --git a/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java b/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java index db82aff5b7..bf75c56a34 100644 --- a/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java @@ -705,8 +705,7 @@ public class SettingsActivity extends ThemedPreferenceActivity if (storagePath.equals(newPath)) { return true; } - - StorageMigration storageMigration = new StorageMigration(this, storagePath, newPath); + StorageMigration storageMigration = new StorageMigration(this, user, storagePath, newPath); storageMigration.setStorageMigrationProgressListener(this); storageMigration.migrate(); 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 9c22028271..7d987e3e01 100644 --- a/src/main/java/com/owncloud/android/ui/activity/StorageMigration.java +++ b/src/main/java/com/owncloud/android/ui/activity/StorageMigration.java @@ -30,6 +30,7 @@ import android.content.DialogInterface.OnClickListener; import android.os.AsyncTask; import android.view.View; +import com.nextcloud.client.account.User; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; @@ -48,6 +49,7 @@ public class StorageMigration { private static final String TAG = StorageMigration.class.getName(); private Context mContext; + private User user; private String mSourceStoragePath; private String mTargetStoragePath; @@ -58,8 +60,9 @@ public class StorageMigration { void onCancelMigration(); } - public StorageMigration(Context context, String sourcePath, String targetPath) { + public StorageMigration(Context context, User user, String sourcePath, String targetPath) { mContext = context; + this.user = user; mSourceStoragePath = sourcePath; mTargetStoragePath = targetPath; } @@ -76,6 +79,7 @@ public class StorageMigration { progressDialog.show(); new FileMigrationTask( mContext, + user, mSourceStoragePath, mTargetStoragePath, progressDialog, @@ -118,6 +122,7 @@ public class StorageMigration { progressDialog.show(); new StoragePathSwitchTask( mContext, + user, mSourceStoragePath, mTargetStoragePath, progressDialog, @@ -134,6 +139,7 @@ public class StorageMigration { progressDialog.show(); new FileMigrationTask( mContext, + user, mSourceStoragePath, mTargetStoragePath, progressDialog, @@ -167,6 +173,7 @@ public class StorageMigration { protected String mStorageSource; protected String mStorageTarget; protected Context mContext; + protected User user; protected ProgressDialog mProgressDialog; protected StorageMigrationProgressListener mListener; @@ -174,11 +181,13 @@ public class StorageMigration { protected Account[] mOcAccounts; public FileMigrationTaskBase(Context context, + User user, String source, String target, ProgressDialog progressDialog, StorageMigrationProgressListener listener) throws SecurityException { mContext = context; + this.user = user; mStorageSource = source; mStorageTarget = target; mProgressDialog = progressDialog; @@ -290,11 +299,12 @@ public class StorageMigration { static private class StoragePathSwitchTask extends FileMigrationTaskBase { public StoragePathSwitchTask(Context context, + User user, String source, String target, ProgressDialog progressDialog, StorageMigrationProgressListener listener) { - super(context, source, target, progressDialog, listener); + super(context, user, source, target, progressDialog, listener); } @Override @@ -337,11 +347,12 @@ public class StorageMigration { } public FileMigrationTask(Context context, + User user, String source, String target, ProgressDialog progressDialog, StorageMigrationProgressListener listener) { - super(context, source, target, progressDialog, listener); + super(context, user, source, target, progressDialog, listener); } @Override @@ -436,8 +447,7 @@ public class StorageMigration { } private void updateIndex(Context context) throws MigrationException { - final Account nullAccount = null; - FileDataStorageManager manager = new FileDataStorageManager(nullAccount, context.getContentResolver()); + FileDataStorageManager manager = new FileDataStorageManager(user, context.getContentResolver()); try { manager.migrateStoredFiles(mStorageSource, mStorageTarget); diff --git a/src/main/java/com/owncloud/android/utils/theme/CapabilityUtils.java b/src/main/java/com/owncloud/android/utils/theme/CapabilityUtils.java index b08f75d375..ff801f46ee 100644 --- a/src/main/java/com/owncloud/android/utils/theme/CapabilityUtils.java +++ b/src/main/java/com/owncloud/android/utils/theme/CapabilityUtils.java @@ -22,9 +22,9 @@ */ package com.owncloud.android.utils.theme; -import android.accounts.Account; import android.content.Context; +import com.nextcloud.client.account.User; import com.nextcloud.client.account.UserAccountManagerImpl; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.lib.resources.status.OCCapability; @@ -34,22 +34,15 @@ import com.owncloud.android.lib.resources.status.OCCapability; */ public final class CapabilityUtils { public static OCCapability getCapability(Context context) { - return getCapability(null, context); - } - - private static OCCapability getCapability(Account acc, Context context) { - Account account = null; - - if (acc != null) { - account = acc; - } else if (context != null) { + User user = null; + if (context != null) { // TODO: refactor when dark theme work is completed - account = UserAccountManagerImpl.fromContext(context).getCurrentAccount(); + user = UserAccountManagerImpl.fromContext(context).getUser(); } - if (account != null) { - FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver()); - return storageManager.getCapability(account.name); + if (user != null) { + FileDataStorageManager storageManager = new FileDataStorageManager(user, context.getContentResolver()); + return storageManager.getCapability(user.getAccountName()); } else { return new OCCapability(); } diff --git a/src/main/java/com/owncloud/android/utils/theme/ThemeColorUtils.java b/src/main/java/com/owncloud/android/utils/theme/ThemeColorUtils.java index 301a887b80..5eccae4f02 100644 --- a/src/main/java/com/owncloud/android/utils/theme/ThemeColorUtils.java +++ b/src/main/java/com/owncloud/android/utils/theme/ThemeColorUtils.java @@ -29,6 +29,7 @@ import android.graphics.Color; import com.nextcloud.client.account.User; import com.nextcloud.client.account.UserAccountManagerImpl; +import com.nextcloud.java.util.Optional; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.lib.resources.status.OCCapability; @@ -290,18 +291,18 @@ public final class ThemeColorUtils { } private static OCCapability getCapability(Account acc, Context context) { - Account account = null; + Optional user = Optional.empty(); if (acc != null) { - account = acc; + user = UserAccountManagerImpl.fromContext(context).getUser(acc.name); } else if (context != null) { // TODO: refactor when dark theme work is completed - account = UserAccountManagerImpl.fromContext(context).getCurrentAccount(); + user = Optional.of(UserAccountManagerImpl.fromContext(context).getUser()); } - if (account != null) { - FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver()); - return storageManager.getCapability(account.name); + if (user.isPresent()) { + FileDataStorageManager storageManager = new FileDataStorageManager(user.get(), context.getContentResolver()); + return storageManager.getCapability(user.get().getAccountName()); } else { return new OCCapability(); }