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

Commit 1493fa2f authored by Kuan Wang's avatar Kuan Wang
Browse files

Database restructure: use protobuf to save battery information fields.

This patch only updates the existing fields.
There will be 2 following patches to:
1. Expose the new fields (foreground / foreground service / background x
   usage time / power consumption) to UI.
2. Get the full charge cycle start time from Database and remove the
   SharedPreference.

Test: make RunSettingsRoboTests + manual
Bug: 253553141
Change-Id: Iee02dc7e671f97899cb1495323acfa0173e31df2
parent 45c9b116
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ android_library {
        "settings-log-bridge-protos-lite",
        "settings-telephony-protos-lite",
        "fuelgauge-log-protos-lite",
        "fuelgauge-usage-state-protos-lite",
        "contextualcards",
        "settings-logtags",
        "statslog-settings",
+8 −0
Original line number Diff line number Diff line
@@ -41,3 +41,11 @@ java_library {
    },
    srcs: ["fuelgauge_log.proto"],
}

java_library {
    name: "fuelgauge-usage-state-protos-lite",
    proto: {
        type: "lite",
    },
    srcs: ["fuelgauge_usage_state.proto"],
}
+41 −0
Original line number Diff line number Diff line
syntax = "proto2";

package com.android.settings.intelligence;
option java_multiple_files = true;
option java_package = "com.android.settings.fuelgauge.batteryusage";
option java_outer_classname = "FuelgaugeUsageStateProto";

// Stores device battery relative information.
message DeviceBatteryState {
  optional int32 battery_level = 1;
  optional int32 battery_status = 2;
  optional int32 battery_health = 3;
}

message BatteryInformation {
  // Records device battery relative information.
  optional DeviceBatteryState device_battery_state = 1;

  // Whether the data is represented as a system component or not?
  optional bool is_hidden = 2;

  // Records the timestamp relative information.
  optional int64 boot_timestamp = 3;
  optional string zone_id = 4;

  // Records app relative information.
  optional string app_label = 7;

  // Records the battery usage relative information.
  optional double total_power = 10;
  optional double consume_power = 11;
  optional double percent_of_total = 12;
  optional int32 drain_type = 13;
  optional int64 foreground_usage_time_in_ms = 14;
  optional int64 foreground_service_usage_time_in_ms = 15;
  optional int64 background_usage_time_in_ms = 16;
  optional double foreground_usage_consume_power = 17;
  optional double foreground_service_usage_consume_power = 18;
  optional double background_usage_consume_power = 19;
  optional double cached_usage_consume_power = 20;
}
 No newline at end of file
+2 −19
Original line number Diff line number Diff line
@@ -19,13 +19,10 @@ 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;
@@ -76,22 +73,8 @@ public final class BatteryHistoricalLogUtil {
    }

    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();
        }
        return BatteryUtils.parseProtoFromString(
                storedLogs, BatteryOptimizeHistoricalLog.getDefaultInstance());
    }

    /**
+26 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.os.Process;
import android.os.SystemClock;
import android.os.UidBatteryConsumer;
import android.os.UserHandle;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.IntDef;
@@ -52,6 +53,9 @@ import com.android.settingslib.fuelgauge.PowerAllowlistBackend;
import com.android.settingslib.utils.PowerUtil;
import com.android.settingslib.utils.ThreadUtils;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageLite;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.Duration;
@@ -316,6 +320,28 @@ public class BatteryUtils {
        }
    }

    /**
     * Parses proto object from string.
     *
     * @param serializedProto the serialized proto string
     * @param protoClass class of the proto
     * @return instance of the proto class parsed from the string
     */
    @SuppressWarnings("unchecked")
    public 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();
        }
    }

    public void setForceAppStandby(int uid, String packageName,
            int mode) {
        final boolean isPreOApp = isPreOApp(packageName);
Loading