]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Fix starting a managed profile as Always-on VPN
authorTobias Brunner <tobias@strongswan.org>
Thu, 3 Apr 2025 13:54:02 +0000 (15:54 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 3 Apr 2025 14:19:53 +0000 (16:19 +0200)
The callbacks provided via ProcessLifecycleOwner are only triggered when
Activities are started.  However, when Android triggers the Always-on
VPN it directly starts our VpnService subclass, no Activity.  So the
configs were not loaded and the VPN couldn't be initiated with a managed
profile.  This ensures the config is loaded right from the start of
the app.  And by registering for modifications in onCreate() we can also
use the correct config if the app is never started in-between changes to
the managed profiles and triggering the Always-on VPN.

src/frontends/android/app/src/main/java/org/strongswan/android/logic/StrongSwanApplication.java

index 7f73dbfb86992b82e5ae5b01b5a3ed99758f2445..a49e1a2696e9a0517cd4abd7986df2faebe1c8a0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2023 Relution GmbH
- * Copyright (C) 2014-2024 Tobias Brunner
+ * Copyright (C) 2014-2025 Tobias Brunner
  *
  * Copyright (C) secunet Security Networks AG
  *
@@ -38,14 +38,10 @@ import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
-import androidx.annotation.NonNull;
 import androidx.core.os.HandlerCompat;
-import androidx.lifecycle.DefaultLifecycleObserver;
-import androidx.lifecycle.LifecycleOwner;
-import androidx.lifecycle.ProcessLifecycleOwner;
 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
-public class StrongSwanApplication extends Application implements DefaultLifecycleObserver
+public class StrongSwanApplication extends Application
 {
        private static final String TAG = StrongSwanApplication.class.getSimpleName();
 
@@ -80,6 +76,7 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
        public void onCreate()
        {
                super.onCreate();
+
                StrongSwanApplication.mContext = getApplicationContext();
                StrongSwanApplication.mInstance = this;
 
@@ -92,24 +89,12 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
 
                mUserCertificateManager = new ManagedUserCertificateManager(mContext, mManagedConfigurationService, mDatabaseHelper);
 
-               ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
-       }
-
-       @Override
-       public void onResume(@NonNull LifecycleOwner owner)
-       {
                reloadManagedConfigurationAndNotifyListeners();
 
                final IntentFilter restrictionsFilter = new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
                registerReceiver(mRestrictionsReceiver, restrictionsFilter);
        }
 
-       @Override
-       public void onPause(@NonNull LifecycleOwner owner)
-       {
-               unregisterReceiver(mRestrictionsReceiver);
-       }
-
        private void reloadManagedConfigurationAndNotifyListeners()
        {
                final Set<String> uuids = new HashSet<>(mManagedConfigurationService.getManagedProfiles().keySet());