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

Commit 681e9b1f authored by David Su's avatar David Su
Browse files

WifiActivityEnergyInfo: clean up API surface

- Removed setters and made object immutable.
- Added annotations where appropriate.
- Marked isValid() as @hide.
- Refactored BatteryExternalStatsWorker to not
    use setters

Bug: 148218218
Test: compiles
Change-Id: I799149f5ec6a88192f686cc23e947466765b2a1e
parent 0d66c262
Loading
Loading
Loading
Loading
+7 −14
Original line number Diff line number Diff line
@@ -8983,23 +8983,15 @@ package android.os.connectivity {
  }
  public final class WifiActivityEnergyInfo implements android.os.Parcelable {
    ctor public WifiActivityEnergyInfo(long, int, long, long, long, long);
    ctor public WifiActivityEnergyInfo(long, int, @IntRange(from=0) long, @IntRange(from=0) long, @IntRange(from=0) long, @IntRange(from=0) long);
    method public int describeContents();
    method public long getControllerEnergyUsedMicroJoules();
    method public long getControllerIdleDurationMillis();
    method public long getControllerRxDurationMillis();
    method public long getControllerScanDurationMillis();
    method public long getControllerTxDurationMillis();
    method @IntRange(from=0) public long getControllerEnergyUsedMicroJoules();
    method @IntRange(from=0) public long getControllerIdleDurationMillis();
    method @IntRange(from=0) public long getControllerRxDurationMillis();
    method @IntRange(from=0) public long getControllerScanDurationMillis();
    method @IntRange(from=0) public long getControllerTxDurationMillis();
    method public int getStackState();
    method public long getTimeSinceBootMillis();
    method public boolean isValid();
    method public void setControllerEnergyUsedMicroJoules(long);
    method public void setControllerIdleDurationMillis(long);
    method public void setControllerRxDurationMillis(long);
    method public void setControllerScanDurationMillis(long);
    method public void setControllerTxDurationMillis(long);
    method public void setStackState(int);
    method public void setTimeSinceBootMillis(long);
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.os.connectivity.WifiActivityEnergyInfo> CREATOR;
    field public static final int STACK_STATE_INVALID = 0; // 0x0
@@ -14244,3 +14236,4 @@ package android.webkit {
  }
}
+66 −66
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package android.os.connectivity;

import android.annotation.ElapsedRealtimeLong;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.app.ActivityThread;
@@ -37,14 +39,20 @@ import java.lang.annotation.RetentionPolicy;
 */
