From 01ea2a2862cdafb7a2907816d230007de4a0ddbe Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Tue, 20 Feb 2024 22:03:23 +0100 Subject: [PATCH] Fix plugin setup pending intent background permission --- .../settings/plugins/PluginSettingsScreen.kt | 5 +++-- .../de/mm20/launcher2/ktx/PendingIntent.kt | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 core/ktx/src/main/java/de/mm20/launcher2/ktx/PendingIntent.kt diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/plugins/PluginSettingsScreen.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/plugins/PluginSettingsScreen.kt index a93a3b10..a1f54c03 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/plugins/PluginSettingsScreen.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/plugins/PluginSettingsScreen.kt @@ -48,6 +48,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import coil.compose.AsyncImage import de.mm20.launcher2.crashreporter.CrashReporter +import de.mm20.launcher2.ktx.sendWithBackgroundPermission import de.mm20.launcher2.plugin.PluginState import de.mm20.launcher2.plugin.PluginType import de.mm20.launcher2.ui.R @@ -312,7 +313,7 @@ fun PluginSettingsScreen(pluginId: String) { primaryAction = { TextButton(onClick = { try { - state.setupActivity.send() + state.setupActivity.sendWithBackgroundPermission() } catch (e: PendingIntent.CanceledException) { CrashReporter.logException(e) } @@ -362,7 +363,7 @@ fun PluginSettingsScreen(pluginId: String) { primaryAction = { TextButton(onClick = { try { - state.setupActivity.send() + state.setupActivity.sendWithBackgroundPermission() } catch (e: PendingIntent.CanceledException) { CrashReporter.logException(e) } diff --git a/core/ktx/src/main/java/de/mm20/launcher2/ktx/PendingIntent.kt b/core/ktx/src/main/java/de/mm20/launcher2/ktx/PendingIntent.kt new file mode 100644 index 00000000..92f0c3c8 --- /dev/null +++ b/core/ktx/src/main/java/de/mm20/launcher2/ktx/PendingIntent.kt @@ -0,0 +1,20 @@ +package de.mm20.launcher2.ktx + +import android.app.ActivityOptions +import android.app.PendingIntent + +fun PendingIntent.sendWithBackgroundPermission() { + if (isAtLeastApiLevel(34)) { + val options = ActivityOptions.makeBasic() + .setPendingIntentCreatorBackgroundActivityStartMode( + ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED + ) + .setPendingIntentBackgroundActivityStartMode( + ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED + ) + .toBundle() + send(options) + } else { + send() + } +} \ No newline at end of file