From d3377f9c8e8340624f52f20b8bdbaf46344827d9 Mon Sep 17 00:00:00 2001 From: Alexander Perfilyev Date: Mon, 16 Oct 2023 09:35:00 +0300 Subject: [PATCH] Fix trampoline activity launch on Android 14 In my opinion, it would be better to start a VPN Service as a foreground one, but I've seen(https://github.com/WireGuard/wireguard-android/pull/29) that you don't want additional permissions that come with it. --- .../com/wireguard/android/QuickTileService.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ui/src/main/java/com/wireguard/android/QuickTileService.kt b/ui/src/main/java/com/wireguard/android/QuickTileService.kt index f74c8bdf4..075fcda82 100644 --- a/ui/src/main/java/com/wireguard/android/QuickTileService.kt +++ b/ui/src/main/java/com/wireguard/android/QuickTileService.kt @@ -54,12 +54,7 @@ class QuickTileService : TileService() { null -> { val intent = Intent(this, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)) - } else { - @Suppress("DEPRECATION") - startActivityAndCollapse(intent) - } + startActivityCompat(intent) } else -> { unlockAndRun { @@ -70,7 +65,7 @@ class QuickTileService : TileService() { } catch (_: Throwable) { val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java) toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - startActivity(toggleIntent) + startActivityCompat(toggleIntent) } } } @@ -78,6 +73,15 @@ class QuickTileService : TileService() { } } + private fun startActivityCompat(intent: Intent) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)) + } else { + @Suppress("DEPRECATION", "StartActivityAndCollapseDeprecated") + startActivityAndCollapse(intent) + } + } + override fun onCreate() { isAdded = true if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {