From 4d164944c1aad233098ca47028867a0772e1c112 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Wed, 29 Jun 2022 15:09:37 +0200 Subject: [PATCH] Deleting files in background --- .../droidfs/explorers/ExplorerActivity.kt | 39 +++++++------------ .../file_operations/FileOperationService.kt | 24 ++++++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt index 905b741..b303e48 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -373,7 +373,19 @@ class ExplorerActivity : BaseExplorerActivity() { val size = explorerAdapter.selectedItems.size val dialog = CustomAlertDialogBuilder(this, themeValue) dialog.setTitle(R.string.warning) - dialog.setPositiveButton(R.string.ok) { _, _ -> removeSelectedItems() } + dialog.setPositiveButton(R.string.ok) { _, _ -> + taskScope.launch { + fileOperationService.removeElements(explorerAdapter.selectedItems.map { i -> explorerElements[i] })?.let { failedItem -> + CustomAlertDialogBuilder(this@ExplorerActivity, themeValue) + .setTitle(R.string.error) + .setMessage(getString(R.string.remove_failed, failedItem)) + .setPositiveButton(R.string.ok, null) + .show() + } + setCurrentPath(currentDirectoryPath) //refresh + } + unselectAll() + } dialog.setNegativeButton(R.string.cancel, null) if (size > 1) { dialog.setMessage(getString(R.string.multiple_delete_confirm, explorerAdapter.selectedItems.size.toString())) @@ -501,29 +513,4 @@ class ExplorerActivity : BaseExplorerActivity() { super.onBackPressed() } } - - private fun removeSelectedItems() { - var failedItem: String? = null - for (i in explorerAdapter.selectedItems) { - val element = explorerAdapter.explorerElements[i] - val fullPath = PathUtils.pathJoin(currentDirectoryPath, element.name) - if (element.isDirectory) { - val result = encryptedVolume.recursiveRemoveDirectory(fullPath) - result?.let{ failedItem = it } - } else { - if (!encryptedVolume.deleteFile(fullPath)) { - failedItem = fullPath - } - } - if (failedItem != null) { - CustomAlertDialogBuilder(this, themeValue) - .setTitle(R.string.error) - .setMessage(getString(R.string.remove_failed, failedItem)) - .setPositiveButton(R.string.ok, null) - .show() - break - } - } - setCurrentPath(currentDirectoryPath) //refresh - } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt b/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt index fa4bda1..32f325b 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt @@ -402,6 +402,30 @@ class FileOperationService : Service() { waitForTask(notification, task) } + suspend fun removeElements(items: List): String? = coroutineScope { + val notification = showNotification(R.string.file_op_delete_msg, items.size) + val task = async(Dispatchers.IO) { + var failedItem: String? = null + for ((i, element) in items.withIndex()) { + if (element.isDirectory) { + val result = encryptedVolume.recursiveRemoveDirectory(element.fullPath) + result?.let { failedItem = it } + } else { + if (!encryptedVolume.deleteFile(element.fullPath)) { + failedItem = element.fullPath + } + } + if (failedItem == null) { + updateNotificationProgress(notification, i + 1, items.size) + } else { + break + } + } + failedItem + } + waitForTask(notification, task).failedItem + } + private fun recursiveCountChildElements(rootDirectory: DocumentFile, scope: CoroutineScope): Int { if (!scope.isActive) { return 0 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 58216ae..d3fa7bf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -246,4 +246,5 @@ CryFS Gocryptfs support has been disabled CryFS support has been disabled + Deleting files…