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

Merge pull request #9333 from nextcloud/migrate-ocupload-to-user

Migate OCUpload to User
Álvaro Brey 3 жил өмнө
parent
commit
de79c93991

+ 5 - 11
src/main/java/com/owncloud/android/db/OCUpload.java

@@ -27,6 +27,7 @@ import android.accounts.Account;
 import android.os.Parcel;
 import android.os.Parcelable;
 
+import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.UploadsStorageManager;
@@ -135,7 +136,7 @@ public class OCUpload implements Parcelable {
      *
      * @param localPath         Absolute path in the local file system to the file to be uploaded.
      * @param remotePath        Absolute path in the remote account to set to the uploaded file.
-     * @param accountName       Name of an ownCloud account to update the file to.
+     * @param accountName       Name of an file owner account.
      */
     public OCUpload(String localPath, String remotePath, String accountName) {
         if (localPath == null || !localPath.startsWith(File.separator)) {
@@ -157,10 +158,10 @@ public class OCUpload implements Parcelable {
      * Convenience constructor to re-upload already existing {@link OCFile}s.
      *
      * @param  ocFile           {@link OCFile} instance to update in the remote server.
-     * @param  account          ownCloud {@link Account} where ocFile is contained.
+     * @param  user             file owner
      */
-    public OCUpload(OCFile ocFile, Account account) {
-        this(ocFile.getStoragePath(), ocFile.getRemotePath(), account.name);
+    public OCUpload(OCFile ocFile, User user) {
+        this(ocFile.getStoragePath(), ocFile.getRemotePath(), user.getAccountName());
     }
 
     /**
@@ -230,13 +231,6 @@ public class OCUpload implements Parcelable {
         return MimeTypeUtil.getBestMimeTypeByFilename(localPath);
     }
 
-    /**
-     * Returns owncloud account as {@link Account} object.
-     */
-    public Account getAccount(UserAccountManager accountManager) {
-        return accountManager.getAccountByName(getAccountName());
-    }
-
     /**
      * For debugging purposes only.
      */

+ 20 - 39
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -468,7 +468,7 @@ public class FileUploader extends Service
             return;
         }
 
-        OCUpload ocUpload = new OCUpload(file, user.toPlatformAccount());
+        OCUpload ocUpload = new OCUpload(file, user);
         ocUpload.setFileSize(file.getFileLength());
         ocUpload.setNameCollisionPolicy(nameCollisionPolicy);
         ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
@@ -1055,10 +1055,10 @@ public class FileUploader extends Service
     /**
      * Retry a failed {@link OCUpload} identified by {@link OCUpload#getRemotePath()}
      */
-    public static void retryUpload(@NonNull Context context, @NonNull Account account, @NonNull OCUpload upload) {
+    public static void retryUpload(@NonNull Context context, @NonNull User user, @NonNull OCUpload upload) {
         Intent i = new Intent(context, FileUploader.class);
         i.putExtra(FileUploader.KEY_RETRY, true);
-        i.putExtra(FileUploader.KEY_ACCOUNT, account);
+        i.putExtra(FileUploader.KEY_ACCOUNT, user.toPlatformAccount());
         i.putExtra(FileUploader.KEY_RETRY_UPLOAD, upload);
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -1070,33 +1070,19 @@ public class FileUploader extends Service
 
     /**
      * Retry a subset of all the stored failed uploads.
-     *
-     * @param context      Caller {@link Context}
-     * @param account      If not null, only failed uploads to this OC account will be retried; otherwise, uploads of
-     *                     all accounts will be retried.
-     * @param uploadResult If not null, only failed uploads with the result specified will be retried; otherwise, failed
-     *                     uploads due to any result will be retried.
      */
     public static void retryFailedUploads(
         @NonNull final Context context,
-        @Nullable final Account account,
         @NonNull final UploadsStorageManager uploadsStorageManager,
         @NonNull final ConnectivityService connectivityService,
         @NonNull final UserAccountManager accountManager,
-        @NonNull final PowerManagementService powerManagementService,
-        @Nullable final UploadResult uploadResult
+        @NonNull final PowerManagementService powerManagementService
     ) {
         OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
-        if(failedUploads.length == 0)
-        {
-            //nothing to do
-            return;
+        if(failedUploads == null || failedUploads.length == 0) {
+            return; //nothing to do
         }
 
-        Account currentAccount = null;
-        boolean resultMatch;
-        boolean accountMatch;
-
         final Connectivity connectivity = connectivityService.getConnectivity();
         final boolean gotNetwork = connectivity.isConnected() && !connectivityService.isInternetWalled();
         final boolean gotWifi = connectivity.isWifi();
@@ -1104,27 +1090,22 @@ public class FileUploader extends Service
         final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
         final boolean isPowerSaving = powerManagementService.isPowerSavingEnabled();
 
+        Optional<User> uploadUser = Optional.empty();
         for (OCUpload failedUpload : failedUploads) {
-            accountMatch = account == null || account.name.equals(failedUpload.getAccountName());
-            resultMatch = uploadResult == null || uploadResult == failedUpload.getLastResult();
-            if (accountMatch && resultMatch) {
-                // 1. extract failed upload owner account in efficient name (expensive query)
-                if (currentAccount == null || !currentAccount.name.equals(failedUpload.getAccountName())) {
-                    currentAccount = failedUpload.getAccount(accountManager);
-                }
-
-                if (!new File(failedUpload.getLocalPath()).exists()) {
-                    // 2A. for deleted files, mark as permanently failed
-                    if (failedUpload.getLastResult() != UploadResult.FILE_NOT_FOUND) {
-                        failedUpload.setLastResult(UploadResult.FILE_NOT_FOUND);
-                        uploadsStorageManager.updateUpload(failedUpload);
-                    }
-                } else {
-                    // 2B. for existing local files, try restarting it if possible
-                    if (!isPowerSaving && gotNetwork && canUploadBeRetried(failedUpload, gotWifi, charging)) {
-                        retryUpload(context, currentAccount, failedUpload);
-                    }
+            // 1. extract failed upload owner account and cache it between loops (expensive query)
+            if (!uploadUser.isPresent() || !uploadUser.get().nameEquals(failedUpload.getAccountName())) {
+                uploadUser = accountManager.getUser(failedUpload.getAccountName());
+            }
+            final boolean isDeleted = !new File(failedUpload.getLocalPath()).exists();
+            if (isDeleted) {
+                // 2A. for deleted files, mark as permanently failed
+                if (failedUpload.getLastResult() != UploadResult.FILE_NOT_FOUND) {
+                    failedUpload.setLastResult(UploadResult.FILE_NOT_FOUND);
+                    uploadsStorageManager.updateUpload(failedUpload);
                 }
+            } else if (!isPowerSaving && gotNetwork && canUploadBeRetried(failedUpload, gotWifi, charging)) {
+                // 2B. for existing local files, try restarting it if possible
+                retryUpload(context, uploadUser.get(), failedUpload);
             }
         }
     }

+ 1 - 3
src/main/java/com/owncloud/android/ui/activity/UploadListActivity.java

@@ -187,12 +187,10 @@ public class UploadListActivity extends FileActivity {
         // retry failed uploads
         new Thread(() -> FileUploader.retryFailedUploads(
             this,
-            null,
             uploadsStorageManager,
             connectivityService,
             userAccountManager,
-            powerManagementService,
-            null
+            powerManagementService
         )).start();
 
         // update UI

+ 17 - 13
src/main/java/com/owncloud/android/ui/adapter/UploadListAdapter.java

@@ -140,12 +140,10 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
                 case FAILED:
                     new Thread(() -> FileUploader.retryFailedUploads(
                         parentActivity,
-                        null,
                         uploadsStorageManager,
                         connectivityService,
                         accountManager,
-                        powerManagementService,
-                        null
+                        powerManagementService
                     )).start();
                     break;
 
@@ -358,7 +356,9 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
             final UploadResult uploadResult = item.getLastResult();
             itemViewHolder.binding.uploadListItemLayout.setOnClickListener(v -> {
                 if (uploadResult == UploadResult.CREDENTIAL_ERROR) {
-                    parentActivity.getFileOperationsHelper().checkCurrentCredentials(item.getAccount(accountManager));
+                    final Optional<User> user = accountManager.getUser(item.getAccountName());
+                    final Account account = user.orElseThrow(RuntimeException::new).toPlatformAccount();
+                    parentActivity.getFileOperationsHelper().checkCurrentCredentials(account);
                     return;
                 } else if (uploadResult == UploadResult.SYNC_CONFLICT && optionalUser.isPresent()) {
                     User user = optionalUser.get();
@@ -369,8 +369,9 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
 
                 // not a credentials error
                 File file = new File(item.getLocalPath());
-                if (file.exists()) {
-                    FileUploader.retryUpload(parentActivity, item.getAccount(accountManager), item);
+                Optional<User> user = accountManager.getUser(item.getAccountName());
+                if (file.exists() && user.isPresent()) {
+                    FileUploader.retryUpload(parentActivity, user.get(), item);
                     loadUploadItemsFromDb();
                 } else {
                     DisplayUtils.showSnackMessage(
@@ -574,13 +575,16 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         file.setStoragePath(upload.getLocalPath());
 
         Context context = MainApp.getAppContext();
-        Intent intent = ConflictsResolveActivity.createIntent(file,
-                                                              upload.getAccount(accountManager),
-                                                              upload.getUploadId(),
-                                                              Intent.FLAG_ACTIVITY_NEW_TASK,
-                                                              context);
-
-        context.startActivity(intent);
+        Optional<User> user = accountManager.getUser(upload.getAccountName());
+        if (user.isPresent()) {
+            Intent intent = ConflictsResolveActivity.createIntent(file,
+                                                                  user.get().toPlatformAccount(),
+                                                                  upload.getUploadId(),
+                                                                  Intent.FLAG_ACTIVITY_NEW_TASK,
+                                                                  context);
+
+            context.startActivity(intent);
+        }
     }
 
     /**

+ 1 - 3
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -239,12 +239,10 @@ public final class FilesSyncHelper {
             if (connectivityService.getConnectivity().isConnected() && !connectivityService.isInternetWalled()) {
                 FileUploader.retryFailedUploads(
                     context,
-                    null,
                     uploadsStorageManager,
                     connectivityService,
                     accountManager,
-                    powerManagementService,
-                    null
+                    powerManagementService
                 );
             }
         }).start();