ManageDialog.java revision 34e7813e962de99df9813014678ef5901227c5f1
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 View view = View.inflate(this, R.layout.manage, null); 68 if (mConfig.session != null) { 69 ((TextView) view.findViewById(R.id.session)).setText(mConfig.session); 70 } 71 mDuration = (TextView) view.findViewById(R.id.duration); 72 mDataTransmitted = (TextView) view.findViewById(R.id.data_transmitted); 73 mDataReceived = (TextView) view.findViewById(R.id.data_received); 74 75 if (mConfig.packagz.equals(VpnConfig.LEGACY_VPN)) { 76 mDialog = new AlertDialog.Builder(this) 77 .setIcon(android.R.drawable.ic_dialog_info) 78 .setTitle(R.string.legacy_title) 79 .setView(view) 80 .setNeutralButton(R.string.disconnect, this) 81 .setNegativeButton(android.R.string.cancel, this) 82 .create(); 83 } else { 84 PackageManager pm = getPackageManager(); 85 ApplicationInfo app = pm.getApplicationInfo(mConfig.packagz, 0); 86 87 mDialog = new AlertDialog.Builder(this) 88 .setIcon(app.loadIcon(pm)) 89 .setTitle(app.loadLabel(pm)) 90 .setView(view) 91 .setNeutralButton(R.string.disconnect, this) 92 .setNegativeButton(android.R.string.cancel, this) 93 .create(); 94 } 95 96 if (mConfig.configureIntent != null) { 97 mDialog.setButton(DialogInterface.BUTTON_POSITIVE, 98 getText(R.string.configure), this); 99 } 100 mDialog.setOnDismissListener(this); 101 mDialog.show(); 102 103 if (mHandler == null) { 104 mHandler = new Handler(this); 105 } 106 mHandler.sendEmptyMessage(0); 107 } catch (Exception e) { 108 Log.e(TAG, "onResume", e); 109 finish(); 110 } 111 } 112 113 @Override 114 protected void onPause() { 115 super.onPause(); 116 if (mDialog != null) { 117 mDialog.setOnDismissListener(null); 118 mDialog.dismiss(); 119 } 120 } 121 122 @Override 123 public void onClick(DialogInterface dialog, int which) { 124 try { 125 if (which == AlertDialog.BUTTON_POSITIVE) { 126 mConfig.configureIntent.send(); 127 } else if (which == AlertDialog.BUTTON_NEUTRAL) { 128 mService.prepareVpn(VpnConfig.LEGACY_VPN); 129 } 130 } catch (Exception e) { 131 Log.e(TAG, "onClick", e); 132 finish(); 133 } 134 } 135 136 @Override 137 public void onDismiss(DialogInterface dialog) { 138 finish(); 139 } 140 141 @Override 142 public boolean handleMessage(Message message) { 143 mHandler.removeMessages(0); 144 145 if (mDialog.isShowing()) { 146 if (mConfig.startTime != 0) { 147 long seconds = (SystemClock.elapsedRealtime() - mConfig.startTime) / 1000; 148 mDuration.setText(String.format("%02d:%02d:%02d", 149 seconds / 3600, seconds / 60 % 60, seconds % 60)); 150 } 151 152 String[] numbers = getStatistics(); 153 if (numbers != null) { 154 // [1] and [2] are received data in bytes and packets. 155 mDataReceived.setText(getString(R.string.data_value_format, 156 numbers[1], numbers[2])); 157 158 // [9] and [10] are transmitted data in bytes and packets. 159 mDataTransmitted.setText(getString(R.string.data_value_format, 160 numbers[9], numbers[10])); 161 } 162 mHandler.sendEmptyMessageDelayed(0, 1000); 163 } 164 return true; 165 } 166 167 private String[] getStatistics() { 168 DataInputStream in = null; 169 try { 170 // See dev_seq_printf_stats() in net/core/dev.c. 171 in = new DataInputStream(new FileInputStream("/proc/net/dev")); 172 String prefix = mConfig.interfaze + ':'; 173 174 while (true) { 175 String line = in.readLine().trim(); 176 if (line.startsWith(prefix)) { 177 String[] numbers = line.substring(prefix.length()).split(" +"); 178 if (numbers.length == 17) { 179 return numbers; 180 } 181 break; 182 } 183 } 184 } catch (Exception e) { 185 // ignore 186 } finally { 187 try { 188 in.close(); 189 } catch (Exception e) { 190 // ignore 191 } 192 } 193 return null; 194 } 195} 196