OSDN Git Service

Cache preferences and services in status dialog.
authorLorenzo Colitti <lorenzo@google.com>
Thu, 7 Nov 2013 18:53:29 +0000 (03:53 +0900)
committerLorenzo Colitti <lorenzo@google.com>
Mon, 25 Nov 2013 08:34:08 +0000 (17:34 +0900)
Currently, the status dialog fetches the preferences and system
services that it uses to display connectivity information (e.g.,
IP and MAC addresses) only when the dialog is first opened. This
is fine because the connectivity information is only filled in
once and never updated.

In order to have this information update dynamically (like the
rest of the dialog), fetch and cache these objects in onCreate.
The next change will actually make this information update on
network changes.

[Cherry-pick of 6eb6a908217e080aea43406028be491d269bdbb0]

Bug: 10232006
Change-Id: Ib4072674543e517cf6935e3e03f35090e728090a

src/com/android/settings/Utils.java
src/com/android/settings/deviceinfo/Status.java

index a8d0e8f..b9e729c 100644 (file)
@@ -345,9 +345,7 @@ public class Utils {
      * @param context the application context
      * @return the formatted and newline-separated IP addresses, or null if none.
      */
-    public static String getDefaultIpAddresses(Context context) {
-        ConnectivityManager cm = (ConnectivityManager)
-                context.getSystemService(Context.CONNECTIVITY_SERVICE);
+    public static String getDefaultIpAddresses(ConnectivityManager cm) {
         LinkProperties prop = cm.getActiveLinkProperties();
         return formatIpAddresses(prop);
     }
index 6a15027..707e528 100644 (file)
@@ -122,18 +122,26 @@ public class Status extends PreferenceActivity {
 
     private static final int EVENT_UPDATE_STATS = 500;
 
+    private ConnectivityManager mCM;
     private TelephonyManager mTelephonyManager;
+    private WifiManager mWifiManager;
+
     private Phone mPhone = null;
     private PhoneStateIntentReceiver mPhoneStateReceiver;
     private Resources mRes;
-    private Preference mSignalStrength;
-    private Preference mUptime;
     private boolean mShowLatestAreaInfo;
 
-    private String sUnknown;
+    private String mUnknown;
+    private String mUnavailable;
 
+    private Preference mSignalStrength;
+    private Preference mUptime;
     private Preference mBatteryStatus;
     private Preference mBatteryLevel;
+    private Preference mBtAddress;
+    private Preference mIpAddress;
+    private Preference mWifiMacAddress;
+    private Preference mWimaxMacAddress;
 
     private Handler mHandler;
 
@@ -207,20 +215,36 @@ public class Status extends PreferenceActivity {
         }
     };
 
+    private boolean hasBluetooth() {
+        return BluetoothAdapter.getDefaultAdapter() != null;
+    }
+
+    private boolean hasWimax() {
+        return  mCM.getNetworkInfo(ConnectivityManager.TYPE_WIMAX) != null;
+    }
+
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
         mHandler = new MyHandler(this);
 
+        mCM = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
         mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
+        mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
 
         addPreferencesFromResource(R.xml.device_info_status);
         mBatteryLevel = findPreference(KEY_BATTERY_LEVEL);
         mBatteryStatus = findPreference(KEY_BATTERY_STATUS);
+        mBtAddress = findPreference(KEY_BT_ADDRESS);
+        mWifiMacAddress = findPreference(KEY_WIFI_MAC_ADDRESS);
+        mWimaxMacAddress = findPreference(KEY_WIMAX_MAC_ADDRESS);
+        mIpAddress = findPreference(KEY_IP_ADDRESS);
 
         mRes = getResources();
-        sUnknown = mRes.getString(R.string.device_info_default);
+        mUnknown = mRes.getString(R.string.device_info_default);
+        mUnavailable = mRes.getString(R.string.status_unavailable);
+
         if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
             mPhone = PhoneFactory.getDefaultPhone();
         }
@@ -291,10 +315,17 @@ public class Status extends PreferenceActivity {
             }
         }
 
