Эх сурвалжийг харах

Migrate UploadFileOperation to User model

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
Chris Narkiewicz 4 жил өмнө
parent
commit
7bb49bd610

+ 8 - 2
src/androidTest/java/com/owncloud/android/AbstractIT.java

@@ -12,12 +12,14 @@ import android.os.Bundle;
 
 import com.facebook.testing.screenshot.Screenshot;
 import com.nextcloud.client.RetryTestRule;
+import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManagerImpl;
 import com.nextcloud.client.device.BatteryStatus;
 import com.nextcloud.client.device.PowerManagementService;
 import com.nextcloud.client.network.Connectivity;
 import com.nextcloud.client.network.ConnectivityService;
+import com.nextcloud.java.util.Optional;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.UploadsStorageManager;
@@ -73,6 +75,7 @@ public abstract class AbstractIT {
 
     protected static OwnCloudClient client;
     protected static Account account;
+    protected static User user;
     protected static Context targetContext;
 
     private Activity currentActivity;
@@ -109,6 +112,9 @@ public abstract class AbstractIT {
                 throw new ActivityNotFoundException();
             }
 
+            Optional<User> optionalUser = userAccountManager.getUser(account.name);
+            user = optionalUser.orElseThrow(IllegalAccessError::new);
+
             client = OwnCloudClientFactory.createOwnCloudClient(account, targetContext);
 
             createDummyFiles();
@@ -263,7 +269,7 @@ public abstract class AbstractIT {
     }
 
     public OCFile createFolder(String remotePath) {
-        TestCase.assertTrue(new CreateFolderOperation(remotePath, account, targetContext)
+        TestCase.assertTrue(new CreateFolderOperation(remotePath, user, targetContext)
                                 .execute(client, getStorageManager())
                                 .isSuccess());
 
@@ -309,7 +315,7 @@ public abstract class AbstractIT {
             uploadsStorageManager,
             connectivityServiceMock,
             powerManagementServiceMock,
-            account,
+            user,
             null,
             ocUpload,
             FileUploader.NameCollisionPolicy.DEFAULT,

+ 6 - 6
src/androidTest/java/com/owncloud/android/files/services/FileUploaderIT.kt

@@ -82,7 +82,7 @@ class FileUploaderIT : AbstractIT() {
                 uploadsStorageManager,
                 connectivityServiceMock,
                 powerManagementServiceMock,
-                account,
+                user,
                 null,
                 ocUpload,
                 FileUploader.NameCollisionPolicy.DEFAULT,
@@ -108,7 +108,7 @@ class FileUploaderIT : AbstractIT() {
                 uploadsStorageManager,
                 connectivityServiceMock,
                 powerManagementServiceMock,
-                account,
+                user,
                 null,
                 ocUpload2,
                 FileUploader.NameCollisionPolicy.OVERWRITE,
@@ -189,7 +189,7 @@ class FileUploaderIT : AbstractIT() {
                 uploadsStorageManager,
                 connectivityServiceMock,
                 powerManagementServiceMock,
-                account,
+                user,
                 null,
                 ocUpload,
                 FileUploader.NameCollisionPolicy.DEFAULT,
@@ -216,7 +216,7 @@ class FileUploaderIT : AbstractIT() {
                 uploadsStorageManager,
                 connectivityServiceMock,
                 powerManagementServiceMock,
-                account,
+                user,
                 null,
                 ocUpload2,
                 FileUploader.NameCollisionPolicy.RENAME,
@@ -309,7 +309,7 @@ class FileUploaderIT : AbstractIT() {
                 uploadsStorageManager,
                 connectivityServiceMock,
                 powerManagementServiceMock,
-                account,
+                user,
                 null,
                 ocUpload,
                 FileUploader.NameCollisionPolicy.DEFAULT,
@@ -335,7 +335,7 @@ class FileUploaderIT : AbstractIT() {
                 uploadsStorageManager,
                 connectivityServiceMock,
                 powerManagementServiceMock,
-                account,
+                user,
                 null,
                 ocUpload2,
                 FileUploader.NameCollisionPolicy.CANCEL,

+ 5 - 5
src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentIT.kt

@@ -91,7 +91,7 @@ class OCFileListFragmentIT : AbstractIT() {
     @Test
     @ScreenshotTest
     fun showRichWorkspace() {
-        assertTrue(CreateFolderOperation("/test/", account, targetContext).execute(client, storageManager).isSuccess)
+        assertTrue(CreateFolderOperation("/test/", user, targetContext).execute(client, storageManager).isSuccess)
 
         val ocUpload = OCUpload(
             FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt",
@@ -102,7 +102,7 @@ class OCFileListFragmentIT : AbstractIT() {
             UploadsStorageManager(UserAccountManagerImpl.fromContext(targetContext), targetContext.contentResolver),
             connectivityServiceMock,
             powerManagementServiceMock,
-            account,
+            user,
             null,
             ocUpload,
             FileUploader.NameCollisionPolicy.DEFAULT,
@@ -180,7 +180,7 @@ class OCFileListFragmentIT : AbstractIT() {
     fun createAndShowShareToUser() {
         val path = "/shareToAdmin/"
         TestCase.assertTrue(
-            CreateFolderOperation(path, account, targetContext)
+            CreateFolderOperation(path, user, targetContext)
                 .execute(client, storageManager)
                 .isSuccess
         )
@@ -211,7 +211,7 @@ class OCFileListFragmentIT : AbstractIT() {
     fun createAndShowShareToGroup() {
         val path = "/shareToGroup/"
         TestCase.assertTrue(
-            CreateFolderOperation(path, account, targetContext)
+            CreateFolderOperation(path, user, targetContext)
                 .execute(client, storageManager)
                 .isSuccess
         )
@@ -275,7 +275,7 @@ class OCFileListFragmentIT : AbstractIT() {
     fun createAndShowShareViaLink() {
         val path = "/shareViaLink/"
         TestCase.assertTrue(
-            CreateFolderOperation(path, account, targetContext)
+            CreateFolderOperation(path, user, targetContext)
                 .execute(client, storageManager)
                 .isSuccess
         )

+ 19 - 13
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -53,6 +53,7 @@ import com.nextcloud.client.device.BatteryStatus;
 import com.nextcloud.client.device.PowerManagementService;
 import com.nextcloud.client.network.Connectivity;
 import com.nextcloud.client.network.ConnectivityService;
+import com.nextcloud.java.util.Optional;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AuthenticatorActivity;
@@ -292,10 +293,15 @@ public class FileUploader extends Service
             return Service.START_NOT_STICKY;
         }
 
-        Account account = intent.getParcelableExtra(KEY_ACCOUNT);
-        if (!accountManager.exists(account)) {
+        final Account account = intent.getParcelableExtra(KEY_ACCOUNT);
+        if (account == null) {
             return Service.START_NOT_STICKY;
         }
+        Optional<User> optionalUser = accountManager.getUser(account.name);
+        if (!optionalUser.isPresent()) {
+            return Service.START_NOT_STICKY;
+        }
+        final User user = optionalUser.get();
 
         boolean retry = intent.getBooleanExtra(KEY_RETRY, false);
         List<String> requestedUploads = new ArrayList<>();
@@ -309,7 +315,7 @@ public class FileUploader extends Service
                 return Service.START_NOT_STICKY;
             }
 
-            Integer error = gatherAndStartNewUploads(intent, account, requestedUploads, onWifiOnly, whileChargingOnly);
+            Integer error = gatherAndStartNewUploads(intent, user, requestedUploads, onWifiOnly, whileChargingOnly);
             if (error != null) {
                 return error;
             }
@@ -318,7 +324,7 @@ public class FileUploader extends Service
                 Log_OC.e(TAG, "Not enough information provided in intent: no KEY_RETRY_UPLOAD_KEY");
                 return START_NOT_STICKY;
             }
-            retryUploads(intent, account, requestedUploads);
+            retryUploads(intent, user, requestedUploads);
         }
 
         if (requestedUploads.size() > 0) {
@@ -339,7 +345,7 @@ public class FileUploader extends Service
     @Nullable
     private Integer gatherAndStartNewUploads(
         Intent intent,
-        Account account,
+        User user,
         List<String> requestedUploads,
         boolean onWifiOnly,
         boolean whileChargingOnly
@@ -402,7 +408,7 @@ public class FileUploader extends Service
         try {
             for (OCFile file : files) {
                 startNewUpload(
-                    account,
+                    user,
                     requestedUploads,
                     onWifiOnly,
                     whileChargingOnly,
@@ -430,7 +436,7 @@ public class FileUploader extends Service
      * Start a new {@link UploadFileOperation}.
      */
     private void startNewUpload(
-        Account account,
+        User user,
         List<String> requestedUploads,
         boolean onWifiOnly,
         boolean whileChargingOnly,
@@ -440,7 +446,7 @@ public class FileUploader extends Service
         int createdBy,
         OCFile file
     ) {
-        OCUpload ocUpload = new OCUpload(file, account);
+        OCUpload ocUpload = new OCUpload(file, user.toPlatformAccount());
         ocUpload.setFileSize(file.getFileLength());
         ocUpload.setNameCollisionPolicy(nameCollisionPolicy);
         ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
@@ -454,7 +460,7 @@ public class FileUploader extends Service
             mUploadsStorageManager,
             connectivityService,
             powerManagementService,
-            account,
+            user,
             file,
             ocUpload,
             nameCollisionPolicy,
@@ -473,7 +479,7 @@ public class FileUploader extends Service
         newUpload.addRenameUploadListener(this);
 
         Pair<String, String> putResult = mPendingUploads.putIfAbsent(
-            account.name,
+            user.getAccountName(),
             file.getRemotePath(),
             newUpload
         );
@@ -490,7 +496,7 @@ public class FileUploader extends Service
     /**
      * Retries a list of uploads.
      */
-    private void retryUploads(Intent intent, Account account, List<String> requestedUploads) {
+    private void retryUploads(Intent intent, User user, List<String> requestedUploads) {
         boolean onWifiOnly;
         boolean whileChargingOnly;
         OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
@@ -502,7 +508,7 @@ public class FileUploader extends Service
             mUploadsStorageManager,
             connectivityService,
             powerManagementService,
-            account,
+            user,
             null,
             upload,
             upload.getNameCollisionPolicy(),
@@ -518,7 +524,7 @@ public class FileUploader extends Service
         newUpload.addRenameUploadListener(this);
 
         Pair<String, String> putResult = mPendingUploads.putIfAbsent(
-            account.name,
+            user.getAccountName(),
             upload.getRemotePath(),
             newUpload
         );

+ 7 - 6
src/main/java/com/owncloud/android/operations/CreateFolderOperation.java

@@ -26,6 +26,7 @@ import android.content.Context;
 import android.os.Build;
 import android.util.Pair;
 
+import com.nextcloud.client.account.User;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.DecryptedFolderMetadata;
 import com.owncloud.android.datamodel.EncryptedFolderMetadata;
@@ -63,15 +64,15 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
 
     protected String remotePath;
     private RemoteFile createdRemoteFolder;
-    private Account account;
+    private User user;
     private Context context;
 
     /**
      * Constructor
      */
-    public CreateFolderOperation(String remotePath, Account account, Context context) {
+    public CreateFolderOperation(String remotePath, User user, Context context) {
         this.remotePath = remotePath;
-        this.account = account;
+        this.user = user;
         this.context = context;
     }
 
@@ -112,8 +113,8 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
     @RequiresApi(api = Build.VERSION_CODES.KITKAT)
     private RemoteOperationResult encryptedCreate(OCFile parent, String remoteParentPath, OwnCloudClient client) {
         ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
-        String privateKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PRIVATE_KEY);
-        String publicKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PUBLIC_KEY);
+        String privateKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PRIVATE_KEY);
+        String publicKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PUBLIC_KEY);
 
         String token = null;
         Boolean metadataExists;
@@ -236,7 +237,7 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
             if (encryptedRemotePath != null) {
                 RemoteOperationResult removeResult = new RemoveRemoteEncryptedFileOperation(encryptedRemotePath,
                                                                                             parent.getLocalId(),
-                                                                                            account,
+                                                                                            user.toPlatformAccount(),
                                                                                             context,
                                                                                             filename).execute(client);
 

+ 13 - 15
src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -30,6 +30,7 @@ import android.text.TextUtils;
 import android.util.Log;
 import android.util.Pair;
 
+import com.nextcloud.client.account.User;
 import com.nextcloud.client.device.BatteryStatus;
 import com.nextcloud.client.device.PowerManagementService;
 import com.nextcloud.client.network.Connectivity;
@@ -139,7 +140,7 @@ public class UploadFileOperation extends SyncOperation {
 
     private RequestEntity mEntity;
 
-    private final Account mAccount;
+    private final User user;
     private final OCUpload mUpload;
     private final UploadsStorageManager uploadsStorageManager;
     private final ConnectivityService connectivityService;
@@ -176,7 +177,7 @@ public class UploadFileOperation extends SyncOperation {
     public UploadFileOperation(UploadsStorageManager uploadsStorageManager,
                                ConnectivityService connectivityService,
                                PowerManagementService powerManagementService,
-                               Account account,
+                               User user,
                                OCFile file,
                                OCUpload upload,
                                FileUploader.NameCollisionPolicy nameCollisionPolicy,
@@ -185,9 +186,6 @@ public class UploadFileOperation extends SyncOperation {
                                boolean onWifiOnly,
                                boolean whileChargingOnly
     ) {
-        if (account == null) {
-            throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation " + "creation");
-        }
         if (upload == null) {
             throw new IllegalArgumentException("Illegal NULL file in UploadFileOperation creation");
         }
@@ -200,7 +198,7 @@ public class UploadFileOperation extends SyncOperation {
         this.uploadsStorageManager = uploadsStorageManager;
         this.connectivityService = connectivityService;
         this.powerManagementService = powerManagementService;
-        mAccount = account;
+        this.user = user;
         mUpload = upload;
         if (file == null) {
             mFile = obtainNewOCFileToUpload(
@@ -237,7 +235,7 @@ public class UploadFileOperation extends SyncOperation {
     public boolean isIgnoringPowerSaveMode() { return mIgnoringPowerSaveMode; }
 
     public Account getAccount() {
-        return mAccount;
+        return user.toPlatformAccount();
     }
 
     public String getFileName() {
@@ -477,7 +475,7 @@ public class UploadFileOperation extends SyncOperation {
                 return collisionResult;
             }
 
-            String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
+            String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
             expectedFile = new File(expectedPath);
 
             result = copyFile(originalFile, expectedPath);
@@ -520,7 +518,7 @@ public class UploadFileOperation extends SyncOperation {
             } catch (FileNotFoundException e) {
                 // this basically means that the file is on SD card
                 // try to copy file to temporary dir if it doesn't exist
-                String temporalPath = FileStorageUtils.getInternalTemporalPath(mAccount.name, mContext) +
+                String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) +
                     mFile.getRemotePath();
                 mFile.setStoragePath(temporalPath);
                 temporalFile = new File(temporalPath);
@@ -729,7 +727,7 @@ public class UploadFileOperation extends SyncOperation {
                 return collisionResult;
             }
 
-            String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
+            String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
             expectedFile = new File(expectedPath);
 
             result = copyFile(originalFile, expectedPath);
@@ -748,7 +746,7 @@ public class UploadFileOperation extends SyncOperation {
             } catch (FileNotFoundException e) {
                 // this basically means that the file is on SD card
                 // try to copy file to temporary dir if it doesn't exist
-                String temporalPath = FileStorageUtils.getInternalTemporalPath(mAccount.name, mContext) +
+                String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) +
                     mFile.getRemotePath();
                 mFile.setStoragePath(temporalPath);
                 temporalFile = new File(temporalPath);
@@ -876,7 +874,7 @@ public class UploadFileOperation extends SyncOperation {
     private RemoteOperationResult copyFile(File originalFile, String expectedPath) throws OperationCancelledException,
             IOException {
         if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY && !mOriginalStoragePath.equals(expectedPath)) {
-            String temporalPath = FileStorageUtils.getInternalTemporalPath(mAccount.name, mContext) +
+            String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) +
                 mFile.getRemotePath();
             mFile.setStoragePath(temporalPath);
             File temporalFile = new File(temporalPath);
@@ -958,7 +956,7 @@ public class UploadFileOperation extends SyncOperation {
                 break;
 
             case FileUploader.LOCAL_BEHAVIOUR_MOVE:
-                String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
+                String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
                 File newFile = new File(expectedPath);
 
                 try {
@@ -991,7 +989,7 @@ public class UploadFileOperation extends SyncOperation {
         RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, false);
         RemoteOperationResult result = operation.execute(client);
         if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
-            SyncOperation syncOp = new CreateFolderOperation(pathToGrant, getAccount(), getContext());
+            SyncOperation syncOp = new CreateFolderOperation(pathToGrant, user, getContext());
             result = syncOp.execute(client, getStorageManager());
         }
         if (result.isSuccess()) {
@@ -1310,7 +1308,7 @@ public class UploadFileOperation extends SyncOperation {
 
         // generate new Thumbnail
         final ThumbnailsCacheManager.ThumbnailGenerationTask task =
-                new ThumbnailsCacheManager.ThumbnailGenerationTask(getStorageManager(), mAccount);
+                new ThumbnailsCacheManager.ThumbnailGenerationTask(getStorageManager(), user.toPlatformAccount());
         task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getRemoteId()));
     }
 

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

@@ -473,7 +473,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         FileDataStorageManager storageManager = targetFolder.getStorageManager();
 
         RemoteOperationResult result = new CreateFolderOperation(newDirPath,
-                                                                 accountManager.getCurrentAccount(),
+                                                                 accountManager.getUser(),
                                                                  getContext())
             .execute(targetFolder.getClient(), storageManager);
 

+ 4 - 3
src/main/java/com/owncloud/android/services/OperationsService.java

@@ -516,6 +516,7 @@ public class OperationsService extends Service {
 
             } else {
                 Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
+                User user = toUser(account);
                 String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
                 target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl));
 
@@ -640,13 +641,13 @@ public class OperationsService extends Service {
 
                     case ACTION_CREATE_FOLDER:
                         remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
-                        operation = new CreateFolderOperation(remotePath, account, getApplicationContext());
+                        operation = new CreateFolderOperation(remotePath, user, getApplicationContext());
                         break;
 
                     case ACTION_SYNC_FILE:
                         remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                         boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true);
-                        operation = new SynchronizeFileOperation(remotePath, toUser(account), syncFileContents,
+                        operation = new SynchronizeFileOperation(remotePath, user, syncFileContents,
                                 getApplicationContext());
                         break;
 
@@ -655,7 +656,7 @@ public class OperationsService extends Service {
                         operation = new SynchronizeFolderOperation(
                                 this,                       // TODO remove this dependency from construction time
                                 remotePath,
-                                toUser(account),
+                                user,
                                 System.currentTimeMillis()  // TODO remove this dependency from construction time
                         );
                         break;