From 56cd0bb14f52db5bb6a493853a1be624acac1fbd Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 15 Mar 2017 22:12:57 +0100 Subject: [PATCH] reuse some file operations from the shared lib --- app/build.gradle | 2 +- .../filemanager/activities/MainActivity.kt | 9 ++-- .../filemanager/adapters/ItemsAdapter.kt | 30 ++++------- .../filemanager/dialogs/CopyDialog.kt | 52 +++++++++---------- .../filemanager/fragments/ItemsFragment.kt | 36 ++----------- 5 files changed, 43 insertions(+), 86 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0e266e7c..63c902d7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.9.9' + compile 'com.simplemobiletools:commons:2.11.5' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt index d5b96ad2..927fc606 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -22,7 +22,6 @@ import com.simplemobiletools.filemanager.SCROLL_STATE import com.simplemobiletools.filemanager.extensions.config import com.simplemobiletools.filemanager.fragments.ItemsFragment import kotlinx.android.synthetic.main.activity_main.* -import java.io.File import java.util.* class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener { @@ -128,7 +127,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initRootFileManager() } else { - toast(R.string.no_permissions) + toast(R.string.no_storage_permissions) finish() } } @@ -150,9 +149,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br } fun changePath(pickedPath: String) { - if (!isShowingPermDialog(File(pickedPath))) { - mBasePath = pickedPath - openPath(pickedPath) - } + mBasePath = pickedPath + openPath(pickedPath) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt index 60e2386f..275290d2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt @@ -166,27 +166,19 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList(selections.size) val removeFiles = ArrayList(selections.size) - var isShowingPermDialog = false - activity.runOnUiThread { - if (activity.isShowingPermDialog(File(mItems[selections[0]].path))) { - isShowingPermDialog = true + activity.handleSAFDialog(File(mItems[selections[0]].path)) { + selections.reverse() + selections.forEach { + val file = mItems[it] + files.add(File(file.path)) + removeFiles.add(file) + notifyItemRemoved(it) } + + mItems.removeAll(removeFiles) + markedItems.clear() + listener?.deleteFiles(files) } - - if (isShowingPermDialog) - return - - selections.reverse() - selections.forEach { - val file = mItems[it] - files.add(File(file.path)) - removeFiles.add(file) - notifyItemRemoved(it) - } - - mItems.removeAll(removeFiles) - markedItems.clear() - listener?.deleteFiles(files) } private fun getSelectedMedia(): List { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt index cf817332..a8fc8351 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt @@ -57,39 +57,35 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList, val c } } - if (activity.isShowingPermDialog(destinationDir)) { - return@setOnClickListener - } - - if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { - context.toast(R.string.copying) - val pair = Pair, File>(files, destinationDir) - CopyMoveTask(context, false, config.treeUri, false, copyMoveListener).execute(pair) - dismiss() - } else { - if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) { - if (activity.isShowingPermDialog(files[0])) { - return@setOnClickListener - } - - context.toast(R.string.moving) + activity.handleSAFDialog(destinationDir) { + if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { + context.toast(R.string.copying) val pair = Pair, File>(files, destinationDir) - CopyMoveTask(context, true, config.treeUri, false, copyMoveListener).execute(pair) + CopyMoveTask(context, false, config.treeUri, false, copyMoveListener).execute(pair) dismiss() } else { - val updatedFiles = ArrayList(files.size * 2) - updatedFiles.addAll(files) - for (file in files) { - val destination = File(destinationDir, file.name) - if (file.renameTo(destination)) - updatedFiles.add(destination) - } - - context.scanFiles(updatedFiles) { - activity.runOnUiThread { - copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) + if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) { + activity.handleSAFDialog(files[0]) { + context.toast(R.string.moving) + val pair = Pair, File>(files, destinationDir) + CopyMoveTask(context, true, config.treeUri, false, copyMoveListener).execute(pair) dismiss() } + } else { + val updatedFiles = ArrayList(files.size * 2) + updatedFiles.addAll(files) + for (file in files) { + val destination = File(destinationDir, file.name) + if (!destination.exists() && file.renameTo(destination)) + updatedFiles.add(destination) + } + + context.scanFiles(updatedFiles) { + activity.runOnUiThread { + copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) + dismiss() + } + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt index 13be4424..d3581216 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt @@ -189,41 +189,13 @@ class ItemsFragment : android.support.v4.app.Fragment(), ItemsAdapter.ItemOperat } override fun deleteFiles(files: ArrayList) { - val act = activity as SimpleActivity - if (act.isShowingPermDialog(files[0])) { - return - } - - Thread({ - var hadSuccess = false - files.forEach { - if (it.isDirectory) { - for (child in it.listFiles()) { - deleteFile(child, act) - } - } - if (deleteFile(it, act)) { - hadSuccess = true - context.deleteFromMediaStore(it) + (activity as SimpleActivity).deleteFiles(files) { + if (!it) { + activity.runOnUiThread { + activity.toast(R.string.unknown_error_occurred) } } - if (!hadSuccess) - act.runOnUiThread { - act.toast(R.string.unknown_error_occurred) - } - }).start() - } - - private fun deleteFile(file: File, act: SimpleActivity): Boolean { - if (file.delete() || act.tryFastDocumentDelete(file)) { - return true - } else { - val document = act.getFileDocument(file.absolutePath, context.config.treeUri) ?: return false - if (document.isFile && document.delete()) { - return true - } } - return false } override fun refreshItems() {