some updates related to file copy/move/de/compress

This commit is contained in:
tibbi 2017-09-03 12:59:52 +02:00
parent 0eb5228102
commit 5f39a70be3
3 changed files with 24 additions and 54 deletions

View File

@ -27,7 +27,9 @@ import com.simplemobiletools.filemanager.dialogs.CompressAsDialog
import com.simplemobiletools.filemanager.extensions.config
import com.simplemobiletools.filemanager.extensions.isZipFile
import kotlinx.android.synthetic.main.list_item.view.*
import java.io.*
import java.io.Closeable
import java.io.File
import java.io.FileInputStream
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
@ -201,9 +203,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val source = if (files[0].isFile) files[0].parent else files[0].absolutePath
FilePickerDialog(activity, source, false, config.shouldShowHidden, true) {
activity.copyMoveFilesTo(files, source, it, isCopyOperation, false) {
if (!isCopyOperation) {
listener?.refreshItems()
}
listener?.refreshItems()
actMode?.finish()
}
}
@ -265,7 +265,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val entry = entries.nextElement()
val file = File(it.parent, entry.name)
if (entry.isDirectory) {
if (!createDirectorySync(file)) {
if (!activity.createDirectorySync(file)) {
val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath)
activity.showErrorToast(error)
return false
@ -273,32 +273,23 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} else {
val ins = zipFile.getInputStream(entry)
ins.use {
val fos = getFileOutputStream(file.absolutePath, file.getMimeType())
val fos = activity.getFileOutputStreamSync(file.absolutePath, file.getMimeType())
if (fos != null)
ins.copyTo(fos)
}
}
}
} catch (e: Exception) {
activity.showErrorToast(e.toString())
} catch (exception: Exception) {
activity.showErrorToast(exception)
return false
}
}
return true
}
private fun createDirectorySync(file: File): Boolean {
if (activity.needsStupidWritePermissions(file.absolutePath)) {
val documentFile = activity.getFileDocument(file.absolutePath) ?: return false
val newDir = documentFile.createDirectory(file.name)
return newDir != null
}
return file.mkdirs()
}
fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean {
val queue = LinkedList<File>()
val fos = getFileOutputStream(targetPath, "application/zip") ?: return false
val fos = activity.getFileOutputStreamSync(targetPath, "application/zip") ?: return false
val zout = ZipOutputStream(fos)
var res: Closeable = fos
@ -338,8 +329,8 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
}
}
}
} catch (e: Exception) {
activity.showErrorToast(e.toString())
} catch (exception: Exception) {
activity.showErrorToast(exception)
return false
} finally {
res.close()
@ -347,24 +338,6 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
return true
}
private fun getFileOutputStream(targetPath: String, mimeType: String): OutputStream? {
val targetFile = File(targetPath)
return if (activity.needsStupidWritePermissions(targetPath)) {
val documentFile = activity.getFileDocument(targetFile.parent)
if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_file), targetFile.parent)
activity.showErrorToast(error)
return null
}
val newDocument = documentFile.createFile(mimeType, File(targetPath).name)
activity.contentResolver.openOutputStream(newDocument!!.uri)
} else {
FileOutputStream(targetFile)
}
}
fun selectAll() {
val cnt = mItems.size
for (i in 0 until cnt) {

View File

@ -10,7 +10,7 @@ import kotlinx.android.synthetic.main.dialog_create_new.view.*
import java.io.File
import java.io.IOException
class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val callback: () -> Unit) {
class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val callback: (success: Boolean) -> Unit) {
private val view = activity.layoutInflater.inflate(R.layout.dialog_create_new, null)
init {
@ -33,15 +33,11 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_directory) {
createDirectory(file, this) {
if (!it) {
errorOccurred()
}
callback(it)
}
} else {
createFile(file, this) {
if (!it) {
errorOccurred()
}
callback(it)
}
}
} else {
@ -56,6 +52,8 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(file) {
val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_folder), file.absolutePath)
activity.showErrorToast(error)
callback(false)
return@handleSAFDialog
}
@ -64,22 +62,19 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
}
file.mkdirs() -> {
success(alertDialog)
callback(true)
}
else -> callback(false)
}
}
private fun errorOccurred() {
activity.toast(R.string.unknown_error_occurred)
}
private fun createFile(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
try {
if (activity.needsStupidWritePermissions(path)) {
activity.handleSAFDialog(file) {
val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath)
activity.showErrorToast(error)
callback(false)
return@handleSAFDialog
}
@ -88,15 +83,15 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
}
} else if (file.createNewFile()) {
success(alertDialog)
callback(true)
}
} catch (exception: IOException) {
activity.showErrorToast(exception.toString())
activity.showErrorToast(exception)
callback(false)
}
}
private fun success(alertDialog: AlertDialog) {
alertDialog.dismiss()
callback()
callback(true)
}
}

View File

@ -223,7 +223,9 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
private fun createNewItem() {
CreateNewItemDialog(activity as SimpleActivity, mPath) {
fillItems()
if (it) {
fillItems()
}
}
}