From 522918f4baebd6bd37bc6ea314c3c45adf61b565 Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Fri, 19 Mar 2021 22:56:26 +0800 Subject: [PATCH] Move deduceRestrictedCapability to libs/net and rename it NetworkCapabilities is included in framework-connectivity, so external module cannot have dependencies on its hidden API. Move the method to libs/net so that external modules can use it by including the library. Bug: 178777253 Test: FrameworksNetTests (cherry-picked from ag/13921626) Merged-In: I77970b3a5e5e0e9d263639694b1f06519169bf64 Change-Id: I77970b3a5e5e0e9d263639694b1f06519169bf64 --- .../src/android/net/NetworkCapabilities.java | 66 +------------------ .../android/net/NetworkCapabilitiesTest.java | 36 ---------- 2 files changed, 2 insertions(+), 100 deletions(-) diff --git a/framework/src/android/net/NetworkCapabilities.java b/framework/src/android/net/NetworkCapabilities.java index 881fa8c270..49248518d2 100644 --- a/framework/src/android/net/NetworkCapabilities.java +++ b/framework/src/android/net/NetworkCapabilities.java @@ -537,43 +537,6 @@ public final class NetworkCapabilities implements Parcelable { | (1 << NET_CAPABILITY_TRUSTED) | (1 << NET_CAPABILITY_NOT_VPN); - /** - * Capabilities that suggest that a network is restricted. - * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES} - */ - @VisibleForTesting - /* package */ static final long RESTRICTED_CAPABILITIES = - (1 << NET_CAPABILITY_CBS) - | (1 << NET_CAPABILITY_DUN) - | (1 << NET_CAPABILITY_EIMS) - | (1 << NET_CAPABILITY_FOTA) - | (1 << NET_CAPABILITY_IA) - | (1 << NET_CAPABILITY_IMS) - | (1 << NET_CAPABILITY_MCX) - | (1 << NET_CAPABILITY_RCS) - | (1 << NET_CAPABILITY_VEHICLE_INTERNAL) - | (1 << NET_CAPABILITY_XCAP) - | (1 << NET_CAPABILITY_ENTERPRISE); - - /** - * Capabilities that force network to be restricted. - * {@see #maybeMarkCapabilitiesRestricted}. - */ - private static final long FORCE_RESTRICTED_CAPABILITIES = - (1 << NET_CAPABILITY_OEM_PAID) - | (1 << NET_CAPABILITY_OEM_PRIVATE); - - /** - * Capabilities that suggest that a network is unrestricted. - * {@see #maybeMarkCapabilitiesRestricted}. - */ - @VisibleForTesting - /* package */ static final long UNRESTRICTED_CAPABILITIES = - (1 << NET_CAPABILITY_INTERNET) - | (1 << NET_CAPABILITY_MMS) - | (1 << NET_CAPABILITY_SUPL) - | (1 << NET_CAPABILITY_WIFI_P2P); - /** * Capabilities that are managed by ConnectivityService. */ @@ -811,37 +774,12 @@ public final class NetworkCapabilities implements Parcelable { } /** - * Deduces that all the capabilities it provides are typically provided by restricted networks - * or not. - * - * @return {@code true} if the network should be restricted. - * @hide - */ - public boolean deduceRestrictedCapability() { - // Check if we have any capability that forces the network to be restricted. - final boolean forceRestrictedCapability = - (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0; - - // Verify there aren't any unrestricted capabilities. If there are we say - // the whole thing is unrestricted unless it is forced to be restricted. - final boolean hasUnrestrictedCapabilities = - (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0; - - // Must have at least some restricted capabilities. - final boolean hasRestrictedCapabilities = - (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0; - - return forceRestrictedCapability - || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities); - } - - /** - * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted. + * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if inferring the network is restricted. * * @hide */ public void maybeMarkCapabilitiesRestricted() { - if (deduceRestrictedCapability()) { + if (NetworkCapabilitiesUtils.inferRestrictedCapability(this)) { removeCapability(NET_CAPABILITY_NOT_RESTRICTED); } } diff --git a/tests/net/common/java/android/net/NetworkCapabilitiesTest.java b/tests/net/common/java/android/net/NetworkCapabilitiesTest.java index 1f50e31cf2..e7718b546c 100644 --- a/tests/net/common/java/android/net/NetworkCapabilitiesTest.java +++ b/tests/net/common/java/android/net/NetworkCapabilitiesTest.java @@ -38,14 +38,12 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; -import static android.net.NetworkCapabilities.RESTRICTED_CAPABILITIES; import static android.net.NetworkCapabilities.SIGNAL_STRENGTH_UNSPECIFIED; import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; import static android.net.NetworkCapabilities.TRANSPORT_TEST; import static android.net.NetworkCapabilities.TRANSPORT_VPN; import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; -import static android.net.NetworkCapabilities.UNRESTRICTED_CAPABILITIES; import static android.os.Process.INVALID_UID; import static com.android.modules.utils.build.SdkLevel.isAtLeastR; @@ -103,20 +101,6 @@ public class NetworkCapabilitiesTest { @Test public void testMaybeMarkCapabilitiesRestricted() { - // verify EIMS is restricted - assertEquals((1 << NET_CAPABILITY_EIMS) & RESTRICTED_CAPABILITIES, - (1 << NET_CAPABILITY_EIMS)); - - // verify CBS is also restricted - assertEquals((1 << NET_CAPABILITY_CBS) & RESTRICTED_CAPABILITIES, - (1 << NET_CAPABILITY_CBS)); - - // verify default is not restricted - assertEquals((1 << NET_CAPABILITY_INTERNET) & RESTRICTED_CAPABILITIES, 0); - - // just to see - assertEquals(RESTRICTED_CAPABILITIES & UNRESTRICTED_CAPABILITIES, 0); - // check that internet does not get restricted NetworkCapabilities netCap = new NetworkCapabilities(); netCap.addCapability(NET_CAPABILITY_INTERNET); @@ -985,26 +969,6 @@ public class NetworkCapabilitiesTest { assertNotEquals(-50, nc.getSignalStrength()); } - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testDeduceRestrictedCapability() { - final NetworkCapabilities nc = new NetworkCapabilities(); - // Default capabilities don't have restricted capability. - assertFalse(nc.deduceRestrictedCapability()); - // If there is a force restricted capability, then the network capabilities is restricted. - nc.addCapability(NET_CAPABILITY_OEM_PAID); - nc.addCapability(NET_CAPABILITY_INTERNET); - assertTrue(nc.deduceRestrictedCapability()); - // Except for the force restricted capability, if there is any unrestricted capability in - // capabilities, then the network capabilities is not restricted. - nc.removeCapability(NET_CAPABILITY_OEM_PAID); - nc.addCapability(NET_CAPABILITY_CBS); - assertFalse(nc.deduceRestrictedCapability()); - // Except for the force restricted capability, the network capabilities will only be treated - // as restricted when there is no any unrestricted capability. - nc.removeCapability(NET_CAPABILITY_INTERNET); - assertTrue(nc.deduceRestrictedCapability()); - } - private void assertNoTransport(NetworkCapabilities nc) { for (int i = MIN_TRANSPORT; i <= MAX_TRANSPORT; i++) { assertFalse(nc.hasTransport(i));