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 {
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"
}

View File

@ -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)
}
}

View File

@ -166,27 +166,19 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val files = ArrayList<File>(selections.size)
val removeFiles = ArrayList<FileDirItem>(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<FileDirItem> {

View File

@ -57,39 +57,35 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, 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<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)
activity.handleSAFDialog(destinationDir) {
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, true, config.treeUri, false, copyMoveListener).execute(pair)
CopyMoveTask(context, false, config.treeUri, false, copyMoveListener).execute(pair)
dismiss()
} else {
val updatedFiles = ArrayList<File>(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<ArrayList<File>, File>(files, destinationDir)
CopyMoveTask(context, true, config.treeUri, false, copyMoveListener).execute(pair)
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>) {
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() {