replace some functions with extension helpers

This commit is contained in:
tibbi
2016-11-12 15:43:06 +01:00
parent 75231e8f9b
commit 7ab4c01010
6 changed files with 19 additions and 44 deletions

View File

@ -8,12 +8,13 @@ import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.fragments.ItemsFragment import com.simplemobiletools.filemanager.fragments.ItemsFragment
import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanItem import com.simplemobiletools.filepicker.extensions.scanFile
import com.simplemobiletools.filepicker.extensions.scanFiles
import java.io.* import java.io.*
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
class CopyTask(listener: CopyTask.CopyListener, val context: Context, val deleteAfterCopy: Boolean) : AsyncTask<Pair<List<File>, File>, Void, Boolean>() { 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 val TAG = CopyTask::class.java.simpleName
private var mListener: WeakReference<CopyListener>? = null private var mListener: WeakReference<CopyListener>? = null
private var mMovedFiles: ArrayList<File> private var mMovedFiles: ArrayList<File>
@ -25,7 +26,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete
mConfig = Config.newInstance(context) mConfig = Config.newInstance(context)
} }
override fun doInBackground(vararg params: Pair<List<File>, File>): Boolean? { override fun doInBackground(vararg params: Pair<ArrayList<File>, File>): Boolean? {
val pair = params[0] val pair = params[0]
val files = pair.first val files = pair.first
for (file in files) { for (file in files) {
@ -50,6 +51,8 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete
} }
} }
} }
context.scanFiles(files) {}
context.scanFiles(mMovedFiles) {}
return true return true
} }
@ -85,7 +88,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete
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) context.scanFile(destination) {}
mMovedFiles.add(source) mMovedFiles.add(source)
} }
} else { } else {
@ -112,7 +115,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete
} }
copyStream(inputStream, out) copyStream(inputStream, out)
context.rescanItem(destination) context.scanFile(destination) {}
mMovedFiles.add(source) mMovedFiles.add(source)
} }

View File

@ -365,7 +365,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
if (itemIndexes.isEmpty()) if (itemIndexes.isEmpty())
return; return;
final List<File> itemsToCopy = new ArrayList<>(itemIndexes.size()); final ArrayList<File> itemsToCopy = new ArrayList<>(itemIndexes.size());
for (Integer i : itemIndexes) { for (Integer i : itemIndexes) {
FileDirItem item = mItems.get(i); FileDirItem item = mItems.get(i);
itemsToCopy.add(new File(item.getPath())); itemsToCopy.add(new File(item.getPath()));
@ -454,16 +454,6 @@ public class ItemsFragment extends android.support.v4.app.Fragment
mToBeDeleted.clear(); mToBeDeleted.clear();
} }
private void rescanItem(File item) {
if (item.isDirectory()) {
for (File child : item.listFiles()) {
rescanItem(child);
}
}
MediaScannerConnection.scanFile(getContext(), new String[]{item.getAbsolutePath()}, null, null);
}
private void deleteItem(File item) { private void deleteItem(File item) {
if (item.isDirectory()) { if (item.isDirectory()) {
for (File child : item.listFiles()) { for (File child : item.listFiles()) {

View File

@ -14,8 +14,9 @@ import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
import com.simplemobiletools.filepicker.extensions.* import com.simplemobiletools.filepicker.extensions.*
import kotlinx.android.synthetic.main.copy_item.view.* import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File import java.io.File
import java.util.*
class CopyDialog(val activity: Activity, val files: List<File>, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) { class CopyDialog(val activity: Activity, val files: ArrayList<File>, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) {
init { init {
val context = activity val context = activity
@ -82,21 +83,20 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying) context.toast(R.string.copying)
val pair = Pair<List<File>, File>(files, destinationDir) val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyTask(copyListener, context, false).execute(pair) CopyTask(copyListener, context, false).execute(pair)
dismiss() dismiss()
} else { } else {
if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) { if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) {
context.toast(R.string.moving) context.toast(R.string.moving)
val pair = Pair<List<File>, File>(files, destinationDir) val pair = Pair<ArrayList<File>, File>(files, destinationDir)
CopyTask(copyListener, context, true).execute(pair) CopyTask(copyListener, context, true).execute(pair)
dismiss() dismiss()
} else { } else {
for (file in files) { for (file in files) {
val destination = File(destinationDir, file.name) val destination = File(destinationDir, file.name)
file.renameTo(destination) file.renameTo(destination)
context.rescanItem(file) context.scanFiles(arrayListOf(file, destination)) {}
context.rescanItem(destination)
} }
context.toast(R.string.moving_success) context.toast(R.string.moving_success)

View File

@ -7,11 +7,7 @@ import android.view.View
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.extensions.isValidFilename import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.filemanager.extensions.value
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
import kotlinx.android.synthetic.main.create_new.view.* import kotlinx.android.synthetic.main.create_new.view.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@ -32,7 +28,7 @@ class CreateNewItemDialog(val context: Context, val path: String, val listener:
val name = view.item_name.value val name = view.item_name.value
if (name.isEmpty()) { if (name.isEmpty()) {
context.toast(R.string.empty_name) context.toast(R.string.empty_name)
} else if (name.isValidFilename()) { } else if (name.isAValidFilename()) {
val file = File(path, name) val file = File(path, name)
if (file.exists()) { if (file.exists()) {
context.toast(R.string.name_taken) context.toast(R.string.name_taken)

View File

@ -6,12 +6,7 @@ import android.view.LayoutInflater
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.extensions.isValidFilename import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.filemanager.extensions.value
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanItem
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.filepicker.models.FileDirItem import com.simplemobiletools.filepicker.models.FileDirItem
import kotlinx.android.synthetic.main.rename_item.view.* import kotlinx.android.synthetic.main.rename_item.view.*
import java.io.File import java.io.File
@ -32,7 +27,7 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
show() show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val newName = view.item_name.value val newName = view.item_name.value
if (newName.isValidFilename()) { if (newName.isAValidFilename()) {
val currFile = File(path, item.name) val currFile = File(path, item.name)
val newFile = File(path, newName) val newFile = File(path, newName)
@ -63,8 +58,7 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
} }
private fun sendSuccess(currFile: File, newFile: File) { private fun sendSuccess(currFile: File, newFile: File) {
context.rescanItem(currFile) context.scanFiles(arrayListOf(currFile, newFile)) {}
context.rescanItem(newFile)
listener.onSuccess() listener.onSuccess()
} }

View File

@ -1,8 +1,6 @@
package com.simplemobiletools.filemanager.extensions package com.simplemobiletools.filemanager.extensions
import android.text.format.DateFormat
import java.text.DecimalFormat import java.text.DecimalFormat
import java.util.*
fun Long.formatSize(): String { fun Long.formatSize(): String {
if (this <= 0) if (this <= 0)
@ -12,9 +10,3 @@ fun Long.formatSize(): String {
val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt() val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups] return DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
} }
fun Long.formatLastModified(): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = this
return DateFormat.format("dd.MM.yyyy HH:mm", cal).toString()
}