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

Commit 4596e5fc authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Remove dependency of battery history on com.android.internal.os.BatteryStatsImpl

Bug: 235635119
Test: atest FrameworksCoreTests:com.android.internal.os.BatteryStatsTests
Change-Id: I6b573f90591aa0e404e7963c004de4dc2ac2452b
parent a76d24a0
Loading
Loading
Loading
Loading
+56 −14
Original line number Original line Diff line number Diff line
@@ -16,7 +16,6 @@


package com.android.internal.os;
package com.android.internal.os;


import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.os.BatteryStats;
import android.os.BatteryStats;
import android.os.Parcel;
import android.os.Parcel;
@@ -36,6 +35,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
import java.util.Set;
import java.util.Set;
import java.util.function.Supplier;


/**
/**
 * BatteryStatsHistory encapsulates battery history files.
 * BatteryStatsHistory encapsulates battery history files.
@@ -60,12 +60,51 @@ import java.util.Set;
public class BatteryStatsHistory {
public class BatteryStatsHistory {
    private static final boolean DEBUG = false;
    private static final boolean DEBUG = false;
    private static final String TAG = "BatteryStatsHistory";
    private static final String TAG = "BatteryStatsHistory";

    // Current on-disk Parcel version. Must be updated when the format of the parcelable changes
    public static final int VERSION = 208;

    public static final String HISTORY_DIR = "battery-history";
    public static final String HISTORY_DIR = "battery-history";
    public static final String FILE_SUFFIX = ".bin";
    public static final String FILE_SUFFIX = ".bin";
    private static final int MIN_FREE_SPACE = 100 * 1024 * 1024;
    private static final int MIN_FREE_SPACE = 100 * 1024 * 1024;
    // Part of initial delta int that specifies the time delta.
    public static final int DELTA_TIME_MASK = 0x7ffff;
    public static final int DELTA_TIME_LONG = 0x7ffff;   // The delta is a following long
    public static final int DELTA_TIME_INT = 0x7fffe;    // The delta is a following int
    public static final int DELTA_TIME_ABS = 0x7fffd;    // Following is an entire abs update.
    // Flag in delta int: a new battery level int follows.
    public static final int DELTA_BATTERY_LEVEL_FLAG  = 0x00080000;
    // Flag in delta int: a new full state and battery status int follows.
    public static final int DELTA_STATE_FLAG          = 0x00100000;
    // Flag in delta int: a new full state2 int follows.
    public static final int DELTA_STATE2_FLAG         = 0x00200000;
    // Flag in delta int: contains a wakelock or wakeReason tag.
    public static final int DELTA_WAKELOCK_FLAG       = 0x00400000;
    // Flag in delta int: contains an event description.
    public static final int DELTA_EVENT_FLAG          = 0x00800000;
    // Flag in delta int: contains the battery charge count in uAh.
    public static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
    // These upper bits are the frequently changing state bits.
    public static final int DELTA_STATE_MASK          = 0xfe000000;
    // These are the pieces of battery state that are packed in to the upper bits of
    // the state int that have been packed in to the first delta int.  They must fit
    // in STATE_BATTERY_MASK.
    public static final int STATE_BATTERY_MASK         = 0xff000000;
    public static final int STATE_BATTERY_STATUS_MASK  = 0x00000007;
    public static final int STATE_BATTERY_STATUS_SHIFT = 29;
    public static final int STATE_BATTERY_HEALTH_MASK  = 0x00000007;
    public static final int STATE_BATTERY_HEALTH_SHIFT = 26;
    public static final int STATE_BATTERY_PLUG_MASK    = 0x00000003;
    public static final int STATE_BATTERY_PLUG_SHIFT   = 24;
    // We use the low bit of the battery state int to indicate that we have full details
    // from a battery level change.
    public static final int BATTERY_DELTA_LEVEL_FLAG   = 0x00000001;
    // Flag in history tag index: indicates that this is the first occurrence of this tag,
    // therefore the tag value is written in the parcel
    public static final int TAG_FIRST_OCCURRENCE_FLAG = 0x8000;


    @Nullable
    @Nullable
    private final BatteryStatsImpl mStats;
    private final Supplier<Integer> mMaxHistoryFiles;
    private final Parcel mHistoryBuffer;
    private final Parcel mHistoryBuffer;
    private final File mHistoryDir;
    private final File mHistoryDir;
    /**
    /**
@@ -107,15 +146,17 @@ public class BatteryStatsHistory {


    /**
    /**
     * Constructor
     * Constructor
     * @param stats BatteryStatsImpl object.
     *
     * @param systemDir typically /data/system
     * @param historyBuffer   The in-memory history buffer.
     * @param historyBuffer   The in-memory history buffer.
     * @param systemDir       typically /data/system
     * @param maxHistoryFiles the largest number of history buffer files to keep
     */
     */
    public BatteryStatsHistory(@NonNull BatteryStatsImpl stats, File systemDir,
    public BatteryStatsHistory(Parcel historyBuffer, File systemDir,
            Parcel historyBuffer) {
            Supplier<Integer> maxHistoryFiles) {
        mStats = stats;
        mHistoryBuffer = historyBuffer;
        mHistoryBuffer = historyBuffer;
        mHistoryDir = new File(systemDir, HISTORY_DIR);
        mHistoryDir = new File(systemDir, HISTORY_DIR);
        mMaxHistoryFiles = maxHistoryFiles;

        mHistoryDir.mkdirs();
        mHistoryDir.mkdirs();
        if (!mHistoryDir.exists()) {
        if (!mHistoryDir.exists()) {
            Slog.wtf(TAG, "HistoryDir does not exist:" + mHistoryDir.getPath());
            Slog.wtf(TAG, "HistoryDir does not exist:" + mHistoryDir.getPath());
@@ -157,9 +198,9 @@ public class BatteryStatsHistory {
     * @param historyBuffer the history buffer
     * @param historyBuffer the history buffer
     */
     */
    public BatteryStatsHistory(Parcel historyBuffer) {
    public BatteryStatsHistory(Parcel historyBuffer) {
        mStats = null;
        mHistoryDir = null;
        mHistoryDir = null;
        mHistoryBuffer = historyBuffer;
        mHistoryBuffer = historyBuffer;
        mMaxHistoryFiles = null;
    }
    }


    public File getHistoryDirectory() {
    public File getHistoryDirectory() {
@@ -193,8 +234,8 @@ public class BatteryStatsHistory {
     * create next history file.
     * create next history file.
     */
     */
    public void startNextFile() {
    public void startNextFile() {
        if (mStats == null) {
        if (mMaxHistoryFiles == null) {
            Slog.wtf(TAG, "mStats should not be null when writing history");
            Slog.wtf(TAG, "mMaxHistoryFiles should not be zero when writing history");
            return;
            return;
        }
        }


@@ -221,8 +262,9 @@ public class BatteryStatsHistory {
        }
        }


        // if there are more history files than allowed, delete oldest history files.
        // if there are more history files than allowed, delete oldest history files.
        // MAX_HISTORY_FILES can be updated by GService config at run time.
        // mMaxHistoryFiles comes from Constants.MAX_HISTORY_FILES and can be updated by GService
        while (mFileNumbers.size() > mStats.mConstants.MAX_HISTORY_FILES) {
        // config at run time.
        while (mFileNumbers.size() > mMaxHistoryFiles.get()) {
            int oldest = mFileNumbers.get(0);
            int oldest = mFileNumbers.get(0);
            getFile(oldest).delete();
            getFile(oldest).delete();
            mFileNumbers.remove(0);
            mFileNumbers.remove(0);
@@ -377,7 +419,7 @@ public class BatteryStatsHistory {
    private boolean skipHead(Parcel p) {
    private boolean skipHead(Parcel p) {
        p.setDataPosition(0);
        p.setDataPosition(0);
        final int version = p.readInt();
        final int version = p.readInt();
        if (version != BatteryStatsImpl.VERSION) {
        if (version != VERSION) {
            return false;
            return false;
        }
        }
        // skip historyBaseTime field.
        // skip historyBaseTime field.
+23 −23
Original line number Original line Diff line number Diff line
@@ -62,7 +62,7 @@ public class BatteryStatsHistoryIterator {


    void readHistoryDelta(Parcel src, BatteryStats.HistoryItem cur) {
    void readHistoryDelta(Parcel src, BatteryStats.HistoryItem cur) {
        int firstToken = src.readInt();
        int firstToken = src.readInt();
        int deltaTimeToken = firstToken & BatteryStatsImpl.DELTA_TIME_MASK;
        int deltaTimeToken = firstToken & BatteryStatsHistory.DELTA_TIME_MASK;
        cur.cmd = BatteryStats.HistoryItem.CMD_UPDATE;
        cur.cmd = BatteryStats.HistoryItem.CMD_UPDATE;
        cur.numReadInts = 1;
        cur.numReadInts = 1;
        if (DEBUG) {
        if (DEBUG) {
@@ -70,13 +70,13 @@ public class BatteryStatsHistoryIterator {
                    + " deltaTimeToken=" + deltaTimeToken);
                    + " deltaTimeToken=" + deltaTimeToken);
        }
        }


        if (deltaTimeToken < BatteryStatsImpl.DELTA_TIME_ABS) {
        if (deltaTimeToken < BatteryStatsHistory.DELTA_TIME_ABS) {
            cur.time += deltaTimeToken;
            cur.time += deltaTimeToken;
        } else if (deltaTimeToken == BatteryStatsImpl.DELTA_TIME_ABS) {
        } else if (deltaTimeToken == BatteryStatsHistory.DELTA_TIME_ABS) {
            cur.readFromParcel(src);
            cur.readFromParcel(src);
            if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
            if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
            return;
            return;
        } else if (deltaTimeToken == BatteryStatsImpl.DELTA_TIME_INT) {
        } else if (deltaTimeToken == BatteryStatsHistory.DELTA_TIME_INT) {
            int delta = src.readInt();
            int delta = src.readInt();
            cur.time += delta;
            cur.time += delta;
            cur.numReadInts += 1;
            cur.numReadInts += 1;
@@ -89,7 +89,7 @@ public class BatteryStatsHistoryIterator {
        }
        }


        final int batteryLevelInt;
        final int batteryLevelInt;
        if ((firstToken & BatteryStatsImpl.DELTA_BATTERY_LEVEL_FLAG) != 0) {
        if ((firstToken & BatteryStatsHistory.DELTA_BATTERY_LEVEL_FLAG) != 0) {
            batteryLevelInt = src.readInt();
            batteryLevelInt = src.readInt();
            readBatteryLevelInt(batteryLevelInt, cur);
            readBatteryLevelInt(batteryLevelInt, cur);
            cur.numReadInts += 1;
            cur.numReadInts += 1;
@@ -104,16 +104,16 @@ public class BatteryStatsHistoryIterator {
            batteryLevelInt = 0;
            batteryLevelInt = 0;
        }
        }


        if ((firstToken & BatteryStatsImpl.DELTA_STATE_FLAG) != 0) {
        if ((firstToken & BatteryStatsHistory.DELTA_STATE_FLAG) != 0) {
            int stateInt = src.readInt();
            int stateInt = src.readInt();
            cur.states = (firstToken & BatteryStatsImpl.DELTA_STATE_MASK) | (stateInt
            cur.states = (firstToken & BatteryStatsHistory.DELTA_STATE_MASK) | (stateInt
                    & (~BatteryStatsImpl.STATE_BATTERY_MASK));
                    & (~BatteryStatsHistory.STATE_BATTERY_MASK));
            cur.batteryStatus = (byte) ((stateInt >> BatteryStatsImpl.STATE_BATTERY_STATUS_SHIFT)
            cur.batteryStatus = (byte) ((stateInt >> BatteryStatsHistory.STATE_BATTERY_STATUS_SHIFT)
                    & BatteryStatsImpl.STATE_BATTERY_STATUS_MASK);
                    & BatteryStatsHistory.STATE_BATTERY_STATUS_MASK);
            cur.batteryHealth = (byte) ((stateInt >> BatteryStatsImpl.STATE_BATTERY_HEALTH_SHIFT)
            cur.batteryHealth = (byte) ((stateInt >> BatteryStatsHistory.STATE_BATTERY_HEALTH_SHIFT)
                    & BatteryStatsImpl.STATE_BATTERY_HEALTH_MASK);
                    & BatteryStatsHistory.STATE_BATTERY_HEALTH_MASK);
            cur.batteryPlugType = (byte) ((stateInt >> BatteryStatsImpl.STATE_BATTERY_PLUG_SHIFT)
            cur.batteryPlugType = (byte) ((stateInt >> BatteryStatsHistory.STATE_BATTERY_PLUG_SHIFT)
                    & BatteryStatsImpl.STATE_BATTERY_PLUG_MASK);
                    & BatteryStatsHistory.STATE_BATTERY_PLUG_MASK);
            switch (cur.batteryPlugType) {
            switch (cur.batteryPlugType) {
                case 1:
                case 1:
                    cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
                    cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
@@ -135,11 +135,11 @@ public class BatteryStatsHistoryIterator {
                        + " states=0x" + Integer.toHexString(cur.states));
                        + " states=0x" + Integer.toHexString(cur.states));
            }
            }
        } else {
        } else {
            cur.states = (firstToken & BatteryStatsImpl.DELTA_STATE_MASK) | (cur.states
            cur.states = (firstToken & BatteryStatsHistory.DELTA_STATE_MASK) | (cur.states
                    & (~BatteryStatsImpl.STATE_BATTERY_MASK));
                    & (~BatteryStatsHistory.STATE_BATTERY_MASK));
        }
        }


        if ((firstToken & BatteryStatsImpl.DELTA_STATE2_FLAG) != 0) {
        if ((firstToken & BatteryStatsHistory.DELTA_STATE2_FLAG) != 0) {
            cur.states2 = src.readInt();
            cur.states2 = src.readInt();
            if (DEBUG) {
            if (DEBUG) {
                Slog.i(TAG, "READ DELTA: states2=0x"
                Slog.i(TAG, "READ DELTA: states2=0x"
@@ -147,7 +147,7 @@ public class BatteryStatsHistoryIterator {
            }
            }
        }
        }


        if ((firstToken & BatteryStatsImpl.DELTA_WAKELOCK_FLAG) != 0) {
        if ((firstToken & BatteryStatsHistory.DELTA_WAKELOCK_FLAG) != 0) {
            final int indexes = src.readInt();
            final int indexes = src.readInt();
            final int wakeLockIndex = indexes & 0xffff;
            final int wakeLockIndex = indexes & 0xffff;
            final int wakeReasonIndex = (indexes >> 16) & 0xffff;
            final int wakeReasonIndex = (indexes >> 16) & 0xffff;
@@ -167,7 +167,7 @@ public class BatteryStatsHistoryIterator {
            cur.wakeReasonTag = null;
            cur.wakeReasonTag = null;
        }
        }


        if ((firstToken & BatteryStatsImpl.DELTA_EVENT_FLAG) != 0) {
        if ((firstToken & BatteryStatsHistory.DELTA_EVENT_FLAG) != 0) {
            cur.eventTag = cur.localEventTag;
            cur.eventTag = cur.localEventTag;
            final int codeAndIndex = src.readInt();
            final int codeAndIndex = src.readInt();
            cur.eventCode = (codeAndIndex & 0xffff);
            cur.eventCode = (codeAndIndex & 0xffff);
@@ -187,14 +187,14 @@ public class BatteryStatsHistoryIterator {
            cur.eventCode = BatteryStats.HistoryItem.EVENT_NONE;
            cur.eventCode = BatteryStats.HistoryItem.EVENT_NONE;
        }
        }


        if ((batteryLevelInt & BatteryStatsImpl.BATTERY_DELTA_LEVEL_FLAG) != 0) {
        if ((batteryLevelInt & BatteryStatsHistory.BATTERY_DELTA_LEVEL_FLAG) != 0) {
            cur.stepDetails = mReadHistoryStepDetails;
            cur.stepDetails = mReadHistoryStepDetails;
            cur.stepDetails.readFromParcel(src);
            cur.stepDetails.readFromParcel(src);
        } else {
        } else {
            cur.stepDetails = null;
            cur.stepDetails = null;
        }
        }


        if ((firstToken & BatteryStatsImpl.DELTA_BATTERY_CHARGE_FLAG) != 0) {
        if ((firstToken & BatteryStatsHistory.DELTA_BATTERY_CHARGE_FLAG) != 0) {
            cur.batteryChargeUah = src.readInt();
            cur.batteryChargeUah = src.readInt();
        }
        }
        cur.modemRailChargeMah = src.readDouble();
        cur.modemRailChargeMah = src.readDouble();
@@ -206,10 +206,10 @@ public class BatteryStatsHistoryIterator {
            return false;
            return false;
        }
        }


        if ((index & BatteryStatsImpl.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
        if ((index & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
            BatteryStats.HistoryTag tag = new BatteryStats.HistoryTag();
            BatteryStats.HistoryTag tag = new BatteryStats.HistoryTag();
            tag.readFromParcel(src);
            tag.readFromParcel(src);
            tag.poolIdx = index & ~BatteryStatsImpl.TAG_FIRST_OCCURRENCE_FLAG;
            tag.poolIdx = index & ~BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG;
            mHistoryTags.put(tag.poolIdx, tag);
            mHistoryTags.put(tag.poolIdx, tag);


            outTag.setTo(tag);
            outTag.setTo(tag);
+48 −73
Original line number Original line Diff line number Diff line
@@ -166,8 +166,8 @@ public class BatteryStatsImpl extends BatteryStats {
    // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
    // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
    private static final int MAGIC = 0xBA757475; // 'BATSTATS'
    private static final int MAGIC = 0xBA757475; // 'BATSTATS'
    // Current on-disk Parcel version
    // Current on-disk Parcel version. Must be updated when the format of the parcelable changes
    static final int VERSION = 208;
    public static final int VERSION = 208;
    // The maximum number of names wakelocks we will keep track of
    // The maximum number of names wakelocks we will keep track of
    // per uid; once the limit is reached, we batch the remaining wakelocks
    // per uid; once the limit is reached, we batch the remaining wakelocks
@@ -1625,7 +1625,8 @@ public class BatteryStatsImpl extends BatteryStats {
            mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer);
            mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer);
        } else {
        } else {
            mStatsFile = new AtomicFile(new File(historyDirectory, "batterystats.bin"));
            mStatsFile = new AtomicFile(new File(historyDirectory, "batterystats.bin"));
            mBatteryStatsHistory = new BatteryStatsHistory(this, historyDirectory, mHistoryBuffer);
            mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer, historyDirectory,
                    this::getMaxHistoryFiles);
        }
        }
        mHandler = null;
        mHandler = null;
        mPlatformIdleStateCallback = null;
        mPlatformIdleStateCallback = null;
@@ -3980,8 +3981,8 @@ public class BatteryStatsImpl extends BatteryStats {
        int idx;
        int idx;
        if (idxObj != null) {
        if (idxObj != null) {
            idx = idxObj;
            idx = idxObj;
            if ((idx & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
            if ((idx & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
                mHistoryTagPool.put(tag, idx & ~TAG_FIRST_OCCURRENCE_FLAG);
                mHistoryTagPool.put(tag, idx & ~BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG);
            }
            }
            return idx;
            return idx;
        } else if (mNextHistoryTagIdx < HISTORY_TAG_INDEX_LIMIT) {
        } else if (mNextHistoryTagIdx < HISTORY_TAG_INDEX_LIMIT) {
@@ -3996,10 +3997,10 @@ public class BatteryStatsImpl extends BatteryStats {
            if (mHistoryTags != null) {
            if (mHistoryTags != null) {
                mHistoryTags.put(idx, key);
                mHistoryTags.put(idx, key);
            }
            }
            return idx | TAG_FIRST_OCCURRENCE_FLAG;
            return idx | BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG;
        } else {
        } else {
            // Tag pool overflow: include the tag itself in the parcel
            // Tag pool overflow: include the tag itself in the parcel
            return HISTORY_TAG_INDEX_LIMIT | TAG_FIRST_OCCURRENCE_FLAG;
            return HISTORY_TAG_INDEX_LIMIT | BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG;
        }
        }
    }
    }
@@ -4084,49 +4085,10 @@ public class BatteryStatsImpl extends BatteryStats {
        in coulombs follows.
        in coulombs follows.
     */
     */
    // Part of initial delta int that specifies the time delta.
    static final int DELTA_TIME_MASK = 0x7ffff;
    static final int DELTA_TIME_LONG = 0x7ffff;   // The delta is a following long
    static final int DELTA_TIME_INT = 0x7fffe;    // The delta is a following int
    static final int DELTA_TIME_ABS = 0x7fffd;    // Following is an entire abs update.
    // Flag in delta int: a new battery level int follows.
    static final int DELTA_BATTERY_LEVEL_FLAG               = 0x00080000;
    // Flag in delta int: a new full state and battery status int follows.
    static final int DELTA_STATE_FLAG                       = 0x00100000;
    // Flag in delta int: a new full state2 int follows.
    static final int DELTA_STATE2_FLAG                      = 0x00200000;
    // Flag in delta int: contains a wakelock or wakeReason tag.
    static final int DELTA_WAKELOCK_FLAG                    = 0x00400000;
    // Flag in delta int: contains an event description.
    static final int DELTA_EVENT_FLAG                       = 0x00800000;
    // Flag in delta int: contains the battery charge count in uAh.
    static final int DELTA_BATTERY_CHARGE_FLAG              = 0x01000000;
    // These upper bits are the frequently changing state bits.
    static final int DELTA_STATE_MASK                       = 0xfe000000;
    // Flag in history tag index: indicates that this is the first occurrence of this tag,
    // therefore the tag value is written in the parcel
    static final int TAG_FIRST_OCCURRENCE_FLAG = 0x8000;
    // These are the pieces of battery state that are packed in to the upper bits of
    // the state int that have been packed in to the first delta int.  They must fit
    // in STATE_BATTERY_MASK.
    static final int STATE_BATTERY_MASK         = 0xff000000;
    static final int STATE_BATTERY_STATUS_MASK  = 0x00000007;
    static final int STATE_BATTERY_STATUS_SHIFT = 29;
    static final int STATE_BATTERY_HEALTH_MASK  = 0x00000007;
    static final int STATE_BATTERY_HEALTH_SHIFT = 26;
    static final int STATE_BATTERY_PLUG_MASK    = 0x00000003;
    static final int STATE_BATTERY_PLUG_SHIFT   = 24;
    // We use the low bit of the battery state int to indicate that we have full details
    // from a battery level change.
    static final int BATTERY_DELTA_LEVEL_FLAG   = 0x00000001;
    @GuardedBy("this")
    @GuardedBy("this")
    public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
    public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
        if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
        if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
            dest.writeInt(DELTA_TIME_ABS);
            dest.writeInt(BatteryStatsHistory.DELTA_TIME_ABS);
            cur.writeToParcel(dest, 0);
            cur.writeToParcel(dest, 0);
            return;
            return;
        }
        }
@@ -4137,48 +4099,48 @@ public class BatteryStatsImpl extends BatteryStats {
        int deltaTimeToken;
        int deltaTimeToken;
        if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
        if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
            deltaTimeToken = DELTA_TIME_LONG;
            deltaTimeToken = BatteryStatsHistory.DELTA_TIME_LONG;
        } else if (deltaTime >= DELTA_TIME_ABS) {
        } else if (deltaTime >= BatteryStatsHistory.DELTA_TIME_ABS) {
            deltaTimeToken = DELTA_TIME_INT;
            deltaTimeToken = BatteryStatsHistory.DELTA_TIME_INT;
        } else {
        } else {
            deltaTimeToken = (int)deltaTime;
            deltaTimeToken = (int)deltaTime;
        }
        }
        int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
        int firstToken = deltaTimeToken | (cur.states & BatteryStatsHistory.DELTA_STATE_MASK);
        final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
        final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
                ? BATTERY_DELTA_LEVEL_FLAG : 0;
                ? BatteryStatsHistory.BATTERY_DELTA_LEVEL_FLAG : 0;
        final boolean computeStepDetails = includeStepDetails != 0
        final boolean computeStepDetails = includeStepDetails != 0
                || mLastHistoryStepDetails == null;
                || mLastHistoryStepDetails == null;
        final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
        final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
        final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
        final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
        if (batteryLevelIntChanged) {
        if (batteryLevelIntChanged) {
            firstToken |= DELTA_BATTERY_LEVEL_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_BATTERY_LEVEL_FLAG;
        }
        }
        final int stateInt = buildStateInt(cur);
        final int stateInt = buildStateInt(cur);
        final boolean stateIntChanged = stateInt != lastStateInt;
        final boolean stateIntChanged = stateInt != lastStateInt;
        if (stateIntChanged) {
        if (stateIntChanged) {
            firstToken |= DELTA_STATE_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_STATE_FLAG;
        }
        }
        final boolean state2IntChanged = cur.states2 != last.states2;
        final boolean state2IntChanged = cur.states2 != last.states2;
        if (state2IntChanged) {
        if (state2IntChanged) {
            firstToken |= DELTA_STATE2_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_STATE2_FLAG;
        }
        }
        if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
        if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
            firstToken |= DELTA_WAKELOCK_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_WAKELOCK_FLAG;
        }
        }
        if (cur.eventCode != HistoryItem.EVENT_NONE) {
        if (cur.eventCode != HistoryItem.EVENT_NONE) {
            firstToken |= DELTA_EVENT_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_EVENT_FLAG;
        }
        }
        final boolean batteryChargeChanged = cur.batteryChargeUah != last.batteryChargeUah;
        final boolean batteryChargeChanged = cur.batteryChargeUah != last.batteryChargeUah;
        if (batteryChargeChanged) {
        if (batteryChargeChanged) {
            firstToken |= DELTA_BATTERY_CHARGE_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_BATTERY_CHARGE_FLAG;
        }
        }
        dest.writeInt(firstToken);
        dest.writeInt(firstToken);
        if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
        if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
                + " deltaTime=" + deltaTime);
                + " deltaTime=" + deltaTime);
        if (deltaTimeToken >= DELTA_TIME_INT) {
        if (deltaTimeToken >= BatteryStatsHistory.DELTA_TIME_INT) {
            if (deltaTimeToken == DELTA_TIME_INT) {
            if (deltaTimeToken == BatteryStatsHistory.DELTA_TIME_INT) {
                if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
                if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
                dest.writeInt((int)deltaTime);
                dest.writeInt((int)deltaTime);
            } else {
            } else {
@@ -4226,11 +4188,13 @@ public class BatteryStatsImpl extends BatteryStats {
                wakeReasonIndex = 0xffff;
                wakeReasonIndex = 0xffff;
            }
            }
            dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
            dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
            if (cur.wakelockTag != null && (wakeLockIndex & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
            if (cur.wakelockTag != null
                    && (wakeLockIndex & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
                cur.wakelockTag.writeToParcel(dest, 0);
                cur.wakelockTag.writeToParcel(dest, 0);
                cur.tagsFirstOccurrence = true;
                cur.tagsFirstOccurrence = true;
            }
            }
            if (cur.wakeReasonTag != null && (wakeReasonIndex & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
            if (cur.wakeReasonTag != null
                    && (wakeReasonIndex & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
                cur.wakeReasonTag.writeToParcel(dest, 0);
                cur.wakeReasonTag.writeToParcel(dest, 0);
                cur.tagsFirstOccurrence = true;
                cur.tagsFirstOccurrence = true;
            }
            }
@@ -4239,7 +4203,7 @@ public class BatteryStatsImpl extends BatteryStats {
            final int index = writeHistoryTag(cur.eventTag);
            final int index = writeHistoryTag(cur.eventTag);
            final int codeAndIndex = (cur.eventCode & 0xffff) | (index << 16);
            final int codeAndIndex = (cur.eventCode & 0xffff) | (index << 16);
            dest.writeInt(codeAndIndex);
            dest.writeInt(codeAndIndex);
            if ((index & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
            if ((index & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
                cur.eventTag.writeToParcel(dest, 0);
                cur.eventTag.writeToParcel(dest, 0);
                cur.tagsFirstOccurrence = true;
                cur.tagsFirstOccurrence = true;
            }
            }
@@ -4298,10 +4262,13 @@ public class BatteryStatsImpl extends BatteryStats {
        } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
        } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
            plugType = 3;
            plugType = 3;
        }
        }
        return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
        return ((h.batteryStatus & BatteryStatsHistory.STATE_BATTERY_STATUS_MASK)
                | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
                << BatteryStatsHistory.STATE_BATTERY_STATUS_SHIFT)
                | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
                | ((h.batteryHealth & BatteryStatsHistory.STATE_BATTERY_HEALTH_MASK)
                | (h.states&(~STATE_BATTERY_MASK));
                << BatteryStatsHistory.STATE_BATTERY_HEALTH_SHIFT)
                | ((plugType & BatteryStatsHistory.STATE_BATTERY_PLUG_MASK)
                << BatteryStatsHistory.STATE_BATTERY_PLUG_SHIFT)
                | (h.states & (~BatteryStatsHistory.STATE_BATTERY_MASK));
    }
    }
    private void computeHistoryStepDetails(final HistoryStepDetails out,
    private void computeHistoryStepDetails(final HistoryStepDetails out,
@@ -4501,7 +4468,7 @@ public class BatteryStatsImpl extends BatteryStats {
            // Mark every entry in the pool with a flag indicating that the tag
            // Mark every entry in the pool with a flag indicating that the tag
            // has not yet been encountered while writing the current history buffer.
            // has not yet been encountered while writing the current history buffer.
            for (Map.Entry<HistoryTag, Integer> entry: mHistoryTagPool.entrySet()) {
            for (Map.Entry<HistoryTag, Integer> entry: mHistoryTagPool.entrySet()) {
                entry.setValue(entry.getValue() | TAG_FIRST_OCCURRENCE_FLAG);
                entry.setValue(entry.getValue() | BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG);
            }
            }
            startRecordingHistory(elapsedRealtimeMs, uptimeMs, false);
            startRecordingHistory(elapsedRealtimeMs, uptimeMs, false);
            HistoryItem newItem = new HistoryItem();
            HistoryItem newItem = new HistoryItem();
@@ -12363,7 +12330,8 @@ public class BatteryStatsImpl extends BatteryStats {
            mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer);
            mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer);
        } else {
        } else {
            mStatsFile = new AtomicFile(new File(systemDir, "batterystats.bin"));
            mStatsFile = new AtomicFile(new File(systemDir, "batterystats.bin"));
            mBatteryStatsHistory = new BatteryStatsHistory(this, systemDir, mHistoryBuffer);
            mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer, systemDir,
                    this::getMaxHistoryFiles);
        }
        }
        mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
        mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
        mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
        mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
@@ -12388,6 +12356,12 @@ public class BatteryStatsImpl extends BatteryStats {
        FrameworkStatsLog.write(FrameworkStatsLog.DEVICE_IDLE_MODE_STATE_CHANGED, mDeviceIdleMode);
        FrameworkStatsLog.write(FrameworkStatsLog.DEVICE_IDLE_MODE_STATE_CHANGED, mDeviceIdleMode);
    }
    }
    private int getMaxHistoryFiles() {
        synchronized (this) {
            return mConstants.MAX_HISTORY_FILES;
        }
    }
    @VisibleForTesting
    @VisibleForTesting
    protected void initTimersAndCounters() {
    protected void initTimersAndCounters() {
        mScreenOnTimer = new StopwatchTimer(mClock, null, -1, null, mOnBatteryTimeBase);
        mScreenOnTimer = new StopwatchTimer(mClock, null, -1, null, mOnBatteryTimeBase);
@@ -12926,7 +12900,8 @@ public class BatteryStatsImpl extends BatteryStats {
        mHistoryTags = new SparseArray<>(mHistoryTagPool.size());
        mHistoryTags = new SparseArray<>(mHistoryTagPool.size());
        for (Map.Entry<HistoryTag, Integer> entry: mHistoryTagPool.entrySet()) {
        for (Map.Entry<HistoryTag, Integer> entry: mHistoryTagPool.entrySet()) {
            mHistoryTags.put(entry.getValue() & ~TAG_FIRST_OCCURRENCE_FLAG, entry.getKey());
            mHistoryTags.put(entry.getValue() & ~BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG,
                    entry.getKey());
        }
        }
    }
    }
@@ -17089,9 +17064,9 @@ public class BatteryStatsImpl extends BatteryStats {
    @GuardedBy("this")
    @GuardedBy("this")
    void  readHistoryBuffer(Parcel in) throws ParcelFormatException {
    void  readHistoryBuffer(Parcel in) throws ParcelFormatException {
        final int version = in.readInt();
        final int version = in.readInt();
        if (version != VERSION) {
        if (version != BatteryStatsHistory.VERSION) {
            Slog.w("BatteryStats", "readHistoryBuffer: version got " + version
            Slog.w("BatteryStats", "readHistoryBuffer: version got " + version
                    + ", expected " + VERSION + "; erasing old stats");
                    + ", expected " + BatteryStatsHistory.VERSION + "; erasing old stats");
            return;
            return;
        }
        }
@@ -17152,7 +17127,7 @@ public class BatteryStatsImpl extends BatteryStats {
            TimeUtils.formatDuration(mLastHistoryElapsedRealtimeMs, sb);
            TimeUtils.formatDuration(mLastHistoryElapsedRealtimeMs, sb);
            Slog.i(TAG, sb.toString());
            Slog.i(TAG, sb.toString());
        }
        }
        out.writeInt(VERSION);
        out.writeInt(BatteryStatsHistory.VERSION);
        out.writeLong(mHistoryBaseTimeMs + mLastHistoryElapsedRealtimeMs);
        out.writeLong(mHistoryBaseTimeMs + mLastHistoryElapsedRealtimeMs);
        if (!inclData) {
        if (!inclData) {
            out.writeInt(0);
            out.writeInt(0);
+1 −1
Original line number Original line Diff line number Diff line
@@ -227,7 +227,7 @@ public class BatteryUsageStatsProvider {
            final File systemDir =
            final File systemDir =
                    batteryStatsImpl.mBatteryStatsHistory.getHistoryDirectory().getParentFile();
                    batteryStatsImpl.mBatteryStatsHistory.getHistoryDirectory().getParentFile();
            final BatteryStatsHistory batteryStatsHistory =
            final BatteryStatsHistory batteryStatsHistory =
                    new BatteryStatsHistory(batteryStatsImpl, systemDir, historyBuffer);
                    new BatteryStatsHistory(historyBuffer, systemDir, null);


            batteryUsageStatsBuilder.setBatteryHistory(batteryStatsHistory);
            batteryUsageStatsBuilder.setBatteryHistory(batteryStatsHistory);
        }
        }
+8 −8

File changed.

Preview size limit exceeded, changes collapsed.