Merge "Define NOT_ROAMING network capability." am: 8f380ab29c

am: 3fe1ae29cd

Change-Id: Ib511f7af82c80fd4e95f4da9de1fdce200deb987
This commit is contained in:
Jeff Sharkey 2017-11-03 23:07:03 +00:00 • committed by android-build-merger
commit db888d1704
2 changed files with 335 additions and 53 deletions

View file

@ -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 { }
@ -217,12 +228,17 @@ 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);
/**
@ -315,6 +332,21 @@ public final class NetworkCapabilities implements Parcelable {
return this;
}
/**
* 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.
*
@ -325,6 +357,15 @@ public final class NetworkCapabilities implements Parcelable {
return BitUtils.unpackBits(mNetworkCapabilities);
}
/**
* 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.
*
@ -514,6 +555,21 @@ public final class NetworkCapabilities implements Parcelable {
return this;
}
/**
* 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.
*
@ -524,6 +580,15 @@ public final class NetworkCapabilities implements Parcelable {
return BitUtils.unpackBits(mTransportTypes);
}
/**
* 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.
*
@ -548,13 +613,19 @@ public final class NetworkCapabilities implements Parcelable {
return (nc.mTransportTypes == this.mTransportTypes);
}
/**
* 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;
}
/**
@ -970,6 +1058,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);
}

View file

@ -305,11 +305,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;
@ -318,6 +324,7 @@ public class NetworkInfo implements Parcelable {
/** {@hide} */
@VisibleForTesting
@Deprecated
public void setRoaming(boolean isRoaming) {
synchronized (this) {
mIsRoaming = isRoaming;