Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.vpndialogs; |
| 18 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.DialogInterface; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ApplicationInfo; |
| 23 | import android.content.pm.PackageManager; |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 24 | import android.net.IConnectivityManager; |
| 25 | import android.os.ServiceManager; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | import android.view.View; |
| 28 | import android.widget.Button; |
| 29 | import android.widget.CompoundButton; |
| 30 | import android.widget.ImageView; |
| 31 | import android.widget.TextView; |
| 32 | |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 33 | import com.android.internal.app.AlertActivity; |
| 34 | |
| 35 | public class ConfirmDialog extends AlertActivity implements |
| 36 | CompoundButton.OnCheckedChangeListener, DialogInterface.OnClickListener { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 37 | private static final String TAG = "VpnConfirm"; |
| 38 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 39 | private String mPackage; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 40 | |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 41 | private IConnectivityManager mService; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 42 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 43 | private Button mButton; |
| 44 | |
| 45 | @Override |
| 46 | protected void onResume() { |
| 47 | super.onResume(); |
| 48 | try { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 49 | mPackage = getCallingPackage(); |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 50 | |
| 51 | mService = IConnectivityManager.Stub.asInterface( |
| 52 | ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 53 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 54 | if (mService.prepareVpn(mPackage, null)) { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 55 | setResult(RESULT_OK); |
| 56 | finish(); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | PackageManager pm = getPackageManager(); |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 61 | ApplicationInfo app = pm.getApplicationInfo(mPackage, 0); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 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 | |
Björn Lundén | 1129a85 | 2012-09-20 13:58:51 +0200 | [diff] [blame] | 69 | mAlertParams.mIconAttrId = android.R.attr.alertDialogIcon; |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 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(); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 77 | |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 78 | getWindow().setCloseOnTouchOutside(false); |
| 79 | mButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 80 | mButton.setEnabled(false); |
Kenny Root | 56480ef | 2013-02-13 16:32:36 -0800 | [diff] [blame] | 81 | mButton.setFilterTouchesWhenObscured(true); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 82 | } catch (Exception e) { |
| 83 | Log.e(TAG, "onResume", e); |
| 84 | finish(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 89 | public void onBackPressed() { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 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 { |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 100 | if (which == DialogInterface.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 101 | setResult(RESULT_OK); |
| 102 | } |
| 103 | } catch (Exception e) { |
| 104 | Log.e(TAG, "onClick", e); |
| 105 | } |
| 106 | } |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 107 | } |