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