Status.java revision 9a23adf69dc53126c9858b19760eab5b67c23b97
1/*
2 * Copyright (C) 2008 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.settings.deviceinfo;
18
19import android.bluetooth.BluetoothAdapter;
20import android.content.BroadcastReceiver;
21import android.content.ClipboardManager;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.res.Resources;
26import android.net.ConnectivityManager;
27import android.net.wifi.WifiInfo;
28import android.net.wifi.WifiManager;
29import android.os.Build;
30import android.os.Bundle;
31import android.os.Handler;
32import android.os.Message;
33import android.os.SystemClock;
34import android.os.SystemProperties;
35import android.os.UserHandle;
36import android.preference.Preference;
37import android.preference.PreferenceActivity;
38import android.text.TextUtils;
39import android.view.View;
40import android.widget.AdapterView;
41import android.widget.ListAdapter;
42import android.widget.Toast;
43
44import com.android.internal.util.ArrayUtils;
45import com.android.settings.R;
46import com.android.settings.Utils;
47
48import java.lang.ref.WeakReference;
49
50/**
51 * Display the following information
52 * # Battery Strength  : TODO
53 * # Uptime
54 * # Awake Time
55 * # XMPP/buzz/tickle status : TODO
56 *
57 */
58public class Status extends PreferenceActivity {
59
60    private static final String KEY_BATTERY_STATUS = "battery_status";
61    private static final String KEY_BATTERY_LEVEL = "battery_level";
62    private static final String KEY_IP_ADDRESS = "wifi_ip_address";
63    private static final String KEY_WIFI_MAC_ADDRESS = "wifi_mac_address";
64    private static final String KEY_BT_ADDRESS = "bt_address";
65    private static final String KEY_SERIAL_NUMBER = "serial_number";
66    private static final String KEY_WIMAX_MAC_ADDRESS = "wimax_mac_address";
67    private static final String KEY_SIM_STATUS = "sim_status";
68    private static final String KEY_IMEI_INFO = "imei_info";
69
70    // Broadcasts to listen to for connectivity changes.
71    private static final String[] CONNECTIVITY_INTENTS = {
72            BluetoothAdapter.ACTION_STATE_CHANGED,
73            ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE,
74            WifiManager.LINK_CONFIGURATION_CHANGED_ACTION,
75            WifiManager.NETWORK_STATE_CHANGED_ACTION,
76    };
77
78    private static final int EVENT_UPDATE_STATS = 500;
79
80    private static final int EVENT_UPDATE_CONNECTIVITY = 600;
81
82    private ConnectivityManager mCM;
83    private WifiManager mWifiManager;
84
85    private Resources mRes;
86
87    private String mUnknown;
88    private String mUnavailable;
89
90    private Preference mUptime;
91    private Preference mBatteryStatus;
92    private Preference mBatteryLevel;
93    private Preference mBtAddress;
94    private Preference mIpAddress;
95    private Preference mWifiMacAddress;
96    private Preference mWimaxMacAddress;
97
98    private Handler mHandler;
99
100    private static class MyHandler extends Handler {
101        private WeakReference<Status> mStatus;
102
103        public MyHandler(Status activity) {
104            mStatus = new WeakReference<Status>(activity);
105        }
106
107        @Override
108        public void handleMessage(Message msg) {
109            Status status = mStatus.get();
110            if (status == null) {
111                return;
112            }
113
114            switch (msg.what) {
115                case EVENT_UPDATE_STATS:
116                    status.updateTimes();
117                    sendEmptyMessageDelayed(EVENT_UPDATE_STATS, 1000);
118                    break;
119
120                case EVENT_UPDATE_CONNECTIVITY:
121                    status.updateConnectivity();
122                    break;
123            }
124        }
125    }
126
127    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
128
129        @Override
130        public void onReceive(Context context, Intent intent) {
131            String action = intent.getAction();
132            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
133                mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent));
134                mBatteryStatus.setSummary(Utils.getBatteryStatus(getResources(), intent));
135            }
136        }
137    };
138
139    private IntentFilter mConnectivityIntentFilter;
140    private final BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() {
141        @Override
142        public void onReceive(Context context, Intent intent) {
143            String action = intent.getAction();
144            if (ArrayUtils.contains(CONNECTIVITY_INTENTS, action)) {
145                mHandler.sendEmptyMessage(EVENT_UPDATE_CONNECTIVITY);
146            }
147        }
148    };
149
150    private boolean hasBluetooth() {
151        return BluetoothAdapter.getDefaultAdapter() != null;
152    }
153
154    private boolean hasWimax() {
155        return  mCM.getNetworkInfo(ConnectivityManager.TYPE_WIMAX) != null;
156    }
157
158    @Override
159    protected void onCreate(Bundle icicle) {
160        super.onCreate(icicle);
161
162        mHandler = new MyHandler(this);
163
164        mCM = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
165        mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
166
167        addPreferencesFromResource(R.xml.device_info_status);
168        mBatteryLevel = findPreference(KEY_BATTERY_LEVEL);
169        mBatteryStatus = findPreference(KEY_BATTERY_STATUS);
170        mBtAddress = findPreference(KEY_BT_ADDRESS);
171        mWifiMacAddress = findPreference(KEY_WIFI_MAC_ADDRESS);
172        mWimaxMacAddress = findPreference(KEY_WIMAX_MAC_ADDRESS);
173        mIpAddress = findPreference(KEY_IP_ADDRESS);
174
175        mRes = getResources();
176        mUnknown = mRes.getString(R.string.device_info_default);
177        mUnavailable = mRes.getString(R.string.status_unavailable);
178
179        // Note - missing in zaku build, be careful later...
180        mUptime = findPreference("up_time");
181
182        if (!hasBluetooth()) {
183            getPreferenceScreen().removePreference(mBtAddress);
184            mBtAddress = null;
185        }
186
187        if (!hasWimax()) {
188            getPreferenceScreen().removePreference(mWimaxMacAddress);
189            mWimaxMacAddress = null;
190        }
191
192        mConnectivityIntentFilter = new IntentFilter();
193        for (String intent: CONNECTIVITY_INTENTS) {
194             mConnectivityIntentFilter.addAction(intent);
195        }
196
197        updateConnectivity();
198
199        String serial = Build.SERIAL;
200        if (serial != null && !serial.equals("")) {
201            setSummaryText(KEY_SERIAL_NUMBER, serial);
202        } else {
203            removePreferenceFromScreen(KEY_SERIAL_NUMBER);
204        }
205
206        //Remove SimStatus and Imei for Secondary user as it access Phone b/19165700
207        if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
208            removePreferenceFromScreen(KEY_SIM_STATUS);
209            removePreferenceFromScreen(KEY_IMEI_INFO);
210        }
211
212        // Make every pref on this screen copy its data to the clipboard on longpress.
213        // Super convenient for capturing the IMEI, MAC addr, serial, etc.
214        getListView().setOnItemLongClickListener(
215            new AdapterView.OnItemLongClickListener() {
216                @Override
217                public boolean onItemLongClick(AdapterView<?> parent, View view,
218                        int position, long id) {
219                    ListAdapter listAdapter = (ListAdapter) parent.getAdapter();
220                    Preference pref = (Preference) listAdapter.getItem(position);
221
222                    ClipboardManager cm = (ClipboardManager)
223                            getSystemService(Context.CLIPBOARD_SERVICE);
224                    cm.setText(pref.getSummary());
225                    Toast.makeText(
226                        Status.this,
227                        com.android.internal.R.string.text_copied,
228                        Toast.LENGTH_SHORT).show();
229                    return true;
230                }
231            });
232    }
233
234    @Override
235    protected void onResume() {
236        super.onResume();
237        registerReceiver(mConnectivityReceiver, mConnectivityIntentFilter,
238                         android.Manifest.permission.CHANGE_NETWORK_STATE, null);
239        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
240        mHandler.sendEmptyMessage(EVENT_UPDATE_STATS);
241    }
242
243    @Override
244    public void onPause() {
245        super.onPause();
246
247        unregisterReceiver(mBatteryInfoReceiver);
248        unregisterReceiver(mConnectivityReceiver);
249        mHandler.removeMessages(EVENT_UPDATE_STATS);
250    }
251
252    /**
253     * Removes the specified preference, if it exists.
254     * @param key the key for the Preference item
255     */
256    private void removePreferenceFromScreen(String key) {
257        Preference pref = findPreference(key);
258        if (pref != null) {
259            getPreferenceScreen().removePreference(pref);
260        }
261    }
262
263    /**
264     * @param preference The key for the Preference item
265     * @param property The system property to fetch
266     * @param alt The default value, if the property doesn't exist
267     */
268    private void setSummary(String preference, String property, String alt) {
269        try {
270            findPreference(preference).setSummary(
271                    SystemProperties.get(property, alt));
272        } catch (RuntimeException e) {
273
274        }
275    }
276
277    private void setSummaryText(String preference, String text) {
278            if (TextUtils.isEmpty(text)) {
279               text = mUnknown;
280             }
281             // some preferences may be missing
282             if (findPreference(preference) != null) {
283                 findPreference(preference).setSummary(text);
284             }
285    }
286
287    private void setWimaxStatus() {
288        if (mWimaxMacAddress != null) {
289            String macAddress = SystemProperties.get("net.wimax.mac.address", mUnavailable);
290            mWimaxMacAddress.setSummary(macAddress);
291        }
292    }
293
294    private void setWifiStatus() {
295        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
296        String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
297        mWifiMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);
298    }
299
300    private void setIpAddressStatus() {
301        String ipAddress = Utils.getDefaultIpAddresses(this.mCM);
302        if (ipAddress != null) {
303            mIpAddress.setSummary(ipAddress);
304        } else {
305            mIpAddress.setSummary(mUnavailable);
306        }
307    }
308
309    private void setBtStatus() {
310        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
311        if (bluetooth != null && mBtAddress != null) {
312            String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
313            if (!TextUtils.isEmpty(address)) {
314               // Convert the address to lowercase for consistency with the wifi MAC address.
315                mBtAddress.setSummary(address.toLowerCase());
316            } else {
317                mBtAddress.setSummary(mUnavailable);
318            }
319        }
320    }
321
322    void updateConnectivity() {
323        setWimaxStatus();
324        setWifiStatus();
325        setBtStatus();
326        setIpAddressStatus();
327    }
328
329    void updateTimes() {
330        long at = SystemClock.uptimeMillis() / 1000;
331        long ut = SystemClock.elapsedRealtime() / 1000;
332
333        if (ut == 0) {
334            ut = 1;
335        }
336
337        mUptime.setSummary(convert(ut));
338    }
339
340    private String pad(int n) {
341        if (n >= 10) {
342            return String.valueOf(n);
343        } else {
344            return "0" + String.valueOf(n);
345        }
346    }
347
348    private String convert(long t) {
349        int s = (int)(t % 60);
350        int m = (int)((t / 60) % 60);
351        int h = (int)((t / 3600));
352
353        return h + ":" + pad(m) + ":" + pad(s);
354    }
355}
356