Fix 3391330: Use BATTERY_STATUS_FULL as "Charged" state

Some devices that use LiPo batteries do not charge them to 100%
as a safety margin and to preserve battery longevity.  This change
allows KeyguardUpdateMonitor to determine when the battery state should be
reported as "Charged", provided the device sets BATTERY_STATUS_FULL in
that case.

Manual merge of Change-Id: Iac6cb78e24f9a696017459cc773c38ef7fe7779f

Change-Id: I15c316a17108c064bf2c7e657ca908f8767be936
diff --git a/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java b/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java
index e775dac..5ed67a9 100644
--- a/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java
+++ b/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java
@@ -26,6 +26,7 @@
 import static android.os.BatteryManager.BATTERY_STATUS_FULL;
 import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
 import android.media.AudioManager;
+import android.os.BatteryManager;
 import android.os.Handler;
 import android.os.Message;
 import android.os.SystemClock;
@@ -70,12 +71,12 @@
 
     private boolean mKeyguardBypassEnabled;
 
-    private boolean mDevicePluggedIn;
-
     private boolean mDeviceProvisioned;
 
     private int mBatteryLevel;
 
+    private int mBatteryStatus;
+
     private CharSequence mTelephonyPlmn;
     private CharSequence mTelephonySpn;
 
@@ -203,7 +204,7 @@
 
         // take a guess to start
         mSimState = IccCard.State.READY;
-        mDevicePluggedIn = true;
+        mBatteryStatus = BATTERY_STATUS_FULL;
         mBatteryLevel = 100;
 
         mTelephonyPlmn = getDefaultPlmn();
@@ -283,13 +284,12 @@
     /**
      * Handle {@link #MSG_BATTERY_UPDATE}
      */
-    private void handleBatteryUpdate(int pluggedInStatus, int batteryLevel) {
+    private void handleBatteryUpdate(int batteryStatus, int batteryLevel) {
         if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
-        final boolean pluggedIn = isPluggedIn(pluggedInStatus);
-
-        if (isBatteryUpdateInteresting(pluggedIn, batteryLevel)) {
+        if (isBatteryUpdateInteresting(batteryStatus, batteryLevel)) {
+            mBatteryStatus = batteryStatus;
             mBatteryLevel = batteryLevel;
-            mDevicePluggedIn = pluggedIn;
+            final boolean pluggedIn = isPluggedIn(batteryStatus);;
             for (int i = 0; i < mInfoCallbacks.size(); i++) {
                 mInfoCallbacks.get(i).onRefreshBatteryInfo(
                         shouldShowBatteryInfo(), pluggedIn, batteryLevel);
@@ -336,26 +336,34 @@
         return status == BATTERY_STATUS_CHARGING || status == BATTERY_STATUS_FULL;
     }
 
-    private boolean isBatteryUpdateInteresting(boolean pluggedIn, int batteryLevel) {
+    private boolean isBatteryUpdateInteresting(int batteryStatus, int batteryLevel) {
         // change in plug is always interesting
-        if (mDevicePluggedIn != pluggedIn) {
+        final boolean isPluggedIn = isPluggedIn(batteryStatus);
+        final boolean wasPluggedIn = isPluggedIn(mBatteryStatus);
+        final boolean stateChangedWhilePluggedIn =
+            wasPluggedIn == true && isPluggedIn == true && (mBatteryStatus != batteryStatus);
+        if (wasPluggedIn != isPluggedIn || stateChangedWhilePluggedIn) {
             return true;
         }
 
         // change in battery level while plugged in
-        if (pluggedIn && mBatteryLevel != batteryLevel) {
+        if (isPluggedIn && mBatteryLevel != batteryLevel) {
             return true;
         }
 
-        if (!pluggedIn) {
+        if (!isPluggedIn) {
             // not plugged in and below threshold
-            if (batteryLevel < LOW_BATTERY_THRESHOLD && batteryLevel != mBatteryLevel) {
+            if (isBatteryLow(batteryLevel) && batteryLevel != mBatteryLevel) {
                 return true;
             }
         }
         return false;
     }
 
+    private boolean isBatteryLow(int batteryLevel) {
+        return batteryLevel < LOW_BATTERY_THRESHOLD;
+    }
+
     /**
      * @param intent The intent with action {@link Telephony.Intents#SPN_STRINGS_UPDATED_ACTION}
      * @return The string to use for the plmn, or null if it should not be shown.
@@ -485,7 +493,12 @@
     }
 
     public boolean isDevicePluggedIn() {
-        return mDevicePluggedIn;
+        return isPluggedIn(mBatteryStatus);
+    }
+
+    public boolean isDeviceCharged() {
+        return mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL
+                || mBatteryLevel >= 100; // in case a particular device doesn't flag it
     }
 
     public int getBatteryLevel() {
@@ -493,7 +506,7 @@
     }
 
     public boolean shouldShowBatteryInfo() {
-        return mDevicePluggedIn || mBatteryLevel < LOW_BATTERY_THRESHOLD;
+        return isPluggedIn(mBatteryStatus) || isBatteryLow(mBatteryLevel);
     }
 
     public CharSequence getTelephonyPlmn() {
diff --git a/policy/src/com/android/internal/policy/impl/StatusView.java b/policy/src/com/android/internal/policy/impl/StatusView.java
index da7bbb8..2aff4a8 100644
--- a/policy/src/com/android/internal/policy/impl/StatusView.java
+++ b/policy/src/com/android/internal/policy/impl/StatusView.java
@@ -51,6 +51,7 @@
     private LockPatternUtils mLockPatternUtils;
     private int mHelpMessageId;
     private int mHelpIconId;
+    private KeyguardUpdateMonitor mUpdateMonitor;
 
     private View findViewById(int id) {
         return mView.findViewById(id);
@@ -97,6 +98,7 @@
         mHasDate = (mDate != null);
         mDateFormatString = getContext().getString(R.string.full_wday_month_day_no_year);
         mLockPatternUtils = lockPatternUtils;
+        mUpdateMonitor = updateMonitor;
 
         refreshTimeAndDateDisplay();
 
@@ -186,7 +188,7 @@
             // Battery status
             if (mPluggedIn) {
                 // Charging or charged
-                if (mBatteryLevel >= 100) {
+                if (mUpdateMonitor.isDeviceCharged()) {
                     mStatus1.setText(getContext().getString(R.string.lockscreen_charged));
                 } else {
                     mStatus1.setText(getContext().getString(R.string.lockscreen_plugged_in,