-        setWimaxStatus();
-        setWifiStatus();
-        setBtStatus();
-        setIpAddressStatus();
+        if (!hasBluetooth()) {
+            getPreferenceScreen().removePreference(mBtAddress);
+            mBtAddress = null;
+        }
+
+        if (!hasWimax()) {
+            getPreferenceScreen().removePreference(mWimaxMacAddress);
+            mWimaxMacAddress = null;
+        }
+
+        updateConnectivity();
 
         String serial = Build.SERIAL;
         if (serial != null && !serial.equals("")) {
@@ -371,7 +402,7 @@ public class Status extends PreferenceActivity {
 
     private void setSummaryText(String preference, String text) {
             if (TextUtils.isEmpty(text)) {
-               text = sUnknown;
+               text = mUnknown;
              }
              // some preferences may be missing
              if (findPreference(preference) != null) {
@@ -474,55 +505,47 @@ public class Status extends PreferenceActivity {
     }
 
     private void setWimaxStatus() {
-        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
-        NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
-
-        if (ni == null) {
-            PreferenceScreen root = getPreferenceScreen();
-            Preference ps = (Preference) findPreference(KEY_WIMAX_MAC_ADDRESS);
-            if (ps != null) root.removePreference(ps);
-        } else {
-            Preference wimaxMacAddressPref = findPreference(KEY_WIMAX_MAC_ADDRESS);
-            String macAddress = SystemProperties.get("net.wimax.mac.address",
-                    getString(R.string.status_unavailable));
-            wimaxMacAddressPref.setSummary(macAddress);
+        if (mWimaxMacAddress != null) {
+            String macAddress = SystemProperties.get("net.wimax.mac.address", mUnavailable);
+            mWimaxMacAddress.setSummary(macAddress);
         }
     }
-    private void setWifiStatus() {
-        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
-        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
-
-        Preference wifiMacAddressPref = findPreference(KEY_WIFI_MAC_ADDRESS);
 
+    private void setWifiStatus() {
+        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
         String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
-        wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
-                : getString(R.string.status_unavailable));
+        mWifiMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);
     }
 
     private void setIpAddressStatus() {
-        Preference ipAddressPref = findPreference(KEY_IP_ADDRESS);
-        String ipAddress = Utils.getDefaultIpAddresses(this);
+        String ipAddress = Utils.getDefaultIpAddresses(this.mCM);
         if (ipAddress != null) {
-            ipAddressPref.setSummary(ipAddress);
+            mIpAddress.setSummary(ipAddress);
         } else {
-            ipAddressPref.setSummary(getString(R.string.status_unavailable));
+            mIpAddress.setSummary(mUnavailable);
         }
     }
 
     private void setBtStatus() {
         BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
-        Preference btAddressPref = findPreference(KEY_BT_ADDRESS);
-
-        if (bluetooth == null) {
-            // device not BT capable
-            getPreferenceScreen().removePreference(btAddressPref);
-        } else {
+        if (bluetooth != null && mBtAddress != null) {
             String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
-            btAddressPref.setSummary(!TextUtils.isEmpty(address) ? address
-                    : getString(R.string.status_unavailable));
+            if (!TextUtils.isEmpty(address)) {
+               // Convert the address to lowercase for consistency with the wifi MAC address.
+                mBtAddress.setSummary(address.toLowerCase());
+            } else {
+                mBtAddress.setSummary(mUnavailable);
+            }
         }
     }
 
+    void updateConnectivity() {
+        setWimaxStatus();
+        setWifiStatus();
+        setBtStatus();
+        setIpAddressStatus();
+    }
+
     void updateTimes() {
         long at = SystemClock.uptimeMillis() / 1000;
         long ut = SystemClock.elapsedRealtime() / 1000;