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