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.net.IConnectivityManager;
25import android.os.ServiceManager;
26import android.util.Log;
27import android.view.View;
28import android.widget.Button;
29import android.widget.CompoundButton;
30import android.widget.ImageView;
31import android.widget.TextView;
32
33import com.android.internal.app.AlertActivity;
34
35public class ConfirmDialog extends AlertActivity implements
36        CompoundButton.OnCheckedChangeListener, DialogInterface.OnClickListener {
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
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)) {
55                setResult(RESULT_OK);
56                finish();
57                return;
58            }
59
60            PackageManager pm = getPackageManager();
61            ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
62
63            View view = View.inflate(this, R.layout.confirm, null);
64            ((ImageView) view.findViewById(R.id.icon)).setImageDrawable(app.loadIcon(pm));
65            ((TextView) view.findViewById(R.id.prompt)).setText(
66                    getString(R.string.prompt, app.loadLabel(pm)));
67            ((CompoundButton) view.findViewById(R.id.check)).setOnCheckedChangeListener(this);
68
69            mAlertParams.mIconAttrId = android.R.attr.alertDialogIcon;
70            mAlertParams.mTitle = getText(android.R.string.dialog_alert_title);
71            mAlertParams.mPositiveButtonText = getText(android.R.string.ok);
72            mAlertParams.mPositiveButtonListener = this;
73            mAlertParams.mNegativeButtonText = getText(android.R.string.cancel);
74            mAlertParams.mNegativeButtonListener = this;
75            mAlertParams.mView = view;
76            setupAlert();
77
78            getWindow().setCloseOnTouchOutside(false);
79            mButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
80            mButton.setEnabled(false);
81            mButton.setFilterTouchesWhenObscured(true);
82        } catch (Exception e) {
83            Log.e(TAG, "onResume", e);
84            finish();
85        }
86    }
87
88    @Override
89    public void onBackPressed() {
90    }
91
92    @Override
93    public void onCheckedChanged(CompoundButton button, boolean checked) {
94        mButton.setEnabled(checked);
95    }
96
97    @Override
98    public void onClick(DialogInterface dialog, int which) {
99        try {
100            if (which == DialogInterface.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) {
101                setResult(RESULT_OK);
102            }
103        } catch (Exception e) {
104            Log.e(TAG, "onClick", e);
105        }
106    }
107}
108