android: Close activity when dialog is canceled if it was not visible before

onPause/onResume() won't work because onPause() is called right before
onNewIntent().
This commit is contained in:
Tobias Brunner 2017-06-30 09:46:56 +02:00
parent 1265b353d4
commit 6333a756ee
1 changed files with 42 additions and 8 deletions

View File

@ -69,8 +69,10 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
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()
@ -88,11 +90,11 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
if (START_PROFILE.equals(getIntent().getAction()))
{
startVpnProfile(getIntent());
startVpnProfile(getIntent(), false);
}
else if (DISCONNECT.equals(getIntent().getAction()))
{
disconnect();
disconnect(false);
}
}
};
@ -126,6 +128,20 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
}
}
@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
*/
@ -136,11 +152,11 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
if (START_PROFILE.equals(intent.getAction()))
{
startVpnProfile(intent);
startVpnProfile(intent, mIsVisible);
}
else if (DISCONNECT.equals(intent.getAction()))
{
disconnect();
disconnect(mIsVisible);
}
}
@ -245,6 +261,17 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
@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());
@ -258,6 +285,7 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
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);
@ -290,8 +318,9 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
* 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)
@ -305,7 +334,7 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
if (profile != null)
{
onVpnProfileSelected(profile);
startVpnProfile(profile, foreground);
}
else
{
@ -316,7 +345,7 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
/**
* Disconnect the current connection, if any (silently ignored if there is no connection).
*/
private void disconnect()
private void disconnect(boolean foreground)
{
removeFragmentByTag(DIALOG_TAG);
@ -324,11 +353,11 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
{
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;
}
}
@ -429,6 +458,11 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
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();
}