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

Commit fac8075d authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Export BatteryUsageStats into prepopulated buider

Instead of overriding what has already been captured in the builder,
always add new estimates. This way we can accumulate BatteryUsageStats
without allocating new CursorWindows.

Bug: 366493365
Test: atest PowerStatsTests
Flag: EXEMPT bugfix
Change-Id: Ica681e20c1091c2336532e58583ca5d3f55db85f
parent 2350f6b2
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ public final class AggregateBatteryConsumer extends BatteryConsumer {
            throw new XmlPullParserException("Invalid XML parser state");
        }

        consumerBuilder.setConsumedPower(
        consumerBuilder.addConsumedPower(
                parser.getAttributeDouble(null, BatteryUsageStats.XML_ATTR_POWER));

        while (!(eventType == XmlPullParser.END_TAG && parser.getName().equals(
@@ -131,12 +131,20 @@ public final class AggregateBatteryConsumer extends BatteryConsumer {
            return this;
        }

        /**
         * Adds the total power included in this aggregate.
         */
        public Builder addConsumedPower(double consumedPowerMah) {
            mData.putDouble(COLUMN_INDEX_CONSUMED_POWER,
                    mData.getDouble(COLUMN_INDEX_CONSUMED_POWER) + consumedPowerMah);
            return this;
        }

        /**
         * Adds power and usage duration from the supplied AggregateBatteryConsumer.
         */
        public void add(AggregateBatteryConsumer aggregateBatteryConsumer) {
            setConsumedPower(mData.getDouble(COLUMN_INDEX_CONSUMED_POWER)
                    + aggregateBatteryConsumer.getConsumedPower());
            addConsumedPower(aggregateBatteryConsumer.getConsumedPower());
            mPowerComponentsBuilder.addPowerAndDuration(aggregateBatteryConsumer.mPowerComponents);
        }

+30 −0
Original line number Diff line number Diff line
@@ -1064,7 +1064,9 @@ public abstract class BatteryConsumer {
         * @param componentId    The ID of the power component, e.g.
         *                       {@link BatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentPower Amount of consumed power in mAh.
         * @deprecated use {@link #addConsumedPower}
         */
        @Deprecated
        @NonNull
        public T setConsumedPower(@PowerComponentId int componentId, double componentPower) {
            return setConsumedPower(componentId, componentPower, POWER_MODEL_POWER_PROFILE);
@@ -1076,7 +1078,9 @@ public abstract class BatteryConsumer {
         * @param componentId    The ID of the power component, e.g.
         *                       {@link BatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentPower Amount of consumed power in mAh.
         * @deprecated use {@link #addConsumedPower}
         */
        @Deprecated
        @SuppressWarnings("unchecked")
        @NonNull
        public T setConsumedPower(@PowerComponentId int componentId, double componentPower,
@@ -1102,6 +1106,21 @@ public abstract class BatteryConsumer {
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T addConsumedPower(@PowerComponentId int componentId, double componentPower) {
            mPowerComponentsBuilder.addConsumedPower(getKey(componentId, PROCESS_STATE_UNSPECIFIED),
                    componentPower, POWER_MODEL_UNDEFINED);
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T addConsumedPower(Key key, double componentPower) {
            mPowerComponentsBuilder.addConsumedPower(key, componentPower, POWER_MODEL_UNDEFINED);
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T addConsumedPower(Key key, double componentPower, @PowerModel int powerModel) {
@@ -1115,7 +1134,9 @@ public abstract class BatteryConsumer {
         * @param componentId              The ID of the power component, e.g.
         *                                 {@link UidBatteryConsumer#POWER_COMPONENT_CPU}.
         * @param componentUsageTimeMillis Amount of time in microseconds.
         * @deprecated use {@link #addUsageDurationMillis}
         */
        @Deprecated
        @SuppressWarnings("unchecked")
        @NonNull
        public T setUsageDurationMillis(@PowerComponentId int componentId,
@@ -1126,6 +1147,7 @@ public abstract class BatteryConsumer {
            return (T) this;
        }

        @Deprecated
        @SuppressWarnings("unchecked")
        @NonNull
        public T setUsageDurationMillis(Key key, long componentUsageTimeMillis) {
@@ -1133,6 +1155,14 @@ public abstract class BatteryConsumer {
            return (T) this;
        }

        @NonNull
        public T addUsageDurationMillis(@PowerComponentId int componentId,
                long componentUsageTimeMillis) {
            mPowerComponentsBuilder.addUsageDurationMillis(
                    getKey(componentId, PROCESS_STATE_UNSPECIFIED), componentUsageTimeMillis);
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        @NonNull
        public T addUsageDurationMillis(Key key, long componentUsageTimeMillis) {
+1 −1
Original line number Diff line number Diff line
@@ -209,7 +209,7 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
        }

        builder.getAggregateBatteryConsumerBuilder(AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS)
                .setConsumedPower(totalPowerMah);
                .addConsumedPower(totalPowerMah);

        mAggregateBatteryConsumers =
                new AggregateBatteryConsumer[AGGREGATE_BATTERY_CONSUMER_SCOPE_COUNT];
+10 −2
Original line number Diff line number Diff line
@@ -439,8 +439,8 @@ class PowerComponents {
                        }
                        final BatteryConsumer.Key key = builder.mData.layout.getKey(componentId,
                                processState, screenState, powerState);
                        builder.setConsumedPower(key, powerMah, model);
                        builder.setUsageDurationMillis(key, durationMs);
                        builder.addConsumedPower(key, powerMah, model);
                        builder.addUsageDurationMillis(key, durationMs);
                        break;
                    }
                }
@@ -468,6 +468,10 @@ class PowerComponents {
            }
        }

        /**
         * @deprecated use {@link #addConsumedPower(BatteryConsumer.Key, double, int)}
         */
        @Deprecated
        @NonNull
        public Builder setConsumedPower(BatteryConsumer.Key key, double componentPower,
                int powerModel) {
@@ -489,6 +493,10 @@ class PowerComponents {
            return this;
        }

        /**
         * @deprecated use {@link #addUsageDurationMillis(BatteryConsumer.Key, long)}
         */
        @Deprecated
        @NonNull
        public Builder setUsageDurationMillis(BatteryConsumer.Key key,
                long componentUsageDurationMillis) {
+4 −13
Original line number Diff line number Diff line
@@ -210,12 +210,6 @@ public final class UidBatteryConsumer extends BatteryConsumer {
            serializer.attribute(null, BatteryUsageStats.XML_ATTR_HIGHEST_DRAIN_PACKAGE,
                    packageWithHighestDrain);
        }
        serializer.attributeLong(null, BatteryUsageStats.XML_ATTR_TIME_IN_FOREGROUND,
                getTimeInProcessStateMs(PROCESS_STATE_FOREGROUND));
        serializer.attributeLong(null, BatteryUsageStats.XML_ATTR_TIME_IN_BACKGROUND,
                getTimeInProcessStateMs(PROCESS_STATE_BACKGROUND));
        serializer.attributeLong(null, BatteryUsageStats.XML_ATTR_TIME_IN_FOREGROUND_SERVICE,
                getTimeInProcessStateMs(PROCESS_STATE_FOREGROUND_SERVICE));
        mPowerComponents.writeToXml(serializer);
        serializer.endTag(null, BatteryUsageStats.XML_TAG_UID);
    }
@@ -235,13 +229,6 @@ public final class UidBatteryConsumer extends BatteryConsumer {

        consumerBuilder.setPackageWithHighestDrain(
                parser.getAttributeValue(null, BatteryUsageStats.XML_ATTR_HIGHEST_DRAIN_PACKAGE));
        consumerBuilder.setTimeInProcessStateMs(PROCESS_STATE_FOREGROUND,
                parser.getAttributeLong(null, BatteryUsageStats.XML_ATTR_TIME_IN_FOREGROUND));
        consumerBuilder.setTimeInProcessStateMs(PROCESS_STATE_BACKGROUND,
                parser.getAttributeLong(null, BatteryUsageStats.XML_ATTR_TIME_IN_BACKGROUND));
        consumerBuilder.setTimeInProcessStateMs(PROCESS_STATE_FOREGROUND_SERVICE,
                parser.getAttributeLong(null,
                        BatteryUsageStats.XML_ATTR_TIME_IN_FOREGROUND_SERVICE));
        while (!(eventType == XmlPullParser.END_TAG
                && parser.getName().equals(BatteryUsageStats.XML_TAG_UID))
                && eventType != XmlPullParser.END_DOCUMENT) {
@@ -335,7 +322,11 @@ public final class UidBatteryConsumer extends BatteryConsumer {
        /**
         * Sets the duration, in milliseconds, that this UID was active in a particular process
         * state, such as foreground service.
         *
         * @deprecated time in process is now derived from the
         * {@link BatteryConsumer#POWER_COMPONENT_BASE} duration
         */
        @Deprecated
        @NonNull
        public Builder setTimeInProcessStateMs(@ProcessState int state, long timeInProcessStateMs) {
            Key key = getKey(POWER_COMPONENT_BASE, state);
Loading