From 96777a7d367669c38a93f2f49533b47ef7f36ee4 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 1 Dec 2016 17:28:06 +0100 Subject: [PATCH 1/2] extracted string value first try to get uploadStorageManager show job --- .../datamodel/UploadsStorageManager.java | 33 +++++++++++++++++-- .../android/files/services/FileUploader.java | 4 +-- .../services/SyncedFolderJobService.java | 19 +++++++---- .../observer/SyncedFolderObserver.java | 12 +++---- .../ui/activity/UploadListActivity.java | 6 ++-- .../adapter/ExpandableUploadListAdapter.java | 2 +- 6 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/com/owncloud/android/datamodel/UploadsStorageManager.java b/src/com/owncloud/android/datamodel/UploadsStorageManager.java index a6d87e5858..a42bd0dc2e 100644 --- a/src/com/owncloud/android/datamodel/UploadsStorageManager.java +++ b/src/com/owncloud/android/datamodel/UploadsStorageManager.java @@ -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,33 @@ 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 getPendingJobs() { + JobScheduler js = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE); + + ArrayList 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; } /** diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index d63e7a9e81..d465773392 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -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? diff --git a/src/com/owncloud/android/services/SyncedFolderJobService.java b/src/com/owncloud/android/services/SyncedFolderJobService.java index a4f5ff3405..fbc246d377 100644 --- a/src/com/owncloud/android/services/SyncedFolderJobService.java +++ b/src/com/owncloud/android/services/SyncedFolderJobService.java @@ -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); diff --git a/src/com/owncloud/android/services/observer/SyncedFolderObserver.java b/src/com/owncloud/android/services/observer/SyncedFolderObserver.java index c2e81e050e..9c33ac3913 100644 --- a/src/com/owncloud/android/services/observer/SyncedFolderObserver.java +++ b/src/com/owncloud/android/services/observer/SyncedFolderObserver.java @@ -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()); + 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); diff --git a/src/com/owncloud/android/ui/activity/UploadListActivity.java b/src/com/owncloud/android/ui/activity/UploadListActivity.java index f230620764..bc410bc737 100755 --- a/src/com/owncloud/android/ui/activity/UploadListActivity.java +++ b/src/com/owncloud/android/ui/activity/UploadListActivity.java @@ -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; diff --git a/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java b/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java index 81ce06a9dc..9a70b41254 100755 --- a/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java @@ -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 From 2b86d9912ea93166b03cc5aaf4411f6990181afb Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 29 Dec 2016 16:07:25 +0100 Subject: [PATCH 2/2] show pending jobs in upload view cancel pending jobs --- .../android/datamodel/UploadsStorageManager.java | 13 +++++++++++++ .../android/files/services/FileUploader.java | 8 ++++---- .../services/observer/SyncedFolderObserver.java | 2 +- .../ui/adapter/ExpandableUploadListAdapter.java | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/com/owncloud/android/datamodel/UploadsStorageManager.java b/src/com/owncloud/android/datamodel/UploadsStorageManager.java index a42bd0dc2e..221ddd3bc6 100644 --- a/src/com/owncloud/android/datamodel/UploadsStorageManager.java +++ b/src/com/owncloud/android/datamodel/UploadsStorageManager.java @@ -401,6 +401,19 @@ public class UploadsStorageManager extends Observable { 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; + } + } + } + /** * Get all failed uploads. */ diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index d465773392..c0c9659784 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -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); } } diff --git a/src/com/owncloud/android/services/observer/SyncedFolderObserver.java b/src/com/owncloud/android/services/observer/SyncedFolderObserver.java index 9c33ac3913..baa870b6d3 100644 --- a/src/com/owncloud/android/services/observer/SyncedFolderObserver.java +++ b/src/com/owncloud/android/services/observer/SyncedFolderObserver.java @@ -47,7 +47,7 @@ class SyncedFolderObserver extends RecursiveFileObserver { PersistableBundle bundle = new PersistableBundle(); // TODO extract bundle.putString(SyncedFolderJobService.LOCAL_PATH, path); - bundle.putString(SyncedFolderJobService.REMOTE_PATH, syncedFolder.getRemotePath()); + 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()); diff --git a/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java b/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java index 9a70b41254..810f2f6146 100755 --- a/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java @@ -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