From f77aeb78c6a607497b58850a4111e0421a42e107 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:04:42 +0200 Subject: [PATCH] fix: Fix folder access test Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com> --- .../SettingsScreen/Tiles/SaveFolderTile.kt | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/Tiles/SaveFolderTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/Tiles/SaveFolderTile.kt index 956311e2..4d40c27d 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/Tiles/SaveFolderTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/Tiles/SaveFolderTile.kt @@ -2,7 +2,6 @@ package app.myzel394.alibi.ui.components.SettingsScreen.Tiles import android.Manifest import android.content.Intent -import android.net.Uri import android.os.Build import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -97,35 +96,40 @@ fun SaveFolderTile( val successMessage = stringResource(R.string.ui_settings_option_saveFolder_success) fun updateValue(path: String?) { - if (path == null) { - return - } - - if (!BatchesFolder.canAccessFolder(context, path.toUri())) { - showError = true - return - } + if (path != null && path != RECORDER_MEDIA_SELECTED_VALUE) { + context.contentResolver.takePersistableUriPermission( + path.toUri(), + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) - if (settings.saveFolder != null && settings.saveFolder != RECORDER_MEDIA_SELECTED_VALUE) { - runCatching { - // Clean up - val grantedURIs = context.contentResolver.persistedUriPermissions; + if (!BatchesFolder.canAccessFolder(context, path.toUri())) { + showError = true - grantedURIs.forEach { permission -> + runCatching { context.contentResolver.releasePersistableUriPermission( - permission.uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION - or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + path.toUri(), + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) } + return } } - if (path != RECORDER_MEDIA_SELECTED_VALUE) { - context.contentResolver.takePersistableUriPermission( - Uri.parse(path), - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - ) + runCatching { + // Clean up + val grantedURIs = context.contentResolver.persistedUriPermissions; + + grantedURIs.forEach { permission -> + if (permission.uri == path?.toUri()) { + return@forEach + } + + context.contentResolver.releasePersistableUriPermission( + permission.uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION + or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + } } scope.launch {