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