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

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

Merge "Use POWER_COMPONENT_ constants instead of TIME_COMPONENT_... constants" into sc-dev

parents 22001255 70b37cbf
Loading
Loading
Loading
Loading
+9 −57
Original line number Diff line number Diff line
@@ -84,54 +84,6 @@ public abstract class BatteryConsumer {
    public static final int FIRST_CUSTOM_POWER_COMPONENT_ID = 1000;
    public static final int LAST_CUSTOM_POWER_COMPONENT_ID = 9999;

    /**
     * Time usage component, describing the particular part of the system
     * that was used for the corresponding amount of time.
     *
     * @hide
     */
    @IntDef(prefix = {"TIME_COMPONENT_"}, value = {
            TIME_COMPONENT_SCREEN,
            TIME_COMPONENT_CPU,
            TIME_COMPONENT_CPU_FOREGROUND,
            TIME_COMPONENT_BLUETOOTH,
            TIME_COMPONENT_CAMERA,
            TIME_COMPONENT_FLASHLIGHT,
            TIME_COMPONENT_MOBILE_RADIO,
            TIME_COMPONENT_SENSORS,
            TIME_COMPONENT_GNSS,
            TIME_COMPONENT_WIFI,
            TIME_COMPONENT_WAKELOCK,
            TIME_COMPONENT_MEMORY,
            TIME_COMPONENT_PHONE,
            TIME_COMPONENT_IDLE,
    })
    @Retention(RetentionPolicy.SOURCE)
    public static @interface TimeComponent {
    }

    public static final int TIME_COMPONENT_SCREEN = 0;
    public static final int TIME_COMPONENT_CPU = 1;
    public static final int TIME_COMPONENT_CPU_FOREGROUND = 2;
    public static final int TIME_COMPONENT_BLUETOOTH = 3;
    public static final int TIME_COMPONENT_CAMERA = 4;
    public static final int TIME_COMPONENT_AUDIO = 5;
    public static final int TIME_COMPONENT_VIDEO = 6;
    public static final int TIME_COMPONENT_FLASHLIGHT = 7;
    public static final int TIME_COMPONENT_MOBILE_RADIO = 8;
    public static final int TIME_COMPONENT_SENSORS = 9;
    public static final int TIME_COMPONENT_GNSS = 10;
    public static final int TIME_COMPONENT_WIFI = 11;
    public static final int TIME_COMPONENT_WAKELOCK = 12;
    public static final int TIME_COMPONENT_MEMORY = 13;
    public static final int TIME_COMPONENT_PHONE = 14;
    public static final int TIME_COMPONENT_IDLE = 15;

    public static final int TIME_COMPONENT_COUNT = 16;

    public static final int FIRST_CUSTOM_TIME_COMPONENT_ID = 1000;
    public static final int LAST_CUSTOM_TIME_COMPONENT_ID = 9999;

    /**
     * Identifiers of models used for power estimation.
     *
@@ -166,7 +118,7 @@ public abstract class BatteryConsumer {
     * Total power consumed by this consumer, in mAh.
     */
    public double getConsumedPower() {
        return mPowerComponents.getTotalConsumedPower();
        return mPowerComponents.getConsumedPower();
    }

    /**
@@ -221,11 +173,11 @@ public abstract class BatteryConsumer {
     * Returns the amount of time since BatteryStats reset used by the specified component, e.g.
     * CPU, WiFi etc.
     *
     * @param componentId The ID of the time component, e.g.
     *                    {@link UidBatteryConsumer#TIME_COMPONENT_CPU}.
     * @param componentId The ID of the power component, e.g.
     *                    {@link UidBatteryConsumer#POWER_COMPONENT_CPU}.
     * @return Amount of time in milliseconds.
     */
    public long getUsageDurationMillis(@TimeComponent int componentId) {
    public long getUsageDurationMillis(@PowerComponent int componentId) {
        return mPowerComponents.getUsageDurationMillis(componentId);
    }

@@ -248,9 +200,9 @@ public abstract class BatteryConsumer {
        final PowerComponents.Builder mPowerComponentsBuilder;

        public BaseBuilder(@NonNull String[] customPowerComponentNames,
                int customTimeComponentCount, boolean includePowerModels) {
                boolean includePowerModels) {
            mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentNames,
                    customTimeComponentCount, includePowerModels);
                    includePowerModels);
        }

        /**
@@ -296,13 +248,13 @@ public abstract class BatteryConsumer {
        /**
         * Sets the amount of time used by the specified component, e.g. CPU, WiFi etc.
         *
         * @param componentId              The ID of the time component, e.g.
         *                                 {@link UidBatteryConsumer#TIME_COMPONENT_CPU}.
         * @param componentId              The ID of the power component, e.g.
         *                                 {@link UidBatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentUsageTimeMillis Amount of time in microseconds.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setUsageDurationMillis(@UidBatteryConsumer.TimeComponent int componentId,
        public T setUsageDurationMillis(@UidBatteryConsumer.PowerComponent int componentId,
                long componentUsageTimeMillis) {
            mPowerComponentsBuilder.setUsageDurationMillis(componentId, componentUsageTimeMillis);
            return (T) this;
+6 −9
Original line number Diff line number Diff line
@@ -292,7 +292,6 @@ public final class BatteryUsageStats implements Parcelable {
    public static final class Builder {
        @NonNull
        private final String[] mCustomPowerComponentNames;
        private final int mCustomTimeComponentCount;
        private final boolean mIncludePowerModels;
        private long mStatsStartTimestampMs;
        private int mDischargePercentage;
@@ -309,14 +308,12 @@ public final class BatteryUsageStats implements Parcelable {
        private Parcel mHistoryBuffer;
        private List<BatteryStats.HistoryTag> mHistoryTagPool;

        public Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount) {
            this(customPowerComponentNames, customTimeComponentCount, false);
        public Builder(@NonNull String[] customPowerComponentNames) {
            this(customPowerComponentNames, false);
        }

        public Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount,
                boolean includePowerModels) {
        public Builder(@NonNull String[] customPowerComponentNames,  boolean includePowerModels) {
            mCustomPowerComponentNames = customPowerComponentNames;
            mCustomTimeComponentCount = customTimeComponentCount;
            mIncludePowerModels = includePowerModels;
        }

@@ -399,7 +396,7 @@ public final class BatteryUsageStats implements Parcelable {
            UidBatteryConsumer.Builder builder = mUidBatteryConsumerBuilders.get(uid);
            if (builder == null) {
                builder = new UidBatteryConsumer.Builder(mCustomPowerComponentNames,
                        mCustomTimeComponentCount, mIncludePowerModels, batteryStatsUid);
                        mIncludePowerModels, batteryStatsUid);
                mUidBatteryConsumerBuilders.put(uid, builder);
            }
            return builder;
@@ -415,7 +412,7 @@ public final class BatteryUsageStats implements Parcelable {
            SystemBatteryConsumer.Builder builder = mSystemBatteryConsumerBuilders.get(drainType);
            if (builder == null) {
                builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentNames,
                        mCustomTimeComponentCount, mIncludePowerModels, drainType);
                        mIncludePowerModels, drainType);
                mSystemBatteryConsumerBuilders.put(drainType, builder);
            }
            return builder;
@@ -430,7 +427,7 @@ public final class BatteryUsageStats implements Parcelable {
            UserBatteryConsumer.Builder builder = mUserBatteryConsumerBuilders.get(userId);
            if (builder == null) {
                builder = new UserBatteryConsumer.Builder(mCustomPowerComponentNames,
                        mCustomTimeComponentCount, mIncludePowerModels, userId);
                        mIncludePowerModels, userId);
                mUserBatteryConsumerBuilders.put(userId, builder);
            }
            return builder;
+40 −44
Original line number Diff line number Diff line
@@ -26,12 +26,10 @@ import android.annotation.NonNull;
class PowerComponents {
    private static final int CUSTOM_POWER_COMPONENT_OFFSET = BatteryConsumer.POWER_COMPONENT_COUNT
            - BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID;
    private static final int CUSTOM_TIME_COMPONENT_OFFSET = BatteryConsumer.TIME_COMPONENT_COUNT
            - BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID;

    private final double mTotalConsumedPowerMah;
    private final double mConsumedPowerMah;
    private final double[] mPowerComponentsMah;
    private final long[] mTimeComponentsMs;
    private final long[] mUsageDurationsMs;
    private final int mCustomPowerComponentCount;
    private final byte[] mPowerModels;
    // Not written to Parcel and must be explicitly restored during the parent object's unparceling
@@ -41,16 +39,16 @@ class PowerComponents {
        mCustomPowerComponentNames = builder.mCustomPowerComponentNames;
        mCustomPowerComponentCount = mCustomPowerComponentNames.length;
        mPowerComponentsMah = builder.mPowerComponentsMah;
        mTimeComponentsMs = builder.mTimeComponentsMs;
        mTotalConsumedPowerMah = builder.getTotalPower();
        mUsageDurationsMs = builder.mUsageDurationsMs;
        mConsumedPowerMah = builder.getTotalPower();
        mPowerModels = builder.mPowerModels;
    }

    PowerComponents(@NonNull Parcel source) {
        mTotalConsumedPowerMah = source.readDouble();
        mConsumedPowerMah = source.readDouble();
        mCustomPowerComponentCount = source.readInt();
        mPowerComponentsMah = source.createDoubleArray();
        mTimeComponentsMs = source.createLongArray();
        mUsageDurationsMs = source.createLongArray();
        if (source.readBoolean()) {
            mPowerModels = new byte[BatteryConsumer.POWER_COMPONENT_COUNT];
            source.readByteArray(mPowerModels);
@@ -61,10 +59,10 @@ class PowerComponents {

    /** Writes contents to Parcel */
    void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeDouble(mTotalConsumedPowerMah);
        dest.writeDouble(mConsumedPowerMah);
        dest.writeInt(mCustomPowerComponentCount);
        dest.writeDoubleArray(mPowerComponentsMah);
        dest.writeLongArray(mTimeComponentsMs);
        dest.writeLongArray(mUsageDurationsMs);
        if (mPowerModels != null) {
            dest.writeBoolean(true);
            dest.writeByteArray(mPowerModels);
@@ -76,8 +74,8 @@ class PowerComponents {
    /**
     * Total power consumed by this consumer, in mAh.
     */
    public double getTotalConsumedPower() {
        return mTotalConsumedPowerMah;
    public double getConsumedPower() {
        return mConsumedPowerMah;
    }

    /**
@@ -152,17 +150,17 @@ class PowerComponents {
    /**
     * Returns the amount of time used by the specified component, e.g. CPU, WiFi etc.
     *
     * @param componentId The ID of the time component, e.g.
     *                    {@link BatteryConsumer#TIME_COMPONENT_CPU}.
     * @param componentId The ID of the power component, e.g.
     *                    {@link BatteryConsumer#POWER_COMPONENT_CPU}.
     * @return Amount of time in milliseconds.
     */
    public long getUsageDurationMillis(@BatteryConsumer.TimeComponent int componentId) {
        if (componentId >= BatteryConsumer.TIME_COMPONENT_COUNT) {
    public long getUsageDurationMillis(@BatteryConsumer.PowerComponent int componentId) {
        if (componentId >= BatteryConsumer.POWER_COMPONENT_COUNT) {
            throw new IllegalArgumentException(
                    "Unsupported time component ID: " + componentId);
                    "Unsupported power component ID: " + componentId);
        }
        try {
            return mTimeComponentsMs[componentId];
            return mUsageDurationsMs[componentId];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException("Unsupported power component ID: " + componentId);
        }
@@ -175,15 +173,15 @@ class PowerComponents {
     * @return Amount of time in milliseconds.
     */
    public long getUsageDurationForCustomComponentMillis(int componentId) {
        if (componentId < BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID) {
        if (componentId < BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID) {
            throw new IllegalArgumentException(
                    "Unsupported custom time component ID: " + componentId);
                    "Unsupported custom power component ID: " + componentId);
        }
        try {
            return mTimeComponentsMs[CUSTOM_TIME_COMPONENT_OFFSET + componentId];
            return mUsageDurationsMs[CUSTOM_POWER_COMPONENT_OFFSET + componentId];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new IllegalArgumentException(
                    "Unsupported custom time component ID: " + componentId);
                    "Unsupported custom power component ID: " + componentId);
        }
    }

@@ -192,13 +190,13 @@ class PowerComponents {
    }

    /**
     * Returns the largest usage duration among all time components.
     * Returns the largest usage duration among all power components.
     */
    public long getMaxComponentUsageDurationMillis() {
        long max = 0;
        for (int i = mTimeComponentsMs.length - 1; i >= 0; i--) {
            if (mTimeComponentsMs[i] > max) {
                max = mTimeComponentsMs[i];
        for (int i = mUsageDurationsMs.length - 1; i >= 0; i--) {
            if (mUsageDurationsMs[i] > max) {
                max = mUsageDurationsMs[i];
            }
        }
        return max;
@@ -210,17 +208,15 @@ class PowerComponents {
    static final class Builder {
        private final double[] mPowerComponentsMah;
        private final String[] mCustomPowerComponentNames;
        private final long[] mTimeComponentsMs;
        private final long[] mUsageDurationsMs;
        private final byte[] mPowerModels;

        Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount,
                boolean includePowerModels) {
        Builder(@NonNull String[] customPowerComponentNames, boolean includePowerModels) {
            mCustomPowerComponentNames = customPowerComponentNames;
            int powerComponentCount =
                    BatteryConsumer.POWER_COMPONENT_COUNT + mCustomPowerComponentNames.length;
            mPowerComponentsMah = new double[powerComponentCount];
            mTimeComponentsMs =
                    new long[BatteryConsumer.TIME_COMPONENT_COUNT + customTimeComponentCount];
            mUsageDurationsMs = new long[powerComponentCount];
            if (includePowerModels) {
                mPowerModels = new byte[BatteryConsumer.POWER_COMPONENT_COUNT];
            } else {
@@ -281,22 +277,22 @@ class PowerComponents {
        /**
         * Sets the amount of time used by the specified component, e.g. CPU, WiFi etc.
         *
         * @param componentId                  The ID of the time component, e.g.
         *                                     {@link BatteryConsumer#TIME_COMPONENT_CPU}.
         * @param componentId                  The ID of the power component, e.g.
         *                                     {@link BatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentUsageDurationMillis Amount of time in milliseconds.
         */
        @NonNull
        public Builder setUsageDurationMillis(@BatteryConsumer.TimeComponent int componentId,
        public Builder setUsageDurationMillis(@BatteryConsumer.PowerComponent int componentId,
                long componentUsageDurationMillis) {
            if (componentId >= BatteryConsumer.TIME_COMPONENT_COUNT) {
            if (componentId >= BatteryConsumer.POWER_COMPONENT_COUNT) {
                throw new IllegalArgumentException(
                        "Unsupported time component ID: " + componentId);
                        "Unsupported power component ID: " + componentId);
            }
            try {
                mTimeComponentsMs[componentId] = componentUsageDurationMillis;
                mUsageDurationsMs[componentId] = componentUsageDurationMillis;
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
                        "Unsupported time component ID: " + componentId);
                        "Unsupported power component ID: " + componentId);
            }
            return this;
        }
@@ -310,16 +306,16 @@ class PowerComponents {
        @NonNull
        public Builder setUsageDurationForCustomComponentMillis(int componentId,
                long componentUsageDurationMillis) {
            if (componentId < BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID) {
            if (componentId < BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID) {
                throw new IllegalArgumentException(
                        "Unsupported custom time component ID: " + componentId);
                        "Unsupported custom power component ID: " + componentId);
            }
            try {
                mTimeComponentsMs[CUSTOM_TIME_COMPONENT_OFFSET + componentId] =
                mUsageDurationsMs[CUSTOM_POWER_COMPONENT_OFFSET + componentId] =
                        componentUsageDurationMillis;
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
                        "Unsupported custom time component ID: " + componentId);
                        "Unsupported custom power component ID: " + componentId);
            }
            return this;
        }
@@ -328,8 +324,8 @@ class PowerComponents {
            for (int i = mPowerComponentsMah.length - 1; i >= 0; i--) {
                mPowerComponentsMah[i] += other.mPowerComponentsMah[i];
            }
            for (int i = mTimeComponentsMs.length - 1; i >= 0; i--) {
                mTimeComponentsMs[i] += other.mTimeComponentsMs[i];
            for (int i = mUsageDurationsMs.length - 1; i >= 0; i--) {
                mUsageDurationsMs[i] += other.mUsageDurationsMs[i];
            }
        }

+2 −2
Original line number Diff line number Diff line
@@ -147,9 +147,9 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
        private double mPowerConsumedByAppsMah;
        private List<UidBatteryConsumer.Builder> mUidBatteryConsumers;

        Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount,
        Builder(@NonNull String[] customPowerComponentNames,
                boolean includePowerModels, @DrainType int drainType) {
            super(customPowerComponentNames, customTimeComponentCount, includePowerModels);
            super(customPowerComponentNames, includePowerModels);
            mDrainType = drainType;
        }

+2 −2
Original line number Diff line number Diff line
@@ -139,9 +139,9 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela
        public long mTimeInBackgroundMs;
        private boolean mExcludeFromBatteryUsageStats;

        public Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount,
        public Builder(@NonNull String[] customPowerComponentNames,
                boolean includePowerModels, @NonNull BatteryStats.Uid batteryStatsUid) {
            super(customPowerComponentNames, customTimeComponentCount, includePowerModels);
            super(customPowerComponentNames, includePowerModels);
            mBatteryStatsUid = batteryStatsUid;
            mUid = batteryStatsUid.getUid();
        }
Loading