Merge "Be more defensive on failure to disconnect legacy VPNs"

This commit is contained in:
Robin Lee
2016-08-16 13:27:01 +00:00
committed by Android (Google) Code Review

View File

@@ -110,8 +110,10 @@ public class ConfigDialogFragment extends DialogFragment implements
KeyStore.getInstance().put(Credentials.VPN + profile.key, profile.encode(), KeyStore.getInstance().put(Credentials.VPN + profile.key, profile.encode(),
KeyStore.UID_SELF, /* flags */ 0); KeyStore.UID_SELF, /* flags */ 0);
// Flush out old version of profile // Flush out previous connection, which may be an old version of the profile
disconnect(profile); if (!disconnect(profile)) {
Log.w(TAG, "Unable to remove previous connection. Continuing anyway.");
}
updateLockdownVpn(dialog.isVpnAlwaysOn(), profile); updateLockdownVpn(dialog.isVpnAlwaysOn(), profile);
@@ -125,7 +127,10 @@ public class ConfigDialogFragment extends DialogFragment implements
} }
} else if (button == DialogInterface.BUTTON_NEUTRAL) { } else if (button == DialogInterface.BUTTON_NEUTRAL) {
// Disable profile if connected // Disable profile if connected
disconnect(profile); if (!disconnect(profile)) {
Log.e(TAG, "Failed to disconnect VPN. Leaving profile in keystore.");
return;
}
// Delete from KeyStore // Delete from KeyStore
KeyStore keyStore = KeyStore.getInstance(); KeyStore keyStore = KeyStore.getInstance();
@@ -172,16 +177,27 @@ public class ConfigDialogFragment extends DialogFragment implements
} }
} }
private void disconnect(VpnProfile profile) { /**
* Ensure that the VPN profile pointed at by {@param profile} is disconnected.
*
* @return {@code true} iff this VPN profile is no longer connected. Note that another profile
* may still be active - this function will then do nothing but still return success.
*/
private boolean disconnect(VpnProfile profile) {
try { try {
LegacyVpnInfo connected = mService.getLegacyVpnInfo(UserHandle.myUserId()); if (!isConnected(profile)) {
if (connected != null && profile.key.equals(connected.key)) { return true;
VpnUtils.clearLockdownVpn(getContext());
mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN,
UserHandle.myUserId());
} }
VpnUtils.clearLockdownVpn(getContext());
return mService.prepareVpn(null, VpnConfig.LEGACY_VPN, UserHandle.myUserId());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Failed to disconnect", e); Log.e(TAG, "Failed to disconnect", e);
return false;
} }
} }
private boolean isConnected(VpnProfile profile) throws RemoteException {
LegacyVpnInfo connected = mService.getLegacyVpnInfo(UserHandle.myUserId());
return connected != null && profile.key.equals(connected.key);
}
} }