From 2222c886500f11606266eb7de6cf8f611817c776 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 11 Nov 2016 19:15:24 +0100 Subject: [PATCH] couple copy adjustments --- .../filemanager/asynctasks/CopyTask.kt | 21 ++++++++++++------- .../filemanager/fragments/ItemsFragment.java | 3 +-- .../filemanager/dialogs/CopyDialog.kt | 7 +++---- 3 files changed, 17 insertions(+), 14 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 4174cb33..fc0280cb 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt +++ b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt @@ -5,13 +5,13 @@ import android.os.AsyncTask import android.support.v4.util.Pair import android.util.Log import com.simplemobiletools.filemanager.Utils +import com.simplemobiletools.filemanager.extensions.rescanItem import java.io.* import java.lang.ref.WeakReference class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTask, File>, Void, Boolean>() { private val TAG = CopyTask::class.java.simpleName private var mListener: WeakReference? = null - private var destinationDir: File? = null init { mListener = WeakReference(listener) @@ -22,10 +22,13 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas val files = pair.first for (file in files) { try { - destinationDir = File(pair.second, file.name) - copy(file, destinationDir!!) + val curFile = File(pair.second, file.name) + if (curFile.exists()) + continue + + copy(file, curFile) } catch (e: Exception) { - Log.e(TAG, "copy " + e) + Log.e(TAG, "copy $e") return false } } @@ -47,7 +50,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas val document = Utils.getFileDocument(context, destination.absolutePath) document.createDirectory(destination.name) } else if (!destination.mkdirs()) { - throw IOException("Could not create dir " + destination.absolutePath) + throw IOException("Could not create dir ${destination.absolutePath}") } } @@ -64,6 +67,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas val inputStream = FileInputStream(newFile) val out = context.contentResolver.openOutputStream(document.uri) copyStream(inputStream, out) + context.rescanItem(destination) } } else { copy(newFile, File(destination, child)) @@ -74,7 +78,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas private fun copyFile(source: File, destination: File) { val directory = destination.parentFile if (!directory.exists() && !directory.mkdirs()) { - throw IOException("Could not create dir " + directory.absolutePath) + throw IOException("Could not create dir ${directory.absolutePath}") } val inputStream = FileInputStream(source) @@ -88,6 +92,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas } copyStream(inputStream, out) + context.rescanItem(destination) } private fun copyStream(inputStream: InputStream, out: OutputStream?) { @@ -105,14 +110,14 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas val listener = mListener?.get() ?: return if (success) { - listener.copySucceeded(destinationDir!!) + listener.copySucceeded() } else { listener.copyFailed() } } interface CopyListener { - fun copySucceeded(destinationDir: File) + fun copySucceeded() fun copyFailed() } diff --git a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java index 0823c894..35f95d25 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -492,8 +492,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment } @Override - public void copySucceeded(File file) { - rescanItem(file); + public void copySucceeded() { fillItems(); Utils.Companion.showToast(getContext(), R.string.copying_success); } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt index 63510434..941d8b97 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt @@ -14,7 +14,6 @@ import com.simplemobiletools.filemanager.activities.MainActivity import com.simplemobiletools.filemanager.asynctasks.CopyTask import com.simplemobiletools.filemanager.extensions.rescanItem import com.simplemobiletools.filemanager.extensions.toast -import com.simplemobiletools.filemanager.extensions.value import com.simplemobiletools.filepicker.dialogs.FilePickerDialog import com.simplemobiletools.filepicker.extensions.humanizePath import kotlinx.android.synthetic.main.copy_item.view.* @@ -25,9 +24,9 @@ class CopyDialog(val activity: Activity, val files: List, val copyListener init { val context = activity val view = LayoutInflater.from(context).inflate(R.layout.copy_item, null) - val path = files[0].parent.trimEnd('/') + val sourcePath = files[0].parent.trimEnd('/') var destinationPath = "" - view.source.text = "${context.humanizePath(path)}/" + view.source.text = "${context.humanizePath(sourcePath)}/" view.destination.setOnClickListener { val config = Config.newInstance(context) @@ -91,7 +90,7 @@ class CopyDialog(val activity: Activity, val files: List, val copyListener CopyTask(copyListener, context).execute(pair) dismiss() } else { - if (Utils.isPathOnSD(context, view.source.value) && Utils.isPathOnSD(context, destinationPath)) { + if (Utils.isPathOnSD(context, sourcePath) && Utils.isPathOnSD(context, destinationPath)) { for (f in files) { val destination = File(destinationDir, f.name) f.renameTo(destination)