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

View file

@ -99,6 +99,7 @@ object PermissionUtil {
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.
*
@ -132,10 +133,6 @@ object PermissionUtil {
}
}
/**
* Request a storage permission
*/
// TODO inject this class to avoid passing ViewThemeUtils around
private fun requestStoragePermission(
activity: Activity,
readOnly: Boolean,
@ -145,10 +142,8 @@ object PermissionUtil {
val preferences: AppPreferences = AppPreferencesImpl.fromContext(activity)
if (permissionRequired || !preferences.isStoragePermissionRequested) {
// determine required permissions
val permissions = if (readOnly && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// use granular media permissions
arrayOf(
Manifest.permission.READ_MEDIA_IMAGES,
Manifest.permission.READ_MEDIA_VIDEO
@ -160,29 +155,39 @@ object PermissionUtil {
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
fun doRequest() {
ActivityCompat.requestPermissions(
activity,
permissions,
PERMISSIONS_EXTERNAL_STORAGE
)
preferences.isStoragePermissionRequested = true
val grantedPermissions = permissions.all {
ContextCompat.checkSelfPermission(activity, it) == PackageManager.PERMISSION_GRANTED
}
// Check if we should show an explanation
if (permissions.any { shouldShowRequestPermissionRationale(activity, it) }) {
// Show explanation to the user and then request permission
if (grantedPermissions) {
// Permissions already granted, proceed with functionality
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(
activity.findViewById(android.R.id.content),
R.string.permission_storage_access,
Snackbar.LENGTH_INDEFINITE
).setAction(R.string.common_ok) {
doRequest()
Snackbar.LENGTH_LONG
).setAction(R.string.common_settings) {
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()
} else {
// No explanation needed, request the permission.
doRequest()
ActivityCompat.requestPermissions(activity, permissions, PERMISSIONS_EXTERNAL_STORAGE)
}
// 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_send_file">Send</string>
<string name="actionbar_sort">Sort</string>
<string name="common_settings">Settings</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_z_a">Z - A</string>
@ -500,7 +501,6 @@
<string name="forbidden_permissions_create">to create this file</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="file_migration_dialog_title">Updating data storage folder</string>
<string name="file_migration_preparing">Preparing migration…</string>
<string name="file_migration_checking_destination">Checking destination…</string>