From 14bbe5bf01b8983de8011f217356b40d912bd4a1 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Wed, 7 Jan 2015 18:23:53 +0100 Subject: [PATCH 1/4] First try --- .../android/files/services/FileDownloader.java | 13 +++++-------- .../operations/SynchronizeFolderOperation.java | 13 +++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index db80691315..eb870e1d71 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -96,9 +96,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis private NotificationCompat.Builder mNotificationBuilder; private int mLastPercent; - private Account mAccount; - private OCFile mFile; - public static String getDownloadAddedMessage() { return FileDownloader.class.getName().toString() + DOWNLOAD_ADDED_MESSAGE; @@ -150,24 +147,24 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis Log_OC.e(TAG, "Not enough information provided in intent"); return START_NOT_STICKY; } else { - mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); - mFile = intent.getParcelableExtra(EXTRA_FILE); + final Account account = intent.getParcelableExtra(EXTRA_ACCOUNT); + final OCFile file = intent.getParcelableExtra(EXTRA_FILE); if (ACTION_CANCEL_FILE_DOWNLOAD.equals(intent.getAction())) { new Thread(new Runnable() { public void run() { // Cancel the download - cancel(mAccount,mFile); + cancel(account, file); } }).start(); } else { AbstractList requestedDownloads = new Vector(); // dvelasco: now this always contains just one element, but that can change in a near future (download of multiple selection) - String downloadKey = buildRemoteName(mAccount, mFile); + String downloadKey = buildRemoteName(account, file); try { - DownloadFileOperation newDownload = new DownloadFileOperation(mAccount, mFile); + DownloadFileOperation newDownload = new DownloadFileOperation(account, file); mPendingDownloads.putIfAbsent(downloadKey, newDownload); newDownload.addDatatransferProgressListener(this); newDownload.addDatatransferProgressListener((FileDownloaderBinder) mBinder); diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index 0dee54f919..8bdc55e07d 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -165,6 +165,11 @@ public class SynchronizeFolderOperation extends SyncOperation { sendBroadcastForNotifyingUIUpdate(result.isSuccess()); } } + + if (mCancellationRequested.get()) { + throw new OperationCancelledException(); + } + } catch (OperationCancelledException e) { result = new RemoteOperationResult(e); @@ -172,6 +177,14 @@ public class SynchronizeFolderOperation extends SyncOperation { for (SyncOperation synchOp: mFoldersToWalkDown) { ((SynchronizeFolderOperation) synchOp).cancel(); } + + /// cancellation of download needs to be done separately in any case; a SynchronizeFolderOperation + // may finish much sooner than the real download of the files in the folder + Intent intent = new Intent(mContext, FileDownloader.class); + intent.setAction(FileDownloader.ACTION_CANCEL_FILE_DOWNLOAD); + intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); + intent.putExtra(FileDownloader.EXTRA_FILE, mLocalFolder); + mContext.startService(intent); } return result; From 737381eaad33262ebd06cd2a4954f1baa19dead4 Mon Sep 17 00:00:00 2001 From: jabarros Date: Thu, 8 Jan 2015 14:59:27 +0100 Subject: [PATCH 2/4] Fix for downloads that are not cancelled --- .../SynchronizeFolderOperation.java | 47 ++++++++++++++----- .../android/ui/activity/FileActivity.java | 2 +- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index 6b62547ebb..957cf62719 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -172,12 +172,7 @@ public class SynchronizeFolderOperation extends SyncOperation { } catch (OperationCancelledException e) { result = new RemoteOperationResult(e); - - // cancel 'child' synchronizations - for (SyncOperation synchOp: mFoldersToWalkDown) { - ((SynchronizeFolderOperation) synchOp).cancel(); - } - + /// cancellation of download needs to be done separately in any case; a SynchronizeFolderOperation // may finish much sooner than the real download of the files in the folder Intent intent = new Intent(mContext, FileDownloader.class); @@ -288,7 +283,8 @@ public class SynchronizeFolderOperation extends SyncOperation { * retrieved. * @return 'True' when any change was made in the local data, 'false' otherwise */ - private void synchronizeData(ArrayList folderAndFiles, OwnCloudClient client) { + private void synchronizeData(ArrayList folderAndFiles, OwnCloudClient client) + throws OperationCancelledException { FileDataStorageManager storageManager = getStorageManager(); // parse data from remote folder @@ -303,7 +299,13 @@ public class SynchronizeFolderOperation extends SyncOperation { mFilesForDirectDownload.clear(); mFilesToSyncContentsWithoutUpload.clear(); mFavouriteFilesToSyncContents.clear(); - mFoldersToWalkDown.clear(); + + synchronized(mFoldersToWalkDown) { + if (mCancellationRequested.get()) { + throw new OperationCancelledException(); + } + mFoldersToWalkDown.clear(); + } // get current data about local contents of the folder to synchronize List localFiles = storageManager.getFolderContent(mLocalFolder); @@ -365,8 +367,14 @@ public class SynchronizeFolderOperation extends SyncOperation { mAccount, mCurrentSyncTime ); - mFoldersToWalkDown.add(synchFolderOp); - + + synchronized(mFoldersToWalkDown) { + if (mCancellationRequested.get()) { + throw new OperationCancelledException(); + } + mFoldersToWalkDown.add(synchFolderOp); + } + } else if (remoteFile.keepInSync()) { /// prepare content synchronization for kept-in-sync files SynchronizeFileOperation operation = new SynchronizeFileOperation( @@ -400,7 +408,7 @@ public class SynchronizeFolderOperation extends SyncOperation { } - private void prepareOpsFromLocalKnowledge() { + private void prepareOpsFromLocalKnowledge() throws OperationCancelledException { List children = getStorageManager().getFolderContent(mLocalFolder); for (OCFile child : children) { /// classify file to sync/download contents later @@ -412,8 +420,14 @@ public class SynchronizeFolderOperation extends SyncOperation { mAccount, mCurrentSyncTime ); - mFoldersToWalkDown.add(synchFolderOp); - + + synchronized(mFoldersToWalkDown) { + if (mCancellationRequested.get()) { + throw new OperationCancelledException(); + } + mFoldersToWalkDown.add(synchFolderOp); + } + } else { /// prepare limited synchronization for regular files if (!child.isDown()) { @@ -554,6 +568,13 @@ public class SynchronizeFolderOperation extends SyncOperation { */ public void cancel() { mCancellationRequested.set(true); + + synchronized(mFoldersToWalkDown) { + // cancel 'child' synchronizations + for (SyncOperation synchOp : mFoldersToWalkDown) { + ((SynchronizeFolderOperation) synchOp).cancel(); + } + } } public String getFolderPath() { diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 5ab3ebaf89..536800bd33 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -512,7 +512,7 @@ implements OnRemoteOperationListener, ComponentsGetter { } private void onSynchronizeFolderOperationFinish(SynchronizeFolderOperation operation, RemoteOperationResult result) { - if (!result.isSuccess()){ + if (!result.isSuccess() && result.getCode() != ResultCode.CANCELLED){ Toast t = Toast.makeText(this, ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), Toast.LENGTH_LONG); t.show(); From e6fc5d13ad8aff2f70a84d9cf96b253a108b836d Mon Sep 17 00:00:00 2001 From: jabarros Date: Thu, 8 Jan 2015 15:02:42 +0100 Subject: [PATCH 3/4] Notify the download cancelation for updating the screen and remove the yellow arrow that continues being shown after the cancel --- .../android/operations/SynchronizeFolderOperation.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index 957cf62719..f12d5dd71c 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -173,6 +173,10 @@ public class SynchronizeFolderOperation extends SyncOperation { } catch (OperationCancelledException e) { result = new RemoteOperationResult(e); + // Needed in case that cancellation occurs before starting any download. + // If not, yellow arrow continues being shown. + sendBroadcastForNotifyingUIUpdate(result.isSuccess()); + /// cancellation of download needs to be done separately in any case; a SynchronizeFolderOperation // may finish much sooner than the real download of the files in the folder Intent intent = new Intent(mContext, FileDownloader.class); From 1975f9e776fc715ec7eae20698562863872547e9 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Thu, 8 Jan 2015 16:56:21 +0100 Subject: [PATCH 4/4] Aesthetic fixes --- .../android/operations/SynchronizeFolderOperation.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index f12d5dd71c..3e12f95ecc 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -175,10 +175,8 @@ public class SynchronizeFolderOperation extends SyncOperation { // Needed in case that cancellation occurs before starting any download. // If not, yellow arrow continues being shown. - sendBroadcastForNotifyingUIUpdate(result.isSuccess()); + sendBroadcastForNotifyingUIUpdate(false); - /// cancellation of download needs to be done separately in any case; a SynchronizeFolderOperation - // may finish much sooner than the real download of the files in the folder Intent intent = new Intent(mContext, FileDownloader.class); intent.setAction(FileDownloader.ACTION_CANCEL_FILE_DOWNLOAD); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);