diff options
Diffstat (limited to 'core/java/android/os/PowerComponents.java')
| -rw-r--r-- | core/java/android/os/PowerComponents.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/core/java/android/os/PowerComponents.java b/core/java/android/os/PowerComponents.java index 964f1b681866..a90ed20d54fc 100644 --- a/core/java/android/os/PowerComponents.java +++ b/core/java/android/os/PowerComponents.java @@ -15,7 +15,11 @@ */ package android.os; +import static android.os.BatteryConsumer.convertMahToDeciCoulombs; + import android.annotation.NonNull; +import android.annotation.Nullable; +import android.util.proto.ProtoOutputStream; import com.android.internal.os.PowerCalculator; @@ -237,6 +241,59 @@ class PowerComponents { } } + /** Returns whether there are any atoms.proto POWER_COMPONENTS data to write to a proto. */ + boolean hasStatsProtoData() { + return writeStatsProtoImpl(null); + } + + /** Writes all atoms.proto POWER_COMPONENTS for this PowerComponents to the given proto. */ + void writeStatsProto(@NonNull ProtoOutputStream proto) { + writeStatsProtoImpl(proto); + } + + /** + * Returns whether there are any atoms.proto POWER_COMPONENTS data to write to a proto, + * and writes it to the given proto if it is non-null. + */ + private boolean writeStatsProtoImpl(@Nullable ProtoOutputStream proto) { + boolean interestingData = false; + + for (int idx = 0; idx < mPowerComponentsMah.length; idx++) { + final int componentId = idx < BatteryConsumer.POWER_COMPONENT_COUNT ? + idx : idx - CUSTOM_POWER_COMPONENT_OFFSET; + final long powerDeciCoulombs = convertMahToDeciCoulombs(mPowerComponentsMah[idx]); + final long durationMs = mUsageDurationsMs[idx]; + + if (powerDeciCoulombs == 0 && durationMs == 0) { + // No interesting data. Make sure not to even write the COMPONENT int. + continue; + } + + interestingData = true; + if (proto == null) { + // We're just asked whether there is data, not to actually write it. And there is. + return true; + } + + final long token = + proto.start(BatteryUsageStatsAtomsProto.BatteryConsumerData.POWER_COMPONENTS); + proto.write( + BatteryUsageStatsAtomsProto.BatteryConsumerData.PowerComponentUsage + .COMPONENT, + componentId); + proto.write( + BatteryUsageStatsAtomsProto.BatteryConsumerData.PowerComponentUsage + .POWER_DECI_COULOMBS, + powerDeciCoulombs); + proto.write( + BatteryUsageStatsAtomsProto.BatteryConsumerData.PowerComponentUsage + .DURATION_MILLIS, + durationMs); + proto.end(token); + } + return interestingData; + } + /** * Builder for PowerComponents. */ |
