blob: 1a180c571de6cb25f207aa8cc5f2af317b1f8943 [file] [log] [blame]
Robin Lee2bd92d52015-04-09 17:13:08 +01001/*
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
17package com.android.settings.vpn2;
18
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.content.Context;
22import android.content.DialogInterface;
Victor Chang6005aef2016-03-17 20:58:50 +000023import android.net.ConnectivityManager;
Robin Lee2bd92d52015-04-09 17:13:08 +010024import android.net.IConnectivityManager;
25import android.os.Bundle;
26import android.os.RemoteException;
27import android.os.ServiceManager;
Robin Lee01b35bc2015-05-12 18:35:37 +010028import android.os.UserHandle;
Robin Lee2bd92d52015-04-09 17:13:08 +010029import android.security.Credentials;
30import android.security.KeyStore;
31import android.util.Log;
32import android.widget.Toast;
33
Fan Zhang1e516282016-09-16 12:45:07 -070034import com.android.internal.logging.MetricsProto;
Robin Lee2bd92d52015-04-09 17:13:08 +010035import com.android.internal.net.LegacyVpnInfo;
36import com.android.internal.net.VpnConfig;
37import com.android.internal.net.VpnProfile;
38import com.android.settings.R;
Fan Zhang1e516282016-09-16 12:45:07 -070039import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Robin Lee2bd92d52015-04-09 17:13:08 +010040
41/**
42 * Fragment wrapper around a {@link ConfigDialog}.
43 */
Fan Zhang1e516282016-09-16 12:45:07 -070044public class ConfigDialogFragment extends InstrumentedDialogFragment
45 implements DialogInterface.OnClickListener {
Robin Lee2bd92d52015-04-09 17:13:08 +010046 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 Zhang1e516282016-09-16 12:45:07 -070058
59 @Override
60 public int getMetricsCategory() {
61 return MetricsProto.MetricsEvent.DIALOG_LEGACY_VPN_CONFIG;
62 }
63
Robin Lee2bd92d52015-04-09 17:13:08 +010064 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 Lee4c856392015-12-17 11:58:24 +0000117 KeyStore.UID_SELF, /* flags */ 0);
Robin Lee2bd92d52015-04-09 17:13:08 +0100118
Robin Lee23e53b32016-07-29 12:12:15 +0100119 // 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 Lee2bd92d52015-04-09 17:13:08 +0100123
Victor Chang6005aef2016-03-17 20:58:50 +0000124 updateLockdownVpn(dialog.isVpnAlwaysOn(), profile);
125
Robin Lee2bd92d52015-04-09 17:13:08 +0100126 // If we are not editing, connect!
Victor Chang6005aef2016-03-17 20:58:50 +0000127 if (!dialog.isEditing() && !VpnUtils.isVpnLockdown(profile.key)) {
Robin Lee2bd92d52015-04-09 17:13:08 +0100128 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 Lee23e53b32016-07-29 12:12:15 +0100136 if (!disconnect(profile)) {
137 Log.e(TAG, "Failed to disconnect VPN. Leaving profile in keystore.");
138 return;
139 }
Robin Lee2bd92d52015-04-09 17:13:08 +0100140
141 // Delete from KeyStore
Lorenzo Colittic311c942015-10-13 15:23:38 +0900142 KeyStore keyStore = KeyStore.getInstance();
143 keyStore.delete(Credentials.VPN + profile.key, KeyStore.UID_SELF);
144
Victor Chang6005aef2016-03-17 20:58:50 +0000145 updateLockdownVpn(false, profile);
Robin Lee2bd92d52015-04-09 17:13:08 +0100146 }
147 dismiss();
148 }
149
150 @Override
Robin Lee2bd92d52015-04-09 17:13:08 +0100151 public void onCancel(DialogInterface dialog) {
152 dismiss();
153 super.onCancel(dialog);
154 }
155
Victor Chang6005aef2016-03-17 20:58:50 +0000156 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 Lee20ddd1c2016-04-12 17:56:38 +0100166 final ConnectivityManager conn = ConnectivityManager.from(getActivity());
Robin Leecdebe282016-05-03 13:27:05 +0100167 conn.setAlwaysOnVpnPackageForUser(UserHandle.myUserId(), null,
168 /* lockdownEnabled */ false);
Robin Lee20ddd1c2016-04-12 17:56:38 +0100169 VpnUtils.setLockdownVpn(getContext(), profile.key);
Victor Chang6005aef2016-03-17 20:58:50 +0000170 } 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 Lee2bd92d52015-04-09 17:13:08 +0100178 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 Lee23e53b32016-07-29 12:12:15 +0100186 /**
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 Lee2bd92d52015-04-09 17:13:08 +0100193 try {
Robin Lee23e53b32016-07-29 12:12:15 +0100194 if (!isConnected(profile)) {
195 return true;
Robin Lee2bd92d52015-04-09 17:13:08 +0100196 }
Robin Lee23e53b32016-07-29 12:12:15 +0100197 VpnUtils.clearLockdownVpn(getContext());
198 return mService.prepareVpn(null, VpnConfig.LEGACY_VPN, UserHandle.myUserId());
Robin Lee2bd92d52015-04-09 17:13:08 +0100199 } catch (RemoteException e) {
200 Log.e(TAG, "Failed to disconnect", e);
Robin Lee23e53b32016-07-29 12:12:15 +0100201 return false;
Robin Lee2bd92d52015-04-09 17:13:08 +0100202 }
203 }
Robin Lee23e53b32016-07-29 12:12:15 +0100204
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 Lee2bd92d52015-04-09 17:13:08 +0100209}