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