blob: eb20995ade1073e7560d605c0e88f76e645dde69 [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;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070021import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageManager;
Chia-chi Yehf530da62011-06-15 17:05:25 -070023import android.net.IConnectivityManager;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070024import android.os.Handler;
25import android.os.Message;
Chia-chi Yehf530da62011-06-15 17:05:25 -070026import android.os.ServiceManager;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070027import android.os.SystemClock;
28import android.util.Log;
29import android.view.View;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070030import android.widget.TextView;
31
Chia-chi Yehae380fb2012-01-23 18:33:26 -080032import com.android.internal.app.AlertActivity;
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070033import com.android.internal.net.VpnConfig;
34
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070035import java.io.DataInputStream;
36import java.io.FileInputStream;
37
Chia-chi Yehae380fb2012-01-23 18:33:26 -080038public class ManageDialog extends AlertActivity implements
39 DialogInterface.OnClickListener, Handler.Callback {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070040 private static final String TAG = "VpnManage";
41
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070042 private VpnConfig mConfig;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070043
Chia-chi Yehf530da62011-06-15 17:05:25 -070044 private IConnectivityManager mService;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070045
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070046 private TextView mDuration;
47 private TextView mDataTransmitted;
48 private TextView mDataReceived;
Chia-chi Yehb0736ab2012-02-27 17:30:41 -080049 private boolean mDataRowsHidden;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070050
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070051 private Handler mHandler;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070052
53 @Override
54 protected void onResume() {
55 super.onResume();
Chia-chi Yeh339abf12011-07-15 12:00:55 -070056
57 if (getCallingPackage() != null) {
58 Log.e(TAG, getCallingPackage() + " cannot start this activity");
59 finish();
60 return;
61 }
62
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070063 try {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070064
Chia-chi Yehf530da62011-06-15 17:05:25 -070065 mService = IConnectivityManager.Stub.asInterface(
66 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070067
Chad Brubakerbf6ff2c2013-07-16 18:59:12 -070068 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 Yeh19f054b2011-06-03 17:06:29 -070076 View view = View.inflate(this, R.layout.manage, null);
Chia-chi Yeh34e78132011-07-03 03:07:07 -070077 if (mConfig.session != null) {
78 ((TextView) view.findViewById(R.id.session)).setText(mConfig.session);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070079 }
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 Yehb0736ab2012-02-27 17:30:41 -080083 mDataRowsHidden = true;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -070084
Jeff Sharkey899223b2012-08-04 15:24:58 -070085 if (mConfig.legacy) {
Chia-chi Yehae380fb2012-01-23 18:33:26 -080086 mAlertParams.mIconId = android.R.drawable.ic_dialog_info;
87 mAlertParams.mTitle = getText(R.string.legacy_title);
Chia-chi Yeh5db03df2011-06-30 23:52:29 -070088 } else {
89 PackageManager pm = getPackageManager();
Chia-chi Yehfcc1b412011-08-03 15:39:59 -070090 ApplicationInfo app = pm.getApplicationInfo(mConfig.user, 0);
Chia-chi Yehae380fb2012-01-23 18:33:26 -080091 mAlertParams.mIcon = app.loadIcon(pm);
92 mAlertParams.mTitle = app.loadLabel(pm);
Chia-chi Yeh5db03df2011-06-30 23:52:29 -070093 }
Chia-chi Yeh5db03df2011-06-30 23:52:29 -070094 if (mConfig.configureIntent != null) {
Chia-chi Yehae380fb2012-01-23 18:33:26 -080095 mAlertParams.mPositiveButtonText = getText(R.string.configure);
96 mAlertParams.mPositiveButtonListener = this;
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -070097 }
Chia-chi Yehae380fb2012-01-23 18:33:26 -080098 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 Yeh19f054b2011-06-03 17:06:29 -0700104
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700105 if (mHandler == null) {
106 mHandler = new Handler(this);
107 }
108 mHandler.sendEmptyMessage(0);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700109 } 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 Yehae380fb2012-01-23 18:33:26 -0800118 if (!isFinishing()) {
119 finish();
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700120 }
121 }
122
123 @Override
124 public void onClick(DialogInterface dialog, int which) {
125 try {
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800126 if (which == DialogInterface.BUTTON_POSITIVE) {
Chia-chi Yeh5db03df2011-06-30 23:52:29 -0700127 mConfig.configureIntent.send();
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800128 } else if (which == DialogInterface.BUTTON_NEUTRAL) {
Jeff Sharkey82f85212012-08-24 11:17:25 -0700129 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 Yeh19f054b2011-06-03 17:06:29 -0700134 }
135 } catch (Exception e) {
136 Log.e(TAG, "onClick", e);
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700137 finish();
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700138 }
139 }
140
141 @Override
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700142 public boolean handleMessage(Message message) {
143 mHandler.removeMessages(0);
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700144
Chia-chi Yehae380fb2012-01-23 18:33:26 -0800145 if (!isFinishing()) {
Vinit Deshapnde2b862e52013-10-02 11:50:39 -0700146 if (mConfig.startTime != -1) {
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700147 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 Yeh19f054b2011-06-03 17:06:29 -0700150 }
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700151
Chia-chi Yehb0736ab2012-02-27 17:30:41 -0800152 String[] numbers = getNumbers();
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700153 if (numbers != null) {
Chia-chi Yehb0736ab2012-02-27 17:30:41 -0800154 // 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 Yeh42bd53a2011-06-17 15:27:41 -0700161 // [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 Yeh19f054b2011-06-03 17:06:29 -0700170 }
Chia-chi Yeh42bd53a2011-06-17 15:27:41 -0700171 return true;
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700172 }
173
Chia-chi Yehb0736ab2012-02-27 17:30:41 -0800174 private String[] getNumbers() {
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700175 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 Yeh34e78132011-07-03 03:07:07 -0700179 String prefix = mConfig.interfaze + ':';
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700180
181 while (true) {
182 String line = in.readLine().trim();
183 if (line.startsWith(prefix)) {
184 String[] numbers = line.substring(prefix.length()).split(" +");
Chia-chi Yeh72fddaa2011-09-26 15:05:52 -0700185 for (int i = 1; i < 17; ++i) {
186 if (!numbers[i].equals("0")) {
187 return numbers;
188 }
Chia-chi Yeh19f054b2011-06-03 17:06:29 -0700189 }
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}