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

Commit 472367a9 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Rename MeasuredEnergy to EnergyConsumer

Bug: 256945119
Test: atest FrameworksServicesTests:BatteryStatsTests

Change-Id: I0e10a3bd0bfffcb55658c09dd57a0888d9efb462
parent d70ac512
Loading
Loading
Loading
Loading
+6 −6
Original line number Original line Diff line number Diff line
@@ -132,7 +132,7 @@ public abstract class BatteryConsumer {
    @IntDef(prefix = {"POWER_MODEL_"}, value = {
    @IntDef(prefix = {"POWER_MODEL_"}, value = {
            POWER_MODEL_UNDEFINED,
            POWER_MODEL_UNDEFINED,
            POWER_MODEL_POWER_PROFILE,
            POWER_MODEL_POWER_PROFILE,
            POWER_MODEL_MEASURED_ENERGY,
            POWER_MODEL_ENERGY_CONSUMPTION,
    })
    })
    @Retention(RetentionPolicy.SOURCE)
    @Retention(RetentionPolicy.SOURCE)
    public @interface PowerModel {
    public @interface PowerModel {
@@ -150,9 +150,9 @@ public abstract class BatteryConsumer {
    public static final int POWER_MODEL_POWER_PROFILE = 1;
    public static final int POWER_MODEL_POWER_PROFILE = 1;


    /**
    /**
     * Power model that is based on energy consumption measured by on-device power monitors.
     * Power model that is based on energy consumption stats provided by PowerStats HAL.
     */
     */
    public static final int POWER_MODEL_MEASURED_ENERGY = 2;
    public static final int POWER_MODEL_ENERGY_CONSUMPTION = 2;


    /**
    /**
     * Identifiers of consumed power aggregations.
     * Identifiers of consumed power aggregations.
@@ -469,8 +469,8 @@ public abstract class BatteryConsumer {
     */
     */
    public static String powerModelToString(@BatteryConsumer.PowerModel int powerModel) {
    public static String powerModelToString(@BatteryConsumer.PowerModel int powerModel) {
        switch (powerModel) {
        switch (powerModel) {
            case BatteryConsumer.POWER_MODEL_MEASURED_ENERGY:
            case BatteryConsumer.POWER_MODEL_ENERGY_CONSUMPTION:
                return "measured energy";
                return "energy consumption";
            case BatteryConsumer.POWER_MODEL_POWER_PROFILE:
            case BatteryConsumer.POWER_MODEL_POWER_PROFILE:
                return "power profile";
                return "power profile";
            default:
            default:
@@ -484,7 +484,7 @@ public abstract class BatteryConsumer {
     */
     */
    public static int powerModelToProtoEnum(@BatteryConsumer.PowerModel int powerModel) {
    public static int powerModelToProtoEnum(@BatteryConsumer.PowerModel int powerModel) {
        switch (powerModel) {
        switch (powerModel) {
            case BatteryConsumer.POWER_MODEL_MEASURED_ENERGY:
            case BatteryConsumer.POWER_MODEL_ENERGY_CONSUMPTION:
                return BatteryUsageStatsAtomsProto.PowerComponentModel.MEASURED_ENERGY;
                return BatteryUsageStatsAtomsProto.PowerComponentModel.MEASURED_ENERGY;
            case BatteryConsumer.POWER_MODEL_POWER_PROFILE:
            case BatteryConsumer.POWER_MODEL_POWER_PROFILE:
                return BatteryUsageStatsAtomsProto.PowerComponentModel.POWER_PROFILE;
                return BatteryUsageStatsAtomsProto.PowerComponentModel.POWER_PROFILE;
+42 −36
Original line number Original line Diff line number Diff line
@@ -1043,12 +1043,13 @@ public abstract class BatteryStats {


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of bluetooth for this uid,
         * Returns the battery consumption (in microcoulombs) of bluetooth for this uid,
         * derived from on device power measurement data.
         * derived from {@link android.hardware.power.stats.EnergyConsumerType#BLUETOOTH} bucket
         * provided by the PowerStats service.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getBluetoothMeasuredBatteryConsumptionUC();
        public abstract long getBluetoothEnergyConsumptionUC();


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's bluetooth usage
         * Returns the battery consumption (in microcoulombs) of the uid's bluetooth usage
@@ -1057,17 +1058,18 @@ public abstract class BatteryStats {
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getBluetoothMeasuredBatteryConsumptionUC(
        public abstract long getBluetoothEnergyConsumptionUC(
                @BatteryConsumer.ProcessState int processState);
                @BatteryConsumer.ProcessState int processState);


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's cpu usage, derived from
         * Returns the battery consumption (in microcoulombs) of the uid's cpu usage, derived from
         * on device power measurement data.
         * derived from {@link android.hardware.power.stats.EnergyConsumerType#CPU} bucket
         * provided by the PowerStats service.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getCpuMeasuredBatteryConsumptionUC();
        public abstract long getCpuEnergyConsumptionUC();


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's cpu usage when in the
         * Returns the battery consumption (in microcoulombs) of the uid's cpu usage when in the
@@ -1076,26 +1078,28 @@ public abstract class BatteryStats {
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getCpuMeasuredBatteryConsumptionUC(
        public abstract long getCpuEnergyConsumptionUC(
                @BatteryConsumer.ProcessState int processState);
                @BatteryConsumer.ProcessState int processState);


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's GNSS usage, derived from
         * Returns the battery consumption (in microcoulombs) of the uid's GNSS usage, derived from
         * on device power measurement data.
         * derived from {@link android.hardware.power.stats.EnergyConsumerType#GNSS} bucket
         * provided by the PowerStats service.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getGnssMeasuredBatteryConsumptionUC();
        public abstract long getGnssEnergyConsumptionUC();


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's radio usage, derived from
         * Returns the battery consumption (in microcoulombs) of the uid's radio usage, derived from
         * on device power measurement data.
         * derived from {@link android.hardware.power.stats.EnergyConsumerType#MOBILE_RADIO}
         * bucket provided by the PowerStats service.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getMobileRadioMeasuredBatteryConsumptionUC();
        public abstract long getMobileRadioEnergyConsumptionUC();


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's radio usage when in the
         * Returns the battery consumption (in microcoulombs) of the uid's radio usage when in the
@@ -1104,26 +1108,28 @@ public abstract class BatteryStats {
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getMobileRadioMeasuredBatteryConsumptionUC(
        public abstract long getMobileRadioEnergyConsumptionUC(
                @BatteryConsumer.ProcessState int processState);
                @BatteryConsumer.ProcessState int processState);


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the screen while on and uid active,
         * Returns the battery consumption (in microcoulombs) of the screen while on and uid active,
         * derived from on device power measurement data.
         * derived from {@link android.hardware.power.stats.EnergyConsumerType#DISPLAY} bucket
         * provided by the PowerStats service.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getScreenOnMeasuredBatteryConsumptionUC();
        public abstract long getScreenOnEnergyConsumptionUC();


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of wifi for this uid,
         * Returns the battery consumption (in microcoulombs) of wifi for this uid,
         * derived from on device power measurement data.
         * derived from {@link android.hardware.power.stats.EnergyConsumerType#WIFI} bucket
         * provided by the PowerStats service.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getWifiMeasuredBatteryConsumptionUC();
        public abstract long getWifiEnergyConsumptionUC();


        /**
        /**
         * Returns the battery consumption (in microcoulombs) of the uid's wifi usage when in the
         * Returns the battery consumption (in microcoulombs) of the uid's wifi usage when in the
@@ -1132,7 +1138,7 @@ public abstract class BatteryStats {
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract long getWifiMeasuredBatteryConsumptionUC(
        public abstract long getWifiEnergyConsumptionUC(
                @BatteryConsumer.ProcessState int processState);
                @BatteryConsumer.ProcessState int processState);




@@ -1147,7 +1153,7 @@ public abstract class BatteryStats {
         *
         *
         * {@hide}
         * {@hide}
         */
         */
        public abstract @Nullable long[] getCustomConsumerMeasuredBatteryConsumptionUC();
        public abstract @Nullable long[] getCustomEnergyConsumerBatteryConsumptionUC();


        public static abstract class Sensor {
        public static abstract class Sensor {


@@ -1776,7 +1782,7 @@ public abstract class BatteryStats {
    /**
    /**
     * Measured energy delta from the previous reading.
     * Measured energy delta from the previous reading.
     */
     */
    public static final class MeasuredEnergyDetails {
    public static final class EnergyConsumerDetails {
        /**
        /**
         * Description of the energy consumer, such as CPU, DISPLAY etc
         * Description of the energy consumer, such as CPU, DISPLAY etc
         */
         */
@@ -1986,8 +1992,8 @@ public abstract class BatteryStats {
        // Non-null when there is more detailed information at this step.
        // Non-null when there is more detailed information at this step.
        public HistoryStepDetails stepDetails;
        public HistoryStepDetails stepDetails;


        // Non-null when there is measured energy information
        // Non-null when there is energy consumer information
        public MeasuredEnergyDetails measuredEnergyDetails;
        public EnergyConsumerDetails energyConsumerDetails;


        // Non-null when there is CPU usage information
        // Non-null when there is CPU usage information
        public CpuUsageDetails cpuUsageDetails;
        public CpuUsageDetails cpuUsageDetails;
@@ -2200,7 +2206,7 @@ public abstract class BatteryStats {
            eventCode = EVENT_NONE;
            eventCode = EVENT_NONE;
            eventTag = null;
            eventTag = null;
            tagsFirstOccurrence = false;
            tagsFirstOccurrence = false;
            measuredEnergyDetails = null;
            energyConsumerDetails = null;
            cpuUsageDetails = null;
            cpuUsageDetails = null;
        }
        }


@@ -2251,7 +2257,7 @@ public abstract class BatteryStats {
            }
            }
            tagsFirstOccurrence = o.tagsFirstOccurrence;
            tagsFirstOccurrence = o.tagsFirstOccurrence;
            currentTime = o.currentTime;
            currentTime = o.currentTime;
            measuredEnergyDetails = o.measuredEnergyDetails;
            energyConsumerDetails = o.energyConsumerDetails;
            cpuUsageDetails = o.cpuUsageDetails;
            cpuUsageDetails = o.cpuUsageDetails;
        }
        }


@@ -2858,7 +2864,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getBluetoothMeasuredBatteryConsumptionUC();
    public abstract long getBluetoothEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) of the cpu, derived from on device power
     * Returns the battery consumption (in microcoulombs) of the cpu, derived from on device power
@@ -2867,7 +2873,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getCpuMeasuredBatteryConsumptionUC();
    public abstract long getCpuEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) of the GNSS, derived from on device power
     * Returns the battery consumption (in microcoulombs) of the GNSS, derived from on device power
@@ -2876,7 +2882,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getGnssMeasuredBatteryConsumptionUC();
    public abstract long getGnssEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) of the radio, derived from on device power
     * Returns the battery consumption (in microcoulombs) of the radio, derived from on device power
@@ -2885,7 +2891,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getMobileRadioMeasuredBatteryConsumptionUC();
    public abstract long getMobileRadioEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) of the screen while on, derived from on
     * Returns the battery consumption (in microcoulombs) of the screen while on, derived from on
@@ -2894,7 +2900,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getScreenOnMeasuredBatteryConsumptionUC();
    public abstract long getScreenOnEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) of the screen in doze, derived from on
     * Returns the battery consumption (in microcoulombs) of the screen in doze, derived from on
@@ -2903,7 +2909,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getScreenDozeMeasuredBatteryConsumptionUC();
    public abstract long getScreenDozeEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) of wifi, derived from on
     * Returns the battery consumption (in microcoulombs) of wifi, derived from on
@@ -2912,7 +2918,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract long getWifiMeasuredBatteryConsumptionUC();
    public abstract long getWifiEnergyConsumptionUC();


    /**
    /**
     * Returns the battery consumption (in microcoulombs) that each
     * Returns the battery consumption (in microcoulombs) that each
@@ -2924,7 +2930,7 @@ public abstract class BatteryStats {
     *
     *
     * {@hide}
     * {@hide}
     */
     */
    public abstract @Nullable long[] getCustomConsumerMeasuredBatteryConsumptionUC();
    public abstract @Nullable long[] getCustomEnergyConsumerBatteryConsumptionUC();


    /**
    /**
     * Returns the names of all {@link android.hardware.power.stats.EnergyConsumer}'s
     * Returns the names of all {@link android.hardware.power.stats.EnergyConsumer}'s
@@ -7088,19 +7094,19 @@ public abstract class BatteryStats {
                    }
                    }
                }
                }
                boolean firstExtension = true;
                boolean firstExtension = true;
                if (rec.measuredEnergyDetails != null) {
                if (rec.energyConsumerDetails != null) {
                    firstExtension = false;
                    firstExtension = false;
                    if (!checkin) {
                    if (!checkin) {
                        item.append(" ext=energy:");
                        item.append(" ext=energy:");
                        item.append(rec.measuredEnergyDetails);
                        item.append(rec.energyConsumerDetails);
                    } else {
                    } else {
                        item.append(",XE");
                        item.append(",XE");
                        for (int i = 0; i < rec.measuredEnergyDetails.consumers.length; i++) {
                        for (int i = 0; i < rec.energyConsumerDetails.consumers.length; i++) {
                            if (rec.measuredEnergyDetails.chargeUC[i] != POWER_DATA_UNAVAILABLE) {
                            if (rec.energyConsumerDetails.chargeUC[i] != POWER_DATA_UNAVAILABLE) {
                                item.append(',');
                                item.append(',');
                                item.append(rec.measuredEnergyDetails.consumers[i].name);
                                item.append(rec.energyConsumerDetails.consumers[i].name);
                                item.append('=');
                                item.append('=');
                                item.append(rec.measuredEnergyDetails.chargeUC[i]);
                                item.append(rec.energyConsumerDetails.chargeUC[i]);
                            }
                            }
                        }
                        }
                    }
                    }
+13 −13
Original line number Original line Diff line number Diff line
@@ -22,10 +22,10 @@ import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.BatteryStats;
import android.os.BatteryStats.BitDescription;
import android.os.BatteryStats.BitDescription;
import android.os.BatteryStats.CpuUsageDetails;
import android.os.BatteryStats.CpuUsageDetails;
import android.os.BatteryStats.EnergyConsumerDetails;
import android.os.BatteryStats.HistoryItem;
import android.os.BatteryStats.HistoryItem;
import android.os.BatteryStats.HistoryStepDetails;
import android.os.BatteryStats.HistoryStepDetails;
import android.os.BatteryStats.HistoryTag;
import android.os.BatteryStats.HistoryTag;
import android.os.BatteryStats.MeasuredEnergyDetails;
import android.os.Build;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcel;
import android.os.ParcelFormatException;
import android.os.ParcelFormatException;
@@ -984,9 +984,9 @@ public class BatteryStatsHistory {
    /**
    /**
     * Records measured energy data.
     * Records measured energy data.
     */
     */
    public void recordMeasuredEnergyDetails(long elapsedRealtimeMs, long uptimeMs,
    public void recordEnergyConsumerDetails(long elapsedRealtimeMs, long uptimeMs,
            MeasuredEnergyDetails measuredEnergyDetails) {
            EnergyConsumerDetails energyConsumerDetails) {
        mHistoryCur.measuredEnergyDetails = measuredEnergyDetails;
        mHistoryCur.energyConsumerDetails = energyConsumerDetails;
        mHistoryCur.states2 |= HistoryItem.STATE2_EXTENSIONS_FLAG;
        mHistoryCur.states2 |= HistoryItem.STATE2_EXTENSIONS_FLAG;
        writeHistoryItem(elapsedRealtimeMs, uptimeMs);
        writeHistoryItem(elapsedRealtimeMs, uptimeMs);
    }
    }
@@ -1293,7 +1293,7 @@ public class BatteryStatsHistory {
                && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
                && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
                && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
                && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
                && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage
                && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage
                && mHistoryLastWritten.measuredEnergyDetails == null
                && mHistoryLastWritten.energyConsumerDetails == null
                && mHistoryLastWritten.cpuUsageDetails == null) {
                && mHistoryLastWritten.cpuUsageDetails == null) {
            // We can merge this new change in with the last one.  Merging is
            // We can merge this new change in with the last one.  Merging is
            // allowed as long as only the states have changed, and within those states
            // allowed as long as only the states have changed, and within those states
@@ -1396,7 +1396,7 @@ public class BatteryStatsHistory {
        cur.eventCode = HistoryItem.EVENT_NONE;
        cur.eventCode = HistoryItem.EVENT_NONE;
        cur.eventTag = null;
        cur.eventTag = null;
        cur.tagsFirstOccurrence = false;
        cur.tagsFirstOccurrence = false;
        cur.measuredEnergyDetails = null;
        cur.energyConsumerDetails = null;
        cur.cpuUsageDetails = null;
        cur.cpuUsageDetails = null;
        if (DEBUG) {
        if (DEBUG) {
            Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
            Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
@@ -1517,7 +1517,7 @@ public class BatteryStatsHistory {
        if (stateIntChanged) {
        if (stateIntChanged) {
            firstToken |= BatteryStatsHistory.DELTA_STATE_FLAG;
            firstToken |= BatteryStatsHistory.DELTA_STATE_FLAG;
        }
        }
        if (cur.measuredEnergyDetails != null) {
        if (cur.energyConsumerDetails != null) {
            extensionFlags |= BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_FLAG;
            extensionFlags |= BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_FLAG;
            if (!mMeasuredEnergyHeaderWritten) {
            if (!mMeasuredEnergyHeaderWritten) {
                extensionFlags |= BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_HEADER_FLAG;
                extensionFlags |= BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_HEADER_FLAG;
@@ -1653,22 +1653,22 @@ public class BatteryStatsHistory {
        dest.writeDouble(cur.wifiRailChargeMah);
        dest.writeDouble(cur.wifiRailChargeMah);
        if (extensionFlags != 0) {
        if (extensionFlags != 0) {
            dest.writeInt(extensionFlags);
            dest.writeInt(extensionFlags);
            if (cur.measuredEnergyDetails != null) {
            if (cur.energyConsumerDetails != null) {
                if (DEBUG) {
                if (DEBUG) {
                    Slog.i(TAG, "WRITE DELTA: measuredEnergyDetails=" + cur.measuredEnergyDetails);
                    Slog.i(TAG, "WRITE DELTA: measuredEnergyDetails=" + cur.energyConsumerDetails);
                }
                }
                if (!mMeasuredEnergyHeaderWritten) {
                if (!mMeasuredEnergyHeaderWritten) {
                    MeasuredEnergyDetails.EnergyConsumer[] consumers =
                    EnergyConsumerDetails.EnergyConsumer[] consumers =
                            cur.measuredEnergyDetails.consumers;
                            cur.energyConsumerDetails.consumers;
                    dest.writeInt(consumers.length);
                    dest.writeInt(consumers.length);
                    for (MeasuredEnergyDetails.EnergyConsumer consumer : consumers) {
                    for (EnergyConsumerDetails.EnergyConsumer consumer : consumers) {
                        dest.writeInt(consumer.type);
                        dest.writeInt(consumer.type);
                        dest.writeInt(consumer.ordinal);
                        dest.writeInt(consumer.ordinal);
                        dest.writeString(consumer.name);
                        dest.writeString(consumer.name);
                    }
                    }
                    mMeasuredEnergyHeaderWritten = true;
                    mMeasuredEnergyHeaderWritten = true;
                }
                }
                mVarintParceler.writeLongArray(dest, cur.measuredEnergyDetails.chargeUC);
                mVarintParceler.writeLongArray(dest, cur.energyConsumerDetails.chargeUC);
            }
            }


            if (cur.cpuUsageDetails != null) {
            if (cur.cpuUsageDetails != null) {
+14 −14
Original line number Original line Diff line number Diff line
@@ -36,7 +36,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor
    private final BatteryStats.HistoryStepDetails mReadHistoryStepDetails =
    private final BatteryStats.HistoryStepDetails mReadHistoryStepDetails =
            new BatteryStats.HistoryStepDetails();
            new BatteryStats.HistoryStepDetails();
    private final SparseArray<BatteryStats.HistoryTag> mHistoryTags = new SparseArray<>();
    private final SparseArray<BatteryStats.HistoryTag> mHistoryTags = new SparseArray<>();
    private BatteryStats.MeasuredEnergyDetails mMeasuredEnergyDetails;
    private BatteryStats.EnergyConsumerDetails mEnergyConsumerDetails;
    private BatteryStats.CpuUsageDetails mCpuUsageDetails;
    private BatteryStats.CpuUsageDetails mCpuUsageDetails;
    private final BatteryStatsHistory.VarintParceler mVarintParceler =
    private final BatteryStatsHistory.VarintParceler mVarintParceler =
            new BatteryStatsHistory.VarintParceler();
            new BatteryStatsHistory.VarintParceler();
@@ -230,8 +230,8 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor
        if ((cur.states2 & BatteryStats.HistoryItem.STATE2_EXTENSIONS_FLAG) != 0) {
        if ((cur.states2 & BatteryStats.HistoryItem.STATE2_EXTENSIONS_FLAG) != 0) {
            final int extensionFlags = src.readInt();
            final int extensionFlags = src.readInt();
            if ((extensionFlags & BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_HEADER_FLAG) != 0) {
            if ((extensionFlags & BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_HEADER_FLAG) != 0) {
                if (mMeasuredEnergyDetails == null) {
                if (mEnergyConsumerDetails == null) {
                    mMeasuredEnergyDetails = new BatteryStats.MeasuredEnergyDetails();
                    mEnergyConsumerDetails = new BatteryStats.EnergyConsumerDetails();
                }
                }


                final int consumerCount = src.readInt();
                final int consumerCount = src.readInt();
@@ -241,28 +241,28 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor
                            "EnergyConsumer count too high: " + consumerCount
                            "EnergyConsumer count too high: " + consumerCount
                                    + ". Max = " + MAX_ENERGY_CONSUMER_COUNT);
                                    + ". Max = " + MAX_ENERGY_CONSUMER_COUNT);
                }
                }
                mMeasuredEnergyDetails.consumers =
                mEnergyConsumerDetails.consumers =
                        new BatteryStats.MeasuredEnergyDetails.EnergyConsumer[consumerCount];
                        new BatteryStats.EnergyConsumerDetails.EnergyConsumer[consumerCount];
                mMeasuredEnergyDetails.chargeUC = new long[consumerCount];
                mEnergyConsumerDetails.chargeUC = new long[consumerCount];
                for (int i = 0; i < consumerCount; i++) {
                for (int i = 0; i < consumerCount; i++) {
                    BatteryStats.MeasuredEnergyDetails.EnergyConsumer consumer =
                    BatteryStats.EnergyConsumerDetails.EnergyConsumer consumer =
                            new BatteryStats.MeasuredEnergyDetails.EnergyConsumer();
                            new BatteryStats.EnergyConsumerDetails.EnergyConsumer();
                    consumer.type = src.readInt();
                    consumer.type = src.readInt();
                    consumer.ordinal = src.readInt();
                    consumer.ordinal = src.readInt();
                    consumer.name = src.readString();
                    consumer.name = src.readString();
                    mMeasuredEnergyDetails.consumers[i] = consumer;
                    mEnergyConsumerDetails.consumers[i] = consumer;
                }
                }
            }
            }


            if ((extensionFlags & BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_FLAG) != 0) {
            if ((extensionFlags & BatteryStatsHistory.EXTENSION_MEASURED_ENERGY_FLAG) != 0) {
                if (mMeasuredEnergyDetails == null) {
                if (mEnergyConsumerDetails == null) {
                    throw new IllegalStateException("MeasuredEnergyDetails without a header");
                    throw new IllegalStateException("MeasuredEnergyDetails without a header");
                }
                }


                mVarintParceler.readLongArray(src, mMeasuredEnergyDetails.chargeUC);
                mVarintParceler.readLongArray(src, mEnergyConsumerDetails.chargeUC);
                cur.measuredEnergyDetails = mMeasuredEnergyDetails;
                cur.energyConsumerDetails = mEnergyConsumerDetails;
            } else {
            } else {
                cur.measuredEnergyDetails = null;
                cur.energyConsumerDetails = null;
            }
            }


            if ((extensionFlags & BatteryStatsHistory.EXTENSION_CPU_USAGE_HEADER_FLAG) != 0) {
            if ((extensionFlags & BatteryStatsHistory.EXTENSION_CPU_USAGE_HEADER_FLAG) != 0) {
@@ -295,7 +295,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor
                cur.cpuUsageDetails = null;
                cur.cpuUsageDetails = null;
            }
            }
        } else {
        } else {
            cur.measuredEnergyDetails = null;
            cur.energyConsumerDetails = null;
            cur.cpuUsageDetails = null;
            cur.cpuUsageDetails = null;
        }
        }
    }
    }
+17 −17
Original line number Original line Diff line number Diff line
@@ -37,15 +37,14 @@ import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Arrays;


/**
/**
 * Tracks the measured charge consumption of various subsystems according to their
 * Tracks the charge consumption of various subsystems according to their
 * {@link StandardPowerBucket} or custom power bucket (which is tied to
 * {@link StandardPowerBucket} or custom power bucket (which is tied to
 * {@link android.hardware.power.stats.EnergyConsumer.ordinal}).
 * {@link android.hardware.power.stats.EnergyConsumer.ordinal}).
 *
 *
 * This class doesn't use a TimeBase, and instead requires manually decisions about when to
 * This class doesn't use a TimeBase, and instead requires manual decisions about when to
 * accumulate since it is trivial. However, in the future, a TimeBase could be used instead.
 * accumulate since it is trivial. However, in the future, a TimeBase could be used instead.
 */
 */
@VisibleForTesting
public class EnergyConsumerStats {
public class MeasuredEnergyStats {
    private static final String TAG = "MeasuredEnergyStats";
    private static final String TAG = "MeasuredEnergyStats";


    // Note: {@link BatteryStats#VERSION} MUST be updated if standard
    // Note: {@link BatteryStats#VERSION} MUST be updated if standard
@@ -205,7 +204,7 @@ public class MeasuredEnergyStats {
         */
         */
        private String getBucketName(int index) {
        private String getBucketName(int index) {
            if (isValidStandardBucket(index)) {
            if (isValidStandardBucket(index)) {
                return DebugUtils.valueToString(MeasuredEnergyStats.class, "POWER_BUCKET_", index);
                return DebugUtils.valueToString(EnergyConsumerStats.class, "POWER_BUCKET_", index);
            }
            }
            final int customBucket = indexToCustomBucket(index);
            final int customBucket = indexToCustomBucket(index);
            StringBuilder name = new StringBuilder().append("CUSTOM_").append(customBucket);
            StringBuilder name = new StringBuilder().append("CUSTOM_").append(customBucket);
@@ -242,7 +241,7 @@ public class MeasuredEnergyStats {
     * supportedStandardBuckets must be of size {@link #NUMBER_STANDARD_POWER_BUCKETS}.
     * supportedStandardBuckets must be of size {@link #NUMBER_STANDARD_POWER_BUCKETS}.
     * numCustomBuckets >= 0 is the number of (non-standard) custom power buckets on the device.
     * numCustomBuckets >= 0 is the number of (non-standard) custom power buckets on the device.
     */
     */
    public MeasuredEnergyStats(MeasuredEnergyStats.Config config) {
    public EnergyConsumerStats(EnergyConsumerStats.Config config) {
        mConfig = config;
        mConfig = config;
        final int numTotalBuckets = config.getNumberOfBuckets();
        final int numTotalBuckets = config.getNumberOfBuckets();
        mAccumulatedChargeMicroCoulomb = new long[numTotalBuckets];
        mAccumulatedChargeMicroCoulomb = new long[numTotalBuckets];
@@ -259,15 +258,15 @@ public class MeasuredEnergyStats {
     * Reads a MeasuredEnergyStats from the supplied Parcel.
     * Reads a MeasuredEnergyStats from the supplied Parcel.
     */
     */
    @Nullable
    @Nullable
    public static MeasuredEnergyStats createFromParcel(Config config, Parcel in) {
    public static EnergyConsumerStats createFromParcel(Config config, Parcel in) {
        if (!in.readBoolean()) {
        if (!in.readBoolean()) {
            return null;
            return null;
        }
        }
        return new MeasuredEnergyStats(config, in);
        return new EnergyConsumerStats(config, in);
    }
    }


    /** Construct from parcel. */
    /** Construct from parcel. */
    public MeasuredEnergyStats(MeasuredEnergyStats.Config config, Parcel in) {
    public EnergyConsumerStats(EnergyConsumerStats.Config config, Parcel in) {
        mConfig = config;
        mConfig = config;


        final int size = in.readInt();
        final int size = in.readInt();
@@ -533,21 +532,22 @@ public class MeasuredEnergyStats {
     * possible (not necessarily supported) standard and custom buckets.
     * possible (not necessarily supported) standard and custom buckets.
     *
     *
     * Corresponding write performed by
     * Corresponding write performed by
     * {@link #writeSummaryToParcel(MeasuredEnergyStats, Parcel)}.
     * {@link #writeSummaryToParcel(EnergyConsumerStats, Parcel)}.
     *
     *
     * @return a new MeasuredEnergyStats object as described.
     * @return a new MeasuredEnergyStats object as described.
     *         Returns null if the stats contain no non-0 information (such as if template is null
     *         Returns null if the stats contain no non-0 information (such as if template is null
     *         or if the parcel indicates there is no data to populate).
     *         or if the parcel indicates there is no data to populate).
     */
     */
    public static @Nullable MeasuredEnergyStats createAndReadSummaryFromParcel(
    @Nullable
            @Nullable Config config, Parcel in) {
    public static EnergyConsumerStats createAndReadSummaryFromParcel(@Nullable Config config,
            Parcel in) {
        final int arraySize = in.readInt();
        final int arraySize = in.readInt();
        // Check if any MeasuredEnergyStats exists on the parcel
        // Check if any MeasuredEnergyStats exists on the parcel
        if (arraySize == 0) return null;
        if (arraySize == 0) return null;


        if (config == null) {
        if (config == null) {
            // Nothing supported anymore. Create placeholder object just to consume the parcel data.
            // Nothing supported anymore. Create placeholder object just to consume the parcel data.
            final MeasuredEnergyStats mes = new MeasuredEnergyStats(
            final EnergyConsumerStats mes = new EnergyConsumerStats(
                    new Config(new boolean[arraySize], null, new int[0], new String[]{""}));
                    new Config(new boolean[arraySize], null, new int[0], new String[]{""}));
            mes.readSummaryFromParcel(in);
            mes.readSummaryFromParcel(in);
            return null;
            return null;
@@ -557,12 +557,12 @@ public class MeasuredEnergyStats {
            Slog.wtf(TAG, "Size of MeasuredEnergyStats parcel (" + arraySize
            Slog.wtf(TAG, "Size of MeasuredEnergyStats parcel (" + arraySize
                    + ") does not match config (" + config.getNumberOfBuckets() + ").");
                    + ") does not match config (" + config.getNumberOfBuckets() + ").");
            // Something is horribly wrong. Just consume the parcel and return null.
            // Something is horribly wrong. Just consume the parcel and return null.
            final MeasuredEnergyStats mes = new MeasuredEnergyStats(config);
            final EnergyConsumerStats mes = new EnergyConsumerStats(config);
            mes.readSummaryFromParcel(in);
            mes.readSummaryFromParcel(in);
            return null;
            return null;
        }
        }


        final MeasuredEnergyStats stats = new MeasuredEnergyStats(config);
        final EnergyConsumerStats stats = new EnergyConsumerStats(config);
        stats.readSummaryFromParcel(in);
        stats.readSummaryFromParcel(in);
        if (stats.containsInterestingData()) {
        if (stats.containsInterestingData()) {
            return stats;
            return stats;
@@ -585,7 +585,7 @@ public class MeasuredEnergyStats {
     *
     *
     * Corresponding read performed by {@link #createAndReadSummaryFromParcel}.
     * Corresponding read performed by {@link #createAndReadSummaryFromParcel}.
     */
     */
    public static void writeSummaryToParcel(@Nullable MeasuredEnergyStats stats, Parcel dest) {
    public static void writeSummaryToParcel(@Nullable EnergyConsumerStats stats, Parcel dest) {
        if (stats == null) {
        if (stats == null) {
            dest.writeInt(0);
            dest.writeInt(0);
            return;
            return;
@@ -607,7 +607,7 @@ public class MeasuredEnergyStats {
    }
    }


    /** Reset accumulated charges of the given stats. */
    /** Reset accumulated charges of the given stats. */
    public static void resetIfNotNull(@Nullable MeasuredEnergyStats stats) {
    public static void resetIfNotNull(@Nullable EnergyConsumerStats stats) {
        if (stats != null) stats.reset();
        if (stats != null) stats.reset();
    }
    }


Loading