Browse Source

Pause jobs if magic is changed

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 years ago
parent
commit
95d75c5bb0

+ 24 - 10
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -79,6 +79,8 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Vector;
 
+import javax.annotation.Nullable;
+
 /**
  * Service for uploading files. Invoke using context.startService(...).
  *
@@ -710,7 +712,7 @@ public class FileUploader extends Service
          * @param file    A file in the queue of pending uploads
          */
         public void cancel(Account account, OCFile file) {
-            cancel(account.name, file.getRemotePath());
+            cancel(account.name, file.getRemotePath(), null);
         }
 
         /**
@@ -719,7 +721,7 @@ public class FileUploader extends Service
          * @param storedUpload Upload operation persisted
          */
         public void cancel(OCUpload storedUpload) {
-            cancel(storedUpload.getAccountName(), storedUpload.getRemotePath());
+            cancel(storedUpload.getAccountName(), storedUpload.getRemotePath(), null);
 
         }
 
@@ -728,8 +730,10 @@ public class FileUploader extends Service
          *
          * @param accountName Local name of an ownCloud account where the remote file will be stored.
          * @param remotePath  Remote target of the upload
+         *
+         * Setting result code will pause rather than cancel the job
          */
-        private void cancel(String accountName, String remotePath) {
+        private void cancel(String accountName, String remotePath, @Nullable ResultCode resultCode ) {
             Pair<UploadFileOperation, String> removeResult =
                     mPendingUploads.remove(accountName, remotePath);
             UploadFileOperation upload = removeResult.first;
@@ -740,12 +744,17 @@ public class FileUploader extends Service
 
                 upload = mCurrentUpload;
             }
+
             if (upload != null) {
                 upload.cancel();
                 // need to update now table in mUploadsStorageManager,
                 // since the operation will not get to be run by FileUploader#uploadFile
-                mUploadsStorageManager.updateDatabaseUploadResult(Re, upload);
-                mUploadsStorageManager.removeUpload(accountName, remotePath);
+                if (resultCode != null) {
+                    mUploadsStorageManager.updateDatabaseUploadResult(new RemoteOperationResult(resultCode), upload);
+                    notifyUploadResult(upload, new RemoteOperationResult(resultCode));
+                } else {
+                    mUploadsStorageManager.removeUpload(accountName, remotePath);
+                }
             } else {
                 // try to cancel job in jobScheduler
                 mUploadsStorageManager.cancelPendingJob(accountName, remotePath);
@@ -896,11 +905,16 @@ public class FileUploader extends Service
                 boundListener.onTransferProgress(progressRate, totalTransferredSoFar,
                         totalToTransfer, fileName);
 
-                if (MainApp.getAppContext() != null &&
-                        (mCurrentUpload.getIsWifiRequired() && !Device.getNetworkType(MainApp.getAppContext()).
-                        equals(JobRequest.NetworkType.UNMETERED)) || (mCurrentUpload.getIsChargingRequired() &&
-                        !Device.isCharging(MainApp.getAppContext()))) {
-                    // pause
+                if (MainApp.getAppContext() != null) {
+                    if (mCurrentUpload.getIsWifiRequired() && !Device.getNetworkType(MainApp.getAppContext()).
+                            equals(JobRequest.NetworkType.UNMETERED)) {
+                        cancel(mCurrentUpload.getAccount().name, mCurrentUpload.getFile().getRemotePath()
+                                , ResultCode.DELAYED_FOR_WIFI);
+                    } else if (mCurrentUpload.getIsChargingRequired() &&
+                            !Device.isCharging(MainApp.getAppContext())) {
+                        cancel(mCurrentUpload.getAccount().name, mCurrentUpload.getFile().getRemotePath()
+                                , ResultCode.DELAYED_FOR_CHARGING);
+                    }
                 }
             }
         }

+ 50 - 66
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -191,8 +191,6 @@ public class FilesSyncHelper {
         final Context context = MainApp.getAppContext();
         boolean restartedInCurrentIteration;
 
-        int countRestartedJobs = 0;
-
         FileUploader.UploadRequester uploadRequester = new FileUploader.UploadRequester();
 
         boolean accountExists;
@@ -213,34 +211,28 @@ public class FilesSyncHelper {
             }
 
             if (accountExists && fileExists) {
-                if (countRestartedJobs < 5) {
-                    // Handle case of charging
-                    if (jobRequest.requiresCharging() && Device.isCharging(context)) {
-                        if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
-                                !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
-                            jobRequest.cancelAndEdit().build().schedule();
-                            countRestartedJobs++;
-                            restartedInCurrentIteration = true;
-                        } else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
-                                Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
-                            jobRequest.cancelAndEdit().build().schedule();
-                            countRestartedJobs++;
-                            restartedInCurrentIteration = true;
-                        }
+                // Handle case of charging
+                if (jobRequest.requiresCharging() && Device.isCharging(context)) {
+                    if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
+                            !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+                        jobRequest.cancelAndEdit().build().schedule();
+                        restartedInCurrentIteration = true;
+                    } else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
+                            Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
+                        jobRequest.cancelAndEdit().build().schedule();
+                        restartedInCurrentIteration = true;
                     }
+                }
 
-                    // Handle case of wifi
-
-                    if (!restartedInCurrentIteration) {
-                        if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
-                                !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
-                            jobRequest.cancelAndEdit().build().schedule();
-                            countRestartedJobs++;
-                        } else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
-                                Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
-                            jobRequest.cancelAndEdit().build().schedule();
-                            countRestartedJobs++;
-                        }
+                // Handle case of wifi
+
+                if (!restartedInCurrentIteration) {
+                    if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
+                            !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+                        jobRequest.cancelAndEdit().build().schedule();
+                    } else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
+                            Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
+                        jobRequest.cancelAndEdit().build().schedule();
                     }
                 }
             } else {
@@ -251,7 +243,7 @@ public class FilesSyncHelper {
         UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(context.getContentResolver(), context);
         OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
 
-        for (OCUpload failedUpload: failedUploads) {
+        for (OCUpload failedUpload : failedUploads) {
             accountExists = false;
             fileExists = new File(failedUpload.getLocalPath()).exists();
             restartedInCurrentIteration = false;
@@ -267,51 +259,43 @@ public class FilesSyncHelper {
             if (!failedUpload.getLastResult().equals(UploadResult.UPLOADED)) {
                 if (failedUpload.getCreadtedBy() == UploadFileOperation.CREATED_AS_INSTANT_PICTURE) {
                     if (accountExists && fileExists) {
-                        if (countRestartedJobs < 5) {
-                            // 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;
-                                    countRestartedJobs++;
-                                } else if (!failedUpload.isUseWifiOnly() &&
-                                        !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
-                                    uploadRequester.retry(context, failedUpload);
-                                    restartedInCurrentIteration = true;
-                                    countRestartedJobs++;
-                                }
+                        // 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
 
-                            // Handle case of wifi
-
-                            if (!restartedInCurrentIteration) {
-                                if (failedUpload.isUseWifiOnly() &&
-                                        Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
-                                    uploadRequester.retry(context, failedUpload);
-                                    countRestartedJobs++;
-                                } else if (!failedUpload.isUseWifiOnly() &&
-                                        !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
-                                    uploadRequester.retry(context, failedUpload);
-                                    countRestartedJobs++;
-                                }
+                        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 {
-                        uploadsStorageManager.removeUpload(failedUpload);
                     }
                 } else {
-                    if (accountExists && fileExists) {
-                        if (countRestartedJobs < 5) {
-                            uploadRequester.retry(context, failedUpload);
-                            countRestartedJobs++;
-                        }
-                    } else {
-                        uploadsStorageManager.removeUpload(failedUpload);
-                    }
+                    uploadsStorageManager.removeUpload(failedUpload);
+                }
+            } else {
+                if (accountExists && fileExists) {
+                    uploadRequester.retry(context, failedUpload);
+                } else {
+                    uploadsStorageManager.removeUpload(failedUpload);
                 }
             }
         }
     }
 }
+}