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

Commit 610a0a09 authored by jackqdyulei's avatar jackqdyulei
Browse files

Write currentDuration into Parcel

This cl writes currentDuration to Parcel, so in battery settings we could
use this data in anomaly detection.

Also note that in getTotalDurationMsLock(aka method1), the logic is:

result = mTotalDurationMs + getCurrentDurationMsLocked(aka method2)

Since now method2 is not zero, we also tweaked the value for
mTotalDurationMs to make method1 return the same value compared with
before.

Since getMaxDurationMsLocked depends on mNesting, which is always zero,
so we don't need to tweak value for mMaxDurationMs

All these methods are covered in BatteryStatsDurationTimerTest

Bug: 64255589
Test: runtest -x BatteryStatsTest
Change-Id: I9168be099d00bb68fedbc5bfbb7bf7f0d9aae85a
parent 98af408a
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private static final int MAGIC = 0xBA757475; // 'BATSTATS'

    // Current on-disk Parcel version
    private static final int VERSION = 164 + (USE_OLD_HISTORY ? 1000 : 0);
    private static final int VERSION = 165 + (USE_OLD_HISTORY ? 1000 : 0);

    // Maximum number of items we will record in the history.
    private static final int MAX_HISTORY_ITEMS;
@@ -1823,6 +1823,7 @@ public class BatteryStatsImpl extends BatteryStats {
            super(clocks, uid, type, timerPool, timeBase, in);
            mMaxDurationMs = in.readLong();
            mTotalDurationMs = in.readLong();
            mCurrentDurationMs = in.readLong();
        }

        public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
@@ -1834,7 +1835,8 @@ public class BatteryStatsImpl extends BatteryStats {
        public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
            super.writeToParcel(out, elapsedRealtimeUs);
            out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
            out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
            out.writeLong(mTotalDurationMs);
            out.writeLong(getCurrentDurationMsLocked(elapsedRealtimeUs / 1000));
        }

        /**
@@ -1965,6 +1967,10 @@ public class BatteryStatsImpl extends BatteryStats {
         *
         * Note that this time is NOT split between the timers in the timer group that
         * this timer is attached to.  It is the TOTAL time.
         *
         * Note that if running timer is parceled and unparceled, this method will return
         * current duration value at the time of parceling even though timer may not be
         * currently running.
         */
        @Override
        public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
+5 −4
Original line number Diff line number Diff line
@@ -134,7 +134,8 @@ public class BatteryStatsDurationTimerTest extends TestCase {
                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase);
        summary.startRunningLocked(3100);
        summary.readSummaryFromParcelLocked(summaryParcel);
        // The new one shouldn't be running, and therefore 0 for current time
        // The new one shouldn't be running, and therefore 0 for current time if using
        // summary parcel
        assertFalse(summary.isRunningLocked());
        assertEquals(0, summary.getCurrentDurationMsLocked(6300));
        // The new one should have the max and total durations that we had when we wrote it
@@ -149,10 +150,10 @@ public class BatteryStatsDurationTimerTest extends TestCase {
        // Read full - Should be the same as the summary as far as DurationTimer is concerned.
        final BatteryStatsImpl.DurationTimer full = new BatteryStatsImpl.DurationTimer(clocks,
                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase, fullParcel);
        // The new one shouldn't be running, and therefore 0 for current time
        // The new one shouldn't be running
        assertFalse(full.isRunningLocked());
        assertEquals(0, full.getCurrentDurationMsLocked(6300));
        // The new one should have the max and total durations that we had when we wrote it
        // The new one should have the current, max and total durations that we had when we wrote it
        assertEquals(1200, full.getCurrentDurationMsLocked(6300));
        assertEquals(1200, full.getMaxDurationMsLocked(6301));
        assertEquals(1200, full.getTotalDurationMsLocked(6302));