diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt index cd2fccc82..b2662e5cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt @@ -7,33 +7,51 @@ import android.util.Log import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions import com.simplemobiletools.filepicker.extensions.scanFile +import com.simplemobiletools.filepicker.extensions.scanFiles import com.simplemobiletools.gallery.Config import java.io.* import java.lang.ref.WeakReference +import java.util.* -class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : AsyncTask, File>, Void, Boolean>() { +class CopyTask(listener: CopyTask.CopyListener, val context: Context, val deleteAfterCopy: Boolean) : AsyncTask, File>, Void, Boolean>() { private val TAG = CopyTask::class.java.simpleName - private var mListener: WeakReference? = null - private var destinationDir: File? = null + private var mListener: WeakReference? = null + private var mMovedFiles: ArrayList private var mConfig: Config init { mListener = WeakReference(listener) + mMovedFiles = arrayListOf() mConfig = Config.newInstance(context) } - override fun doInBackground(vararg params: Pair, File>): Boolean? { + override fun doInBackground(vararg params: Pair, File>): Boolean? { val pair = params[0] 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 } } + + if (deleteAfterCopy) { + for (file in mMovedFiles) { + if (context.needsStupidWritePermissions(file.absolutePath)) { + context.getFileDocument(file.absolutePath, mConfig.treeUri) + } else { + file.delete() + } + } + } + context.scanFiles(files) {} + context.scanFiles(mMovedFiles) {} return true } @@ -52,7 +70,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn val document = context.getFileDocument(destination.absolutePath, mConfig.treeUri) document.createDirectory(destination.name) } else if (!destination.mkdirs()) { - throw IOException("Could not create dir " + destination.absolutePath) + throw IOException("Could not create dir ${destination.absolutePath}") } } @@ -70,6 +88,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn val out = context.contentResolver.openOutputStream(document.uri) copyStream(inputStream, out) context.scanFile(destination) {} + mMovedFiles.add(source) } } else { copy(newFile, File(destination, child)) @@ -80,7 +99,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn 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 +107,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn if (context.needsStupidWritePermissions(destination.absolutePath)) { var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri) document = document.createFile("", destination.name) + out = context.contentResolver.openOutputStream(document.uri) } else { out = FileOutputStream(destination) @@ -95,6 +115,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn copyStream(inputStream, out) context.scanFile(destination) {} + mMovedFiles.add(source) } private fun copyStream(inputStream: InputStream, out: OutputStream?) { @@ -112,14 +133,14 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn val listener = mListener?.get() ?: return if (success) { - listener.copySucceeded(destinationDir!!) + listener.copySucceeded(deleteAfterCopy) } else { listener.copyFailed() } } - interface CopyDoneListener { - fun copySucceeded(destinationDir: File) + interface CopyListener { + fun copySucceeded(deleted: Boolean) fun copyFailed() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt index 1dfe9d786..c6344ecdc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt @@ -5,14 +5,16 @@ import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager import com.simplemobiletools.filepicker.extensions.humanizePath +import com.simplemobiletools.filepicker.extensions.isPathOnSD import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.asynctasks.CopyTask import kotlinx.android.synthetic.main.dialog_copy_move.view.* import java.io.File +import java.util.* -class CopyDialog(val activity: SimpleActivity, val files: List, val copyListener: CopyTask.CopyDoneListener, val listener: OnCopyListener) { +class CopyDialog(val activity: SimpleActivity, val files: ArrayList, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) { init { val context = activity @@ -70,11 +72,18 @@ class CopyDialog(val activity: SimpleActivity, val files: List, val copyLi if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { context.toast(R.string.copying) - val pair = Pair, File>(files, destinationDir) - CopyTask(copyListener, context).execute(pair) + val pair = Pair, File>(files, destinationDir) + CopyTask(copyListener, context, false).execute(pair) dismiss() } else { + if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) { + context.toast(R.string.moving) + val pair = Pair, File>(files, destinationDir) + CopyTask(copyListener, context, true).execute(pair) + dismiss() + } else { + } } }) } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 65c886eca..8b3087e19 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -50,9 +50,11 @@ Bitte wähle ein Ziel Source and destination cannot be the same Konnte die Datei nicht kopieren - Kopiere + Kopiere… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully A file with that name already exists diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 2cf577b44..40fe10373 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -50,9 +50,11 @@ Please select a destination Source and destination cannot be the same Could not copy the files - Copying + Copying… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully A file with that name already exists diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 3ce52d40a..6ac5d820a 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -50,9 +50,11 @@ Seleziona una destinazione Source and destination cannot be the same Impossibile copiare i file - Copia in corso + Copia in corso… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully A file with that name already exists diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 3e7c3b53b..ee195d7a9 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -50,9 +50,11 @@ 宛先を選択してください Source and destination cannot be the same ファイルをコピーできませんでした - コピー中 + コピー中… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully A file with that name already exists diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index 45fd9cd7f..9dd59bce6 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -50,9 +50,11 @@ Por favor selecione um destino A origem e o destino não podem ser iguais Não foi possível copiar os ficheiros - A copiar + A copiar… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully Já existe um ficheiro com este nome diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index cc7e31d56..df65cf443 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -50,9 +50,11 @@ Please select a destination Source and destination cannot be the same Kunde inte kopiera filen - Kopierar + Kopierar… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully A file with that name already exists diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 24e025676..4a50b94aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,9 +50,11 @@ Please select a destination Source and destination cannot be the same Could not copy the files - Copying + Copying… Files copied successfully - An error occurred during the copy + An error occurred + Moving… + Files moved successfully A file with that name already exists