Home | History | Annotate | Download | only in vpndialogs
      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 
     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;
     24 import android.net.IConnectivityManager;
     25 import android.os.ServiceManager;
     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 
     33 import com.android.internal.app.AlertActivity;
     34 
     35 public class ConfirmDialog extends AlertActivity implements
     36         CompoundButton.OnCheckedChangeListener, DialogInterface.OnClickListener {
     37     private static final String TAG = "VpnConfirm";
     38 
     39     private String mPackage;
     40 
     41     private IConnectivityManager mService;
     42 
     43     private Button mButton;
     44 
     45     @Override
     46     protected void onResume() {
     47         super.onResume();
     48         try {
     49             mPackage = getCallingPackage();
     50 
     51             mService = IConnectivityManager.Stub.asInterface(
     52                     ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
     53 
     54             if (mService.prepareVpn(mPackage, null)) {
     55                 setResult(RESULT_OK);
     56                 finish();
     57                 return;
     58             }
     59 
     60             PackageManager pm = getPackageManager();
     61             ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
     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 
     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();
     77 
     78             getWindow().setCloseOnTouchOutside(false);
     79             mButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
     80             mButton.setEnabled(false);
     81         } catch (Exception e) {
     82             Log.e(TAG, "onResume", e);
     83             finish();
     84         }
     85     }
     86 
     87     @Override
     88     public void onBackPressed() {
     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 {
     99             if (which == DialogInterface.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) {
    100                 setResult(RESULT_OK);
    101             }
    102         } catch (Exception e) {
    103             Log.e(TAG, "onClick", e);
    104         }
    105     }
    106 }
    107