Merge pull request #487 from nextcloud/pendingJobsinUploadView

Pending jobs in upload view
This commit is contained in:
Andy Scherzinger 2016-12-31 17:52:25 +01:00 committed by GitHub
commit d718676709
6 changed files with 74 additions and 25 deletions

View file

@ -20,10 +20,14 @@
*/
package com.owncloud.android.datamodel;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.PersistableBundle;
import com.owncloud.android.db.OCUpload;
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
@ -33,7 +37,9 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.operations.UploadFileOperation;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Observable;
/**
@ -43,6 +49,7 @@ import java.util.Observable;
public class UploadsStorageManager extends Observable {
private ContentResolver mContentResolver;
private Context mContext;
private static final String AND = " AND ";
static private final String TAG = UploadsStorageManager.class.getSimpleName();
@ -89,11 +96,12 @@ public class UploadsStorageManager extends Observable {
}
public UploadsStorageManager(ContentResolver contentResolver) {
public UploadsStorageManager(ContentResolver contentResolver, Context context) {
if (contentResolver == null) {
throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
}
mContentResolver = contentResolver;
mContext = context;
}
/**
@ -364,12 +372,46 @@ public class UploadsStorageManager extends Observable {
* Get all uploads which are currently being uploaded or waiting in the queue to be uploaded.
*/
public OCUpload[] getCurrentAndPendingUploads() {
return getUploads(
OCUpload[] uploads = getUploads(
ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_IN_PROGRESS.value + " OR " +
ProviderTableMeta.UPLOADS_LAST_RESULT + "==" + UploadResult.DELAYED_FOR_WIFI.getValue() + " OR " +
ProviderTableMeta.UPLOADS_LAST_RESULT + "==" + UploadResult.DELAYED_FOR_CHARGING.getValue(),
null
);
// add pending Jobs
return getPendingJobs().toArray(uploads);
}
private List<OCUpload> getPendingJobs() {
JobScheduler js = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
ArrayList<OCUpload> list = new ArrayList<>();
for (JobInfo ji: js.getAllPendingJobs()) {
PersistableBundle extras = ji.getExtras();
OCUpload upload = new OCUpload(extras.getString("filePath"),
extras.getString("remotePath"),
extras.getString("account"));
list.add(upload);
}
return list;
}
public void cancelPendingJob(String accountName, String remotePath){
JobScheduler js = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
for (JobInfo ji: js.getAllPendingJobs()) {
PersistableBundle extras = ji.getExtras();
if (remotePath.equalsIgnoreCase(extras.getString("remotePath")) &&
accountName.equalsIgnoreCase(extras.getString("account"))){
js.cancel(ji.getId());
break;
}
}
}
/**

View file

@ -294,7 +294,7 @@ public class FileUploader extends Service
* 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());
UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(context.getContentResolver(), context);
OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
Account currentAccount = null;
boolean resultMatch, accountMatch;
@ -346,7 +346,7 @@ public class FileUploader extends Service
mServiceHandler = new ServiceHandler(mServiceLooper, this);
mBinder = new FileUploaderBinder();
mUploadsStorageManager = new UploadsStorageManager(getContentResolver());
mUploadsStorageManager = new UploadsStorageManager(getContentResolver(), getApplicationContext());
int failedCounter = mUploadsStorageManager.failInProgressUploads(
UploadResult.SERVICE_INTERRUPTED // Add UploadResult.KILLED?
@ -677,10 +677,10 @@ public class FileUploader extends Service
upload.cancel();
// need to update now table in mUploadsStorageManager,
// since the operation will not get to be run by FileUploader#uploadFile
mUploadsStorageManager.removeUpload(
accountName,
remotePath
);
mUploadsStorageManager.removeUpload(accountName, remotePath);
} else {
// try to cancel job in jobScheduler
mUploadsStorageManager.cancelPendingJob(accountName, remotePath);
}
}

View file

@ -44,6 +44,13 @@ import java.io.File;
public class SyncedFolderJobService extends JobService {
private static final String TAG = "SyncedFolderJobService";
public static final String LOCAL_PATH = "filePath";
public static final String REMOTE_PATH = "remotePath";
public static final String DATE_TAKEN = "dateTaken";
public static final String SUBFOLDER_BY_DATE = "subfolderByDate";
public static final String ACCOUNT = "account";
public static final String UPLOAD_BEHAVIOUR = "uploadBehaviour";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
@ -53,12 +60,12 @@ public class SyncedFolderJobService extends JobService {
public boolean onStartJob(JobParameters params) {
Context context = MainApp.getAppContext();
PersistableBundle bundle = params.getExtras();
String filePath = bundle.getString("filePath");
String remoteFolder = bundle.getString("remotePath");
Long dateTaken = bundle.getLong("dateTaken");
Boolean subfolderByDate = bundle.getInt("subfolderByDate") == 1;
Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString("account"));
Integer uploadBehaviour = bundle.getInt("uploadBehaviour");
String filePath = bundle.getString(LOCAL_PATH);
String remoteFolder = bundle.getString(REMOTE_PATH);
Long dateTaken = bundle.getLong(DATE_TAKEN);
Boolean subfolderByDate = bundle.getInt(SUBFOLDER_BY_DATE) == 1;
Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT));
Integer uploadBehaviour = bundle.getInt(UPLOAD_BEHAVIOUR);
Log_OC.d(TAG, "startJob: " + params.getJobId() + ", filePath: " + filePath);

View file

@ -46,12 +46,12 @@ class SyncedFolderObserver extends RecursiveFileObserver {
if (!temp.getName().equalsIgnoreCase("null")) {
PersistableBundle bundle = new PersistableBundle();
// TODO extract
bundle.putString("filePath", path);
bundle.putString("remotePath", syncedFolder.getRemotePath());
bundle.putLong("dateTaken", new Date().getTime());
bundle.putString("account", syncedFolder.getAccount());
bundle.putInt("uploadBehaviour", syncedFolder.getUploadAction());
bundle.putInt("subfolderByDate", syncedFolder.getSubfolderByDate() ? 1 : 0);
bundle.putString(SyncedFolderJobService.LOCAL_PATH, path);
bundle.putString(SyncedFolderJobService.REMOTE_PATH, syncedFolder.getRemotePath() + "/" + temp.getName());
bundle.putLong(SyncedFolderJobService.DATE_TAKEN, new Date().getTime());
bundle.putString(SyncedFolderJobService.ACCOUNT, syncedFolder.getAccount());
bundle.putInt(SyncedFolderJobService.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());
bundle.putInt(SyncedFolderJobService.SUBFOLDER_BY_DATE, syncedFolder.getSubfolderByDate() ? 1 : 0);
JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

View file

@ -202,19 +202,19 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
break;
case R.id.action_clear_failed_uploads:
storageManager = new UploadsStorageManager(getContentResolver());
storageManager = new UploadsStorageManager(getContentResolver(), getApplicationContext());
storageManager.clearFailedButNotDelayedUploads();
uploadListFragment.updateUploads();
break;
case R.id.action_clear_successfull_uploads:
storageManager = new UploadsStorageManager(getContentResolver());
storageManager = new UploadsStorageManager(getContentResolver(), getApplicationContext());
storageManager.clearSuccessfulUploads();
uploadListFragment.updateUploads();
break;
case R.id.action_clear_finished_uploads:
storageManager = new UploadsStorageManager(getContentResolver());
storageManager = new UploadsStorageManager(getContentResolver(), getApplicationContext());
storageManager.clearAllFinishedButNotDelayedUploads();
uploadListFragment.updateUploads();
break;

View file

@ -136,7 +136,7 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
public ExpandableUploadListAdapter(FileActivity parentActivity) {
Log_OC.d(TAG, "ExpandableUploadListAdapter");
mParentActivity = parentActivity;
mUploadsStorageManager = new UploadsStorageManager(mParentActivity.getContentResolver());
mUploadsStorageManager = new UploadsStorageManager(mParentActivity.getContentResolver(), parentActivity.getApplicationContext());
mUploadGroups = new UploadGroup[3];
mUploadGroups[0] = new UploadGroup(mParentActivity.getString(R.string.uploads_view_group_current_uploads)) {
@Override
@ -209,7 +209,7 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
view = inflator.inflate(R.layout.upload_list_item, parent, false);
}
if (uploadsItems != null && uploadsItems.length > position) {
if (uploadsItems != null && uploadsItems.length > position && uploadsItems[position] != null) {
final OCUpload upload = uploadsItems[position];
// local file name