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

Commit fabf8e4c authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add support for Fine Granularity PowerMonitors

Bug: 341941666
Flag: android.permission.flags.fine_power_monitor_permission
Test: atest PowerStatsTests; atest android.os.health.cts.PowerMonitorsTest
Change-Id: I8f4abf2116d42e07dbc1aa97c15df29b00acead6
parent 0fd6b40d
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -11561,6 +11561,12 @@ package android.os {
    method @RequiresPermission(android.Manifest.permission.SET_LOW_POWER_STANDBY_PORTS) public void release();
  }
  @FlaggedApi("com.android.server.power.optimization.power_monitor_api") public final class PowerMonitorReadings {
    method @FlaggedApi("android.permission.flags.fine_power_monitor_permission") public int getGranularity();
    field @FlaggedApi("android.permission.flags.fine_power_monitor_permission") public static final int GRANULARITY_FINE = 1; // 0x1
    field @FlaggedApi("android.permission.flags.fine_power_monitor_permission") public static final int GRANULARITY_UNSPECIFIED = 0; // 0x0
  }
  @Deprecated public class PowerWhitelistManager {
    method @Deprecated @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void addToWhitelist(@NonNull String);
    method @Deprecated @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void addToWhitelist(@NonNull java.util.List<java.lang.String>);
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ interface IPowerStatsService {
    const String KEY_ENERGY = "energy";
    /** @hide */
    const String KEY_TIMESTAMPS = "timestamps";
    /** @hide */
    const String KEY_GRANULARITY = "granularity";

    /** @hide */
    const int RESULT_SUCCESS = 0;
+51 −1
Original line number Diff line number Diff line
@@ -18,8 +18,12 @@ package android.os;

import android.annotation.ElapsedRealtimeLong;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.SystemApi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Comparator;

@@ -38,6 +42,37 @@ public final class PowerMonitorReadings {
    @NonNull
    private final long[] mTimestampsMs;

    /**
     * PowerMonitorReadings have the default level of granularity, which may be coarse or fine
     * as determined by the implementation.
     * @hide
     */
    @FlaggedApi(android.permission.flags.Flags.FLAG_FINE_POWER_MONITOR_PERMISSION)
    @SystemApi
    public static final int GRANULARITY_UNSPECIFIED = 0;

    /**
     * PowerMonitorReadings have a high level of granularity. This level of granularity is
     * provided to applications that have the
     * {@link android.Manifest.permission#ACCESS_FINE_POWER_MONITORS} permission.
     *
     * @hide
     */
    @FlaggedApi(android.permission.flags.Flags.FLAG_FINE_POWER_MONITOR_PERMISSION)
    @SystemApi
    public static final int GRANULARITY_FINE = 1;

    /** @hide */
    @IntDef(prefix = {"GRANULARITY_"}, value = {
            GRANULARITY_UNSPECIFIED,
            GRANULARITY_FINE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface PowerMonitorGranularity {}

    @PowerMonitorGranularity
    private final int mGranularity;

    private static final Comparator<PowerMonitor> POWER_MONITOR_COMPARATOR =
            Comparator.comparingInt(pm -> pm.index);

@@ -46,10 +81,12 @@ public final class PowerMonitorReadings {
     * @hide
     */
    public PowerMonitorReadings(@NonNull PowerMonitor[] powerMonitors,
            @NonNull long[] energyUws, @NonNull long[] timestampsMs) {
            @NonNull long[] energyUws, @NonNull long[] timestampsMs,
            @PowerMonitorGranularity int granularity) {
        mPowerMonitors = powerMonitors;
        mEnergyUws = energyUws;
        mTimestampsMs = timestampsMs;
        mGranularity = granularity;
    }

    /**
@@ -79,6 +116,19 @@ public final class PowerMonitorReadings {
        return 0;
    }

    /**
     * Returns the granularity level of the results, which refers to the maximum age of the
     * power monitor readings, {@link #GRANULARITY_FINE} indicating the highest level
     * of freshness supported by the service implementation.
     * @hide
     */
    @FlaggedApi(android.permission.flags.Flags.FLAG_FINE_POWER_MONITOR_PERMISSION)
    @SystemApi
    @PowerMonitorGranularity
    public int getGranularity() {
        return mGranularity;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
+2 −1
Original line number Diff line number Diff line
@@ -428,7 +428,8 @@ public class SystemHealthManager {
                    if (resultCode == IPowerStatsService.RESULT_SUCCESS) {
                        PowerMonitorReadings result = new PowerMonitorReadings(powerMonitorsArray,
                                resultData.getLongArray(IPowerStatsService.KEY_ENERGY),
                                resultData.getLongArray(IPowerStatsService.KEY_TIMESTAMPS));
                                resultData.getLongArray(IPowerStatsService.KEY_TIMESTAMPS),
                                resultData.getInt(IPowerStatsService.KEY_GRANULARITY));
                        if (executor != null) {
                            executor.execute(() -> onResult.onResult(result));
                        } else {
+1 −0
Original line number Diff line number Diff line
@@ -264,6 +264,7 @@ applications that come with the platform
        <!-- Needed for test only -->
        <permission name="android.permission.BATTERY_PREDICTION"/>
        <permission name="android.permission.BATTERY_STATS"/>
        <permission name="android.permission.ACCESS_FINE_POWER_MONITORS" />
        <!-- BLUETOOTH_PRIVILEGED is needed for test only -->
        <permission name="android.permission.BLUETOOTH_PRIVILEGED"/>
        <permission name="android.permission.BIND_APPWIDGET"/>
Loading