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

Commit 807aa72a authored by mxyyiyi's avatar mxyyiyi
Browse files

[UR] Add battery optimization mode update log in shardpreference for certain actions.

Bug: 400626521
Bug: 350657779
Test: atest PowerBackgroundUsageDetailTest BatteryOptimizationActionLogUtilsTest BatteryOptimizationModeUtilsTest
Test: atest BatteryAnomalyTipTest
Flag: EXEMPT for simple feature
Change-Id: I3debd1a15513ee72a3da2c05fc1f2039e1d55268
parent 95e184da
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ message BatteryOptimizeHistoricalLogEntry {
    FORCE_RESET = 6;
    EXTERNAL_UPDATE = 7;
    EXPIRATION_RESET = 8;
    BATTERY_TIP_APPLY = 9;
    BATTERY_TIP_ACCEPT= 10;
  }

  optional string package_name = 1;
+7 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.fuelgauge;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
@@ -29,6 +30,7 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.fuelgauge.PowerBackgroundUsageDetail.LaunchSourceType;
import com.android.settingslib.PrimarySwitchPreference;

/** Controller to update the manage battery usage preference in App Battery Usage page */
@@ -103,8 +105,12 @@ public class BackgroundUsageAllowabilityPreferenceController extends BasePrefere
        if (isPreferenceEnabled) {
            mBackgroundUsageAllowabilityPreference.setOnPreferenceClickListener(
                    preference -> {
                        Bundle arguments = mDashboardFragment.getArguments();
                        arguments.putString(
                                PowerBackgroundUsageDetail.EXTRA_LAUNCH_SOURCE,
                                LaunchSourceType.APP_BATTERY_USAGE_PAGE.name());
                        PowerBackgroundUsageDetail.startPowerBackgroundUsageDetailPage(
                                mContext, mDashboardFragment.getArguments());
                                mContext, arguments);
                        return true;
                    });
            mBackgroundUsageAllowabilityPreference.setOnPreferenceChangeListener(
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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

import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import androidx.core.content.edit
import com.android.settings.fuelgauge.BatteryOptimizeHistoricalLogEntry.Action
import java.io.PrintWriter

/** Writes and reads historical logs of battery optimization mode with certain actions. */
object BatteryOptimizationActionLogUtils {
    private const val TAG = "BatteryOptimizationActionLogUtils"
    private const val BATTERY_OPTIMIZE_ACTION_FILE_NAME = "battery_optimize_action_historical_logs"

    /** Writes the battery optimization mode update action history. */
    @JvmStatic
    fun writeLog(context: Context, packageName: String, action: Action) {
        val sharedPreferences = getSharedPreferences(context)
        sharedPreferences.edit { putInt(packageName, action.number) }
    }

    /** Gets the battery optimization mode change action for the given packages. */
    @JvmStatic
    fun getBatteryOptimizeActionLogs(
        context: Context,
        packageNames: List<String>
    ): List<Action> {
        val sharedPreferences = getSharedPreferences(context)
        val actionList: MutableList<Action> = ArrayList(packageNames.size)
        for (packageName in packageNames) {
            actionList.add(
                Action.forNumber(
                    sharedPreferences.getInt(packageName, Action.UNKNOWN.number)
                ) ?: Action.UNKNOWN
            )
        }
        return actionList
    }

    /** Prints the historical log that has previously been stored by this utility. */
    @JvmStatic
    fun printBatteryOptimizationActionLogs(context: Context, writer: PrintWriter) {
        val sharedPreferences = getSharedPreferences(context)
        writer.println("Battery optimization action history:")
        for (key in sharedPreferences.all.keys) {
            val action = Action.forNumber(sharedPreferences.getInt(key, Action.UNKNOWN.number))
            if (action != null && action != Action.UNKNOWN) {
                writer.print("$key=$action\t")
            }
        }
        writer.println()
    }

    @VisibleForTesting
    @JvmStatic
    fun getSharedPreferences(context: Context): SharedPreferences {
        return context.applicationContext
            .getSharedPreferences(
                BATTERY_OPTIMIZE_ACTION_FILE_NAME, Context.MODE_PRIVATE
            )
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -374,6 +374,9 @@ public class BatteryOptimizeUtils {
        if (action != Action.RESET) { // reset has been notified in resetAppOptimizationMode
            BatterySettingsStorage.get(context).notifyChange(toChangeReason(action));
        }
        if (action == Action.BATTERY_TIP_APPLY || action == Action.BATTERY_TIP_ACCEPT) {
            BatteryOptimizationActionLogUtils.writeLog(context, packageNameKey, action);
        }
    }

    private static String createLogEvent(int appStandbyMode, boolean allowListed) {
+22 −0
Original line number Diff line number Diff line
@@ -54,6 +54,16 @@ public class PowerBackgroundUsageDetail extends DashboardFragment {
    public static final String EXTRA_LABEL = "extra_label";
    public static final String EXTRA_POWER_USAGE_AMOUNT = "extra_power_usage_amount";
    public static final String EXTRA_ICON_ID = "extra_icon_id";
    public static final String EXTRA_LAUNCH_SOURCE = "extra_launch_source";

    /** Launch Source type of current fragment. */
    public enum LaunchSourceType {
        UNKNOWN,
        BATTERY_TIP,
        APP_BATTERY_USAGE_PAGE,
        INTENT,
    }

    private static final String KEY_PREF_HEADER = "header_view";
    private static final String KEY_FOOTER_PREFERENCE = "app_usage_footer_preference";
    private static final String KEY_BATTERY_OPTIMIZATION_MODE_CATEGORY =
@@ -65,6 +75,7 @@ public class PowerBackgroundUsageDetail extends DashboardFragment {
    @VisibleForTesting ApplicationsState.AppEntry mAppEntry;
    @VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
    @VisibleForTesting StringBuilder mLogStringBuilder;
    @VisibleForTesting LaunchSourceType mLaunchSourceType = LaunchSourceType.UNKNOWN;

    @VisibleForTesting @BatteryOptimizeUtils.OptimizationMode
    int mOptimizationMode = BatteryOptimizeUtils.MODE_UNKNOWN;
@@ -76,6 +87,9 @@ public class PowerBackgroundUsageDetail extends DashboardFragment {
        final Bundle bundle = getArguments();
        final int uid = bundle.getInt(EXTRA_UID, 0);
        final String packageName = bundle.getString(EXTRA_PACKAGE_NAME);
        mLaunchSourceType =
                LaunchSourceType.valueOf(
                        bundle.getString(EXTRA_LAUNCH_SOURCE, LaunchSourceType.UNKNOWN.name()));
        mBatteryOptimizeUtils = new BatteryOptimizeUtils(getContext(), uid, packageName);
        mState = ApplicationsState.getInstance(getActivity().getApplication());
        if (packageName != null) {
@@ -112,6 +126,14 @@ public class PowerBackgroundUsageDetail extends DashboardFragment {
                            BatteryOptimizeLogUtils.getPackageNameWithUserId(
                                    mBatteryOptimizeUtils.getPackageName(), UserHandle.myUserId()),
                            mLogStringBuilder.toString());
                    if (mLaunchSourceType == LaunchSourceType.BATTERY_TIP
                            && mOptimizationMode == BatteryOptimizeUtils.MODE_UNRESTRICTED
                            && currentOptimizeMode == BatteryOptimizeUtils.MODE_OPTIMIZED) {
                        BatteryOptimizationActionLogUtils.writeLog(
                                applicationContext,
                                mBatteryOptimizeUtils.getPackageName(),
                                Action.BATTERY_TIP_ACCEPT);
                    }
                });
        Log.d(TAG, "Leave with mode: " + currentOptimizeMode);
    }
Loading