From e3cc1b91cc589f4ebf1eaad0d4e0b5bd8a44040b Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 11 Nov 2016 23:40:33 +0100 Subject: [PATCH] rely on some extension functions from the filepicker library --- .../com/simplemobiletools/gallery/Utils.kt | 34 ++++--------------- .../gallery/activities/MainActivity.java | 2 +- .../gallery/activities/MediaActivity.java | 2 +- .../gallery/activities/ViewPagerActivity.java | 2 +- .../gallery/activities/EditActivity.kt | 10 +++--- .../activities/SetWallpaperActivity.kt | 4 +-- .../gallery/asynctasks/CopyTask.kt | 23 +++++++------ .../gallery/dialogs/CopyDialog.kt | 2 +- .../gallery/dialogs/RenameDirectoryDialog.kt | 9 +++-- .../gallery/dialogs/RenameFileDialog.kt | 9 +++-- .../gallery/dialogs/SaveAsDialog.kt | 2 +- .../gallery/extensions/context.kt | 5 --- .../gallery/extensions/textView.kt | 2 +- 13 files changed, 46 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt index f4bda57b9..887b46379 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt @@ -1,27 +1,25 @@ package com.simplemobiletools.gallery -import android.Manifest import android.annotation.TargetApi import android.app.Activity import android.content.Context import android.content.Intent -import android.content.pm.PackageManager import android.content.res.Resources import android.database.Cursor import android.net.Uri import android.os.Build import android.provider.MediaStore -import android.support.v4.content.ContextCompat -import android.support.v4.provider.DocumentFile import android.support.v7.app.ActionBar import android.util.DisplayMetrics import android.util.TypedValue import android.view.* import android.webkit.MimeTypeMap -import com.simplemobiletools.filepicker.extensions.getSDCardPath +import com.simplemobiletools.filepicker.extensions.getFileDocument +import com.simplemobiletools.filepicker.extensions.hasStoragePermission +import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.dialogs.WritePermissionDialog import com.simplemobiletools.gallery.extensions.scanFile -import com.simplemobiletools.gallery.extensions.toast import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -84,9 +82,7 @@ class Utils { } } - fun hasStoragePermission(cxt: Context): Boolean { - return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED - } + fun hasStoragePermission(context: Context) = context.hasStoragePermission() fun getMimeType(url: String): String { val extension = MimeTypeMap.getFileExtensionFromUrl(url) @@ -141,25 +137,9 @@ class Utils { } } - fun needsStupidWritePermissions(context: Context, path: String) = isPathOnSD(context, path) && isKitkat() && !context.getSDCardPath().isEmpty() + fun needsStupidWritePermissions(context: Context, path: String) = context.needsStupidWritePermissions(path) - fun isPathOnSD(context: Context, path: String): Boolean { - return path.startsWith(context.getSDCardPath()) - } - - fun isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT - - fun getFileDocument(context: Context, path: String): DocumentFile { - val relativePath = path.substring(context.getSDCardPath().length + 1) - var document = DocumentFile.fromTreeUri(context, Uri.parse(Config.newInstance(context).treeUri)) - val parts = relativePath.split("/") - for (part in parts) { - val currDocument = document.findFile(part) - if (currDocument != null) - document = currDocument - } - return document - } + fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri) @TargetApi(Build.VERSION_CODES.KITKAT) fun saveTreeUri(context: Context, resultData: Intent) { diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index 8968abe75..d5d502a3a 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -255,7 +255,7 @@ public class MainActivity extends SimpleActivity private void deleteItem(File file) { if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) { if (!Utils.Companion.isShowingWritePermissions(this, file)) { - final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath()); + final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath(), mConfig.getTreeUri()); document.delete(); } } else { diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index c43616a99..d61badd8c 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -361,7 +361,7 @@ public class MediaActivity extends SimpleActivity if (Utils.Companion.isShowingWritePermissions(this, file)) return; - final DocumentFile document = Utils.Companion.getFileDocument(this, delPath); + final DocumentFile document = Utils.Companion.getFileDocument(this, delPath, mConfig.getTreeUri()); if (document.delete()) { wereFilesDeleted = true; } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 2b78d0fbd..875f140b6 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -285,7 +285,7 @@ public class ViewPagerActivity extends SimpleActivity final File file = new File(mToBeDeleted); if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) { if (!Utils.Companion.isShowingWritePermissions(this, file)) { - final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted); + final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted, mConfig.getTreeUri()); if (document.canWrite()) { mWasFileDeleted = document.delete(); } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index 745c5847a..b057dfd4a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -8,10 +8,12 @@ import android.os.Bundle import android.util.Log import android.view.Menu import android.view.MenuItem +import com.simplemobiletools.filepicker.extensions.getFileDocument +import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.dialogs.SaveAsDialog -import com.simplemobiletools.gallery.extensions.toast import com.theartofdev.edmodo.cropper.CropImageView import kotlinx.android.synthetic.main.activity_edit.* import java.io.File @@ -96,7 +98,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener finish() } } else { - toast("${getString(R.string.image_editing_failed)}: ${result.error.message}") + //toast("${getString(R.string.image_editing_failed)}: ${result.error.message}") } } @@ -105,11 +107,11 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener var out: OutputStream? = null try { - if (Utils.needsStupidWritePermissions(this, path)) { + if (needsStupidWritePermissions(path)) { if (Utils.isShowingWritePermissions(this, file)) return - var document = Utils.getFileDocument(this, path) + var document = getFileDocument(path, mConfig.treeUri) if (!file.exists()) { document = document.createFile("", file.name) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SetWallpaperActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SetWallpaperActivity.kt index f49d4b373..955f87227 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SetWallpaperActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SetWallpaperActivity.kt @@ -8,8 +8,8 @@ import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R -import com.simplemobiletools.gallery.extensions.toast import com.theartofdev.edmodo.cropper.CropImageView import kotlinx.android.synthetic.main.activity_edit.* @@ -83,7 +83,7 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete finish() }).start() } else { - toast("${getString(R.string.image_editing_failed)}: ${result.error.message}") + //toast("${getString(R.string.image_editing_failed)}: ${result.error.message}") } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt index 7a9758907..eec6eb0b7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt @@ -4,8 +4,10 @@ import android.content.Context import android.os.AsyncTask import android.support.v4.util.Pair import android.util.Log -import com.simplemobiletools.gallery.Utils -import com.simplemobiletools.gallery.extensions.scanFile +import com.simplemobiletools.filepicker.extensions.getFileDocument +import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions +import com.simplemobiletools.filepicker.extensions.rescanItem +import com.simplemobiletools.gallery.Config import java.io.* import java.lang.ref.WeakReference @@ -13,9 +15,11 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn private val TAG = CopyTask::class.java.simpleName private var mListener: WeakReference? = null private var destinationDir: File? = null + private var mConfig: Config init { mListener = WeakReference(listener) + mConfig = Config.newInstance(context) } override fun doInBackground(vararg params: Pair, File>): Boolean? { @@ -44,8 +48,8 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn private fun copyDirectory(source: File, destination: File) { if (!destination.exists()) { - if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { - val document = Utils.getFileDocument(context, destination.absolutePath) + 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) @@ -55,11 +59,11 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn val children = source.list() for (child in children) { val newFile = File(source, child) - if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { + if (context.needsStupidWritePermissions(destination.absolutePath)) { if (newFile.isDirectory) { copyDirectory(newFile, File(destination, child)) } else { - var document = Utils.getFileDocument(context, destination.absolutePath) + var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri) document = document.createFile("", child) val inputStream = FileInputStream(newFile) @@ -80,16 +84,15 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn val inputStream = FileInputStream(source) val out: OutputStream? - if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { - var document = Utils.getFileDocument(context, destination.absolutePath) + 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) } - val paths = arrayOf(destination.absolutePath) - context.scanFile(paths) + context.rescanItem(destination) copyStream(inputStream, out) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt index 6fae4f98a..574b3eb52 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt @@ -6,10 +6,10 @@ import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager import com.simplemobiletools.filepicker.extensions.humanizePath +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.asynctasks.CopyTask -import com.simplemobiletools.gallery.extensions.toast import kotlinx.android.synthetic.main.copy_item.view.* import java.io.File diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt index 55b2a15b3..4e99f732c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt @@ -4,10 +4,13 @@ import android.app.Activity import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager +import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.humanizePath +import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions +import com.simplemobiletools.filepicker.extensions.toast +import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils -import com.simplemobiletools.gallery.extensions.toast import com.simplemobiletools.gallery.extensions.value import kotlinx.android.synthetic.main.rename_directory.view.* import java.io.File @@ -42,11 +45,11 @@ class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener: updatedFiles.add(dir.absolutePath) val newDir = File(dir.parent, newDirName) - if (Utils.needsStupidWritePermissions(context, dir.absolutePath)) { + if (context.needsStupidWritePermissions(dir.absolutePath)) { if (Utils.isShowingWritePermissions(activity, dir)) return@setOnClickListener - val document = Utils.Companion.getFileDocument(context, dir.absolutePath) + val document = context.getFileDocument(dir.absolutePath, Config.newInstance(context).treeUri) if (document.canWrite()) document.renameTo(newDirName) sendSuccess(updatedFiles, newDir) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt index 9c880eeac..b9fdf3718 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt @@ -5,10 +5,13 @@ import android.media.MediaScannerConnection import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager +import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.humanizePath +import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions +import com.simplemobiletools.filepicker.extensions.toast +import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils -import com.simplemobiletools.gallery.extensions.toast import com.simplemobiletools.gallery.extensions.value import kotlinx.android.synthetic.main.rename_file.view.* import java.io.File @@ -50,11 +53,11 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR val newFile = File(file.parent, "$fileName.$extension") - if (Utils.needsStupidWritePermissions(context, file.absolutePath)) { + if (context.needsStupidWritePermissions(file.absolutePath)) { if (Utils.isShowingWritePermissions(activity, file)) return@setOnClickListener - val document = Utils.Companion.getFileDocument(context, file.absolutePath) + val document = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri) if (document.canWrite()) document.renameTo(newFile.name) sendSuccess(file, newFile) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt index 7c5c0bf80..e4af5d6f2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt @@ -5,9 +5,9 @@ import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager import com.simplemobiletools.filepicker.extensions.getFilenameFromPath +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.isNameValid -import com.simplemobiletools.gallery.extensions.toast import com.simplemobiletools.gallery.extensions.value import kotlinx.android.synthetic.main.rename_file.view.* diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index 4b8afda12..33cdef78a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -2,10 +2,5 @@ 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) = MediaScannerConnection.scanFile(this, paths, null, null) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/textView.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/textView.kt index 80e64d1cc..7bcf1f833 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/textView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/textView.kt @@ -2,4 +2,4 @@ package com.simplemobiletools.gallery.extensions import android.widget.TextView -val TextView.value: String get() = this.text.toString().trim() +val TextView.value: String get() = text.toString().trim()