From 8709abd7d7ed5f74dbfb7b51cf08a23495ac7d65 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Wed, 29 Jun 2022 14:47:50 +0200 Subject: [PATCH] Don't cancel file operations when changing configuration --- .../droidfs/explorers/BaseExplorerActivity.kt | 8 ++++---- .../hardcore/droidfs/explorers/ExplorerActivity.kt | 13 ++++++------- .../droidfs/explorers/ExplorerActivityPick.kt | 2 +- .../droidfs/file_operations/FileOperationService.kt | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index ad5e057..71c52ac 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -21,9 +21,7 @@ import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext +import kotlinx.coroutines.* import sushi.hardcore.droidfs.BaseActivity import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.ConstValues.isAudio @@ -60,6 +58,7 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene explorerViewModel.currentDirectoryPath = value } protected lateinit var fileOperationService: FileOperationService + protected val taskScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) protected lateinit var explorerElements: MutableList protected lateinit var explorerAdapter: ExplorerElementAdapter private var isCreating = true @@ -464,7 +463,7 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene if (items.size > 0) { checkPathOverwrite(items, currentDirectoryPath) { checkedItems -> checkedItems?.let { - lifecycleScope.launch { + taskScope.launch { val taskResult = fileOperationService.importFilesFromUris(checkedItems.map { it.dstPath!! }, uris) if (taskResult.cancelled) { setCurrentPath(currentDirectoryPath) @@ -593,6 +592,7 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene } protected open fun closeVolumeOnDestroy() { + taskScope.cancel() if (!encryptedVolume.isClosed()) { encryptedVolume.close() } 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 4175798..905b741 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -8,7 +8,6 @@ import android.view.MenuItem import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile -import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch import sushi.hardcore.droidfs.CameraActivity import sushi.hardcore.droidfs.MainActivity @@ -64,7 +63,7 @@ class ExplorerActivity : BaseExplorerActivity() { if (items == null) { remoteEncryptedVolume.close() } else { - lifecycleScope.launch { + taskScope.launch { val failedItem = fileOperationService.copyElements(items, remoteEncryptedVolume) if (failedItem == null) { Toast.makeText(this@ExplorerActivity, R.string.success_import, Toast.LENGTH_SHORT).show() @@ -95,7 +94,7 @@ class ExplorerActivity : BaseExplorerActivity() { } private val pickExportDirectory = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri -> if (uri != null) { - lifecycleScope.launch { + taskScope.launch { val result = fileOperationService.exportFiles(uri, explorerAdapter.selectedItems.map { i -> explorerElements[i] }) if (!result.cancelled) { if (result.failedItem == null) { @@ -118,7 +117,7 @@ class ExplorerActivity : BaseExplorerActivity() { val operation = OperationFile(PathUtils.pathJoin(currentDirectoryPath, tree.name!!), Stat.S_IFDIR) checkPathOverwrite(arrayListOf(operation), currentDirectoryPath) { checkedOperation -> checkedOperation?.let { - lifecycleScope.launch { + taskScope.launch { val result = fileOperationService.importDirectory(checkedOperation[0].dstPath!!, tree) if (result.taskResult.cancelled) { setCurrentPath(currentDirectoryPath) @@ -140,7 +139,7 @@ class ExplorerActivity : BaseExplorerActivity() { ${getString(R.string.ask_for_wipe)} """.trimIndent()) .setPositiveButton(R.string.yes) { _, _ -> - lifecycleScope.launch { + taskScope.launch { val errorMsg = fileOperationService.wipeUris(urisToWipe, rootFile) if (errorMsg == null) { Toast.makeText(this@ExplorerActivity, R.string.wipe_successful, Toast.LENGTH_SHORT).show() @@ -320,7 +319,7 @@ class ExplorerActivity : BaseExplorerActivity() { if (currentItemAction == ItemsActions.COPY){ checkPathOverwrite(itemsToProcess, currentDirectoryPath){ items -> items?.let { - lifecycleScope.launch { + taskScope.launch { val failedItem = fileOperationService.copyElements(it.toMutableList() as ArrayList) if (!isFinishing) { if (failedItem == null) { @@ -351,7 +350,7 @@ class ExplorerActivity : BaseExplorerActivity() { toMove, toClean, ) { - lifecycleScope.launch { + taskScope.launch { val failedItem = fileOperationService.moveElements(toMove, toClean) if (failedItem == null) { Toast.makeText(this@ExplorerActivity, R.string.move_success, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt index 7327db8..3e14300 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt @@ -83,7 +83,7 @@ class ExplorerActivityPick : BaseExplorerActivity() { override fun closeVolumeOnDestroy() { if (!isFinishingIntentionally && !usf_keep_open){ - intent.getParcelableExtra("destinationVolume")?.let { it.close() } + intent.getParcelableExtra("destinationVolume")?.close() super.closeVolumeOnDestroy() } } 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 407e6ea..fa4bda1 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 @@ -110,7 +110,7 @@ class FileOperationService : Service() { tasks[notificationId]?.cancel() } - open class TaskResult(val cancelled: Boolean, val failedItem: T?) + class TaskResult(val cancelled: Boolean, val failedItem: T?) private suspend fun waitForTask(notification: FileOperationNotification, task: Deferred): TaskResult { tasks[notification.notificationId] = task