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

Commit 26ef6826 authored by Roshan Pius's avatar Roshan Pius
Browse files

WifiActivityEnergyInfo: Use time values for energy calculation

This removes PowerProfile @hide usage in wifi service.

Bug: 145533104
Test: Compiles
Change-Id: I56d366d557f5afe379f4ea0cbbff2c3b72440d99
parent eb7fd8c9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6621,7 +6621,7 @@ package android.os.connectivity {
  }
  public final class WifiActivityEnergyInfo implements android.os.Parcelable {
    ctor public WifiActivityEnergyInfo(long, int, long, long, long, long, long);
    ctor public WifiActivityEnergyInfo(long, int, long, long, long, long);
    method public int describeContents();
    method public long getControllerEnergyUsedMicroJoules();
    method public long getControllerIdleDurationMillis();
+26 −6
Original line number Diff line number Diff line
@@ -19,9 +19,13 @@ package android.os.connectivity;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.app.ActivityThread;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.os.PowerProfile;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@@ -72,7 +76,6 @@ public final class WifiActivityEnergyInfo implements Parcelable {
     * @param scanDurationMillis Cumulative milliseconds when radio is awake due to scan.
     * @param idleDurationMillis Cumulative milliseconds when radio is awake but not transmitting or
     *                       receiving.
     * @param energyUsedMicroJoules Cumulative energy consumed by Wifi, in microjoules.
     */
    public WifiActivityEnergyInfo(
            long timeSinceBootMillis,
@@ -80,14 +83,33 @@ public final class WifiActivityEnergyInfo implements Parcelable {
            long txDurationMillis,
            long rxDurationMillis,
            long scanDurationMillis,
            long idleDurationMillis,
            long energyUsedMicroJoules) {
            long idleDurationMillis) {
        mTimeSinceBootMillis = timeSinceBootMillis;
        mStackState = stackState;
        mControllerTxDurationMillis = txDurationMillis;
        mControllerRxDurationMillis = rxDurationMillis;
        mControllerScanDurationMillis = scanDurationMillis;
        mControllerIdleDurationMillis = idleDurationMillis;

        final Context context = ActivityThread.currentActivityThread().getSystemContext();
        if (context == null) {
            mControllerEnergyUsedMicroJoules = 0L;
            return;
        }
        // Calculate energy used using PowerProfile.
        PowerProfile powerProfile = new PowerProfile(context);
        final double rxIdleCurrent = powerProfile.getAveragePower(
                PowerProfile.POWER_WIFI_CONTROLLER_IDLE);
        final double rxCurrent = powerProfile.getAveragePower(
                PowerProfile.POWER_WIFI_CONTROLLER_RX);
        final double txCurrent = powerProfile.getAveragePower(
                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)
                * voltage);
        mControllerEnergyUsedMicroJoules = energyUsedMicroJoules;
    }

@@ -113,9 +135,8 @@ public final class WifiActivityEnergyInfo implements Parcelable {
            long rxTime = in.readLong();
            long scanTime = in.readLong();
            long idleTime = in.readLong();
            long energyUsed = in.readLong();
            return new WifiActivityEnergyInfo(timestamp, stackState,
                    txTime, rxTime, scanTime, idleTime, energyUsed);
                    txTime, rxTime, scanTime, idleTime);
        }
        public WifiActivityEnergyInfo[] newArray(int size) {
            return new WifiActivityEnergyInfo[size];
@@ -130,7 +151,6 @@ public final class WifiActivityEnergyInfo implements Parcelable {
        out.writeLong(mControllerRxDurationMillis);
        out.writeLong(mControllerScanDurationMillis);
        out.writeLong(mControllerIdleDurationMillis);
        out.writeLong(mControllerEnergyUsedMicroJoules);
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync {
    // Keep the last WiFi stats so we can compute a delta.
    @GuardedBy("mWorkerLock")
    private WifiActivityEnergyInfo mLastInfo =
            new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0, 0);
            new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0);

    /**
     * Timestamp at which all external stats were last collected in
+2 −2
Original line number Diff line number Diff line
@@ -161,7 +161,7 @@ public class WifiManagerTest {
                mRunnable.run();
            }
        };
        mWifiActivityEnergyInfo = new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0, 0);
        mWifiActivityEnergyInfo = new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0);
    }

    /**
@@ -1713,7 +1713,7 @@ public class WifiManagerTest {
    @Test
    public void testGetControllerActivityEnergyInfo() throws Exception {
        WifiActivityEnergyInfo activityEnergyInfo =
                new WifiActivityEnergyInfo(5, 3, 3, 5, 5, 5, 5);
                new WifiActivityEnergyInfo(5, 3, 3, 5, 5, 5);
        when(mWifiService.reportActivityInfo()).thenReturn(activityEnergyInfo);

        assertEquals(activityEnergyInfo, mWifiManager.getControllerActivityEnergyInfo());