Update the OEM unlock preference summary to reflect the disable reason

3 cases implemented:
1) Bootloader unlocked
2) Device SIM-locked
3) Device Configuration, such as SIM-lock, has not been retrieved.

Bug: 29047618
Change-Id: Idf5cc1c9b1d105a0efd408cc2aa0e229e874442e
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 6b88636..d3716fe 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -7689,4 +7689,10 @@
     <string name="gesture_setting_on">On</string>
     <string name="gesture_setting_off">Off</string>
 
+    <!-- setting enable OEM unlock Checkbox's summary to explain this Checkbox is disabled because the bootloader has been unlocked [CHAR_LIMIT=60] -->
+    <string name="oem_unlock_enable_disabled_summary_bootloader_unlocked">Bootloader is already unlocked</string>
+    <!-- setting enable OEM unlock Checkbox's summary to explain this Checkbox is disabled because there is no connectivity. [CHAR_LIMIT=60] -->
+    <string name="oem_unlock_enable_disabled_summary_connectivity">Connect to the Internet first</string>
+    <!-- setting enable OEM unlock Checkbox's summary to explain this Checkbox is disabled because this setting is unavailable on sim-locked devices. [CHAR_LIMIT=60] -->
+    <string name="oem_unlock_enable_disabled_summary_sim_locked_device">Unavailable on carrier-locked devices</string>
 </resources>
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java
index ee6a7e9..934e752 100644
--- a/src/com/android/settings/DevelopmentSettings.java
+++ b/src/com/android/settings/DevelopmentSettings.java
@@ -64,6 +64,7 @@
 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
 import android.support.v7.preference.PreferenceGroup;
 import android.support.v7.preference.PreferenceScreen;
+import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.ThreadedRenderer;
@@ -224,6 +225,7 @@
     private UserManager mUm;
     private WifiManager mWifiManager;
     private PersistentDataBlockManager mOemUnlockManager;
+    private TelephonyManager mTelephonyManager;
 
     private SwitchBar mSwitchBar;
     private boolean mLastEnabledState;
@@ -332,6 +334,7 @@
             IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate"));
         mOemUnlockManager = (PersistentDataBlockManager)getActivity()
                 .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
+        mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 
         mDpm = (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
@@ -658,6 +661,7 @@
                 Settings.Secure.BLUETOOTH_HCI_LOG, 0) != 0);
         if (mEnableOemUnlock != null) {
             updateSwitchPreference(mEnableOemUnlock, Utils.isOemUnlockEnabled(getActivity()));
+            updateOemUnlockSettingDescription();
         }
         updateSwitchPreference(mDebugViewAttributes, Settings.Global.getInt(cr,
                 Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0);
@@ -1003,14 +1007,7 @@
     }
 
     private boolean enableOemUnlockPreference() {
-        int flashLockState = PersistentDataBlockManager.FLASH_LOCK_UNKNOWN;
-        if (mOemUnlockManager != null) {
-            flashLockState = mOemUnlockManager.getFlashLockState();
-        }
-
-        return flashLockState != PersistentDataBlockManager.FLASH_LOCK_UNLOCKED
-                && Settings.Global.getInt(getActivity().getContentResolver(),
-                        Settings.Global.OEM_UNLOCK_DISALLOWED, 0) == 0;
+        return !isBootloaderUnlocked() && isOemUnlockAllowed();
     }
 
     private void updateOemUnlockOptions() {
@@ -2181,4 +2178,54 @@
                 .show();
 
     }
+
+    private void updateOemUnlockSettingDescription() {
+        if (mEnableOemUnlock != null) {
+            int oemUnlockSummary = R.string.oem_unlock_enable_summary;
+            if (isBootloaderUnlocked()) {
+                oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_bootloader_unlocked;
+            } else if (isSimLockedDevice()) {
+                oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_sim_locked_device;
+            } else if (!isOemUnlockAllowed()) {
+                // If the device isn't SIM-locked but OEM unlock is disabled by Global setting, this
+                // means the device hasn't been able to confirm whether SIM-lock or any other
+                // restrictions apply (or hasn't been able to apply such restrictions yet). Ask the
+                // user to connect to the internet in order to retrieve all restrictions.
+                oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_connectivity;
+            }
+            mEnableOemUnlock.setSummary(getString(oemUnlockSummary));
+        }
+    }
+
+    /** Returns {@code true} if the device is SIM-locked. Otherwise, returns {@code false}. */
+    private boolean isSimLockedDevice() {
+        int phoneCount = mTelephonyManager.getPhoneCount();
+        for (int i = 0; i < phoneCount; i++) {
+            if (mTelephonyManager.getAllowedCarriers(i).size() > 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns {@code true} if the bootloader has been unlocked. Otherwise, returns {code false}.
+     */
+    private boolean isBootloaderUnlocked() {
+        int flashLockState = PersistentDataBlockManager.FLASH_LOCK_UNKNOWN;
+        if (mOemUnlockManager != null) {
+            flashLockState = mOemUnlockManager.getFlashLockState();
+        }
+
+        return flashLockState == PersistentDataBlockManager.FLASH_LOCK_UNLOCKED;
+    }
+
+    /**
+     * Returns {@code true} if OEM unlock is not disabled by Global policy. Otherwise, returns
+     * {@code false}.
+     */
+    private boolean isOemUnlockAllowed() {
+        return Settings.Global.getInt(getActivity().getContentResolver(),
+                Settings.Global.OEM_UNLOCK_DISALLOWED, 0) == 0;
+    }
 }