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

Commit 71ecd94b authored by Ivo Kay's avatar Ivo Kay Committed by Android (Google) Code Review
Browse files

Merge "Update nullability annotation for NetworkStats atom queries" into main

parents 91d09b9f 9a486fc3
Loading
Loading
Loading
Loading
+30 −46
Original line number Diff line number Diff line
@@ -1275,49 +1275,39 @@ public class StatsPullAtomService extends SystemService {
            case FrameworkStatsLog.WIFI_BYTES_TRANSFER: {
                final NetworkStats stats = getUidNetworkStatsSnapshotForTransportLocked(
                        TRANSPORT_WIFI);
                if (stats != null) {
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUid(stats),
                        new int[]{TRANSPORT_WIFI}, /*slicedByFgbg=*/false));
                }
                break;
            }
            case FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: {
                final NetworkStats stats = getUidNetworkStatsSnapshotForTransportLocked(
                        TRANSPORT_WIFI);
                if (stats != null) {
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats),
                        new int[]{TRANSPORT_WIFI}, /*slicedByFgbg=*/true));
                }
                break;
            }
            case FrameworkStatsLog.MOBILE_BYTES_TRANSFER: {
                final NetworkStats stats =
                        getUidNetworkStatsSnapshotForTransportLocked(TRANSPORT_CELLULAR);
                if (stats != null) {
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUid(stats),
                        new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/false));
                }
                break;
            }
            case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: {
                final NetworkStats stats =
                        getUidNetworkStatsSnapshotForTransportLocked(TRANSPORT_CELLULAR);
                if (stats != null) {
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats),
                        new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true));
                }
                break;
            }
            case FrameworkStatsLog.PROXY_BYTES_TRANSFER_BY_FG_BG: {
                final NetworkStats stats = getUidNetworkStatsSnapshotForTemplateLocked(
                        new NetworkTemplate.Builder(MATCH_PROXY).build(),  /*includeTags=*/false);
                if (stats != null) {
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats),
                        new int[]{TRANSPORT_BLUETOOTH},
                        /*slicedByFgbg=*/true, /*slicedByTag=*/false,
                        /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN,
                        /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/true));
                }
                break;
            }
            case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: {
@@ -1326,14 +1316,12 @@ public class StatsPullAtomService extends SystemService {
                final NetworkStats cellularStats = getUidNetworkStatsSnapshotForTemplateLocked(
                        new NetworkTemplate.Builder(MATCH_MOBILE)
                                .setMeteredness(METERED_YES).build(), /*includeTags=*/true);
                if (wifiStats != null && cellularStats != null) {
                final NetworkStats stats = wifiStats.add(cellularStats);
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUidTagAndMetered(stats),
                        new int[]{TRANSPORT_WIFI, TRANSPORT_CELLULAR},
                        /*slicedByFgbg=*/false, /*slicedByTag=*/true,
                        /*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN,
                        /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/false));
                }
                break;
            }
            case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER: {
@@ -1519,14 +1507,12 @@ public class StatsPullAtomService extends SystemService {
                final NetworkStats stats = getUidNetworkStatsSnapshotForTemplateLocked(
                        template, false);
                final Integer transport = ruleAndTransport.second;
                if (stats != null) {
                ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats),
                        new int[]{transport}, /*slicedByFgbg=*/true, /*slicedByTag=*/false,
                        /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN,
                        /*subInfo=*/null, oemManaged, /*isTypeProxy=*/false));
            }
        }
        }

        return ret;
    }
