use the copytask from filepicker library

This commit is contained in:
tibbi 2016-11-12 19:03:20 +01:00
parent e960f496ed
commit aad62d56ae
3 changed files with 11 additions and 159 deletions

View File

@ -1,147 +0,0 @@
package com.simplemobiletools.filemanager.asynctasks
import android.content.Context
import android.os.AsyncTask
import android.support.v4.util.Pair
import android.util.Log
import com.simplemobiletools.filemanager.Config
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 java.io.*
import java.lang.ref.WeakReference
import java.util.*
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<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<ArrayList<File>, File>): Boolean? {
val pair = params[0]
val files = pair.first
for (file in files) {
try {
val curFile = File(pair.second, file.name)
if (curFile.exists())
continue
copy(file, curFile)
} catch (e: Exception) {
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
}
@Throws(Exception::class)
private fun copy(source: File, destination: File) {
if (source.isDirectory) {
copyDirectory(source, destination)
} else {
copyFile(source, destination)
}
}
private fun copyDirectory(source: File, destination: File) {
if (!destination.exists()) {
if (context.needsStupidWritePermissions(destination.absolutePath)) {
val document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document.createDirectory(destination.name)
} else if (!destination.mkdirs()) {
throw IOException("Could not create dir ${destination.absolutePath}")
}
}
val children = source.list()
for (child in children) {
val newFile = File(source, child)
if (context.needsStupidWritePermissions(destination.absolutePath)) {
if (newFile.isDirectory) {
copyDirectory(newFile, File(destination, child))
} else {
var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document = document.createFile("", child)
val inputStream = FileInputStream(newFile)
val out = context.contentResolver.openOutputStream(document.uri)
copyStream(inputStream, out)
context.scanFile(destination) {}
mMovedFiles.add(source)
}
} else {
copy(newFile, File(destination, child))
}
}
}
private fun copyFile(source: File, destination: File) {
val directory = destination.parentFile
if (!directory.exists() && !directory.mkdirs()) {
throw IOException("Could not create dir ${directory.absolutePath}")
}
val inputStream = FileInputStream(source)
val out: OutputStream?
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)
}
copyStream(inputStream, out)
context.scanFile(destination) {}
mMovedFiles.add(source)
}
private fun copyStream(inputStream: InputStream, out: OutputStream?) {
val buf = ByteArray(1024)
var len: Int
while (true) {
len = inputStream.read(buf)
if (len <= 0)
break
out?.write(buf, 0, len)
}
}
override fun onPostExecute(success: Boolean) {
val listener = mListener?.get() ?: return
if (success) {
listener.copySucceeded(deleteAfterCopy)
} else {
listener.copyFailed()
}
}
interface CopyListener {
fun copySucceeded(deleted: Boolean)
fun copyFailed()
}
}

View File

@ -30,10 +30,10 @@ import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.activities.SimpleActivity;
import com.simplemobiletools.filemanager.adapters.ItemsAdapter;
import com.simplemobiletools.filemanager.asynctasks.CopyTask;
import com.simplemobiletools.filemanager.dialogs.CopyDialog;
import com.simplemobiletools.filemanager.dialogs.CreateNewItemDialog;
import com.simplemobiletools.filemanager.dialogs.RenameItemDialog;
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask;
import com.simplemobiletools.filepicker.models.FileDirItem;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
@ -51,7 +51,7 @@ import butterknife.OnClick;
public class ItemsFragment extends android.support.v4.app.Fragment
implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, ListView.MultiChoiceModeListener,
ListView.OnTouchListener, CopyTask.CopyListener {
ListView.OnTouchListener, CopyMoveTask.CopyListener {
@BindView(R.id.items_list) ListView mListView;
@BindView(R.id.items_swipe_refresh) SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.items_holder) CoordinatorLayout mCoordinatorLayout;

View File

@ -7,7 +7,7 @@ import android.view.WindowManager
import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.activities.SimpleActivity
import com.simplemobiletools.filemanager.asynctasks.CopyTask
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.isPathOnSD
@ -17,7 +17,7 @@ import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File
import java.util.*
class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) {
class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val copyMoveListener: CopyMoveTask.CopyListener) {
init {
val context = activity
@ -79,31 +79,30 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val c
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying)
val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyTask(copyListener, context, false).execute(pair)
CopyMoveTask(context, false, config.treeUri, copyMoveListener).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)
CopyMoveTask(context, true, config.treeUri, copyMoveListener).execute(pair)
dismiss()
} else {
val updatedFiles = ArrayList<File>(files.size * 2)
updatedFiles.addAll(files)
for (file in files) {
val destination = File(destinationDir, file.name)
file.renameTo(destination)
context.scanFiles(arrayListOf(file, destination)) {}
updatedFiles.add(destination)
}
context.scanFiles(updatedFiles) {}
context.toast(R.string.moving_success)
dismiss()
listener.onSuccess()
copyMoveListener.copySucceeded(true)
}
}
})
}
}
interface OnCopyListener {
fun onSuccess()
}
}