2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.vpndialogs;
19 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
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;
38 import com.android.internal.app.AlertActivity;
39 import com.android.internal.net.VpnConfig;
41 public class ConfirmDialog extends AlertActivity
42 implements DialogInterface.OnClickListener, ImageGetter {
43 private static final String TAG = "VpnConfirm";
45 private String mPackage;
47 private IConnectivityManager mService;
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 mPackage = getCallingPackage();
53 mService = IConnectivityManager.Stub.asInterface(
54 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
61 if (UserManager.get(this).hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) {
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)) {
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;
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);
88 private String getAlwaysOnVpnPackage() {
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
98 private boolean prepareVpn() {
100 return mService.prepareVpn(mPackage, null, UserHandle.myUserId());
101 } catch (RemoteException e) {
102 throw new IllegalStateException(e);
106 private CharSequence getVpnLabel() {
108 return VpnConfig.getVpnLabel(this, mPackage);
109 } catch (PackageManager.NameNotFoundException e) {
110 throw new IllegalStateException(e);
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());
123 public void onBackPressed() {
127 public void onClick(DialogInterface dialog, int which) {
129 if (mService.prepareVpn(null, mPackage, UserHandle.myUserId())) {
130 // Authorize this app to initiate VPN connections in the future without user
132 mService.setVpnPackageAuthorization(mPackage, UserHandle.myUserId(), true);
133 setResult(RESULT_OK);
135 } catch (Exception e) {
136 Log.e(TAG, "onClick", e);