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