Record app optimization mode backup into BatteryHistoricalLog
App optimization mode format: https://screenshot.googleplex.com/di9DDzBfYf7ihfV App optimization mode backup format: https://screenshot.googleplex.com/GkVW5HrgGvmv5yh Bug: 192523697 Test: make SettingsRoboTests Change-Id: I60a9a76a8ffc89d625ee3f77c138a19181c81c38
This commit is contained in:
@@ -37,40 +37,40 @@ public final class 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) {
|
||||
/** Writes a log entry for battery optimization mode. */
|
||||
static void writeLog(
|
||||
Context context, Action action, String packageName, String actionDescription) {
|
||||
writeLog(getSharedPreferences(context), action, packageName, actionDescription);
|
||||
}
|
||||
|
||||
static void writeLog(SharedPreferences sharedPreferences, Action action,
|
||||
String packageName, String actionDescription) {
|
||||
writeLog(
|
||||
context,
|
||||
sharedPreferences,
|
||||
BatteryOptimizeHistoricalLogEntry.newBuilder()
|
||||
.setPackageName(pkg)
|
||||
.setPackageName(packageName)
|
||||
.setAction(action)
|
||||
.setActionDescription(actionDescription)
|
||||
.setTimestamp(System.currentTimeMillis())
|
||||
.build());
|
||||
}
|
||||
|
||||
private static void writeLog(Context context, BatteryOptimizeHistoricalLogEntry logEntry) {
|
||||
SharedPreferences sharedPreferences = getSharedPreferences(context);
|
||||
|
||||
private static void writeLog(
|
||||
SharedPreferences sharedPreferences, BatteryOptimizeHistoricalLogEntry logEntry) {
|
||||
BatteryOptimizeHistoricalLog existingLog =
|
||||
parseLogFromString(sharedPreferences.getString(LOGS_KEY, ""));
|
||||
BatteryOptimizeHistoricalLog.Builder newLogBuilder = existingLog.toBuilder();
|
||||
// Prune old entries
|
||||
// Prune old entries to limit the max logging data count.
|
||||
if (existingLog.getLogEntryCount() >= MAX_ENTRIES) {
|
||||
newLogBuilder.removeLogEntry(0);
|
||||
}
|
||||
newLogBuilder.addLogEntry(logEntry);
|
||||
|
||||
String loggingContent =
|
||||
Base64.encodeToString(newLogBuilder.build().toByteArray(), Base64.DEFAULT);
|
||||
sharedPreferences
|
||||
.edit()
|
||||
.putString(
|
||||
LOGS_KEY,
|
||||
Base64.encodeToString(newLogBuilder.build().toByteArray(), Base64.DEFAULT))
|
||||
.putString(LOGS_KEY, loggingContent)
|
||||
.apply();
|
||||
}
|
||||
|
||||
@@ -79,34 +79,36 @@ public final class BatteryHistoricalLogUtil {
|
||||
storedLogs, BatteryOptimizeHistoricalLog.getDefaultInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the historical log that has previously been stored by this utility.
|
||||
*/
|
||||
/** Prints the historical log that has previously been stored by this utility. */
|
||||
public static void printBatteryOptimizeHistoricalLog(Context context, PrintWriter writer) {
|
||||
printBatteryOptimizeHistoricalLog(getSharedPreferences(context), writer);
|
||||
}
|
||||
|
||||
/** Prints the historical log that has previously been stored by this utility. */
|
||||
public static void printBatteryOptimizeHistoricalLog(
|
||||
SharedPreferences sharedPreferences, 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.");
|
||||
writer.println("\tnothing to dump");
|
||||
} else {
|
||||
writer.println("0:RESTRICTED 1:UNRESTRICTED 2:OPTIMIZED 3:UNKNOWN");
|
||||
writer.println("0:UNKNOWN 1:RESTRICTED 2:UNRESTRICTED 3:OPTIMIZED");
|
||||
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;
|
||||
/** Gets the unique key for logging. */
|
||||
static String getPackageNameWithUserId(String packageName, int userId) {
|
||||
return packageName + ":" + userId;
|
||||
}
|
||||
|
||||
private static String toString(BatteryOptimizeHistoricalLogEntry entry) {
|
||||
return String.format("%s\tAction:%s\tEvent:%s\tTimestamp:%s", entry.getPackageName(),
|
||||
entry.getAction(), entry.getActionDescription(),
|
||||
ConvertUtils.utcToLocalTimeForLogging(entry.getTimestamp()));
|
||||
return String.format("%s\t%s\taction:%s\tevent:%s",
|
||||
ConvertUtils.utcToLocalTimeForLogging(entry.getTimestamp()),
|
||||
entry.getPackageName(), entry.getAction(),
|
||||
entry.getActionDescription());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
Reference in New Issue
Block a user