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

Commit a919306d authored by mxyyiyi's avatar mxyyiyi
Browse files

Provide foreground_service usage time in BatteryDiffEntry.

- Fetch foreground service info through new api from UidBatteryConsumer.
- Record fgs & bg usage time separately, combine them till ui display.

Bug: 315255868
Test: make RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.fuelgauge.*"
Change-Id: Ic19844db7908a6ae6522c7a72972f44f94dcfca4
parent dd2cc366
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -148,7 +148,8 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
        launchArgs.mShowTimeInformation = showTimeInformation;
        if (launchArgs.mShowTimeInformation) {
            launchArgs.mForegroundTimeMs = diffEntry.mForegroundUsageTimeInMs;
            launchArgs.mBackgroundTimeMs = diffEntry.mBackgroundUsageTimeInMs;
            launchArgs.mBackgroundTimeMs =
                    diffEntry.mBackgroundUsageTimeInMs + diffEntry.mForegroundServiceUsageTimeInMs;
            launchArgs.mScreenOnTimeMs = diffEntry.mScreenOnTimeInMs;
            launchArgs.mAnomalyHintPrefKey = anomalyHintPrefKey;
            launchArgs.mAnomalyHintText = anomalyHintText;
+49 −42
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ public class BatteryDiffEntry {
    public String mLegacyLabel;
    public int mConsumerType;
    public long mForegroundUsageTimeInMs;
    public long mForegroundServiceUsageTimeInMs;
    public long mBackgroundUsageTimeInMs;
    public long mScreenOnTimeInMs;
    public double mConsumePower;
@@ -125,6 +126,7 @@ public class BatteryDiffEntry {
            String legacyLabel,
            int consumerType,
            long foregroundUsageTimeInMs,
            long foregroundServiceUsageTimeInMs,
            long backgroundUsageTimeInMs,
            long screenOnTimeInMs,
            double consumePower,
@@ -142,6 +144,7 @@ public class BatteryDiffEntry {
        mLegacyLabel = legacyLabel;
        mConsumerType = consumerType;
        mForegroundUsageTimeInMs = foregroundUsageTimeInMs;
        mForegroundServiceUsageTimeInMs = foregroundServiceUsageTimeInMs;
        mBackgroundUsageTimeInMs = backgroundUsageTimeInMs;
        mScreenOnTimeInMs = screenOnTimeInMs;
        mConsumePower = consumePower;
@@ -164,6 +167,7 @@ public class BatteryDiffEntry {
                legacyLabel,
                consumerType,
                /* foregroundUsageTimeInMs= */ 0,
                /* foregroundServiceUsageTimeInMs= */ 0,
                /* backgroundUsageTimeInMs= */ 0,
                /* screenOnTimeInMs= */ 0,
                /* consumePower= */ 0,
@@ -232,6 +236,7 @@ public class BatteryDiffEntry {
                this.mLegacyLabel,
                this.mConsumerType,
                this.mForegroundUsageTimeInMs,
                this.mForegroundServiceUsageTimeInMs,
                this.mBackgroundUsageTimeInMs,
                this.mScreenOnTimeInMs,
                this.mConsumePower,
@@ -515,34 +520,36 @@ public class BatteryDiffEntry {

    @Override
    public String toString() {
        final StringBuilder builder =
                new StringBuilder()
                        .append("BatteryDiffEntry{")
                        .append(
                                String.format(
                                        "\n\tname=%s restrictable=%b",
                                        mAppLabel, mValidForRestriction))
                        .append(
        final StringBuilder builder = new StringBuilder();
        builder.append("BatteryDiffEntry{");
        builder.append(
                String.format("\n\tname=%s restrictable=%b", mAppLabel, mValidForRestriction));
        builder.append(
                String.format(
                        "\n\tconsume=%.2f%% %f/%f",
                                        mPercentage, mConsumePower, mTotalConsumePower))
                        .append(
                        mPercentage, mConsumePower, mTotalConsumePower));
        builder.append(
                String.format(
                        "\n\tconsume power= foreground:%f foregroundService:%f",
                                        mForegroundUsageConsumePower,
                                        mForegroundServiceUsageConsumePower))
                        .append(
                        mForegroundUsageConsumePower, mForegroundServiceUsageConsumePower));
        builder.append(
                String.format(
                        "\n\tconsume power= background:%f cached:%f",
                                        mBackgroundUsageConsumePower, mCachedUsageConsumePower))
                        .append(
                        mBackgroundUsageConsumePower, mCachedUsageConsumePower));
        builder.append(
                String.format(
                                        "\n\ttime= foreground:%s background:%s screen-on:%s",
                        "\n\ttime= foreground:%s foregroundService:%s "
                                + "background:%s screen-on:%s",
                        StringUtil.formatElapsedTime(
                                mContext,
                                (double) mForegroundUsageTimeInMs,
                                /* withSeconds= */ true,
                                /* collapseTimeUnit= */ false),
                        StringUtil.formatElapsedTime(
                                mContext,
                                (double) mForegroundServiceUsageTimeInMs,
                                /* withSeconds= */ true,
                                /* collapseTimeUnit= */ false),
                        StringUtil.formatElapsedTime(
                                mContext,
                                (double) mBackgroundUsageTimeInMs,
@@ -552,8 +559,8 @@ public class BatteryDiffEntry {
                                mContext,
                                (double) mScreenOnTimeInMs,
                                /* withSeconds= */ true,
                                                /* collapseTimeUnit= */ false)))
                        .append(
                                /* collapseTimeUnit= */ false)));
        builder.append(
                String.format(
                        "\n\tpackage:%s|%s uid:%d userId:%d",
                        mLegacyPackageName, getPackageName(), mUid, mUserId));
+24 −14
Original line number Diff line number Diff line
@@ -111,6 +111,7 @@ public class BatteryEntry {
    @BatteryConsumer.PowerComponent private final int mPowerComponentId;
    private long mUsageDurationMs;
    private long mTimeInForegroundMs;
    private long mTimeInForegroundServiceMs;
    private long mTimeInBackgroundMs;

    public String mName;
@@ -188,9 +189,14 @@ public class BatteryEntry {
                }
            }
            mTimeInForegroundMs =
                    uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_FOREGROUND);
                    uidBatteryConsumer.getTimeInProcessStateMs(
                            UidBatteryConsumer.PROCESS_STATE_FOREGROUND);
            mTimeInForegroundServiceMs =
                    uidBatteryConsumer.getTimeInProcessStateMs(
                            UidBatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
            mTimeInBackgroundMs =
                    uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_BACKGROUND);
                    uidBatteryConsumer.getTimeInProcessStateMs(
                            UidBatteryConsumer.PROCESS_STATE_BACKGROUND);
            mConsumedPowerInForeground =
                    safeGetConsumedPower(
                            uidBatteryConsumer, BATTERY_DIMENSIONS[BATTERY_USAGE_INDEX_FOREGROUND]);
@@ -433,20 +439,19 @@ public class BatteryEntry {

    /** Returns foreground time/ms that is attributed to this entry. */
    public long getTimeInForegroundMs() {
        if (mBatteryConsumer instanceof UidBatteryConsumer) {
            return mTimeInForegroundMs;
        } else {
            return mUsageDurationMs;
        return (mBatteryConsumer instanceof UidBatteryConsumer)
                ? mTimeInForegroundMs
                : mUsageDurationMs;
    }

    /** Returns foreground service time/ms that is attributed to this entry. */
    public long getTimeInForegroundServiceMs() {
        return (mBatteryConsumer instanceof UidBatteryConsumer) ? mTimeInForegroundServiceMs : 0;
    }

    /** Returns background activity time/ms that is attributed to this entry. */
    public long getTimeInBackgroundMs() {
        if (mBatteryConsumer instanceof UidBatteryConsumer) {
            return mTimeInBackgroundMs;
        } else {
            return 0;
        }
        return (mBatteryConsumer instanceof UidBatteryConsumer) ? mTimeInBackgroundMs : 0;
    }

    /** Returns total amount of power (in milli-amp-hours) that is attributed to this entry. */
@@ -510,9 +515,14 @@ public class BatteryEntry {
        if (batteryConsumer instanceof UidBatteryConsumer) {
            UidBatteryConsumer uidBatteryConsumer = (UidBatteryConsumer) batteryConsumer;
            mTimeInForegroundMs +=
                    uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_FOREGROUND);
                    uidBatteryConsumer.getTimeInProcessStateMs(
                            UidBatteryConsumer.PROCESS_STATE_FOREGROUND);
            mTimeInForegroundServiceMs +=
                    uidBatteryConsumer.getTimeInProcessStateMs(
                            UidBatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
            mTimeInBackgroundMs +=
                    uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_BACKGROUND);
                    uidBatteryConsumer.getTimeInProcessStateMs(
                            UidBatteryConsumer.PROCESS_STATE_BACKGROUND);
            mConsumedPowerInForeground +=
                    safeGetConsumedPower(
                            uidBatteryConsumer, BATTERY_DIMENSIONS[BATTERY_USAGE_INDEX_FOREGROUND]);
+52 −51
Original line number Diff line number Diff line
@@ -20,8 +20,6 @@ import android.database.Cursor;
import android.os.BatteryConsumer;
import android.util.Log;

import java.time.Duration;

/** A container class to carry data from {@link ContentValues}. */
public class BatteryHistEntry {
    private static final boolean DEBUG = false;
@@ -57,6 +55,7 @@ public class BatteryHistEntry {
    public final double mCachedUsageConsumePower;
    public final double mPercentOfTotal;
    public final long mForegroundUsageTimeInMs;
    public final long mForegroundServiceUsageTimeInMs;
    public final long mBackgroundUsageTimeInMs;
    @BatteryConsumer.PowerComponent public final int mDrainType;
    @ConvertUtils.ConsumerType public final int mConsumerType;
@@ -89,6 +88,7 @@ public class BatteryHistEntry {
        mCachedUsageConsumePower = batteryInformation.getCachedUsageConsumePower();
        mPercentOfTotal = batteryInformation.getPercentOfTotal();
        mForegroundUsageTimeInMs = batteryInformation.getForegroundUsageTimeInMs();
        mForegroundServiceUsageTimeInMs = batteryInformation.getForegroundServiceUsageTimeInMs();
        mBackgroundUsageTimeInMs = batteryInformation.getBackgroundUsageTimeInMs();
        mDrainType = batteryInformation.getDrainType();
        final DeviceBatteryState deviceBatteryState = batteryInformation.getDeviceBatteryState();
@@ -118,6 +118,7 @@ public class BatteryHistEntry {
        mCachedUsageConsumePower = batteryInformation.getCachedUsageConsumePower();
        mPercentOfTotal = batteryInformation.getPercentOfTotal();
        mForegroundUsageTimeInMs = batteryInformation.getForegroundUsageTimeInMs();
        mForegroundServiceUsageTimeInMs = batteryInformation.getForegroundServiceUsageTimeInMs();
        mBackgroundUsageTimeInMs = batteryInformation.getBackgroundUsageTimeInMs();
        mDrainType = batteryInformation.getDrainType();
        final DeviceBatteryState deviceBatteryState = batteryInformation.getDeviceBatteryState();
@@ -137,6 +138,7 @@ public class BatteryHistEntry {
            double backgroundUsageConsumePower,
            double cachedUsageConsumePower,
            long foregroundUsageTimeInMs,
            long foregroundServiceUsageTimeInMs,
            long backgroundUsageTimeInMs,
            int batteryLevel) {
        mUid = fromEntry.mUid;
@@ -155,6 +157,7 @@ public class BatteryHistEntry {
        mCachedUsageConsumePower = cachedUsageConsumePower;
        mPercentOfTotal = fromEntry.mPercentOfTotal;
        mForegroundUsageTimeInMs = foregroundUsageTimeInMs;
        mForegroundServiceUsageTimeInMs = foregroundServiceUsageTimeInMs;
        mBackgroundUsageTimeInMs = backgroundUsageTimeInMs;
        mDrainType = fromEntry.mDrainType;
        mConsumerType = fromEntry.mConsumerType;
@@ -189,42 +192,37 @@ public class BatteryHistEntry {
    @Override
    public String toString() {
        final String recordAtDateTime = ConvertUtils.utcToLocalTimeForLogging(mTimestamp);
        final StringBuilder builder =
                new StringBuilder()
                        .append("\nBatteryHistEntry{")
                        .append(
        final StringBuilder builder = new StringBuilder();
        builder.append("\nBatteryHistEntry{");
        builder.append(
                String.format(
                        "\n\tpackage=%s|label=%s|uid=%d|userId=%d|isHidden=%b",
                                        mPackageName, mAppLabel, mUid, mUserId, mIsHidden))
                        .append(
                        mPackageName, mAppLabel, mUid, mUserId, mIsHidden));
        builder.append(
                String.format(
                        "\n\ttimestamp=%s|zoneId=%s|bootTimestamp=%d",
                                        recordAtDateTime,
                                        mZoneId,
                                        Duration.ofMillis(mBootTimestamp).getSeconds()))
                        .append(
                        recordAtDateTime, mZoneId, TimestampUtils.getSeconds(mBootTimestamp)));
        builder.append(
                String.format(
                        "\n\tusage=%f|total=%f|consume=%f",
                                        mPercentOfTotal, mTotalPower, mConsumePower))
                        .append(
                        mPercentOfTotal, mTotalPower, mConsumePower));
        builder.append(
                String.format(
                        "\n\tforeground=%f|foregroundService=%f",
                                        mForegroundUsageConsumePower,
                                        mForegroundServiceUsageConsumePower))
                        .append(
                        mForegroundUsageConsumePower, mForegroundServiceUsageConsumePower));
        builder.append(
                String.format(
                        "\n\tbackground=%f|cached=%f",
                                        mBackgroundUsageConsumePower, mCachedUsageConsumePower))
                        .append(
                                String.format(
                                        "\n\telapsedTime=%d|%d",
                                        Duration.ofMillis(mForegroundUsageTimeInMs).getSeconds(),
                                        Duration.ofMillis(mBackgroundUsageTimeInMs).getSeconds()))
                        .append(
                        mBackgroundUsageConsumePower, mCachedUsageConsumePower));
        builder.append(
                String.format(
                                        "\n\tdrainType=%d|consumerType=%d",
                                        mDrainType, mConsumerType))
                        .append(
                        "\n\telapsedTime,fg=%d|fgs=%d|bg=%d",
                        TimestampUtils.getSeconds(mBackgroundUsageTimeInMs),
                        TimestampUtils.getSeconds(mForegroundServiceUsageTimeInMs),
                        TimestampUtils.getSeconds(mBackgroundUsageTimeInMs)));
        builder.append(
                String.format("\n\tdrainType=%d|consumerType=%d", mDrainType, mConsumerType));
        builder.append(
                String.format(
                        "\n\tbattery=%d|status=%d|health=%d\n}",
                        mBatteryLevel, mBatteryStatus, mBatteryHealth));
@@ -323,19 +321,20 @@ public class BatteryHistEntry {
                        ratio);
        final double foregroundUsageTimeInMs =
                interpolate(
                        (double)
                        (lowerHistEntry == null ? 0 : lowerHistEntry.mForegroundUsageTimeInMs),
                        upperHistEntry.mForegroundUsageTimeInMs,
                        ratio);
        final double foregroundServiceUsageTimeInMs =
                interpolate(
                        (lowerHistEntry == null
                                ? 0
                                        : lowerHistEntry.mForegroundUsageTimeInMs),
                        (double) upperHistEntry.mForegroundUsageTimeInMs,
                                : lowerHistEntry.mForegroundServiceUsageTimeInMs),
                        upperHistEntry.mForegroundServiceUsageTimeInMs,
                        ratio);
        final double backgroundUsageTimeInMs =
                interpolate(
                        (double)
                                (lowerHistEntry == null
                                        ? 0
                                        : lowerHistEntry.mBackgroundUsageTimeInMs),
                        (double) upperHistEntry.mBackgroundUsageTimeInMs,
                        (lowerHistEntry == null ? 0 : lowerHistEntry.mBackgroundUsageTimeInMs),
                        upperHistEntry.mBackgroundUsageTimeInMs,
                        ratio);
        // Checks whether there is any abnormal cases!
        if (upperHistEntry.mConsumePower < consumePower
@@ -345,6 +344,7 @@ public class BatteryHistEntry {
                || upperHistEntry.mBackgroundUsageConsumePower < backgroundUsageConsumePower
                || upperHistEntry.mCachedUsageConsumePower < cachedUsageConsumePower
                || upperHistEntry.mForegroundUsageTimeInMs < foregroundUsageTimeInMs
                || upperHistEntry.mForegroundServiceUsageTimeInMs < foregroundServiceUsageTimeInMs
                || upperHistEntry.mBackgroundUsageTimeInMs < backgroundUsageTimeInMs) {
            if (DEBUG) {
                Log.w(
@@ -371,6 +371,7 @@ public class BatteryHistEntry {
                backgroundUsageConsumePower,
                cachedUsageConsumePower,
                Math.round(foregroundUsageTimeInMs),
                Math.round(foregroundServiceUsageTimeInMs),
                Math.round(backgroundUsageTimeInMs),
                (int) Math.round(batteryLevel));
    }
+1 −1
Original line number Diff line number Diff line
@@ -400,7 +400,7 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
                        mPrefContext,
                        entry.isSystemEntry(),
                        entry.mForegroundUsageTimeInMs,
                        entry.mBackgroundUsageTimeInMs,
                        entry.mBackgroundUsageTimeInMs + entry.mForegroundServiceUsageTimeInMs,
                        entry.mScreenOnTimeInMs));
    }
}
Loading