From 7a9a7c4af568db7ec725f57fbf20d24e6c407ecc Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 24 Sep 2024 16:21:41 +0200 Subject: [PATCH] Protection against ActivityNotFoundException. --- .../androidutils/system/SystemUtils.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt b/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt index 1c213dd0ab..4a55b8549e 100644 --- a/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt +++ b/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt @@ -71,7 +71,10 @@ fun Context.copyToClipboard( * Shows notification settings for the current app. * In android O will directly opens the notification settings, in lower version it will show the App settings */ -fun Context.startNotificationSettingsIntent(activityResultLauncher: ActivityResultLauncher? = null) { +fun Context.startNotificationSettingsIntent( + activityResultLauncher: ActivityResultLauncher? = null, + noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found), +) { val intent = Intent() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS @@ -85,10 +88,14 @@ fun Context.startNotificationSettingsIntent(activityResultLauncher: ActivityResu intent.data = Uri.fromParts("package", packageName, null) } - if (activityResultLauncher != null) { - activityResultLauncher.launch(intent) - } else { - startActivity(intent) + try { + if (activityResultLauncher != null) { + activityResultLauncher.launch(intent) + } else { + startActivity(intent) + } + } catch (activityNotFoundException: ActivityNotFoundException) { + toast(noActivityFoundMessage) } }