blob: 6faf4e09fcbb1be6e948c1cd98e7f9ef7237e0a6 [file] [log] [blame]
Chia-chi Yeh19f054b2011-06-03 17:06:29 -07001/*
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
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070019import android.content.Context;
20import android.content.DialogInterface;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
23import android.content.pm.PackageManager;
Chia-chi Yehf530da62011-06-15 17:05:25 -070024import android.net.IConnectivityManager;
25import android.os.ServiceManager;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070026import android.util.Log;
27import android.view.View;
28import android.widget.Button;
29import android.widget.CompoundButton;
30import android.widget.ImageView;
31import android.widget.TextView;
32
Chia-chi Yehae380fb2012-01-23 18:33:26 -080033import com.android.internal.app.AlertActivity;
34
35public class ConfirmDialog extends AlertActivity implements
36 CompoundButton.OnCheckedChangeListener, DialogInterface.OnClickListener {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070037 private static final String TAG = "VpnConfirm";
38
Chia-chi Yeh100155a2011-07-03 16:52:38 -070039 private String mPackage;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070040
Chia-chi Yehf530da62011-06-15 17:05:25 -070041 private IConnectivityManager mService;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070042
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070043 private Button mButton;
44
45 @Override
46 protected void onResume() {
47 super.onResume();
48 try {
Chia-chi Yeh100155a2011-07-03 16:52:38 -070049 mPackage = getCallingPackage();
Chia-chi Yehf530da62011-06-15 17:05:25 -070050
51 mService = IConnectivityManager.Stub.asInterface(
52 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070053
Chia-chi Yeh100155a2011-07-03 16:52:38 -070054 if (mService.prepareVpn(mPackage, null)) {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070055 setResult(RESULT_OK);
56 finish();
57 return;
58 }
59
60 PackageManager pm = getPackageManager();
Chia-chi Yeh100155a2011-07-03 16:52:38 -070061 ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070062
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
Björn Lundén1129a852012-09-20 13:58:51 +020069 mAlertParams.mIconAttrId = android.R.attr.alertDialogIcon;
Chia-chi Yehae380fb2012-01-23 18:33:26 -080070 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();
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070077
Chia-chi Yehae380fb2012-01-23 18:33:26 -080078 getWindow().setCloseOnTouchOutside(false);
79 mButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070080 mButton.setEnabled(false);
Kenny Root56480ef2013-02-13 16:32:36 -080081 mButton.setFilterTouchesWhenObscured(true);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070082 } catch (Exception e) {
83 Log.e(TAG, "onResume", e);
84 finish();
85 }
86 }
87
88 @Override
Chia-chi Yehae380fb2012-01-23 18:33:26 -080089 public void onBackPressed() {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070090 }
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 {
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800100 if (which == DialogInterface.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700101 setResult(RESULT_OK);
102 }
103 } catch (Exception e) {
104 Log.e(TAG, "onClick", e);
105 }
106 }
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700107}