diff options
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/net/NetworkCapabilities.java | 119 | ||||
| -rw-r--r-- | core/java/android/net/NetworkIdentity.java | 4 | ||||
| -rw-r--r-- | core/java/android/net/NetworkInfo.java | 13 |
3 files changed, 116 insertions, 20 deletions
diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java index db12dd9724dc..ee75fd443052 100644 --- a/core/java/android/net/NetworkCapabilities.java +++ b/core/java/android/net/NetworkCapabilities.java @@ -17,6 +17,7 @@ package android.net; import android.annotation.IntDef; +import android.net.ConnectivityManager.NetworkCallback; import android.os.Parcel; import android.os.Parcelable; @@ -30,15 +31,24 @@ import java.util.Objects; import java.util.StringJoiner; /** - * This class represents the capabilities of a network. This is used both to specify - * needs to {@link ConnectivityManager} and when inspecting a network. - * - * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method - * of network selection. Rather than indicate a need for Wi-Fi because an application - * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE), - * the application should specify it needs high bandwidth. Similarly if an application - * needs an unmetered network for a bulk transfer it can specify that rather than assuming - * all cellular based connections are metered and all Wi-Fi based connections are not. + * Representation of the capabilities of a network. This object serves two + * purposes: + * <ul> + * <li>An expression of the current capabilities of an active network, typically + * expressed through + * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} + * or {@link ConnectivityManager#getNetworkCapabilities(Network)}. + * <li>An expression of the future capabilities of a desired network, typically + * expressed through {@link NetworkRequest}. + * </ul> + * <p> + * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of + * network selection. Rather than indicate a need for Wi-Fi because an + * application needs high bandwidth and risk obsolescence when a new, fast + * network appears (like LTE), the application should specify it needs high + * bandwidth. Similarly if an application needs an unmetered network for a bulk + * transfer it can specify that rather than assuming all cellular based + * connections are metered and all Wi-Fi based connections are not. */ public final class NetworkCapabilities implements Parcelable { private static final String TAG = "NetworkCapabilities"; @@ -101,6 +111,7 @@ public final class NetworkCapabilities implements Parcelable { NET_CAPABILITY_NOT_VPN, NET_CAPABILITY_VALIDATED, NET_CAPABILITY_CAPTIVE_PORTAL, + NET_CAPABILITY_NOT_ROAMING, NET_CAPABILITY_FOREGROUND, }) public @interface NetCapability { } @@ -218,11 +229,16 @@ public final class NetworkCapabilities implements Parcelable { public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17; /** + * Indicates that this network is not roaming. + */ + public static final int NET_CAPABILITY_NOT_ROAMING = 18; + + /** * Indicates that this network is available for use by apps, and not a network that is being * kept up in the background to facilitate fast network switching. * @hide */ - public static final int NET_CAPABILITY_FOREGROUND = 18; + public static final int NET_CAPABILITY_FOREGROUND = 19; private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND; @@ -237,6 +253,7 @@ public final class NetworkCapabilities implements Parcelable { (1 << NET_CAPABILITY_TRUSTED) | (1 << NET_CAPABILITY_VALIDATED) | (1 << NET_CAPABILITY_CAPTIVE_PORTAL) | + (1 << NET_CAPABILITY_NOT_ROAMING) | (1 << NET_CAPABILITY_FOREGROUND); /** @@ -316,6 +333,21 @@ public final class NetworkCapabilities implements Parcelable { } /** + * Sets (or clears) the given capability on this {@link NetworkCapabilities} + * instance. + * + * @hide + */ + public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) { + if (value) { + addCapability(capability); + } else { + removeCapability(capability); + } + return this; + } + + /** * Gets all the capabilities set on this {@code NetworkCapability} instance. * * @return an array of capability values for this instance. @@ -326,6 +358,15 @@ public final class NetworkCapabilities implements Parcelable { } /** + * Sets all the capabilities set on this {@code NetworkCapability} instance. + * + * @hide + */ + public void setCapabilities(@NetCapability int[] capabilities) { + mNetworkCapabilities = BitUtils.packBits(capabilities); + } + + /** * Tests for the presence of a capabilitity on this instance. * * @param capability the capabilities to be tested for. @@ -515,6 +556,21 @@ public final class NetworkCapabilities implements Parcelable { } /** + * Sets (or clears) the given transport on this {@link NetworkCapabilities} + * instance. + * + * @hide + */ + public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) { + if (value) { + addTransportType(transportType); + } else { + removeTransportType(transportType); + } + return this; + } + + /** * Gets all the transports set on this {@code NetworkCapability} instance. * * @return an array of transport type values for this instance. @@ -525,6 +581,15 @@ public final class NetworkCapabilities implements Parcelable { } /** + * Sets all the transports set on this {@code NetworkCapability} instance. + * + * @hide + */ + public void setTransportTypes(@Transport int[] transportTypes) { + mTransportTypes = BitUtils.packBits(transportTypes); + } + + /** * Tests for the presence of a transport on this instance. * * @param transportType the transport type to be tested for. @@ -549,12 +614,18 @@ public final class NetworkCapabilities implements Parcelable { } /** + * Value indicating that link bandwidth is unspecified. + * @hide + */ + public static final int LINK_BANDWIDTH_UNSPECIFIED = 0; + + /** * Passive link bandwidth. This is a rough guide of the expected peak bandwidth * for the first hop on the given transport. It is not measured, but may take into account * link parameters (Radio technology, allocated channels, etc). */ - private int mLinkUpBandwidthKbps; - private int mLinkDownBandwidthKbps; + private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED; + private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED; /** * Sets the upstream bandwidth for this network in Kbps. This always only refers to @@ -571,8 +642,9 @@ public final class NetworkCapabilities implements Parcelable { * @param upKbps the estimated first hop upstream (device to network) bandwidth. * @hide */ - public void setLinkUpstreamBandwidthKbps(int upKbps) { + public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) { mLinkUpBandwidthKbps = upKbps; + return this; } /** @@ -600,8 +672,9 @@ public final class NetworkCapabilities implements Parcelable { * @param downKbps the estimated first hop downstream (network to device) bandwidth. * @hide */ - public void setLinkDownstreamBandwidthKbps(int downKbps) { + public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) { mLinkDownBandwidthKbps = downKbps; + return this; } /** @@ -628,6 +701,20 @@ public final class NetworkCapabilities implements Parcelable { return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); } + /** @hide */ + public static int minBandwidth(int a, int b) { + if (a == LINK_BANDWIDTH_UNSPECIFIED) { + return b; + } else if (b == LINK_BANDWIDTH_UNSPECIFIED) { + return a; + } else { + return Math.min(a, b); + } + } + /** @hide */ + public static int maxBandwidth(int a, int b) { + return Math.max(a, b); + } private NetworkSpecifier mNetworkSpecifier = null; @@ -708,8 +795,9 @@ public final class NetworkCapabilities implements Parcelable { * @param signalStrength the bearer-specific signal strength. * @hide */ - public void setSignalStrength(int signalStrength) { + public NetworkCapabilities setSignalStrength(int signalStrength) { mSignalStrength = signalStrength; + return this; } /** @@ -968,6 +1056,7 @@ public final class NetworkCapabilities implements Parcelable { case NET_CAPABILITY_NOT_VPN: return "NOT_VPN"; case NET_CAPABILITY_VALIDATED: return "VALIDATED"; case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL"; + case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING"; case NET_CAPABILITY_FOREGROUND: return "FOREGROUND"; default: return Integer.toString(capability); } diff --git a/core/java/android/net/NetworkIdentity.java b/core/java/android/net/NetworkIdentity.java index 0775bdaaf380..df404b7dce27 100644 --- a/core/java/android/net/NetworkIdentity.java +++ b/core/java/android/net/NetworkIdentity.java @@ -189,7 +189,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> { String subscriberId = null; String networkId = null; - boolean roaming = false; + boolean roaming = !state.networkCapabilities.hasCapability( + NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); boolean metered = !state.networkCapabilities.hasCapability( NetworkCapabilities.NET_CAPABILITY_NOT_METERED); @@ -203,7 +204,6 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> { } subscriberId = state.subscriberId; - roaming = state.networkInfo.isRoaming(); } else if (type == TYPE_WIFI) { if (state.networkId != null) { diff --git a/core/java/android/net/NetworkInfo.java b/core/java/android/net/NetworkInfo.java index 84c32bec8ef7..d5549387755d 100644 --- a/core/java/android/net/NetworkInfo.java +++ b/core/java/android/net/NetworkInfo.java @@ -307,11 +307,17 @@ public class NetworkInfo implements Parcelable { } /** - * Indicates whether the device is currently roaming on this network. - * When {@code true}, it suggests that use of data on this network - * may incur extra costs. + * Indicates whether the device is currently roaming on this network. When + * {@code true}, it suggests that use of data on this network may incur + * extra costs. + * * @return {@code true} if roaming is in effect, {@code false} otherwise. + * @deprecated Callers should switch to checking + * {@link NetworkCapabilities#NET_CAPABILITY_NOT_ROAMING} + * instead, since that handles more complex situations, such as + * VPNs. */ + @Deprecated public boolean isRoaming() { synchronized (this) { return mIsRoaming; @@ -320,6 +326,7 @@ public class NetworkInfo implements Parcelable { /** {@hide} */ @VisibleForTesting + @Deprecated public void setRoaming(boolean isRoaming) { synchronized (this) { mIsRoaming = isRoaming; |
