| 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 | |
| 19 | import android.app.Dialog; |
| 20 | import android.app.DialogFragment; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 23 | import android.net.ConnectivityManager; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 24 | import android.net.IConnectivityManager; |
| 25 | import android.os.Bundle; |
| 26 | import android.os.RemoteException; |
| 27 | import android.os.ServiceManager; |
| Robin Lee | 01b35bc | 2015-05-12 18:35:37 +0100 | [diff] [blame] | 28 | import android.os.UserHandle; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 29 | import android.security.Credentials; |
| 30 | import android.security.KeyStore; |
| 31 | import android.util.Log; |
| 32 | import android.widget.Toast; |
| 33 | |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame^] | 34 | import com.android.internal.logging.MetricsProto; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 35 | import com.android.internal.net.LegacyVpnInfo; |
| 36 | import com.android.internal.net.VpnConfig; |
| 37 | import com.android.internal.net.VpnProfile; |
| 38 | import com.android.settings.R; |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame^] | 39 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Fragment wrapper around a {@link ConfigDialog}. |
| 43 | */ |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame^] | 44 | public class ConfigDialogFragment extends InstrumentedDialogFragment |
| 45 | implements DialogInterface.OnClickListener { |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 46 | private static final String TAG_CONFIG_DIALOG = "vpnconfigdialog"; |
| 47 | private static final String TAG = "ConfigDialogFragment"; |
| 48 | |
| 49 | private static final String ARG_PROFILE = "profile"; |
| 50 | private static final String ARG_EDITING = "editing"; |
| 51 | private static final String ARG_EXISTS = "exists"; |
| 52 | |
| 53 | private final IConnectivityManager mService = IConnectivityManager.Stub.asInterface( |
| 54 | ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); |
| 55 | |
| 56 | private boolean mUnlocking = false; |
| 57 | |
| Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame^] | 58 | |
| 59 | @Override |
| 60 | public int getMetricsCategory() { |
| 61 | return MetricsProto.MetricsEvent.DIALOG_LEGACY_VPN_CONFIG; |
| 62 | } |
| 63 | |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 64 | public static void show(VpnSettings parent, VpnProfile profile, boolean edit, boolean exists) { |
| 65 | if (!parent.isAdded()) return; |
| 66 | |
| 67 | Bundle args = new Bundle(); |
| 68 | args.putParcelable(ARG_PROFILE, profile); |
| 69 | args.putBoolean(ARG_EDITING, edit); |
| 70 | args.putBoolean(ARG_EXISTS, exists); |
| 71 | |
| 72 | final ConfigDialogFragment frag = new ConfigDialogFragment(); |
| 73 | frag.setArguments(args); |
| 74 | frag.setTargetFragment(parent, 0); |
| 75 | frag.show(parent.getFragmentManager(), TAG_CONFIG_DIALOG); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onResume() { |
| 80 | super.onResume(); |
| 81 | |
| 82 | // Check KeyStore here, so others do not need to deal with it. |
| 83 | if (!KeyStore.getInstance().isUnlocked()) { |
| 84 | if (!mUnlocking) { |
| 85 | // Let us unlock KeyStore. See you later! |
| 86 | Credentials.getInstance().unlock(getActivity()); |
| 87 | } else { |
| 88 | // We already tried, but it is still not working! |
| 89 | dismiss(); |
| 90 | } |
| 91 | mUnlocking = !mUnlocking; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // Now KeyStore is always unlocked. Reset the flag. |
| 96 | mUnlocking = false; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 101 | Bundle args = getArguments(); |
| 102 | VpnProfile profile = (VpnProfile) args.getParcelable(ARG_PROFILE); |
| 103 | boolean editing = args.getBoolean(ARG_EDITING); |
| 104 | boolean exists = args.getBoolean(ARG_EXISTS); |
| 105 | |
| 106 | return new ConfigDialog(getActivity(), this, profile, editing, exists); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onClick(DialogInterface dialogInterface, int button) { |
| 111 | ConfigDialog dialog = (ConfigDialog) getDialog(); |
| 112 | VpnProfile profile = dialog.getProfile(); |
| 113 | |
| 114 | if (button == DialogInterface.BUTTON_POSITIVE) { |
| 115 | // Update KeyStore entry |
| 116 | KeyStore.getInstance().put(Credentials.VPN + profile.key, profile.encode(), |
| Robin Lee | 4c85639 | 2015-12-17 11:58:24 +0000 | [diff] [blame] | 117 | KeyStore.UID_SELF, /* flags */ 0); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 118 | |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 119 | // Flush out previous connection, which may be an old version of the profile |
| 120 | if (!disconnect(profile)) { |
| 121 | Log.w(TAG, "Unable to remove previous connection. Continuing anyway."); |
| 122 | } |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 123 | |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 124 | updateLockdownVpn(dialog.isVpnAlwaysOn(), profile); |
| 125 | |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 126 | // If we are not editing, connect! |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 127 | if (!dialog.isEditing() && !VpnUtils.isVpnLockdown(profile.key)) { |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 128 | try { |
| 129 | connect(profile); |
| 130 | } catch (RemoteException e) { |
| 131 | Log.e(TAG, "Failed to connect", e); |
| 132 | } |
| 133 | } |
| 134 | } else if (button == DialogInterface.BUTTON_NEUTRAL) { |
| 135 | // Disable profile if connected |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 136 | if (!disconnect(profile)) { |
| 137 | Log.e(TAG, "Failed to disconnect VPN. Leaving profile in keystore."); |
| 138 | return; |
| 139 | } |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 140 | |
| 141 | // Delete from KeyStore |
| Lorenzo Colitti | c311c94 | 2015-10-13 15:23:38 +0900 | [diff] [blame] | 142 | KeyStore keyStore = KeyStore.getInstance(); |
| 143 | keyStore.delete(Credentials.VPN + profile.key, KeyStore.UID_SELF); |
| 144 | |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 145 | updateLockdownVpn(false, profile); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 146 | } |
| 147 | dismiss(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 151 | public void onCancel(DialogInterface dialog) { |
| 152 | dismiss(); |
| 153 | super.onCancel(dialog); |
| 154 | } |
| 155 | |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 156 | private void updateLockdownVpn(boolean isVpnAlwaysOn, VpnProfile profile) { |
| 157 | // Save lockdown vpn |
| 158 | if (isVpnAlwaysOn) { |
| 159 | // Show toast if vpn profile is not valid |
| 160 | if (!profile.isValidLockdownProfile()) { |
| 161 | Toast.makeText(getContext(), R.string.vpn_lockdown_config_error, |
| 162 | Toast.LENGTH_LONG).show(); |
| 163 | return; |
| 164 | } |
| 165 | |
| Robin Lee | 20ddd1c | 2016-04-12 17:56:38 +0100 | [diff] [blame] | 166 | final ConnectivityManager conn = ConnectivityManager.from(getActivity()); |
| Robin Lee | cdebe28 | 2016-05-03 13:27:05 +0100 | [diff] [blame] | 167 | conn.setAlwaysOnVpnPackageForUser(UserHandle.myUserId(), null, |
| 168 | /* lockdownEnabled */ false); |
| Robin Lee | 20ddd1c | 2016-04-12 17:56:38 +0100 | [diff] [blame] | 169 | VpnUtils.setLockdownVpn(getContext(), profile.key); |
| Victor Chang | 6005aef | 2016-03-17 20:58:50 +0000 | [diff] [blame] | 170 | } else { |
| 171 | // update only if lockdown vpn has been changed |
| 172 | if (VpnUtils.isVpnLockdown(profile.key)) { |
| 173 | VpnUtils.clearLockdownVpn(getContext()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 178 | private void connect(VpnProfile profile) throws RemoteException { |
| 179 | try { |
| 180 | mService.startLegacyVpn(profile); |
| 181 | } catch (IllegalStateException e) { |
| 182 | Toast.makeText(getActivity(), R.string.vpn_no_network, Toast.LENGTH_LONG).show(); |
| 183 | } |
| 184 | } |
| 185 | |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 186 | /** |
| 187 | * Ensure that the VPN profile pointed at by {@param profile} is disconnected. |
| 188 | * |
| 189 | * @return {@code true} iff this VPN profile is no longer connected. Note that another profile |
| 190 | * may still be active - this function will then do nothing but still return success. |
| 191 | */ |
| 192 | private boolean disconnect(VpnProfile profile) { |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 193 | try { |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 194 | if (!isConnected(profile)) { |
| 195 | return true; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 196 | } |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 197 | VpnUtils.clearLockdownVpn(getContext()); |
| 198 | return mService.prepareVpn(null, VpnConfig.LEGACY_VPN, UserHandle.myUserId()); |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 199 | } catch (RemoteException e) { |
| 200 | Log.e(TAG, "Failed to disconnect", e); |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 201 | return false; |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 202 | } |
| 203 | } |
| Robin Lee | 23e53b3 | 2016-07-29 12:12:15 +0100 | [diff] [blame] | 204 | |
| 205 | private boolean isConnected(VpnProfile profile) throws RemoteException { |
| 206 | LegacyVpnInfo connected = mService.getLegacyVpnInfo(UserHandle.myUserId()); |
| 207 | return connected != null && profile.key.equals(connected.key); |
| 208 | } |
| Robin Lee | 2bd92d5 | 2015-04-09 17:13:08 +0100 | [diff] [blame] | 209 | } |