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