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.content.Intent; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.net.IConnectivityManager; 25 import android.os.Handler; 26 import android.os.Message; 27 import android.os.ServiceManager; 28 import android.os.SystemClock; 29 import android.util.Log; 30 import android.view.View; 31 import android.widget.Button; 32 import android.widget.CompoundButton; 33 import android.widget.ImageView; 34 import android.widget.TextView; 35 36 import com.android.internal.app.AlertActivity; 37 import com.android.internal.net.VpnConfig; 38 39 import java.io.DataInputStream; 40 import java.io.FileInputStream; 41 42 public 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 private boolean mDataRowsHidden; 54 55 private Handler mHandler; 56 57 @Override 58 protected void onResume() { 59 super.onResume(); 60 61 if (getCallingPackage() != null) { 62 Log.e(TAG, getCallingPackage() + " cannot start this activity"); 63 finish(); 64 return; 65 } 66 67 try { 68 mConfig = getIntent().getParcelableExtra("config"); 69 70 mService = IConnectivityManager.Stub.asInterface( 71 ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); 72 73 View view = View.inflate(this, R.layout.manage, null); 74 if (mConfig.session != null) { 75 ((TextView) view.findViewById(R.id.session)).setText(mConfig.session); 76 } 77 mDuration = (TextView) view.findViewById(R.id.duration); 78 mDataTransmitted = (TextView) view.findViewById(R.id.data_transmitted); 79 mDataReceived = (TextView) view.findViewById(R.id.data_received); 80 mDataRowsHidden = true; 81 82 if (mConfig.legacy) { 83 mAlertParams.mIconId = android.R.drawable.ic_dialog_info; 84 mAlertParams.mTitle = getText(R.string.legacy_title); 85 } else { 86 PackageManager pm = getPackageManager(); 87 ApplicationInfo app = pm.getApplicationInfo(mConfig.user, 0); 88 mAlertParams.mIcon = app.loadIcon(pm); 89 mAlertParams.mTitle = app.loadLabel(pm); 90 } 91 if (mConfig.configureIntent != null) { 92 mAlertParams.mPositiveButtonText = getText(R.string.configure); 93 mAlertParams.mPositiveButtonListener = this; 94 } 95 mAlertParams.mNeutralButtonText = getText(R.string.disconnect); 96 mAlertParams.mNeutralButtonListener = this; 97 mAlertParams.mNegativeButtonText = getText(android.R.string.cancel); 98 mAlertParams.mNegativeButtonListener = this; 99 mAlertParams.mView = view; 100 setupAlert(); 101 102 if (mHandler == null) { 103 mHandler = new Handler(this); 104 } 105 mHandler.sendEmptyMessage(0); 106 } catch (Exception e) { 107 Log.e(TAG, "onResume", e); 108 finish(); 109 } 110 } 111 112 @Override 113 protected void onPause() { 114 super.onPause(); 115 if (!isFinishing()) { 116 finish(); 117 } 118 } 119 120 @Override 121 public void onClick(DialogInterface dialog, int which) { 122 try { 123 if (which == DialogInterface.BUTTON_POSITIVE) { 124 mConfig.configureIntent.send(); 125 } else if (which == DialogInterface.BUTTON_NEUTRAL) { 126 if (mConfig.legacy) { 127 mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN); 128 } else { 129 mService.prepareVpn(mConfig.user, VpnConfig.LEGACY_VPN); 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 != 0) { 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