ManageDialog.java revision b0736abca910fbba8c31c002d7335360f5bc0929
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    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.user.equals(VpnConfig.LEGACY_VPN)) {
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                mService.prepareVpn(mConfig.user, VpnConfig.LEGACY_VPN);
127            }
128        } catch (Exception e) {
129            Log.e(TAG, "onClick", e);
130            finish();
131        }
132    }
133
134    @Override
135    public boolean handleMessage(Message message) {
136        mHandler.removeMessages(0);
137
138        if (!isFinishing()) {
139            if (mConfig.startTime != 0) {
140                long seconds = (SystemClock.elapsedRealtime() - mConfig.startTime) / 1000;
141                mDuration.setText(String.format("%02d:%02d:%02d",
142                        seconds / 3600, seconds / 60 % 60, seconds % 60));
143            }
144
145            String[] numbers = getNumbers();
146            if (numbers != null) {
147                // First unhide the related data rows.
148                if (mDataRowsHidden) {
149                    findViewById(R.id.data_transmitted_row).setVisibility(View.VISIBLE);
150                    findViewById(R.id.data_received_row).setVisibility(View.VISIBLE);
151                    mDataRowsHidden = false;
152                }
153
154                // [1] and [2] are received data in bytes and packets.
155                mDataReceived.setText(getString(R.string.data_value_format,
156                        numbers[1], numbers[2]));
157
158                // [9] and [10] are transmitted data in bytes and packets.
159                mDataTransmitted.setText(getString(R.string.data_value_format,
160                        numbers[9], numbers[10]));
161            }
162            mHandler.sendEmptyMessageDelayed(0, 1000);
163        }
164        return true;
165    }
166
167    private String[] getNumbers() {
168        DataInputStream in = null;
169        try {
170            // See dev_seq_printf_stats() in net/core/dev.c.
171            in = new DataInputStream(new FileInputStream("/proc/net/dev"));
172            String prefix = mConfig.interfaze + ':';
173
174            while (true) {
175                String line = in.readLine().trim();
176                if (line.startsWith(prefix)) {
177                    String[] numbers = line.substring(prefix.length()).split(" +");
178                    for (int i = 1; i < 17; ++i) {
179                        if (!numbers[i].equals("0")) {
180                            return numbers;
181                        }
182                    }
183                    break;
184                }
185            }
186        } catch (Exception e) {
187            // ignore
188        } finally {
189            try {
190                in.close();
191            } catch (Exception e) {
192                // ignore
193            }
194        }
195        return null;
196    }
197}
198