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

Commit 0b4fe941 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Add discharge power estimates to BatteryUsageStats" into sc-dev am: 9fb89c22

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13538912

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Id8b5163e2924cc88d21407e6f4d2ec6584db8d99
parents debe0891 9fb89c22
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ public abstract class BatteryConsumer {
     * Total power consumed by this consumer, in mAh.
     */
    public double getConsumedPower() {
        return mPowerComponents.getTotalPowerConsumed();
        return mPowerComponents.getTotalConsumedPower();
    }

    /**
+12 −7
Original line number Diff line number Diff line
@@ -1671,7 +1671,7 @@ public abstract class BatteryStats implements Parcelable {
        public char batteryVoltage;

        // The charge of the battery in micro-Ampere-hours.
        public int batteryChargeUAh;
        public int batteryChargeUah;

        public double modemRailChargeMah;
        public double wifiRailChargeMah;
@@ -1884,7 +1884,7 @@ public abstract class BatteryStats implements Parcelable {
            bat = (((int)batteryTemperature)&0xffff)
                    | ((((int)batteryVoltage)<<16)&0xffff0000);
            dest.writeInt(bat);
            dest.writeInt(batteryChargeUAh);
            dest.writeInt(batteryChargeUah);
            dest.writeDouble(modemRailChargeMah);
            dest.writeDouble(wifiRailChargeMah);
            dest.writeInt(states);
@@ -1916,7 +1916,7 @@ public abstract class BatteryStats implements Parcelable {
            int bat2 = src.readInt();
            batteryTemperature = (short)(bat2&0xffff);
            batteryVoltage = (char)((bat2>>16)&0xffff);
            batteryChargeUAh = src.readInt();
            batteryChargeUah = src.readInt();
            modemRailChargeMah = src.readDouble();
            wifiRailChargeMah = src.readDouble();
            states = src.readInt();
@@ -1959,7 +1959,7 @@ public abstract class BatteryStats implements Parcelable {
            batteryPlugType = 0;
            batteryTemperature = 0;
            batteryVoltage = 0;
            batteryChargeUAh = 0;
            batteryChargeUah = 0;
            modemRailChargeMah = 0;
            wifiRailChargeMah = 0;
            states = 0;
@@ -1991,7 +1991,7 @@ public abstract class BatteryStats implements Parcelable {
            batteryPlugType = o.batteryPlugType;
            batteryTemperature = o.batteryTemperature;
            batteryVoltage = o.batteryVoltage;
            batteryChargeUAh = o.batteryChargeUAh;
            batteryChargeUah = o.batteryChargeUah;
            modemRailChargeMah = o.modemRailChargeMah;
            wifiRailChargeMah = o.wifiRailChargeMah;
            states = o.states;
@@ -2025,7 +2025,7 @@ public abstract class BatteryStats implements Parcelable {
                    && batteryPlugType == o.batteryPlugType
                    && batteryTemperature == o.batteryTemperature
                    && batteryVoltage == o.batteryVoltage
                    && batteryChargeUAh == o.batteryChargeUAh
                    && batteryChargeUah == o.batteryChargeUah
                    && modemRailChargeMah == o.modemRailChargeMah
                    && wifiRailChargeMah == o.wifiRailChargeMah
                    && states == o.states
@@ -2858,6 +2858,11 @@ public abstract class BatteryStats implements Parcelable {
     */
    public abstract boolean getIsOnBattery();

    /**
     * Returns the timestamp of when battery stats collection started, in microseconds.
     */
    public abstract long getStatsStartRealtime();

    /**
     * Returns a SparseArray containing the statistics for each uid.
     */
@@ -6520,7 +6525,7 @@ public abstract class BatteryStats implements Parcelable {
                    item.append(checkin ? ",Bv=" : " volt=");
                    item.append(oldVolt);
                }
                final int chargeMAh = rec.batteryChargeUAh / 1000;
                final int chargeMAh = rec.batteryChargeUah / 1000;
                if (oldChargeMAh != chargeMAh) {
                    oldChargeMAh = chargeMAh;
                    item.append(checkin ? ",Bcc=" : " charge=");
+67 −18
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
package android.os;

import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.util.Range;
import android.util.SparseArray;

import java.util.ArrayList;
@@ -31,35 +31,59 @@ import java.util.List;
public final class BatteryUsageStats implements Parcelable {
    private final double mConsumedPower;
    private final int mDischargePercentage;
    private final long mStatsStartRealtimeMs;
    private final double mDischargedPowerLowerBound;
    private final double mDischargedPowerUpperBound;
    private final ArrayList<UidBatteryConsumer> mUidBatteryConsumers;
    private final ArrayList<SystemBatteryConsumer> mSystemBatteryConsumers;
    private final ArrayList<UserBatteryConsumer> mUserBatteryConsumers;

    private BatteryUsageStats(@NonNull Builder builder) {
        mConsumedPower = builder.mConsumedPower;
        mStatsStartRealtimeMs = builder.mStatsStartRealtimeMs;
        mDischargePercentage = builder.mDischargePercentage;
        mDischargedPowerLowerBound = builder.mDischargedPowerLowerBoundMah;
        mDischargedPowerUpperBound = builder.mDischargedPowerUpperBoundMah;

        int uidBatteryConsumerCount = builder.mUidBatteryConsumerBuilders.size();
        double totalPower = 0;

        final int uidBatteryConsumerCount = builder.mUidBatteryConsumerBuilders.size();
        mUidBatteryConsumers = new ArrayList<>(uidBatteryConsumerCount);
        for (int i = 0; i < uidBatteryConsumerCount; i++) {
            UidBatteryConsumer.Builder uidBatteryConsumerBuilder =
            final UidBatteryConsumer.Builder uidBatteryConsumerBuilder =
                    builder.mUidBatteryConsumerBuilders.valueAt(i);
            if (!uidBatteryConsumerBuilder.isExcludedFromBatteryUsageStats()) {
                mUidBatteryConsumers.add(uidBatteryConsumerBuilder.build());
                final UidBatteryConsumer consumer = uidBatteryConsumerBuilder.build();
                totalPower += consumer.getConsumedPower();
                mUidBatteryConsumers.add(consumer);
            }
        }

        int systemBatteryConsumerCount = builder.mSystemBatteryConsumerBuilders.size();
        final int systemBatteryConsumerCount = builder.mSystemBatteryConsumerBuilders.size();
        mSystemBatteryConsumers = new ArrayList<>(systemBatteryConsumerCount);
        for (int i = 0; i < systemBatteryConsumerCount; i++) {
            mSystemBatteryConsumers.add(builder.mSystemBatteryConsumerBuilders.valueAt(i).build());
            final SystemBatteryConsumer consumer =
                    builder.mSystemBatteryConsumerBuilders.valueAt(i).build();
            totalPower += consumer.getConsumedPower();
            mSystemBatteryConsumers.add(consumer);
        }

        int userBatteryConsumerCount = builder.mUserBatteryConsumerBuilders.size();
        final int userBatteryConsumerCount = builder.mUserBatteryConsumerBuilders.size();
        mUserBatteryConsumers = new ArrayList<>(userBatteryConsumerCount);
        for (int i = 0; i < userBatteryConsumerCount; i++) {
            mUserBatteryConsumers.add(builder.mUserBatteryConsumerBuilders.valueAt(i).build());
            final UserBatteryConsumer consumer =
                    builder.mUserBatteryConsumerBuilders.valueAt(i).build();
            totalPower += consumer.getConsumedPower();
            mUserBatteryConsumers.add(consumer);
        }

        mConsumedPower = totalPower;
    }

    /**
     * Timestamp of the latest battery stats reset, in milliseconds.
     */
    public long getStatsStartRealtime() {
        return mStatsStartRealtimeMs;
    }

    /**
@@ -70,6 +94,14 @@ public final class BatteryUsageStats implements Parcelable {
        return mDischargePercentage;
    }

    /**
     * Returns the discharged power since BatteryStats were last reset, in mAh as an estimated
     * range.
     */
    public Range<Double> getDischargedPowerRange() {
        return Range.create(mDischargedPowerLowerBound, mDischargedPowerUpperBound);
    }

    /**
     * Total amount of battery charge drained since BatteryStats reset (e.g. due to being fully
     * charged), in mAh
@@ -99,23 +131,29 @@ public final class BatteryUsageStats implements Parcelable {
    }

    private BatteryUsageStats(@NonNull Parcel source) {
        mStatsStartRealtimeMs = source.readLong();
        mConsumedPower = source.readDouble();
        mDischargePercentage = source.readInt();
        mDischargedPowerLowerBound = source.readDouble();
        mDischargedPowerUpperBound = source.readDouble();
        mUidBatteryConsumers = new ArrayList<>();
        source.readParcelableList(mUidBatteryConsumers, getClass().getClassLoader());
        mSystemBatteryConsumers = new ArrayList<>();
        source.readParcelableList(mSystemBatteryConsumers, getClass().getClassLoader());
        mUserBatteryConsumers = new ArrayList<>();
        source.readParcelableList(mUserBatteryConsumers, getClass().getClassLoader());
        mConsumedPower = source.readDouble();
        mDischargePercentage = source.readInt();
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeLong(mStatsStartRealtimeMs);
        dest.writeDouble(mConsumedPower);
        dest.writeInt(mDischargePercentage);
        dest.writeDouble(mDischargedPowerLowerBound);
        dest.writeDouble(mDischargedPowerUpperBound);
        dest.writeParcelableList(mUidBatteryConsumers, flags);
        dest.writeParcelableList(mSystemBatteryConsumers, flags);
        dest.writeParcelableList(mUserBatteryConsumers, flags);
        dest.writeDouble(mConsumedPower);
        dest.writeInt(mDischargePercentage);
    }

    @NonNull
@@ -135,8 +173,10 @@ public final class BatteryUsageStats implements Parcelable {
    public static final class Builder {
        private final int mCustomPowerComponentCount;
        private final int mCustomTimeComponentCount;
        private double mConsumedPower;
        private long mStatsStartRealtimeMs;
        private int mDischargePercentage;
        private double mDischargedPowerLowerBoundMah;
        private double mDischargedPowerUpperBoundMah;
        private final SparseArray<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders =
                new SparseArray<>();
        private final SparseArray<SystemBatteryConsumer.Builder> mSystemBatteryConsumerBuilders =
@@ -157,11 +197,18 @@ public final class BatteryUsageStats implements Parcelable {
            return new BatteryUsageStats(this);
        }

        /**
         * Sets the timestamp of the latest battery stats reset, in milliseconds.
         */
        public Builder setStatsStartRealtime(long statsStartRealtimeMs) {
            mStatsStartRealtimeMs = statsStartRealtimeMs;
            return this;
        }

        /**
         * Sets the battery discharge amount since BatteryStats reset as percentage of the full
         * charge.
         */
        @SuppressLint("PercentageInt") // See b/174188159
        @NonNull
        public Builder setDischargePercentage(int dischargePercentage) {
            mDischargePercentage = dischargePercentage;
@@ -169,11 +216,13 @@ public final class BatteryUsageStats implements Parcelable {
        }

        /**
         * Sets the battery discharge amount since BatteryStats reset, in mAh.
         * Sets the estimated battery discharge range.
         */
        @NonNull
        public Builder setConsumedPower(double consumedPower) {
            mConsumedPower = consumedPower;
        public Builder setDischargedPowerRange(double dischargedPowerLowerBoundMah,
                double dischargedPowerUpperBoundMah) {
            mDischargedPowerLowerBoundMah = dischargedPowerLowerBoundMah;
            mDischargedPowerUpperBoundMah = dischargedPowerUpperBoundMah;
            return this;
        }

+35 −34
Original line number Diff line number Diff line
@@ -29,38 +29,38 @@ class PowerComponents {
    public static final int CUSTOM_TIME_COMPONENT_OFFSET = BatteryConsumer.TIME_COMPONENT_COUNT
            - BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID;

    private final double mTotalPowerConsumed;
    private final double[] mPowerComponents;
    private final long[] mTimeComponents;
    private final double mTotalConsumedPowerMah;
    private final double[] mPowerComponentsMah;
    private final long[] mTimeComponentsMs;
    private final int mCustomPowerComponentCount;

    PowerComponents(@NonNull Builder builder) {
        mCustomPowerComponentCount = builder.mCustomPowerComponentCount;
        mPowerComponents = builder.mPowerComponents;
        mTimeComponents = builder.mTimeComponents;
        mTotalPowerConsumed = builder.getTotalPower();
        mPowerComponentsMah = builder.mPowerComponentsMah;
        mTimeComponentsMs = builder.mTimeComponentsMs;
        mTotalConsumedPowerMah = builder.getTotalPower();
    }

    PowerComponents(@NonNull Parcel source) {
        mTotalPowerConsumed = source.readDouble();
        mTotalConsumedPowerMah = source.readDouble();
        mCustomPowerComponentCount = source.readInt();
        mPowerComponents = source.createDoubleArray();
        mTimeComponents = source.createLongArray();
        mPowerComponentsMah = source.createDoubleArray();
        mTimeComponentsMs = source.createLongArray();
    }

    /** Writes contents to Parcel */
    void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeDouble(mTotalPowerConsumed);
        dest.writeDouble(mTotalConsumedPowerMah);
        dest.writeInt(mCustomPowerComponentCount);
        dest.writeDoubleArray(mPowerComponents);
        dest.writeLongArray(mTimeComponents);
        dest.writeDoubleArray(mPowerComponentsMah);
        dest.writeLongArray(mTimeComponentsMs);
    }

    /**
     * Total power consumed by this consumer, in mAh.
     */
    public double getTotalPowerConsumed() {
        return mTotalPowerConsumed;
    public double getTotalConsumedPower() {
        return mTotalConsumedPowerMah;
    }

    /**
@@ -76,7 +76,7 @@ class PowerComponents {
                    "Unsupported power component ID: " + componentId);
        }
        try {
            return mPowerComponents[componentId];
            return mPowerComponentsMah[componentId];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException("Unsupported power component ID: " + componentId);
        }
@@ -92,7 +92,7 @@ class PowerComponents {
        if (componentId >= BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID
                && componentId < BatteryConsumer.LAST_CUSTOM_POWER_COMPONENT_ID) {
            try {
                return mPowerComponents[CUSTOM_POWER_COMPONENT_OFFSET + componentId];
                return mPowerComponentsMah[CUSTOM_POWER_COMPONENT_OFFSET + componentId];
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
                        "Unsupported custom power component ID: " + componentId);
@@ -116,7 +116,7 @@ class PowerComponents {
                    "Unsupported time component ID: " + componentId);
        }
        try {
            return mTimeComponents[componentId];
            return mTimeComponentsMs[componentId];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException("Unsupported power component ID: " + componentId);
        }
@@ -134,7 +134,7 @@ class PowerComponents {
                    "Unsupported custom time component ID: " + componentId);
        }
        try {
            return mTimeComponents[CUSTOM_TIME_COMPONENT_OFFSET + componentId];
            return mTimeComponentsMs[CUSTOM_TIME_COMPONENT_OFFSET + componentId];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException(
                    "Unsupported custom time component ID: " + componentId);
@@ -145,16 +145,16 @@ class PowerComponents {
     * Builder for PowerComponents.
     */
    static final class Builder {
        private final double[] mPowerComponents;
        private final double[] mPowerComponentsMah;
        private final int mCustomPowerComponentCount;
        private final long[] mTimeComponents;
        private final long[] mTimeComponentsMs;

        Builder(int customPowerComponentCount, int customTimeComponentCount) {
            mCustomPowerComponentCount = customPowerComponentCount;
            int powerComponentCount =
                    BatteryConsumer.POWER_COMPONENT_COUNT + customPowerComponentCount;
            mPowerComponents = new double[powerComponentCount];
            mTimeComponents =
            mPowerComponentsMah = new double[powerComponentCount];
            mTimeComponentsMs =
                    new long[BatteryConsumer.TIME_COMPONENT_COUNT + customTimeComponentCount];
        }

@@ -173,7 +173,7 @@ class PowerComponents {
                        "Unsupported power component ID: " + componentId);
            }
            try {
                mPowerComponents[componentId] = componentPower;
                mPowerComponentsMah[componentId] = componentPower;
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
                        "Unsupported power component ID: " + componentId);
@@ -192,7 +192,8 @@ class PowerComponents {
            if (componentId >= BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID
                    && componentId < BatteryConsumer.LAST_CUSTOM_POWER_COMPONENT_ID) {
                try {
                    mPowerComponents[CUSTOM_POWER_COMPONENT_OFFSET + componentId] = componentPower;
                    mPowerComponentsMah[CUSTOM_POWER_COMPONENT_OFFSET + componentId] =
                            componentPower;
                } catch (ArrayIndexOutOfBoundsException e) {
                    throw new IllegalArgumentException(
                            "Unsupported custom power component ID: " + componentId);
@@ -219,7 +220,7 @@ class PowerComponents {
                        "Unsupported time component ID: " + componentId);
            }
            try {
                mTimeComponents[componentId] = componentUsageDurationMillis;
                mTimeComponentsMs[componentId] = componentUsageDurationMillis;
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
                        "Unsupported time component ID: " + componentId);
@@ -241,7 +242,7 @@ class PowerComponents {
                        "Unsupported custom time component ID: " + componentId);
            }
            try {
                mTimeComponents[CUSTOM_TIME_COMPONENT_OFFSET + componentId] =
                mTimeComponentsMs[CUSTOM_TIME_COMPONENT_OFFSET + componentId] =
                        componentUsageDurationMillis;
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
@@ -251,11 +252,11 @@ class PowerComponents {
        }

        public void addPowerAndDuration(Builder other) {
            for (int i = 0; i < mPowerComponents.length; i++) {
                mPowerComponents[i] += other.mPowerComponents[i];
            for (int i = 0; i < mPowerComponentsMah.length; i++) {
                mPowerComponentsMah[i] += other.mPowerComponentsMah[i];
            }
            for (int i = 0; i < mTimeComponents.length; i++) {
                mTimeComponents[i] += other.mTimeComponents[i];
            for (int i = 0; i < mTimeComponentsMs.length; i++) {
                mTimeComponentsMs[i] += other.mTimeComponentsMs[i];
            }
        }

@@ -264,11 +265,11 @@ class PowerComponents {
         * by the time the {@code build()} method is called.
         */
        public double getTotalPower() {
            double totalPower = 0;
            for (int i = mPowerComponents.length - 1; i >= 0; i--) {
                totalPower += mPowerComponents[i];
            double totalPowerMah = 0;
            for (int i = mPowerComponentsMah.length - 1; i >= 0; i--) {
                totalPowerMah += mPowerComponentsMah[i];
            }
            return totalPower;
            return totalPowerMah;
        }

        /**
+2 −4
Original line number Diff line number Diff line
@@ -45,10 +45,10 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
            DRAIN_TYPE_FLASHLIGHT,
            DRAIN_TYPE_IDLE,
            DRAIN_TYPE_MEMORY,
            DRAIN_TYPE_OVERCOUNTED,
            // Reserved: OVERCOUNTED,
            DRAIN_TYPE_PHONE,
            DRAIN_TYPE_SCREEN,
            DRAIN_TYPE_UNACCOUNTED,
            // Reserved: UNACCOUNTED,
            // Reserved: USER,
            DRAIN_TYPE_WIFI,
    })
@@ -63,10 +63,8 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
    public static final int DRAIN_TYPE_FLASHLIGHT = 5;
    public static final int DRAIN_TYPE_IDLE = 6;
    public static final int DRAIN_TYPE_MEMORY = 7;
    public static final int DRAIN_TYPE_OVERCOUNTED = 8;
    public static final int DRAIN_TYPE_PHONE = 9;
    public static final int DRAIN_TYPE_SCREEN = 10;
    public static final int DRAIN_TYPE_UNACCOUNTED = 11;
    public static final int DRAIN_TYPE_WIFI = 13;

    @DrainType
Loading