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