From d1a44bfc360a930524c3da088c56c301503e2310 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 3 Jan 2024 14:06:18 +0100 Subject: [PATCH] Fix cancelAllDownloads Signed-off-by: alperozturk --- .../files/downloader/FileDownloadHelper.kt | 5 ++++ .../files/downloader/FileDownloadWorker.kt | 28 +++++++++++++++---- .../client/jobs/BackgroundJobManager.kt | 1 - .../client/jobs/BackgroundJobManagerImpl.kt | 4 +-- .../android/files/services/IndexedForest.java | 6 +++- .../SynchronizeFolderOperation.java | 17 ----------- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt index 370800b6c97f..fea0cc61e9a5 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt @@ -22,7 +22,9 @@ package com.nextcloud.client.files.downloader import com.nextcloud.client.account.User +import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.cancelAllDownloads import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.folderDownloadStatusPair +import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.removePendingDownload import com.nextcloud.client.jobs.BackgroundJobManager import com.owncloud.android.MainApp import com.owncloud.android.datamodel.FileDataStorageManager @@ -81,6 +83,7 @@ class FileDownloadHelper { fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) { if (user == null || file == null) return + cancelAllDownloads() backgroundJobManager.cancelFilesDownloadJob(user, file) } @@ -92,6 +95,8 @@ class FileDownloadHelper { currentDownload.cancel() } + + removePendingDownload(accountName) } fun saveFile( diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt index c319d15f52e1..f4dcbb4ac1fe 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt @@ -24,6 +24,7 @@ package com.nextcloud.client.files.downloader import android.accounts.Account import android.accounts.AccountManager import android.accounts.OnAccountsUpdateListener +import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import androidx.core.util.component1 @@ -38,6 +39,8 @@ import com.nextcloud.client.account.UserAccountManager import com.nextcloud.java.util.Optional import com.nextcloud.model.WorkerState import com.nextcloud.model.WorkerStateLiveData +import com.owncloud.android.MainApp +import com.owncloud.android.R import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.OCFile import com.owncloud.android.datamodel.UploadsStorageManager @@ -57,7 +60,7 @@ import java.util.Vector @Suppress("LongParameterList") class FileDownloadWorker( - viewThemeUtils: ViewThemeUtils, + private val viewThemeUtils: ViewThemeUtils, private val accountManager: UserAccountManager, private val uploadsStorageManager: UploadsStorageManager, private var localBroadcastManager: LocalBroadcastManager, @@ -93,6 +96,24 @@ class FileDownloadWorker( fun getDownloadFinishMessage(): String { return FileDownloadWorker::class.java.name + "DOWNLOAD_FINISH" } + + private val pendingDownloads = IndexedForest() + + fun removePendingDownload(accountName: String?) { + pendingDownloads.remove(accountName) + } + + fun cancelAllDownloads() { + pendingDownloads.all.forEach { + it.value.payload?.cancel() + } + + val notificationManager = + MainApp.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.cancel(R.string.downloader_download_in_progress_ticker) + + pendingDownloads.all.clear() + } } private var currentDownload: DownloadFileOperation? = null @@ -100,7 +121,6 @@ class FileDownloadWorker( private var lastPercent = 0 private val intents = FileDownloadIntents(context) private val notificationManager = DownloadNotificationManager(context, viewThemeUtils) - private val pendingDownloads = IndexedForest() private var downloadProgressListener = FileDownloadProgressListener() private var currentUser = Optional.empty() private var storageManager: FileDataStorageManager? = null @@ -266,10 +286,6 @@ class FileDownloadWorker( } } - private fun removePendingDownload(accountName: String?) { - pendingDownloads.remove(accountName) - } - private fun notifyDownloadStart(download: DownloadFileOperation) { lastPercent = 0 diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt index f60b7f3000fd..cc29456db280 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -24,7 +24,6 @@ import androidx.work.ListenableWorker import com.nextcloud.client.account.User import com.owncloud.android.datamodel.OCFile import com.owncloud.android.operations.DownloadType -import java.util.concurrent.atomic.AtomicBoolean /** * This interface allows to control, schedule and monitor all application diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index c89cc4176ab1..735270490574 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -524,7 +524,7 @@ internal class BackgroundJobManagerImpl( FileDownloadWorker.USER_NAME to user.accountName, FileDownloadWorker.FOLDER_ID to folder.fileId, FileDownloadWorker.FILES to gson.toJson(files), - FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString(), + FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString() ) val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user) @@ -551,7 +551,7 @@ internal class BackgroundJobManagerImpl( FileDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(), FileDownloadWorker.ACTIVITY_NAME to activityName, FileDownloadWorker.PACKAGE_NAME to packageName, - FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId, + FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId ) val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user) diff --git a/app/src/main/java/com/owncloud/android/files/services/IndexedForest.java b/app/src/main/java/com/owncloud/android/files/services/IndexedForest.java index 26def14d01d4..3ca41db53297 100644 --- a/app/src/main/java/com/owncloud/android/files/services/IndexedForest.java +++ b/app/src/main/java/com/owncloud/android/files/services/IndexedForest.java @@ -43,8 +43,12 @@ public class IndexedForest { private ConcurrentMap> mMap = new ConcurrentHashMap<>(); + public ConcurrentMap> getAll() { + return mMap; + } + @SuppressWarnings("PMD.ShortClassName") - private class Node { + public class Node { private String mKey; private Node mParent; private Set> mChildren = new HashSet<>(); // TODO be careful with hash() diff --git a/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java b/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java index 03e5728868d7..ec65a4cc7e3b 100644 --- a/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -448,23 +448,6 @@ private void startDirectDownloads() { FileDownloadHelper.Companion.instance().downloadFolder(mLocalFolder, user, mFilesForDirectDownload); - - // FIXME cancel request - /* - folderDownloadStatusPair.put(mLocalFolder.getFileId(), true); - - for (OCFile file : mFilesForDirectDownload) { - synchronized(mCancellationRequested) { - if (mCancellationRequested.get()) { - throw new OperationCancelledException(); - } - FileDownloadHelper.Companion.instance().downloadFile(user, file); - } - } - - folderDownloadStatusPair.replace(mLocalFolder.getFileId(), false); - */ - } /**