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

Commit b5f7d62b authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Log optimization mode for app usage in BatteryUsageSlot." into main

parents 58606356 08d4b7a2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -39,6 +39,9 @@ public interface PowerUsageFeatureProvider {
    /** Check whether the battery tips card is enabled in the battery usage page */
    boolean isBatteryTipsEnabled();

    /** Check whether to log the optimization mode of app entry in period job */
    boolean isAppOptimizationModeLogged();

    /**
     * Returns a threshold (in milliseconds) for the minimal screen on time in battery usage list
     */
+5 −0
Original line number Diff line number Diff line
@@ -83,6 +83,11 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
        return false;
    }

    @Override
    public boolean isAppOptimizationModeLogged() {
        return false;
    }

    @Override
    public double getBatteryUsageListScreenOnTimeThresholdInMs() {
        return 0;
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 com.android.settings.fuelgauge.batteryusage;

import android.content.Context;
import android.util.ArrayMap;

import androidx.annotation.VisibleForTesting;

import com.android.settings.fuelgauge.BatteryOptimizeUtils;
import com.android.settingslib.fuelgauge.PowerAllowlistBackend;

import java.util.Map;

/** A cache to log battery optimization mode of an app */
final class BatteryOptimizationModeCache {
    private static final String TAG = "BatteryOptimizationModeCache";

    @VisibleForTesting final Map<Integer, BatteryOptimizationMode> mBatteryOptimizeModeCacheMap;

    private final Context mContext;

    BatteryOptimizationModeCache(final Context context) {
        mContext = context;
        mBatteryOptimizeModeCacheMap = new ArrayMap<>();
        PowerAllowlistBackend.getInstance(mContext).refreshList();
    }

    BatteryOptimizationMode getBatteryOptimizeMode(final int uid, final String packageName) {
        if (!mBatteryOptimizeModeCacheMap.containsKey(uid)) {
            final BatteryOptimizeUtils batteryOptimizeUtils =
                    new BatteryOptimizeUtils(mContext, uid, packageName);
            mBatteryOptimizeModeCacheMap.put(
                    uid,
                    BatteryOptimizationMode.forNumber(
                            batteryOptimizeUtils.getAppOptimizationMode(/* refreshList= */ false)));
        }
        return mBatteryOptimizeModeCacheMap.get(uid);
    }
}
+12 −7
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.util.Log;
import androidx.annotation.VisibleForTesting;

import com.android.settings.fuelgauge.BatteryUsageHistoricalLogEntry.Action;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.fuelgauge.batteryusage.bugreport.BatteryUsageLogUtils;
import com.android.settings.overlay.FeatureFactory;

@@ -124,9 +125,15 @@ public final class BatteryUsageDataLoader {
                        userIdsSeries,
                        /* isFromPeriodJob= */ true,
                        batteryDiffDataMap -> {
                            final PowerUsageFeatureProvider featureProvider =
                                    FeatureFactory.getFeatureFactory()
                                            .getPowerUsageFeatureProvider();
                            DatabaseUtils.sendBatteryUsageSlotData(
                                    context,
                                    ConvertUtils.convertToBatteryUsageSlotList(batteryDiffDataMap));
                                    ConvertUtils.convertToBatteryUsageSlotList(
                                            context,
                                            batteryDiffDataMap,
                                            featureProvider.isAppOptimizationModeLogged()));
                            if (batteryDiffDataMap.values().stream()
                                    .anyMatch(
                                            data ->
@@ -135,9 +142,7 @@ public final class BatteryUsageDataLoader {
                                                                            .isEmpty()
                                                                    || !data.getAppDiffEntryList()
                                                                            .isEmpty()))) {
                                FeatureFactory.getFeatureFactory()
                                        .getPowerUsageFeatureProvider()
                                        .detectPowerAnomaly(
                                featureProvider.detectPowerAnomaly(
                                        context,
                                        /* displayDrain= */ 0,
                                        DetectRequestSourceType.TYPE_DATA_LOADER);
+21 −7
Original line number Diff line number Diff line
@@ -345,10 +345,15 @@ public final class ConvertUtils {

    /** Converts from {@link Map<Long, BatteryDiffData>} to {@link List<BatteryUsageSlot>} */
    public static List<BatteryUsageSlot> convertToBatteryUsageSlotList(
            final Map<Long, BatteryDiffData> batteryDiffDataMap) {
            final Context context,
            final Map<Long, BatteryDiffData> batteryDiffDataMap,
            final boolean isAppOptimizationModeLogged) {
        List<BatteryUsageSlot> batteryUsageSlotList = new ArrayList<>();
        final BatteryOptimizationModeCache optimizationModeCache =
                isAppOptimizationModeLogged ? new BatteryOptimizationModeCache(context) : null;
        for (BatteryDiffData batteryDiffData : batteryDiffDataMap.values()) {
            batteryUsageSlotList.add(convertToBatteryUsageSlot(batteryDiffData));
            batteryUsageSlotList.add(
                    convertToBatteryUsageSlot(batteryDiffData, optimizationModeCache));
        }
        return batteryUsageSlotList;
    }
@@ -479,9 +484,10 @@ public final class ConvertUtils {
        }
    }


    @VisibleForTesting
    static BatteryUsageDiff convertToBatteryUsageDiff(BatteryDiffEntry batteryDiffEntry) {
    static BatteryUsageDiff convertToBatteryUsageDiff(
            final BatteryDiffEntry batteryDiffEntry,
            final @Nullable BatteryOptimizationModeCache optimizationModeCache) {
        BatteryUsageDiff.Builder builder =
                BatteryUsageDiff.newBuilder()
                        .setUid(batteryDiffEntry.mUid)
@@ -511,11 +517,18 @@ public final class ConvertUtils {
        if (batteryDiffEntry.mLegacyLabel != null) {
            builder.setLabel(batteryDiffEntry.mLegacyLabel);
        }
        // Log the battery optimization mode of AppEntry while converting to batteryUsageSlot.
        if (optimizationModeCache != null && !batteryDiffEntry.isSystemEntry()) {
            builder.setAppOptimizationMode(
                    optimizationModeCache.getBatteryOptimizeMode(
                            (int) batteryDiffEntry.mUid, batteryDiffEntry.getPackageName()));
        }
        return builder.build();
    }

    private static BatteryUsageSlot convertToBatteryUsageSlot(
            final BatteryDiffData batteryDiffData) {
            final BatteryDiffData batteryDiffData,
            final @Nullable BatteryOptimizationModeCache optimizationModeCache) {
        if (batteryDiffData == null) {
            return BatteryUsageSlot.getDefaultInstance();
        }
@@ -527,10 +540,11 @@ public final class ConvertUtils {
                        .setEndBatteryLevel(batteryDiffData.getEndBatteryLevel())
                        .setScreenOnTime(batteryDiffData.getScreenOnTime());
        for (BatteryDiffEntry batteryDiffEntry : batteryDiffData.getAppDiffEntryList()) {
            builder.addAppUsage(convertToBatteryUsageDiff(batteryDiffEntry));
            builder.addAppUsage(convertToBatteryUsageDiff(batteryDiffEntry, optimizationModeCache));
        }
        for (BatteryDiffEntry batteryDiffEntry : batteryDiffData.getSystemDiffEntryList()) {
            builder.addSystemUsage(convertToBatteryUsageDiff(batteryDiffEntry));
            builder.addSystemUsage(
                    convertToBatteryUsageDiff(batteryDiffEntry, /* optimizationModeCache= */ null));
        }
        return builder.build();
    }
Loading