1 /* 2 * Copyright (C) 2011 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.vpndialogs; 18 19 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; 20 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.content.pm.PackageManager; 24 import android.graphics.drawable.Drawable; 25 import android.net.IConnectivityManager; 26 import android.os.Bundle; 27 import android.os.RemoteException; 28 import android.os.ServiceManager; 29 import android.os.UserHandle; 30 import android.os.UserManager; 31 import android.text.Html; 32 import android.text.Html.ImageGetter; 33 import android.util.Log; 34 import android.view.View; 35 import android.widget.Button; 36 import android.widget.TextView; 37 38 import com.android.internal.app.AlertActivity; 39 import com.android.internal.net.VpnConfig; 40 41 public class ConfirmDialog extends AlertActivity 42 implements DialogInterface.OnClickListener, ImageGetter { 43 private static final String TAG = "VpnConfirm"; 44 45 private String mPackage; 46 47 private IConnectivityManager mService; 48 49 @Override onCreate(Bundle savedInstanceState)50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 mPackage = getCallingPackage(); 53 mService = IConnectivityManager.Stub.asInterface( 54 ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); 55 56 if (prepareVpn()) { 57 setResult(RESULT_OK); 58 finish(); 59 return; 60 } 61 if (UserManager.get(this).hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) { 62 finish(); 63 return; 64 } 65 final String alwaysOnVpnPackage = getAlwaysOnVpnPackage(); 66 // Can't prepare new vpn app when another vpn is always-on 67 if (alwaysOnVpnPackage != null && !alwaysOnVpnPackage.equals(mPackage)) { 68 finish(); 69 return; 70 } 71 View view = View.inflate(this, R.layout.confirm, null); 72 ((TextView) view.findViewById(R.id.warning)).setText( 73 Html.fromHtml(getString(R.string.warning, getVpnLabel()), 74 this, null /* tagHandler */)); 75 mAlertParams.mTitle = getText(R.string.prompt); 76 mAlertParams.mPositiveButtonText = getText(android.R.string.ok); 77 mAlertParams.mPositiveButtonListener = this; 78 mAlertParams.mNegativeButtonText = getText(android.R.string.cancel); 79 mAlertParams.mView = view; 80 setupAlert(); 81 82 getWindow().setCloseOnTouchOutside(false); 83 getWindow().addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 84 Button button = mAlert.getButton(DialogInterface.BUTTON_POSITIVE); 85 button.setFilterTouchesWhenObscured(true); 86 } 87 getAlwaysOnVpnPackage()88 private String getAlwaysOnVpnPackage() { 89 try { 90 return mService.getAlwaysOnVpnPackage(UserHandle.myUserId()); 91 } catch (RemoteException e) { 92 Log.e(TAG, "fail to call getAlwaysOnVpnPackage", e); 93 // Fallback to null to show the dialog 94 return null; 95 } 96 } 97 prepareVpn()98 private boolean prepareVpn() { 99 try { 100 return mService.prepareVpn(mPackage, null, UserHandle.myUserId()); 101 } catch (RemoteException e) { 102 throw new IllegalStateException(e); 103 } 104 } 105 getVpnLabel()106 private CharSequence getVpnLabel() { 107 try { 108 return VpnConfig.getVpnLabel(this, mPackage); 109 } catch (PackageManager.NameNotFoundException e) { 110 throw new IllegalStateException(e); 111 } 112 } 113 114 @Override getDrawable(String source)115 public Drawable getDrawable(String source) { 116 // Should only reach this when fetching the VPN icon for the warning string. 117 Drawable icon = getDrawable(R.drawable.ic_vpn_dialog); 118 icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); 119 return icon; 120 } 121 122 @Override onBackPressed()123 public void onBackPressed() { 124 } 125 126 @Override onClick(DialogInterface dialog, int which)127 public void onClick(DialogInterface dialog, int which) { 128 try { 129 if (mService.prepareVpn(null, mPackage, UserHandle.myUserId())) { 130 // Authorize this app to initiate VPN connections in the future without user 131 // intervention. 132 mService.setVpnPackageAuthorization(mPackage, UserHandle.myUserId(), true); 133 setResult(RESULT_OK); 134 } 135 } catch (Exception e) { 136 Log.e(TAG, "onClick", e); 137 } 138 } 139 } 140