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 android.content.Context;
20 import android.content.DialogInterface;
21 import android.graphics.drawable.Drawable;
22 import android.net.IConnectivityManager;
23 import android.os.ServiceManager;
24 import android.os.UserHandle;
25 import android.text.Html;
26 import android.text.Html.ImageGetter;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.Button;
30 import android.widget.TextView;
31 
32 import com.android.internal.app.AlertActivity;
33 import com.android.internal.net.VpnConfig;
34 
35 public class ConfirmDialog extends AlertActivity
36         implements DialogInterface.OnClickListener, ImageGetter {
37     private static final String TAG = "VpnConfirm";
38 
39     private String mPackage;
40 
41     private IConnectivityManager mService;
42 
43     private Button mButton;
44 
45     @Override
onResume()46     protected void onResume() {
47         super.onResume();
48         try {
49             mPackage = getCallingPackage();
50 
51             mService = IConnectivityManager.Stub.asInterface(
52                     ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
53 
54             if (mService.prepareVpn(mPackage, null, UserHandle.myUserId())) {
55                 setResult(RESULT_OK);
56                 finish();
57                 return;
58             }
59 
60             View view = View.inflate(this, R.layout.confirm, null);
61 
62             ((TextView) view.findViewById(R.id.warning)).setText(
63                     Html.fromHtml(
64                             getString(R.string.warning, VpnConfig.getVpnLabel(this, mPackage)),
65                     this, null /* tagHandler */));
66 
67             mAlertParams.mTitle = getText(R.string.prompt);
68             mAlertParams.mPositiveButtonText = getText(android.R.string.ok);
69             mAlertParams.mPositiveButtonListener = this;
70             mAlertParams.mNegativeButtonText = getText(android.R.string.cancel);
71             mAlertParams.mView = view;
72             setupAlert();
73 
74             getWindow().setCloseOnTouchOutside(false);
75             mButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
76             mButton.setFilterTouchesWhenObscured(true);
77         } catch (Exception e) {
78             Log.e(TAG, "onResume", e);
79             finish();
80         }
81     }
82 
83     @Override
getDrawable(String source)84     public Drawable getDrawable(String source) {
85         // Should only reach this when fetching the VPN icon for the warning string.
86         Drawable icon = getDrawable(R.drawable.ic_vpn_dialog);
87         icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
88         return icon;
89     }
90 
91     @Override
onBackPressed()92     public void onBackPressed() {
93     }
94 
95     @Override
onClick(DialogInterface dialog, int which)96     public void onClick(DialogInterface dialog, int which) {
97         try {
98             if (mService.prepareVpn(null, mPackage, UserHandle.myUserId())) {
99                 // Authorize this app to initiate VPN connections in the future without user
100                 // intervention.
101                 mService.setVpnPackageAuthorization(mPackage, UserHandle.myUserId(), true);
102                 setResult(RESULT_OK);
103             }
104         } catch (Exception e) {
105             Log.e(TAG, "onClick", e);
106         }
107     }
108 }
109