From baf2f4741334494e15c658bce86e0b2e83a8d3e6 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 22 Jun 2018 13:57:51 +0200 Subject: [PATCH] android: Handle restarts of the control Activity better 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. --- .../android/ui/VpnProfileControlActivity.java | 59 ++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileControlActivity.java b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileControlActivity.java index a9d0791bf7..2ebed6f823 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileControlActivity.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileControlActivity.java @@ -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 */ -- 2.39.2