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

@ -21,6 +21,7 @@ import android.webkit.MimeTypeMap
import android.widget.Toast import android.widget.Toast
import com.simplemobiletools.filepicker.extensions.getSDCardPath import com.simplemobiletools.filepicker.extensions.getSDCardPath
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog import com.simplemobiletools.gallery.dialogs.WritePermissionDialog
import com.simplemobiletools.gallery.extensions.scanFile
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
@ -182,5 +183,9 @@ class Utils {
false false
} }
} }
fun scanFiles(context: Context, paths: Array<String>) {
context.scanFile(paths)
}
} }
} }

View file

@ -167,7 +167,8 @@ public class MediaActivity extends SimpleActivity
rescanDirectory(files[i]); rescanDirectory(files[i]);
} }
} }
MediaScannerConnection.scanFile(getApplicationContext(), paths, null, null);
Utils.Companion.scanFiles(getApplicationContext(), paths);
} }
private void showSortingDialog() { private void showSortingDialog() {

View file

@ -151,6 +151,9 @@ public class ViewPagerActivity extends SimpleActivity
case R.id.menu_set_as_wallpaper: case R.id.menu_set_as_wallpaper:
setAsWallpaper(); setAsWallpaper();
return true; return true;
case R.id.menu_copy:
displayCopyDialog();
return true;
case R.id.menu_open_with: case R.id.menu_open_with:
openWith(); openWith();
return true; return true;
@ -181,6 +184,10 @@ public class ViewPagerActivity extends SimpleActivity
adapter.updateItems(mPos); adapter.updateItems(mPos);
} }
private void displayCopyDialog() {
}
private void openEditor() { private void openEditor() {
final Intent intent = new Intent(Intent.ACTION_EDIT); final Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setDataAndType(Uri.fromFile(getCurrentFile()), "image/*"); intent.setDataAndType(Uri.fromFile(getCurrentFile()), "image/*");

View file

@ -7,6 +7,7 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.util.* import java.util.*
@ -27,7 +28,7 @@ class PickAlbumActivity : SimpleActivity(), AdapterView.OnItemClickListener, Get
if (Utils.hasStoragePermission(applicationContext)) { if (Utils.hasStoragePermission(applicationContext)) {
getDirectories() getDirectories()
} else { } 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 package com.simplemobiletools.gallery.extensions
import android.content.Context import android.content.Context
import android.media.MediaScannerConnection
import android.widget.Toast import android.widget.Toast
fun Context.toast(msg: String, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, msg, duration).show() 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.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()

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin">
<TextView
android:id="@+id/source_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/source"
android:textSize="@dimen/details_text_size"/>
<TextView
android:id="@+id/source"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:layout_marginLeft="@dimen/activity_margin"
android:paddingRight="@dimen/small_padding"
android:paddingTop="@dimen/small_padding"
android:text="source"/>
<TextView
android:id="@+id/destination_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/destination"
android:textSize="@dimen/details_text_size"/>
<TextView
android:id="@+id/destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:layout_marginLeft="@dimen/activity_margin"
android:paddingBottom="@dimen/small_padding"
android:paddingRight="@dimen/small_padding"
android:paddingTop="@dimen/small_padding"
android:text="@string/select_destination"/>
<RadioGroup
android:id="@+id/dialog_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/dialog_radio_copy">
<RadioButton
android:id="@+id/dialog_radio_copy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_padding"
android:text="@string/copy"/>
<RadioButton
android:id="@+id/dialog_radio_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/move"/>
</RadioGroup>
</LinearLayout>

View file

@ -5,6 +5,10 @@
android:id="@+id/menu_set_as_wallpaper" android:id="@+id/menu_set_as_wallpaper"
android:title="@string/set_as_wallpaper" android:title="@string/set_as_wallpaper"
app:showAsAction="never"/> app:showAsAction="never"/>
<item
android:id="@+id/menu_copy"
android:title="@string/copy_move"
app:showAsAction="never"/>
<item <item
android:id="@+id/menu_open_with" android:id="@+id/menu_open_with"
android:title="@string/open_with" android:title="@string/open_with"

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string> <string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string> <string name="file_saved">File saved successfully</string>
<string name="copy">Kopieren</string>
<string name="move">Verschieben</string>
<string name="copy_move">Kopieren/Verschieben</string>
<string name="copy_item">Datei/Ordner kopieren</string>
<string name="copy_items">Dateien/Ordner kopieren</string>
<string name="source">Quelle</string>
<string name="destination">Ziel</string>
<string name="select_destination">Ziel auswählen</string>
<string name="invalid_destination">Konnte nicht an ausgewähltes Ziel schreiben</string>
<string name="please_select_destination">Bitte wähle ein Ziel</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Konnte die Datei nicht kopieren</string>
<string name="copying">Kopiere</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 Ordner gelöscht</item> <item quantity="one">1 Ordner gelöscht</item>

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Salvar como</string> <string name="save_as">Salvar como</string>
<string name="file_saved">Archivo guardado con éxito</string> <string name="file_saved">Archivo guardado con éxito</string>
<string name="copy">Copy</string>
<string name="move">Move</string>
<string name="copy_move">Copy/Move to</string>
<string name="copy_item">Copy item</string>
<string name="copy_items">Copy items</string>
<string name="source">Source</string>
<string name="destination">Destination</string>
<string name="select_destination">Select destination</string>
<string name="invalid_destination">Could not write to the selected destination</string>
<string name="please_select_destination">Please select a destination</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Could not copy the files</string>
<string name="copying">Copying</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 carpeta eliminada</item> <item quantity="one">1 carpeta eliminada</item>

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string> <string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string> <string name="file_saved">File saved successfully</string>
<string name="copy">Copia</string>
<string name="move">Sposta</string>
<string name="copy_move">Copia/Sposta</string>
<string name="copy_item">Copia elemento</string>
<string name="copy_items">Copia elementi</string>
<string name="source">Fonte</string>
<string name="destination">Destinazione</string>
<string name="select_destination">Seleziona destinazione</string>
<string name="invalid_destination">Impossibile scrivere nella destinazione selezionata</string>
<string name="please_select_destination">Seleziona una destinazione</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Impossibile copiare i file</string>
<string name="copying">Copia in corso</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 cartella eliminata</item> <item quantity="one">1 cartella eliminata</item>

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string> <string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string> <string name="file_saved">File saved successfully</string>
<string name="copy">コピー</string>
<string name="move">移動</string>
<string name="copy_move">Copy/Move to</string>
<string name="copy_item">アイテムをコピー</string>
<string name="copy_items">アイテムをコピー</string>
<string name="source"></string>
<string name="destination"></string>
<string name="select_destination">宛先を選択</string>
<string name="invalid_destination">選択した宛先に書き込みできませんでした</string>
<string name="please_select_destination">宛先を選択してください</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">ファイルをコピーできませんでした</string>
<string name="copying">コピー中</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 フォルダーを削除しました</item> <item quantity="one">1 フォルダーを削除しました</item>

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string> <string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string> <string name="file_saved">File saved successfully</string>
<string name="copy">Copiar</string>
<string name="move">Mover</string>
<string name="copy_move">Copiar/Mover</string>
<string name="copy_item">Copiar item</string>
<string name="copy_items">Copiar itens</string>
<string name="source">Origem</string>
<string name="destination">Destino</string>
<string name="select_destination">Selecionar destino</string>
<string name="invalid_destination">Não foi possível escrever no destino selecionado</string>
<string name="please_select_destination">Por favor selecione um destino</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Não foi possível copiar os ficheiros</string>
<string name="copying">A copiar</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 pasta eliminada</item> <item quantity="one">1 pasta eliminada</item>

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string> <string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string> <string name="file_saved">File saved successfully</string>
<string name="copy">Kopiera</string>
<string name="move">Flytta</string>
<string name="copy_move">Copy/Move to</string>
<string name="copy_item">Kopiera objekt</string>
<string name="copy_items">Kopiera objekt</string>
<string name="source">Källa</string>
<string name="destination">Mål</string>
<string name="select_destination">Välj mål</string>
<string name="invalid_destination">Det gick inte att skriva till målet</string>
<string name="please_select_destination">Please select a destination</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Kunde inte kopiera filen</string>
<string name="copying">Kopierar</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 mapp borttagen</item> <item quantity="one">1 mapp borttagen</item>

View file

@ -14,5 +14,6 @@
<dimen name="undo_size">56dp</dimen> <dimen name="undo_size">56dp</dimen>
<dimen name="settings_padding">8dp</dimen> <dimen name="settings_padding">8dp</dimen>
<dimen name="details_text_size">12sp</dimen>
<dimen name="normal_text_size">14sp</dimen> <dimen name="normal_text_size">14sp</dimen>
</resources> </resources>

View file

@ -40,6 +40,20 @@
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string> <string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string> <string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string> <string name="file_saved">File saved successfully</string>
<string name="copy">Copy</string>
<string name="move">Move</string>
<string name="copy_move">Copy/Move to</string>
<string name="copy_item">Copy item</string>
<string name="copy_items">Copy items</string>
<string name="source">Source</string>
<string name="destination">Destination</string>
<string name="select_destination">Select destination</string>
<string name="invalid_destination">Could not write to the selected destination</string>
<string name="please_select_destination">Please select a destination</string>
<string name="source_and_destination_same">Source and destination cannot be the same</string>
<string name="copy_failed">Could not copy the files</string>
<string name="copying">Copying</string>
<string name="already_exists">A file with that name already exists</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 folder deleted</item> <item quantity="one">1 folder deleted</item>