From 51f746161d9da2383d698d95dd2b10f69dee9970 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Mon, 5 Aug 2024 11:02:49 +0200 Subject: [PATCH] android: Add workaround for a bug preventing background service starts from TileService When targeting Android 14, we get a "Background activity launch blocked!" exception when trying to start the connection in the background (closing the drawer works). Which is apparently a bug: https://issuetracker.google.com/issues/305035828 The workaround here is kinda ugly. In particular, because it's not possible anymore since a few versions to open a dialog that allows users to directly grant the required permission to the app. We can only open the generic settings dialog where users have to search for the app and grant the permission themselves (we could add a dialog with an explanation similar to the one for the power whitelist if necessary). Hopefully this gets fixed at some point (the current beta of Android 15 still has the same bug, though). --- src/frontends/android/app/src/main/AndroidManifest.xml | 1 + .../java/org/strongswan/android/ui/VpnTileService.java | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/frontends/android/app/src/main/AndroidManifest.xml b/src/frontends/android/app/src/main/AndroidManifest.xml index e102b70d9b..a5e6aabc2c 100644 --- a/src/frontends/android/app/src/main/AndroidManifest.xml +++ b/src/frontends/android/app/src/main/AndroidManifest.xml @@ -26,6 +26,7 @@ + diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java index ac8c49ade9..05c3e2da9e 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java @@ -28,6 +28,7 @@ import android.content.SharedPreferences; import android.graphics.drawable.Icon; import android.os.Build; import android.os.IBinder; +import android.provider.Settings; import android.service.quicksettings.Tile; import android.service.quicksettings.TileService; @@ -191,6 +192,15 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt } else { + /* a bug in Android 14+ requires us to request this permission in + * order to start the activity from this "background" service */ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && !Settings.canDrawOverlays(this)) + { + Intent permIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); + permIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivityAndCollapse(PendingIntent.getActivity(this, 0, permIntent, PendingIntent.FLAG_IMMUTABLE)); + return; + } startActivity(intent); } return; -- 2.47.2