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