update the copy task

This commit is contained in:
tibbi 2016-11-12 17:52:35 +01:00
parent d4855573c9
commit 36aaf03541
9 changed files with 73 additions and 29 deletions

View file

@ -7,33 +7,51 @@ import android.util.Log
import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.scanFile import com.simplemobiletools.filepicker.extensions.scanFile
import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.Config
import java.io.* import java.io.*
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.*
class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : AsyncTask<Pair<List<File>, File>, Void, Boolean>() { class CopyTask(listener: CopyTask.CopyListener, val context: Context, val deleteAfterCopy: Boolean) : AsyncTask<Pair<ArrayList<File>, File>, Void, Boolean>() {
private val TAG = CopyTask::class.java.simpleName private val TAG = CopyTask::class.java.simpleName
private var mListener: WeakReference<CopyDoneListener>? = null private var mListener: WeakReference<CopyListener>? = null
private var destinationDir: File? = null private var mMovedFiles: ArrayList<File>
private var mConfig: Config private var mConfig: Config
init { init {
mListener = WeakReference(listener) mListener = WeakReference(listener)
mMovedFiles = arrayListOf()
mConfig = Config.newInstance(context) mConfig = Config.newInstance(context)
} }
override fun doInBackground(vararg params: Pair<List<File>, File>): Boolean? { override fun doInBackground(vararg params: Pair<ArrayList<File>, File>): Boolean? {
val pair = params[0] val pair = params[0]
val files = pair.first val files = pair.first
for (file in files) { for (file in files) {
try { try {
destinationDir = File(pair.second, file.name) val curFile = File(pair.second, file.name)
copy(file, destinationDir!!) if (curFile.exists())
continue
copy(file, curFile)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "copy " + e) Log.e(TAG, "copy $e")
return false 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 return true
} }
@ -52,7 +70,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
val document = context.getFileDocument(destination.absolutePath, mConfig.treeUri) val document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document.createDirectory(destination.name) document.createDirectory(destination.name)
} else if (!destination.mkdirs()) { } 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) val out = context.contentResolver.openOutputStream(document.uri)
copyStream(inputStream, out) copyStream(inputStream, out)
context.scanFile(destination) {} context.scanFile(destination) {}
mMovedFiles.add(source)
} }
} else { } else {
copy(newFile, File(destination, child)) 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) { private fun copyFile(source: File, destination: File) {
val directory = destination.parentFile val directory = destination.parentFile
if (!directory.exists() && !directory.mkdirs()) { 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) val inputStream = FileInputStream(source)
@ -88,6 +107,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
if (context.needsStupidWritePermissions(destination.absolutePath)) { if (context.needsStupidWritePermissions(destination.absolutePath)) {
var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri) var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document = document.createFile("", destination.name) document = document.createFile("", destination.name)
out = context.contentResolver.openOutputStream(document.uri) out = context.contentResolver.openOutputStream(document.uri)
} else { } else {
out = FileOutputStream(destination) out = FileOutputStream(destination)
@ -95,6 +115,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
copyStream(inputStream, out) copyStream(inputStream, out)
context.scanFile(destination) {} context.scanFile(destination) {}
mMovedFiles.add(source)
} }
private fun copyStream(inputStream: InputStream, out: OutputStream?) { 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 val listener = mListener?.get() ?: return
if (success) { if (success) {
listener.copySucceeded(destinationDir!!) listener.copySucceeded(deleteAfterCopy)
} else { } else {
listener.copyFailed() listener.copyFailed()
} }
} }
interface CopyDoneListener { interface CopyListener {
fun copySucceeded(destinationDir: File) fun copySucceeded(deleted: Boolean)
fun copyFailed() fun copyFailed()
} }

View file

@ -5,14 +5,16 @@ import android.support.v7.app.AlertDialog
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.filepicker.extensions.humanizePath import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.isPathOnSD
import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.asynctasks.CopyTask import com.simplemobiletools.gallery.asynctasks.CopyTask
import kotlinx.android.synthetic.main.dialog_copy_move.view.* import kotlinx.android.synthetic.main.dialog_copy_move.view.*
import java.io.File import java.io.File
import java.util.*
class CopyDialog(val activity: SimpleActivity, val files: List<File>, val copyListener: CopyTask.CopyDoneListener, val listener: OnCopyListener) { class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) {
init { init {
val context = activity val context = activity
@ -70,11 +72,18 @@ class CopyDialog(val activity: SimpleActivity, val files: List<File>, val copyLi
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying) context.toast(R.string.copying)
val pair = Pair<List<File>, File>(files, destinationDir) val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyTask(copyListener, context).execute(pair) CopyTask(copyListener, context, false).execute(pair)
dismiss() dismiss()
} else { } else {
if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) {
context.toast(R.string.moving)
val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyTask(copyListener, context, true).execute(pair)
dismiss()
} else {
}
} }
}) })
} }

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">Bitte wähle ein Ziel</string> <string name="please_select_destination">Bitte wähle ein Ziel</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string> <string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Konnte die Datei nicht kopieren</string> <string name="copy_failed">Konnte die Datei nicht kopieren</string>
<string name="copying">Kopiere</string> <string name="copying">Kopiere&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">Please select a destination</string> <string name="please_select_destination">Please select a destination</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string> <string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Could not copy the files</string> <string name="copy_failed">Could not copy the files</string>
<string name="copying">Copying</string> <string name="copying">Copying&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">Seleziona una destinazione</string> <string name="please_select_destination">Seleziona una destinazione</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string> <string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Impossibile copiare i file</string> <string name="copy_failed">Impossibile copiare i file</string>
<string name="copying">Copia in corso</string> <string name="copying">Copia in corso&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">宛先を選択してください</string> <string name="please_select_destination">宛先を選択してください</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string> <string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">ファイルをコピーできませんでした</string> <string name="copy_failed">ファイルをコピーできませんでした</string>
<string name="copying">コピー中</string> <string name="copying">コピー中&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">Por favor selecione um destino</string> <string name="please_select_destination">Por favor selecione um destino</string>
<string name="source_and_destination_same">A origem e o destino não podem ser iguais</string> <string name="source_and_destination_same">A origem e o destino não podem ser iguais</string>
<string name="copy_failed">Não foi possível copiar os ficheiros</string> <string name="copy_failed">Não foi possível copiar os ficheiros</string>
<string name="copying">A copiar</string> <string name="copying">A copiar&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">Já existe um ficheiro com este nome</string> <string name="already_exists">Já existe um ficheiro com este nome</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">Please select a destination</string> <string name="please_select_destination">Please select a destination</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string> <string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Kunde inte kopiera filen</string> <string name="copy_failed">Kunde inte kopiera filen</string>
<string name="copying">Kopierar</string> <string name="copying">Kopierar&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">Please select a destination</string> <string name="please_select_destination">Please select a destination</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string> <string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Could not copy the files</string> <string name="copy_failed">Could not copy the files</string>
<string name="copying">Copying</string> <string name="copying">Copying&#8230;</string>
<string name="copying_success">Files copied successfully</string> <string name="copying_success">Files copied successfully</string>
<string name="copying_failed">An error occurred during the copy</string> <string name="copying_failed">An error occurred</string>
<string name="moving">Moving&#8230;</string>
<string name="moving_success">Files moved successfully</string>
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">