VpnSettings: slightly more robust callback context
Test: runtest -x com/android/settings/vpn2/VpnTests.java
Change-Id: I45fa0509c56211602f6abd55a2f44cdf76f28829
diff --git a/src/com/android/settings/vpn2/ConfigDialogFragment.java b/src/com/android/settings/vpn2/ConfigDialogFragment.java
index 86245d4..d9f35af 100644
--- a/src/com/android/settings/vpn2/ConfigDialogFragment.java
+++ b/src/com/android/settings/vpn2/ConfigDialogFragment.java
@@ -55,6 +55,7 @@
private final IConnectivityManager mService = IConnectivityManager.Stub.asInterface(
ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
+ private Context mContext;
private boolean mUnlocking = false;
@@ -79,6 +80,12 @@
}
@Override
+ public void onAttach(final Context context) {
+ super.onAttach(context);
+ mContext = context;
+ }
+
+ @Override
public void onResume() {
super.onResume();
@@ -86,7 +93,7 @@
if (!KeyStore.getInstance().isUnlocked()) {
if (!mUnlocking) {
// Let us unlock KeyStore. See you later!
- Credentials.getInstance().unlock(getActivity());
+ Credentials.getInstance().unlock(mContext);
} else {
// We already tried, but it is still not working!
dismiss();
@@ -142,9 +149,9 @@
// Possibly throw up a dialog to explain lockdown VPN.
final boolean shouldLockdown = dialog.isVpnAlwaysOn();
final boolean shouldConnect = shouldLockdown || !dialog.isEditing();
- final boolean wasAlwaysOn = VpnUtils.isAlwaysOnOrLegacyLockdownActive(getContext());
+ final boolean wasAlwaysOn = VpnUtils.isAlwaysOnOrLegacyLockdownActive(mContext);
try {
- final boolean replace = VpnUtils.isVpnActive(getContext());
+ final boolean replace = VpnUtils.isVpnActive(mContext);
if (shouldConnect && !isConnected(profile) &&
ConfirmLockdownFragment.shouldShow(replace, wasAlwaysOn, shouldLockdown)) {
final Bundle opts = new Bundle();
@@ -185,19 +192,19 @@
if (isVpnAlwaysOn) {
// Show toast if vpn profile is not valid
if (!profile.isValidLockdownProfile()) {
- Toast.makeText(getContext(), R.string.vpn_lockdown_config_error,
+ Toast.makeText(mContext, R.string.vpn_lockdown_config_error,
Toast.LENGTH_LONG).show();
return;
}
- final ConnectivityManager conn = ConnectivityManager.from(getActivity());
+ final ConnectivityManager conn = ConnectivityManager.from(mContext);
conn.setAlwaysOnVpnPackageForUser(UserHandle.myUserId(), null,
/* lockdownEnabled */ false);
- VpnUtils.setLockdownVpn(getContext(), profile.key);
+ VpnUtils.setLockdownVpn(mContext, profile.key);
} else {
// update only if lockdown vpn has been changed
if (VpnUtils.isVpnLockdown(profile.key)) {
- VpnUtils.clearLockdownVpn(getContext());
+ VpnUtils.clearLockdownVpn(mContext);
}
}
}
@@ -219,11 +226,11 @@
// Now try to start the VPN - this is not necessary if the profile is set as lockdown,
// because just saving the profile in this mode will start a connection.
if (!VpnUtils.isVpnLockdown(profile.key)) {
- VpnUtils.clearLockdownVpn(getContext());
+ VpnUtils.clearLockdownVpn(mContext);
try {
mService.startLegacyVpn(profile);
} catch (IllegalStateException e) {
- Toast.makeText(getActivity(), R.string.vpn_no_network, Toast.LENGTH_LONG).show();
+ Toast.makeText(mContext, R.string.vpn_no_network, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
Log.e(TAG, "Failed to connect", e);
}
@@ -241,7 +248,7 @@
if (!isConnected(profile)) {
return true;
}
- VpnUtils.clearLockdownVpn(getContext());
+ VpnUtils.clearLockdownVpn(mContext);
return mService.prepareVpn(null, VpnConfig.LEGACY_VPN, UserHandle.myUserId());
} catch (RemoteException e) {
Log.e(TAG, "Failed to disconnect", e);