couple copy adjustments
This commit is contained in:
parent
08338f3d7a
commit
2222c88650
|
@ -5,13 +5,13 @@ import android.os.AsyncTask
|
||||||
import android.support.v4.util.Pair
|
import android.support.v4.util.Pair
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.simplemobiletools.filemanager.Utils
|
import com.simplemobiletools.filemanager.Utils
|
||||||
|
import com.simplemobiletools.filemanager.extensions.rescanItem
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
|
|
||||||
class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTask<Pair<List<File>, File>, Void, Boolean>() {
|
class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTask<Pair<List<File>, File>, Void, Boolean>() {
|
||||||
private val TAG = CopyTask::class.java.simpleName
|
private val TAG = CopyTask::class.java.simpleName
|
||||||
private var mListener: WeakReference<CopyListener>? = null
|
private var mListener: WeakReference<CopyListener>? = null
|
||||||
private var destinationDir: File? = null
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
mListener = WeakReference(listener)
|
mListener = WeakReference(listener)
|
||||||
|
@ -22,10 +22,13 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +50,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
|
||||||
val document = Utils.getFileDocument(context, destination.absolutePath)
|
val document = Utils.getFileDocument(context, destination.absolutePath)
|
||||||
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}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +67,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
|
||||||
val inputStream = FileInputStream(newFile)
|
val inputStream = FileInputStream(newFile)
|
||||||
val out = context.contentResolver.openOutputStream(document.uri)
|
val out = context.contentResolver.openOutputStream(document.uri)
|
||||||
copyStream(inputStream, out)
|
copyStream(inputStream, out)
|
||||||
|
context.rescanItem(destination)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
copy(newFile, File(destination, child))
|
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) {
|
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 +92,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context) : AsyncTas
|
||||||
}
|
}
|
||||||
|
|
||||||
copyStream(inputStream, out)
|
copyStream(inputStream, out)
|
||||||
|
context.rescanItem(destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun copyStream(inputStream: InputStream, out: OutputStream?) {
|
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
|
val listener = mListener?.get() ?: return
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
listener.copySucceeded(destinationDir!!)
|
listener.copySucceeded()
|
||||||
} else {
|
} else {
|
||||||
listener.copyFailed()
|
listener.copyFailed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CopyListener {
|
interface CopyListener {
|
||||||
fun copySucceeded(destinationDir: File)
|
fun copySucceeded()
|
||||||
|
|
||||||
fun copyFailed()
|
fun copyFailed()
|
||||||
}
|
}
|
||||||
|
|
|
@ -492,8 +492,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void copySucceeded(File file) {
|
public void copySucceeded() {
|
||||||
rescanItem(file);
|
|
||||||
fillItems();
|
fillItems();
|
||||||
Utils.Companion.showToast(getContext(), R.string.copying_success);
|
Utils.Companion.showToast(getContext(), R.string.copying_success);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,6 @@ import com.simplemobiletools.filemanager.activities.MainActivity
|
||||||
import com.simplemobiletools.filemanager.asynctasks.CopyTask
|
import com.simplemobiletools.filemanager.asynctasks.CopyTask
|
||||||
import com.simplemobiletools.filemanager.extensions.rescanItem
|
import com.simplemobiletools.filemanager.extensions.rescanItem
|
||||||
import com.simplemobiletools.filemanager.extensions.toast
|
import com.simplemobiletools.filemanager.extensions.toast
|
||||||
import com.simplemobiletools.filemanager.extensions.value
|
|
||||||
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
|
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
|
||||||
import com.simplemobiletools.filepicker.extensions.humanizePath
|
import com.simplemobiletools.filepicker.extensions.humanizePath
|
||||||
import kotlinx.android.synthetic.main.copy_item.view.*
|
import kotlinx.android.synthetic.main.copy_item.view.*
|
||||||
|
@ -25,9 +24,9 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
|
||||||
init {
|
init {
|
||||||
val context = activity
|
val context = activity
|
||||||
val view = LayoutInflater.from(context).inflate(R.layout.copy_item, null)
|
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 = ""
|
var destinationPath = ""
|
||||||
view.source.text = "${context.humanizePath(path)}/"
|
view.source.text = "${context.humanizePath(sourcePath)}/"
|
||||||
|
|
||||||
view.destination.setOnClickListener {
|
view.destination.setOnClickListener {
|
||||||
val config = Config.newInstance(context)
|
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)
|
CopyTask(copyListener, context).execute(pair)
|
||||||
dismiss()
|
dismiss()
|
||||||
} else {
|
} 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) {
|
for (f in files) {
|
||||||
val destination = File(destinationDir, f.name)
|
val destination = File(destinationDir, f.name)
|
||||||
f.renameTo(destination)
|
f.renameTo(destination)
|
||||||
|
|
Loading…
Reference in New Issue