| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings.vpn2; |
| 18 | |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 20 | import android.app.Dialog; |
| 21 | import android.app.DialogFragment; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 25 | import android.net.IConnectivityManager; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
| Robin Lee | 01b35bc | 2015-05-12 18:35:37 +0100 | [diff] [blame] | 29 | import android.os.UserHandle; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 30 | import android.security.Credentials; |
| 31 | import android.security.KeyStore; |
| 32 | import android.util.Log; |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 33 | import android.view.View; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 34 | import android.widget.Toast; |
| 35 | |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 36 | import com.android.internal.logging.MetricsProto; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 37 | import com.android.internal.net.LegacyVpnInfo; |
| 38 | import com.android.internal.net.VpnConfig; |
| 39 | import com.android.internal.net.VpnProfile; |
| 40 | import com.android.settings.R; |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 41 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Fragment wrapper around a {@link ConfigDialog}. |
| 45 | */ |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 46 | public class ConfigDialogFragment extends InstrumentedDialogFragment implements |
| 47 | DialogInterface.OnClickListener, DialogInterface.OnShowListener, View.OnClickListener, |
| 48 | ConfirmLockdownFragment.ConfirmLockdownListener { |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 49 | private static final String TAG_CONFIG_DIALOG = "vpnconfigdialog"; |
| 50 | private static final String TAG = "ConfigDialogFragment"; |
| 51 | |
| 52 | private static final String ARG_PROFILE = "profile"; |
| 53 | private static final String ARG_EDITING = "editing"; |
| 54 | private static final String ARG_EXISTS = "exists"; |
| 55 | |
| 56 | private final IConnectivityManager mService = IConnectivityManager.Stub.asInterface( |
| 57 | ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); |
| 58 | |
| 59 | private boolean mUnlocking = false; |
| 60 | |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 61 | |
| 62 | @Override |
| 63 | public int getMetricsCategory() { |
| 64 | return MetricsProto.MetricsEvent.DIALOG_LEGACY_VPN_CONFIG; |
| 65 | } |
| 66 | |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 67 | public static void show(VpnSettings parent, VpnProfile profile, boolean edit, boolean exists) { |
| 68 | if (!parent.isAdded()) return; |
| 69 | |
| 70 | Bundle args = new Bundle(); |
| 71 | args.putParcelable(ARG_PROFILE, profile); |
| 72 | args.putBoolean(ARG_EDITING, edit); |
| 73 | args.putBoolean(ARG_EXISTS, exists); |
| 74 | |
| 75 | final ConfigDialogFragment frag = new ConfigDialogFragment(); |
| 76 | frag.setArguments(args); |
| 77 | frag.setTargetFragment(parent, 0); |
| 78 | frag.show(parent.getFragmentManager(), TAG_CONFIG_DIALOG); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onResume() { |
| 83 | super.onResume(); |
| 84 | |
| 85 | // Check KeyStore here, so others do not need to deal with it. |
| 86 | if (!KeyStore.getInstance().isUnlocked()) { |
| 87 | if (!mUnlocking) { |
| 88 | // Let us unlock KeyStore. See you later! |
| 89 | Credentials.getInstance().unlock(getActivity()); |
| 90 | } else { |
| 91 | // We already tried, but it is still not working! |
| 92 | dismiss(); |
| 93 | } |
| 94 | mUnlocking = !mUnlocking; |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // Now KeyStore is always unlocked. Reset the flag. |
| 99 | mUnlocking = false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 104 | Bundle args = getArguments(); |
| 105 | VpnProfile profile = (VpnProfile) args.getParcelable(ARG_PROFILE); |
| 106 | boolean editing = args.getBoolean(ARG_EDITING); |
| 107 | boolean exists = args.getBoolean(ARG_EXISTS); |
| 108 | |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 109 | final Dialog dialog = new ConfigDialog(getActivity(), this, profile, editing, exists); |
| 110 | dialog.setOnShowListener(this); |
| 111 | return dialog; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Override for the default onClick handler which also calls dismiss(). |
| 116 | * |
| 117 | * @see DialogInterface.OnClickListener#onClick(DialogInterface, int) |
| 118 | */ |
| 119 | @Override |
| 120 | public void onShow(DialogInterface dialogInterface) { |
| 121 | ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onClick(View positiveButton) { |
| 126 | onClick(getDialog(), AlertDialog.BUTTON_POSITIVE); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void onConfirmLockdown(Bundle options, boolean isEnabled) { |
| 131 | VpnProfile profile = (VpnProfile) options.getParcelable(ARG_PROFILE); |
| 132 | connect(profile, isEnabled); |
| 133 | dismiss(); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onClick(DialogInterface dialogInterface, int button) { |
| 138 | ConfigDialog dialog = (ConfigDialog) getDialog(); |
| 139 | VpnProfile profile = dialog.getProfile(); |
| 140 | |
| 141 | if (button == DialogInterface.BUTTON_POSITIVE) { |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 142 | // Possibly throw up a dialog to explain lockdown VPN. |
| 143 | final boolean shouldLockdown = dialog.isVpnAlwaysOn(); |
| 144 | final boolean shouldConnect = shouldLockdown || !dialog.isEditing(); |
| 145 | final boolean wasAlwaysOn = VpnUtils.isAlwaysOnOrLegacyLockdownActive(getContext()); |
| 146 | try { |
| 147 | final boolean replace = VpnUtils.isVpnActive(getContext()); |
| 148 | if (shouldConnect && !isConnected(profile) && |
| 149 | ConfirmLockdownFragment.shouldShow(replace, wasAlwaysOn, shouldLockdown)) { |
| 150 | final Bundle opts = new Bundle(); |
| 151 | opts.putParcelable(ARG_PROFILE, profile); |
| 152 | ConfirmLockdownFragment.show(this, replace, wasAlwaysOn, shouldLockdown, opts); |
| 153 | } else if (shouldConnect) { |
| 154 | connect(profile, shouldLockdown); |
| 155 | } else { |
| 156 | save(profile, false); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 157 | } |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 158 | } catch (RemoteException e) { |
| 159 | Log.w(TAG, "Failed to check active VPN state. Skipping.", e); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 160 | } |
| 161 | } else if (button == DialogInterface.BUTTON_NEUTRAL) { |
| 162 | // Disable profile if connected |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 163 | if (!disconnect(profile)) { |
| 164 | Log.e(TAG, "Failed to disconnect VPN. Leaving profile in keystore."); |
| 165 | return; |
| 166 | } |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 167 | |
| 168 | // Delete from KeyStore |
| Lorenzo Colitti | c311c94 | 2015-10-13 15:23:38 +0900 | [diff] [blame] | 169 | KeyStore keyStore = KeyStore.getInstance(); |
| 170 | keyStore.delete(Credentials.VPN + profile.key, KeyStore.UID_SELF); |
| 171 | |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 172 | updateLockdownVpn(false, profile); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 173 | } |
| 174 | dismiss(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 178 | public void onCancel(DialogInterface dialog) { |
| 179 | dismiss(); |
| 180 | super.onCancel(dialog); |
| 181 | } |
| 182 | |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 183 | private void updateLockdownVpn(boolean isVpnAlwaysOn, VpnProfile profile) { |
| 184 | // Save lockdown vpn |
| 185 | if (isVpnAlwaysOn) { |
| 186 | // Show toast if vpn profile is not valid |
| 187 | if (!profile.isValidLockdownProfile()) { |
| 188 | Toast.makeText(getContext(), R.string.vpn_lockdown_config_error, |
| 189 | Toast.LENGTH_LONG).show(); |
| 190 | return; |
| 191 | } |
| 192 | |
| Robin Lee | 20ddd1c | 2016-04-12 17:56:38 +0100 | [diff] [blame] | 193 | final ConnectivityManager conn = ConnectivityManager.from(getActivity()); |
| Robin Lee | cdebe28 | 2016-05-03 13:27:05 +0100 | [diff] [blame] | 194 | conn.setAlwaysOnVpnPackageForUser(UserHandle.myUserId(), null, |
| 195 | /* lockdownEnabled */ false); |
| Robin Lee | 20ddd1c | 2016-04-12 17:56:38 +0100 | [diff] [blame] | 196 | VpnUtils.setLockdownVpn(getContext(), profile.key); |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 197 | } else { |
| 198 | // update only if lockdown vpn has been changed |
| 199 | if (VpnUtils.isVpnLockdown(profile.key)) { |
| 200 | VpnUtils.clearLockdownVpn(getContext()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| Robin Lee | b6f787c | 2016-07-05 10:21:28 +0100 | [diff] [blame] | 205 | private void save(VpnProfile profile, boolean lockdown) { |
| 206 | KeyStore.getInstance().put(Credentials.VPN + profile.key, profile.encode(), |
| 207 | KeyStore.UID_SELF, /* flags */ 0); |
| 208 | |
| 209 | // Flush out old version of profile |
| 210 | disconnect(profile); |
| 211 | |
| 212 | // Notify lockdown VPN that the profile has changed. |
| 213 | updateLockdownVpn(lockdown, profile); |
| 214 | } |
| 215 | |
| 216 | private void connect(VpnProfile profile, boolean lockdown) { |
| 217 | save(profile, lockdown); |
| 218 | |
| 219 | // Now try to start the VPN - this is not necessary if the profile is set as lockdown, |
| 220 | // because just saving the profile in this mode will start a connection. |
| 221 | if (!VpnUtils.isVpnLockdown(profile.key)) { |
| 222 | VpnUtils.clearLockdownVpn(getContext()); |
| 223 | try { |
| 224 | mService.startLegacyVpn(profile); |
| 225 | } catch (IllegalStateException e) { |
| 226 | Toast.makeText(getActivity(), R.string.vpn_no_network, Toast.LENGTH_LONG).show(); |
| 227 | } catch (RemoteException e) { |
| 228 | Log.e(TAG, "Failed to connect", e); |
| 229 | } |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 233 | /** |
| 234 | * Ensure that the VPN profile pointed at by {@param profile} is disconnected. |
| 235 | * |
| 236 | * @return {@code true} iff this VPN profile is no longer connected. Note that another profile |
| 237 | * may still be active - this function will then do nothing but still return success. |
| 238 | */ |
| 239 | private boolean disconnect(VpnProfile profile) { |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 240 | try { |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 241 | if (!isConnected(profile)) { |
| 242 | return true; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 243 | } |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 244 | VpnUtils.clearLockdownVpn(getContext()); |
| 245 | return mService.prepareVpn(null, VpnConfig.LEGACY_VPN, UserHandle.myUserId()); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 246 | } catch (RemoteException e) { |
| 247 | Log.e(TAG, "Failed to disconnect", e); |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 248 | return false; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 251 | |
| 252 | private boolean isConnected(VpnProfile profile) throws RemoteException { |
| 253 | LegacyVpnInfo connected = mService.getLegacyVpnInfo(UserHandle.myUserId()); |
| 254 | return connected != null && profile.key.equals(connected.key); |
| 255 | } |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 256 | } |