From c9be32e9f103a5703ed6a77dc4e61954d732ea36 Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Thu, 27 Jan 2022 00:03:41 +0800 Subject: [PATCH] Move the implement of getAllCollapsedRatTypes to StatsPullAtomService To make data usage as a mainline module, move getAllCollapsedRatTypes to StatsPullAtomService since currently it is the only user. Also, the method needs to call getCollapsedRatType, thus move getCollapsedRatType to NetworkStatsManager and expose it as module API. Bug: 210073043 Test: builds, FrameworksNetTests Change-Id: Ibe41b50f173464694c21dd22841552bdb69a6a14 --- core/api/module-lib-current.txt | 1 + .../app/usage/NetworkStatsManager.java | 48 +++++++++++ .../src/android/net/NetworkTemplate.java | 86 +------------------ .../server/net/NetworkStatsService.java | 2 +- .../net/NetworkStatsSubscriptionsMonitor.java | 4 +- .../stats/pull/StatsPullAtomService.java | 22 ++++- 6 files changed, 75 insertions(+), 88 deletions(-) diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 9d115eb78773a..4dd7bf9cdc080 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -55,6 +55,7 @@ package android.app.usage { public class NetworkStatsManager { method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void forceUpdate(); + method public static int getCollapsedRatType(int); method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void notifyNetworkStatus(@NonNull java.util.List, @NonNull java.util.List, @Nullable String, @NonNull java.util.List); method @NonNull @WorkerThread public android.app.usage.NetworkStats queryDetailsForDevice(@NonNull android.net.NetworkTemplate, long, long); method @NonNull @WorkerThread public android.app.usage.NetworkStats queryDetailsForUidTagState(@NonNull android.net.NetworkTemplate, long, long, int, int, int) throws java.lang.SecurityException; diff --git a/packages/ConnectivityT/framework-t/src/android/app/usage/NetworkStatsManager.java b/packages/ConnectivityT/framework-t/src/android/app/usage/NetworkStatsManager.java index d7e513f1211d3..84adef53e488e 100644 --- a/packages/ConnectivityT/framework-t/src/android/app/usage/NetworkStatsManager.java +++ b/packages/ConnectivityT/framework-t/src/android/app/usage/NetworkStatsManager.java @@ -1123,4 +1123,52 @@ public class NetworkStatsManager { throw e.rethrowFromSystemServer(); } } + + /** + * Get a RAT type representative of a group of RAT types for network statistics. + * + * Collapse the given Radio Access Technology (RAT) type into a bucket that + * is representative of the original RAT type for network statistics. The + * mapping mostly corresponds to {@code TelephonyManager#NETWORK_CLASS_BIT_MASK_*} + * but with adaptations specific to the virtual types introduced by + * networks stats. + * + * @param ratType An integer defined in {@code TelephonyManager#NETWORK_TYPE_*}. + * + * @hide + */ + @SystemApi(client = MODULE_LIBRARIES) + public static int getCollapsedRatType(int ratType) { + switch (ratType) { + case TelephonyManager.NETWORK_TYPE_GPRS: + case TelephonyManager.NETWORK_TYPE_GSM: + case TelephonyManager.NETWORK_TYPE_EDGE: + case TelephonyManager.NETWORK_TYPE_IDEN: + case TelephonyManager.NETWORK_TYPE_CDMA: + case TelephonyManager.NETWORK_TYPE_1xRTT: + return TelephonyManager.NETWORK_TYPE_GSM; + case TelephonyManager.NETWORK_TYPE_EVDO_0: + case TelephonyManager.NETWORK_TYPE_EVDO_A: + case TelephonyManager.NETWORK_TYPE_EVDO_B: + case TelephonyManager.NETWORK_TYPE_EHRPD: + case TelephonyManager.NETWORK_TYPE_UMTS: + case TelephonyManager.NETWORK_TYPE_HSDPA: + case TelephonyManager.NETWORK_TYPE_HSUPA: + case TelephonyManager.NETWORK_TYPE_HSPA: + case TelephonyManager.NETWORK_TYPE_HSPAP: + case TelephonyManager.NETWORK_TYPE_TD_SCDMA: + return TelephonyManager.NETWORK_TYPE_UMTS; + case TelephonyManager.NETWORK_TYPE_LTE: + case TelephonyManager.NETWORK_TYPE_IWLAN: + return TelephonyManager.NETWORK_TYPE_LTE; + case TelephonyManager.NETWORK_TYPE_NR: + return TelephonyManager.NETWORK_TYPE_NR; + // Virtual RAT type for 5G NSA mode, see + // {@link NetworkStatsManager#NETWORK_TYPE_5G_NSA}. + case NetworkStatsManager.NETWORK_TYPE_5G_NSA: + return NetworkStatsManager.NETWORK_TYPE_5G_NSA; + default: + return TelephonyManager.NETWORK_TYPE_UNKNOWN; + } + } } diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java index 649f54b09e61a..27e717fb59de6 100644 --- a/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java +++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java @@ -48,7 +48,6 @@ import android.os.Build; import android.os.Parcel; import android.os.Parcelable; import android.telephony.Annotation.NetworkType; -import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.ArraySet; @@ -59,9 +58,7 @@ import com.android.net.module.util.NetworkStatsUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Arrays; -import java.util.Collection; import java.util.Comparator; -import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; @@ -703,7 +700,8 @@ public final class NetworkTemplate implements Parcelable { private boolean matchesCollapsedRatType(NetworkIdentity ident) { return mRatType == NETWORK_TYPE_ALL - || getCollapsedRatType(mRatType) == getCollapsedRatType(ident.mRatType); + || NetworkStatsManager.getCollapsedRatType(mRatType) + == NetworkStatsManager.getCollapsedRatType(ident.mRatType); } /** @@ -746,86 +744,6 @@ public final class NetworkTemplate implements Parcelable { } } - /** - * Get a Radio Access Technology(RAT) type that is representative of a group of RAT types. - * The mapping is corresponding to {@code TelephonyManager#NETWORK_CLASS_BIT_MASK_*}. - * - * @param ratType An integer defined in {@code TelephonyManager#NETWORK_TYPE_*}. - * - * @hide - */ - // TODO: 1. Consider move this to TelephonyManager if used by other modules. - // 2. Consider make this configurable. - // 3. Use TelephonyManager APIs when available. - // TODO: @SystemApi when ready. - public static int getCollapsedRatType(int ratType) { - switch (ratType) { - case TelephonyManager.NETWORK_TYPE_GPRS: - case TelephonyManager.NETWORK_TYPE_GSM: - case TelephonyManager.NETWORK_TYPE_EDGE: - case TelephonyManager.NETWORK_TYPE_IDEN: - case TelephonyManager.NETWORK_TYPE_CDMA: - case TelephonyManager.NETWORK_TYPE_1xRTT: - return TelephonyManager.NETWORK_TYPE_GSM; - case TelephonyManager.NETWORK_TYPE_EVDO_0: - case TelephonyManager.NETWORK_TYPE_EVDO_A: - case TelephonyManager.NETWORK_TYPE_EVDO_B: - case TelephonyManager.NETWORK_TYPE_EHRPD: - case TelephonyManager.NETWORK_TYPE_UMTS: - case TelephonyManager.NETWORK_TYPE_HSDPA: - case TelephonyManager.NETWORK_TYPE_HSUPA: - case TelephonyManager.NETWORK_TYPE_HSPA: - case TelephonyManager.NETWORK_TYPE_HSPAP: - case TelephonyManager.NETWORK_TYPE_TD_SCDMA: - return TelephonyManager.NETWORK_TYPE_UMTS; - case TelephonyManager.NETWORK_TYPE_LTE: - case TelephonyManager.NETWORK_TYPE_IWLAN: - return TelephonyManager.NETWORK_TYPE_LTE; - case TelephonyManager.NETWORK_TYPE_NR: - return TelephonyManager.NETWORK_TYPE_NR; - // Virtual RAT type for 5G NSA mode, see - // {@link NetworkStatsManager#NETWORK_TYPE_5G_NSA}. - case NetworkStatsManager.NETWORK_TYPE_5G_NSA: - return NetworkStatsManager.NETWORK_TYPE_5G_NSA; - default: - return TelephonyManager.NETWORK_TYPE_UNKNOWN; - } - } - - /** - * Return all supported collapsed RAT types that could be returned by - * {@link #getCollapsedRatType(int)}. - * - * @hide - */ - // TODO: @SystemApi when ready. - @NonNull - public static final int[] getAllCollapsedRatTypes() { - final int[] ratTypes = TelephonyManager.getAllNetworkTypes(); - final HashSet collapsedRatTypes = new HashSet<>(); - for (final int ratType : ratTypes) { - collapsedRatTypes.add(NetworkTemplate.getCollapsedRatType(ratType)); - } - // Add NETWORK_TYPE_5G_NSA to the returned list since 5G NSA is a virtual RAT type and - // it is not in TelephonyManager#NETWORK_TYPE_* constants. - // See {@link NetworkStatsManager#NETWORK_TYPE_5G_NSA}. - collapsedRatTypes.add( - NetworkTemplate.getCollapsedRatType(NetworkStatsManager.NETWORK_TYPE_5G_NSA)); - // Ensure that unknown type is returned. - collapsedRatTypes.add(TelephonyManager.NETWORK_TYPE_UNKNOWN); - return toIntArray(collapsedRatTypes); - } - - @NonNull - private static int[] toIntArray(@NonNull Collection list) { - final int[] array = new int[list.size()]; - int i = 0; - for (final Integer item : list) { - array[i++] = item; - } - return array; - } - /** * Check if matches Wi-Fi network template. */ diff --git a/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java b/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java index 243d62164705b..5c069e9d4022e 100644 --- a/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java +++ b/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java @@ -244,7 +244,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { /** * When enabled, all mobile data is reported under {@link NetworkTemplate#NETWORK_TYPE_ALL}. * When disabled, mobile data is broken down by a granular ratType representative of the - * actual ratType. {@see NetworkTemplate#getCollapsedRatType}. + * actual ratType. {@see android.app.usage.NetworkStatsManager#getCollapsedRatType}. * Enabling this decreases the level of detail but saves performance, disk space and * amount of data logged. */ diff --git a/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java b/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java index b6c3af5a7bebd..3e35e603e87ef 100644 --- a/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java +++ b/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java @@ -17,7 +17,7 @@ package com.android.server.net; import static android.app.usage.NetworkStatsManager.NETWORK_TYPE_5G_NSA; -import static android.net.NetworkTemplate.getCollapsedRatType; +import static android.app.usage.NetworkStatsManager.getCollapsedRatType; import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED; import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA; import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE; @@ -57,7 +57,7 @@ public class NetworkStatsSubscriptionsMonitor extends * * @param subscriberId IMSI of the subscription. * @param collapsedRatType collapsed RAT type. - * @see android.net.NetworkTemplate#getCollapsedRatType(int). + * @see android.app.usage.NetworkStatsManager#getCollapsedRatType(int). */ void onCollapsedRatTypeChanged(@NonNull String subscriberId, @Annotation.NetworkType int collapsedRatType); diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java index c0989657f5e46..1fa948cef2161 100644 --- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java @@ -29,7 +29,6 @@ import static android.net.NetworkTemplate.MATCH_WIFI; import static android.net.NetworkTemplate.OEM_MANAGED_ALL; import static android.net.NetworkTemplate.OEM_MANAGED_PAID; import static android.net.NetworkTemplate.OEM_MANAGED_PRIVATE; -import static android.net.NetworkTemplate.getAllCollapsedRatTypes; import static android.os.Debug.getIonHeapsSizeKb; import static android.os.Process.LAST_SHARED_APPLICATION_GID; import static android.os.Process.getUidForPid; @@ -1383,6 +1382,27 @@ public class StatsPullAtomService extends SystemService { return ret; } + /** + * Return all supported collapsed RAT types that could be returned by + * {@link android.app.usage.NetworkStatsManager#getCollapsedRatType(int)}. + */ + @NonNull + private static int[] getAllCollapsedRatTypes() { + final int[] ratTypes = TelephonyManager.getAllNetworkTypes(); + final HashSet collapsedRatTypes = new HashSet<>(); + for (final int ratType : ratTypes) { + collapsedRatTypes.add(NetworkStatsManager.getCollapsedRatType(ratType)); + } + // Add NETWORK_TYPE_5G_NSA to the returned list since 5G NSA is a virtual RAT type and + // it is not in TelephonyManager#NETWORK_TYPE_* constants. + // See {@link NetworkStatsManager#NETWORK_TYPE_5G_NSA}. + collapsedRatTypes.add( + NetworkStatsManager.getCollapsedRatType(NetworkStatsManager.NETWORK_TYPE_5G_NSA)); + // Ensure that unknown type is returned. + collapsedRatTypes.add(TelephonyManager.NETWORK_TYPE_UNKNOWN); + return com.android.net.module.util.CollectionUtils.toIntArray(collapsedRatTypes); + } + @NonNull private NetworkStats sliceNetworkStatsByUid(@NonNull NetworkStats stats) { return sliceNetworkStats(stats, (entry) -> {