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