From c2c1e4b1e931df4c38d77fa5362d1c6c1f91edad Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Fri, 27 Aug 2021 19:47:35 +0200 Subject: [PATCH] Delete file & folder on wipe --- .../droidfs/explorers/BaseExplorerActivity.kt | 4 ++-- .../droidfs/explorers/ExplorerActivity.kt | 6 ++++-- .../file_operations/FileOperationService.kt | 10 ++++++---- .../java/sushi/hardcore/droidfs/util/Wiper.kt | 15 +++++++++------ 4 files changed, 21 insertions(+), 14 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 26faf80..f063f20 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -425,14 +425,14 @@ open class BaseExplorerActivity : BaseActivity() { } } - fun importDirectory(sourceUri: Uri, callback: (String?, List) -> Unit) { + fun importDirectory(sourceUri: Uri, callback: (String?, List, DocumentFile) -> Unit) { val tree = DocumentFile.fromTreeUri(this, sourceUri)!! //non-null after Lollipop val operation = OperationFile.fromExplorerElement(ExplorerElement(tree.name!!, 0, parentPath = currentDirectoryPath)) checkPathOverwrite(arrayListOf(operation), currentDirectoryPath) { checkedOperation -> checkedOperation?.let { fileOperationService.importDirectory(checkedOperation[0].dstPath!!, tree) { failedItem, uris -> runOnUiThread { - callback(failedItem, uris) + callback(failedItem, uris, tree) } } } 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 9c116b9..7c591ba 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -9,6 +9,7 @@ import android.view.WindowManager import android.widget.EditText import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts +import androidx.documentfile.provider.DocumentFile import sushi.hardcore.droidfs.CameraActivity import sushi.hardcore.droidfs.GocryptfsVolume import sushi.hardcore.droidfs.OpenActivity @@ -121,7 +122,7 @@ class ExplorerActivity : BaseExplorerActivity() { } } - private fun onImportComplete(failedItem: String?, uris: List) { + private fun onImportComplete(failedItem: String?, urisToWipe: List, rootFile: DocumentFile? = null) { if (failedItem == null){ ColoredAlertDialogBuilder(this) .setTitle(R.string.success_import) @@ -130,9 +131,10 @@ class ExplorerActivity : BaseExplorerActivity() { ${getString(R.string.ask_for_wipe)} """.trimIndent()) .setPositiveButton(R.string.yes) { _, _ -> - fileOperationService.wipeUris(uris) { errorMsg -> + fileOperationService.wipeUris(urisToWipe) { errorMsg -> runOnUiThread { if (errorMsg == null){ + rootFile?.delete() Toast.makeText(this, R.string.wipe_successful, Toast.LENGTH_SHORT).show() } else { ColoredAlertDialogBuilder(this) 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 67d4663..99406f4 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 @@ -288,15 +288,17 @@ class FileOperationService : Service() { return@Thread } - updateNotificationProgress(notification, 0, dstDirs.size) - // create destination folders so the new files can use them - for (mkdir in dstDirs) { + for (dir in dstDirs) { if (notifications[notification.notificationId]!!) { cancelNotification(notification) return@Thread } - gocryptfsVolume.mkdir(mkdir) + if (!gocryptfsVolume.mkdir(dir)) { + cancelNotification(notification) + callback(dir, srcUris) + break + } } importFilesFromUris(dstFiles, srcUris, notification) { failedItem -> diff --git a/app/src/main/java/sushi/hardcore/droidfs/util/Wiper.kt b/app/src/main/java/sushi/hardcore/droidfs/util/Wiper.kt index 54c65ca..5475d44 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/util/Wiper.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/util/Wiper.kt @@ -4,6 +4,7 @@ import android.content.Context import android.net.Uri import android.provider.OpenableColumns import android.widget.EditText +import androidx.documentfile.provider.DocumentFile import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.R import java.io.* @@ -23,26 +24,28 @@ object Wiper { val size = cursor.getLong(sizeIndex) cursor.close() try { - var os = context.contentResolver.openOutputStream(uri) + var os = context.contentResolver.openOutputStream(uri)!! val buff = ByteArray(buff_size) Arrays.fill(buff, 0.toByte()) val writes = ceil(size.toDouble() / buff_size).toInt() for (i in 0 until ConstValues.wipe_passes) { for (j in 0 until writes) { - os!!.write(buff) + os.write(buff) } if (i < ConstValues.wipe_passes - 1) { //reopening to flush and seek - os!!.close() - os = context.contentResolver.openOutputStream(uri) + os.close() + os = context.contentResolver.openOutputStream(uri)!! } } try { context.contentResolver.delete(uri, null, null) } catch (e: UnsupportedOperationException){ - (os as FileOutputStream).channel.truncate(0) //truncate to 0 if cannot delete + if (DocumentFile.fromSingleUri(context, uri)?.delete() != true) { + (os as FileOutputStream).channel.truncate(0) //truncate to 0 if cannot delete + } } - os!!.close() + os.close() return null } catch (e: Exception) { return e.message