From 243893f2ad2848f7df8e75c9976553041a18a295 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 16 Oct 2022 21:58:23 +0200 Subject: [PATCH] rework the permission request flow a bit --- .../gallery/pro/activities/MainActivity.kt | 16 +++++----- .../pro/activities/SettingsActivity.kt | 2 +- .../pro/activities/ViewPagerActivity.kt | 6 ++-- .../gallery/pro/adapters/MediaAdapter.kt | 6 ++-- .../pro/dialogs/AllFilesPermissionDialog.kt | 32 +++++++++++++++++++ .../gallery/pro/extensions/Activity.kt | 29 +++++++++-------- .../gallery/pro/helpers/Config.kt | 4 +++ .../gallery/pro/helpers/Constants.kt | 1 + 8 files changed, 67 insertions(+), 29 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/AllFilesPermissionDialog.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index e326c3fe4..7e5611ffb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -150,8 +150,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } // just request the permission, tryLoadGallery will then trigger in onResume - handleMediaPermissions { - if (!it) { + handleMediaPermissions { success -> + if (!success) { toast(R.string.no_storage_permissions) finish() } @@ -169,7 +169,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (!mWasMediaManagementPromptShown) { mWasMediaManagementPromptShown = true - handleMediaManagementPrompt(false) { } + handleMediaManagementPrompt { } } } } @@ -479,12 +479,12 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private fun tryLoadGallery() { // avoid calling anything right after granting the permission, it will be called from onResume() val wasMissingPermission = config.appRunCount == 1 && !hasPermission(getPermissionToRequest()) - handleMediaPermissions { - if (wasMissingPermission) { - return@handleMediaPermissions - } + handleMediaPermissions { success -> + if (success) { + if (wasMissingPermission) { + return@handleMediaPermissions + } - if (it) { if (!mWasDefaultFolderChecked) { openDefaultFolder() mWasDefaultFolderChecked = true diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt index 8a162278a..7fa1d10da 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt @@ -485,7 +485,7 @@ class SettingsActivity : SimpleActivity() { private fun setupKeepLastModified() { settings_keep_last_modified.isChecked = config.keepLastModified settings_keep_last_modified_holder.setOnClickListener { - handleMediaManagementPrompt(false) { + handleMediaManagementPrompt { settings_keep_last_modified.toggle() config.keepLastModified = settings_keep_last_modified.isChecked } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 7f94ea3c9..04680c25c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -662,7 +662,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun checkMediaManagementAndCopy(isCopyOperation: Boolean) { - handleMediaManagementPrompt(true) { + handleMediaManagementPrompt { copyMoveTo(isCopyOperation) } } @@ -1109,7 +1109,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View return } - handleMediaManagementPrompt(true) { + handleMediaManagementPrompt { if (config.isDeletePasswordProtectionOn) { handleDeletePasswordProtection { deleteConfirmed() @@ -1225,7 +1225,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun checkMediaManagementAndRename() { - handleMediaManagementPrompt(true) { + handleMediaManagementPrompt { renameFile() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index e9d991951..b784dc0f2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -235,7 +235,7 @@ class MediaAdapter( } private fun checkMediaManagementAndRename() { - activity.handleMediaManagementPrompt(true) { + activity.handleMediaManagementPrompt { renameFile() } } @@ -367,7 +367,7 @@ class MediaAdapter( } private fun checkMediaManagementAndCopy(isCopyOperation: Boolean) { - activity.handleMediaManagementPrompt(true) { + activity.handleMediaManagementPrompt { copyMoveTo(isCopyOperation) } } @@ -446,7 +446,7 @@ class MediaAdapter( } private fun checkDeleteConfirmation() { - activity.handleMediaManagementPrompt(true) { + activity.handleMediaManagementPrompt { if (config.isDeletePasswordProtectionOn) { activity.handleDeletePasswordProtection { deleteFiles() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/AllFilesPermissionDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/AllFilesPermissionDialog.kt new file mode 100644 index 000000000..26f95b20e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/AllFilesPermissionDialog.kt @@ -0,0 +1,32 @@ +package com.simplemobiletools.gallery.pro.dialogs + +import androidx.appcompat.app.AlertDialog +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.getAlertDialogBuilder +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.gallery.pro.R +import kotlinx.android.synthetic.main.dialog_confirm_delete_folder.view.* + +class AllFilesPermissionDialog( + val activity: BaseSimpleActivity, message: String = "", val callback: (result: Boolean) -> Unit, val neutralPressed: () -> Unit +) { + private var dialog: AlertDialog? = null + + init { + val view = activity.layoutInflater.inflate(R.layout.dialog_message, null) + view.message.text = message + + activity.getAlertDialogBuilder().setPositiveButton(R.string.all_files) { dialog, which -> positivePressed() } + .setNeutralButton(R.string.media_only) { dialog, which -> neutralPressed() } + .apply { + activity.setupDialogStuff(view, this) { alertDialog -> + dialog = alertDialog + } + } + } + + private fun positivePressed() { + dialog?.dismiss() + callback(true) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index b006b3ebc..13e1151d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -26,7 +26,6 @@ import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.commons.activities.BaseSimpleActivity -import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.SecurityDialog import com.simplemobiletools.commons.extensions.* @@ -37,6 +36,7 @@ import com.simplemobiletools.gallery.pro.BuildConfig import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.SettingsActivity import com.simplemobiletools.gallery.pro.activities.SimpleActivity +import com.simplemobiletools.gallery.pro.dialogs.AllFilesPermissionDialog import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN import com.simplemobiletools.gallery.pro.models.DateTaken @@ -127,19 +127,21 @@ fun SimpleActivity.launchAbout() { startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true) } -fun BaseSimpleActivity.handleMediaManagementPrompt(avoidShowingAllFiles: Boolean, callback: () -> Unit) { +fun BaseSimpleActivity.handleMediaManagementPrompt(callback: () -> Unit) { if (canManageMedia() || isExternalStorageManager()) { callback() - } else if (isRPlus() && resources.getBoolean(R.bool.require_all_files_access) && !avoidShowingAllFiles) { + } else if (isRPlus() && resources.getBoolean(R.bool.require_all_files_access) && !config.avoidShowingAllFilesPrompt) { if (Environment.isExternalStorageManager()) { callback() } else { var messagePrompt = getString(R.string.access_storage_prompt) - if (isSPlus()) { - messagePrompt += "\n\n${getString(R.string.media_management_alternative)}" + messagePrompt += if (isSPlus()) { + "\n\n${getString(R.string.media_management_alternative)}" + } else { + "\n\n${getString(R.string.alternative_media_access)}" } - ConfirmationAdvancedDialog(this, messagePrompt, 0, R.string.ok, 0, true) { success -> + AllFilesPermissionDialog(this, messagePrompt, callback = { success -> if (success) { try { val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) @@ -155,15 +157,14 @@ fun BaseSimpleActivity.handleMediaManagementPrompt(avoidShowingAllFiles: Boolean showErrorToast(e) } } - } else { - finish() } - } - } - } else if (isSPlus() && !MediaStore.canManageMedia(this) && !isExternalStorageManager()) { - val message = "${getString(R.string.media_management_prompt)}\n\n${getString(R.string.media_management_note)}" - ConfirmationDialog(this, message, 0, R.string.ok, 0) { - launchMediaManagementIntent(callback) + }, neutralPressed = { + if (isSPlus()) { + launchMediaManagementIntent(callback) + } else { + config.avoidShowingAllFilesPrompt = true + } + }) } } else { callback() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt index 72151b88d..9dd3c3ef5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt @@ -546,4 +546,8 @@ class Config(context: Context) : BaseConfig(context) { var customFoldersOrder: String get() = prefs.getString(CUSTOM_FOLDERS_ORDER, "")!! set(customFoldersOrder) = prefs.edit().putString(CUSTOM_FOLDERS_ORDER, customFoldersOrder).apply() + + var avoidShowingAllFilesPrompt: Boolean + get() = prefs.getBoolean(AVOID_SHOWING_ALL_FILES_PROMPT, false) + set(avoidShowingAllFilesPrompt) = prefs.edit().putBoolean(AVOID_SHOWING_ALL_FILES_PROMPT, avoidShowingAllFilesPrompt).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt index 1a37f469d..7ab90af63 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt @@ -96,6 +96,7 @@ const val LIMIT_FOLDER_TITLE = "folder_limit_title" const val THUMBNAIL_SPACING = "thumbnail_spacing" const val FILE_ROUNDED_CORNERS = "file_rounded_corners" const val CUSTOM_FOLDERS_ORDER = "custom_folders_order" +const val AVOID_SHOWING_ALL_FILES_PROMPT = "avoid_showing_all_files_prompt" // slideshow const val SLIDESHOW_INTERVAL = "slideshow_interval"