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,16 +166,7 @@ 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 {
if (activity.isShowingPermDialog(File(mItems[selections[0]].path))) {
isShowingPermDialog = true
}
}
if (isShowingPermDialog)
return
selections.reverse() selections.reverse()
selections.forEach { selections.forEach {
val file = mItems[it] val file = mItems[it]
@ -188,6 +179,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
markedItems.clear() markedItems.clear()
listener?.deleteFiles(files) listener?.deleteFiles(files)
} }
}
private fun getSelectedMedia(): List<FileDirItem> { private fun getSelectedMedia(): List<FileDirItem> {
val positions = multiSelector.selectedPositions val positions = multiSelector.selectedPositions

View File

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

View File

@ -189,42 +189,14 @@ 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() {
fillItems() fillItems()