[SM04] Support fetching data with NetworkTemplate with subType

Add a NetworkTemplate build function that allows user to specify
subType. NetworkStats corresponding to the same group would
be retrieved.

Test: atest FrameworksNetTests
Bug: 129082217

Change-Id: Ie2d229be0b6bd239f799989c070475c73a096d71
Merged-In: Ie2d229be0b6bd239f799989c070475c73a096d71
(cherry picked from commit e19045cc4ab1f2cd133ce5dc0c3a38e3275417ad)
This commit is contained in:
junyulai
2019-12-20 16:35:34 +08:00
committed by Junyu Lai
parent 825f0828ac
commit 00d94e3335

View File

@@ -34,10 +34,13 @@ import static android.net.NetworkStats.ROAMING_NO;
import static android.net.NetworkStats.ROAMING_YES; import static android.net.NetworkStats.ROAMING_YES;
import static android.net.wifi.WifiInfo.sanitizeSsid; import static android.net.wifi.WifiInfo.sanitizeSsid;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.telephony.Annotation.NetworkType;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.BackupUtils; import android.util.BackupUtils;
import android.util.Log; import android.util.Log;
@@ -74,6 +77,14 @@ public class NetworkTemplate implements Parcelable {
public static final int MATCH_BLUETOOTH = 8; public static final int MATCH_BLUETOOTH = 8;
public static final int MATCH_PROXY = 9; public static final int MATCH_PROXY = 9;
/**
* Include all network types when filtering. This is meant to merge in with the
* {@code TelephonyManager.NETWORK_TYPE_*} constants, and thus needs to stay in sync.
*
* @hide
*/
public static final int NETWORK_TYPE_ALL = -1;
private static boolean isKnownMatchRule(final int rule) { private static boolean isKnownMatchRule(final int rule) {
switch (rule) { switch (rule) {
case MATCH_MOBILE: case MATCH_MOBILE:
@@ -118,7 +129,22 @@ public class NetworkTemplate implements Parcelable {
} }
/** /**
* Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, * Template to match cellular networks with the given IMSI and {@code ratType}.
* Use {@link #NETWORK_TYPE_ALL} to include all network types when filtering.
* See {@code TelephonyManager.NETWORK_TYPE_*}.
*/
public static NetworkTemplate buildTemplateMobileWithRatType(@Nullable String subscriberId,
@NetworkType int ratType) {
if (TextUtils.isEmpty(subscriberId)) {
return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null, null,
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType);
}
return new NetworkTemplate(MATCH_MOBILE, subscriberId, new String[]{subscriberId}, null,
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType);
}
/**
* Template to match metered {@link ConnectivityManager#TYPE_MOBILE} networks,
* regardless of IMSI. * regardless of IMSI.
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@@ -127,7 +153,7 @@ public class NetworkTemplate implements Parcelable {
} }
/** /**
* Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, * Template to match all metered {@link ConnectivityManager#TYPE_WIFI} networks,
* regardless of SSID. * regardless of SSID.
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@@ -193,6 +219,7 @@ public class NetworkTemplate implements Parcelable {
private final int mMetered; private final int mMetered;
private final int mRoaming; private final int mRoaming;
private final int mDefaultNetwork; private final int mDefaultNetwork;
private final int mSubType;
@UnsupportedAppUsage @UnsupportedAppUsage
public NetworkTemplate(int matchRule, String subscriberId, String networkId) { public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
@@ -202,11 +229,11 @@ public class NetworkTemplate implements Parcelable {
public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
String networkId) { String networkId) {
this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL, this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL); DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL);
} }
public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
String networkId, int metered, int roaming, int defaultNetwork) { String networkId, int metered, int roaming, int defaultNetwork, int subType) {
mMatchRule = matchRule; mMatchRule = matchRule;
mSubscriberId = subscriberId; mSubscriberId = subscriberId;
mMatchSubscriberIds = matchSubscriberIds; mMatchSubscriberIds = matchSubscriberIds;
@@ -214,6 +241,7 @@ public class NetworkTemplate implements Parcelable {
mMetered = metered; mMetered = metered;
mRoaming = roaming; mRoaming = roaming;
mDefaultNetwork = defaultNetwork; mDefaultNetwork = defaultNetwork;
mSubType = subType;
if (!isKnownMatchRule(matchRule)) { if (!isKnownMatchRule(matchRule)) {
Log.e(TAG, "Unknown network template rule " + matchRule Log.e(TAG, "Unknown network template rule " + matchRule
@@ -229,6 +257,7 @@ public class NetworkTemplate implements Parcelable {
mMetered = in.readInt(); mMetered = in.readInt();
mRoaming = in.readInt(); mRoaming = in.readInt();
mDefaultNetwork = in.readInt(); mDefaultNetwork = in.readInt();
mSubType = in.readInt();
} }
@Override @Override
@@ -240,6 +269,7 @@ public class NetworkTemplate implements Parcelable {
dest.writeInt(mMetered); dest.writeInt(mMetered);
dest.writeInt(mRoaming); dest.writeInt(mRoaming);
dest.writeInt(mDefaultNetwork); dest.writeInt(mDefaultNetwork);
dest.writeInt(mSubType);
} }
@Override @Override
@@ -272,13 +302,16 @@ public class NetworkTemplate implements Parcelable {
builder.append(", defaultNetwork=").append(NetworkStats.defaultNetworkToString( builder.append(", defaultNetwork=").append(NetworkStats.defaultNetworkToString(
mDefaultNetwork)); mDefaultNetwork));
} }
if (mSubType != NETWORK_TYPE_ALL) {
builder.append(", subType=").append(mSubType);
}
return builder.toString(); return builder.toString();
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming, return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming,
mDefaultNetwork); mDefaultNetwork, mSubType);
} }
@Override @Override
@@ -290,7 +323,8 @@ public class NetworkTemplate implements Parcelable {
&& Objects.equals(mNetworkId, other.mNetworkId) && Objects.equals(mNetworkId, other.mNetworkId)
&& mMetered == other.mMetered && mMetered == other.mMetered
&& mRoaming == other.mRoaming && mRoaming == other.mRoaming
&& mDefaultNetwork == other.mDefaultNetwork; && mDefaultNetwork == other.mDefaultNetwork
&& mSubType == other.mSubType;
} }
return false; return false;
} }
@@ -377,6 +411,11 @@ public class NetworkTemplate implements Parcelable {
|| (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork); || (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork);
} }
private boolean matchesCollapsedRatType(NetworkIdentity ident) {
return mSubType == NETWORK_TYPE_ALL
|| getCollapsedRatType(mSubType) == getCollapsedRatType(ident.mSubType);
}
public boolean matchesSubscriberId(String subscriberId) { public boolean matchesSubscriberId(String subscriberId) {
return ArrayUtils.contains(mMatchSubscriberIds, subscriberId); return ArrayUtils.contains(mMatchSubscriberIds, subscriberId);
} }
@@ -389,9 +428,13 @@ public class NetworkTemplate implements Parcelable {
// TODO: consider matching against WiMAX subscriber identity // TODO: consider matching against WiMAX subscriber identity
return true; return true;
} else { } 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 (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered)) return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered))
&& !ArrayUtils.isEmpty(mMatchSubscriberIds) && !ArrayUtils.isEmpty(mMatchSubscriberIds)
&& ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId); && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId)
&& matchesCollapsedRatType(ident);
} }
} }
@@ -461,7 +504,8 @@ public class NetworkTemplate implements Parcelable {
if (ident.mType == TYPE_WIMAX) { if (ident.mType == TYPE_WIMAX) {
return true; return true;
} else { } else {
return sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered); return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered))
&& matchesCollapsedRatType(ident);
} }
} }