couple copy adjustments

This commit is contained in:
tibbi 2016-11-11 19:15:24 +01:00
parent 08338f3d7a
commit 2222c88650
3 changed files with 17 additions and 14 deletions

View File

@ -5,13 +5,13 @@ import android.os.AsyncTask
import android.support.v4.util.Pair
import android.util.Log
import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.extensions.rescanItem
import java.io.*
import java.lang.ref.WeakReference
class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTask<Pair<List<File>, File>, Void, Boolean>() {
private val TAG = CopyTask::class.java.simpleName
private var mListener: WeakReference<CopyListener>? = null
private var destinationDir: File? = null
init {
mListener = WeakReference(listener)
@ -22,10 +22,13 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
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
}
}
@ -47,7 +50,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
val document = Utils.getFileDocument(context, destination.absolutePath)
document.createDirectory(destination.name)
} else if (!destination.mkdirs()) {
throw IOException("Could not create dir " + destination.absolutePath)
throw IOException("Could not create dir ${destination.absolutePath}")
}
}
@ -64,6 +67,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
val inputStream = FileInputStream(newFile)
val out = context.contentResolver.openOutputStream(document.uri)
copyStream(inputStream, out)
context.rescanItem(destination)
}
} else {
copy(newFile, File(destination, child))
@ -74,7 +78,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
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 +92,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
}
copyStream(inputStream, out)
context.rescanItem(destination)
}
private fun copyStream(inputStream: InputStream, out: OutputStream?) {
@ -105,14 +110,14 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
val listener = mListener?.get() ?: return
if (success) {
listener.copySucceeded(destinationDir!!)
listener.copySucceeded()
} else {
listener.copyFailed()
}
}
interface CopyListener {
fun copySucceeded(destinationDir: File)
fun copySucceeded()
fun copyFailed()
}

View File

@ -492,8 +492,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
}
@Override
public void copySucceeded(File file) {
rescanItem(file);
public void copySucceeded() {
fillItems();
Utils.Companion.showToast(getContext(), R.string.copying_success);
}

View File

@ -14,7 +14,6 @@ import com.simplemobiletools.filemanager.activities.MainActivity
import com.simplemobiletools.filemanager.asynctasks.CopyTask
import com.simplemobiletools.filemanager.extensions.rescanItem
import com.simplemobiletools.filemanager.extensions.toast
import com.simplemobiletools.filemanager.extensions.value
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
import com.simplemobiletools.filepicker.extensions.humanizePath
import kotlinx.android.synthetic.main.copy_item.view.*
@ -25,9 +24,9 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
init {
val context = activity
val view = LayoutInflater.from(context).inflate(R.layout.copy_item, null)
val path = files[0].parent.trimEnd('/')
val sourcePath = files[0].parent.trimEnd('/')
var destinationPath = ""
view.source.text = "${context.humanizePath(path)}/"
view.source.text = "${context.humanizePath(sourcePath)}/"
view.destination.setOnClickListener {
val config = Config.newInstance(context)
@ -91,7 +90,7 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
CopyTask(copyListener, context).execute(pair)
dismiss()
} else {
if (Utils.isPathOnSD(context, view.source.value) && Utils.isPathOnSD(context, destinationPath)) {
if (Utils.isPathOnSD(context, sourcePath) && Utils.isPathOnSD(context, destinationPath)) {
for (f in files) {
val destination = File(destinationDir, f.name)
f.renameTo(destination)