Deleting files in background

This commit is contained in:
Matéo Duparc 2022-06-29 15:09:37 +02:00
parent 8709abd7d7
commit 4d164944c1
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A
3 changed files with 38 additions and 26 deletions

View File

@ -373,7 +373,19 @@ class ExplorerActivity : BaseExplorerActivity() {
val size = explorerAdapter.selectedItems.size val size = explorerAdapter.selectedItems.size
val dialog = CustomAlertDialogBuilder(this, themeValue) val dialog = CustomAlertDialogBuilder(this, themeValue)
dialog.setTitle(R.string.warning) 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) dialog.setNegativeButton(R.string.cancel, null)
if (size > 1) { if (size > 1) {
dialog.setMessage(getString(R.string.multiple_delete_confirm, explorerAdapter.selectedItems.size.toString())) dialog.setMessage(getString(R.string.multiple_delete_confirm, explorerAdapter.selectedItems.size.toString()))
@ -501,29 +513,4 @@ class ExplorerActivity : BaseExplorerActivity() {
super.onBackPressed() 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
}
} }

View File

@ -402,6 +402,30 @@ class FileOperationService : Service() {
waitForTask(notification, task) waitForTask(notification, task)
} }
suspend fun removeElements(items: List<ExplorerElement>): 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 { private fun recursiveCountChildElements(rootDirectory: DocumentFile, scope: CoroutineScope): Int {
if (!scope.isActive) { if (!scope.isActive) {
return 0 return 0

View File

@ -246,4 +246,5 @@
<string name="cryfs">CryFS</string> <string name="cryfs">CryFS</string>
<string name="gocryptfs_disabled">Gocryptfs support has been disabled</string> <string name="gocryptfs_disabled">Gocryptfs support has been disabled</string>
<string name="cryfs_disabled">CryFS support has been disabled</string> <string name="cryfs_disabled">CryFS support has been disabled</string>
<string name="file_op_delete_msg">Deleting files…</string>
</resources> </resources>