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.app.Activity;
20import android.app.AlertDialog;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.net.IConnectivityManager;
27import android.os.ServiceManager;
28import android.util.Log;
29import android.view.View;
30import android.widget.Button;
31import android.widget.CompoundButton;
32import android.widget.ImageView;
33import android.widget.TextView;
34
35public class ConfirmDialog extends Activity implements CompoundButton.OnCheckedChangeListener,
36        DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
37    private static final String TAG = "VpnConfirm";
38
39    private String mPackage;
40
41    private IConnectivityManager mService;
42
43    private AlertDialog mDialog;
44    private Button mButton;
45
46    @Override
47    protected void onResume() {
48        super.onResume();
49        try {
50            mPackage = getCallingPackage();
51
52            mService = IConnectivityManager.Stub.asInterface(
53                    ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
54
55            if (mService.prepareVpn(mPackage, null)) {
56                setResult(RESULT_OK);
57                finish();
58                return;
59            }
60
61            PackageManager pm = getPackageManager();
62            ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
63
64            View view = View.inflate(this, R.layout.confirm, null);
65            ((ImageView) view.findViewById(R.id.icon)).setImageDrawable(app.loadIcon(pm));
66            ((TextView) view.findViewById(R.id.prompt)).setText(
67                    getString(R.string.prompt, app.loadLabel(pm)));
68            ((CompoundButton) view.findViewById(R.id.check)).setOnCheckedChangeListener(this);
69
70            mDialog = new AlertDialog.Builder(this)
71                    .setIcon(android.R.drawable.ic_dialog_alert)
72                    .setTitle(android.R.string.dialog_alert_title)
73                    .setView(view)
74                    .setPositiveButton(android.R.string.ok, this)
75                    .setNegativeButton(android.R.string.cancel, this)
76                    .setCancelable(false)
77                    .create();
78            mDialog.setOnDismissListener(this);
79            mDialog.show();
80
81            mButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
82            mButton.setEnabled(false);
83        } catch (Exception e) {
84            Log.e(TAG, "onResume", e);
85            finish();
86        }
87    }
88
89    @Override
90    protected void onPause() {
91        super.onPause();
92        if (mDialog != null) {
93            mDialog.setOnDismissListener(null);
94            mDialog.dismiss();
95        }
96    }
97
98    @Override
99    public void onCheckedChanged(CompoundButton button, boolean checked) {
100        mButton.setEnabled(checked);
101    }
102
103    @Override
104    public void onClick(DialogInterface dialog, int which) {
105        try {
106            if (which == AlertDialog.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) {
107                setResult(RESULT_OK);
108            }
109        } catch (Exception e) {
110            Log.e(TAG, "onClick", e);
111        }
112    }
113
114    @Override
115    public void onDismiss(DialogInterface dialog) {
116        finish();
117    }
118}
119