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

Commit 42b6bb05 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Change the API for getting PowerProfile-only BatteryUsageStats

Bug: 175644968
Test: mp :BatteryStatsViewer && adb shell am start -n com.android.frameworks.core.batterystatsviewer/.BatteryStatsViewerActivity
Change-Id: I66ce64321805423d2545df4691a4df8508f40440
parent f4ee5731
Loading
Loading
Loading
Loading
+2 −14
Original line number Diff line number Diff line
@@ -69,17 +69,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;

    /**
     * Modeled power components are used for testing only.  They are returned if the
     * {@link BatteryUsageStatsQuery#FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED} is set.
     * The modeled power components are retrieved with {@link #getConsumedPowerForCustomComponent}.
     * The ID of a modeled power component is calculated as
     * (FIRST_MODELED_POWER_COMPONENT_ID + powerComponentId), e.g.
     * FIRST_MODELED_POWER_COMPONENT_ID + POWER_COMPONENT_CPU.
     */
    public static final int FIRST_MODELED_POWER_COMPONENT_ID = 10000;
    public static final int LAST_MODELED_POWER_COMPONENT_ID = 19999;

    /**
     * Time usage component, describing the particular part of the system
     * that was used for the corresponding amount of time.
@@ -182,10 +171,9 @@ public abstract class BatteryConsumer {
    protected abstract static class BaseBuilder<T extends BaseBuilder<?>> {
        final PowerComponents.Builder mPowerComponentsBuilder;

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

        /**
+14 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.internal.app.IBatteryStats;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
 * This class provides an API surface for internal system components to report events that are
@@ -183,8 +184,20 @@ public final class BatteryStatsManager {
    @RequiresPermission(android.Manifest.permission.BATTERY_STATS)
    @NonNull
    public BatteryUsageStats getBatteryUsageStats(BatteryUsageStatsQuery query) {
        return getBatteryUsageStats(List.of(query)).get(0);
    }

    /**
     * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem
     * and per-UID basis.
     *
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.BATTERY_STATS)
    @NonNull
    public List<BatteryUsageStats> getBatteryUsageStats(List<BatteryUsageStatsQuery> queries) {
        try {
            return mBatteryStats.getBatteryUsageStats(query);
            return mBatteryStats.getBatteryUsageStats(queries);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+3 −6
Original line number Diff line number Diff line
@@ -114,7 +114,6 @@ public final class BatteryUsageStats implements Parcelable {
    public static final class Builder {
        private final int mCustomPowerComponentCount;
        private final int mCustomTimeComponentCount;
        private final boolean mIncludeModeledComponents;
        private double mConsumedPower;
        private int mDischargePercentage;
        private final SparseArray<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders =
@@ -122,11 +121,9 @@ public final class BatteryUsageStats implements Parcelable {
        private final SparseArray<SystemBatteryConsumer.Builder> mSystemBatteryConsumerBuilders =
                new SparseArray<>();

        public Builder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includeModeledComponents) {
        public Builder(int customPowerComponentCount, int customTimeComponentCount) {
            mCustomPowerComponentCount = customPowerComponentCount;
            mCustomTimeComponentCount = customTimeComponentCount;
            mIncludeModeledComponents = includeModeledComponents;
        }

        /**
@@ -169,7 +166,7 @@ public final class BatteryUsageStats implements Parcelable {
            UidBatteryConsumer.Builder builder = mUidBatteryConsumerBuilders.get(uid);
            if (builder == null) {
                builder = new UidBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, mIncludeModeledComponents, batteryStatsUid);
                        mCustomTimeComponentCount, batteryStatsUid);
                mUidBatteryConsumerBuilders.put(uid, builder);
            }
            return builder;
@@ -185,7 +182,7 @@ public final class BatteryUsageStats implements Parcelable {
            SystemBatteryConsumer.Builder builder = mSystemBatteryConsumerBuilders.get(drainType);
            if (builder == null) {
                builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, mIncludeModeledComponents, drainType);
                        mCustomTimeComponentCount, drainType);
                mSystemBatteryConsumerBuilders.put(drainType, builder);
            }
            return builder;
+10 −7
Original line number Diff line number Diff line
@@ -38,18 +38,19 @@ public final class BatteryUsageStatsQuery implements Parcelable {
     * @hide
     */
    @IntDef(flag = true, prefix = { "FLAG_BATTERY_USAGE_STATS_" }, value = {
            FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED,
            FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BatteryUsageStatsFlags {}

    /**
     * Indicates that modeled battery usage stats should be returned along with
     * measured ones.
     * Indicates that power estimations should be based on the usage time and
     * average power constants provided in the PowerProfile, even if on-device power monitoring
     * is available.
     *
     * @hide
     */
    public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED = 1;
    public static final int FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL = 1;

    private final int mFlags;

@@ -112,11 +113,13 @@ public final class BatteryUsageStatsQuery implements Parcelable {
        }

        /**
         * Requests to include modeled battery usage stats along with measured ones.
         * Requests to return modeled battery usage stats only, even if on-device
         * power monitoring data is available.
         *
         * Should only be used for testing and debugging.
         */
        public Builder includeModeled() {
            mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_MODELED;
        public Builder powerProfileModeledOnly() {
            mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL;
            return this;
        }
    }
+1 −30
Original line number Diff line number Diff line
@@ -33,11 +33,9 @@ class PowerComponents {
    private final double[] mPowerComponents;
    private final long[] mTimeComponents;
    private final int mCustomPowerComponentCount;
    private final int mModeledPowerComponentOffset;

    PowerComponents(@NonNull Builder builder) {
        mCustomPowerComponentCount = builder.mCustomPowerComponentCount;
        mModeledPowerComponentOffset = builder.mModeledPowerComponentOffset;
        mPowerComponents = builder.mPowerComponents;
        mTimeComponents = builder.mTimeComponents;
        double totalPower = 0;
@@ -50,9 +48,6 @@ class PowerComponents {
    PowerComponents(@NonNull Parcel source) {
        mTotalPowerConsumed = source.readDouble();
        mCustomPowerComponentCount = source.readInt();
        mModeledPowerComponentOffset =
                BatteryConsumer.POWER_COMPONENT_COUNT + mCustomPowerComponentCount
                        - BatteryConsumer.FIRST_MODELED_POWER_COMPONENT_ID;
        mPowerComponents = source.createDoubleArray();
        mTimeComponents = source.createLongArray();
    }
@@ -106,14 +101,6 @@ class PowerComponents {
                throw new IllegalArgumentException(
                        "Unsupported custom power component ID: " + componentId);
            }
        } else if (componentId >= BatteryConsumer.FIRST_MODELED_POWER_COMPONENT_ID
                && componentId < BatteryConsumer.LAST_MODELED_POWER_COMPONENT_ID) {
            try {
                return mPowerComponents[mModeledPowerComponentOffset + componentId];
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException(
                        "Unsupported modeled power component ID: " + componentId);
            }
        } else {
            throw new IllegalArgumentException(
                    "Unsupported custom power component ID: " + componentId);
@@ -165,20 +152,12 @@ class PowerComponents {
        private final double[] mPowerComponents;
        private final int mCustomPowerComponentCount;
        private final long[] mTimeComponents;
        private final int mModeledPowerComponentOffset;

        Builder(int customPowerComponentCount, int customTimeComponentCount,
                boolean includeModeledPowerComponents) {
        Builder(int customPowerComponentCount, int customTimeComponentCount) {
            mCustomPowerComponentCount = customPowerComponentCount;
            int powerComponentCount =
                    BatteryConsumer.POWER_COMPONENT_COUNT + customPowerComponentCount;
            if (includeModeledPowerComponents) {
                powerComponentCount += BatteryConsumer.POWER_COMPONENT_COUNT;
            }
            mPowerComponents = new double[powerComponentCount];
            mModeledPowerComponentOffset =
                    BatteryConsumer.POWER_COMPONENT_COUNT + mCustomPowerComponentCount
                            - BatteryConsumer.FIRST_MODELED_POWER_COMPONENT_ID;
            mTimeComponents =
                    new long[BatteryConsumer.TIME_COMPONENT_COUNT + customTimeComponentCount];
        }
@@ -222,14 +201,6 @@ class PowerComponents {
                    throw new IllegalArgumentException(
                            "Unsupported custom power component ID: " + componentId);
                }
            } else if (componentId >= BatteryConsumer.FIRST_MODELED_POWER_COMPONENT_ID
                    && componentId < BatteryConsumer.LAST_MODELED_POWER_COMPONENT_ID) {
                try {
                    mPowerComponents[mModeledPowerComponentOffset + componentId] = componentPower;
                } catch (ArrayIndexOutOfBoundsException e) {
                    throw new IllegalArgumentException(
                            "Unsupported modeled power component ID: " + componentId);
                }
            } else {
                throw new IllegalArgumentException(
                        "Unsupported custom power component ID: " + componentId);
Loading