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 | |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 69 | mAlertParams.mIconId = android.R.drawable.ic_dialog_alert; |
| 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); |
| 81 | } catch (Exception e) { |
| 82 | Log.e(TAG, "onResume", e); |
| 83 | finish(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 88 | public void onBackPressed() { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void onCheckedChanged(CompoundButton button, boolean checked) { |
| 93 | mButton.setEnabled(checked); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onClick(DialogInterface dialog, int which) { |
| 98 | try { |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 99 | if (which == DialogInterface.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 100 | setResult(RESULT_OK); |
| 101 | } |
| 102 | } catch (Exception e) { |
| 103 | Log.e(TAG, "onClick", e); |
| 104 | } |
| 105 | } |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 106 | } |