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.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<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 var mListener: WeakReference<CopyDoneListener>? = null
private var destinationDir: File? = null
private var mListener: WeakReference<CopyListener>? = null
private var mMovedFiles: ArrayList<File>
private var mConfig: Config
init {
mListener = WeakReference(listener)
mMovedFiles = arrayListOf()
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 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()
}

View file

@ -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<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 {
val context = activity
@ -70,12 +72,19 @@ class CopyDialog(val activity: SimpleActivity, val files: List<File>, val copyLi
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)
val pair = Pair<ArrayList<File>, 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<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="source_and_destination_same">Source and destination cannot be the same</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_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>
<plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<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="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_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>
<plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<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="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_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>
<plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<string name="please_select_destination">宛先を選択してください</string>
<string name="source_and_destination_same">Source and destination cannot be the same</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_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>
<plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<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="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_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>
<plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<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="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_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>
<plurals name="folders_deleted">

View file

@ -50,9 +50,11 @@
<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="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_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>
<plurals name="folders_deleted">