]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Add workaround for a bug preventing background service starts from TileService
authorTobias Brunner <tobias@strongswan.org>
Mon, 5 Aug 2024 09:02:49 +0000 (11:02 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 6 Aug 2024 16:01:54 +0000 (18:01 +0200)
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
src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java

index e102b70d9bfddf1a9e9e3ff22996882144bbfce1..a5e6aabc2c11fd623459cfd2e8638b6f1bedcd33 100644 (file)
@@ -26,6 +26,7 @@
     <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
+    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
     <!-- necessary to allow users to select ex-/included apps and EAP-TNC -->
     <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
         tools:ignore="QueryAllPackagesPermission" />
index ac8c49ade991764369189767745a001858c08c99..05c3e2da9ed301ff26e236628e36728f40e47710 100644 (file)
@@ -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;