fix infinite storage permission check

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-05 09:43:18 +01:00
parent f86238b0af
commit 2b43c5279a
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
3 changed files with 28 additions and 25 deletions

View file

@ -191,6 +191,7 @@ class SyncedFoldersActivity :
setTheme(R.style.FallbackThemingTheme) setTheme(R.style.FallbackThemingTheme)
} }
binding.emptyList.emptyListViewAction.setOnClickListener { showHiddenItems() } binding.emptyList.emptyListViewAction.setOnClickListener { showHiddenItems() }
PermissionUtil.requestExternalStoragePermission(this, viewThemeUtils, true)
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -808,9 +809,6 @@ class SyncedFoldersActivity :
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted // permission was granted
load(getItemsDisplayedPerFolder(), true) load(getItemsDisplayedPerFolder(), true)
} else {
// permission denied --> request again
PermissionUtil.requestExternalStoragePermission(this, viewThemeUtils, true)
} }
} }
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)

View file

@ -99,6 +99,7 @@ object PermissionUtil {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
} }
// TODO rename function name to requestExternalStoragePermissionIfNeeded to prevent confusion
/** /**
* Request relevant external storage permission depending on SDK, if needed. * Request relevant external storage permission depending on SDK, if needed.
* *
@ -132,10 +133,6 @@ object PermissionUtil {
} }
} }
/**
* Request a storage permission
*/
// TODO inject this class to avoid passing ViewThemeUtils around
private fun requestStoragePermission( private fun requestStoragePermission(
activity: Activity, activity: Activity,
readOnly: Boolean, readOnly: Boolean,
@ -145,10 +142,8 @@ object PermissionUtil {
val preferences: AppPreferences = AppPreferencesImpl.fromContext(activity) val preferences: AppPreferences = AppPreferencesImpl.fromContext(activity)
if (permissionRequired || !preferences.isStoragePermissionRequested) { if (permissionRequired || !preferences.isStoragePermissionRequested) {
// determine required permissions
val permissions = if (readOnly && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { val permissions = if (readOnly && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// use granular media permissions
arrayOf( arrayOf(
Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_IMAGES,
Manifest.permission.READ_MEDIA_VIDEO Manifest.permission.READ_MEDIA_VIDEO
@ -160,29 +155,39 @@ object PermissionUtil {
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE) arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
} }
fun doRequest() { val grantedPermissions = permissions.all {
ActivityCompat.requestPermissions( ContextCompat.checkSelfPermission(activity, it) == PackageManager.PERMISSION_GRANTED
activity,
permissions,
PERMISSIONS_EXTERNAL_STORAGE
)
preferences.isStoragePermissionRequested = true
} }
// Check if we should show an explanation if (grantedPermissions) {
if (permissions.any { shouldShowRequestPermissionRationale(activity, it) }) { // Permissions already granted, proceed with functionality
// Show explanation to the user and then request permission return
}
val permanentlyDeniedPermissions = permissions.filter {
!ActivityCompat.shouldShowRequestPermissionRationale(activity, it) &&
ContextCompat.checkSelfPermission(activity, it) != PackageManager.PERMISSION_GRANTED
}
if (permissions.any { ActivityCompat.shouldShowRequestPermissionRationale(activity, it) } ||
permanentlyDeniedPermissions.isNotEmpty()
) {
Snackbar.make( Snackbar.make(
activity.findViewById(android.R.id.content), activity.findViewById(android.R.id.content),
R.string.permission_storage_access, R.string.permission_storage_access,
Snackbar.LENGTH_INDEFINITE Snackbar.LENGTH_LONG
).setAction(R.string.common_ok) { ).setAction(R.string.common_settings) {
doRequest() val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", activity.packageName, null)
}
activity.startActivity(intent)
}.also { viewThemeUtils.material.themeSnackbar(it) }.show() }.also { viewThemeUtils.material.themeSnackbar(it) }.show()
} else { } else {
// No explanation needed, request the permission. ActivityCompat.requestPermissions(activity, permissions, PERMISSIONS_EXTERNAL_STORAGE)
doRequest()
} }
// Only mark as requested after actual request
preferences.isStoragePermissionRequested = true
} }
} }

View file

@ -16,6 +16,7 @@
<string name="actionbar_see_details">Details</string> <string name="actionbar_see_details">Details</string>
<string name="actionbar_send_file">Send</string> <string name="actionbar_send_file">Send</string>
<string name="actionbar_sort">Sort</string> <string name="actionbar_sort">Sort</string>
<string name="common_settings">Settings</string>
<string name="sort_by">Sort by</string> <string name="sort_by">Sort by</string>
<string name="menu_item_sort_by_name_a_z">A - Z</string> <string name="menu_item_sort_by_name_a_z">A - Z</string>
<string name="menu_item_sort_by_name_z_a">Z - A</string> <string name="menu_item_sort_by_name_z_a">Z - A</string>
@ -500,7 +501,6 @@
<string name="forbidden_permissions_create">to create this file</string> <string name="forbidden_permissions_create">to create this file</string>
<string name="uploader_upload_forbidden_permissions">to upload to this folder</string> <string name="uploader_upload_forbidden_permissions">to upload to this folder</string>
<string name="downloader_download_file_not_found">The file is no longer available on the server</string> <string name="downloader_download_file_not_found">The file is no longer available on the server</string>
<string name="file_migration_dialog_title">Updating data storage folder</string> <string name="file_migration_dialog_title">Updating data storage folder</string>
<string name="file_migration_preparing">Preparing migration…</string> <string name="file_migration_preparing">Preparing migration…</string>
<string name="file_migration_checking_destination">Checking destination…</string> <string name="file_migration_checking_destination">Checking destination…</string>