am 558aeb90: Merge "Make NetworkCapabilities publicly immutable." into lmp-preview-dev

* commit '558aeb901144fac460575402e38fc8baa2da83eb':
  Make NetworkCapabilities publicly immutable.
This commit is contained in:
Robert Greenwalt
2014-06-12 18:49:07 +00:00
committed by Android Git Automerger
4 changed files with 158 additions and 54 deletions

View File

@@ -429,6 +429,11 @@ public class ConnectivityManager {
*/ */
public final static int INVALID_NET_ID = 0; public final static int INVALID_NET_ID = 0;
/**
* @hide
*/
public final static int REQUEST_ID_UNSET = 0;
private final IConnectivityManager mService; private final IConnectivityManager mService;
private final String mPackageName; private final String mPackageName;
@@ -883,8 +888,8 @@ public class ConnectivityManager {
* @hide * @hide
*/ */
public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) { public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
for (Integer capability : nc.getNetworkCapabilities()) { for (int capability : nc.getCapabilities()) {
switch (capability.intValue()) { switch (capability) {
case NetworkCapabilities.NET_CAPABILITY_CBS: case NetworkCapabilities.NET_CAPABILITY_CBS:
case NetworkCapabilities.NET_CAPABILITY_DUN: case NetworkCapabilities.NET_CAPABILITY_DUN:
case NetworkCapabilities.NET_CAPABILITY_EIMS: case NetworkCapabilities.NET_CAPABILITY_EIMS:
@@ -903,7 +908,7 @@ public class ConnectivityManager {
} }
// All the capabilities are typically provided by restricted networks. // All the capabilities are typically provided by restricted networks.
// Conclude that this network is restricted. // Conclude that this network is restricted.
nc.removeNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
} }
private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
@@ -927,15 +932,14 @@ public class ConnectivityManager {
return null; return null;
} }
NetworkCapabilities netCap = new NetworkCapabilities(); NetworkCapabilities netCap = new NetworkCapabilities();
netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
netCap.addNetworkCapability(cap);
maybeMarkCapabilitiesRestricted(netCap); maybeMarkCapabilitiesRestricted(netCap);
return netCap; return netCap;
} else if (networkType == TYPE_WIFI) { } else if (networkType == TYPE_WIFI) {
if ("p2p".equals(feature)) { if ("p2p".equals(feature)) {
NetworkCapabilities netCap = new NetworkCapabilities(); NetworkCapabilities netCap = new NetworkCapabilities();
netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
maybeMarkCapabilitiesRestricted(netCap); maybeMarkCapabilitiesRestricted(netCap);
return netCap; return netCap;
} }

View File

@@ -44,6 +44,9 @@ public final class NetworkCapabilities implements Parcelable {
private static final String TAG = "NetworkCapabilities"; private static final String TAG = "NetworkCapabilities";
private static final boolean DBG = false; private static final boolean DBG = false;
/**
* @hide
*/
public NetworkCapabilities() { public NetworkCapabilities() {
} }
@@ -154,58 +157,64 @@ public final class NetworkCapabilities implements Parcelable {
* Multiple capabilities may be applied sequentially. Note that when searching * Multiple capabilities may be applied sequentially. Note that when searching
* for a network to satisfy a request, all capabilities requested must be satisfied. * for a network to satisfy a request, all capabilities requested must be satisfied.
* *
* @param networkCapability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added. * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
* @return This NetworkCapability to facilitate chaining.
* @hide
*/ */
public void addNetworkCapability(int networkCapability) { public NetworkCapabilities addCapability(int capability) {
if (networkCapability < MIN_NET_CAPABILITY || if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
networkCapability > MAX_NET_CAPABILITY) {
throw new IllegalArgumentException("NetworkCapability out of range"); throw new IllegalArgumentException("NetworkCapability out of range");
} }
mNetworkCapabilities |= 1 << networkCapability; mNetworkCapabilities |= 1 << capability;
return this;
} }
/** /**
* Removes (if found) the given capability from this {@code NetworkCapability} instance. * Removes (if found) the given capability from this {@code NetworkCapability} instance.
* *
* @param networkCapability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed. * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
* @return This NetworkCapability to facilitate chaining.
* @hide
*/ */
public void removeNetworkCapability(int networkCapability) { public NetworkCapabilities removeCapability(int capability) {
if (networkCapability < MIN_NET_CAPABILITY || if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
networkCapability > MAX_NET_CAPABILITY) {
throw new IllegalArgumentException("NetworkCapability out of range"); throw new IllegalArgumentException("NetworkCapability out of range");
} }
mNetworkCapabilities &= ~(1 << networkCapability); mNetworkCapabilities &= ~(1 << capability);
return this;
} }
/** /**
* Gets all the capabilities set on this {@code NetworkCapability} instance. * Gets all the capabilities set on this {@code NetworkCapability} instance.
* *
* @return a {@link Collection} of {@code NetworkCapabilities.NET_CAPABILITY_*} values * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
* for this instance. * for this instance.
* @hide
*/ */
public Collection<Integer> getNetworkCapabilities() { public int[] getCapabilities() {
return enumerateBits(mNetworkCapabilities); return enumerateBits(mNetworkCapabilities);
} }
/** /**
* Tests for the presence of a capabilitity on this instance. * Tests for the presence of a capabilitity on this instance.
* *
* @param networkCapability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for. * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
* @return {@code true} if set on this instance. * @return {@code true} if set on this instance.
*/ */
public boolean hasCapability(int networkCapability) { public boolean hasCapability(int capability) {
if (networkCapability < MIN_NET_CAPABILITY || if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
networkCapability > MAX_NET_CAPABILITY) {
return false; return false;
} }
return ((mNetworkCapabilities & (1 << networkCapability)) != 0); return ((mNetworkCapabilities & (1 << capability)) != 0);
} }
private Collection<Integer> enumerateBits(long val) { private int[] enumerateBits(long val) {
ArrayList<Integer> result = new ArrayList<Integer>(); int size = Long.bitCount(val);
int[] result = new int[size];
int index = 0;
int resource = 0; int resource = 0;
while (val > 0) { while (val > 0) {
if ((val & 1) == 1) result.add(resource); if ((val & 1) == 1) result[index++] = resource;
val = val >> 1; val = val >> 1;
resource++; resource++;
} }
@@ -265,33 +274,40 @@ public final class NetworkCapabilities implements Parcelable {
* {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
* *
* @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added. * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
* @return This NetworkCapability to facilitate chaining.
* @hide
*/ */
public void addTransportType(int transportType) { public NetworkCapabilities addTransportType(int transportType) {
if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
throw new IllegalArgumentException("TransportType out of range"); throw new IllegalArgumentException("TransportType out of range");
} }
mTransportTypes |= 1 << transportType; mTransportTypes |= 1 << transportType;
return this;
} }
/** /**
* Removes (if found) the given transport from this {@code NetworkCapability} instance. * Removes (if found) the given transport from this {@code NetworkCapability} instance.
* *
* @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed. * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
* @return This NetworkCapability to facilitate chaining.
* @hide
*/ */
public void removeTransportType(int transportType) { public NetworkCapabilities removeTransportType(int transportType) {
if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
throw new IllegalArgumentException("TransportType out of range"); throw new IllegalArgumentException("TransportType out of range");
} }
mTransportTypes &= ~(1 << transportType); mTransportTypes &= ~(1 << transportType);
return this;
} }
/** /**
* Gets all the transports set on this {@code NetworkCapability} instance. * Gets all the transports set on this {@code NetworkCapability} instance.
* *
* @return a {@link Collection} of {@code NetworkCapabilities.TRANSPORT_*} values * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
* for this instance. * for this instance.
* @hide
*/ */
public Collection<Integer> getTransportTypes() { public int[] getTransportTypes() {
return enumerateBits(mTransportTypes); return enumerateBits(mTransportTypes);
} }
@@ -340,6 +356,7 @@ public final class NetworkCapabilities implements Parcelable {
* fast backhauls and slow backhauls. * fast backhauls and slow backhauls.
* *
* @param upKbps the estimated first hop upstream (device to network) bandwidth. * @param upKbps the estimated first hop upstream (device to network) bandwidth.
* @hide
*/ */
public void setLinkUpstreamBandwidthKbps(int upKbps) { public void setLinkUpstreamBandwidthKbps(int upKbps) {
mLinkUpBandwidthKbps = upKbps; mLinkUpBandwidthKbps = upKbps;
@@ -368,6 +385,7 @@ public final class NetworkCapabilities implements Parcelable {
* fast backhauls and slow backhauls. * fast backhauls and slow backhauls.
* *
* @param downKbps the estimated first hop downstream (network to device) bandwidth. * @param downKbps the estimated first hop downstream (network to device) bandwidth.
* @hide
*/ */
public void setLinkDownstreamBandwidthKbps(int downKbps) { public void setLinkDownstreamBandwidthKbps(int downKbps) {
mLinkDownBandwidthKbps = downKbps; mLinkDownBandwidthKbps = downKbps;
@@ -464,24 +482,22 @@ public final class NetworkCapabilities implements Parcelable {
}; };
public String toString() { public String toString() {
Collection<Integer> types = getTransportTypes(); int[] types = getTransportTypes();
String transports = (types.size() > 0 ? " Transports: " : ""); String transports = (types.length > 0 ? " Transports: " : "");
Iterator<Integer> i = types.iterator(); for (int i = 0; i < types.length;) {
while (i.hasNext()) { switch (types[i]) {
switch (i.next()) {
case TRANSPORT_CELLULAR: transports += "CELLULAR"; break; case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
case TRANSPORT_WIFI: transports += "WIFI"; break; case TRANSPORT_WIFI: transports += "WIFI"; break;
case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break; case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
case TRANSPORT_ETHERNET: transports += "ETHERNET"; break; case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
} }
if (i.hasNext()) transports += "|"; if (++i < types.length) transports += "|";
} }
types = getNetworkCapabilities(); types = getCapabilities();
String capabilities = (types.size() > 0 ? " Capabilities: " : ""); String capabilities = (types.length > 0 ? " Capabilities: " : "");
i = types.iterator(); for (int i = 0; i < types.length; ) {
while (i.hasNext()) { switch (types[i]) {
switch (i.next().intValue()) {
case NET_CAPABILITY_MMS: capabilities += "MMS"; break; case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break; case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
case NET_CAPABILITY_DUN: capabilities += "DUN"; break; case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
@@ -497,7 +513,7 @@ public final class NetworkCapabilities implements Parcelable {
case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break; case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break; case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
} }
if (i.hasNext()) capabilities += "&"; if (++i < types.length) capabilities += "&";
} }
String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +

View File

@@ -22,19 +22,14 @@ import android.os.Parcelable;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Defines a request for a network, made by calling {@link ConnectivityManager#requestNetwork} * Defines a request for a network, made through {@link NetworkRequest.Builder} and used
* or {@link ConnectivityManager#listenForNetwork}. * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes
* * via {@link ConnectivityManager#listenForNetwork}.
* This token records the {@link NetworkCapabilities} used to make the request and identifies
* the request. It should be used to release the request via
* {@link ConnectivityManager#releaseNetworkRequest} when the network is no longer desired.
*/ */
public class NetworkRequest implements Parcelable { public class NetworkRequest implements Parcelable {
/** /**
* The {@link NetworkCapabilities} that define this request. This should not be modified. * The {@link NetworkCapabilities} that define this request.
* The networkCapabilities of the request are set when * @hide
* {@link ConnectivityManager#requestNetwork} is called and the value is presented here
* as a convenient reminder of what was requested.
*/ */
public final NetworkCapabilities networkCapabilities; public final NetworkCapabilities networkCapabilities;
@@ -71,6 +66,95 @@ public class NetworkRequest implements Parcelable {
this.legacyType = that.legacyType; this.legacyType = that.legacyType;
} }
/**
* Builder used to create {@link NetworkRequest} objects. Specify the Network features
* needed in terms of {@link NetworkCapabilities} features
*/
public static class Builder {
private final NetworkCapabilities mNetworkCapabilities = new NetworkCapabilities();
/**
* Default constructor for Builder.
*/
public Builder() {}
/**
* Build {@link NetworkRequest} give the current set of capabilities.
*/
public NetworkRequest build() {
return new NetworkRequest(mNetworkCapabilities, ConnectivityManager.TYPE_NONE,
ConnectivityManager.REQUEST_ID_UNSET);
}
/**
* Add the given capability requirement to this builder. These represent
* the requested network's required capabilities. Note that when searching
* for a network to satisfy a request, all capabilities requested must be
* satisfied. See {@link NetworkCapabilities} for {@code NET_CAPABILITIY_*}
* definitions.
*
* @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to add.
* @return The builder to facilitate chaining
* {@code builder.addCapability(...).addCapability();}.
*/
public Builder addCapability(int capability) {
mNetworkCapabilities.addCapability(capability);
return this;
}
/**
* Removes (if found) the given capability from this builder instance.
*
* @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to remove.
* @return The builder to facilitate chaining.
*/
public Builder removeCapability(int capability) {
mNetworkCapabilities.removeCapability(capability);
return this;
}
/**
* Adds the given transport requirement to this builder. These represent
* the set of allowed transports for the request. Only networks using one
* of these transports will satisfy the request. If no particular transports
* are required, none should be specified here. See {@link NetworkCapabilities}
* for {@code TRANSPORT_*} definitions.
*
* @param transportType The {@code NetworkCapabilities.TRANSPORT_*} to add.
* @return The builder to facilitate chaining.
*/
public Builder addTransportType(int transportType) {
mNetworkCapabilities.addTransportType(transportType);
return this;
}
/**
* Removes (if found) the given transport from this builder instance.
*
* @param transportType The {@code NetworkCapabilities.TRANSPORT_*} to remove.
* @return The builder to facilitate chaining.
*/
public Builder removeTransportType(int transportType) {
mNetworkCapabilities.removeTransportType(transportType);
return this;
}
/**
* @hide
*/
public Builder setLinkUpstreamBandwidthKbps(int upKbps) {
mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps);
return this;
}
/**
* @hide
*/
public Builder setLinkDownstreamBandwidthKbps(int downKbps) {
mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps);
return this;
}
}
// implement the Parcelable interface // implement the Parcelable interface
public int describeContents() { public int describeContents() {
return 0; return 0;

View File

@@ -630,8 +630,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
if (DBG) log("ConnectivityService starting up"); if (DBG) log("ConnectivityService starting up");
NetworkCapabilities netCap = new NetworkCapabilities(); NetworkCapabilities netCap = new NetworkCapabilities();
netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
mDefaultRequest = new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); mDefaultRequest = new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId());
NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(),
NetworkRequestInfo.REQUEST); NetworkRequestInfo.REQUEST);