From 9e5936b8a0ad63e245bd80dd0c63c1008efd9519 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Nov 2016 12:04:38 +0100 Subject: [PATCH] make use of some of the new extension functions --- .../com/simplemobiletools/gallery/Utils.kt | 19 +----------- .../gallery/activities/MainActivity.java | 4 +-- .../gallery/activities/MediaActivity.java | 4 +-- .../gallery/activities/ViewPagerActivity.java | 4 +-- .../gallery/activities/EditActivity.kt | 4 +-- .../activities/SetWallpaperActivity.kt | 2 +- .../gallery/activities/SimpleActivity.kt | 4 +++ .../gallery/asynctasks/CopyTask.kt | 4 +-- .../gallery/dialogs/CopyDialog.kt | 17 +++++----- .../gallery/dialogs/RenameDirectoryDialog.kt | 7 ++--- .../gallery/dialogs/RenameFileDialog.kt | 7 ++--- .../gallery/dialogs/WritePermissionDialog.kt | 31 ------------------- 12 files changed, 31 insertions(+), 76 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/WritePermissionDialog.kt diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt index 887b46379..9bd33d178 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt @@ -18,7 +18,6 @@ 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.models.Medium import java.io.File @@ -150,22 +149,6 @@ class Utils { context.contentResolver.takePersistableUriPermission(treeUri, takeFlags) } - fun isShowingWritePermissions(activity: Activity, file: File): Boolean { - return if ((needsStupidWritePermissions(activity, file.absolutePath) && Config.newInstance(activity).treeUri.isEmpty())) { - WritePermissionDialog(activity, object : WritePermissionDialog.OnWritePermissionListener { - override fun onConfirmed() { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - activity.startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE) - } - }) - true - } else { - false - } - } - - fun scanFiles(context: Context, paths: Array) { - context.scanFile(paths) - } + fun scanFiles(context: Context, paths: Array) = context.scanFile(paths) } } 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 d5d502a3a..5127877a0 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -199,7 +199,7 @@ public class MainActivity extends SimpleActivity } for (String path : mToBeDeleted) { - if (Utils.Companion.isShowingWritePermissions(this, new File(path))) { + if (isShowingPermDialog(new File(path))) { return; } } @@ -254,7 +254,7 @@ public class MainActivity extends SimpleActivity private void deleteItem(File file) { if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) { - if (!Utils.Companion.isShowingWritePermissions(this, file)) { + if (!isShowingPermDialog(file)) { final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath(), mConfig.getTreeUri()); document.delete(); } 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 d61badd8c..d8e8f9135 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -306,7 +306,7 @@ public class MediaActivity extends SimpleActivity } private void prepareForDeleting() { - if (Utils.Companion.isShowingWritePermissions(this, new File(mPath))) + if (isShowingPermDialog(new File(mPath))) return; Utils.Companion.showToast(this, R.string.deleting); @@ -358,7 +358,7 @@ public class MediaActivity extends SimpleActivity final File file = new File(delPath); if (file.exists()) { if (Utils.Companion.needsStupidWritePermissions(this, delPath)) { - if (Utils.Companion.isShowingWritePermissions(this, file)) + if (isShowingPermDialog(file)) return; final DocumentFile document = Utils.Companion.getFileDocument(this, delPath, mConfig.getTreeUri()); 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 875f140b6..0730e81d4 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -260,7 +260,7 @@ public class ViewPagerActivity extends SimpleActivity } private void notifyDeletion() { - if (Utils.Companion.isShowingWritePermissions(this, new File(mPath))) + if (isShowingPermDialog(new File(mPath))) return; mToBeDeleted = getCurrentFile().getAbsolutePath(); @@ -284,7 +284,7 @@ public class ViewPagerActivity extends SimpleActivity final File file = new File(mToBeDeleted); if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) { - if (!Utils.Companion.isShowingWritePermissions(this, file)) { + if (!isShowingPermDialog(file)) { 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 b057dfd4a..ac650f13e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -98,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}") } } @@ -108,7 +108,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener var out: OutputStream? = null try { if (needsStupidWritePermissions(path)) { - if (Utils.isShowingWritePermissions(this, file)) + if (isShowingPermDialog(file)) return var document = getFileDocument(path, mConfig.treeUri) 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 955f87227..7aefbd99c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SetWallpaperActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SetWallpaperActivity.kt @@ -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/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt index c881c67e8..b97720dd6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt @@ -5,10 +5,12 @@ import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.MenuItem +import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils +import java.io.File open class SimpleActivity : AppCompatActivity() { lateinit var mConfig: Config @@ -39,4 +41,6 @@ open class SimpleActivity : AppCompatActivity() { Utils.saveTreeUri(this, resultData) } } + + fun isShowingPermDialog(file: File) = isShowingWritePermissions(file, mConfig.treeUri, Constants.OPEN_DOCUMENT_TREE) } 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 eec6eb0b7..8c74a555c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt @@ -6,7 +6,7 @@ import android.support.v4.util.Pair import android.util.Log import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions -import com.simplemobiletools.filepicker.extensions.rescanItem +import com.simplemobiletools.filepicker.extensions.rescanFile import com.simplemobiletools.gallery.Config import java.io.* import java.lang.ref.WeakReference @@ -92,7 +92,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn out = FileOutputStream(destination) } - context.rescanItem(destination) + context.rescanFile(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 574b3eb52..169d3d082 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt @@ -1,25 +1,26 @@ 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.filepicker.extensions.humanizePath 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.activities.SimpleActivity import com.simplemobiletools.gallery.asynctasks.CopyTask import kotlinx.android.synthetic.main.copy_item.view.* import java.io.File -class CopyDialog(val activity: Activity, val files: List, val copyListener: CopyTask.CopyDoneListener, val listener: OnCopyListener) { +class CopyDialog(val activity: SimpleActivity, val files: List, val copyListener: CopyTask.CopyDoneListener, val listener: OnCopyListener) { init { val context = activity val view = LayoutInflater.from(context).inflate(R.layout.copy_item, null) val sourcePath = files[0].parent.trimEnd('/') var destinationPath = "" + val config = Config.newInstance(context) view.source.text = context.humanizePath(sourcePath) @@ -65,15 +66,15 @@ class CopyDialog(val activity: Activity, val files: List, val copyListener } } - if (Utils.isShowingWritePermissions(context, destinationDir)) { + if (activity.isShowingPermDialog(destinationDir)) { return@setOnClickListener } //if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) { - context.toast(R.string.copying) - val pair = Pair, File>(files, destinationDir) - CopyTask(copyListener, context).execute(pair) - dismiss() + context.toast(R.string.copying) + val pair = Pair, File>(files, destinationDir) + CopyTask(copyListener, context).execute(pair) + dismiss() /*} else { if (Utils.isPathOnSD(context, sourcePath) && Utils.isPathOnSD(context, destinationPath)) { val paths = ArrayList() 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 4e99f732c..d2de737c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.gallery.dialogs -import android.app.Activity import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager @@ -10,13 +9,13 @@ 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.activities.SimpleActivity import com.simplemobiletools.gallery.extensions.value import kotlinx.android.synthetic.main.rename_directory.view.* import java.io.File import java.util.* -class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener: OnRenameDirListener) { +class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val listener: OnRenameDirListener) { val context = activity init { @@ -46,7 +45,7 @@ class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener: val newDir = File(dir.parent, newDirName) if (context.needsStupidWritePermissions(dir.absolutePath)) { - if (Utils.isShowingWritePermissions(activity, dir)) + if (activity.isShowingPermDialog(dir)) return@setOnClickListener val document = context.getFileDocument(dir.absolutePath, Config.newInstance(context).treeUri) 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 b9fdf3718..c0d4b5df3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.gallery.dialogs -import android.app.Activity import android.media.MediaScannerConnection import android.support.v7.app.AlertDialog import android.view.LayoutInflater @@ -11,12 +10,12 @@ 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.activities.SimpleActivity import com.simplemobiletools.gallery.extensions.value import kotlinx.android.synthetic.main.rename_file.view.* import java.io.File -class RenameFileDialog(val activity: Activity, val file: File, val listener: OnRenameFileListener) { +class RenameFileDialog(val activity: SimpleActivity, val file: File, val listener: OnRenameFileListener) { init { val context = activity @@ -54,7 +53,7 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR val newFile = File(file.parent, "$fileName.$extension") if (context.needsStupidWritePermissions(file.absolutePath)) { - if (Utils.isShowingWritePermissions(activity, file)) + if (activity.isShowingPermDialog(file)) return@setOnClickListener val document = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/WritePermissionDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/WritePermissionDialog.kt deleted file mode 100644 index 0000ce4ca..000000000 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/WritePermissionDialog.kt +++ /dev/null @@ -1,31 +0,0 @@ -package com.simplemobiletools.gallery.dialogs - -import android.content.Context -import android.support.v7.app.AlertDialog -import android.view.LayoutInflater -import com.simplemobiletools.gallery.R - -class WritePermissionDialog(val context: Context, val listener: OnWritePermissionListener) { - var dialog: AlertDialog? = null - - init { - val view = LayoutInflater.from(context).inflate(R.layout.dialog_write_permission, null) - - dialog = AlertDialog.Builder(context) - .setTitle(context.resources.getString(R.string.confirm_storage_access_title)) - .setView(view) - .setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() }) - .create() - - dialog?.show() - } - - private fun dialogConfirmed() { - dialog?.dismiss() - listener.onConfirmed() - } - - interface OnWritePermissionListener { - fun onConfirmed() - } -}