From 7d08cd8653bdb2a41959cc401d8100a5398eaecd Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 5 Nov 2016 19:37:03 +0100 Subject: [PATCH] add directory copying on SD card --- .../filemanager/asynctasks/CopyTask.kt | 76 +++++++++++++------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt index 0bb48a2b..7797c95f 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt +++ b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt @@ -36,41 +36,67 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas @Throws(Exception::class) private fun copy(source: File, destination: File) { if (source.isDirectory) { - if (!destination.exists() && !destination.mkdirs()) { + copyDirectory(source, destination) + } else { + copyFile() + } + } + + private fun copyDirectory(source: File, destination: File) { + if (!destination.exists()) { + if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { + val document = Utils.getFileDocument(context, destination.absolutePath) + document.createDirectory(destination.name) + } else if (!destination.mkdirs()) { throw IOException("Could not create dir " + destination.absolutePath) } + } - val children = source.list() - for (child in children) { - copy(File(source, child), File(destination, child)) - } - } else { - val directory = destination.parentFile - if (!directory.exists() && !directory.mkdirs()) { - throw IOException("Could not create dir " + directory.absolutePath) - } - - val inputStream = FileInputStream(source) - var out: OutputStream? + val children = source.list() + for (child in children) { if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { var document = Utils.getFileDocument(context, destination.absolutePath) - document = document.createFile("", destination.name) - out = context.contentResolver.openOutputStream(document.uri) - } else { - out = FileOutputStream(destination) - } + document = document.createFile("", child) - val buf = ByteArray(1024) - var len: Int - while (true) { - len = inputStream.read(buf) - if (len <= 0) - break - out!!.write(buf, 0, len) + val inputStream = FileInputStream(File(source, child)) + val out = context.contentResolver.openOutputStream(document.uri) + copyStream(inputStream, out) + } else { + copy(File(source, child), File(destination, child)) } } } + private fun copyFile(source: File, destination: File) { + val directory = destination.parentFile + if (!directory.exists() && !directory.mkdirs()) { + throw IOException("Could not create dir " + directory.absolutePath) + } + + val inputStream = FileInputStream(source) + val out: OutputStream? + if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { + var document = Utils.getFileDocument(context, destination.absolutePath) + document = document.createFile("", destination.name) + out = context.contentResolver.openOutputStream(document.uri) + } else { + out = FileOutputStream(destination) + } + + copyStream(inputStream, out) + } + + private fun copyStream(inputStream: InputStream, out: OutputStream?) { + val buf = ByteArray(1024) + var len: Int + while (true) { + len = inputStream.read(buf) + if (len <= 0) + break + out?.write(buf, 0, len) + } + } + override fun onPostExecute(success: Boolean) { val listener = mListener?.get() ?: return