From 06efdb61c31785a70a61ae4bc5f2e2d8fb76908f Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sat, 29 Aug 2020 20:48:12 +0200 Subject: [PATCH] Bug fixes & Allow export to SD cards --- .../sushi/hardcore/droidfs/OpenActivity.kt | 8 +- .../droidfs/explorers/BaseExplorerActivity.kt | 11 ++- .../droidfs/explorers/ExplorerActivity.kt | 83 +++++++++++-------- app/src/main/res/menu/explorer.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 65 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt index 192d957..ea1b912 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt @@ -37,6 +37,7 @@ class OpenActivity : BaseActivity() { private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver private lateinit var rootCipherDir: String private var sessionID = -1 + private var isStartingActivity = false private var isFinishingIntentionally = false private var usf_fingerprint = false override fun onCreate(savedInstanceState: Bundle?) { @@ -83,6 +84,7 @@ class OpenActivity : BaseActivity() { fun pickDirectory(view: View?) { val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + isStartingActivity = true startActivityForResult(i, PICK_DIRECTORY_REQUEST_CODE) } @@ -237,7 +239,11 @@ class OpenActivity : BaseActivity() { override fun onPause() { super.onPause() if (intent.action == "pick"){ - finish() + if (isStartingActivity){ + isStartingActivity = false + } else { + finish() + } } if (::fingerprintPasswordHashSaver.isInitialized && fingerprintPasswordHashSaver.isListening){ fingerprintPasswordHashSaver.stopListening() 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 9ad1aff..c76bd33 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -144,10 +144,9 @@ open class BaseExplorerActivity : BaseActivity() { .show() } } - } else { - invalidateOptionsMenu() } } + invalidateOptionsMenu() } protected open fun onExplorerItemLongClick(position: Int) { @@ -425,8 +424,12 @@ open class BaseExplorerActivity : BaseActivity() { override fun onPause() { super.onPause() - if (!isChangingConfigurations && !isStartingActivity){ - finish() + if (!isChangingConfigurations){ + if (isStartingActivity){ + isStartingActivity = false + } else { + finish() + } } } 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 bad2be0..62d15b3 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -11,6 +11,7 @@ import android.view.WindowManager import android.widget.EditText import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.documentfile.provider.DocumentFile import sushi.hardcore.droidfs.CameraActivity import sushi.hardcore.droidfs.OpenActivity import sushi.hardcore.droidfs.R @@ -225,35 +226,38 @@ class ExplorerActivity : BaseExplorerActivity() { if (resultCode == Activity.RESULT_OK && data != null) { object : LoadingTask(this, R.string.loading_msg_export){ override fun doTask(activity: AppCompatActivity) { - val uri = data.data - val outputDir = PathUtils.getFullPathFromTreeUri(uri, activity) - var failedItem: String? = null - for (i in explorerAdapter.selectedItems) { - val element = explorerAdapter.getItem(i) - val fullPath = PathUtils.path_join(currentDirectoryPath, element.name) - failedItem = if (element.isDirectory) { - recursiveExportDirectory(fullPath, outputDir) - } else { - if (gocryptfsVolume.exportFile(fullPath, PathUtils.path_join(outputDir, element.name))) null else fullPath - } - if (failedItem != null) { - stopTask { - ColoredAlertDialogBuilder(activity) - .setTitle(R.string.error) - .setMessage(getString(R.string.export_failed, failedItem)) - .setPositiveButton(R.string.ok, null) - .show() + data.data?.let {uri -> + contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION) + DocumentFile.fromTreeUri(activity, uri)?.let { treeDocumentFile -> + var failedItem: String? = null + for (i in explorerAdapter.selectedItems) { + val element = explorerAdapter.getItem(i) + val fullPath = PathUtils.path_join(currentDirectoryPath, element.name) + failedItem = if (element.isDirectory) { + recursiveExportDirectory(fullPath, treeDocumentFile) + } else { + if (exportFileInto(fullPath, treeDocumentFile)) null else fullPath + } + if (failedItem != null) { + stopTask { + ColoredAlertDialogBuilder(activity) + .setTitle(R.string.error) + .setMessage(getString(R.string.export_failed, failedItem)) + .setPositiveButton(R.string.ok, null) + .show() + } + break + } + } + if (failedItem == null) { + stopTask { + ColoredAlertDialogBuilder(activity) + .setTitle(R.string.success_export) + .setMessage(R.string.success_export_msg) + .setPositiveButton(R.string.ok, null) + .show() + } } - break - } - } - if (failedItem == null) { - stopTask { - ColoredAlertDialogBuilder(activity) - .setTitle(R.string.success_export) - .setMessage(R.string.success_export_msg) - .setPositiveButton(R.string.ok, null) - .show() } } } @@ -631,7 +635,7 @@ class ExplorerActivity : BaseExplorerActivity() { } private fun recursiveImportDirectoryFromOtherVolume(remote_gocryptfsVolume: GocryptfsVolume, remote_directory_path: String, outputPath: String): String? { - val mappedElements = gocryptfsVolume.recursiveMapFiles(remote_directory_path) + val mappedElements = remote_gocryptfsVolume.recursiveMapFiles(remote_directory_path) val dstDirectoryPath = checkPathOverwrite(PathUtils.path_join(outputPath, File(remote_directory_path).name), true) if (dstDirectoryPath == null){ return "" @@ -663,23 +667,34 @@ class ExplorerActivity : BaseExplorerActivity() { return null } - private fun recursiveExportDirectory(plain_directory_path: String, output_dir: String?): String? { - if (File(PathUtils.path_join(output_dir, plain_directory_path)).mkdir()) { + private fun exportFileInto(srcPath: String, treeDocumentFile: DocumentFile): Boolean { + val outputStream = treeDocumentFile.createFile("*/*", File(srcPath).name)?.uri?.let { + contentResolver.openOutputStream(it) + } + return if (outputStream != null){ + gocryptfsVolume.exportFile(srcPath, outputStream) + } else { + false + } + } + + private fun recursiveExportDirectory(plain_directory_path: String, treeDocumentFile: DocumentFile): String? { + treeDocumentFile.createDirectory(plain_directory_path)?.let {childTree -> val explorerElements = gocryptfsVolume.listDir(plain_directory_path) for (e in explorerElements) { val fullPath = PathUtils.path_join(plain_directory_path, e.name) if (e.isDirectory) { - val failedItem = recursiveExportDirectory(fullPath, output_dir) + val failedItem = recursiveExportDirectory(fullPath, childTree) failedItem?.let { return it } } else { - if (!gocryptfsVolume.exportFile(fullPath, PathUtils.path_join(output_dir, fullPath))) { + if (!exportFileInto(fullPath, childTree)){ return fullPath } } } return null } - return output_dir + return treeDocumentFile.name } private fun recursiveRemoveDirectory(plain_directory_path: String): String? { diff --git a/app/src/main/res/menu/explorer.xml b/app/src/main/res/menu/explorer.xml index 304c0be..6bfc8c6 100644 --- a/app/src/main/res/menu/explorer.xml +++ b/app/src/main/res/menu/explorer.xml @@ -32,7 +32,7 @@ app:showAsAction="ifRoom" android:visible="false" android:icon="@drawable/icon_decrypt" - android:title="@string/decrypt"/> + android:title="@string/decrypt_files"/> GitHub Want to read the documentation, request feature, report bug, read the source code… Check the DroidFS\'s repository ! Share - Decrypt + Export/Decrypt Copying selected items… Copy of %s failed. The selected items have been successfully copied.