From 7cf5cd30445b87d5c31f2b7a4601f4820164d358 Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Mon, 26 Jul 2021 19:49:10 +0800 Subject: [PATCH] Respect metered filter in matchesMobile* In old design, only metered mobile network would be matched, which means that only metered stats could be read if callers query the mobile stats. Currently, the caller in NetworkPolicyManagerService was updated to use the NetworkTemplate constructor with metered parameter in aosp/1563312 which means that the template will be created by a given meteredness. Thus, the hardcode metered can be removed. Remove the metered check from matches method will cause a different behavior. Therefore, to keep the original behavior, if the match rule is MATCH_MOBILE or MATCH_MOBILE_WILDCARD, it should pass METERED_YES to the constructor to build a metered template. Also, if a caller creates a template by calling the constructor with metered as a parameter, the behavior will also be changed. Therefore, if the caller expects to get metered stats then it should change to pass METERED_YES. If the caller expects to get both metered and unmetered stats then it should remain METERED_ALL and it would be a bug that been fixed by this patch. Bug: 183776809 Bug: 202092687 Test: atest FrameworksNetTests:NetworkStatsServiceTest manually test by generating traffic and check the data counts from "adb shell dumpsys netstats --uid" almost equal to the result from "adb shell cmd stats pull-source 10082" Change-Id: I66dd51b020f6d895cd002acc05bef6b6315cd447 Merged-In: I66dd51b020f6d895cd002acc05bef6b6315cd447 --- core/java/android/net/NetworkTemplate.java | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/core/java/android/net/NetworkTemplate.java b/core/java/android/net/NetworkTemplate.java index 08f75df5d8..73c26277ff 100644 --- a/core/java/android/net/NetworkTemplate.java +++ b/core/java/android/net/NetworkTemplate.java @@ -168,11 +168,11 @@ public class NetworkTemplate implements Parcelable { @NetworkType int ratType) { if (TextUtils.isEmpty(subscriberId)) { return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null, null, - METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL, + METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL, SUBSCRIBER_ID_MATCH_RULE_EXACT); } return new NetworkTemplate(MATCH_MOBILE, subscriberId, new String[]{subscriberId}, null, - METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL, + METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL, SUBSCRIBER_ID_MATCH_RULE_EXACT); } @@ -305,6 +305,7 @@ public class NetworkTemplate implements Parcelable { } } + // TODO: Deprecate this constructor, mark it @UnsupportedAppUsage(maxTargetSdk = S) @UnsupportedAppUsage public NetworkTemplate(int matchRule, String subscriberId, String networkId) { this(matchRule, subscriberId, new String[] { subscriberId }, networkId); @@ -312,9 +313,14 @@ public class NetworkTemplate implements Parcelable { public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, String networkId) { - this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL, - DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL, - SUBSCRIBER_ID_MATCH_RULE_EXACT); + // Older versions used to only match MATCH_MOBILE and MATCH_MOBILE_WILDCARD templates + // to metered networks. It is now possible to match mobile with any meteredness, but + // in order to preserve backward compatibility of @UnsupportedAppUsage methods, this + //constructor passes METERED_YES for these types. + this(matchRule, subscriberId, matchSubscriberIds, networkId, + (matchRule == MATCH_MOBILE || matchRule == MATCH_MOBILE_WILDCARD) ? METERED_YES + : METERED_ALL , ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, + OEM_MANAGED_ALL, SUBSCRIBER_ID_MATCH_RULE_EXACT); } // TODO: Remove it after updating all of the caller. @@ -589,11 +595,7 @@ public class NetworkTemplate implements Parcelable { // TODO: consider matching against WiMAX subscriber identity return true; } else { - // Only metered mobile network would be matched regardless of metered filter. - // This is used to exclude non-metered APNs, e.g. IMS. See ag/908650. - // TODO: Respect metered filter and remove mMetered condition. - return (ident.mType == TYPE_MOBILE && ident.mMetered) - && !ArrayUtils.isEmpty(mMatchSubscriberIds) + return ident.mType == TYPE_MOBILE && !ArrayUtils.isEmpty(mMatchSubscriberIds) && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId) && matchesCollapsedRatType(ident); } @@ -707,8 +709,7 @@ public class NetworkTemplate implements Parcelable { if (ident.mType == TYPE_WIMAX) { return true; } else { - return (ident.mType == TYPE_MOBILE && ident.mMetered) - && matchesCollapsedRatType(ident); + return ident.mType == TYPE_MOBILE && matchesCollapsedRatType(ident); } }