2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.deviceinfo;
19 import android.bluetooth.BluetoothAdapter;
20 import android.content.BroadcastReceiver;
21 import android.content.ClipboardManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.res.Resources;
26 import android.net.ConnectivityManager;
27 import android.net.NetworkInfo;
28 import android.net.wifi.WifiInfo;
29 import android.net.wifi.WifiManager;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.os.Handler;
33 import android.os.Message;
34 import android.os.SystemClock;
35 import android.os.SystemProperties;
36 import android.os.UserHandle;
37 import android.preference.Preference;
38 import android.preference.PreferenceActivity;
39 import android.text.TextUtils;
40 import android.view.View;
41 import android.widget.AdapterView;
42 import android.widget.ListAdapter;
43 import android.widget.Toast;
45 import com.android.internal.util.ArrayUtils;
46 import com.android.settings.R;
47 import com.android.settings.Utils;
49 import java.lang.ref.WeakReference;
52 * Display the following information
53 * # Battery Strength : TODO
56 * # XMPP/buzz/tickle status : TODO
59 public class Status extends PreferenceActivity {
61 private static final String KEY_BATTERY_STATUS = "battery_status";
62 private static final String KEY_BATTERY_LEVEL = "battery_level";
63 private static final String KEY_IP_ADDRESS = "wifi_ip_address";
64 private static final String KEY_WIFI_MAC_ADDRESS = "wifi_mac_address";
65 private static final String KEY_BT_ADDRESS = "bt_address";
66 private static final String KEY_SERIAL_NUMBER = "serial_number";
67 private static final String KEY_WIMAX_MAC_ADDRESS = "wimax_mac_address";
68 private static final String KEY_SIM_STATUS = "sim_status";
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,
78 private static final int EVENT_UPDATE_STATS = 500;
80 private static final int EVENT_UPDATE_CONNECTIVITY = 600;
82 private ConnectivityManager mCM;
83 private WifiManager mWifiManager;
85 private Resources mRes;
87 private String mUnknown;
88 private String mUnavailable;
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;
98 private Handler mHandler;
100 private static class MyHandler extends Handler {
101 private WeakReference<Status> mStatus;
103 public MyHandler(Status activity) {
104 mStatus = new WeakReference<Status>(activity);
108 public void handleMessage(Message msg) {
109 Status status = mStatus.get();
110 if (status == null) {
115 case EVENT_UPDATE_STATS:
116 status.updateTimes();
117 sendEmptyMessageDelayed(EVENT_UPDATE_STATS, 1000);
120 case EVENT_UPDATE_CONNECTIVITY:
121 status.updateConnectivity();
127 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
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));
139 private IntentFilter mConnectivityIntentFilter;
140 private final BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() {
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);
150 private boolean hasBluetooth() {
151 return BluetoothAdapter.getDefaultAdapter() != null;
154 private boolean hasWimax() {
155 return mCM.getNetworkInfo(ConnectivityManager.TYPE_WIMAX) != null;
159 protected void onCreate(Bundle icicle) {
160 super.onCreate(icicle);
162 mHandler = new MyHandler(this);
164 mCM = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
165 mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
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);
175 mRes = getResources();
176 mUnknown = mRes.getString(R.string.device_info_default);
177 mUnavailable = mRes.getString(R.string.status_unavailable);
179 // Note - missing in zaku build, be careful later...
180 mUptime = findPreference("up_time");
182 if (!hasBluetooth()) {
183 getPreferenceScreen().removePreference(mBtAddress);
188 getPreferenceScreen().removePreference(mWimaxMacAddress);
189 mWimaxMacAddress = null;
192 mConnectivityIntentFilter = new IntentFilter();
193 for (String intent: CONNECTIVITY_INTENTS) {
194 mConnectivityIntentFilter.addAction(intent);
197 updateConnectivity();
199 String serial = Build.SERIAL;
200 if (serial != null && !serial.equals("")) {
201 setSummaryText(KEY_SERIAL_NUMBER, serial);
203 removePreferenceFromScreen(KEY_SERIAL_NUMBER);
206 // Make every pref on this screen copy its data to the clipboard on longpress.
207 // Super convenient for capturing the IMEI, MAC addr, serial, etc.
208 getListView().setOnItemLongClickListener(
209 new AdapterView.OnItemLongClickListener() {
211 public boolean onItemLongClick(AdapterView<?> parent, View view,
212 int position, long id) {
213 ListAdapter listAdapter = (ListAdapter) parent.getAdapter();
214 Preference pref = (Preference) listAdapter.getItem(position);
216 ClipboardManager cm = (ClipboardManager)
217 getSystemService(Context.CLIPBOARD_SERVICE);
218 cm.setText(pref.getSummary());
221 com.android.internal.R.string.text_copied,
222 Toast.LENGTH_SHORT).show();
229 protected void onResume() {
231 registerReceiver(mConnectivityReceiver, mConnectivityIntentFilter,
232 android.Manifest.permission.CHANGE_NETWORK_STATE, null);
233 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
234 mHandler.sendEmptyMessage(EVENT_UPDATE_STATS);
238 public void onPause() {
241 unregisterReceiver(mBatteryInfoReceiver);
242 unregisterReceiver(mConnectivityReceiver);
243 mHandler.removeMessages(EVENT_UPDATE_STATS);
247 * Removes the specified preference, if it exists.
248 * @param key the key for the Preference item
250 private void removePreferenceFromScreen(String key) {
251 Preference pref = findPreference(key);
253 getPreferenceScreen().removePreference(pref);
258 * @param preference The key for the Preference item
259 * @param property The system property to fetch
260 * @param alt The default value, if the property doesn't exist
262 private void setSummary(String preference, String property, String alt) {
264 findPreference(preference).setSummary(
265 SystemProperties.get(property, alt));
266 } catch (RuntimeException e) {
271 private void setSummaryText(String preference, String text) {
272 if (TextUtils.isEmpty(text)) {
275 // some preferences may be missing
276 if (findPreference(preference) != null) {
277 findPreference(preference).setSummary(text);
281 private void setWimaxStatus() {
282 if (mWimaxMacAddress != null) {
283 String macAddress = SystemProperties.get("net.wimax.mac.address", mUnavailable);
284 mWimaxMacAddress.setSummary(macAddress);
288 private void setWifiStatus() {
289 WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
290 String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
291 mWifiMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);
294 private void setIpAddressStatus() {
295 String ipAddress = Utils.getDefaultIpAddresses(this.mCM);
296 if (ipAddress != null) {
297 mIpAddress.setSummary(ipAddress);
299 mIpAddress.setSummary(mUnavailable);
303 private void setBtStatus() {
304 BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
305 if (bluetooth != null && mBtAddress != null) {
306 String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
307 if (!TextUtils.isEmpty(address)) {
308 // Convert the address to lowercase for consistency with the wifi MAC address.
309 mBtAddress.setSummary(address.toLowerCase());
311 mBtAddress.setSummary(mUnavailable);
316 void updateConnectivity() {
320 setIpAddressStatus();
324 long at = SystemClock.uptimeMillis() / 1000;
325 long ut = SystemClock.elapsedRealtime() / 1000;
331 mUptime.setSummary(convert(ut));
334 private String pad(int n) {
336 return String.valueOf(n);
338 return "0" + String.valueOf(n);
342 private String convert(long t) {
343 int s = (int)(t % 60);
344 int m = (int)((t / 60) % 60);
345 int h = (int)((t / 3600));
347 return h + ":" + pad(m) + ":" + pad(s);