@@ -1535,7 +1521,7 @@ public class StatsPullAtomService extends SystemService {
     * Create a snapshot of NetworkStats for a given transport.
     */
    @GuardedBy("mDataBytesTransferLock")
    @Nullable
    @NonNull
    private NetworkStats getUidNetworkStatsSnapshotForTransportLocked(int transport) {
        NetworkTemplate template = null;
        switch (transport) {
@@ -1574,7 +1560,7 @@ public class StatsPullAtomService extends SystemService {
     * some traffic before boot.
     */
    @GuardedBy("mDataBytesTransferLock")
    @Nullable
    @NonNull
    private NetworkStats getUidNetworkStatsSnapshotForTemplateLocked(
            @NonNull NetworkTemplate template, boolean includeTags) {
        final long elapsedMillisSinceBoot = SystemClock.elapsedRealtime();
@@ -1613,7 +1599,7 @@ public class StatsPullAtomService extends SystemService {
    }

    @GuardedBy("mDataBytesTransferLock")
    @Nullable
    @NonNull
    private NetworkStats getUidNetworkStatsSnapshotForTemplateLocked(
            @NonNull NetworkTemplate template, boolean includeTags, long startTime, long endTime) {
        final long elapsedMillisSinceBoot = SystemClock.elapsedRealtime();
@@ -1660,13 +1646,11 @@ public class StatsPullAtomService extends SystemService {
                            .setMeteredness(METERED_YES).build();
            final NetworkStats stats =
                    getUidNetworkStatsSnapshotForTemplateLocked(template, /*includeTags=*/false);
            if (stats != null) {
            ret.add(new NetworkStatsExt(sliceNetworkStatsByFgbg(stats),
                    new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true,
                    /*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo,
                    OEM_MANAGED_ALL, /*isTypeProxy=*/false));
        }
        }
        return ret;
    }

+6 −9
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.stats.pull.netstats;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.NetworkStats;
import android.net.NetworkTemplate;

@@ -55,7 +54,7 @@ public class NetworkStatsAccumulator {
     * This method method may call {@code queryFunction} more than once, which includes maintaining
     * an internal cumulative stats snapshot and querying stats after the snapshot.
     */
    @Nullable
    @NonNull
    public NetworkStats queryStats(long currentTimeMillis,
            @NonNull StatsQueryFunction queryFunction) {
        maybeExpandSnapshot(currentTimeMillis, queryFunction);
@@ -80,23 +79,21 @@ public class NetworkStatsAccumulator {
        if (newEndTimeMillis - mSnapshotEndTimeMillis > mBucketDurationMillis) {
            NetworkStats extraStats = queryFunction.queryNetworkStats(mTemplate, mWithTags,
                    mSnapshotEndTimeMillis, newEndTimeMillis);
            if (extraStats != null) {
            mSnapshot = mSnapshot.add(extraStats);
            mSnapshotEndTimeMillis = newEndTimeMillis;
        }
    }
    }

    /**
     * Adds up stats in the internal cumulative snapshot and the stats that follow after it.
     */
    @Nullable
    @NonNull
    private NetworkStats snapshotPlusFollowingStats(long currentTimeMillis,
            @NonNull StatsQueryFunction queryFunction) {
        // Set end time in the future to include all stats in the active bucket.
        NetworkStats extraStats = queryFunction.queryNetworkStats(mTemplate, mWithTags,
                mSnapshotEndTimeMillis, currentTimeMillis + mBucketDurationMillis);
        return extraStats != null ? mSnapshot.add(extraStats) : null;
        return mSnapshot.add(extraStats);
    }

    @FunctionalInterface
@@ -104,7 +101,7 @@ public class NetworkStatsAccumulator {
        /**
         * Returns network stats during the given time period.
         */
        @Nullable
        @NonNull
        NetworkStats queryNetworkStats(@NonNull NetworkTemplate template, boolean includeTags,
                long startTime, long endTime);
    }
+11 −11
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ class NetworkStatsAccumulatorTest {

        // Accumulator has data until 1000 (= 0), and its end-point is still in the history period.
        // Current time is less than one bucket away from snapshot end-point: 1050 - 1000 < 200
        val stats = snapshot.queryStats(1050, FakeStats(500, 1050, 1))!!
        val stats = snapshot.queryStats(1050, FakeStats(500, 1050, 1))

        // After the query at 1050, accumulator should have 1 * (1050 - 1000) = 50 bytes.
        assertNetworkStatsEquals(stats, networkStatsWithBytes(50))
@@ -72,7 +72,7 @@ class NetworkStatsAccumulatorTest {

        // Accumulator has data until 1000 (= 0), and its end-point is still in the history period.
        // Current time is one bucket away from snapshot end-point: 1250 - 1000 > 200
        val stats = snapshot.queryStats(1250, FakeStats(550, 1250, 2))!!
        val stats = snapshot.queryStats(1250, FakeStats(550, 1250, 2))

        // After the query at 1250, accumulator should have 2 * (1250 - 1000) = 500 bytes.
        assertNetworkStatsEquals(stats, networkStatsWithBytes(500))
@@ -84,7 +84,7 @@ class NetworkStatsAccumulatorTest {

        // Accumulator has data until 1000 (= 0), and its end-point is in the history period.
        // Current time is two buckets away from snapshot end-point: 1450 - 1000 > 2*200
        val stats = snapshot.queryStats(1450, FakeStats(600, 1450, 3))!!
        val stats = snapshot.queryStats(1450, FakeStats(600, 1450, 3))

        // After the query at 1450, accumulator should have 3 * (1450 - 1000) = 1350 bytes.
        assertNetworkStatsEquals(stats, networkStatsWithBytes(1350))
@@ -96,7 +96,7 @@ class NetworkStatsAccumulatorTest {

        // Accumulator has data until 1000 (= 0), and its end-point is still in the history period.
        // Current time is many buckets away from snapshot end-point
        val stats = snapshot.queryStats(6100, FakeStats(900, 6100, 1))!!
        val stats = snapshot.queryStats(6100, FakeStats(900, 6100, 1))

        // After the query at 6100, accumulator should have 1 * (6100 - 1000) = 5100 bytes.
        assertNetworkStatsEquals(stats, networkStatsWithBytes(5100))
@@ -108,9 +108,9 @@ class NetworkStatsAccumulatorTest {

        // Accumulator is queried within the history period, whose starting point stays the same.
        // After each query, accumulator should contain bytes from the initial end-point until now.
        val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1))!!
        val stats2 = snapshot.queryStats(10100, FakeStats(900, 10100, 1))!!
        val stats3 = snapshot.queryStats(15100, FakeStats(900, 15100, 1))!!
        val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1))
        val stats2 = snapshot.queryStats(10100, FakeStats(900, 10100, 1))
        val stats3 = snapshot.queryStats(15100, FakeStats(900, 15100, 1))

        assertNetworkStatsEquals(stats1, networkStatsWithBytes(4100))
        assertNetworkStatsEquals(stats2, networkStatsWithBytes(9100))
@@ -123,9 +123,9 @@ class NetworkStatsAccumulatorTest {

        // Accumulator is queried within the history period, whose starting point is moving.
        // After each query, accumulator should contain bytes from the initial end-point until now.
        val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1))!!
        val stats2 = snapshot.queryStats(10100, FakeStats(4000, 10100, 1))!!
        val stats3 = snapshot.queryStats(15100, FakeStats(7000, 15100, 1))!!
        val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1))
        val stats2 = snapshot.queryStats(10100, FakeStats(4000, 10100, 1))
        val stats3 = snapshot.queryStats(15100, FakeStats(7000, 15100, 1))

        assertNetworkStatsEquals(stats1, networkStatsWithBytes(4100))
        assertNetworkStatsEquals(stats2, networkStatsWithBytes(9100))
@@ -138,7 +138,7 @@ class NetworkStatsAccumulatorTest {

        // Accumulator has data until 1000 (= 0), but its end-point is not in the history period.
        // After the query, accumulator should add only those bytes that are covered by the history.
        val stats = snapshot.queryStats(2700, FakeStats(2200, 2700, 1))!!
        val stats = snapshot.queryStats(2700, FakeStats(2200, 2700, 1))

        assertNetworkStatsEquals(stats, networkStatsWithBytes(500))
    }