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.config
import com.simplemobiletools.filemanager.extensions.isZipFile import com.simplemobiletools.filemanager.extensions.isZipFile
import kotlinx.android.synthetic.main.list_item.view.* 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.*
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipFile 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 val source = if (files[0].isFile) files[0].parent else files[0].absolutePath
FilePickerDialog(activity, source, false, config.shouldShowHidden, true) { FilePickerDialog(activity, source, false, config.shouldShowHidden, true) {
activity.copyMoveFilesTo(files, source, it, isCopyOperation, false) { activity.copyMoveFilesTo(files, source, it, isCopyOperation, false) {
if (!isCopyOperation) {
listener?.refreshItems() listener?.refreshItems()
}
actMode?.finish() actMode?.finish()
} }
} }
@ -265,7 +265,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
val entry = entries.nextElement() val entry = entries.nextElement()
val file = File(it.parent, entry.name) val file = File(it.parent, entry.name)
if (entry.isDirectory) { if (entry.isDirectory) {
if (!createDirectorySync(file)) { if (!activity.createDirectorySync(file)) {
val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath) val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath)
activity.showErrorToast(error) activity.showErrorToast(error)
return false return false
@ -273,32 +273,23 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} else { } else {
val ins = zipFile.getInputStream(entry) val ins = zipFile.getInputStream(entry)
ins.use { ins.use {
val fos = getFileOutputStream(file.absolutePath, file.getMimeType()) val fos = activity.getFileOutputStreamSync(file.absolutePath, file.getMimeType())
if (fos != null) if (fos != null)
ins.copyTo(fos) ins.copyTo(fos)
} }
} }
} }
} catch (e: Exception) { } catch (exception: Exception) {
activity.showErrorToast(e.toString()) activity.showErrorToast(exception)
return false return false
} }
} }
return true 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 { fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean {
val queue = LinkedList<File>() 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) val zout = ZipOutputStream(fos)
var res: Closeable = fos var res: Closeable = fos
@ -338,8 +329,8 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} }
} }
} }
} catch (e: Exception) { } catch (exception: Exception) {
activity.showErrorToast(e.toString()) activity.showErrorToast(exception)
return false return false
} finally { } finally {
res.close() res.close()
@ -347,24 +338,6 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
return true 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() { fun selectAll() {
val cnt = mItems.size val cnt = mItems.size
for (i in 0 until cnt) { 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.File
import java.io.IOException 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) private val view = activity.layoutInflater.inflate(R.layout.dialog_create_new, null)
init { 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) { if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_directory) {
createDirectory(file, this) { createDirectory(file, this) {
if (!it) { callback(it)
errorOccurred()
}
} }
} else { } else {
createFile(file, this) { createFile(file, this) {
if (!it) { callback(it)
errorOccurred()
}
} }
} }
} else { } else {
@ -56,6 +52,8 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(file) { activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(file) {
val documentFile = activity.getFileDocument(file.absolutePath) val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) { if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_folder), file.absolutePath)
activity.showErrorToast(error)
callback(false) callback(false)
return@handleSAFDialog return@handleSAFDialog
} }
@ -64,22 +62,19 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
} }
file.mkdirs() -> { file.mkdirs() -> {
success(alertDialog) success(alertDialog)
callback(true)
} }
else -> callback(false) else -> callback(false)
} }
} }
private fun errorOccurred() {
activity.toast(R.string.unknown_error_occurred)
}
private fun createFile(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) { private fun createFile(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
try { try {
if (activity.needsStupidWritePermissions(path)) { if (activity.needsStupidWritePermissions(path)) {
activity.handleSAFDialog(file) { activity.handleSAFDialog(file) {
val documentFile = activity.getFileDocument(file.absolutePath) val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) { if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_file), file.absolutePath)
activity.showErrorToast(error)
callback(false) callback(false)
return@handleSAFDialog return@handleSAFDialog
} }
@ -88,15 +83,15 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
} }
} else if (file.createNewFile()) { } else if (file.createNewFile()) {
success(alertDialog) success(alertDialog)
callback(true)
} }
} catch (exception: IOException) { } catch (exception: IOException) {
activity.showErrorToast(exception.toString()) activity.showErrorToast(exception)
callback(false)
} }
} }
private fun success(alertDialog: AlertDialog) { private fun success(alertDialog: AlertDialog) {
alertDialog.dismiss() alertDialog.dismiss()
callback() callback(true)
} }
} }

View File

@ -223,9 +223,11 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
private fun createNewItem() { private fun createNewItem() {
CreateNewItemDialog(activity as SimpleActivity, mPath) { CreateNewItemDialog(activity as SimpleActivity, mPath) {
if (it) {
fillItems() fillItems()
} }
} }
}
private fun getGenericMimeType(mimeType: String): String { private fun getGenericMimeType(mimeType: String): String {
if (!mimeType.contains("/")) if (!mimeType.contains("/"))