diff --git a/framework-t/src/android/net/NetworkTemplate.java b/framework-t/src/android/net/NetworkTemplate.java index b2da3719c0..ebbd70c564 100644 --- a/framework-t/src/android/net/NetworkTemplate.java +++ b/framework-t/src/android/net/NetworkTemplate.java @@ -258,7 +258,6 @@ public final class NetworkTemplate implements Parcelable { } private final int mMatchRule; - private final String mSubscriberId; /** * Ugh, templates are designed to target a single subscriber, but we might @@ -313,7 +312,7 @@ public final class NetworkTemplate implements Parcelable { // 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, new String[] { subscriberId }, + this(matchRule, new String[] { subscriberId }, wifiNetworkKey != null ? new String[] { wifiNetworkKey } : new String[0], (matchRule == MATCH_MOBILE || matchRule == MATCH_MOBILE_WILDCARD || matchRule == MATCH_CARRIER) ? METERED_YES : METERED_ALL, @@ -321,13 +320,12 @@ public final class NetworkTemplate implements Parcelable { } /** @hide */ - public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, - String[] matchWifiNetworkKeys, int metered, int roaming, - int defaultNetwork, int ratType, int oemManaged) { + public NetworkTemplate(int matchRule, String[] matchSubscriberIds, + String[] matchWifiNetworkKeys, int metered, int roaming, int defaultNetwork, + int ratType, int oemManaged) { Objects.requireNonNull(matchWifiNetworkKeys); Objects.requireNonNull(matchSubscriberIds); mMatchRule = matchRule; - mSubscriberId = subscriberId; mMatchSubscriberIds = matchSubscriberIds; mMatchWifiNetworkKeys = matchWifiNetworkKeys; mMetered = metered; @@ -344,7 +342,6 @@ public final class NetworkTemplate implements Parcelable { private NetworkTemplate(Parcel in) { mMatchRule = in.readInt(); - mSubscriberId = in.readString(); mMatchSubscriberIds = in.createStringArray(); mMatchWifiNetworkKeys = in.createStringArray(); mMetered = in.readInt(); @@ -357,7 +354,6 @@ public final class NetworkTemplate implements Parcelable { @Override public void writeToParcel(@NonNull Parcel dest, int flags) { dest.writeInt(mMatchRule); - dest.writeString(mSubscriberId); dest.writeStringArray(mMatchSubscriberIds); dest.writeStringArray(mMatchWifiNetworkKeys); dest.writeInt(mMetered); @@ -376,10 +372,6 @@ public final class NetworkTemplate implements Parcelable { public String toString() { final StringBuilder builder = new StringBuilder("NetworkTemplate: "); builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); - if (mSubscriberId != null) { - builder.append(", subscriberId=").append( - NetworkIdentityUtils.scrubSubscriberId(mSubscriberId)); - } if (mMatchSubscriberIds != null) { builder.append(", matchSubscriberIds=").append( Arrays.toString(NetworkIdentityUtils.scrubSubscriberIds(mMatchSubscriberIds))); @@ -406,7 +398,7 @@ public final class NetworkTemplate implements Parcelable { @Override public int hashCode() { - return Objects.hash(mMatchRule, mSubscriberId, Arrays.hashCode(mMatchWifiNetworkKeys), + return Objects.hash(mMatchRule, Arrays.hashCode(mMatchWifiNetworkKeys), mMetered, mRoaming, mDefaultNetwork, mRatType, mOemManaged); } @@ -415,7 +407,6 @@ public final class NetworkTemplate implements Parcelable { if (obj instanceof NetworkTemplate) { final NetworkTemplate other = (NetworkTemplate) obj; return mMatchRule == other.mMatchRule - && Objects.equals(mSubscriberId, other.mSubscriberId) && mMetered == other.mMetered && mRoaming == other.mRoaming && mDefaultNetwork == other.mDefaultNetwork @@ -456,12 +447,15 @@ public final class NetworkTemplate implements Parcelable { /** * Get subscriber Id of the template. + * + * @deprecated User should get a subscriberId by {@link #getSubscriberIds} instead. * @hide */ + @Deprecated @Nullable @UnsupportedAppUsage public String getSubscriberId() { - return mSubscriberId; + return CollectionUtils.isEmpty(mMatchSubscriberIds) ? null : mMatchSubscriberIds[0]; } /** @@ -775,15 +769,16 @@ public final class NetworkTemplate implements Parcelable { // information. For instances: // The TYPE_WIFI with subscriberId means that it is a merged carrier wifi network. // The TYPE_CARRIER means that the network associate to specific carrier network. - if (template.mSubscriberId == null) return template; - if (CollectionUtils.contains(merged, template.mSubscriberId)) { + if (CollectionUtils.isEmpty(template.mMatchSubscriberIds)) return template; + + if (CollectionUtils.contains(merged, template.mMatchSubscriberIds[0])) { // Requested template subscriber is part of the merge group; return // a template that matches all merged subscribers. final String[] matchWifiNetworkKeys = template.mMatchWifiNetworkKeys; // TODO: Use NetworkTemplate.Builder to build a template after NetworkTemplate // could handle incompatible subscriberIds. See b/217805241. - return new NetworkTemplate(template.mMatchRule, merged[0], merged, + return new NetworkTemplate(template.mMatchRule, merged, CollectionUtils.isEmpty(matchWifiNetworkKeys) ? new String[0] : new String[] { matchWifiNetworkKeys[0] }, (template.mMatchRule == MATCH_MOBILE @@ -1002,7 +997,6 @@ public final class NetworkTemplate implements Parcelable { public NetworkTemplate build() { assertRequestableParameters(); return new NetworkTemplate(getWildcardDeducedMatchRule(), - mMatchSubscriberIds.isEmpty() ? null : mMatchSubscriberIds.iterator().next(), mMatchSubscriberIds.toArray(new String[0]), mMatchWifiNetworkKeys.toArray(new String[0]), mMetered, mRoaming, mDefaultNetwork, mRatType, mOemManaged); diff --git a/tests/common/java/android/net/netstats/NetworkTemplateTest.kt b/tests/common/java/android/net/netstats/NetworkTemplateTest.kt index 3c2340cd98..eb282f0da5 100644 --- a/tests/common/java/android/net/netstats/NetworkTemplateTest.kt +++ b/tests/common/java/android/net/netstats/NetworkTemplateTest.kt @@ -75,10 +75,9 @@ class NetworkTemplateTest { listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule -> NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1)) .setMeteredness(METERED_YES).build().let { - val expectedTemplate = NetworkTemplate(matchRule, TEST_IMSI1, - arrayOf(TEST_IMSI1), emptyArray(), METERED_YES, - ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, - OEM_MANAGED_ALL) + val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1), + emptyArray(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, + NETWORK_TYPE_ALL, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) } } @@ -88,10 +87,9 @@ class NetworkTemplateTest { listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule -> NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1)) .setRoaming(ROAMING_YES).setMeteredness(METERED_YES).build().let { - val expectedTemplate = NetworkTemplate(matchRule, TEST_IMSI1, - arrayOf(TEST_IMSI1), emptyArray(), METERED_YES, - ROAMING_YES, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, - OEM_MANAGED_ALL) + val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1), + emptyArray(), METERED_YES, ROAMING_YES, DEFAULT_NETWORK_ALL, + NETWORK_TYPE_ALL, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) } } @@ -104,8 +102,8 @@ class NetworkTemplateTest { // Verify template which matches metered cellular networks, // regardless of IMSI. See buildTemplateMobileWildcard. NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build().let { - val expectedTemplate = NetworkTemplate(MATCH_MOBILE_WILDCARD, null /*subscriberId*/, - emptyArray() /*subscriberIds*/, emptyArray(), + val expectedTemplate = NetworkTemplate(MATCH_MOBILE_WILDCARD, + emptyArray() /*subscriberIds*/, emptyArray() /*wifiNetworkKey*/, METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) @@ -116,30 +114,28 @@ class NetworkTemplateTest { NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(TEST_IMSI1)) .setMeteredness(METERED_YES).setRatType(TelephonyManager.NETWORK_TYPE_UMTS) .build().let { - val expectedTemplate = NetworkTemplate(MATCH_MOBILE, TEST_IMSI1, - arrayOf(TEST_IMSI1), emptyArray(), METERED_YES, - ROAMING_ALL, DEFAULT_NETWORK_ALL, TelephonyManager.NETWORK_TYPE_UMTS, - OEM_MANAGED_ALL) + val expectedTemplate = NetworkTemplate(MATCH_MOBILE, arrayOf(TEST_IMSI1), + emptyArray(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, + TelephonyManager.NETWORK_TYPE_UMTS, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) } // Verify template which matches all wifi networks, // regardless of Wifi Network Key. See buildTemplateWifiWildcard and buildTemplateWifi. NetworkTemplate.Builder(MATCH_WIFI).build().let { - val expectedTemplate = NetworkTemplate(MATCH_WIFI_WILDCARD, null /*subscriberId*/, - emptyArray() /*subscriberIds*/, emptyArray(), - METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, - OEM_MANAGED_ALL) + val expectedTemplate = NetworkTemplate(MATCH_WIFI_WILDCARD, + emptyArray() /*subscriberIds*/, emptyArray(), METERED_ALL, + ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) } // Verify template which matches wifi networks with the given Wifi Network Key. // See buildTemplateWifi(wifiNetworkKey). NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let { - val expectedTemplate = NetworkTemplate(MATCH_WIFI, null /*subscriberId*/, - emptyArray() /*subscriberIds*/, arrayOf(TEST_WIFI_KEY1), - METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, - OEM_MANAGED_ALL) + val expectedTemplate = + NetworkTemplate(MATCH_WIFI, emptyArray() /*subscriberIds*/, + arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, + NETWORK_TYPE_ALL, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) } @@ -147,10 +143,9 @@ class NetworkTemplateTest { // given Wifi Network Key, and IMSI. See buildTemplateWifi(wifiNetworkKey, subscriberId). NetworkTemplate.Builder(MATCH_WIFI).setSubscriberIds(setOf(TEST_IMSI1)) .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let { - val expectedTemplate = NetworkTemplate(MATCH_WIFI, TEST_IMSI1, - arrayOf(TEST_IMSI1), arrayOf(TEST_WIFI_KEY1), - METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, - OEM_MANAGED_ALL) + val expectedTemplate = NetworkTemplate(MATCH_WIFI, arrayOf(TEST_IMSI1), + arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, + NETWORK_TYPE_ALL, OEM_MANAGED_ALL) assertEquals(expectedTemplate, it) } @@ -158,7 +153,7 @@ class NetworkTemplateTest { // See buildTemplateEthernet and buildTemplateBluetooth. listOf(MATCH_ETHERNET, MATCH_BLUETOOTH).forEach { matchRule -> NetworkTemplate.Builder(matchRule).build().let { - val expectedTemplate = NetworkTemplate(matchRule, null /*subscriberId*/, + val expectedTemplate = NetworkTemplate(matchRule, emptyArray() /*subscriberIds*/, emptyArray(), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL) @@ -193,7 +188,7 @@ class NetworkTemplateTest { // Verify template which matches wifi wildcard with the given empty key set. NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf()).build().let { - val expectedTemplate = NetworkTemplate(MATCH_WIFI_WILDCARD, null /*subscriberId*/, + val expectedTemplate = NetworkTemplate(MATCH_WIFI_WILDCARD, emptyArray() /*subscriberIds*/, emptyArray(), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL) diff --git a/tests/unit/java/android/net/NetworkTemplateTest.kt b/tests/unit/java/android/net/NetworkTemplateTest.kt index c3440c5c15..2fb4a8b3b9 100644 --- a/tests/unit/java/android/net/NetworkTemplateTest.kt +++ b/tests/unit/java/android/net/NetworkTemplateTest.kt @@ -447,18 +447,18 @@ class NetworkTemplateTest { @Test fun testParcelUnparcel() { - val templateMobile = NetworkTemplate(MATCH_MOBILE, TEST_IMSI1, arrayOf(TEST_IMSI1), + val templateMobile = NetworkTemplate(MATCH_MOBILE, arrayOf(TEST_IMSI1), emptyArray(), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, TelephonyManager.NETWORK_TYPE_LTE, OEM_MANAGED_ALL) - val templateWifi = NetworkTemplate(MATCH_WIFI, null, emptyArray(), + val templateWifi = NetworkTemplate(MATCH_WIFI, emptyArray(), arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, 0, OEM_MANAGED_ALL) - val templateOem = NetworkTemplate(MATCH_MOBILE_WILDCARD, null, emptyArray(), + val templateOem = NetworkTemplate(MATCH_MOBILE_WILDCARD, emptyArray(), emptyArray(), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, 0, OEM_MANAGED_YES) - assertParcelSane(templateMobile, 9) - assertParcelSane(templateWifi, 9) - assertParcelSane(templateOem, 9) + assertParcelSane(templateMobile, 8) + assertParcelSane(templateWifi, 8) + assertParcelSane(templateOem, 8) } // Verify NETWORK_TYPE_* constants in NetworkTemplate do not conflict with @@ -510,10 +510,10 @@ class NetworkTemplateTest { val matchSubscriberIds = arrayOf(subscriberId) val matchWifiNetworkKeys = arrayOf(templateWifiKey) - val templateOemYes = NetworkTemplate(matchType, subscriberId, matchSubscriberIds, + val templateOemYes = NetworkTemplate(matchType, matchSubscriberIds, matchWifiNetworkKeys, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_YES) - val templateOemAll = NetworkTemplate(matchType, subscriberId, matchSubscriberIds, + val templateOemAll = NetworkTemplate(matchType, matchSubscriberIds, matchWifiNetworkKeys, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL) @@ -524,7 +524,7 @@ class NetworkTemplateTest { // Create a template with each OEM managed type and match it against the NetworkIdentity for (templateOemManagedState in oemManagedStates) { - val template = NetworkTemplate(matchType, subscriberId, matchSubscriberIds, + val template = NetworkTemplate(matchType, matchSubscriberIds, matchWifiNetworkKeys, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, templateOemManagedState) if (identityOemManagedState == templateOemManagedState) {