瀏覽代碼

Cancel fix

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年之前
父節點
當前提交
15dedf8116

+ 8 - 6
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt

@@ -60,15 +60,17 @@ class FileDownloadHelper {
             return false
             return false
         }
         }
 
 
-        return backgroundJobManager.isStartFileDownloadJobScheduled(user, file.fileId) ||
-            backgroundJobManager.isStartFileDownloadJobScheduled(user, file.parentId)
+        return FileDownloadWorker.isDownloading(user.accountName, file.fileId) ||
+            FileDownloadWorker.isDownloading(user.accountName, file.parentId)
     }
     }
 
 
-    fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) {
-        if (user == null || file == null) return
+    fun cancelPendingOrCurrentDownloads(user: User?, files: List<OCFile>?) {
+        if (user == null || files == null) return
 
 
-        FileDownloadWorker.cancelOperation(user.accountName, file.fileId)
-        backgroundJobManager.cancelFilesDownloadJob(user, file.fileId)
+        files.forEach {
+            FileDownloadWorker.cancelOperation(user.accountName, it.fileId)
+            backgroundJobManager.cancelFilesDownloadJob(user, it.fileId)
+        }
     }
     }
 
 
     fun cancelAllDownloadsForAccount(accountName: String?, currentDownload: DownloadFileOperation?) {
     fun cancelAllDownloadsForAccount(accountName: String?, currentDownload: DownloadFileOperation?) {

+ 15 - 0
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -75,6 +75,21 @@ class FileDownloadWorker(
             }
             }
         }
         }
 
 
+        /*
+                Folder 1 -- id = 100
+                    file 1-- parentID 100
+                    folder 2-- parentID 100
+                        file 3-- parentID 100
+                        file 4 -- parentID 100
+                Folder 4 -- parentID 100
+                    file 6         -- parentID 100
+
+         */
+
+        fun isDownloading(accountName: String, fileId: Long): Boolean {
+            return pendingDownloads.all.any { it.value.payload.isMatching(accountName, fileId) }
+        }
+
         const val WORKER_ID = "WORKER_ID"
         const val WORKER_ID = "WORKER_ID"
         const val FILES_SEPARATOR = ","
         const val FILES_SEPARATOR = ","
         const val FOLDER_REMOTE_PATH = "FOLDER_REMOTE_PATH"
         const val FOLDER_REMOTE_PATH = "FOLDER_REMOTE_PATH"

+ 23 - 1
app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -180,6 +180,29 @@ public class FileDataStorageManager {
         return fileDao.getFileByEncryptedRemotePath(path, user.getAccountName()) != null;
         return fileDao.getFileByEncryptedRemotePath(path, user.getAccountName()) != null;
     }
     }
 
 
+    public List<OCFile> getAllFilesRecursivelyInsideFolder(OCFile file) {
+        ArrayList<OCFile> result = new ArrayList<>();
+
+        if (file == null || !file.fileExists()) {
+            return result;
+        }
+
+        if (!file.isFolder()) {
+            result.add(file);
+            return result;
+        }
+
+        List<OCFile> filesInsideFolder = getFolderContent(file.getFileId(), false);
+        for (OCFile item: filesInsideFolder) {
+            if (!item.isFolder()) {
+                result.add(item);
+            } else {
+                result.addAll(getAllFilesRecursivelyInsideFolder(item));
+            }
+        }
+
+        return result;
+    }
 
 
     public List<OCFile> getFolderContent(OCFile ocFile, boolean onlyOnDevice) {
     public List<OCFile> getFolderContent(OCFile ocFile, boolean onlyOnDevice) {
         if (ocFile != null && ocFile.isFolder() && ocFile.fileExists()) {
         if (ocFile != null && ocFile.isFolder() && ocFile.fileExists()) {
@@ -189,7 +212,6 @@ public class FileDataStorageManager {
         }
         }
     }
     }
 
 
-
     public List<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
     public List<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
         List<OCFile> imageList = new ArrayList<>();
         List<OCFile> imageList = new ArrayList<>();
 
 

+ 5 - 1
app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java

@@ -99,8 +99,12 @@ public class DownloadFileOperation extends RemoteOperation {
         this(user, file, null, null, null, context, DownloadType.DOWNLOAD);
         this(user, file, null, null, null, context, DownloadType.DOWNLOAD);
     }
     }
 
 
+    public boolean isMatching(String accountName, long fileId) {
+        return getFile().getFileId() == fileId && getUser().getAccountName().equals(accountName);
+    }
+
     public void cancelMatchingOperation(String accountName, long fileId) {
     public void cancelMatchingOperation(String accountName, long fileId) {
-        if (getFile().getFileId() == fileId && getUser().getAccountName().equals(accountName)) {
+        if (isMatching(accountName, fileId)) {
             cancel();
             cancel();
         }
         }
     }
     }

+ 2 - 1
app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -997,7 +997,8 @@ public class FileOperationsHelper {
         }
         }
 
 
         if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
         if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
-            FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, file);
+            List<OCFile> files = fileActivity.getStorageManager().getAllFilesRecursivelyInsideFolder(file);
+            FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, files);
         }
         }
 
 
         FileUploaderBinder uploaderBinder = fileActivity.getFileUploaderBinder();
         FileUploaderBinder uploaderBinder = fileActivity.getFileUploaderBinder();