Merge "Check power saver when plugged in state changes" into main
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
index 6012ecd..775f34d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
@@ -250,6 +250,7 @@
             mLevel = (int) (100f
                     * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
                     / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
+            int previousPluggedChargingSource = mPluggedChargingSource;
             mPluggedChargingSource = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
             mPluggedIn = mPluggedChargingSource != 0;
             final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
@@ -276,7 +277,9 @@
                 mIsBatteryDefender = isBatteryDefender;
                 fireIsBatteryDefenderChanged();
             }
-
+            if (mPluggedChargingSource != previousPluggedChargingSource) {
+                updatePowerSave();
+            }
             fireBatteryLevelChanged();
         } else if (action.equals(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)) {
             updatePowerSave();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java
index ed8843b..db829a2 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java
@@ -23,6 +23,8 @@
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.staticMockMarker;
 import static com.android.settingslib.fuelgauge.BatterySaverLogging.SAVER_ENABLED_QS;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -308,6 +310,52 @@
         mBatteryController.fireBatteryLevelChanged();
     }
 
+    @Test
+    public void plugAndUnplugWhenInBatterySaver_stateUpdatedWithoutBatterySaverBroadcast() {
+        PowerSaveState state = new PowerSaveState.Builder()
+                .setBatterySaverEnabled(false)
+                .build();
+        when(mPowerManager.getPowerSaveState(PowerManager.ServiceType.AOD)).thenReturn(state);
+
+        // Set up on power save and not charging
+        when(mPowerManager.isPowerSaveMode()).thenReturn(true);
+        mBatteryController.onReceive(
+                getContext(), new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
+        mBatteryController.onReceive(getContext(), createChargingIntent(false));
+
+        TestCallback callback = new TestCallback();
+        mBatteryController.addCallback(callback);
+
+        assertThat(callback.pluggedIn).isFalse();
+        assertThat(callback.powerSaverOn).isTrue();
+
+        // Plug in (battery saver turns off)
+        when(mPowerManager.isPowerSaveMode()).thenReturn(false);
+        mBatteryController.onReceive(getContext(), createChargingIntent(true));
+
+        assertThat(callback.pluggedIn).isTrue();
+        assertThat(callback.powerSaverOn).isFalse();
+
+        // Unplug (battery saver turns back on)
+        when(mPowerManager.isPowerSaveMode()).thenReturn(true);
+        mBatteryController.onReceive(getContext(), createChargingIntent(false));
+
+        assertThat(callback.pluggedIn).isFalse();
+        assertThat(callback.powerSaverOn).isTrue();
+    }
+
+    private Intent createChargingIntent(boolean charging) {
+        Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
+        if (charging) {
+            return intent
+                .putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING)
+                .putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC);
+        } else {
+            return intent
+                .putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_DISCHARGING);
+        }
+    }
+
     private void setupIncompatibleCharging() {
         final List<UsbPort> usbPorts = new ArrayList<>();
         usbPorts.add(mUsbPort);
@@ -318,4 +366,19 @@
         when(mUsbPortStatus.getComplianceWarnings())
                 .thenReturn(new int[]{UsbPortStatus.COMPLIANCE_WARNING_DEBUG_ACCESSORY});
     }
+
+    private static class TestCallback
+        implements BatteryController.BatteryStateChangeCallback {
+        boolean pluggedIn = false;
+        boolean powerSaverOn = false;
+        @Override
+        public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
+            this.pluggedIn = pluggedIn;
+        }
+
+        @Override
+        public void onPowerSaveChanged(boolean isPowerSave) {
+            this.powerSaverOn = isPowerSave;
+        }
+    }
 }