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