ConfirmDialog.java revision ca8594a3411f7aeaf5cd22e7e95a5b19f490ae6c
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.content.Context; 20import android.content.DialogInterface; 21import android.content.pm.PackageManager; 22import android.graphics.drawable.Drawable; 23import android.net.IConnectivityManager; 24import android.os.Bundle; 25import android.os.RemoteException; 26import android.os.ServiceManager; 27import android.os.UserHandle; 28import android.text.Html; 29import android.text.Html.ImageGetter; 30import android.util.Log; 31import android.view.View; 32import android.widget.Button; 33import android.widget.TextView; 34 35import com.android.internal.app.AlertActivity; 36import com.android.internal.net.VpnConfig; 37 38public class ConfirmDialog extends AlertActivity 39 implements DialogInterface.OnClickListener, ImageGetter { 40 private static final String TAG = "VpnConfirm"; 41 42 private String mPackage; 43 44 private IConnectivityManager mService; 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 mPackage = getCallingPackage(); 50 mService = IConnectivityManager.Stub.asInterface( 51 ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); 52 53 if (prepareVpn()) { 54 setResult(RESULT_OK); 55 finish(); 56 return; 57 } 58 View view = View.inflate(this, R.layout.confirm, null); 59 ((TextView) view.findViewById(R.id.warning)).setText( 60 Html.fromHtml(getString(R.string.warning, getVpnLabel()), 61 this, null /* tagHandler */)); 62 mAlertParams.mTitle = getText(R.string.prompt); 63 mAlertParams.mPositiveButtonText = getText(android.R.string.ok); 64 mAlertParams.mPositiveButtonListener = this; 65 mAlertParams.mNegativeButtonText = getText(android.R.string.cancel); 66 mAlertParams.mView = view; 67 setupAlert(); 68 69 getWindow().setCloseOnTouchOutside(false); 70 Button button = mAlert.getButton(DialogInterface.BUTTON_POSITIVE); 71 button.setFilterTouchesWhenObscured(true); 72 } 73 74 private boolean prepareVpn() { 75 try { 76 return mService.prepareVpn(mPackage, null, UserHandle.myUserId()); 77 } catch (RemoteException e) { 78 throw new IllegalStateException(e); 79 } 80 } 81 82 private CharSequence getVpnLabel() { 83 try { 84 return VpnConfig.getVpnLabel(this, mPackage); 85 } catch (PackageManager.NameNotFoundException e) { 86 throw new IllegalStateException(e); 87 } 88 } 89 90 @Override 91 public Drawable getDrawable(String source) { 92 // Should only reach this when fetching the VPN icon for the warning string. 93 Drawable icon = getDrawable(R.drawable.ic_vpn_dialog); 94 icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); 95 return icon; 96 } 97 98 @Override 99 public void onBackPressed() { 100 } 101 102 @Override 103 public void onClick(DialogInterface dialog, int which) { 104 try { 105 if (mService.prepareVpn(null, mPackage, UserHandle.myUserId())) { 106 // Authorize this app to initiate VPN connections in the future without user 107 // intervention. 108 mService.setVpnPackageAuthorization(mPackage, UserHandle.myUserId(), true); 109 setResult(RESULT_OK); 110 } 111 } catch (Exception e) { 112 Log.e(TAG, "onClick", e); 113 } 114 } 115} 116