From f2cfdb196802cd1349e71a615d3d507ee8e858c3 Mon Sep 17 00:00:00 2001 From: MiMoHo <37556964+MiMoHo@users.noreply.github.com> Date: Fri, 3 Jul 2026 20:57:40 +0200 Subject: [PATCH] fix: guard takePersistableUriPermission against SecurityException Picking a custom alarm/timer sound via a file manager that answers ACTION_OPEN_DOCUMENT with a plain content:// uri (only a one-time read grant, no persistable grant) made storeNewYourAlarmSound() crash: contentResolver.takePersistableUriPermission() threw an uncaught SecurityException that propagated up through the host app's onActivityResult. The selected sound is already saved to baseConfig.yourAlarmSounds before this call, so wrapping only the takePersistableUriPermission() call in a try/catch keeps the sound selected and playable for the current session while preventing the crash. Benefits every app that uses SelectAlarmSoundDialog (Clock, Calendar). Fixes FossifyOrg/Clock#267 --- .../main/kotlin/org/fossify/commons/extensions/Context.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Context.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Context.kt index 1cd124ad1b..eeefcef54a 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Context.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Context.kt @@ -811,7 +811,12 @@ fun Context.storeNewYourAlarmSound(resultData: Intent): AlarmSound { baseConfig.yourAlarmSounds = Gson().toJson(yourAlarmSounds) val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION - contentResolver.takePersistableUriPermission(uri, takeFlags) + try { + contentResolver.takePersistableUriPermission(uri, takeFlags) + } catch (_: SecurityException) { + // some file managers return a content uri without a persistable + // permission grant, which would otherwise crash the app + } return newAlarmSound }