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

Commit 32a19285 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add SystemBatteryConsumer

Bug: 158137862
Test: mp :BatteryStatsViewer && adb shell am start -n com.android.frameworks.core.batterystatsviewer/.BatteryStatsViewerActivity

Change-Id: I436096fb4c268f4f03542c7334a8f448bf74a738
parent 30f8a371
Loading
Loading
Loading
Loading
+83 −5
Original line number Diff line number Diff line
@@ -42,9 +42,10 @@ public abstract class BatteryConsumer {
    public static @interface PowerComponent {
    }

    public static final int POWER_COMPONENT_CPU = 0;
    public static final int POWER_COMPONENT_USAGE = 0;
    public static final int POWER_COMPONENT_CPU = 1;

    public static final int POWER_COMPONENT_COUNT = 1;
    public static final int POWER_COMPONENT_COUNT = 2;

    public static final int FIRST_CUSTOM_POWER_COMPONENT_ID = 1000;
    public static final int LAST_CUSTOM_POWER_COMPONENT_ID = 9999;
@@ -63,10 +64,11 @@ public abstract class BatteryConsumer {
    public static @interface TimeComponent {
    }

    public static final int TIME_COMPONENT_CPU = 0;
    public static final int TIME_COMPONENT_CPU_FOREGROUND = 1;
    public static final int TIME_COMPONENT_USAGE = 0;
    public static final int TIME_COMPONENT_CPU = 1;
    public static final int TIME_COMPONENT_CPU_FOREGROUND = 2;

    public static final int TIME_COMPONENT_COUNT = 2;
    public static final int TIME_COMPONENT_COUNT = 3;

    public static final int FIRST_CUSTOM_TIME_COMPONENT_ID = 1000;
    public static final int LAST_CUSTOM_TIME_COMPONENT_ID = 9999;
@@ -131,4 +133,80 @@ public abstract class BatteryConsumer {
    protected void writeToParcel(Parcel dest, int flags) {
        mPowerComponents.writeToParcel(dest, flags);
    }

    protected abstract static class BaseBuilder<T extends BaseBuilder<?>> {
        final PowerComponents.Builder mPowerComponentsBuilder;

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

        /**
         * 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) {
            mPowerComponentsBuilder.setConsumedPower(componentId, componentPower);
            return (T) this;
        }

        /**
         * Sets the amount of drain attributed to the specified custom drain type.
         *
         * @param componentId    The ID of the custom power component.
         * @param componentPower Amount of consumed power in mAh.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setConsumedPowerForCustomComponent(int componentId, double componentPower) {
            mPowerComponentsBuilder.setConsumedPowerForCustomComponent(componentId, componentPower);
            return (T) this;
        }

        /**
         * Sets the total amount of power consumed since BatteryStats reset, mAh.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setConsumedPower(double consumedPower) {
            mPowerComponentsBuilder.setTotalPowerConsumed(consumedPower);
            return (T) this;
        }

        /**
         * 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 componentUsageTimeMillis Amount of time in microseconds.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setUsageDurationMillis(@UidBatteryConsumer.TimeComponent int componentId,
                long componentUsageTimeMillis) {
            mPowerComponentsBuilder.setUsageDurationMillis(componentId, componentUsageTimeMillis);
            return (T) this;
        }

        /**
         * Sets the amount of time used by the specified custom component.
         *
         * @param componentId              The ID of the custom power component.
         * @param componentUsageTimeMillis Amount of time in microseconds.
         */
        @SuppressWarnings("unchecked")
        @NonNull
        public T setUsageDurationForCustomComponentMillis(int componentId,
                long componentUsageTimeMillis) {
            mPowerComponentsBuilder.setUsageDurationForCustomComponentMillis(componentId,
                    componentUsageTimeMillis);
            return (T) this;
        }
    }
}
+58 −11
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.os;

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

import java.util.ArrayList;
import java.util.List;
@@ -31,14 +32,20 @@ public final class BatteryUsageStats implements Parcelable {
    private final double mConsumedPower;
    private final int mDischargePercentage;
    private final ArrayList<UidBatteryConsumer> mUidBatteryConsumers;
    private final ArrayList<SystemBatteryConsumer> mSystemBatteryConsumers;

    private BatteryUsageStats(@NonNull Builder builder) {
        mConsumedPower = builder.mConsumedPower;
        mDischargePercentage = builder.mDischargePercentage;
        final int uidBatteryConsumerCount = builder.mUidBatteryConsumerBuilders.size();
        int uidBatteryConsumerCount = builder.mUidBatteryConsumerBuilders.size();
        mUidBatteryConsumers = new ArrayList<>(uidBatteryConsumerCount);
        for (int i = 0; i < uidBatteryConsumerCount; i++) {
            mUidBatteryConsumers.add(builder.mUidBatteryConsumerBuilders.get(i).build());
            mUidBatteryConsumers.add(builder.mUidBatteryConsumerBuilders.valueAt(i).build());
        }
        int systemBatteryConsumerCount = builder.mSystemBatteryConsumerBuilders.size();
        mSystemBatteryConsumers = new ArrayList<>(systemBatteryConsumerCount);
        for (int i = 0; i < systemBatteryConsumerCount; i++) {
            mSystemBatteryConsumers.add(builder.mSystemBatteryConsumerBuilders.valueAt(i).build());
        }
    }

@@ -63,6 +70,11 @@ public final class BatteryUsageStats implements Parcelable {
        return mUidBatteryConsumers;
    }

    @NonNull
    public List<SystemBatteryConsumer> getSystemBatteryConsumers() {
        return mSystemBatteryConsumers;
    }

    @Override
    public int describeContents() {
        return 0;
@@ -71,6 +83,8 @@ public final class BatteryUsageStats implements Parcelable {
    private BatteryUsageStats(@NonNull Parcel source) {
        mUidBatteryConsumers = new ArrayList<>();
        source.readParcelableList(mUidBatteryConsumers, getClass().getClassLoader());
        mSystemBatteryConsumers = new ArrayList<>();
        source.readParcelableList(mSystemBatteryConsumers, getClass().getClassLoader());
        mConsumedPower = source.readDouble();
        mDischargePercentage = source.readInt();
    }
@@ -78,6 +92,7 @@ public final class BatteryUsageStats implements Parcelable {
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeParcelableList(mUidBatteryConsumers, flags);
        dest.writeParcelableList(mSystemBatteryConsumers, flags);
        dest.writeDouble(mConsumedPower);
        dest.writeInt(mDischargePercentage);
    }
@@ -97,10 +112,19 @@ public final class BatteryUsageStats implements Parcelable {
     * Builder for BatteryUsageStats.
     */
    public static final class Builder {
        private final int mCustomPowerComponentCount;
        private final int mCustomTimeComponentCount;
        private double mConsumedPower;
        private int mDischargePercentage;
        private final ArrayList<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders =
                new ArrayList<>();
        private final SparseArray<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders =
                new SparseArray<>();
        private final SparseArray<SystemBatteryConsumer.Builder> mSystemBatteryConsumerBuilders =
                new SparseArray<>();

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

        /**
         * Constructs a read-only object using the Builder values.
@@ -113,6 +137,7 @@ public final class BatteryUsageStats implements Parcelable {
        /**
         * Sets the battery discharge amount since BatteryStats reset as percentage of the full
         * charge.
         *
         */
        @SuppressLint("PercentageInt") // See b/174188159
        @NonNull
@@ -131,18 +156,40 @@ public final class BatteryUsageStats implements Parcelable {
        }

        /**
         * Adds a UidBatteryConsumer, which represents battery attribution data for an
         * individual UID.
         * Creates or returns a exiting UidBatteryConsumer, which represents battery attribution
         * data for an individual UID.
         */
        @NonNull
        public Builder addUidBatteryConsumerBuilder(
                @NonNull UidBatteryConsumer.Builder uidBatteryConsumer) {
            mUidBatteryConsumerBuilders.add(uidBatteryConsumer);
            return this;
        public UidBatteryConsumer.Builder getOrCreateUidBatteryConsumerBuilder(
                @NonNull BatteryStats.Uid batteryStatsUid) {
            int uid = batteryStatsUid.getUid();
            UidBatteryConsumer.Builder builder = mUidBatteryConsumerBuilders.get(uid);
            if (builder == null) {
                builder = new UidBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, batteryStatsUid);
                mUidBatteryConsumerBuilders.put(uid, builder);
            }
            return builder;
        }

        /**
         * Creates or returns a exiting UidBatteryConsumer, which represents battery attribution
         * data for an individual UID.
         */
        @NonNull
        public SystemBatteryConsumer.Builder getOrCreateSystemBatteryConsumerBuilder(
                @SystemBatteryConsumer.DrainType int drainType) {
            SystemBatteryConsumer.Builder builder = mSystemBatteryConsumerBuilders.get(drainType);
            if (builder == null) {
                builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentCount,
                        mCustomTimeComponentCount, drainType);
                mSystemBatteryConsumerBuilders.put(drainType, builder);
            }
            return builder;
        }

        @NonNull
        public List<UidBatteryConsumer.Builder> getUidBatteryConsumerBuilders() {
        public SparseArray<UidBatteryConsumer.Builder> getUidBatteryConsumerBuilders() {
            return mUidBatteryConsumerBuilders;
        }
    }
+136 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

import android.annotation.IntDef;
import android.annotation.NonNull;

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


/**
 * Contains power consumption data attributed to a system-wide drain type.
 *
 * {@hide}
 */
public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable {

    //                           ****************
    // This list must be kept current with atoms.proto (frameworks/base/cmds/statsd/src/atoms.proto)
    // so the constant values must never change.
    //                           ****************
    @IntDef(prefix = {"DRAIN_TYPE_"}, value = {
            DRAIN_TYPE_AMBIENT_DISPLAY,
            // Reserved: APP
            DRAIN_TYPE_BLUETOOTH,
            DRAIN_TYPE_CAMERA,
            DRAIN_TYPE_CELL,
            DRAIN_TYPE_FLASHLIGHT,
            DRAIN_TYPE_IDLE,
            DRAIN_TYPE_MEMORY,
            DRAIN_TYPE_OVERCOUNTED,
            DRAIN_TYPE_PHONE,
            DRAIN_TYPE_SCREEN,
            DRAIN_TYPE_UNACCOUNTED,
            // Reserved: USER,
            DRAIN_TYPE_WIFI,
    })
    @Retention(RetentionPolicy.SOURCE)
    public static @interface DrainType {
    }

    public static final int DRAIN_TYPE_AMBIENT_DISPLAY = 0;
    public static final int DRAIN_TYPE_BLUETOOTH = 2;
    public static final int DRAIN_TYPE_CAMERA = 3;
    public static final int DRAIN_TYPE_CELL = 4;
    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
    private final int mDrainType;

    @DrainType
    public int getDrainType() {
        return mDrainType;
    }

    private SystemBatteryConsumer(@NonNull SystemBatteryConsumer.Builder builder) {
        super(builder.mPowerComponentsBuilder.build());
        mDrainType = builder.mDrainType;
    }

    private SystemBatteryConsumer(Parcel in) {
        super(new PowerComponents(in));
        mDrainType = in.readInt();
    }

    /**
     * Writes the contents into a Parcel.
     */
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeInt(mDrainType);
    }

    public static final Creator<SystemBatteryConsumer> CREATOR =
            new Creator<SystemBatteryConsumer>() {
                @Override
                public SystemBatteryConsumer createFromParcel(Parcel in) {
                    return new SystemBatteryConsumer(in);
                }

                @Override
                public SystemBatteryConsumer[] newArray(int size) {
                    return new SystemBatteryConsumer[size];
                }
            };

    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * Builder for SystemBatteryConsumer.
     */
    public static final class Builder extends BaseBuilder<Builder> {
        @DrainType
        private final int mDrainType;

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

        /**
         * Creates a read-only object out of the Builder values.
         */
        @NonNull
        public SystemBatteryConsumer build() {
            return new SystemBatteryConsumer(this);
        }
    }
}
+2 −67
Original line number Diff line number Diff line
@@ -80,16 +80,14 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela
    /**
     * Builder for UidBatteryConsumer.
     */
    public static final class Builder {
        private final PowerComponents.Builder mPowerComponentsBuilder;
    public static final class Builder extends BaseBuilder<Builder> {
        private final BatteryStats.Uid mBatteryStatsUid;
        private final int mUid;
        private String mPackageWithHighestDrain;

        public Builder(int customPowerComponentCount, int customTimeComponentCount,
                BatteryStats.Uid batteryStatsUid) {
            mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentCount,
                    customTimeComponentCount);
            super(customPowerComponentCount, customTimeComponentCount);
            mBatteryStatsUid = batteryStatsUid;
            mUid = batteryStatsUid.getUid();
        }
@@ -110,69 +108,6 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela
            return new UidBatteryConsumer(this);
        }

        /**
         * 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.
         */
        @NonNull
        public Builder setConsumedPower(@PowerComponent int componentId, double componentPower) {
            mPowerComponentsBuilder.setConsumedPower(componentId, componentPower);
            return this;
        }

        /**
         * Sets the amount of drain attributed to the specified custom drain type.
         *
         * @param componentId    The ID of the custom power component.
         * @param componentPower Amount of consumed power in mAh.
         */
        @NonNull
        public Builder setConsumedPowerForCustomComponent(int componentId, double componentPower) {
            mPowerComponentsBuilder.setConsumedPowerForCustomComponent(componentId, componentPower);
            return this;
        }

        /**
         * Sets the amount of power consumed since BatteryStats reset, mAh.
         */
        @NonNull
        public Builder setConsumedPower(double consumedPower) {
            mPowerComponentsBuilder.setTotalPowerConsumed(consumedPower);
            return this;
        }

        /**
         * 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 componentUsageDurationMillis Amount of time in milliseconds.
         */
        @NonNull
        public Builder setUsageDurationMillis(@UidBatteryConsumer.TimeComponent int componentId,
                long componentUsageDurationMillis) {
            mPowerComponentsBuilder.setUsageDurationMillis(componentId,
                    componentUsageDurationMillis);
            return this;
        }

        /**
         * Sets the amount of time used by the specified custom component.
         *
         * @param componentId                  The ID of the custom power component.
         * @param componentUsageDurationMillis Amount of time in milliseconds.
         */
        @NonNull
        public Builder setUsageDurationForCustomComponentMillis(int componentId,
                long componentUsageDurationMillis) {
            mPowerComponentsBuilder.setUsageDurationForCustomComponentMillis(componentId,
                    componentUsageDurationMillis);
            return this;
        }

        /**
         * Sets the name of the package owned by this UID that consumed the highest amount
         * of power since BatteryStats reset.
+7 −9
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import android.os.BatteryStats;
import android.os.BatteryUsageStats;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.UidBatteryConsumer;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.SparseArray;
@@ -110,7 +109,8 @@ public class BatteryUsageStatsProvider {
        // TODO(b/174186358): read extra power component number from configuration
        final int customPowerComponentCount = 0;
        final int customTimeComponentCount = 0;
        final BatteryUsageStats.Builder batteryUsageStatsBuilder = new BatteryUsageStats.Builder()
        final BatteryUsageStats.Builder batteryUsageStatsBuilder =
                new BatteryUsageStats.Builder(customPowerComponentCount, customTimeComponentCount)
                        .setDischargePercentage(batteryStatsHelper.getStats().getDischargeAmount(0))
                        .setConsumedPower(batteryStatsHelper.getTotalPower());

@@ -118,11 +118,9 @@ public class BatteryUsageStatsProvider {
        for (int i = 0; i < usageList.size(); i++) {
            final BatterySipper sipper = usageList.get(i);
            if (sipper.drainType == BatterySipper.DrainType.APP) {
                batteryUsageStatsBuilder.addUidBatteryConsumerBuilder(
                        new UidBatteryConsumer.Builder(customPowerComponentCount,
                                customTimeComponentCount, sipper.uidObj)
                batteryUsageStatsBuilder.getOrCreateUidBatteryConsumerBuilder(sipper.uidObj)
                        .setPackageWithHighestDrain(sipper.packageWithHighestDrain)
                                .setConsumedPower(sipper.sumPower()));
                        .setConsumedPower(sipper.sumPower());
            }
        }

Loading