diff options
author | Aaron Huang <huangaaron@google.com> | 2021-03-19 23:02:29 +0800 |
---|---|---|
committer | Aaron Huang <huangaaron@google.com> | 2021-03-22 15:47:31 +0000 |
commit | c3e78490734f787f5fddbc85f417cf91e0dbe8bb (patch) | |
tree | 4168833f9c24d8cd0623ecab157158e0ca6e9a12 /common/framework | |
parent | ef8ae7c3f2e1ef80cba179a5c811a78e1b0e57c7 (diff) | |
download | net-c3e78490734f787f5fddbc85f417cf91e0dbe8bb.tar.gz |
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 and rename it to inferRestrictedCapability so that external
module can use it by inculding the library.
Ignore-AOSP-First: to avoid merging conflict to internal
Bug: 178777253
Test: FrameworksNetTests
NetworkStaticLibTests
Change-Id: I51244048e31699b562b1444d88511e3a3da845ec
Diffstat (limited to 'common/framework')
-rw-r--r-- | common/framework/com/android/net/module/util/NetworkCapabilitiesUtils.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/common/framework/com/android/net/module/util/NetworkCapabilitiesUtils.java b/common/framework/com/android/net/module/util/NetworkCapabilitiesUtils.java index 3de78c61..568a3561 100644 --- a/common/framework/com/android/net/module/util/NetworkCapabilitiesUtils.java +++ b/common/framework/com/android/net/module/util/NetworkCapabilitiesUtils.java @@ -16,6 +16,23 @@ package com.android.net.module.util; +import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS; +import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN; +import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS; +import static android.net.NetworkCapabilities.NET_CAPABILITY_ENTERPRISE; +import static android.net.NetworkCapabilities.NET_CAPABILITY_FOTA; +import static android.net.NetworkCapabilities.NET_CAPABILITY_IA; +import static android.net.NetworkCapabilities.NET_CAPABILITY_IMS; +import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; +import static android.net.NetworkCapabilities.NET_CAPABILITY_MCX; +import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS; +import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID; +import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE; +import static android.net.NetworkCapabilities.NET_CAPABILITY_RCS; +import static android.net.NetworkCapabilities.NET_CAPABILITY_SUPL; +import static android.net.NetworkCapabilities.NET_CAPABILITY_VEHICLE_INTERNAL; +import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; +import static android.net.NetworkCapabilities.NET_CAPABILITY_XCAP; import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH; import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; @@ -24,7 +41,9 @@ import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; import android.annotation.NonNull; +import android.net.NetworkCapabilities; +import com.android.internal.annotations.VisibleForTesting; /** * Utilities to examine {@link android.net.NetworkCapabilities}. @@ -55,6 +74,44 @@ public final class NetworkCapabilitiesUtils { }; /** + * Capabilities that suggest that a network is restricted. + * See {@code NetworkCapabilities#maybeMarkCapabilitiesRestricted}, + * and {@code FORCE_RESTRICTED_CAPABILITIES}. + */ + @VisibleForTesting + 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 {@code NetworkCapabilities#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 {@code NetworkCapabilities#maybeMarkCapabilitiesRestricted}. + */ + @VisibleForTesting + static final long UNRESTRICTED_CAPABILITIES = + (1 << NET_CAPABILITY_INTERNET) + | (1 << NET_CAPABILITY_MMS) + | (1 << NET_CAPABILITY_SUPL) + | (1 << NET_CAPABILITY_WIFI_P2P); + + /** * Get a transport that can be used to classify a network when displaying its info to users. * * While networks can have multiple transports, users generally think of them as "wifi", @@ -79,6 +136,33 @@ public final class NetworkCapabilitiesUtils { return transports[0]; } + + /** + * Infers that all the capabilities it provides are typically provided by restricted networks + * or not. + * + * @param nc the {@link NetworkCapabilities} to infer the restricted capabilities. + * + * @return {@code true} if the network should be restricted. + */ + public static boolean inferRestrictedCapability(NetworkCapabilities nc) { + final long capabilities = packBits(nc.getCapabilities()); + // Check if we have any capability that forces the network to be restricted. + final boolean forceRestrictedCapability = + (capabilities & 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 = + (capabilities & UNRESTRICTED_CAPABILITIES) != 0; + + // Must have at least some restricted capabilities. + final boolean hasRestrictedCapabilities = (capabilities & RESTRICTED_CAPABILITIES) != 0; + + return forceRestrictedCapability + || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities); + } + /** * Unpacks long value into an array of bits. */ |