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

Commit 4004535b authored by David Su's avatar David Su
Browse files

Statsd: Formalize WifiManager#requestActivityInfo

Rename WifiManager#requestActivityInfo to
getWifiActivityEnergyInfoAsync and convert
ResultReceiver to a formal listener interface.

Bug: 145244073
Test: m -j statsd_test && adb root && adb sync data && adb shell data/nativetest64/statsd_test/statsd_test
Test: atest CtsStatsdHostTestCases
Change-Id: I3e5b6f18f18b487683863934c3d0cba7be07a778
parent 453dc24e
Loading
Loading
Loading
Loading
+40 −26
Original line number Diff line number Diff line
@@ -64,8 +64,8 @@ import android.net.INetworkStatsService;
import android.net.Network;
import android.net.NetworkRequest;
import android.net.NetworkStats;
import android.net.wifi.IWifiManager;
import android.net.wifi.WifiActivityEnergyInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryStats;
import android.os.BatteryStatsInternal;
import android.os.Binder;
@@ -170,6 +170,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@@ -342,7 +343,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {

    private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
    private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
    private IWifiManager mWifiManager = null;
    private WifiManager mWifiManager = null;
    private TelephonyManager mTelephony = null;
    @GuardedBy("sStatsdLock")
    private final HashSet<Long> mDeathTimeMillis = new HashSet<>();
@@ -1148,21 +1149,39 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private void pullWifiActivityInfo(
            int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        long token = Binder.clearCallingIdentity();
        WifiManager wifiManager;
        synchronized (this) {
            if (mWifiManager == null) {
                mWifiManager =
                        IWifiManager.Stub.asInterface(
                                ServiceManager.getService(Context.WIFI_SERVICE));
                mWifiManager = mContext.getSystemService(WifiManager.class);
            }
            wifiManager = mWifiManager;
        }
        if (wifiManager == null) {
            return;
        }
        if (mWifiManager != null) {
        long token = Binder.clearCallingIdentity();
        try {
            SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi");
                mWifiManager.requestActivityInfo(wifiReceiver);
            wifiManager.getWifiActivityEnergyInfoAsync(
                    new Executor() {
                        @Override
                        public void execute(Runnable runnable) {
                            // run the listener on the binder thread, if it was run on the main
                            // thread it would deadlock since we would be waiting on ourselves
                            runnable.run();
                        }
                    },
                    info -> {
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, info);
                        wifiReceiver.send(0, bundle);
                    }
            );
            final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver);
                StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos,
                        wallClockNanos);
            if (wifiInfo == null) {
                return;
            }
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            e.writeLong(wifiInfo.getTimeSinceBootMillis());
            e.writeInt(wifiInfo.getStackState());
            e.writeLong(wifiInfo.getControllerTxDurationMillis());
@@ -1170,15 +1189,10 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            e.writeLong(wifiInfo.getControllerIdleDurationMillis());
            e.writeLong(wifiInfo.getControllerEnergyUsedMicroJoules());
            pulledData.add(e);
            } catch (RemoteException e) {
                Slog.e(TAG,
                        "Pulling wifiManager for wifi controller activity energy info has error",
                        e);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }
    }

    private void pullModemActivityInfo(
            int tagId, long elapsedNanos, long wallClockNanos,