From 7ab4c010107a28d0af0fdd1eff1022771e4b7362 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Nov 2016 15:43:06 +0100 Subject: [PATCH] replace some functions with extension helpers --- .../filemanager/asynctasks/CopyTask.kt | 13 ++++++++----- .../filemanager/fragments/ItemsFragment.java | 12 +----------- .../filemanager/dialogs/CopyDialog.kt | 10 +++++----- .../filemanager/dialogs/CreateNewItemDialog.kt | 8 ++------ .../filemanager/dialogs/RenameItemDialog.kt | 12 +++--------- .../filemanager/extensions/long.kt | 8 -------- 6 files changed, 19 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt index cf45a42d..22c1efd6 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt +++ b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt @@ -8,12 +8,13 @@ import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.fragments.ItemsFragment import com.simplemobiletools.filepicker.extensions.getFileDocument 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.lang.ref.WeakReference import java.util.* -class CopyTask(listener: CopyTask.CopyListener, val context: Context, val deleteAfterCopy: Boolean) : AsyncTask, File>, Void, Boolean>() { +class CopyTask(listener: CopyTask.CopyListener, val context: Context, val deleteAfterCopy: Boolean) : AsyncTask, File>, Void, Boolean>() { private val TAG = CopyTask::class.java.simpleName private var mListener: WeakReference? = null private var mMovedFiles: ArrayList @@ -25,7 +26,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete mConfig = Config.newInstance(context) } - override fun doInBackground(vararg params: Pair, File>): Boolean? { + override fun doInBackground(vararg params: Pair, File>): Boolean? { val pair = params[0] val files = pair.first 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 } @@ -85,7 +88,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete val inputStream = FileInputStream(newFile) val out = context.contentResolver.openOutputStream(document.uri) copyStream(inputStream, out) - context.rescanItem(destination) + context.scanFile(destination) {} mMovedFiles.add(source) } } else { @@ -112,7 +115,7 @@ class CopyTask(listener: CopyTask.CopyListener, val context: Context, val delete } copyStream(inputStream, out) - context.rescanItem(destination) + context.scanFile(destination) {} mMovedFiles.add(source) } diff --git a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java index d9a90a03..19f2f8e1 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -365,7 +365,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment if (itemIndexes.isEmpty()) return; - final List itemsToCopy = new ArrayList<>(itemIndexes.size()); + final ArrayList itemsToCopy = new ArrayList<>(itemIndexes.size()); for (Integer i : itemIndexes) { FileDirItem item = mItems.get(i); itemsToCopy.add(new File(item.getPath())); @@ -454,16 +454,6 @@ public class ItemsFragment extends android.support.v4.app.Fragment 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) { if (item.isDirectory()) { for (File child : item.listFiles()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt index 70508377..a08c5b3d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt @@ -14,8 +14,9 @@ import com.simplemobiletools.filepicker.dialogs.FilePickerDialog import com.simplemobiletools.filepicker.extensions.* import kotlinx.android.synthetic.main.copy_item.view.* import java.io.File +import java.util.* -class CopyDialog(val activity: Activity, val files: List, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) { +class CopyDialog(val activity: Activity, val files: ArrayList, val copyListener: CopyTask.CopyListener, val listener: OnCopyListener) { init { val context = activity @@ -82,21 +83,20 @@ class CopyDialog(val activity: Activity, val files: List, val copyListener if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { context.toast(R.string.copying) - val pair = Pair, File>(files, destinationDir) + val pair = Pair, File>(files, destinationDir) CopyTask(copyListener, context, false).execute(pair) dismiss() } else { if (context.isPathOnSD(sourcePath) || context.isPathOnSD(destinationPath)) { context.toast(R.string.moving) - val pair = Pair, File>(files, destinationDir) + val pair = Pair, File>(files, destinationDir) CopyTask(copyListener, context, true).execute(pair) dismiss() } else { for (file in files) { val destination = File(destinationDir, file.name) file.renameTo(destination) - context.rescanItem(file) - context.rescanItem(destination) + context.scanFiles(arrayListOf(file, destination)) {} } context.toast(R.string.moving_success) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt index 26d07ebe..2f27ddbe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt @@ -7,11 +7,7 @@ import android.view.View import android.view.WindowManager import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.R -import com.simplemobiletools.filemanager.extensions.isValidFilename -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 com.simplemobiletools.filepicker.extensions.* import kotlinx.android.synthetic.main.create_new.view.* import java.io.File import java.io.IOException @@ -32,7 +28,7 @@ class CreateNewItemDialog(val context: Context, val path: String, val listener: val name = view.item_name.value if (name.isEmpty()) { context.toast(R.string.empty_name) - } else if (name.isValidFilename()) { + } else if (name.isAValidFilename()) { val file = File(path, name) if (file.exists()) { context.toast(R.string.name_taken) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt index eec655e5..9c186ec4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/RenameItemDialog.kt @@ -6,12 +6,7 @@ import android.view.LayoutInflater import android.view.WindowManager import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.R -import com.simplemobiletools.filemanager.extensions.isValidFilename -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.extensions.* import com.simplemobiletools.filepicker.models.FileDirItem import kotlinx.android.synthetic.main.rename_item.view.* import java.io.File @@ -32,7 +27,7 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir show() getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ val newName = view.item_name.value - if (newName.isValidFilename()) { + if (newName.isAValidFilename()) { val currFile = File(path, item.name) 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) { - context.rescanItem(currFile) - context.rescanItem(newFile) + context.scanFiles(arrayListOf(currFile, newFile)) {} listener.onSuccess() } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/long.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/long.kt index 31cafe25..3fcceb29 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/long.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/long.kt @@ -1,8 +1,6 @@ package com.simplemobiletools.filemanager.extensions -import android.text.format.DateFormat import java.text.DecimalFormat -import java.util.* fun Long.formatSize(): String { if (this <= 0) @@ -12,9 +10,3 @@ fun Long.formatSize(): String { val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt() 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() -}