private static final String PROFILE_REQUIRES_PASSWORD = "org.strongswan.android.MainActivity.REQUIRES_PASSWORD";
private static final String PROFILE_RECONNECT = "org.strongswan.android.MainActivity.RECONNECT";
private static final String PROFILE_DISCONNECT = "org.strongswan.android.MainActivity.DISCONNECT";
+ private static final String PROFILE_FOREGROUND = "org.strongswan.android.MainActivity.PROFILE_FOREGROUND";
private static final String DIALOG_TAG = "Dialog";
+ private boolean mIsVisible;
private Bundle mProfileInfo;
private VpnStateService mService;
private final ServiceConnection mServiceConnection = new ServiceConnection()
if (START_PROFILE.equals(getIntent().getAction()))
{
- startVpnProfile(getIntent());
+ startVpnProfile(getIntent(), false);
}
else if (DISCONNECT.equals(getIntent().getAction()))
{
- disconnect();
+ disconnect(false);
}
}
};
}
}
+ @Override
+ protected void onStart()
+ {
+ super.onStart();
+ mIsVisible = true;
+ }
+
+ @Override
+ protected void onStop()
+ {
+ super.onStop();
+ mIsVisible = false;
+ }
+
/**
* Due to launchMode=singleTop this is called if the Activity already exists
*/
if (START_PROFILE.equals(intent.getAction()))
{
- startVpnProfile(intent);
+ startVpnProfile(intent, mIsVisible);
}
else if (DISCONNECT.equals(intent.getAction()))
{
- disconnect();
+ disconnect(mIsVisible);
}
}
@Override
public void onVpnProfileSelected(VpnProfile profile)
{
+ startVpnProfile(profile, true);
+ }
+
+ /**
+ * Start the given VPN profile
+ *
+ * @param profile VPN profile
+ * @param foreground whether this was initiated when the activity was visible
+ */
+ public void startVpnProfile(VpnProfile profile, boolean foreground)
+ {
Bundle profileInfo = new Bundle();
profileInfo.putLong(VpnProfileDataSource.KEY_ID, profile.getId());
profileInfo.putString(VpnProfileDataSource.KEY_USERNAME, profile.getUsername());
if (mService != null && (mService.getState() == State.CONNECTED || mService.getState() == State.CONNECTING))
{
profileInfo.putBoolean(PROFILE_RECONNECT, mService.getProfile().getId() == profile.getId());
+ profileInfo.putBoolean(PROFILE_FOREGROUND, foreground);
ConfirmationDialog dialog = new ConfirmationDialog();
dialog.setArguments(profileInfo);
* if the profile doesn't exist.
*
* @param intent Intent that caused us to start this
+ * @param foreground whether this was initiated when the activity was visible
*/
- private void startVpnProfile(Intent intent)
+ private void startVpnProfile(Intent intent, boolean foreground)
{
long profileId = intent.getLongExtra(EXTRA_VPN_PROFILE_ID, 0);
if (profileId <= 0)
if (profile != null)
{
- onVpnProfileSelected(profile);
+ startVpnProfile(profile, foreground);
}
else
{
/**
* Disconnect the current connection, if any (silently ignored if there is no connection).
*/
- private void disconnect()
+ private void disconnect(boolean foreground)
{
removeFragmentByTag(DIALOG_TAG);
{
Bundle args = new Bundle();
args.putBoolean(PROFILE_DISCONNECT, true);
+ args.putBoolean(PROFILE_FOREGROUND, foreground);
ConfirmationDialog dialog = new ConfirmationDialog();
dialog.setArguments(args);
dialog.show(this.getSupportFragmentManager(), DIALOG_TAG);
- return;
}
}
public void onClick(DialogInterface dialog, int which)
{
dismiss();
+ if (!profileInfo.getBoolean(PROFILE_FOREGROUND))
+ { /* if the app was not in the foreground before this action was triggered
+ * externally, we just close the activity if canceled */
+ getActivity().finish();
+ }
}
}).create();
}