rework the permission request flow a bit

This commit is contained in:
tibbi 2022-10-16 21:58:23 +02:00
parent 387b7d679b
commit 243893f2ad
8 changed files with 67 additions and 29 deletions

View File

@ -150,8 +150,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
} }
// just request the permission, tryLoadGallery will then trigger in onResume // just request the permission, tryLoadGallery will then trigger in onResume
handleMediaPermissions { handleMediaPermissions { success ->
if (!it) { if (!success) {
toast(R.string.no_storage_permissions) toast(R.string.no_storage_permissions)
finish() finish()
} }
@ -169,7 +169,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
if (!mWasMediaManagementPromptShown) { if (!mWasMediaManagementPromptShown) {
mWasMediaManagementPromptShown = true mWasMediaManagementPromptShown = true
handleMediaManagementPrompt(false) { } handleMediaManagementPrompt { }
} }
} }
} }
@ -479,12 +479,12 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
private fun tryLoadGallery() { private fun tryLoadGallery() {
// avoid calling anything right after granting the permission, it will be called from onResume() // avoid calling anything right after granting the permission, it will be called from onResume()
val wasMissingPermission = config.appRunCount == 1 && !hasPermission(getPermissionToRequest()) val wasMissingPermission = config.appRunCount == 1 && !hasPermission(getPermissionToRequest())
handleMediaPermissions { handleMediaPermissions { success ->
if (wasMissingPermission) { if (success) {
return@handleMediaPermissions if (wasMissingPermission) {
} return@handleMediaPermissions
}
if (it) {
if (!mWasDefaultFolderChecked) { if (!mWasDefaultFolderChecked) {
openDefaultFolder() openDefaultFolder()
mWasDefaultFolderChecked = true mWasDefaultFolderChecked = true

View File

@ -485,7 +485,7 @@ class SettingsActivity : SimpleActivity() {
private fun setupKeepLastModified() { private fun setupKeepLastModified() {
settings_keep_last_modified.isChecked = config.keepLastModified settings_keep_last_modified.isChecked = config.keepLastModified
settings_keep_last_modified_holder.setOnClickListener { settings_keep_last_modified_holder.setOnClickListener {
handleMediaManagementPrompt(false) { handleMediaManagementPrompt {
settings_keep_last_modified.toggle() settings_keep_last_modified.toggle()
config.keepLastModified = settings_keep_last_modified.isChecked config.keepLastModified = settings_keep_last_modified.isChecked
} }

View File

@ -662,7 +662,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
private fun checkMediaManagementAndCopy(isCopyOperation: Boolean) { private fun checkMediaManagementAndCopy(isCopyOperation: Boolean) {
handleMediaManagementPrompt(true) { handleMediaManagementPrompt {
copyMoveTo(isCopyOperation) copyMoveTo(isCopyOperation)
} }
} }
@ -1109,7 +1109,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
return return
} }
handleMediaManagementPrompt(true) { handleMediaManagementPrompt {
if (config.isDeletePasswordProtectionOn) { if (config.isDeletePasswordProtectionOn) {
handleDeletePasswordProtection { handleDeletePasswordProtection {
deleteConfirmed() deleteConfirmed()
@ -1225,7 +1225,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
private fun checkMediaManagementAndRename() { private fun checkMediaManagementAndRename() {
handleMediaManagementPrompt(true) { handleMediaManagementPrompt {
renameFile() renameFile()
} }
} }

View File

@ -235,7 +235,7 @@ class MediaAdapter(
} }
private fun checkMediaManagementAndRename() { private fun checkMediaManagementAndRename() {
activity.handleMediaManagementPrompt(true) { activity.handleMediaManagementPrompt {
renameFile() renameFile()
} }
} }
@ -367,7 +367,7 @@ class MediaAdapter(
} }
private fun checkMediaManagementAndCopy(isCopyOperation: Boolean) { private fun checkMediaManagementAndCopy(isCopyOperation: Boolean) {
activity.handleMediaManagementPrompt(true) { activity.handleMediaManagementPrompt {
copyMoveTo(isCopyOperation) copyMoveTo(isCopyOperation)
} }
} }
@ -446,7 +446,7 @@ class MediaAdapter(
} }
private fun checkDeleteConfirmation() { private fun checkDeleteConfirmation() {
activity.handleMediaManagementPrompt(true) { activity.handleMediaManagementPrompt {
if (config.isDeletePasswordProtectionOn) { if (config.isDeletePasswordProtectionOn) {
activity.handleDeletePasswordProtection { activity.handleDeletePasswordProtection {
deleteFiles() deleteFiles()

View File

@ -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)
}
}

View File

@ -26,7 +26,6 @@ import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.SecurityDialog import com.simplemobiletools.commons.dialogs.SecurityDialog
import com.simplemobiletools.commons.extensions.* 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.R
import com.simplemobiletools.gallery.pro.activities.SettingsActivity import com.simplemobiletools.gallery.pro.activities.SettingsActivity
import com.simplemobiletools.gallery.pro.activities.SimpleActivity 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.dialogs.PickDirectoryDialog
import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN
import com.simplemobiletools.gallery.pro.models.DateTaken 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) 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()) { if (canManageMedia() || isExternalStorageManager()) {
callback() 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()) { if (Environment.isExternalStorageManager()) {
callback() callback()
} else { } else {
var messagePrompt = getString(R.string.access_storage_prompt) var messagePrompt = getString(R.string.access_storage_prompt)
if (isSPlus()) { messagePrompt += if (isSPlus()) {
messagePrompt += "\n\n${getString(R.string.media_management_alternative)}" "\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) { if (success) {
try { try {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
@ -155,15 +157,14 @@ fun BaseSimpleActivity.handleMediaManagementPrompt(avoidShowingAllFiles: Boolean
showErrorToast(e) showErrorToast(e)
} }
} }
} else {
finish()
} }
} }, neutralPressed = {
} if (isSPlus()) {
} else if (isSPlus() && !MediaStore.canManageMedia(this) && !isExternalStorageManager()) { launchMediaManagementIntent(callback)
val message = "${getString(R.string.media_management_prompt)}\n\n${getString(R.string.media_management_note)}" } else {
ConfirmationDialog(this, message, 0, R.string.ok, 0) { config.avoidShowingAllFilesPrompt = true
launchMediaManagementIntent(callback) }
})
} }
} else { } else {
callback() callback()

View File

@ -546,4 +546,8 @@ class Config(context: Context) : BaseConfig(context) {
var customFoldersOrder: String var customFoldersOrder: String
get() = prefs.getString(CUSTOM_FOLDERS_ORDER, "")!! get() = prefs.getString(CUSTOM_FOLDERS_ORDER, "")!!
set(customFoldersOrder) = prefs.edit().putString(CUSTOM_FOLDERS_ORDER, customFoldersOrder).apply() 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()
} }

View File

@ -96,6 +96,7 @@ const val LIMIT_FOLDER_TITLE = "folder_limit_title"
const val THUMBNAIL_SPACING = "thumbnail_spacing" const val THUMBNAIL_SPACING = "thumbnail_spacing"
const val FILE_ROUNDED_CORNERS = "file_rounded_corners" const val FILE_ROUNDED_CORNERS = "file_rounded_corners"
const val CUSTOM_FOLDERS_ORDER = "custom_folders_order" const val CUSTOM_FOLDERS_ORDER = "custom_folders_order"
const val AVOID_SHOWING_ALL_FILES_PROMPT = "avoid_showing_all_files_prompt"
// slideshow // slideshow
const val SLIDESHOW_INTERVAL = "slideshow_interval" const val SLIDESHOW_INTERVAL = "slideshow_interval"