blob: 80f9fcd2d498ae464d08a52c66b10202d952a402 [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;
23import android.net.IConnectivityManager;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.os.ServiceManager;
Robin Lee01b35bc2015-05-12 18:35:37 +010027import android.os.UserHandle;
Robin Lee2bd92d52015-04-09 17:13:08 +010028import android.security.Credentials;
29import android.security.KeyStore;
30import android.util.Log;
31import android.widget.Toast;
32
33import com.android.internal.net.LegacyVpnInfo;
34import com.android.internal.net.VpnConfig;
35import com.android.internal.net.VpnProfile;
36import com.android.settings.R;
37
38/**
39 * Fragment wrapper around a {@link ConfigDialog}.
40 */
41public class ConfigDialogFragment extends DialogFragment implements
42 DialogInterface.OnClickListener {
43 private static final String TAG_CONFIG_DIALOG = "vpnconfigdialog";
44 private static final String TAG = "ConfigDialogFragment";
45
46 private static final String ARG_PROFILE = "profile";
47 private static final String ARG_EDITING = "editing";
48 private static final String ARG_EXISTS = "exists";
49
50 private final IConnectivityManager mService = IConnectivityManager.Stub.asInterface(
51 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
52
53 private boolean mUnlocking = false;
54
55 public static void show(VpnSettings parent, VpnProfile profile, boolean edit, boolean exists) {
56 if (!parent.isAdded()) return;
57
58 Bundle args = new Bundle();
59 args.putParcelable(ARG_PROFILE, profile);
60 args.putBoolean(ARG_EDITING, edit);
61 args.putBoolean(ARG_EXISTS, exists);
62
63 final ConfigDialogFragment frag = new ConfigDialogFragment();
64 frag.setArguments(args);
65 frag.setTargetFragment(parent, 0);
66 frag.show(parent.getFragmentManager(), TAG_CONFIG_DIALOG);
67 }
68
69 @Override
70 public void onResume() {
71 super.onResume();
72
73 // Check KeyStore here, so others do not need to deal with it.
74 if (!KeyStore.getInstance().isUnlocked()) {
75 if (!mUnlocking) {
76 // Let us unlock KeyStore. See you later!
77 Credentials.getInstance().unlock(getActivity());
78 } else {
79 // We already tried, but it is still not working!
80 dismiss();
81 }
82 mUnlocking = !mUnlocking;
83 return;
84 }
85
86 // Now KeyStore is always unlocked. Reset the flag.
87 mUnlocking = false;
88 }
89
90 @Override
91 public Dialog onCreateDialog(Bundle savedInstanceState) {
92 Bundle args = getArguments();
93 VpnProfile profile = (VpnProfile) args.getParcelable(ARG_PROFILE);
94 boolean editing = args.getBoolean(ARG_EDITING);
95 boolean exists = args.getBoolean(ARG_EXISTS);
96
97 return new ConfigDialog(getActivity(), this, profile, editing, exists);
98 }
99
100 @Override
101 public void onClick(DialogInterface dialogInterface, int button) {
102 ConfigDialog dialog = (ConfigDialog) getDialog();
103 VpnProfile profile = dialog.getProfile();
104
105 if (button == DialogInterface.BUTTON_POSITIVE) {
106 // Update KeyStore entry
107 KeyStore.getInstance().put(Credentials.VPN + profile.key, profile.encode(),
108 KeyStore.UID_SELF, KeyStore.FLAG_ENCRYPTED);
109
110 // Flush out old version of profile
111 disconnect(profile);
112
113 // If we are not editing, connect!
114 if (!dialog.isEditing()) {
115 try {
116 connect(profile);
117 } catch (RemoteException e) {
118 Log.e(TAG, "Failed to connect", e);
119 }
120 }
121 } else if (button == DialogInterface.BUTTON_NEUTRAL) {
122 // Disable profile if connected
123 disconnect(profile);
124
125 // Delete from KeyStore
126 KeyStore.getInstance().delete(Credentials.VPN + profile.key, KeyStore.UID_SELF);
127 }
128 dismiss();
129 }
130
131 @Override
Robin Lee2bd92d52015-04-09 17:13:08 +0100132 public void onCancel(DialogInterface dialog) {
133 dismiss();
134 super.onCancel(dialog);
135 }
136
137 private void connect(VpnProfile profile) throws RemoteException {
138 try {
139 mService.startLegacyVpn(profile);
140 } catch (IllegalStateException e) {
141 Toast.makeText(getActivity(), R.string.vpn_no_network, Toast.LENGTH_LONG).show();
142 }
143 }
144
145 private void disconnect(VpnProfile profile) {
146 try {
Robin Lee290784a2015-07-07 12:33:45 -0700147 LegacyVpnInfo connected = mService.getLegacyVpnInfo(UserHandle.myUserId());
Robin Lee2bd92d52015-04-09 17:13:08 +0100148 if (connected != null && profile.key.equals(connected.key)) {
Robin Lee01b35bc2015-05-12 18:35:37 +0100149 mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN,
150 UserHandle.myUserId());
Robin Lee2bd92d52015-04-09 17:13:08 +0100151 }
152 } catch (RemoteException e) {
153 Log.e(TAG, "Failed to disconnect", e);
154 }
155 }
156}