Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 41131997 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge "Fix time-in-foreground to include PROCESS_STATE_FOREGROUND_SERVICE" into sc-dev

parents b1c9be86 a00309cd
Loading
Loading
Loading
Loading
+15 −16
Original line number Diff line number Diff line
@@ -180,29 +180,28 @@ public class BatteryUsageStatsProvider {
    }

    private long getProcessForegroundTimeMs(BatteryStats.Uid uid, long realtimeUs) {
        final long topStateDurationMs = uid.getProcessStateTime(BatteryStats.Uid.PROCESS_STATE_TOP,
                realtimeUs, BatteryStats.STATS_SINCE_CHARGED) / 1000;

        long foregroundActivityDurationMs = 0;
        final long topStateDurationUs = uid.getProcessStateTime(BatteryStats.Uid.PROCESS_STATE_TOP,
                realtimeUs, BatteryStats.STATS_SINCE_CHARGED);
        long foregroundActivityDurationUs = 0;
        final BatteryStats.Timer foregroundActivityTimer = uid.getForegroundActivityTimer();
        if (foregroundActivityTimer != null) {
            foregroundActivityDurationMs = foregroundActivityTimer.getTotalTimeLocked(realtimeUs,
                    BatteryStats.STATS_SINCE_CHARGED) / 1000;
            foregroundActivityDurationUs = foregroundActivityTimer.getTotalTimeLocked(realtimeUs,
                    BatteryStats.STATS_SINCE_CHARGED);
        }

        // Use the min value of STATE_TOP time and foreground activity time, since both of these
        // times are imprecise
        final long foregroundDurationMs = Math.min(topStateDurationMs,
                foregroundActivityDurationMs);
        long totalForegroundDurationUs = Math.min(topStateDurationUs, foregroundActivityDurationUs);

        long foregroundServiceDurationMs = 0;
        final BatteryStats.Timer foregroundServiceTimer = uid.getForegroundServiceTimer();
        if (foregroundServiceTimer != null) {
            foregroundServiceDurationMs = foregroundServiceTimer.getTotalTimeLocked(realtimeUs,
                    BatteryStats.STATS_SINCE_CHARGED) / 1000;
        }
        totalForegroundDurationUs += uid.getProcessStateTime(
                BatteryStats.Uid.PROCESS_STATE_FOREGROUND, realtimeUs,
                BatteryStats.STATS_SINCE_CHARGED);

        totalForegroundDurationUs += uid.getProcessStateTime(
                BatteryStats.Uid.PROCESS_STATE_FOREGROUND_SERVICE, realtimeUs,
                BatteryStats.STATS_SINCE_CHARGED);

        return foregroundDurationMs + foregroundServiceDurationMs;
        return totalForegroundDurationUs / 1000;
    }

    private long getProcessBackgroundTimeMs(BatteryStats.Uid uid, long realtimeUs) {
+8 −2
Original line number Diff line number Diff line
@@ -59,8 +59,14 @@ public class BatteryUsageStatsProviderTest {
                30 * MINUTE_IN_MS, 30 * MINUTE_IN_MS);
        batteryStats.noteUidProcessStateLocked(APP_UID, ActivityManager.PROCESS_STATE_SERVICE,
                30 * MINUTE_IN_MS, 30 * MINUTE_IN_MS);
        batteryStats.noteUidProcessStateLocked(APP_UID, ActivityManager.PROCESS_STATE_CACHED_EMPTY,
        batteryStats.noteUidProcessStateLocked(APP_UID,
                ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE,
                40 * MINUTE_IN_MS, 40 * MINUTE_IN_MS);
        batteryStats.noteUidProcessStateLocked(APP_UID,
                ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE,
                50 * MINUTE_IN_MS, 50 * MINUTE_IN_MS);
        batteryStats.noteUidProcessStateLocked(APP_UID, ActivityManager.PROCESS_STATE_CACHED_EMPTY,
                80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS);

        mStatsRule.setCurrentTime(54321);

@@ -74,7 +80,7 @@ public class BatteryUsageStatsProviderTest {
                batteryUsageStats.getUidBatteryConsumers();
        final UidBatteryConsumer uidBatteryConsumer = uidBatteryConsumers.get(0);
        assertThat(uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_FOREGROUND))
                .isEqualTo(20 * MINUTE_IN_MS);
                .isEqualTo(60 * MINUTE_IN_MS);
        assertThat(uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_BACKGROUND))
                .isEqualTo(10 * MINUTE_IN_MS);