]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Handle restarts of the control Activity better
authorTobias Brunner <tobias@strongswan.org>
Fri, 22 Jun 2018 11:57:51 +0000 (13:57 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 3 Jul 2018 09:31:43 +0000 (11:31 +0200)
For instance, rotating a device will restart it and this previously
could have started the wrong profile or shown the system's VPN
confirmation dialog twice.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileControlActivity.java

index a9d0791bf719086d16b904f2c587e71505bde1eb..2ebed6f82318ed1a97cc7e3857933a0abe6ce2dd 100644 (file)
@@ -50,6 +50,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
        public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
 
        private static final int PREPARE_VPN_SERVICE = 0;
+       private static final String WAITING_FOR_RESULT = "WAITING_FOR_RESULT";
        private static final String PROFILE_NAME = "PROFILE_NAME";
        private static final String PROFILE_REQUIRES_PASSWORD = "REQUIRES_PASSWORD";
        private static final String PROFILE_RECONNECT = "RECONNECT";
@@ -57,6 +58,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
        private static final String DIALOG_TAG = "Dialog";
 
        private Bundle mProfileInfo;
+       private boolean mWaitingForResult;
        private VpnStateService mService;
        private final ServiceConnection mServiceConnection = new ServiceConnection()
        {
@@ -70,15 +72,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
                public void onServiceConnected(ComponentName name, IBinder service)
                {
                        mService = ((VpnStateService.LocalBinder)service).getService();
-
-                       if (START_PROFILE.equals(getIntent().getAction()))
-                       {
-                               startVpnProfile(getIntent());
-                       }
-                       else if (DISCONNECT.equals(getIntent().getAction()))
-                       {
-                               disconnect();
-                       }
+                       handleIntent();
                }
        };
 
@@ -87,10 +81,21 @@ public class VpnProfileControlActivity extends AppCompatActivity
        {
                super.onCreate(savedInstanceState);
 
+               if (savedInstanceState != null)
+               {
+                       mWaitingForResult = savedInstanceState.getBoolean(WAITING_FOR_RESULT, false);
+               }
                this.bindService(new Intent(this, VpnStateService.class),
                                                 mServiceConnection, Service.BIND_AUTO_CREATE);
        }
 
+       @Override
+       protected void onSaveInstanceState(Bundle outState)
+       {
+               super.onSaveInstanceState(outState);
+               outState.putBoolean(WAITING_FOR_RESULT, mWaitingForResult);
+       }
+
        @Override
        protected void onDestroy()
        {
@@ -109,13 +114,12 @@ public class VpnProfileControlActivity extends AppCompatActivity
        {
                super.onNewIntent(intent);
 
-               if (START_PROFILE.equals(intent.getAction()))
-               {
-                       startVpnProfile(intent);
-               }
-               else if (DISCONNECT.equals(intent.getAction()))
+               /* store this intent in case the service is not yet connected or the activity is restarted */
+               setIntent(intent);
+
+               if (mService != null)
                {
-                       disconnect();
+                       handleIntent();
                }
        }
 
@@ -128,6 +132,13 @@ public class VpnProfileControlActivity extends AppCompatActivity
        protected void prepareVpnService(Bundle profileInfo)
        {
                Intent intent;
+
+               if (mWaitingForResult)
+               {
+                       mProfileInfo = profileInfo;
+                       return;
+               }
+
                try
                {
                        intent = VpnService.prepare(this);
@@ -150,6 +161,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
                {
                        try
                        {
+                               mWaitingForResult = true;
                                startActivityForResult(intent, PREPARE_VPN_SERVICE);
                        }
                        catch (ActivityNotFoundException ex)
@@ -159,6 +171,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
                                 * com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog
                                 * will not be found then */
                                VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported);
+                               mWaitingForResult = false;
                        }
                }
                else
@@ -173,6 +186,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
                switch (requestCode)
                {
                        case PREPARE_VPN_SERVICE:
+                               mWaitingForResult = false;
                                if (resultCode == RESULT_OK && mProfileInfo != null)
                                {
                                        if (mService != null)
@@ -324,6 +338,21 @@ public class VpnProfileControlActivity extends AppCompatActivity
                }
        }
 
+       /**
+        * Handle the Intent of this Activity depending on its action
+        */
+       private void handleIntent()
+       {
+               if (START_PROFILE.equals(getIntent().getAction()))
+               {
+                       startVpnProfile(getIntent());
+               }
+               else if (DISCONNECT.equals(getIntent().getAction()))
+               {
+                       disconnect();
+               }
+       }
+
        /**
         * Dismiss dialog if shown
         */