@SystemApi
public final class WifiActivityEnergyInfo implements Parcelable {
    private long mTimeSinceBootMillis;
    @ElapsedRealtimeLong
    private final long mTimeSinceBootMillis;
    @StackState
    private int mStackState;
    private long mControllerTxDurationMillis;
    private long mControllerRxDurationMillis;
    private long mControllerScanDurationMillis;
    private long mControllerIdleDurationMillis;
    private long mControllerEnergyUsedMicroJoules;
    private final int mStackState;
    @IntRange(from = 0)
    private final long mControllerTxDurationMillis;
    @IntRange(from = 0)
    private final long mControllerRxDurationMillis;
    @IntRange(from = 0)
    private final long mControllerScanDurationMillis;
    @IntRange(from = 0)
    private final long mControllerIdleDurationMillis;
    @IntRange(from = 0)
    private final long mControllerEnergyUsedMicroJoules;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
@@ -67,7 +75,7 @@ public final class WifiActivityEnergyInfo implements Parcelable {
    /**
     * Constructor.
     *
     * @param timeSinceBootMillis the time since boot, in milliseconds.
     * @param timeSinceBootMillis the elapsed real time since boot, in milliseconds.
     * @param stackState The current state of the Wifi Stack. One of {@link #STACK_STATE_INVALID},
     *                   {@link #STACK_STATE_STATE_ACTIVE}, {@link #STACK_STATE_STATE_SCANNING},
     *                   or {@link #STACK_STATE_STATE_IDLE}.
@@ -78,23 +86,27 @@ public final class WifiActivityEnergyInfo implements Parcelable {
     *                       receiving.
     */
    public WifiActivityEnergyInfo(
            long timeSinceBootMillis,
            @ElapsedRealtimeLong long timeSinceBootMillis,
            @StackState int stackState,
            long txDurationMillis,
            long rxDurationMillis,
            long scanDurationMillis,
            long idleDurationMillis) {
        mTimeSinceBootMillis = timeSinceBootMillis;
        mStackState = stackState;
        mControllerTxDurationMillis = txDurationMillis;
        mControllerRxDurationMillis = rxDurationMillis;
        mControllerScanDurationMillis = scanDurationMillis;
        mControllerIdleDurationMillis = idleDurationMillis;

            @IntRange(from = 0) long txDurationMillis,
            @IntRange(from = 0) long rxDurationMillis,
            @IntRange(from = 0) long scanDurationMillis,
            @IntRange(from = 0) long idleDurationMillis) {

        this(timeSinceBootMillis,
                stackState,
                txDurationMillis,
                rxDurationMillis,
                scanDurationMillis,
                idleDurationMillis,
                calculateEnergyMicroJoules(txDurationMillis, rxDurationMillis, idleDurationMillis));
    }

    private static long calculateEnergyMicroJoules(
            long txDurationMillis, long rxDurationMillis, long idleDurationMillis) {
        final Context context = ActivityThread.currentActivityThread().getSystemContext();
        if (context == null) {
            mControllerEnergyUsedMicroJoules = 0L;
            return;
            return 0L;
        }
        // Calculate energy used using PowerProfile.
        PowerProfile powerProfile = new PowerProfile(context);
@@ -106,10 +118,28 @@ public final class WifiActivityEnergyInfo implements Parcelable {
                PowerProfile.POWER_WIFI_CONTROLLER_TX);
        final double voltage = powerProfile.getAveragePower(
                PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
        final long energyUsedMicroJoules = (long) ((mControllerTxDurationMillis * txCurrent
                + mControllerRxDurationMillis * rxCurrent
                + mControllerIdleDurationMillis * rxIdleCurrent)

        return (long) ((txDurationMillis * txCurrent
                + rxDurationMillis * rxCurrent
                + idleDurationMillis * rxIdleCurrent)
                * voltage);
    }

    /** @hide */
    public WifiActivityEnergyInfo(
            @ElapsedRealtimeLong long timeSinceBootMillis,
            @StackState int stackState,
            @IntRange(from = 0) long txDurationMillis,
            @IntRange(from = 0) long rxDurationMillis,
            @IntRange(from = 0) long scanDurationMillis,
            @IntRange(from = 0) long idleDurationMillis,
            @IntRange(from = 0) long energyUsedMicroJoules) {
        mTimeSinceBootMillis = timeSinceBootMillis;
        mStackState = stackState;
        mControllerTxDurationMillis = txDurationMillis;
        mControllerRxDurationMillis = rxDurationMillis;
        mControllerScanDurationMillis = scanDurationMillis;
        mControllerIdleDurationMillis = idleDurationMillis;
        mControllerEnergyUsedMicroJoules = energyUsedMicroJoules;
    }

@@ -158,16 +188,12 @@ public final class WifiActivityEnergyInfo implements Parcelable {
        return 0;
    }

    /** Get the timestamp (milliseconds since boot) of record creation. */
    /** Get the timestamp (elapsed real time milliseconds since boot) of record creation. */
    @ElapsedRealtimeLong
    public long getTimeSinceBootMillis() {
        return mTimeSinceBootMillis;
    }

    /** Set the timestamp (milliseconds since boot) of record creation. */
    public void setTimeSinceBootMillis(long timeSinceBootMillis) {
        mTimeSinceBootMillis = timeSinceBootMillis;
    }

    /**
     * Get the Wifi stack reported state. One of {@link #STACK_STATE_INVALID},
     * {@link #STACK_STATE_STATE_ACTIVE}, {@link #STACK_STATE_STATE_SCANNING},
@@ -178,66 +204,40 @@ public final class WifiActivityEnergyInfo implements Parcelable {
        return mStackState;
    }

    /**
     * Set the Wifi stack reported state. One of {@link #STACK_STATE_INVALID},
     * {@link #STACK_STATE_STATE_ACTIVE}, {@link #STACK_STATE_STATE_SCANNING},
     * {@link #STACK_STATE_STATE_IDLE}.
     */
    public void setStackState(@StackState int stackState) {
        mStackState = stackState;
    }

    /** Get the Wifi transmission duration, in milliseconds. */
    @IntRange(from = 0)
    public long getControllerTxDurationMillis() {
        return mControllerTxDurationMillis;
    }

    /** Set the Wifi transmission duration, in milliseconds. */
    public void setControllerTxDurationMillis(long controllerTxDurationMillis) {
        mControllerTxDurationMillis = controllerTxDurationMillis;
    }

    /** Get the Wifi receive duration, in milliseconds. */
    @IntRange(from = 0)
    public long getControllerRxDurationMillis() {
        return mControllerRxDurationMillis;
    }

    /** Set the Wifi receive duration, in milliseconds. */
    public void setControllerRxDurationMillis(long controllerRxDurationMillis) {
        mControllerRxDurationMillis = controllerRxDurationMillis;
    }

    /** Get the Wifi scan duration, in milliseconds. */
    @IntRange(from = 0)
    public long getControllerScanDurationMillis() {
        return mControllerScanDurationMillis;
    }

    /** Set the Wifi scan duration, in milliseconds. */
    public void setControllerScanDurationMillis(long controllerScanDurationMillis) {
        mControllerScanDurationMillis = controllerScanDurationMillis;
    }

    /** Get the Wifi idle duration, in milliseconds. */
    @IntRange(from = 0)
    public long getControllerIdleDurationMillis() {
        return mControllerIdleDurationMillis;
    }

    /** Set the Wifi idle duration, in milliseconds. */
    public void setControllerIdleDurationMillis(long controllerIdleDurationMillis) {
        mControllerIdleDurationMillis = controllerIdleDurationMillis;
    }

    /** Get the energy consumed by Wifi, in microjoules. */
    @IntRange(from = 0)
    public long getControllerEnergyUsedMicroJoules() {
        return mControllerEnergyUsedMicroJoules;
    }

    /** Set the energy consumed by Wifi, in microjoules. */
    public void setControllerEnergyUsedMicroJoules(long controllerEnergyUsedMicroJoules) {
        mControllerEnergyUsedMicroJoules = controllerEnergyUsedMicroJoules;
    }

    /** Returns true if the record is valid, false otherwise. */
    /**
     * Returns true if the record is valid, false otherwise.
     * @hide
     */
    public boolean isValid() {
        return mControllerTxDurationMillis >= 0
                && mControllerRxDurationMillis >= 0
+39 −25
Original line number Diff line number Diff line
@@ -567,39 +567,41 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync {
        final long lastRxMs = mLastInfo.getControllerRxDurationMillis();
        final long lastEnergy = mLastInfo.getControllerEnergyUsedMicroJoules();

        // We will modify the last info object to be the delta, and store the new
        // WifiActivityEnergyInfo object as our last one.
        final WifiActivityEnergyInfo delta = mLastInfo;
        delta.setTimeSinceBootMillis(latest.getTimeSinceBootMillis());
        delta.setStackState(latest.getStackState());
        final long deltaTimeSinceBootMillis = latest.getTimeSinceBootMillis();
        final int deltaStackState = latest.getStackState();
        final long deltaControllerTxDurationMillis;
        final long deltaControllerRxDurationMillis;
        final long deltaControllerScanDurationMillis;
        final long deltaControllerIdleDurationMillis;
        final long deltaControllerEnergyUsedMicroJoules;

        final long txTimeMs = latest.getControllerTxDurationMillis() - lastTxMs;
        final long rxTimeMs = latest.getControllerRxDurationMillis() - lastRxMs;
        final long idleTimeMs = latest.getControllerIdleDurationMillis() - lastIdleMs;
        final long scanTimeMs = latest.getControllerScanDurationMillis() - lastScanMs;

        final boolean wasReset;
        if (txTimeMs < 0 || rxTimeMs < 0 || scanTimeMs < 0 || idleTimeMs < 0) {
            // The stats were reset by the WiFi system (which is why our delta is negative).
            // Returns the unaltered stats. The total on time should not exceed the time
            // duartion between reports.
            // duration between reports.
            final long totalOnTimeMs = latest.getControllerTxDurationMillis()
                    + latest.getControllerRxDurationMillis()
                    + latest.getControllerIdleDurationMillis();
            if (totalOnTimeMs <= timePeriodMs + MAX_WIFI_STATS_SAMPLE_ERROR_MILLIS) {
                delta.setControllerEnergyUsedMicroJoules(
                        latest.getControllerEnergyUsedMicroJoules());
                delta.setControllerRxDurationMillis(latest.getControllerRxDurationMillis());
                delta.setControllerTxDurationMillis(latest.getControllerTxDurationMillis());
                delta.setControllerIdleDurationMillis(latest.getControllerIdleDurationMillis());
                delta.setControllerScanDurationMillis(latest.getControllerScanDurationMillis());
                deltaControllerEnergyUsedMicroJoules = latest.getControllerEnergyUsedMicroJoules();
                deltaControllerRxDurationMillis = latest.getControllerRxDurationMillis();
                deltaControllerTxDurationMillis = latest.getControllerTxDurationMillis();
                deltaControllerIdleDurationMillis = latest.getControllerIdleDurationMillis();
                deltaControllerScanDurationMillis = latest.getControllerScanDurationMillis();
            } else {
                delta.setControllerEnergyUsedMicroJoules(0);
                delta.setControllerRxDurationMillis(0);
                delta.setControllerTxDurationMillis(0);
                delta.setControllerIdleDurationMillis(0);
                delta.setControllerScanDurationMillis(0);
                deltaControllerEnergyUsedMicroJoules = 0;
                deltaControllerRxDurationMillis = 0;
                deltaControllerTxDurationMillis = 0;
                deltaControllerIdleDurationMillis = 0;
                deltaControllerScanDurationMillis = 0;
            }
            Slog.v(TAG, "WiFi energy data was reset, new WiFi energy data is " + delta);
            wasReset = true;
        } else {
            final long totalActiveTimeMs = txTimeMs + rxTimeMs;
            long maxExpectedIdleTimeMs;
@@ -634,21 +636,33 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync {
                maxExpectedIdleTimeMs = timePeriodMs - totalActiveTimeMs;
            }
            // These times seem to be the most reliable.
            delta.setControllerTxDurationMillis(txTimeMs);
            delta.setControllerRxDurationMillis(rxTimeMs);
            delta.setControllerScanDurationMillis(scanTimeMs);
            deltaControllerTxDurationMillis = txTimeMs;
            deltaControllerRxDurationMillis = rxTimeMs;
            deltaControllerScanDurationMillis = scanTimeMs;
            // WiFi calculates the idle time as a difference from the on time and the various
            // Rx + Tx times. There seems to be some missing time there because this sometimes
            // becomes negative. Just cap it at 0 and ensure that it is less than the expected idle
            // time from the difference in timestamps.
            // b/21613534
            delta.setControllerIdleDurationMillis(
                    Math.min(maxExpectedIdleTimeMs, Math.max(0, idleTimeMs)));
            delta.setControllerEnergyUsedMicroJoules(
                    Math.max(0, latest.getControllerEnergyUsedMicroJoules() - lastEnergy));
            deltaControllerIdleDurationMillis =
                    Math.min(maxExpectedIdleTimeMs, Math.max(0, idleTimeMs));
            deltaControllerEnergyUsedMicroJoules =
                    Math.max(0, latest.getControllerEnergyUsedMicroJoules() - lastEnergy);
            wasReset = false;
        }

        mLastInfo = latest;
        WifiActivityEnergyInfo delta = new WifiActivityEnergyInfo(
                deltaTimeSinceBootMillis,
                deltaStackState,
                deltaControllerTxDurationMillis,
                deltaControllerRxDurationMillis,
                deltaControllerScanDurationMillis,
                deltaControllerIdleDurationMillis,
                deltaControllerEnergyUsedMicroJoules);
        if (wasReset) {
            Slog.v(TAG, "WiFi energy data was reset, new WiFi energy data is " + delta);
        }
        return delta;
    }
}