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.
*

View file

@ -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
);

View file

@ -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)

View file

@ -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 {

View file

@ -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 (!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();
}
}
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();
} else {
if (accountExists && fileExists) {
uploadRequester.retry(context, failedUpload);
} else {
uploadsStorageManager.removeUpload(failedUpload);
}
}
}