Implement retry of manual uploads

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-07-07 20:40:14 +02:00 committed by AndyScherzinger
parent 5227513daa
commit 7ac07ab08a
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
5 changed files with 45 additions and 71 deletions

View file

@ -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. * Private implementation of retry.
* *

View file

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

View file

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

View file

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

View file

@ -43,7 +43,9 @@ import com.owncloud.android.datamodel.SyncedFolderProvider;
import com.owncloud.android.datamodel.UploadsStorageManager; import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.db.OCUpload; import com.owncloud.android.db.OCUpload;
import com.owncloud.android.db.UploadResult; import com.owncloud.android.db.UploadResult;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.jobs.AutoUploadJob; import com.owncloud.android.jobs.AutoUploadJob;
import com.owncloud.android.operations.UploadFileOperation;
import org.lukhnos.nnio.file.FileVisitResult; import org.lukhnos.nnio.file.FileVisitResult;
import org.lukhnos.nnio.file.Files; import org.lukhnos.nnio.file.Files;
@ -190,6 +192,8 @@ public class FilesSyncHelper {
final Context context = MainApp.getAppContext(); final Context context = MainApp.getAppContext();
boolean restartedInCurrentIteration = false; boolean restartedInCurrentIteration = false;
FileUploader.UploadRequester uploadRequester = new FileUploader.UploadRequester();
for (JobRequest jobRequest : JobManager.instance().getAllJobRequestsForTag(AutoUploadJob.TAG)) { for (JobRequest jobRequest : JobManager.instance().getAllJobRequestsForTag(AutoUploadJob.TAG)) {
restartedInCurrentIteration = false; restartedInCurrentIteration = false;
// Handle case of charging // Handle case of charging
@ -222,10 +226,10 @@ public class FilesSyncHelper {
OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads(); OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
boolean accountExists; boolean accountExists;
boolean fileExists;
for (OCUpload failedUpload: failedUploads) { for (OCUpload failedUpload: failedUploads) {
accountExists = false; accountExists = false;
if (!failedUpload.getLastResult().equals(UploadResult.UPLOADED)) { fileExists = new File(failedUpload.getLocalPath()).exists();
uploadsStorageManager.removeUpload(failedUpload);
// check if accounts still exists // check if accounts still exists
for (Account account : AccountUtils.getAccounts(context)) { for (Account account : AccountUtils.getAccounts(context)) {
@ -235,7 +239,11 @@ public class FilesSyncHelper {
} }
} }
if (accountExists) { if (!failedUpload.getLastResult().equals(UploadResult.UPLOADED)) {
if (failedUpload.getCreadtedBy() == UploadFileOperation.CREATED_AS_INSTANT_PICTURE) {
uploadsStorageManager.removeUpload(failedUpload);
if (accountExists && fileExists) {
PersistableBundleCompat bundle = new PersistableBundleCompat(); PersistableBundleCompat bundle = new PersistableBundleCompat();
bundle.putString(AutoUploadJob.LOCAL_PATH, failedUpload.getLocalPath()); bundle.putString(AutoUploadJob.LOCAL_PATH, failedUpload.getLocalPath());
bundle.putString(AutoUploadJob.REMOTE_PATH, failedUpload.getRemotePath()); bundle.putString(AutoUploadJob.REMOTE_PATH, failedUpload.getRemotePath());
@ -243,7 +251,7 @@ public class FilesSyncHelper {
bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, failedUpload.getLocalAction()); bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, failedUpload.getLocalAction());
new JobRequest.Builder(AutoUploadJob.TAG) new JobRequest.Builder(AutoUploadJob.TAG)
.setExecutionWindow(30_000L, 80_000L) .setExecutionWindow(10_000L, 10_000L)
.setRequiresCharging(failedUpload.isWhileChargingOnly()) .setRequiresCharging(failedUpload.isWhileChargingOnly())
.setRequiredNetworkType(failedUpload.isUseWifiOnly() ? JobRequest.NetworkType.UNMETERED : .setRequiredNetworkType(failedUpload.isUseWifiOnly() ? JobRequest.NetworkType.UNMETERED :
JobRequest.NetworkType.CONNECTED) JobRequest.NetworkType.CONNECTED)
@ -254,6 +262,13 @@ public class FilesSyncHelper {
.schedule(); .schedule();
} }
} }
} else {
if (accountExists && fileExists) {
uploadRequester.retry(context, failedUpload);
} else {
uploadsStorageManager.removeUpload(failedUpload);
}
}
} }
} }
} }