reuse some file operations from the shared lib

This commit is contained in:
tibbi 2017-03-15 22:12:57 +01:00
parent 6854890b8e
commit 56cd0bb14f
5 changed files with 43 additions and 86 deletions

View File

@ -32,7 +32,7 @@ android {
} }
dependencies { dependencies {
compile 'com.simplemobiletools:commons:2.9.9' compile 'com.simplemobiletools:commons:2.11.5'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }

View File

@ -22,7 +22,6 @@ import com.simplemobiletools.filemanager.SCROLL_STATE
import com.simplemobiletools.filemanager.extensions.config import com.simplemobiletools.filemanager.extensions.config
import com.simplemobiletools.filemanager.fragments.ItemsFragment import com.simplemobiletools.filemanager.fragments.ItemsFragment
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.util.* import java.util.*
class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener { 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) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initRootFileManager() initRootFileManager()
} else { } else {
toast(R.string.no_permissions) toast(R.string.no_storage_permissions)
finish() finish()
} }
} }
@ -150,9 +149,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
} }
fun changePath(pickedPath: String) { fun changePath(pickedPath: String) {
if (!isShowingPermDialog(File(pickedPath))) { mBasePath = pickedPath
mBasePath = pickedPath openPath(pickedPath)
openPath(pickedPath)
}
} }
} }

View File

@ -166,27 +166,19 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val files = ArrayList<File>(selections.size) val files = ArrayList<File>(selections.size)
val removeFiles = ArrayList<FileDirItem>(selections.size) val removeFiles = ArrayList<FileDirItem>(selections.size)
var isShowingPermDialog = false activity.handleSAFDialog(File(mItems[selections[0]].path)) {
activity.runOnUiThread { selections.reverse()
if (activity.isShowingPermDialog(File(mItems[selections[0]].path))) { selections.forEach {
isShowingPermDialog = true 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<FileDirItem> { private fun getSelectedMedia(): List<FileDirItem> {

View File

@ -57,39 +57,35 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val c
} }
} }
if (activity.isShowingPermDialog(destinationDir)) { activity.handleSAFDialog(destinationDir) {
return@setOnClickListener if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
} context.toast(R.string.copying)
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying)
val pair = Pair<ArrayList<File>, 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)
val pair = Pair<ArrayList<File>, File>(files, destinationDir) val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyMoveTask(context, true, config.treeUri, false, copyMoveListener).execute(pair) CopyMoveTask(context, false, config.treeUri, false, copyMoveListener).execute(pair)
dismiss() dismiss()
} else { } else {
val updatedFiles = ArrayList<File>(files.size * 2) if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) {
updatedFiles.addAll(files) activity.handleSAFDialog(files[0]) {
for (file in files) { context.toast(R.string.moving)
val destination = File(destinationDir, file.name) val pair = Pair<ArrayList<File>, File>(files, destinationDir)
if (file.renameTo(destination)) CopyMoveTask(context, true, config.treeUri, false, copyMoveListener).execute(pair)
updatedFiles.add(destination)
}
context.scanFiles(updatedFiles) {
activity.runOnUiThread {
copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size)
dismiss() dismiss()
} }
} else {
val updatedFiles = ArrayList<File>(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()
}
}
} }
} }
} }

View File

@ -189,41 +189,13 @@ class ItemsFragment : android.support.v4.app.Fragment(), ItemsAdapter.ItemOperat
} }
override fun deleteFiles(files: ArrayList<File>) { override fun deleteFiles(files: ArrayList<File>) {
val act = activity as SimpleActivity (activity as SimpleActivity).deleteFiles(files) {
if (act.isShowingPermDialog(files[0])) { if (!it) {
return activity.runOnUiThread {
} activity.toast(R.string.unknown_error_occurred)
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)
} }
} }
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() { override fun refreshItems() {