diff --git a/framework-t/src/android/net/NetworkTemplate.java b/framework-t/src/android/net/NetworkTemplate.java index b6bd1a51a2..2a5e7be755 100644 --- a/framework-t/src/android/net/NetworkTemplate.java +++ b/framework-t/src/android/net/NetworkTemplate.java @@ -47,7 +47,6 @@ import android.net.wifi.WifiInfo; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; -import android.text.TextUtils; import android.util.ArraySet; import com.android.internal.annotations.VisibleForTesting; @@ -59,7 +58,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Arrays; import java.util.Comparator; -import java.util.List; import java.util.Objects; import java.util.Set; import java.util.SortedSet; @@ -197,149 +195,67 @@ public final class NetworkTemplate implements Parcelable { * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with * the given IMSI. * + * @deprecated Use {@link Builder} to build a template. * @hide */ - @UnsupportedAppUsage - public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { - return new NetworkTemplate(MATCH_MOBILE, subscriberId, null); - } - - /** - * Template to match cellular networks with the given IMSI, {@code ratType} and - * {@code metered}. Use {@link #NETWORK_TYPE_ALL} to include all network types when - * filtering. See {@code TelephonyManager.NETWORK_TYPE_*}. - * - * @hide - */ - public static NetworkTemplate buildTemplateMobileWithRatType(@Nullable String subscriberId, - int ratType, int metered) { - if (TextUtils.isEmpty(subscriberId)) { - return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null /* subscriberId */, - null /* matchSubscriberIds */, - new String[0] /* matchWifiNetworkKeys */, metered, ROAMING_ALL, - DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL, - NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT); - } - return new NetworkTemplate(MATCH_MOBILE, subscriberId, new String[] { subscriberId }, - new String[0] /* matchWifiNetworkKeys */, - metered, ROAMING_ALL, DEFAULT_NETWORK_ALL, ratType, OEM_MANAGED_ALL, - NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT); + @Deprecated + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU, + publicAlternatives = "Use {@code Builder} instead.") + public static NetworkTemplate buildTemplateMobileAll(@NonNull String subscriberId) { + return new NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES) + .setSubscriberIds(Set.of(subscriberId)).build(); } /** * Template to match metered {@link ConnectivityManager#TYPE_MOBILE} networks, * regardless of IMSI. * + * @deprecated Use {@link Builder} to build a template. * @hide */ + @Deprecated @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public static NetworkTemplate buildTemplateMobileWildcard() { - return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null); + return new NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build(); } /** * Template to match all metered {@link ConnectivityManager#TYPE_WIFI} networks, * regardless of key of the wifi network. * + * @deprecated Use {@link Builder} to build a template. * @hide */ - @UnsupportedAppUsage + @Deprecated + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU, + publicAlternatives = "Use {@code Builder} instead.") public static NetworkTemplate buildTemplateWifiWildcard() { - // TODO: Consider replace this with MATCH_WIFI with NETWORK_ID_ALL - // and SUBSCRIBER_ID_MATCH_RULE_ALL. - return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null); + return new NetworkTemplate.Builder(MATCH_WIFI).build(); } - /** @hide */ + /** + * @deprecated Use {@link Builder} to build a template. + * @hide + */ @Deprecated - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU, + publicAlternatives = "Use {@code Builder} instead.") public static NetworkTemplate buildTemplateWifi() { return buildTemplateWifiWildcard(); } - /** - * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the - * given key of the wifi network. - * - * @param wifiNetworkKey key of the wifi network. see {@link WifiInfo#getNetworkKey()} - * to know details about the key. - * @hide - */ - public static NetworkTemplate buildTemplateWifi(@NonNull String wifiNetworkKey) { - Objects.requireNonNull(wifiNetworkKey); - return new NetworkTemplate(MATCH_WIFI, null /* subscriberId */, - new String[] { null } /* matchSubscriberIds */, - new String[] { wifiNetworkKey }, METERED_ALL, ROAMING_ALL, - DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL, - NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_ALL); - } - - /** - * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks with the given - * key of the wifi network and IMSI. - * - * Call with {@link #WIFI_NETWORK_KEY_ALL} for {@code wifiNetworkKey} to get result regardless - * of key of the wifi network. - * - * @param wifiNetworkKey key of the wifi network. see {@link WifiInfo#getNetworkKey()} - * to know details about the key. - * @param subscriberId the IMSI associated to this wifi network. - * - * @hide - */ - public static NetworkTemplate buildTemplateWifi(@Nullable String wifiNetworkKey, - @Nullable String subscriberId) { - return new NetworkTemplate(MATCH_WIFI, subscriberId, new String[] { subscriberId }, - wifiNetworkKey != null - ? new String[] { wifiNetworkKey } : new String[0], - METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL, - NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT); - } - /** * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style * networks together. * + * @deprecated Use {@link Builder} to build a template. * @hide */ - @UnsupportedAppUsage + @Deprecated + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU, + publicAlternatives = "Use {@code Builder} instead.") public static NetworkTemplate buildTemplateEthernet() { - return new NetworkTemplate(MATCH_ETHERNET, null, null); - } - - /** - * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style - * networks together. - * - * @hide - */ - public static NetworkTemplate buildTemplateBluetooth() { - return new NetworkTemplate(MATCH_BLUETOOTH, null, null); - } - - /** - * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style - * networks together. - * - * @hide - */ - public static NetworkTemplate buildTemplateProxy() { - return new NetworkTemplate(MATCH_PROXY, null, null); - } - - /** - * Template to match all metered carrier networks with the given IMSI. - * - * @hide - */ - public static NetworkTemplate buildTemplateCarrierMetered(@NonNull String subscriberId) { - Objects.requireNonNull(subscriberId); - return new NetworkTemplate(MATCH_CARRIER, subscriberId, - new String[] { subscriberId }, - new String[0] /* matchWifiNetworkKeys */, - METERED_YES, ROAMING_ALL, - DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL, - NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT); + return new NetworkTemplate.Builder(MATCH_ETHERNET).build(); } private final int mMatchRule; @@ -387,9 +303,13 @@ public final class NetworkTemplate implements Parcelable { } } - /** @hide */ - // TODO: Deprecate this constructor, mark it @UnsupportedAppUsage(maxTargetSdk = S) - @UnsupportedAppUsage + /** + * @deprecated Use {@link Builder} to build a template. + * @hide + */ + @Deprecated + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU, + publicAlternatives = "Use {@code Builder} instead.") public NetworkTemplate(int matchRule, String subscriberId, String wifiNetworkKey) { this(matchRule, subscriberId, new String[] { subscriberId }, wifiNetworkKey); } @@ -409,17 +329,6 @@ public final class NetworkTemplate implements Parcelable { OEM_MANAGED_ALL, NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT); } - /** @hide */ - // TODO: Remove it after updating all of the caller. - public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, - String wifiNetworkKey, int metered, int roaming, int defaultNetwork, int ratType, - int oemManaged) { - this(matchRule, subscriberId, matchSubscriberIds, - wifiNetworkKey != null ? new String[] { wifiNetworkKey } : new String[0], - metered, roaming, defaultNetwork, ratType, oemManaged, - NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT); - } - /** @hide */ public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, String[] matchWifiNetworkKeys, int metered, int roaming, @@ -881,48 +790,29 @@ public final class NetworkTemplate implements Parcelable { * For example, given an incoming template matching B, and the currently * active merge set [A,B], we'd return a new template that primarily matches * A, but also matches B. - * TODO: remove and use {@link #normalize(NetworkTemplate, List)}. * * @hide */ - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU, + publicAlternatives = "There is no alternative for {@code NetworkTemplate.normalize}." + + "Callers should have their own logic to merge template for" + + " different IMSIs and stop calling this function.") public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) { - return normalize(template, Arrays.asList(merged)); - } - - /** - * Examine the given template and normalize it. - * We pick the "lowest" merged subscriber as the primary - * for key purposes, and expand the template to match all other merged - * subscribers. - * - * There can be multiple merged subscriberIds for multi-SIM devices. - * - *

- * For example, given an incoming template matching B, and the currently - * active merge set [A,B], we'd return a new template that primarily matches - * A, but also matches B. - * - * @hide - */ - // TODO: @SystemApi when ready. - public static NetworkTemplate normalize(NetworkTemplate template, List mergedList) { // Now there are several types of network which uses SubscriberId to store network // 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; - for (String[] merged : mergedList) { - if (CollectionUtils.contains(merged, template.mSubscriberId)) { - // Requested template subscriber is part of the merge group; return - // a template that matches all merged subscribers. - final String[] matchWifiNetworkKeys = template.mMatchWifiNetworkKeys; - return new NetworkTemplate(template.mMatchRule, merged[0], merged, - CollectionUtils.isEmpty(matchWifiNetworkKeys) - ? null : matchWifiNetworkKeys[0]); - } + if (CollectionUtils.contains(merged, template.mSubscriberId)) { + // 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, + CollectionUtils.isEmpty(matchWifiNetworkKeys) + ? null : matchWifiNetworkKeys[0]); } return template; diff --git a/tests/unit/java/android/net/NetworkTemplateTest.kt b/tests/unit/java/android/net/NetworkTemplateTest.kt index 666da538bf..c10ba93aa6 100644 --- a/tests/unit/java/android/net/NetworkTemplateTest.kt +++ b/tests/unit/java/android/net/NetworkTemplateTest.kt @@ -32,6 +32,7 @@ import android.net.NetworkStats.METERED_ALL import android.net.NetworkStats.METERED_NO import android.net.NetworkStats.METERED_YES import android.net.NetworkStats.ROAMING_ALL +import android.net.NetworkTemplate.MATCH_CARRIER import android.net.NetworkTemplate.MATCH_MOBILE import android.net.NetworkTemplate.MATCH_MOBILE_WILDCARD import android.net.NetworkTemplate.MATCH_TEST @@ -41,12 +42,8 @@ import android.net.NetworkTemplate.NETWORK_TYPE_ALL import android.net.NetworkTemplate.OEM_MANAGED_ALL import android.net.NetworkTemplate.OEM_MANAGED_NO import android.net.NetworkTemplate.OEM_MANAGED_YES -import android.net.NetworkTemplate.WIFI_NETWORK_KEY_ALL -import android.net.NetworkTemplate.buildTemplateCarrierMetered import android.net.NetworkTemplate.buildTemplateMobileAll import android.net.NetworkTemplate.buildTemplateMobileWildcard -import android.net.NetworkTemplate.buildTemplateMobileWithRatType -import android.net.NetworkTemplate.buildTemplateWifi import android.net.NetworkTemplate.buildTemplateWifiWildcard import android.net.NetworkTemplate.normalize import android.net.wifi.WifiInfo @@ -144,10 +141,16 @@ class NetworkTemplateTest { @Test fun testWifiMatches() { - val templateWifiKey1 = buildTemplateWifi(TEST_WIFI_KEY1) - val templateWifiKey1ImsiNull = buildTemplateWifi(TEST_WIFI_KEY1, null) - val templateWifiKey1Imsi1 = buildTemplateWifi(TEST_WIFI_KEY1, TEST_IMSI1) - val templateWifiKeyAllImsi1 = buildTemplateWifi(WIFI_NETWORK_KEY_ALL, TEST_IMSI1) + val templateWifiKey1 = NetworkTemplate.Builder(MATCH_WIFI) + .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build() + val templateWifiKey1ImsiNull = NetworkTemplate.Builder(MATCH_WIFI) + .setSubscriberIds(setOf(null)) + .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build() + val templateWifiKey1Imsi1 = NetworkTemplate.Builder(MATCH_WIFI) + .setSubscriberIds(setOf(TEST_IMSI1)) + .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build() + val templateWifiKeyAllImsi1 = NetworkTemplate.Builder(MATCH_WIFI) + .setSubscriberIds(setOf(TEST_IMSI1)).build() val identMobile1 = buildNetworkIdentity(mockContext, buildMobileNetworkState(TEST_IMSI1), false, TelephonyManager.NETWORK_TYPE_UMTS) @@ -196,8 +199,10 @@ class NetworkTemplateTest { @Test fun testMobileMatches() { val templateMobileImsi1 = buildTemplateMobileAll(TEST_IMSI1) - val templateMobileImsi2WithRatType = buildTemplateMobileWithRatType(TEST_IMSI2, - TelephonyManager.NETWORK_TYPE_UMTS, METERED_YES) + val templateMobileImsi2WithRatType = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_YES) + .setSubscriberIds(setOf(TEST_IMSI2)) + .setRatType(TelephonyManager.NETWORK_TYPE_UMTS).build() val mobileImsi1 = buildNetworkState(TYPE_MOBILE, TEST_IMSI1, null /* wifiKey */, OEM_NONE, true /* metered */) @@ -225,8 +230,8 @@ class NetworkTemplateTest { @Test fun testMobileWildcardMatches() { val templateMobileWildcard = buildTemplateMobileWildcard() - val templateMobileNullImsiWithRatType = buildTemplateMobileWithRatType(null, - TelephonyManager.NETWORK_TYPE_UMTS, METERED_ALL) + val templateMobileNullImsiWithRatType = NetworkTemplate.Builder(MATCH_MOBILE) + .setRatType(TelephonyManager.NETWORK_TYPE_UMTS).build() val mobileImsi1 = buildMobileNetworkState(TEST_IMSI1) val identMobile1 = buildNetworkIdentity(mockContext, mobileImsi1, @@ -277,7 +282,9 @@ class NetworkTemplateTest { @Test fun testCarrierMeteredMatches() { - val templateCarrierImsi1Metered = buildTemplateCarrierMetered(TEST_IMSI1) + val templateCarrierImsi1Metered = NetworkTemplate.Builder(MATCH_CARRIER) + .setMeteredness(METERED_YES) + .setSubscriberIds(setOf(TEST_IMSI1)).build() val mobileImsi1 = buildMobileNetworkState(TEST_IMSI1) val mobileImsi1Unmetered = buildNetworkState(TYPE_MOBILE, TEST_IMSI1, @@ -322,24 +329,25 @@ class NetworkTemplateTest { // Build UMTS template that matches mobile identities with RAT in the same // group with any IMSI. See {@link NetworkTemplate#getCollapsedRatType}. - val templateUmtsMetered = buildTemplateMobileWithRatType(null, - TelephonyManager.NETWORK_TYPE_UMTS, METERED_YES) + val templateUmtsMetered = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_YES) + .setRatType(TelephonyManager.NETWORK_TYPE_UMTS).build() // Build normal template that matches mobile identities with any RAT and IMSI. - val templateAllMetered = buildTemplateMobileWithRatType(null, NETWORK_TYPE_ALL, - METERED_YES) + val templateAllMetered = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_YES).build() // Build template with UNKNOWN RAT that matches mobile identities with RAT that // cannot be determined. - val templateUnknownMetered = - buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_UNKNOWN, - METERED_YES) - - val templateUmtsNonMetered = buildTemplateMobileWithRatType(null, - TelephonyManager.NETWORK_TYPE_UMTS, METERED_NO) - val templateAllNonMetered = buildTemplateMobileWithRatType(null, NETWORK_TYPE_ALL, - METERED_NO) - val templateUnknownNonMetered = - buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_UNKNOWN, - METERED_NO) + val templateUnknownMetered = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_YES) + .setRatType(TelephonyManager.NETWORK_TYPE_UNKNOWN).build() + val templateUmtsNonMetered = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_NO) + .setRatType(TelephonyManager.NETWORK_TYPE_UMTS).build() + val templateAllNonMetered = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_NO).build() + val templateUnknownNonMetered = NetworkTemplate.Builder(MATCH_MOBILE) + .setMeteredness(METERED_NO) + .setRatType(TelephonyManager.NETWORK_TYPE_UNKNOWN).build() val identUmtsMetered = buildNetworkIdentity( mockContext, stateMobileImsi1Metered, false, TelephonyManager.NETWORK_TYPE_UMTS) @@ -553,7 +561,7 @@ class NetworkTemplateTest { @Test fun testNormalize() { - var mergedImsiList = listOf(arrayOf(TEST_IMSI1, TEST_IMSI2)) + var mergedImsiList = arrayOf(TEST_IMSI1, TEST_IMSI2) val identMobileImsi1 = buildNetworkIdentity(mockContext, buildMobileNetworkState(TEST_IMSI1), false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS) @@ -575,12 +583,18 @@ class NetworkTemplateTest { it.assertMatches(identMobileImsi2) it.assertDoesNotMatch(identMobileImsi3) } - normalize(buildTemplateCarrierMetered(TEST_IMSI1), mergedImsiList).also { + val templateCarrierImsi1 = NetworkTemplate.Builder(MATCH_CARRIER) + .setMeteredness(METERED_YES) + .setSubscriberIds(setOf(TEST_IMSI1)).build() + normalize(templateCarrierImsi1, mergedImsiList).also { it.assertMatches(identMobileImsi1) it.assertMatches(identMobileImsi2) it.assertDoesNotMatch(identMobileImsi3) } - normalize(buildTemplateWifi(TEST_WIFI_KEY1, TEST_IMSI1), mergedImsiList).also { + val templateWifiKey1Imsi1 = NetworkTemplate.Builder(MATCH_WIFI) + .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)) + .setSubscriberIds(setOf(TEST_IMSI1)).build() + normalize(templateWifiKey1Imsi1, mergedImsiList).also { it.assertMatches(identWifiImsi1Key1) it.assertMatches(identWifiImsi2Key1) it.assertDoesNotMatch(identWifiImsi3WifiKey1)