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

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

Merge "Add BatteryConsumer.getPowerModel() method" into sc-dev

parents 4cafa299 c13f44e0
Loading
Loading
Loading
Loading
+52 −4
Original line number Diff line number Diff line
@@ -124,6 +124,30 @@ public abstract class BatteryConsumer {
    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.
     *
     * @hide
     */
    @IntDef(prefix = {"POWER_MODEL_"}, value = {
            POWER_MODEL_POWER_PROFILE,
            POWER_MODEL_MEASURED_ENERGY,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface PowerModel {
    }

    /**
     * Power model that is based on average consumption rates that hardware components
     * consume in various states.
     */
    public static final int POWER_MODEL_POWER_PROFILE = 0;

    /**
     * Power model that is based on energy consumption measured by on-device power monitors.
     */
    public static final int POWER_MODEL_MEASURED_ENERGY = 1;

    private final PowerComponents mPowerComponents;

    protected BatteryConsumer(@NonNull PowerComponents powerComponents) {
@@ -148,6 +172,16 @@ public abstract class BatteryConsumer {
        return mPowerComponents.getConsumedPower(componentId);
    }

    /**
     * Returns the ID of the model that was used for power estimation.
     *
     * @param componentId The ID of the power component, e.g.
     *                    {@link BatteryConsumer#POWER_COMPONENT_CPU}.
     */
    public @PowerModel int getPowerModel(@BatteryConsumer.PowerComponent int componentId) {
        return mPowerComponents.getPowerModel(componentId);
    }

    /**
     * Returns the amount of drain attributed to the specified custom drain type.
     *
@@ -188,9 +222,10 @@ public abstract class BatteryConsumer {
    protected abstract static class BaseBuilder<T extends BaseBuilder<?>> {
        final PowerComponents.Builder mPowerComponentsBuilder;

        public BaseBuilder(int customPowerComponentCount, int customTimeComponentCount) {
        public BaseBuilder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includePowerModels) {
            mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentCount,
                    customTimeComponentCount);
                    customTimeComponentCount, includePowerModels);
        }

        /**
@@ -200,10 +235,23 @@ public abstract class BatteryConsumer {
         *                       {@link BatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentPower Amount of consumed power in mAh.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setConsumedPower(@PowerComponent int componentId, double componentPower) {
            mPowerComponentsBuilder.setConsumedPower(componentId, componentPower);
            return setConsumedPower(componentId, componentPower, POWER_MODEL_POWER_PROFILE);
        }

        /**
         * Sets the amount of drain attributed to the specified drain type, e.g. CPU, WiFi etc.
         *
         * @param componentId    The ID of the power component, e.g.
         *                       {@link BatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentPower Amount of consumed power in mAh.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setConsumedPower(@PowerComponent int componentId, double componentPower,
                @PowerModel int powerModel) {
            mPowerComponentsBuilder.setConsumedPower(componentId, componentPower, powerModel);
            return (T) this;
        }

+10 −3
Original line number Diff line number Diff line
@@ -261,6 +261,7 @@ public final class BatteryUsageStats implements Parcelable {
    public static final class Builder {
        private final int mCustomPowerComponentCount;
        private final int mCustomTimeComponentCount;
        private final boolean mIncludePowerModels;
        private long mStatsStartTimestampMs;
        private int mDischargePercentage;
        private double mDischargedPowerLowerBoundMah;
@@ -277,8 +278,14 @@ public final class BatteryUsageStats implements Parcelable {
        private List<BatteryStats.HistoryTag> mHistoryTagPool;

        public Builder(int customPowerComponentCount, int customTimeComponentCount) {
            this(customPowerComponentCount, customTimeComponentCount, false);
        }

        public Builder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includePowerModels) {
            mCustomPowerComponentCount = customPowerComponentCount;
            mCustomTimeComponentCount = customTimeComponentCount;
            mIncludePowerModels = includePowerModels;
        }

        /**
@@ -360,7 +367,7 @@ public final class BatteryUsageStats implements Parcelable {
            UidBatteryConsumer.Builder builder = mUidBatteryConsumerBuilders.get(uid);
            if (builder == null) {
                builder = new UidBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, batteryStatsUid);
                        mCustomTimeComponentCount, mIncludePowerModels, batteryStatsUid);
                mUidBatteryConsumerBuilders.put(uid, builder);
            }
            return builder;
@@ -376,7 +383,7 @@ public final class BatteryUsageStats implements Parcelable {
            SystemBatteryConsumer.Builder builder = mSystemBatteryConsumerBuilders.get(drainType);
            if (builder == null) {
                builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, drainType);
                        mCustomTimeComponentCount, mIncludePowerModels, drainType);
                mSystemBatteryConsumerBuilders.put(drainType, builder);
            }
            return builder;
@@ -391,7 +398,7 @@ public final class BatteryUsageStats implements Parcelable {
            UserBatteryConsumer.Builder builder = mUserBatteryConsumerBuilders.get(userId);
            if (builder == null) {
                builder = new UserBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, userId);
                        mCustomTimeComponentCount, mIncludePowerModels, userId);
                mUserBatteryConsumerBuilders.put(userId, builder);
            }
            return builder;
+17 −0
Original line number Diff line number Diff line
@@ -60,6 +60,12 @@ public final class BatteryUsageStatsQuery implements Parcelable {
     */
    public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY = 2;

    /**
     * Indicates that identifiers of power models used for computations of power
     * consumption should be included in the BatteryUsageStats.
     */
    public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS = 4;

    private static final long DEFAULT_MAX_STATS_AGE_MS = 5 * 60 * 1000;

    private final int mFlags;
@@ -186,6 +192,17 @@ public final class BatteryUsageStatsQuery implements Parcelable {
            return this;
        }

        /**
         * Requests to return identifiers of models that were used for estimation
         * of power consumption.
         *
         * Should only be used for testing and debugging.
         */
        public Builder includePowerModels() {
            mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS;
            return this;
        }

        /**
         * Set the client's tolerance for stale battery stats. The data may be up to
         * this many milliseconds out-of-date.
+35 −2
Original line number Diff line number Diff line
@@ -33,12 +33,14 @@ class PowerComponents {
    private final double[] mPowerComponentsMah;
    private final long[] mTimeComponentsMs;
    private final int mCustomPowerComponentCount;
    private final byte[] mPowerModels;

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

    PowerComponents(@NonNull Parcel source) {
@@ -46,6 +48,12 @@ class PowerComponents {
        mCustomPowerComponentCount = source.readInt();
        mPowerComponentsMah = source.createDoubleArray();
        mTimeComponentsMs = source.createLongArray();
        if (source.readBoolean()) {
            mPowerModels = new byte[BatteryConsumer.POWER_COMPONENT_COUNT];
            source.readByteArray(mPowerModels);
        } else {
            mPowerModels = null;
        }
    }

    /** Writes contents to Parcel */
@@ -54,6 +62,12 @@ class PowerComponents {
        dest.writeInt(mCustomPowerComponentCount);
        dest.writeDoubleArray(mPowerComponentsMah);
        dest.writeLongArray(mTimeComponentsMs);
        if (mPowerModels != null) {
            dest.writeBoolean(true);
            dest.writeByteArray(mPowerModels);
        } else {
            dest.writeBoolean(false);
        }
    }

    /**
@@ -103,6 +117,15 @@ class PowerComponents {
        }
    }

    @BatteryConsumer.PowerModel
    int getPowerModel(@BatteryConsumer.PowerComponent int component) {
        if (mPowerModels == null) {
            throw new IllegalStateException(
                    "Power model IDs were not requested in the BatteryUsageStatsQuery");
        }
        return mPowerModels[component];
    }

    /**
     * Returns the amount of time used by the specified component, e.g. CPU, WiFi etc.
     *
@@ -148,14 +171,21 @@ class PowerComponents {
        private final double[] mPowerComponentsMah;
        private final int mCustomPowerComponentCount;
        private final long[] mTimeComponentsMs;
        private final byte[] mPowerModels;

        Builder(int customPowerComponentCount, int customTimeComponentCount) {
        Builder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includePowerModels) {
            mCustomPowerComponentCount = customPowerComponentCount;
            int powerComponentCount =
                    BatteryConsumer.POWER_COMPONENT_COUNT + customPowerComponentCount;
            mPowerComponentsMah = new double[powerComponentCount];
            mTimeComponentsMs =
                    new long[BatteryConsumer.TIME_COMPONENT_COUNT + customTimeComponentCount];
            if (includePowerModels) {
                mPowerModels = new byte[BatteryConsumer.POWER_COMPONENT_COUNT];
            } else {
                mPowerModels = null;
            }
        }

        /**
@@ -167,7 +197,7 @@ class PowerComponents {
         */
        @NonNull
        public Builder setConsumedPower(@BatteryConsumer.PowerComponent int componentId,
                double componentPower) {
                double componentPower, @BatteryConsumer.PowerModel int powerModel) {
            if (componentId >= BatteryConsumer.POWER_COMPONENT_COUNT) {
                throw new IllegalArgumentException(
                        "Unsupported power component ID: " + componentId);
@@ -178,6 +208,9 @@ class PowerComponents {
                throw new IllegalArgumentException(
                        "Unsupported power component ID: " + componentId);
            }
            if (mPowerModels != null) {
                mPowerModels[componentId] = (byte) powerModel;
            }
            return this;
        }

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

        Builder(int customPowerComponentCount, int customTimeComponentCount,
                @DrainType int drainType) {
            super(customPowerComponentCount, customTimeComponentCount);
                boolean includePowerModels, @DrainType int drainType) {
            super(customPowerComponentCount, customTimeComponentCount, includePowerModels);
            mDrainType = drainType;
        }

Loading