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.pm.PackageManager;
     22 import android.graphics.drawable.Drawable;
     23 import android.net.IConnectivityManager;
     24 import android.os.Bundle;
     25 import android.os.RemoteException;
     26 import android.os.ServiceManager;
     27 import android.os.UserHandle;
     28 import android.os.UserManager;
     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 public class ConfirmDialog extends AlertActivity
     40         implements DialogInterface.OnClickListener, ImageGetter {
     41     private static final String TAG = "VpnConfirm";
     42 
     43     private String mPackage;
     44 
     45     private IConnectivityManager mService;
     46 
     47     @Override
     48     protected void onCreate(Bundle savedInstanceState) {
     49         super.onCreate(savedInstanceState);
     50         mPackage = getCallingPackage();
     51         mService = IConnectivityManager.Stub.asInterface(
     52                 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
     53 
     54         if (prepareVpn()) {
     55             setResult(RESULT_OK);
     56             finish();
     57             return;
     58         }
     59         if (UserManager.get(this).hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) {
     60             finish();
     61             return;
     62         }
     63         final String alwaysOnVpnPackage = getAlwaysOnVpnPackage();
     64         // Can't prepare new vpn app when another vpn is always-on
     65         if (alwaysOnVpnPackage != null && !alwaysOnVpnPackage.equals(mPackage)) {
     66             finish();
     67             return;
     68         }
     69         View view = View.inflate(this, R.layout.confirm, null);
     70         ((TextView) view.findViewById(R.id.warning)).setText(
     71                 Html.fromHtml(getString(R.string.warning, getVpnLabel()),
     72                         this, null /* tagHandler */));
     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         Button button = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
     82         button.setFilterTouchesWhenObscured(true);
     83     }
     84 
     85     private String getAlwaysOnVpnPackage() {
     86         try {
     87            return mService.getAlwaysOnVpnPackage(UserHandle.myUserId());
     88         } catch (RemoteException e) {
     89             Log.e(TAG, "fail to call getAlwaysOnVpnPackage", e);
     90             // Fallback to null to show the dialog
     91             return null;
     92         }
     93     }
     94 
     95     private boolean prepareVpn() {
     96         try {
     97             return mService.prepareVpn(mPackage, null, UserHandle.myUserId());
     98         } catch (RemoteException e) {
     99             throw new IllegalStateException(e);
    100         }
    101     }
    102 
    103     private CharSequence getVpnLabel() {
    104         try {
    105             return VpnConfig.getVpnLabel(this, mPackage);
    106         } catch (PackageManager.NameNotFoundException e) {
    107             throw new IllegalStateException(e);
    108         }
    109     }
    110 
    111     @Override
    112     public Drawable getDrawable(String source) {
    113         // Should only reach this when fetching the VPN icon for the warning string.
    114         Drawable icon = getDrawable(R.drawable.ic_vpn_dialog);
    115         icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
    116         return icon;
    117     }
    118 
    119     @Override
    120     public void onBackPressed() {
    121     }
    122 
    123     @Override
    124     public void onClick(DialogInterface dialog, int which) {
    125         try {
    126             if (mService.prepareVpn(null, mPackage, UserHandle.myUserId())) {
    127                 // Authorize this app to initiate VPN connections in the future without user
    128                 // intervention.
    129                 mService.setVpnPackageAuthorization(mPackage, UserHandle.myUserId(), true);
    130                 setResult(RESULT_OK);
    131             }
    132         } catch (Exception e) {
    133             Log.e(TAG, "onClick", e);
    134         }
    135     }
    136 }
    137