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