Browse Source

Merge pull request #9330 from nextcloud/remove-account-from-file-data-storage-manager-implementation

Remove platform Account from FileDataStorageManager implementation
Álvaro Brey 3 years ago
parent
commit
1e5b4604fb

+ 4 - 5
src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2012  Bartek Przybylski
  * Copyright (C) 2015 ownCloud Inc.
+ * Copyright (C) 2021 Chris Narkiewicz <hello@ezaquarii.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2,
@@ -19,7 +20,6 @@
 
 package com.owncloud.android.datamodel;
 
-import android.accounts.Account;
 import android.content.ContentProviderClient;
 import android.content.ContentProviderOperation;
 import android.content.ContentProviderResult;
@@ -309,7 +309,7 @@ public class FileDataStorageManager {
                 // remote request
                 ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath);
                 // TODO Deprecated
-                RemoteOperationResult result = operation.execute(getAccount(), context);
+                RemoteOperationResult result = operation.execute(getUser().toPlatformAccount(), context);
                 if (result.isSuccess()) {
                     OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
 
@@ -2404,8 +2404,7 @@ public class FileDataStorageManager {
         return this.contentProviderClient;
     }
 
-    @Deprecated
-    public Account getAccount() {
-        return this.user.toPlatformAccount();
+    public User getUser() {
+        return user;
     }
 }

+ 8 - 12
src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -254,7 +254,7 @@ public final class ThumbnailsCacheManager {
     public static class ResizedImageGenerationTask extends AsyncTask<Object, Void, Bitmap> {
         private FileFragment fileFragment;
         private FileDataStorageManager storageManager;
-        private Account account;
+        private User user;
         private WeakReference<ImageView> imageViewReference;
         private WeakReference<FrameLayout> frameLayoutReference;
         private OCFile file;
@@ -267,15 +267,14 @@ public final class ThumbnailsCacheManager {
                                           FrameLayout emptyListProgress,
                                           FileDataStorageManager storageManager,
                                           ConnectivityService connectivityService,
-                                          Account account,
-                                          int backgroundColor)
-                throws IllegalArgumentException {
+                                          User user,
+                                          int backgroundColor) throws IllegalArgumentException {
             this.fileFragment = fileFragment;
             imageViewReference = new WeakReference<>(imageView);
             frameLayoutReference = new WeakReference<>(emptyListProgress);
             this.storageManager = storageManager;
             this.connectivityService = connectivityService;
-            this.account = account;
+            this.user = user;
             this.backgroundColor = backgroundColor;
         }
 
@@ -286,11 +285,8 @@ public final class ThumbnailsCacheManager {
             file = (OCFile) params[0];
 
             try {
-                if (account != null) {
-                    OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
-                    mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount,
-                                                                                              MainApp.getAppContext());
-                }
+                mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(user.toOwnCloudAccount(),
+                                                                                          MainApp.getAppContext());
 
                 thumbnail = doResizedImageInBackground();
 
@@ -1206,7 +1202,7 @@ public final class ThumbnailsCacheManager {
         }
     }
 
-    public static void generateThumbnailFromOCFile(OCFile file, Account account, Context context) {
+    public static void generateThumbnailFromOCFile(OCFile file, User user, Context context) {
         int pxW;
         int pxH;
         pxW = pxH = getThumbnailDimension();
@@ -1219,7 +1215,7 @@ public final class ThumbnailsCacheManager {
 
             OwnCloudClient client = mClient;
             if (client == null) {
-                OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
+                OwnCloudAccount ocAccount = user.toOwnCloudAccount();
                 client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context);
             }
 

+ 1 - 1
src/main/java/com/owncloud/android/operations/CheckCurrentCredentialsOperation.java

@@ -46,7 +46,7 @@ public class CheckCurrentCredentialsOperation extends SyncOperation {
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
         RemoteOperationResult result;
-        boolean validAccount = user.nameEquals(getStorageManager().getAccount().name);
+        boolean validAccount = user.nameEquals(getStorageManager().getUser().getAccountName());
         if (!validAccount) {
             result = new RemoteOperationResult(new IllegalStateException(
                 "Account to validate is not the account connected to!")

+ 2 - 2
src/main/java/com/owncloud/android/operations/GetCapabilitiesOperation.java

@@ -40,8 +40,8 @@ public class GetCapabilitiesOperation extends SyncOperation {
         final FileDataStorageManager storageManager = getStorageManager();
 
         OCCapability currentCapability = null;
-        if (storageManager.getAccount() != null) {
-            currentCapability = storageManager.getCapability(storageManager.getAccount().name);
+        if (!storageManager.getUser().isAnonymous()) {
+            currentCapability = storageManager.getCapability(storageManager.getUser().getAccountName());
         }
 
         RemoteOperationResult result = new GetCapabilitiesRemoteOperation(currentCapability).execute(client);

+ 1 - 1
src/main/java/com/owncloud/android/operations/GetUserProfileOperation.java

@@ -63,7 +63,7 @@ public class GetUserProfileOperation extends SyncOperation {
             // store display name with account data
             AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
             UserInfo userInfo = result.getResultData();
-            Account storedAccount = getStorageManager().getAccount();
+            Account storedAccount = getStorageManager().getUser().toPlatformAccount();
             accountManager.setUserData(storedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
         }
         return result;

+ 1 - 1
src/main/java/com/owncloud/android/operations/UnshareOperation.java

@@ -75,7 +75,7 @@ public class UnshareOperation extends SyncOperation {
                     // Check if it is the last share
                     List<OCShare> sharesWith = getStorageManager().
                         getSharesWithForAFile(remotePath,
-                                              getStorageManager().getAccount().name);
+                                              getStorageManager().getUser().getAccountName());
                     if (sharesWith.size() == SINGLY_SHARED) {
                         file.setSharedWithSharee(false);
                     }

+ 3 - 3
src/main/java/com/owncloud/android/operations/common/SyncOperation.java

@@ -57,11 +57,11 @@ public abstract class SyncOperation extends RemoteOperation {
      * @return Result of the operation.
      */
     public RemoteOperationResult execute(Context context) {
-        if (storageManager.getAccount() == null) {
+        if (storageManager.getUser().isAnonymous()) {
             throw new IllegalArgumentException("Trying to execute a sync operation with a " +
-                                                   "storage manager for a NULL account");
+                                                   "storage manager for an anonymous account");
         }
-        return super.execute(this.storageManager.getAccount(), context);
+        return super.execute(this.storageManager.getUser().toPlatformAccount(), context);
     }
 
     public RemoteOperationResult execute(@NonNull NextcloudClient client) {

+ 14 - 20
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -4,7 +4,7 @@
  *   @author Bartosz Przybylski
  *   @author Chris Narkiewicz
  *   Copyright (C) 2016  Bartosz Przybylski <bart.p.pl@gmail.com>
- *   Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
+ *   Copyright (C) 2021 Chris Narkiewicz <hello@ezaquarii.com>
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
@@ -44,7 +44,6 @@ import android.util.Log;
 import android.util.SparseArray;
 import android.widget.Toast;
 
-import com.nextcloud.client.account.AnonymousUser;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManagerImpl;
@@ -62,7 +61,6 @@ import com.owncloud.android.files.services.NameCollisionPolicy;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
-import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.files.CheckEtagRemoteOperation;
@@ -93,7 +91,6 @@ import java.util.concurrent.TimeUnit;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.VisibleForTesting;
-import dagger.android.AndroidInjection;
 
 import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
 import static android.os.ParcelFileDescriptor.MODE_WRITE_ONLY;
@@ -197,8 +194,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         Context context = getNonNullContext();
 
         OCFile ocFile = document.getFile();
-        Account account = document.getAccount();
-        User user = accountManager.getUser(account.name).orElseGet(accountManager::getAnonymousUser);
+        User user = document.getUser();
 
         int accessMode = ParcelFileDescriptor.parseMode(mode);
         boolean writeOnly = (accessMode & MODE_WRITE_ONLY) != 0;
@@ -245,7 +241,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
                         // upload file with FileUploader service (off main thread)
                         FileUploader.uploadUpdateFile(
                             context,
-                            account,
+                            user.toPlatformAccount(),
                             ocFile,
                             LOCAL_BEHAVIOUR_DELETE,
                             NameCollisionPolicy.OVERWRITE,
@@ -266,7 +262,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         Context context = getNonNullContext();
         OCFile ocFile = document.getFile();
         RemoteOperationResult result = new CheckEtagRemoteOperation(ocFile.getRemotePath(), ocFile.getEtag())
-            .execute(document.getAccount(), context);
+            .execute(document.getUser().toPlatformAccount(), context);
         switch (result.getCode()) {
             case ETAG_CHANGED:
                 return true;
@@ -328,7 +324,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         boolean exists = ThumbnailsCacheManager.containsBitmap(ThumbnailsCacheManager.PREFIX_THUMBNAIL
                                                                    + file.getRemoteId());
         if (!exists) {
-            ThumbnailsCacheManager.generateThumbnailFromOCFile(file, document.getAccount(), getContext());
+            ThumbnailsCacheManager.generateThumbnailFromOCFile(file, document.getUser(), getContext());
         }
 
         return new AssetFileDescriptor(DiskLruImageCacheFileProvider.getParcelFileDescriptorForOCFile(file),
@@ -592,7 +588,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         OCFile file = document.getStorageManager().getFileByPath(document.getRemotePath());
         RemoteOperationResult result = new RemoveFileOperation(file,
                                                                false,
-                                                               document.getAccount(),
+                                                               document.getUser().toPlatformAccount(),
                                                                true,
                                                                context,
                                                                document.getStorageManager())
@@ -643,7 +639,9 @@ public class DocumentsStorageProvider extends DocumentsProvider {
             // The solution below uses paths and is faster by a factor of 2-10 depending on the nesting level of child.
             // So far, the same document with its unique ID can never be in two places at once.
             // If this assumption ever changes, this code would need to be adapted.
-            return parentDocument.getAccount() == currentDocument.getAccount() && childPath.startsWith(parentPath);
+            User parentDocumentOwner = parentDocument.getUser();
+            User currentDocumentOwner = currentDocument.getUser();
+            return parentDocumentOwner.nameEquals(currentDocumentOwner) && childPath.startsWith(parentPath);
 
         } catch (FileNotFoundException e) {
             Log.e(TAG, "failed to check for child document", e);
@@ -661,7 +659,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
     private FileDataStorageManager getStorageManager(String rootId) {
         for(int i = 0; i < rootIdToStorageManager.size(); i++) {
             FileDataStorageManager storageManager = rootIdToStorageManager.valueAt(i);
-            if (storageManager.getAccount().name.equals(rootId)) {
+            if (storageManager.getUser().nameEquals(rootId)) {
                 return storageManager;
             }
         }
@@ -796,13 +794,8 @@ public class DocumentsStorageProvider extends DocumentsProvider {
             return storageManager;
         }
 
-        public Account getAccount() {
-            return getStorageManager().getAccount();
-        }
-
         public User getUser() {
-            Account account = getAccount();
-            return accountManager.getUser(account.name).orElseGet(accountManager::getAnonymousUser);
+            return getStorageManager().getUser();
         }
 
         public OCFile getFile() {
@@ -815,9 +808,10 @@ public class DocumentsStorageProvider extends DocumentsProvider {
 
         OwnCloudClient getClient() {
             try {
-                OwnCloudAccount ocAccount = new OwnCloudAccount(getAccount(), MainApp.getAppContext());
+
+                OwnCloudAccount ocAccount = getUser().toOwnCloudAccount();
                 return OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, getContext());
-            } catch (OperationCanceledException | IOException | AuthenticatorException | AccountNotFoundException e) {
+            } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                 Log_OC.e(TAG, "Failed to set client", e);
             }
             return null;

+ 1 - 1
src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -565,7 +565,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener,
                                                                               toolbarActivity.getPreviewImageContainer(),
                                                                               containerActivity.getStorageManager(),
                                                                               connectivityService,
-                                                                              containerActivity.getStorageManager().getAccount(),
+                                                                              containerActivity.getStorageManager().getUser(),
                                                                               getResources().getColor(R.color.background_color_inverse)
                         );
 

+ 1 - 1
src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java

@@ -395,7 +395,7 @@ public class FileDetailSharingFragment extends Fragment implements ShareeListAda
 
         // to show share with users/groups info
         List<OCShare> shares = fileDataStorageManager.getSharesWithForAFile(file.getRemotePath(),
-                                                                            user.toPlatformAccount().name);
+                                                                            user.getAccountName());
 
         adapter.addShares(shares);
 

+ 1 - 1
src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -270,7 +270,7 @@ public class PreviewImageFragment extends FileFragment implements Injectable {
                                                                                   binding.emptyListProgress,
                                                                                   containerActivity.getStorageManager(),
                                                                                   connectivityService,
-                                                                                  containerActivity.getStorageManager().getAccount(),
+                                                                                  containerActivity.getStorageManager().getUser(),
                                                                                   getResources().getColor(R.color.background_color_inverse)
                             );
                         if (resizedImage == null) {

+ 15 - 11
src/main/java/org/nextcloud/providers/cursors/RootCursor.java

@@ -20,19 +20,24 @@
 
 package org.nextcloud.providers.cursors;
 
-import android.accounts.Account;
 import android.content.Context;
 import android.database.MatrixCursor;
 import android.provider.DocumentsContract.Root;
 
+import com.nextcloud.client.account.User;
 import com.owncloud.android.R;
 import com.owncloud.android.providers.DocumentsStorageProvider;
 
 public class RootCursor extends MatrixCursor {
 
     private static final String[] DEFAULT_ROOT_PROJECTION = new String[] {
-            Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_ICON, Root.COLUMN_TITLE,
-            Root.COLUMN_DOCUMENT_ID, Root.COLUMN_AVAILABLE_BYTES, Root.COLUMN_SUMMARY,
+            Root.COLUMN_ROOT_ID,
+            Root.COLUMN_FLAGS,
+            Root.COLUMN_ICON,
+            Root.COLUMN_TITLE,
+            Root.COLUMN_DOCUMENT_ID,
+            Root.COLUMN_AVAILABLE_BYTES,
+            Root.COLUMN_SUMMARY,
             Root.COLUMN_FLAGS
     };
 
@@ -41,17 +46,16 @@ public class RootCursor extends MatrixCursor {
     }
 
     public void addRoot(DocumentsStorageProvider.Document document, Context context) {
-        Account account = document.getAccount();
+        User user = document.getUser();
 
-        int rootFlags =
-            Root.FLAG_SUPPORTS_CREATE |
-                Root.FLAG_SUPPORTS_RECENTS |
-                Root.FLAG_SUPPORTS_SEARCH |
-                Root.FLAG_SUPPORTS_IS_CHILD;
+        int rootFlags = Root.FLAG_SUPPORTS_CREATE
+                      | Root.FLAG_SUPPORTS_RECENTS
+                      | Root.FLAG_SUPPORTS_SEARCH
+                      | Root.FLAG_SUPPORTS_IS_CHILD;
 
-        newRow().add(Root.COLUMN_ROOT_ID, account.name)
+        newRow().add(Root.COLUMN_ROOT_ID, user.getAccountName())
             .add(Root.COLUMN_DOCUMENT_ID, document.getDocumentId())
-            .add(Root.COLUMN_SUMMARY, account.name)
+            .add(Root.COLUMN_SUMMARY, user.getAccountName())
             .add(Root.COLUMN_TITLE, context.getString(R.string.app_name))
             .add(Root.COLUMN_ICON, R.mipmap.ic_launcher)
             .add(Root.COLUMN_FLAGS, rootFlags);