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.Handler;
     26 import android.os.Message;
     27 import android.os.ServiceManager;
     28 import android.os.SystemClock;
     29 import android.util.Log;
     30 import android.view.View;
     31 import android.widget.Button;
     32 import android.widget.CompoundButton;
     33 import android.widget.ImageView;
     34 import android.widget.TextView;
     35 
     36 import com.android.internal.app.AlertActivity;
     37 import com.android.internal.net.VpnConfig;
     38 
     39 import java.io.DataInputStream;
     40 import java.io.FileInputStream;
     41 
     42 public class ManageDialog extends AlertActivity implements
     43         DialogInterface.OnClickListener, Handler.Callback {
     44     private static final String TAG = "VpnManage";
     45 
     46     private VpnConfig mConfig;
     47 
     48     private IConnectivityManager mService;
     49 
     50     private TextView mDuration;
     51     private TextView mDataTransmitted;
     52     private TextView mDataReceived;
     53     private boolean mDataRowsHidden;
     54 
     55     private Handler mHandler;
     56 
     57     @Override
     58     protected void onResume() {
     59         super.onResume();
     60 
     61         if (getCallingPackage() != null) {
     62             Log.e(TAG, getCallingPackage() + " cannot start this activity");
     63             finish();
     64             return;
     65         }
     66 
     67         try {
     68 
     69             mService = IConnectivityManager.Stub.asInterface(
     70                     ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
     71 
     72             mConfig = mService.getVpnConfig();
     73 
     74             // mConfig can be null if we are a restricted user, in that case don't show this dialog
     75             if (mConfig == null) {
     76                 finish();
     77                 return;
     78             }
     79 
     80             View view = View.inflate(this, R.layout.manage, null);
     81             if (mConfig.session != null) {
     82                 ((TextView) view.findViewById(R.id.session)).setText(mConfig.session);
     83             }
     84             mDuration = (TextView) view.findViewById(R.id.duration);
     85             mDataTransmitted = (TextView) view.findViewById(R.id.data_transmitted);
     86             mDataReceived = (TextView) view.findViewById(R.id.data_received);
     87             mDataRowsHidden = true;
     88 
     89             if (mConfig.legacy) {
     90                 mAlertParams.mIconId = android.R.drawable.ic_dialog_info;
     91                 mAlertParams.mTitle = getText(R.string.legacy_title);
     92             } else {
     93                 PackageManager pm = getPackageManager();
     94                 ApplicationInfo app = pm.getApplicationInfo(mConfig.user, 0);
     95                 mAlertParams.mIcon = app.loadIcon(pm);
     96                 mAlertParams.mTitle = app.loadLabel(pm);
     97             }
     98             if (mConfig.configureIntent != null) {
     99                 mAlertParams.mPositiveButtonText = getText(R.string.configure);
    100                 mAlertParams.mPositiveButtonListener = this;
    101             }
    102             mAlertParams.mNeutralButtonText = getText(R.string.disconnect);
    103             mAlertParams.mNeutralButtonListener = this;
    104             mAlertParams.mNegativeButtonText = getText(android.R.string.cancel);
    105             mAlertParams.mNegativeButtonListener = this;
    106             mAlertParams.mView = view;
    107             setupAlert();
    108 
    109             if (mHandler == null) {
    110                 mHandler = new Handler(this);
    111             }
    112             mHandler.sendEmptyMessage(0);
    113         } catch (Exception e) {
    114             Log.e(TAG, "onResume", e);
    115             finish();
    116         }
    117     }
    118 
    119     @Override
    120     protected void onPause() {
    121         super.onPause();
    122         if (!isFinishing()) {
    123             finish();
    124         }
    125     }
    126 
    127     @Override
    128     public void onClick(DialogInterface dialog, int which) {
    129         try {
    130             if (which == DialogInterface.BUTTON_POSITIVE) {
    131                 mConfig.configureIntent.send();
    132             } else if (which == DialogInterface.BUTTON_NEUTRAL) {
    133                 if (mConfig.legacy) {
    134                     mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN);
    135                 } else {
    136                     mService.prepareVpn(mConfig.user, VpnConfig.LEGACY_VPN);
    137                 }
    138             }
    139         } catch (Exception e) {
    140             Log.e(TAG, "onClick", e);
    141             finish();
    142         }
    143     }
    144 
    145     @Override
    146     public boolean handleMessage(Message message) {
    147         mHandler.removeMessages(0);
    148 
    149         if (!isFinishing()) {
    150             if (mConfig.startTime != -1) {
    151                 long seconds = (SystemClock.elapsedRealtime() - mConfig.startTime) / 1000;
    152                 mDuration.setText(String.format("%02d:%02d:%02d",
    153                         seconds / 3600, seconds / 60 % 60, seconds % 60));
    154             }
    155 
    156             String[] numbers = getNumbers();
    157             if (numbers != null) {
    158                 // First unhide the related data rows.
    159                 if (mDataRowsHidden) {
    160                     findViewById(R.id.data_transmitted_row).setVisibility(View.VISIBLE);
    161                     findViewById(R.id.data_received_row).setVisibility(View.VISIBLE);
    162                     mDataRowsHidden = false;
    163                 }
    164 
    165                 // [1] and [2] are received data in bytes and packets.
    166                 mDataReceived.setText(getString(R.string.data_value_format,
    167                         numbers[1], numbers[2]));
    168 
    169                 // [9] and [10] are transmitted data in bytes and packets.
    170                 mDataTransmitted.setText(getString(R.string.data_value_format,
    171                         numbers[9], numbers[10]));
    172             }
    173             mHandler.sendEmptyMessageDelayed(0, 1000);
    174         }
    175         return true;
    176     }
    177 
    178     private String[] getNumbers() {
    179         DataInputStream in = null;
    180         try {
    181             // See dev_seq_printf_stats() in net/core/dev.c.
    182             in = new DataInputStream(new FileInputStream("/proc/net/dev"));
    183             String prefix = mConfig.interfaze + ':';
    184 
    185             while (true) {
    186                 String line = in.readLine().trim();
    187                 if (line.startsWith(prefix)) {
    188                     String[] numbers = line.substring(prefix.length()).split(" +");
    189                     for (int i = 1; i < 17; ++i) {
    190                         if (!numbers[i].equals("0")) {
    191                             return numbers;
    192                         }
    193                     }
    194                     break;
    195                 }
    196             }
    197         } catch (Exception e) {
    198             // ignore
    199         } finally {
    200             try {
    201                 in.close();
    202             } catch (Exception e) {
    203                 // ignore
    204             }
    205         }
    206         return null;
    207     }
    208 }
    209