Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 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 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.DialogInterface; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageManager; |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 23 | import android.net.IConnectivityManager; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 26 | import android.os.ServiceManager; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 27 | import android.os.SystemClock; |
| 28 | import android.util.Log; |
| 29 | import android.view.View; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 30 | import android.widget.TextView; |
| 31 | |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 32 | import com.android.internal.app.AlertActivity; |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 33 | import com.android.internal.net.VpnConfig; |
| 34 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 35 | import java.io.DataInputStream; |
| 36 | import java.io.FileInputStream; |
| 37 | |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 38 | public class ManageDialog extends AlertActivity implements |
| 39 | DialogInterface.OnClickListener, Handler.Callback { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 40 | private static final String TAG = "VpnManage"; |
| 41 | |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 42 | private VpnConfig mConfig; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 43 | |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 44 | private IConnectivityManager mService; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 45 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 46 | private TextView mDuration; |
| 47 | private TextView mDataTransmitted; |
| 48 | private TextView mDataReceived; |
Chia-chi Yeh | b0736ab | 2012-02-27 17:30:41 -0800 | [diff] [blame] | 49 | private boolean mDataRowsHidden; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 50 | |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 51 | private Handler mHandler; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | protected void onResume() { |
| 55 | super.onResume(); |
Chia-chi Yeh | 339abf1 | 2011-07-15 12:00:55 -0700 | [diff] [blame] | 56 | |
| 57 | if (getCallingPackage() != null) { |
| 58 | Log.e(TAG, getCallingPackage() + " cannot start this activity"); |
| 59 | finish(); |
| 60 | return; |
| 61 | } |
| 62 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 63 | try { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 64 | |
Chia-chi Yeh | f530da6 | 2011-06-15 17:05:25 -0700 | [diff] [blame] | 65 | mService = IConnectivityManager.Stub.asInterface( |
| 66 | ServiceManager.getService(Context.CONNECTIVITY_SERVICE)); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 67 | |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 68 | mConfig = mService.getVpnConfig(); |
| 69 | |
| 70 | // mConfig can be null if we are a restricted user, in that case don't show this dialog |
| 71 | if (mConfig == null) { |
| 72 | finish(); |
| 73 | return; |
| 74 | } |
| 75 | |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 76 | View view = View.inflate(this, R.layout.manage, null); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 77 | if (mConfig.session != null) { |
| 78 | ((TextView) view.findViewById(R.id.session)).setText(mConfig.session); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 79 | } |
| 80 | mDuration = (TextView) view.findViewById(R.id.duration); |
| 81 | mDataTransmitted = (TextView) view.findViewById(R.id.data_transmitted); |
| 82 | mDataReceived = (TextView) view.findViewById(R.id.data_received); |
Chia-chi Yeh | b0736ab | 2012-02-27 17:30:41 -0800 | [diff] [blame] | 83 | mDataRowsHidden = true; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 85 | if (mConfig.legacy) { |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 86 | mAlertParams.mIconId = android.R.drawable.ic_dialog_info; |
| 87 | mAlertParams.mTitle = getText(R.string.legacy_title); |
Chia-chi Yeh | 5db03df | 2011-06-30 23:52:29 -0700 | [diff] [blame] | 88 | } else { |
| 89 | PackageManager pm = getPackageManager(); |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 90 | ApplicationInfo app = pm.getApplicationInfo(mConfig.user, 0); |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 91 | mAlertParams.mIcon = app.loadIcon(pm); |
| 92 | mAlertParams.mTitle = app.loadLabel(pm); |
Chia-chi Yeh | 5db03df | 2011-06-30 23:52:29 -0700 | [diff] [blame] | 93 | } |
Chia-chi Yeh | 5db03df | 2011-06-30 23:52:29 -0700 | [diff] [blame] | 94 | if (mConfig.configureIntent != null) { |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 95 | mAlertParams.mPositiveButtonText = getText(R.string.configure); |
| 96 | mAlertParams.mPositiveButtonListener = this; |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 97 | } |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 98 | mAlertParams.mNeutralButtonText = getText(R.string.disconnect); |
| 99 | mAlertParams.mNeutralButtonListener = this; |
| 100 | mAlertParams.mNegativeButtonText = getText(android.R.string.cancel); |
| 101 | mAlertParams.mNegativeButtonListener = this; |
| 102 | mAlertParams.mView = view; |
| 103 | setupAlert(); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 104 | |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 105 | if (mHandler == null) { |
| 106 | mHandler = new Handler(this); |
| 107 | } |
| 108 | mHandler.sendEmptyMessage(0); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 109 | } catch (Exception e) { |
| 110 | Log.e(TAG, "onResume", e); |
| 111 | finish(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | protected void onPause() { |
| 117 | super.onPause(); |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 118 | if (!isFinishing()) { |
| 119 | finish(); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onClick(DialogInterface dialog, int which) { |
| 125 | try { |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 126 | if (which == DialogInterface.BUTTON_POSITIVE) { |
Chia-chi Yeh | 5db03df | 2011-06-30 23:52:29 -0700 | [diff] [blame] | 127 | mConfig.configureIntent.send(); |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 128 | } else if (which == DialogInterface.BUTTON_NEUTRAL) { |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 129 | if (mConfig.legacy) { |
| 130 | mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN); |
| 131 | } else { |
| 132 | mService.prepareVpn(mConfig.user, VpnConfig.LEGACY_VPN); |
| 133 | } |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 134 | } |
| 135 | } catch (Exception e) { |
| 136 | Log.e(TAG, "onClick", e); |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 137 | finish(); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 142 | public boolean handleMessage(Message message) { |
| 143 | mHandler.removeMessages(0); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 144 | |
Chia-chi Yeh | ae380fb | 2012-01-23 18:33:26 -0800 | [diff] [blame] | 145 | if (!isFinishing()) { |
Vinit Deshapnde | 2b862e5 | 2013-10-02 11:50:39 -0700 | [diff] [blame] | 146 | if (mConfig.startTime != -1) { |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 147 | long seconds = (SystemClock.elapsedRealtime() - mConfig.startTime) / 1000; |
| 148 | mDuration.setText(String.format("%02d:%02d:%02d", |
| 149 | seconds / 3600, seconds / 60 % 60, seconds % 60)); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 150 | } |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 151 | |
Chia-chi Yeh | b0736ab | 2012-02-27 17:30:41 -0800 | [diff] [blame] | 152 | String[] numbers = getNumbers(); |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 153 | if (numbers != null) { |
Chia-chi Yeh | b0736ab | 2012-02-27 17:30:41 -0800 | [diff] [blame] | 154 | // First unhide the related data rows. |
| 155 | if (mDataRowsHidden) { |
| 156 | findViewById(R.id.data_transmitted_row).setVisibility(View.VISIBLE); |
| 157 | findViewById(R.id.data_received_row).setVisibility(View.VISIBLE); |
| 158 | mDataRowsHidden = false; |
| 159 | } |
| 160 | |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 161 | // [1] and [2] are received data in bytes and packets. |
| 162 | mDataReceived.setText(getString(R.string.data_value_format, |
| 163 | numbers[1], numbers[2])); |
| 164 | |
| 165 | // [9] and [10] are transmitted data in bytes and packets. |
| 166 | mDataTransmitted.setText(getString(R.string.data_value_format, |
| 167 | numbers[9], numbers[10])); |
| 168 | } |
| 169 | mHandler.sendEmptyMessageDelayed(0, 1000); |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 170 | } |
Chia-chi Yeh | 42bd53a | 2011-06-17 15:27:41 -0700 | [diff] [blame] | 171 | return true; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Chia-chi Yeh | b0736ab | 2012-02-27 17:30:41 -0800 | [diff] [blame] | 174 | private String[] getNumbers() { |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 175 | DataInputStream in = null; |
| 176 | try { |
| 177 | // See dev_seq_printf_stats() in net/core/dev.c. |
| 178 | in = new DataInputStream(new FileInputStream("/proc/net/dev")); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 179 | String prefix = mConfig.interfaze + ':'; |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 180 | |
| 181 | while (true) { |
| 182 | String line = in.readLine().trim(); |
| 183 | if (line.startsWith(prefix)) { |
| 184 | String[] numbers = line.substring(prefix.length()).split(" +"); |
Chia-chi Yeh | 72fddaa | 2011-09-26 15:05:52 -0700 | [diff] [blame] | 185 | for (int i = 1; i < 17; ++i) { |
| 186 | if (!numbers[i].equals("0")) { |
| 187 | return numbers; |
| 188 | } |
Chia-chi Yeh | 19f054b | 2011-06-03 17:06:29 -0700 | [diff] [blame] | 189 | } |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | } catch (Exception e) { |
| 194 | // ignore |
| 195 | } finally { |
| 196 | try { |
| 197 | in.close(); |
| 198 | } catch (Exception e) { |
| 199 | // ignore |
| 200 | } |
| 201 | } |
| 202 | return null; |
| 203 | } |
| 204 | } |