Przeglądaj źródła

Fix cancel for unique tag

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 rok temu
rodzic
commit
1689aee89a

+ 1 - 0
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt

@@ -69,6 +69,7 @@ class FileDownloadHelper {
     fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) {
         if (user == null || file == null) return
 
+        FileDownloadWorker.pauseWork()
         sendCancelEvent(user, file)
         backgroundJobManager.cancelFilesDownloadJob(user, file.fileId)
     }

+ 18 - 1
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -55,6 +55,7 @@ import com.owncloud.android.utils.theme.ViewThemeUtils
 import java.security.SecureRandom
 import java.util.AbstractList
 import java.util.Vector
+import java.util.concurrent.atomic.AtomicBoolean
 
 @Suppress("LongParameterList", "TooManyFunctions")
 class FileDownloadWorker(
@@ -68,6 +69,16 @@ class FileDownloadWorker(
     companion object {
         private val TAG = FileDownloadWorker::class.java.simpleName
 
+        private val shouldContinueExecution = AtomicBoolean(true)
+
+        fun pauseWork() {
+            shouldContinueExecution.set(false)
+        }
+
+        fun resumeWork() {
+            shouldContinueExecution.set(true)
+        }
+
         const val CANCEL_EVENT = "CANCEL_EVENT"
         const val EVENT_ACCOUNT_NAME = "EVENT_ACCOUNT_NAME"
         const val EVENT_FILE_ID = "EVENT_FILE_ID"
@@ -124,6 +135,10 @@ class FileDownloadWorker(
             registerCancelEvent()
 
             requestDownloads.forEach {
+                if (!shouldContinueExecution.get()) {
+                    return@forEach
+                }
+
                 downloadFile(it)
             }
 
@@ -185,6 +200,8 @@ class FileDownloadWorker(
             pendingDownloads.all.forEach {
                 it.value.payload?.cancelMatchingOperation(accountName, fileId)
             }
+
+            resumeWork()
         }
     }
 
@@ -212,6 +229,7 @@ class FileDownloadWorker(
     }
 
     private fun getRequestDownloads(): AbstractList<String> {
+        shouldContinueExecution.set(true)
         setUser()
         setFolder()
         val files = getFiles()
@@ -279,7 +297,6 @@ class FileDownloadWorker(
 
         if (filesPathList != null) {
             filesPathList.forEach {
-                // FIXME Check if folder content not exist, DownloadFileOperation will not download via content
                 fileDataStorageManager?.getFileByEncryptedRemotePath(it)?.let { file ->
                     result.add(file)
                 }