Add BatteryHistoricalLogUtil class

- Impl optimize mode log mechanism to record optimize setup flow
 - Log optimize action with MANUAL, APPLY, RESET and RESTORE
 - Use proto file to serialize logs
 sample result: https://screenshot.googleplex.com/5KPrHVGb4pMvS7V.png

Bug: 241735485
Test: make SettingsRoboTests
Change-Id: I879663de4cf45d0cfb9cd7eee957eeb65addfbe1
This commit is contained in:
Wesley Wang
2022-09-28 18:12:35 +08:00
parent bd25f4a303
commit d90026d184
11 changed files with 309 additions and 21 deletions

View File

@@ -32,6 +32,7 @@ import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.fuelgauge.BatteryOptimizeHistoricalLogEntry.Action;
import com.android.settingslib.fuelgauge.PowerAllowlistBackend;
import java.lang.annotation.Retention;
@@ -49,6 +50,7 @@ public class BatteryOptimizeUtils {
@VisibleForTesting boolean mAllowListed;
private final String mPackageName;
private final Context mContext;
private final int mUid;
// If current user is admin, match apps from all users. Otherwise, only match the currect user.
@@ -77,6 +79,7 @@ public class BatteryOptimizeUtils {
public BatteryOptimizeUtils(Context context, int uid, String packageName) {
mUid = uid;
mContext = context;
mPackageName = packageName;
mAppOpsManager = context.getSystemService(AppOpsManager.class);
mBatteryUtils = BatteryUtils.getInstance(context);
@@ -115,12 +118,13 @@ public class BatteryOptimizeUtils {
}
/** Sets the {@link OptimizationMode} for associated app. */
public void setAppUsageState(@OptimizationMode int mode) {
public void setAppUsageState(@OptimizationMode int mode, Action action) {
if (getAppOptimizationMode(mMode, mAllowListed) == mode) {
Log.w(TAG, "set the same optimization mode for: " + mPackageName);
return;
}
setAppUsageStateInternal(mode, mUid, mPackageName, mBatteryUtils, mPowerAllowListBackend);
setAppUsageStateInternal(
mContext, mode, mUid, mPackageName, mBatteryUtils, mPowerAllowListBackend, action);
}
/**
@@ -192,8 +196,8 @@ public class BatteryOptimizeUtils {
}
// Resets to the default mode: MODE_OPTIMIZED.
setAppUsageStateInternal(MODE_OPTIMIZED, info.uid, info.packageName, batteryUtils,
allowlistBackend);
setAppUsageStateInternal(context, MODE_OPTIMIZED, info.uid, info.packageName,
batteryUtils, allowlistBackend, Action.RESET);
}
}
@@ -208,8 +212,9 @@ public class BatteryOptimizeUtils {
}
private static void setAppUsageStateInternal(
@OptimizationMode int mode, int uid, String packageName, BatteryUtils batteryUtils,
PowerAllowlistBackend powerAllowlistBackend) {
Context context, @OptimizationMode int mode, int uid, String packageName,
BatteryUtils batteryUtils, PowerAllowlistBackend powerAllowlistBackend,
Action action) {
if (mode == MODE_UNKNOWN) {
Log.d(TAG, "set unknown app optimization mode.");
return;
@@ -223,14 +228,17 @@ public class BatteryOptimizeUtils {
final boolean allowListed = mode == MODE_UNRESTRICTED;
AsyncTask.execute(() -> {
setAppOptimizationModeInternal(appOpsManagerMode, allowListed, uid, packageName,
batteryUtils, powerAllowlistBackend);
setAppOptimizationModeInternal(context, appOpsManagerMode, allowListed, uid,
packageName, batteryUtils, powerAllowlistBackend, action);
});
}
private static void setAppOptimizationModeInternal(
int appStandbyMode, boolean allowListed, int uid, String packageName,
BatteryUtils batteryUtils, PowerAllowlistBackend powerAllowlistBackend) {
Context context, int appStandbyMode, boolean allowListed, int uid, String packageName,
BatteryUtils batteryUtils, PowerAllowlistBackend powerAllowlistBackend,
Action action) {
final String packageNameKey = BatteryHistoricalLogUtil
.getPackageNameWithUserId(packageName, UserHandle.myUserId());
try {
batteryUtils.setForceAppStandby(uid, packageName, appStandbyMode);
if (allowListed) {
@@ -239,8 +247,15 @@ public class BatteryOptimizeUtils {
powerAllowlistBackend.removeApp(packageName);
}
} catch (Exception e) {
// Error cases, set standby mode as -1 for logging.
appStandbyMode = -1;
Log.e(TAG, "set OPTIMIZATION MODE failed for " + packageName, e);
}
BatteryHistoricalLogUtil.writeLog(
context,
action,
packageNameKey,
createLogEvent(appStandbyMode, allowListed));
}
private void refreshState() {
@@ -251,4 +266,12 @@ public class BatteryOptimizeUtils {
Log.d(TAG, String.format("refresh %s state, allowlisted = %s, mode = %d",
mPackageName, mAllowListed, mMode));
}
private static String createLogEvent(int appStandbyMode, boolean allowListed) {
return appStandbyMode < 0 ? "Apply optimize setting ERROR" :
String.format("\tStandbyMode: %s, allowListed: %s, mode: %s",
appStandbyMode,
allowListed,
getAppOptimizationMode(appStandbyMode, allowListed));
}
}