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

Commit 20689682 authored by lifr's avatar lifr Committed by Frank
Browse files

[DU03-1]Remove INetworkStatsService from BatteryStatsImpl

Expose systemapi NetworkStats.getDetailedUidStats for use by
BatteryStats.

BatteryStatsImpl is using INetworkStatsService APIs, which
cannot be accessed after moving into the mainline module. So, replace
and remove those hidden API usages.

Bug: 210066922
Test: atest BatteryStatsImplTest WifiPowerCalculatorTest
            MobileRadioPowerCalculatorTest NetworkStatsServiceTest
CTS-Coverage-Bug: 213437796
Change-Id: I40d713923278f4654d67bb4d12155cea85c10447
parent d1dfefff
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1850,6 +1850,8 @@ package android.app.usage {
  }
  public class NetworkStatsManager {
    method @NonNull @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public android.net.NetworkStats getMobileUidStats();
    method @NonNull @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public android.net.NetworkStats getWifiUidStats();
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STATS_PROVIDER, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void registerNetworkStatsProvider(@NonNull String, @NonNull android.net.netstats.provider.NetworkStatsProvider);
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STATS_PROVIDER, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void unregisterNetworkStatsProvider(@NonNull android.net.netstats.provider.NetworkStatsProvider);
  }
+11 −13
Original line number Diff line number Diff line
@@ -136,9 +136,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
@@ -11541,13 +11539,15 @@ public class BatteryStatsImpl extends BatteryStats {
    private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
    @VisibleForTesting
    protected NetworkStats readNetworkStatsLocked(@NonNull NetworkStatsManager networkStatsManager,
            String[] ifaces) {
        Objects.requireNonNull(networkStatsManager);
        if (!ArrayUtils.isEmpty(ifaces)) {
            return networkStatsManager.getDetailedUidStats(Set.of(ifaces));
    protected NetworkStats readMobileNetworkStatsLocked(
            @NonNull NetworkStatsManager networkStatsManager) {
        return networkStatsManager.getMobileUidStats();
    }
        return null;
    @VisibleForTesting
    protected NetworkStats readWifiNetworkStatsLocked(
            @NonNull NetworkStatsManager networkStatsManager) {
        return networkStatsManager.getWifiUidStats();
    }
    /**
@@ -11564,8 +11564,7 @@ public class BatteryStatsImpl extends BatteryStats {
        // Grab a separate lock to acquire the network stats, which may do I/O.
        NetworkStats delta = null;
        synchronized (mWifiNetworkLock) {
            final NetworkStats latestStats = readNetworkStatsLocked(networkStatsManager,
                    mWifiIfaces);
            final NetworkStats latestStats = readWifiNetworkStatsLocked(networkStatsManager);
            if (latestStats != null) {
                delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
                        mNetworkStatsPool.acquire());
@@ -11932,8 +11931,7 @@ public class BatteryStatsImpl extends BatteryStats {
        // Grab a separate lock to acquire the network stats, which may do I/O.
        NetworkStats delta = null;
        synchronized (mModemNetworkLock) {
            final NetworkStats latestStats = readNetworkStatsLocked(networkStatsManager,
                    mModemIfaces);
            final NetworkStats latestStats = readMobileNetworkStatsLocked(networkStatsManager);
            if (latestStats != null) {
                delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
                        mNetworkStatsPool.acquire());
+8 −2
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import java.util.concurrent.Future;
public class MockBatteryStatsImpl extends BatteryStatsImpl {
    public BatteryStatsImpl.Clocks clocks;
    public boolean mForceOnBattery;
    // The mNetworkStats will be used for both wifi and mobile categories
    private NetworkStats mNetworkStats;

    MockBatteryStatsImpl(Clocks clocks) {
@@ -107,11 +108,16 @@ public class MockBatteryStatsImpl extends BatteryStatsImpl {
    }

    @Override
    protected NetworkStats readNetworkStatsLocked(@NonNull NetworkStatsManager networkStatsManager,
            String[] ifaces) {
    protected NetworkStats readMobileNetworkStatsLocked(
            @NonNull NetworkStatsManager networkStatsManager) {
        return mNetworkStats;
    }

    @Override
    protected NetworkStats readWifiNetworkStatsLocked(
            @NonNull NetworkStatsManager networkStatsManager) {
        return mNetworkStats;
    }
    public MockBatteryStatsImpl setPowerProfile(PowerProfile powerProfile) {
        mPowerProfile = powerProfile;
        return this;
+38 −14
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package android.app.usage;

import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;

import android.Manifest;
import android.annotation.NonNull;
@@ -55,7 +57,6 @@ import com.android.net.module.util.NetworkIdentityUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Provides access to network usage history and statistics. Usage data is collected in
@@ -670,26 +671,49 @@ public class NetworkStatsManager {
    }

    /**
     * Query realtime network usage statistics details with interfaces constrains.
     * Return snapshot of current UID statistics, including any {@link TrafficStats#UID_TETHERING},
     * video calling data usage and count of network operations that set by
     * {@link TrafficStats#incrementOperationCount}. The returned data doesn't include any
     * statistics that is reported by {@link NetworkStatsProvider}.
     * Query realtime mobile network usage statistics.
     *
     * @param requiredIfaces A list of interfaces the stats should be restricted to, or
     *               {@link NetworkStats#INTERFACES_ALL}.
     * Return a snapshot of current UID network statistics, as it applies
     * to the mobile radios of the device. The snapshot will include any
     * tethering traffic, video calling data usage and count of
     * network operations set by {@link TrafficStats#incrementOperationCount}
     * made over a mobile radio.
     * The snapshot will not include any statistics that cannot be seen by
     * the kernel, e.g. statistics reported by {@link NetworkStatsProvider}s.
     *
     * @hide
     */
    //@SystemApi
    @SystemApi
    @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
    @NonNull public android.net.NetworkStats getMobileUidStats() {
        try {
            return mService.getUidStatsForTransport(TRANSPORT_CELLULAR);
        } catch (RemoteException e) {
            if (DBG) Log.d(TAG, "Remote exception when get Mobile uid stats");
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Query realtime Wi-Fi network usage statistics.
     *
     * Return a snapshot of current UID network statistics, as it applies
     * to the Wi-Fi radios of the device. The snapshot will include any
     * tethering traffic, video calling data usage and count of
     * network operations set by {@link TrafficStats#incrementOperationCount}
     * made over a Wi-Fi radio.
     * The snapshot will not include any statistics that cannot be seen by
     * the kernel, e.g. statistics reported by {@link NetworkStatsProvider}s.
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
    @NonNull public android.net.NetworkStats getDetailedUidStats(
                @NonNull Set<String> requiredIfaces) {
        Objects.requireNonNull(requiredIfaces, "requiredIfaces cannot be null");
    @NonNull public android.net.NetworkStats getWifiUidStats() {
        try {
            return mService.getDetailedUidStats(requiredIfaces.toArray(new String[0]));
            return mService.getUidStatsForTransport(TRANSPORT_WIFI);
        } catch (RemoteException e) {
            if (DBG) Log.d(TAG, "Remote exception when get detailed uid stats");
            if (DBG) Log.d(TAG, "Remote exception when get WiFi uid stats");
            throw e.rethrowFromSystemServer();
        }
    }
+2 −8
Original line number Diff line number Diff line
@@ -49,14 +49,8 @@ interface INetworkStatsService {
    @UnsupportedAppUsage
    NetworkStats getDataLayerSnapshotForUid(int uid);

    /** Get a detailed snapshot of stats since boot for all UIDs.
    *
    * <p>Results will not always be limited to stats on requiredIfaces when specified: stats for
    * interfaces stacked on the specified interfaces, or for interfaces on which the specified
    * interfaces are stacked on, will also be included.
    * @param requiredIfaces Interface names to get data for, or {@link NetworkStats#INTERFACES_ALL}.
    */
    NetworkStats getDetailedUidStats(in String[] requiredIfaces);
    /** Get the transport NetworkStats for all UIDs since boot. */
    NetworkStats getUidStatsForTransport(int transport);

    /** Return set of any ifaces associated with mobile networks since boot. */
    @UnsupportedAppUsage
Loading