Merge "Distribute all the hidden items to apps"
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index c0c92f0..309830b 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -344,23 +344,20 @@
             final List<BatterySipper> usageList = getCoalescedUsageList(
                     USE_FAKE_DATA ? getFakeStats() : mStatsHelper.getUsageList());
 
-            final double screenPowerMah = removeScreenBatterySipper(usageList);
+            final double hiddenPowerMah = removeHiddenBatterySippers(usageList);
 
             final int dischargeAmount = USE_FAKE_DATA ? 5000
                     : stats != null ? stats.getDischargeAmount(mStatsType) : 0;
             final int numSippers = usageList.size();
             for (int i = 0; i < numSippers; i++) {
                 final BatterySipper sipper = usageList.get(i);
-                if (shouldHideSipper(sipper)) {
-                    continue;
-                }
-
-                // Deduct the screen power from total power, used to calculate percentOfTotal
+                // Deduct the power of hidden items from total power, which is used to
+                // calculate percentOfTotal
                 double totalPower = USE_FAKE_DATA ?
-                        4000 : mStatsHelper.getTotalPower() - screenPowerMah;
+                        4000 : mStatsHelper.getTotalPower() - hiddenPowerMah;
 
                 // With deduction in totalPower, percentOfTotal is higher because it adds the part
-                // used in screen
+                // used in screen, system, etc
                 final double percentOfTotal =
                         ((sipper.totalPowerMah / totalPower) * dischargeAmount);
 
@@ -451,7 +448,8 @@
         final int uid = sipper.getUid();
 
         return drainType == DrainType.IDLE || drainType == DrainType.CELL
-                || uid == Process.ROOT_UID || uid == Process.SYSTEM_UID
+                || drainType == DrainType.SCREEN || uid == Process.ROOT_UID
+                || uid == Process.SYSTEM_UID
                 || (sipper.totalPowerMah * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP;
     }
 
@@ -470,16 +468,17 @@
     }
 
     @VisibleForTesting
-    double removeScreenBatterySipper(List<BatterySipper> sippers) {
-        for (int i = 0, size = sippers.size(); i < size; i++) {
+    double removeHiddenBatterySippers(List<BatterySipper> sippers) {
+        double totalPowerMah = 0;
+        for (int i = sippers.size() - 1; i >= 0; i--) {
             final BatterySipper sipper = sippers.get(i);
-            if (sipper.drainType == DrainType.SCREEN) {
+            if (shouldHideSipper(sipper)) {
                 sippers.remove(i);
-                return sipper.totalPowerMah;
+                totalPowerMah += sipper.totalPowerMah;
             }
         }
 
-        return 0;
+        return totalPowerMah;
     }
 
     private static List<BatterySipper> getFakeStats() {
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
index 63cb5e1..60e1f50 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
@@ -44,7 +44,6 @@
 import static com.android.settings.fuelgauge.PowerUsageSummary.MENU_ADDITIONAL_BATTERY_INFO;
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
@@ -61,6 +60,7 @@
     private static final int UID = 123;
     private static final int POWER_MAH = 100;
     private static final double BATTERY_SCREEN_USAGE = 300;
+    private static final double BATTERY_SYSTEM_USAGE = 600;
     private static final double PRECISION = 0.001;
     private static final Intent ADDITIONAL_BATTERY_INFO_INTENT =
             new Intent("com.example.app.ADDITIONAL_BATTERY_INFO");
@@ -80,6 +80,8 @@
     @Mock
     private BatterySipper mScreenBatterySipper;
     @Mock
+    private BatterySipper mSystemBatterySipper;
+    @Mock
     private PowerGaugePreference mPreference;
 
     private TestFragment mFragment;
@@ -114,6 +116,9 @@
 
         mScreenBatterySipper.drainType = BatterySipper.DrainType.SCREEN;
         mScreenBatterySipper.totalPowerMah = BATTERY_SCREEN_USAGE;
+        mSystemBatterySipper.drainType = BatterySipper.DrainType.APP;
+        mSystemBatterySipper.totalPowerMah = BATTERY_SYSTEM_USAGE;
+        when(mSystemBatterySipper.getUid()).thenReturn(Process.SYSTEM_UID);
     }
 
     @Test
@@ -162,17 +167,6 @@
     }
 
     @Test
-    public void testRemoveScreenBatterySipper_ContainsScreenSipper_RemoveAndReturnValue() {
-        final List<BatterySipper> sippers = new ArrayList<>();
-        sippers.add(mNormalBatterySipper);
-        sippers.add(mScreenBatterySipper);
-
-        final double screenUsage = mPowerUsageSummary.removeScreenBatterySipper(sippers);
-        assertThat(sippers).containsExactly(mNormalBatterySipper);
-        assertThat(screenUsage).isWithin(PRECISION).of(BATTERY_SCREEN_USAGE);
-    }
-
-    @Test
     public void testExtractKeyFromSipper_TypeAPPUidObjectNotNull_ReturnUid() {
         mNormalBatterySipper.uidObj = new BatteryStatsImpl.Uid(new BatteryStatsImpl(), UID);
         mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
@@ -182,6 +176,18 @@
     }
 
     @Test
+    public void testRemoveHiddenBatterySippers_ContainsHiddenSippers_RemoveAndReturnValue() {
+        final List<BatterySipper> sippers = new ArrayList<>();
+        sippers.add(mNormalBatterySipper);
+        sippers.add(mScreenBatterySipper);
+        sippers.add(mSystemBatterySipper);
+
+        final double totalUsage = mPowerUsageSummary.removeHiddenBatterySippers(sippers);
+        assertThat(sippers).containsExactly(mNormalBatterySipper);
+        assertThat(totalUsage).isWithin(PRECISION).of(BATTERY_SCREEN_USAGE + BATTERY_SYSTEM_USAGE);
+    }
+
+    @Test
     public void testShouldHideSipper_TypeIdle_ReturnTrue() {
         mNormalBatterySipper.drainType = BatterySipper.DrainType.IDLE;
         assertThat(mPowerUsageSummary.shouldHideSipper(mNormalBatterySipper)).isTrue();
@@ -194,6 +200,12 @@
     }
 
     @Test
+    public void testShouldHideSipper_TypeScreen_ReturnTrue() {
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.SCREEN;
+        assertThat(mPowerUsageSummary.shouldHideSipper(mNormalBatterySipper)).isTrue();
+    }
+
+    @Test
     public void testShouldHideSipper_UidRoot_ReturnTrue() {
         mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
         when(mNormalBatterySipper.getUid()).thenReturn(Process.ROOT_UID);