ManageDialog.java revision 42bd53a9ff3f6358485e37df970754833aab2629
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 17package com.android.vpndialogs; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.content.Context; 22import android.content.DialogInterface; 23import android.content.Intent; 24import android.content.pm.ApplicationInfo; 25import android.content.pm.PackageManager; 26import android.net.IConnectivityManager; 27import android.os.Handler; 28import android.os.Message; 29import android.os.ServiceManager; 30import android.os.SystemClock; 31import android.util.Log; 32import android.view.View; 33import android.widget.Button; 34import android.widget.CompoundButton; 35import android.widget.ImageView; 36import android.widget.TextView; 37 38import com.android.internal.net.VpnConfig; 39 40import java.io.DataInputStream; 41import java.io.FileInputStream; 42 43public class ManageDialog extends Activity implements Handler.Callback, 44 DialogInterface.OnClickListener, DialogInterface.OnDismissListener { 45 private static final String TAG = "VpnManage"; 46 47 private VpnConfig mConfig; 48 49 private IConnectivityManager mService; 50 51 private AlertDialog mDialog; 52 private TextView mDuration; 53 private TextView mDataTransmitted; 54 private TextView mDataReceived; 55 56 private Handler mHandler; 57 58 @Override 59 protected void onResume() { 60 super.onResume(); 61 try { 62 mConfig = getIntent().getParcelableExtra("config"); 63 64 mService = IConnectivityManager.Stub.asInterface( 65 ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); 66 67 PackageManager pm = getPackageManager(); 68 ApplicationInfo app = pm.getApplicationInfo(mConfig.packageName, 0); 69 70 View view = View.inflate(this, R.layout.manage, null); 71 if (mConfig.sessionName != null) { 72 ((TextView) view.findViewById(R.id.session)).setText(mConfig.sessionName); 73 } 74 mDuration = (TextView) view.findViewById(R.id.duration); 75 mDataTransmitted = (TextView) view.findViewById(R.id.data_transmitted); 76 mDataReceived = (TextView) view.findViewById(R.id.data_received); 77 78 mDialog = new AlertDialog.Builder(this) 79 .setIcon(app.loadIcon(pm)) 80 .setTitle(app.loadLabel(pm)) 81 .setView(view) 82 .setNeutralButton(R.string.disconnect, this) 83 .setNegativeButton(android.R.string.cancel, this) 84 .create(); 85 86 if (mConfig.configureActivity != null) { 87 mDialog.setButton(DialogInterface.BUTTON_POSITIVE, 88 getText(R.string.configure), this); 89 } 90 mDialog.setOnDismissListener(this); 91 mDialog.show(); 92 93 if (mHandler == null) { 94 mHandler = new Handler(this); 95 } 96 mHandler.sendEmptyMessage(0); 97 } catch (Exception e) { 98 Log.e(TAG, "onResume", e); 99 finish(); 100 } 101 } 102 103 @Override 104 protected void onPause() { 105 super.onPause(); 106 if (mDialog != null) { 107 mDialog.setOnDismissListener(null); 108 mDialog.dismiss(); 109 } 110 } 111 112 @Override 113 public void onClick(DialogInterface dialog, int which) { 114 try { 115 if (which == AlertDialog.BUTTON_POSITIVE) { 116 Intent intent = new Intent(); 117 intent.setClassName(mConfig.packageName, mConfig.configureActivity); 118 startActivity(intent); 119 } else if (which == AlertDialog.BUTTON_NEUTRAL) { 120 mService.prepareVpn(""); 121 } 122 } catch (Exception e) { 123 Log.e(TAG, "onClick", e); 124 finish(); 125 } 126 } 127 128 @Override 129 public void onDismiss(DialogInterface dialog) { 130 finish(); 131 } 132 133 @Override 134 public boolean handleMessage(Message message) { 135 mHandler.removeMessages(0); 136 137 if (mDialog.isShowing()) { 138 if (mConfig.startTime != 0) { 139 long seconds = (SystemClock.elapsedRealtime() - mConfig.startTime) / 1000; 140 mDuration.setText(String.format("%02d:%02d:%02d", 141 seconds / 3600, seconds / 60 % 60, seconds % 60)); 142 } 143 144 String[] numbers = getStatistics(); 145 if (numbers != null) { 146 // [1] and [2] are received data in bytes and packets. 147 mDataReceived.setText(getString(R.string.data_value_format, 148 numbers[1], numbers[2])); 149 150 // [9] and [10] are transmitted data in bytes and packets. 151 mDataTransmitted.setText(getString(R.string.data_value_format, 152 numbers[9], numbers[10])); 153 } 154 mHandler.sendEmptyMessageDelayed(0, 1000); 155 } 156 return true; 157 } 158 159 private String[] getStatistics() { 160 DataInputStream in = null; 161 try { 162 // See dev_seq_printf_stats() in net/core/dev.c. 163 in = new DataInputStream(new FileInputStream("/proc/net/dev")); 164 String prefix = mConfig.interfaceName + ':'; 165 166 while (true) { 167 String line = in.readLine().trim(); 168 if (line.startsWith(prefix)) { 169 String[] numbers = line.substring(prefix.length()).split(" +"); 170 if (numbers.length == 17) { 171 return numbers; 172 } 173 break; 174 } 175 } 176 } catch (Exception e) { 177 // ignore 178 } finally { 179 try { 180 in.close(); 181 } catch (Exception e) { 182 // ignore 183 } 184 } 185 return null; 186 } 187} 188