blob: 3d5654a56b9e2ccb1c8233762f2dfd044592a1e5 [file] [log] [blame]
Chia-chi Yeh19f054b2011-06-03 17:06:29 -07001/*
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
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070019import android.content.Context;
20import android.content.DialogInterface;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
23import android.content.pm.PackageManager;
Chia-chi Yehf530da62011-06-15 17:05:25 -070024import android.net.IConnectivityManager;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070025import android.os.Handler;
26import android.os.Message;
Chia-chi Yehf530da62011-06-15 17:05:25 -070027import android.os.ServiceManager;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070028import 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
Chia-chi Yehae380fb2012-01-23 18:33:26 -080036import com.android.internal.app.AlertActivity;
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070037import com.android.internal.net.VpnConfig;
38
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070039import java.io.DataInputStream;
40import java.io.FileInputStream;
41
Chia-chi Yehae380fb2012-01-23 18:33:26 -080042public class ManageDialog extends AlertActivity implements
43 DialogInterface.OnClickListener, Handler.Callback {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070044 private static final String TAG = "VpnManage";
45
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070046 private VpnConfig mConfig;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070047
Chia-chi Yehf530da62011-06-15 17:05:25 -070048 private IConnectivityManager mService;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070049
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070050 private TextView mDuration;
51 private TextView mDataTransmitted;
52 private TextView mDataReceived;
Chia-chi Yehb0736ab2012-02-27 17:30:41 -080053 private boolean mDataRowsHidden;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070054
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070055 private Handler mHandler;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070056
57 @Override
58 protected void onResume() {
59 super.onResume();
Chia-chi Yeh339abf12011-07-15 12:00:55 -070060
61 if (getCallingPackage() != null) {
62 Log.e(TAG, getCallingPackage() + " cannot start this activity");
63 finish();
64 return;
65 }
66
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070067 try {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070068
Chia-chi Yehf530da62011-06-15 17:05:25 -070069 mService = IConnectivityManager.Stub.asInterface(
70 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070071
Chad Brubakerbf6ff2c2013-07-16 18:59:12 -070072 mConfig = mService.getVpnConfig();
73
74 // mConfig can be null if we are a restricted user, in that case don't show this dialog
75 if (mConfig == null) {
76 finish();
77 return;
78 }
79
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070080 View view = View.inflate(this, R.layout.manage, null);
Chia-chi Yeh34e78132011-07-03 03:07:07 -070081 if (mConfig.session != null) {
82 ((TextView) view.findViewById(R.id.session)).setText(mConfig.session);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070083 }
84 mDuration = (TextView) view.findViewById(R.id.duration);
85 mDataTransmitted = (TextView) view.findViewById(R.id.data_transmitted);
86 mDataReceived = (TextView) view.findViewById(R.id.data_received);
Chia-chi Yehb0736ab2012-02-27 17:30:41 -080087 mDataRowsHidden = true;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070088
Jeff Sharkey899223b2012-08-04 15:24:58 -070089 if (mConfig.legacy) {
Chia-chi Yehae380fb2012-01-23 18:33:26 -080090 mAlertParams.mIconId = android.R.drawable.ic_dialog_info;
91 mAlertParams.mTitle = getText(R.string.legacy_title);
Chia-chi Yeh5db03df2011-06-30 23:52:29 -070092 } else {
93 PackageManager pm = getPackageManager();
Chia-chi Yehfcc1b412011-08-03 15:39:59 -070094 ApplicationInfo app = pm.getApplicationInfo(mConfig.user, 0);
Chia-chi Yehae380fb2012-01-23 18:33:26 -080095 mAlertParams.mIcon = app.loadIcon(pm);
96 mAlertParams.mTitle = app.loadLabel(pm);
Chia-chi Yeh5db03df2011-06-30 23:52:29 -070097 }
Chia-chi Yeh5db03df2011-06-30 23:52:29 -070098 if (mConfig.configureIntent != null) {
Chia-chi Yehae380fb2012-01-23 18:33:26 -080099 mAlertParams.mPositiveButtonText = getText(R.string.configure);
100 mAlertParams.mPositiveButtonListener = this;
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700101 }
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800102 mAlertParams.mNeutralButtonText = getText(R.string.disconnect);
103 mAlertParams.mNeutralButtonListener = this;
104 mAlertParams.mNegativeButtonText = getText(android.R.string.cancel);
105 mAlertParams.mNegativeButtonListener = this;
106 mAlertParams.mView = view;
107 setupAlert();
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700108
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700109 if (mHandler == null) {
110 mHandler = new Handler(this);
111 }
112 mHandler.sendEmptyMessage(0);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700113 } catch (Exception e) {
114 Log.e(TAG, "onResume", e);
115 finish();
116 }
117 }
118
119 @Override
120 protected void onPause() {
121 super.onPause();
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800122 if (!isFinishing()) {
123 finish();
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700124 }
125 }
126
127 @Override
128 public void onClick(DialogInterface dialog, int which) {
129 try {
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800130 if (which == DialogInterface.BUTTON_POSITIVE) {
Chia-chi Yeh5db03df2011-06-30 23:52:29 -0700131 mConfig.configureIntent.send();
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800132 } else if (which == DialogInterface.BUTTON_NEUTRAL) {
Jeff Sharkey82f85212012-08-24 11:17:25 -0700133 if (mConfig.legacy) {
134 mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN);
135 } else {
136 mService.prepareVpn(mConfig.user, VpnConfig.LEGACY_VPN);
137 }
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700138 }
139 } catch (Exception e) {
140 Log.e(TAG, "onClick", e);
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700141 finish();
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700142 }
143 }
144
145 @Override
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700146 public boolean handleMessage(Message message) {
147 mHandler.removeMessages(0);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700148
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800149 if (!isFinishing()) {
Vinit Deshapnde2b862e52013-10-02 11:50:39 -0700150 if (mConfig.startTime != -1) {
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700151 long seconds = (SystemClock.elapsedRealtime() - mConfig.startTime) / 1000;
152 mDuration.setText(String.format("%02d:%02d:%02d",
153 seconds / 3600, seconds / 60 % 60, seconds % 60));
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700154 }
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700155
Chia-chi Yehb0736ab2012-02-27 17:30:41 -0800156 String[] numbers = getNumbers();
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700157 if (numbers != null) {
Chia-chi Yehb0736ab2012-02-27 17:30:41 -0800158 // First unhide the related data rows.
159 if (mDataRowsHidden) {
160 findViewById(R.id.data_transmitted_row).setVisibility(View.VISIBLE);
161 findViewById(R.id.data_received_row).setVisibility(View.VISIBLE);
162 mDataRowsHidden = false;
163 }
164
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700165 // [1] and [2] are received data in bytes and packets.
166 mDataReceived.setText(getString(R.string.data_value_format,
167 numbers[1], numbers[2]));
168
169 // [9] and [10] are transmitted data in bytes and packets.
170 mDataTransmitted.setText(getString(R.string.data_value_format,
171 numbers[9], numbers[10]));
172 }
173 mHandler.sendEmptyMessageDelayed(0, 1000);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700174 }
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700175 return true;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700176 }
177
Chia-chi Yehb0736ab2012-02-27 17:30:41 -0800178 private String[] getNumbers() {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700179 DataInputStream in = null;
180 try {
181 // See dev_seq_printf_stats() in net/core/dev.c.
182 in = new DataInputStream(new FileInputStream("/proc/net/dev"));
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700183 String prefix = mConfig.interfaze + ':';
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700184
185 while (true) {
186 String line = in.readLine().trim();
187 if (line.startsWith(prefix)) {
188 String[] numbers = line.substring(prefix.length()).split(" +");
Chia-chi Yeh72fddaa2011-09-26 15:05:52 -0700189 for (int i = 1; i < 17; ++i) {
190 if (!numbers[i].equals("0")) {
191 return numbers;
192 }
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700193 }
194 break;
195 }
196 }
197 } catch (Exception e) {
198 // ignore
199 } finally {
200 try {
201 in.close();
202 } catch (Exception e) {
203 // ignore
204 }
205 }
206 return null;
207 }
208}