diff --git a/app/src/main/java/com/simplemobiletools/filemanager/Utils.kt b/app/src/main/java/com/simplemobiletools/filemanager/Utils.kt index 62dbf575..71bfee12 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/Utils.kt +++ b/app/src/main/java/com/simplemobiletools/filemanager/Utils.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.os.Build import android.support.v4.content.ContextCompat import android.support.v4.provider.DocumentFile +import com.simplemobiletools.filemanager.extensions.isPathOnSD import com.simplemobiletools.filepicker.extensions.getSDCardPath import com.simplemobiletools.filepicker.extensions.toast import java.util.regex.Pattern @@ -38,7 +39,7 @@ class Utils { fun needsStupidWritePermissions(context: Context, path: String) = isPathOnSD(context, path) && isKitkat() && !context.getSDCardPath().isEmpty() fun isPathOnSD(context: Context, path: String): Boolean { - return path.startsWith(context.getSDCardPath()) + return context.isPathOnSD(path) } fun isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT 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 fc0280cb..706b0a0e 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt +++ b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt @@ -6,15 +6,19 @@ import android.support.v4.util.Pair import android.util.Log import com.simplemobiletools.filemanager.Utils import com.simplemobiletools.filemanager.extensions.rescanItem +import com.simplemobiletools.filemanager.fragments.ItemsFragment import java.io.* import java.lang.ref.WeakReference +import java.util.* -class CopyTask(listener: CopyTask.CopyListener, 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 mMovedFiles: ArrayList init { mListener = WeakReference(listener) + mMovedFiles = arrayListOf() } override fun doInBackground(vararg params: Pair, File>): Boolean? { @@ -32,6 +36,16 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas return false } } + + if (deleteAfterCopy) { + for (file in mMovedFiles) { + if (Utils.needsStupidWritePermissions(context, file.absolutePath)) { + Utils.getFileDocument(context, file.absolutePath).delete() + } else { + file.delete() + } + } + } return true } @@ -68,6 +82,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas val out = context.contentResolver.openOutputStream(document.uri) copyStream(inputStream, out) context.rescanItem(destination) + mMovedFiles.add(source) } } else { copy(newFile, File(destination, child)) @@ -93,6 +108,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas copyStream(inputStream, out) context.rescanItem(destination) + mMovedFiles.add(source) } private fun copyStream(inputStream: InputStream, out: OutputStream?) { @@ -110,14 +126,14 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas val listener = mListener?.get() ?: return if (success) { - listener.copySucceeded() + listener.copySucceeded(if (deleteAfterCopy) ItemsFragment.ACTION_MOVE else ItemsFragment.ACTION_COPY) } else { listener.copyFailed() } } interface CopyListener { - fun copySucceeded() + fun copySucceeded(action: Int) 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 35f95d25..651138e6 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -68,6 +68,9 @@ public class ItemsFragment extends android.support.v4.app.Fragment private boolean mShowHidden; private int mSelectedItemsCnt; + public static int ACTION_COPY = 1; + public static int ACTION_MOVE = 2; + @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -492,9 +495,9 @@ public class ItemsFragment extends android.support.v4.app.Fragment } @Override - public void copySucceeded() { + public void copySucceeded(int action) { fillItems(); - Utils.Companion.showToast(getContext(), R.string.copying_success); + Utils.Companion.showToast(getContext(), action == ACTION_COPY ? R.string.copying_success : R.string.moving_success); } @Override 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 1ba47451..cf94734d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt @@ -6,16 +6,15 @@ import android.support.v4.util.Pair import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager -import android.widget.Toast import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.Utils import com.simplemobiletools.filemanager.activities.MainActivity import com.simplemobiletools.filemanager.asynctasks.CopyTask +import com.simplemobiletools.filemanager.extensions.isPathOnSD import com.simplemobiletools.filemanager.extensions.rescanItem import com.simplemobiletools.filemanager.extensions.toast import com.simplemobiletools.filepicker.dialogs.FilePickerDialog -import com.simplemobiletools.filepicker.extensions.getBasePath import com.simplemobiletools.filepicker.extensions.humanizePath import kotlinx.android.synthetic.main.copy_item.view.* import java.io.File @@ -88,23 +87,23 @@ class CopyDialog(val activity: Activity, val files: List, val copyListener 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) + CopyTask(copyListener, context, false).execute(pair) dismiss() } else { - if (sourcePath.getBasePath(context) == destinationPath.getBasePath(context)) { + 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 { for (f in files) { val destination = File(destinationDir, f.name) - f.renameTo(destination) context.rescanItem(destination) } + context.toast(R.string.moving_success) dismiss() listener.onSuccess() - } else { - context.toast(R.string.copying_no_delete, Toast.LENGTH_LONG) - val pair = Pair, File>(files, destinationDir) - CopyTask(copyListener, context).execute(pair) - dismiss() } } }) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt index 833a50f5..dc257a1a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/context.kt @@ -3,9 +3,10 @@ package com.simplemobiletools.filemanager.extensions import android.content.Context import android.media.MediaScannerConnection import android.widget.Toast +import com.simplemobiletools.filepicker.extensions.getSDCardPath import java.io.File -fun Context.toast(id: Int, length: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, resources.getString(id), length).show() +fun Context.toast(id: Int) = Toast.makeText(this, resources.getString(id), Toast.LENGTH_SHORT).show() fun Context.rescanItem(item: File) { if (item.isDirectory) { @@ -16,3 +17,5 @@ fun Context.rescanItem(item: File) { MediaScannerConnection.scanFile(this, arrayOf(item.absolutePath), null, null) } + +fun Context.isPathOnSD(path: String) = path.startsWith(getSDCardPath()) diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 4ebc1b59..3552156e 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -33,7 +33,8 @@ Kopiere Files copied successfully Konnte die Datei nicht kopieren - Copying, please delete source files manually + Moving + Files moved successfully A file with that name already exists OK Abbrechen diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index a780890e..a5a0c104 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -33,7 +33,8 @@ Copia in corso Files copied successfully Impossibile copiare i file - Copying, please delete source files manually + Moving + Files moved successfully A file with that name already exists OK Annulla diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 02b1d0be..597331e9 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -33,7 +33,8 @@ コピー中 Files copied successfully ファイルをコピーできませんでした - Copying, please delete source files manually + Moving + Files moved successfully A file with that name already exists OK Cancel diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index f59a0dd9..16a1fc08 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -33,7 +33,8 @@ A copiar Files copied successfully Não foi possível copiar os ficheiros - A copiar, apague os ficheiros originais manualmente + Moving + Files moved successfully Já existe um ficheiro com este nome/string> OK Cancelar diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index 277e5bfb..779c13f3 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -33,7 +33,8 @@ Kopierar Files copied successfully Kunde inte kopiera filen - Copying, please delete source files manually + Moving + Files moved successfully A file with that name already exists OK Cancel diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index eb44587f..948893f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -32,8 +32,9 @@ Source and destination cannot be the same Copying Files copied successfully - An error occurred during the copy - Copying, please delete source files manually + An error occurred + Moving + Files moved successfully A file with that name already exists OK Cancel