a few more Move adjustments

This commit is contained in:
tibbi 2016-11-11 21:09:31 +01:00
parent 7b4a6b401d
commit 5569bd06b5
11 changed files with 52 additions and 24 deletions

View File

@ -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

View File

@ -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<Pair<List<File>, File>, Void, Boolean>() {
class CopyTask(listener: CopyTask.CopyListener, val context: Context, val deleteAfterCopy: Boolean) : AsyncTask<Pair<List<File>, File>, Void, Boolean>() {
private val TAG = CopyTask::class.java.simpleName
private var mListener: WeakReference<CopyListener>? = null
private var mMovedFiles: ArrayList<File>
init {
mListener = WeakReference(listener)
mMovedFiles = arrayListOf()
}
override fun doInBackground(vararg params: Pair<List<File>, 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()
}

View File

@ -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

View File

@ -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<File>, val copyListener
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying)
val pair = Pair<List<File>, File>(files, destinationDir)
CopyTask(copyListener, context).execute(pair)
CopyTask(copyListener, context, false).execute(pair)
dismiss()
} else {
if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) {
context.toast(R.string.moving)
val pair = Pair<List<File>, File>(files, destinationDir)
CopyTask(copyListener, context, true).execute(pair)
dismiss()
} else {
if (sourcePath.getBasePath(context) == destinationPath.getBasePath(context)) {
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<List<File>, File>(files, destinationDir)
CopyTask(copyListener, context).execute(pair)
dismiss()
}
}
})

View File

@ -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())

View File

@ -33,7 +33,8 @@
<string name="copying">Kopiere</string>
<string name="copying_success">Files copied successfully</string>
<string name="copying_failed">Konnte die Datei nicht kopieren</string>
<string name="copying_no_delete">Copying, please delete source files manually</string>
<string name="moving">Moving</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string>
<string name="ok">OK</string>
<string name="cancel">Abbrechen</string>

View File

@ -33,7 +33,8 @@
<string name="copying">Copia in corso</string>
<string name="copying_success">Files copied successfully</string>
<string name="copying_failed">Impossibile copiare i file</string>
<string name="copying_no_delete">Copying, please delete source files manually</string>
<string name="moving">Moving</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string>
<string name="ok">OK</string>
<string name="cancel">Annulla</string>

View File

@ -33,7 +33,8 @@
<string name="copying">コピー中</string>
<string name="copying_success">Files copied successfully</string>
<string name="copying_failed">ファイルをコピーできませんでした</string>
<string name="copying_no_delete">Copying, please delete source files manually</string>
<string name="moving">Moving</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>

View File

@ -33,7 +33,8 @@
<string name="copying">A copiar</string>
<string name="copying_success">Files copied successfully</string>
<string name="copying_failed">Não foi possível copiar os ficheiros</string>
<string name="copying_no_delete">A copiar, apague os ficheiros originais manualmente</string>
<string name="moving">Moving</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">Já existe um ficheiro com este nome/string>
<string name="ok">OK</string>
<string name="cancel">Cancelar</string>

View File

@ -33,7 +33,8 @@
<string name="copying">Kopierar</string>
<string name="copying_success">Files copied successfully</string>
<string name="copying_failed">Kunde inte kopiera filen</string>
<string name="copying_no_delete">Copying, please delete source files manually</string>
<string name="moving">Moving</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>

View File

@ -32,8 +32,9 @@
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copying">Copying</string>
<string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string>
<string name="copying_no_delete">Copying, please delete source files manually</string>
<string name="copying_failed">An error occurred</string>
<string name="moving">Moving</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>