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

@@ -0,0 +1,129 @@
/*
* Copyright (C) 2022 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 android.util.Base64;
import android.util.Log;
import com.android.settings.fuelgauge.BatteryOptimizeHistoricalLogEntry.Action;
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageLite;
import java.io.PrintWriter;
import java.util.List;
/** Writes and reads a historical log of battery related state change events. */
public final class BatteryHistoricalLogUtil {
private static final String BATTERY_OPTIMIZE_FILE_NAME = "battery_optimize_historical_logs";
private static final String LOGS_KEY = "battery_optimize_logs_key";
private static final String TAG = "BatteryHistoricalLogUtil";
@VisibleForTesting
static final int MAX_ENTRIES = 40;
/**
* Writes a log entry.
*
* <p>Keeps up to {@link #MAX_ENTRIES} in the log, once that number is exceeded, it prunes the
* oldest one.
*/
static void writeLog(Context context, Action action, String pkg, String actionDescription) {
writeLog(
context,
BatteryOptimizeHistoricalLogEntry.newBuilder()
.setPackageName(pkg)
.setAction(action)
.setActionDescription(actionDescription)
.build());
}
private static void writeLog(Context context, BatteryOptimizeHistoricalLogEntry logEntry) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
BatteryOptimizeHistoricalLog existingLog =
parseLogFromString(sharedPreferences.getString(LOGS_KEY, ""));
BatteryOptimizeHistoricalLog.Builder newLogBuilder = existingLog.toBuilder();
// Prune old entries
if (existingLog.getLogEntryCount() >= MAX_ENTRIES) {
newLogBuilder.removeLogEntry(0);
}
newLogBuilder.addLogEntry(logEntry);
sharedPreferences
.edit()
.putString(
LOGS_KEY,
Base64.encodeToString(newLogBuilder.build().toByteArray(), Base64.DEFAULT))
.apply();
}
private static BatteryOptimizeHistoricalLog parseLogFromString(String storedLogs) {
return parseProtoFromString(storedLogs, BatteryOptimizeHistoricalLog.getDefaultInstance());
}
@SuppressWarnings("unchecked")
private static <T extends MessageLite> T parseProtoFromString(
String serializedProto, T protoClass) {
if (serializedProto.isEmpty()) {
return (T) protoClass.getDefaultInstanceForType();
}
try {
return (T) protoClass.getParserForType()
.parseFrom(Base64.decode(serializedProto, Base64.DEFAULT));
} catch (InvalidProtocolBufferException e) {
Log.e(TAG, "Failed to deserialize proto class", e);
return (T) protoClass.getDefaultInstanceForType();
}
}
/**
* Prints the historical log that has previously been stored by this utility.
*/
public static void printBatteryOptimizeHistoricalLog(Context context, PrintWriter writer) {
writer.println("Battery optimize state history:");
SharedPreferences sharedPreferences = getSharedPreferences(context);
BatteryOptimizeHistoricalLog existingLog =
parseLogFromString(sharedPreferences.getString(LOGS_KEY, ""));
List<BatteryOptimizeHistoricalLogEntry> logEntryList = existingLog.getLogEntryList();
if (logEntryList.isEmpty()) {
writer.println("\tNo past logs.");
} else {
logEntryList.forEach(entry -> writer.println(toString(entry)));
}
}
/**
* Gets the unique key for logging, combined with package name, delimiter and user id.
*/
static String getPackageNameWithUserId(String pkgName, int userId) {
return pkgName + ":" + userId;
}
private static String toString(BatteryOptimizeHistoricalLogEntry entry) {
return String.format("%s\tAction:%s\tEvent:%s",
entry.getPackageName(), entry.getAction(), entry.getActionDescription());
}
@VisibleForTesting
static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(BATTERY_OPTIMIZE_FILE_NAME, Context.MODE_PRIVATE);
}
}