Selaa lähdekoodia

Implement retry of manual uploads

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 vuotta sitten
vanhempi
commit
7ac07ab08a

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

@@ -300,34 +300,6 @@ 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.
          *

+ 1 - 1
src/main/java/com/owncloud/android/jobs/AutoUploadJob.java

@@ -97,7 +97,7 @@ public class AutoUploadJob extends Job {
                         uploadBehaviour,
                         mimeType,
                         true,           // create parent folder if not existent
-                        UploadFileOperation.CREATED_BY_USER,
+                        UploadFileOperation.CREATED_AS_INSTANT_PICTURE,
                         requiresWifi,
                         requiresCharging
                 );

+ 1 - 1
src/main/java/com/owncloud/android/jobs/FilesSyncJob.java

@@ -130,7 +130,7 @@ public class FilesSyncJob extends Job {
                             bundle.putBoolean(AutoUploadJob.REQUIRES_CHARGING, needsCharging);
 
                             new JobRequest.Builder(AutoUploadJob.TAG)
-                                    .setExecutionWindow(30_000L, 80_000L)
+                                    .setExecutionWindow(10_000L, 10_000L)
                                     .setRequiresCharging(needsCharging)
                                     .setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobRequest.NetworkType.UNMETERED :
                                             JobRequest.NetworkType.CONNECTED)

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

@@ -22,7 +22,6 @@
 package com.owncloud.android.ui.activity;
 
 import android.accounts.Account;
-import android.accounts.AccountManager;
 import android.content.ActivityNotFoundException;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
@@ -40,11 +39,9 @@ import android.view.View;
 import android.widget.Toast;
 
 import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
 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.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -54,6 +51,7 @@ import com.owncloud.android.operations.CheckCurrentCredentialsOperation;
 import com.owncloud.android.ui.fragment.UploadListFragment;
 import com.owncloud.android.utils.AnalyticsUtils;
 import com.owncloud.android.utils.DisplayUtils;
+import com.owncloud.android.utils.FilesSyncHelper;
 import com.owncloud.android.utils.MimeTypeUtil;
 
 import java.io.File;
@@ -221,17 +219,7 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == FileActivity.REQUEST_CODE__UPDATE_CREDENTIALS && resultCode == RESULT_OK) {
-            // Retry uploads of the updated account
-            Account account = AccountUtils.getOwnCloudAccountByName(
-                this,
-                data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)
-            );
-            FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
-            requester.retryFailedUploads(
-                this,
-                account,
-                UploadResult.CREDENTIAL_ERROR
-            );
+            FilesSyncHelper.restartJobsIfNeeded();
         }
     }
 
@@ -252,8 +240,7 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
 
             } else {
                 // already updated -> just retry!
-                FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
-                requester.retryFailedUploads(this, account, UploadResult.CREDENTIAL_ERROR);
+                FilesSyncHelper.restartJobsIfNeeded();
             }
 
         } else {

+ 40 - 25
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -43,7 +43,9 @@ 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.jobs.AutoUploadJob;
+import com.owncloud.android.operations.UploadFileOperation;
 
 import org.lukhnos.nnio.file.FileVisitResult;
 import org.lukhnos.nnio.file.Files;
@@ -190,6 +192,8 @@ public class FilesSyncHelper {
         final Context context = MainApp.getAppContext();
         boolean restartedInCurrentIteration = false;
 
+        FileUploader.UploadRequester uploadRequester = new FileUploader.UploadRequester();
+
         for (JobRequest jobRequest : JobManager.instance().getAllJobRequestsForTag(AutoUploadJob.TAG)) {
             restartedInCurrentIteration = false;
             // Handle case of charging
@@ -222,36 +226,47 @@ public class FilesSyncHelper {
         OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
 
         boolean accountExists;
+        boolean fileExists;
         for (OCUpload failedUpload: failedUploads) {
             accountExists = false;
-            if (!failedUpload.getLastResult().equals(UploadResult.UPLOADED)) {
-                uploadsStorageManager.removeUpload(failedUpload);
+            fileExists = new File(failedUpload.getLocalPath()).exists();
 
-                // check if accounts still exists
-                for (Account account : AccountUtils.getAccounts(context)) {
-                    if (account.name.equals(failedUpload.getAccountName())) {
-                        accountExists = true;
-                        break;
-                    }
+            // check if accounts still exists
+            for (Account account : AccountUtils.getAccounts(context)) {
+                if (account.name.equals(failedUpload.getAccountName())) {
+                    accountExists = true;
+                    break;
                 }
+            }
 
-                if (accountExists) {
-                    PersistableBundleCompat bundle = new PersistableBundleCompat();
-                    bundle.putString(AutoUploadJob.LOCAL_PATH, failedUpload.getLocalPath());
-                    bundle.putString(AutoUploadJob.REMOTE_PATH, failedUpload.getRemotePath());
-                    bundle.putString(AutoUploadJob.ACCOUNT, failedUpload.getAccountName());
-                    bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, failedUpload.getLocalAction());
-
-                    new JobRequest.Builder(AutoUploadJob.TAG)
-                            .setExecutionWindow(30_000L, 80_000L)
-                            .setRequiresCharging(failedUpload.isWhileChargingOnly())
-                            .setRequiredNetworkType(failedUpload.isUseWifiOnly() ? JobRequest.NetworkType.UNMETERED :
-                                    JobRequest.NetworkType.CONNECTED)
-                            .setExtras(bundle)
-                            .setRequirementsEnforced(true)
-                            .setUpdateCurrent(false)
-                            .build()
-                            .schedule();
+            if (!failedUpload.getLastResult().equals(UploadResult.UPLOADED)) {
+                if (failedUpload.getCreadtedBy() == UploadFileOperation.CREATED_AS_INSTANT_PICTURE) {
+                    uploadsStorageManager.removeUpload(failedUpload);
+
+                    if (accountExists && fileExists) {
+                        PersistableBundleCompat bundle = new PersistableBundleCompat();
+                        bundle.putString(AutoUploadJob.LOCAL_PATH, failedUpload.getLocalPath());
+                        bundle.putString(AutoUploadJob.REMOTE_PATH, failedUpload.getRemotePath());
+                        bundle.putString(AutoUploadJob.ACCOUNT, failedUpload.getAccountName());
+                        bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, failedUpload.getLocalAction());
+
+                        new JobRequest.Builder(AutoUploadJob.TAG)
+                                .setExecutionWindow(10_000L, 10_000L)
+                                .setRequiresCharging(failedUpload.isWhileChargingOnly())
+                                .setRequiredNetworkType(failedUpload.isUseWifiOnly() ? JobRequest.NetworkType.UNMETERED :
+                                        JobRequest.NetworkType.CONNECTED)
+                                .setExtras(bundle)
+                                .setRequirementsEnforced(true)
+                                .setUpdateCurrent(false)
+                                .build()
+                                .schedule();
+                    }
+                }
+            } else {
+                if (accountExists && fileExists) {
+                    uploadRequester.retry(context, failedUpload);
+                } else {
+                    uploadsStorageManager.removeUpload(failedUpload);
                 }
             }
         }