瀏覽代碼

Different retry method

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 年之前
父節點
當前提交
1ca744b28f

+ 28 - 0
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -357,6 +357,34 @@ 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 void retryFailedUploads(Context context, Account account, UploadResult uploadResult) {
+            UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(context.getContentResolver(), context);
+            OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
+            Account currentAccount = null;
+            boolean resultMatch;
+            boolean accountMatch;
+            for ( OCUpload failedUpload: failedUploads) {
+                accountMatch = (account == null || account.name.equals(failedUpload.getAccountName()));
+                resultMatch = (uploadResult == null || uploadResult.equals(failedUpload.getLastResult()));
+                if (accountMatch && resultMatch) {
+                    if (currentAccount == null ||
+                            !currentAccount.name.equals(failedUpload.getAccountName())) {
+                        currentAccount = failedUpload.getAccount(context);
+                    }
+                    retry(context, currentAccount, failedUpload);
+                }
+            }
+        }
+
         /**
          * Private implementation of retry.
          *

+ 8 - 42
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -29,8 +29,6 @@ import android.provider.MediaStore;
 import android.text.TextUtils;
 import android.util.Log;
 
-import com.evernote.android.job.JobRequest;
-import com.evernote.android.job.util.Device;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
@@ -40,9 +38,7 @@ import com.owncloud.android.datamodel.SyncedFolder;
 import com.owncloud.android.datamodel.SyncedFolderProvider;
 import com.owncloud.android.datamodel.UploadsStorageManager;
 import com.owncloud.android.db.OCUpload;
-import com.owncloud.android.db.UploadResult;
 import com.owncloud.android.files.services.FileUploader;
-import com.owncloud.android.operations.UploadFileOperation;
 
 import org.lukhnos.nnio.file.FileVisitResult;
 import org.lukhnos.nnio.file.Files;
@@ -187,7 +183,6 @@ public class FilesSyncHelper {
 
     public static void restartJobsIfNeeded() {
         final Context context = MainApp.getAppContext();
-        boolean restartedInCurrentIteration;
 
         FileUploader.UploadRequester uploadRequester = new FileUploader.UploadRequester();
 
@@ -201,7 +196,6 @@ public class FilesSyncHelper {
         for (OCUpload failedUpload : failedUploads) {
             accountExists = false;
             fileExists = new File(failedUpload.getLocalPath()).exists();
-            restartedInCurrentIteration = false;
 
             // check if accounts still exists
             for (Account account : AccountUtils.getAccounts(context)) {
@@ -211,44 +205,16 @@ public class FilesSyncHelper {
                 }
             }
 
-            if (!failedUpload.getLastResult().equals(UploadResult.UPLOADED)) {
-                if (failedUpload.getCreadtedBy() == UploadFileOperation.CREATED_AS_INSTANT_PICTURE) {
-                    if (accountExists && fileExists) {
-                        // Handle case of charging
-
-                        if (failedUpload.isWhileChargingOnly() && Device.isCharging(context)) {
-                            if (failedUpload.isUseWifiOnly() &&
-                                    Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
-                                uploadRequester.retry(context, failedUpload);
-                                restartedInCurrentIteration = true;
-                            } else if (!failedUpload.isUseWifiOnly() &&
-                                    !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
-                                uploadRequester.retry(context, failedUpload);
-                                restartedInCurrentIteration = true;
-                            }
-                        }
-
-                        // Handle case of wifi
-
-                        if (!restartedInCurrentIteration) {
-                            if (failedUpload.isUseWifiOnly() &&
-                                    Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
-                                uploadRequester.retry(context, failedUpload);
-                            } else if (!failedUpload.isUseWifiOnly() &&
-                                    !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
-                                uploadRequester.retry(context, failedUpload);
-                            }
-                        }
-                    }
-                } else {
-                    if (accountExists && fileExists) {
-                        uploadRequester.retry(context, failedUpload);
-                    } else {
-                        uploadsStorageManager.removeUpload(failedUpload);
-                    }
-                }
+            if (!accountExists || !fileExists) {
+                uploadsStorageManager.removeUpload(failedUpload);
             }
         }
+
+        uploadRequester.retryFailedUploads(
+                context,
+                null,
+                null
+        );
     }
 }