add a couple things related to file copying

This commit is contained in:
tibbi
2016-11-08 21:49:54 +01:00
parent fbe1409862
commit c6598528fd
18 changed files with 410 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
@ -27,7 +28,7 @@ class PickAlbumActivity : SimpleActivity(), AdapterView.OnItemClickListener, Get
if (Utils.hasStoragePermission(applicationContext)) {
getDirectories()
} else {
Utils.showToast(applicationContext, R.string.no_permissions)
toast(R.string.no_permissions)
}
}

View File

@ -0,0 +1,119 @@
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.gallery.Utils
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)
}
override fun doInBackground(vararg params: Pair<List<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!!)
} catch (e: Exception) {
Log.e(TAG, "copy " + e)
return false
}
}
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 (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
val document = Utils.getFileDocument(context, destination.absolutePath)
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 (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
if (newFile.isDirectory) {
copyDirectory(newFile, File(destination, child))
} else {
var document = Utils.getFileDocument(context, destination.absolutePath)
document = document.createFile("", child)
val inputStream = FileInputStream(newFile)
val out = context.contentResolver.openOutputStream(document.uri)
copyStream(inputStream, out)
}
} 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 (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
var document = Utils.getFileDocument(context, destination.absolutePath)
document = document.createFile("", destination.name)
out = context.contentResolver.openOutputStream(document.uri)
} else {
out = FileOutputStream(destination)
}
copyStream(inputStream, out)
}
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(destinationDir!!)
} else {
listener.copyFailed()
}
}
interface CopyListener {
fun copySucceeded(destinationDir: File)
fun copyFailed()
}
}

View File

@ -0,0 +1,98 @@
package com.simplemobiletools.gallery.dialogs
import android.app.Activity
import android.support.v4.util.Pair
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.asynctasks.CopyTask
import com.simplemobiletools.gallery.extensions.scanFile
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File
import java.util.*
class CopyDialog(val activity: Activity, val files: List<File>, val path: String, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) {
init {
val context = activity
val view = LayoutInflater.from(context).inflate(R.layout.copy_item, null)
view.source.text = "${path.trimEnd('/')}/"
view.destination.setOnClickListener {
}
AlertDialog.Builder(context)
.setTitle(context.resources.getString(if (files.size == 1) R.string.copy_item else R.string.copy_items))
.setView(view)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val destinationPath = view.destination.value
if (destinationPath == context.resources.getString(R.string.select_destination)) {
context.toast(R.string.please_select_destination)
return@setOnClickListener
}
if (view.source.text.trimEnd('/') == destinationPath.trimEnd('/')) {
context.toast(R.string.source_and_destination_same)
return@setOnClickListener
}
val destinationDir = File(destinationPath)
if (!destinationDir.exists()) {
context.toast(R.string.invalid_destination)
return@setOnClickListener
}
if (files.size == 1) {
val newFile = File(files[0].path)
if (File(destinationPath, newFile.name).exists()) {
context.toast(R.string.already_exists)
return@setOnClickListener
}
}
if (Utils.isShowingWritePermissions(context, destinationDir)) {
return@setOnClickListener
}
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)
dismiss()
} else {
if (Utils.isPathOnSD(context, view.source.value) && Utils.isPathOnSD(context, destinationPath)) {
val paths = ArrayList<String>()
for (f in files) {
val destination = File(destinationDir, f.name)
f.renameTo(destination)
paths.add(destination.absolutePath)
}
context.scanFile(paths.toTypedArray())
dismiss()
listener.onSuccess()
} else {
val pair = Pair<List<File>, File>(files, destinationDir)
CopyTask(copyListener, context).execute(pair)
dismiss()
}
}
})
}
}
interface OnCopyListener {
fun onSuccess()
}
}

View File

@ -1,8 +1,11 @@
package com.simplemobiletools.gallery.extensions
import android.content.Context
import android.media.MediaScannerConnection
import android.widget.Toast
fun Context.toast(msg: String, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, msg, duration).show()
fun Context.toast(msgId: Int, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, resources.getString(msgId), duration).show()
fun Context.scanFile(paths: Array<String>) = MediaScannerConnection.scanFile(this, paths, null, null)

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.gallery.extensions
import android.widget.TextView
val TextView.value: String get() = this.text.toString().trim()