ManageDialog.java revision 19f054b0f69b2f56ea0e98a3bb7e0e62b90ff480
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.app.Activity;
20import android.app.AlertDialog;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.net.ConnectivityManager;
27import android.os.Handler;
28import android.os.Message;
29import android.os.SystemClock;
30import android.util.Log;
31import android.view.View;
32import android.widget.Button;
33import android.widget.CompoundButton;
34import android.widget.ImageView;
35import android.widget.TextView;
36
37import java.io.DataInputStream;
38import java.io.FileInputStream;
39
40public class ManageDialog extends Activity implements
41        DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
42    private static final String TAG = "VpnManage";
43
44    private String mPackageName;
45    private String mInterfaceName;
46    private long mStartTime;
47
48    private ConnectivityManager mService;
49
50    private AlertDialog mDialog;
51    private TextView mDuration;
52    private TextView mDataTransmitted;
53    private TextView mDataReceived;
54
55    private Updater mUpdater;
56
57    @Override
58    protected void onResume() {
59        super.onResume();
60        try {
61            Intent intent = getIntent();
62            // TODO: Move constants into VpnBuilder.
63            mPackageName = intent.getStringExtra("packageName");
64            mInterfaceName = intent.getStringExtra("interfaceName");
65            mStartTime = intent.getLongExtra("startTime", 0);
66
67            mService = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
68
69            PackageManager pm = getPackageManager();
70            ApplicationInfo app = pm.getApplicationInfo(mPackageName, 0);
71
72            View view = View.inflate(this, R.layout.manage, null);
73            String session = intent.getStringExtra("session");
74            if (session != null) {
75                ((TextView) view.findViewById(R.id.session)).setText(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
81            mDialog = new AlertDialog.Builder(this)
82                    .setIcon(app.loadIcon(pm))
83                    .setTitle(app.loadLabel(pm))
84                    .setView(view)
85                    .setPositiveButton(R.string.configure, this)
86                    .setNeutralButton(R.string.disconnect, this)
87                    .setNegativeButton(android.R.string.cancel, this)
88                    .create();
89            mDialog.setOnDismissListener(this);
90            mDialog.show();
91
92            mUpdater = new Updater();
93            mUpdater.sendEmptyMessage(0);
94        } catch (Exception e) {
95            Log.e(TAG, "onResume", e);
96            finish();
97        }
98    }
99
100    @Override
101    protected void onPause() {
102        super.onPause();
103        if (mDialog != null) {
104            mDialog.setOnDismissListener(null);
105            mDialog.dismiss();
106        }
107    }
108
109    @Override
110    public void onClick(DialogInterface dialog, int which) {
111        try {
112            if (which == AlertDialog.BUTTON_POSITIVE) {
113                Intent intent = new Intent(Intent.ACTION_MAIN);
114                intent.setPackage(mPackageName);
115                startActivity(intent);
116            } else if (which == AlertDialog.BUTTON_NEUTRAL) {
117                mService.prepareVpn(mPackageName);
118            }
119        } catch (Exception e) {
120            Log.e(TAG, "onClick", e);
121        }
122    }
123
124    @Override
125    public void onDismiss(DialogInterface dialog) {
126        finish();
127    }
128
129    private class Updater extends Handler {
130        public void handleMessage(Message message) {
131            removeMessages(0);
132
133            if (mDialog.isShowing()) {
134                if (mStartTime != 0) {
135                    long seconds = (SystemClock.elapsedRealtime() - mStartTime) / 1000;
136                    mDuration.setText(String.format("%02d:%02d:%02d",
137                            seconds / 3600, seconds / 60 % 60, seconds % 60));
138                }
139
140                String[] numbers = getStatistics();
141                if (numbers != null) {
142                    // [1] and [2] are received data in bytes and packets.
143                    mDataReceived.setText(getString(R.string.data_value_format,
144                            numbers[1], numbers[2]));
145
146                    // [9] and [10] are transmitted data in bytes and packets.
147                    mDataTransmitted.setText(getString(R.string.data_value_format,
148                            numbers[9], numbers[10]));
149                }
150                sendEmptyMessageDelayed(0, 1000);
151            }
152        }
153    }
154
155    private String[] getStatistics() {
156        DataInputStream in = null;
157        try {
158            // See dev_seq_printf_stats() in net/core/dev.c.
159            in = new DataInputStream(new FileInputStream("/proc/net/dev"));
160            String prefix = mInterfaceName + ':';
161
162            while (true) {
163                String line = in.readLine().trim();
164                if (line.startsWith(prefix)) {
165                    String[] numbers = line.substring(prefix.length()).split(" +");
166                    if (numbers.length == 17) {
167                        return numbers;
168                    }
169                    break;
170                }
171            }
172        } catch (Exception e) {
173            // ignore
174        } finally {
175            try {
176                in.close();
177            } catch (Exception e) {
178                // ignore
179            }
180        }
181        return null;
182    }
183}
184