update the copy task related things

This commit is contained in:
tibbi 2016-11-12 19:10:05 +01:00
parent 807add5579
commit a5deb81e58
5 changed files with 37 additions and 192 deletions

View File

@ -25,12 +25,12 @@ import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.DirectoryAdapter;
import com.simplemobiletools.gallery.asynctasks.CopyTask;
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask;
import com.simplemobiletools.gallery.dialogs.ChangeSorting;
import com.simplemobiletools.gallery.dialogs.CopyDialog;
@ -52,7 +52,7 @@ import butterknife.ButterKnife;
public class MainActivity extends SimpleActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener, GetDirectoriesAsynctask.GetDirectoriesListener,
CopyTask.CopyDoneListener {
CopyMoveTask.CopyListener {
@BindView(R.id.directories_grid) GridView mGridView;
@BindView(R.id.directories_holder) SwipeRefreshLayout mSwipeRefreshLayout;
@ -320,7 +320,7 @@ public class MainActivity extends SimpleActivity
}
private void displayCopyDialog() {
final List<File> files = new ArrayList<>();
final ArrayList<File> files = new ArrayList<>();
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
for (int i = 0; i < cnt; i++) {
@ -331,12 +331,7 @@ public class MainActivity extends SimpleActivity
}
}
new CopyDialog(this, files, this, new CopyDialog.OnCopyListener() {
@Override
public void onSuccess() {
}
});
new CopyDialog(this, files, this);
}
private boolean isPickImageIntent(Intent intent) {
@ -591,9 +586,9 @@ public class MainActivity extends SimpleActivity
}
@Override
public void copySucceeded(@NotNull File destinationDir) {
public void copySucceeded(boolean deleted) {
getDirectories();
Utils.Companion.showToast(getApplicationContext(), R.string.copying_success);
Utils.Companion.showToast(getApplicationContext(), deleted ? R.string.moving_success : R.string.copying_success);
}
@Override

View File

@ -29,18 +29,16 @@ import android.widget.GridView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MediaAdapter;
import com.simplemobiletools.gallery.asynctasks.CopyTask;
import com.simplemobiletools.gallery.dialogs.ChangeSorting;
import com.simplemobiletools.gallery.dialogs.CopyDialog;
import com.simplemobiletools.gallery.models.Medium;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@ -53,7 +51,7 @@ import butterknife.ButterKnife;
public class MediaActivity extends SimpleActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener, CopyTask.CopyDoneListener {
SwipeRefreshLayout.OnRefreshListener, ChangeSorting.ChangeDialogListener, CopyMoveTask.CopyListener {
private static final String TAG = MediaActivity.class.getSimpleName();
@BindView(R.id.media_grid) GridView mGridView;
@BindView(R.id.media_holder) SwipeRefreshLayout mSwipeRefreshLayout;
@ -406,7 +404,7 @@ public class MediaActivity extends SimpleActivity
}
private void displayCopyDialog() {
final List<File> files = new ArrayList<>();
final ArrayList<File> files = new ArrayList<>();
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
@ -417,12 +415,7 @@ public class MediaActivity extends SimpleActivity
}
}
new CopyDialog(this, files, this, new CopyDialog.OnCopyListener() {
@Override
public void onSuccess() {
}
});
new CopyDialog(this, files, this);
}
@Override
@ -540,8 +533,8 @@ public class MediaActivity extends SimpleActivity
}
@Override
public void copySucceeded(@NotNull File destinationDir) {
Utils.Companion.showToast(getApplicationContext(), R.string.copying_success);
public void copySucceeded(boolean deleted) {
Utils.Companion.showToast(getApplicationContext(), deleted ? R.string.moving_success : R.string.copying_success);
}
@Override

View File

@ -17,13 +17,13 @@ import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.MyViewPager;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
import com.simplemobiletools.gallery.asynctasks.CopyTask;
import com.simplemobiletools.gallery.dialogs.CopyDialog;
import com.simplemobiletools.gallery.dialogs.RenameFileDialog;
import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
@ -43,7 +43,7 @@ import butterknife.OnClick;
public class ViewPagerActivity extends SimpleActivity
implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPager.OnTouchListener,
ViewPagerFragment.FragmentClickListener, CopyTask.CopyDoneListener {
ViewPagerFragment.FragmentClickListener, CopyMoveTask.CopyListener {
@BindView(R.id.undo_delete) View mUndoBtn;
@BindView(R.id.view_pager) MyViewPager mPager;
@ -188,15 +188,9 @@ public class ViewPagerActivity extends SimpleActivity
private void displayCopyDialog() {
final File file = getCurrentFile();
final List<File> files = new ArrayList<>();
final ArrayList<File> files = new ArrayList<>();
files.add(file);
new CopyDialog(this, files, this, new CopyDialog.OnCopyListener() {
@Override
public void onSuccess() {
}
});
new CopyDialog(this, files, this);
}
private void openEditor() {
@ -503,8 +497,8 @@ public class ViewPagerActivity extends SimpleActivity
}
@Override
public void copySucceeded(@NotNull File destinationDir) {
Utils.Companion.showToast(getApplicationContext(), R.string.copying_success);
public void copySucceeded(boolean deleted) {
Utils.Companion.showToast(getApplicationContext(), deleted ? R.string.moving_success : R.string.copying_success);
}
@Override

View File

@ -1,147 +0,0 @@
package com.simplemobiletools.gallery.asynctasks
import android.content.Context
import android.os.AsyncTask
import android.support.v4.util.Pair
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.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

@ -4,17 +4,19 @@ import android.support.v4.util.Pair
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.isPathOnSD
import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.Config
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: 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
@ -47,7 +49,7 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val c
return@setOnClickListener
}
if (view.source.text.trimEnd('/') == view.destination.text.trimEnd('/')) {
if (view.source.text.trimEnd('/') == destinationPath.trimEnd('/')) {
context.toast(R.string.source_and_destination_same)
return@setOnClickListener
}
@ -70,26 +72,34 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val c
return@setOnClickListener
}
val config = Config.newInstance(context)
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)
updatedFiles.add(destination)
}
context.scanFiles(updatedFiles) {}
context.toast(R.string.moving_success)
dismiss()
copyMoveListener.copySucceeded(true)
}
}
})
}
}
interface OnCopyListener {
fun onSuccess()
}
}