diff options
| -rw-r--r-- | api/current.txt | 19 | ||||
| -rw-r--r-- | core/java/android/net/ConnectivityManager.java | 16 | ||||
| -rw-r--r-- | core/java/android/net/NetworkCapabilities.java | 88 | ||||
| -rw-r--r-- | core/java/android/net/NetworkRequest.java | 104 | ||||
| -rw-r--r-- | services/core/java/com/android/server/ConnectivityService.java | 4 |
5 files changed, 167 insertions, 64 deletions
diff --git a/api/current.txt b/api/current.txt index 1973da5..76c3e81 100644 --- a/api/current.txt +++ b/api/current.txt @@ -16270,21 +16270,12 @@ package android.net { } public final class NetworkCapabilities implements android.os.Parcelable { - ctor public NetworkCapabilities(); ctor public NetworkCapabilities(android.net.NetworkCapabilities); - method public void addNetworkCapability(int); - method public void addTransportType(int); method public int describeContents(); method public int getLinkDownstreamBandwidthKbps(); method public int getLinkUpstreamBandwidthKbps(); - method public java.util.Collection<java.lang.Integer> getNetworkCapabilities(); - method public java.util.Collection<java.lang.Integer> getTransportTypes(); method public boolean hasCapability(int); method public boolean hasTransport(int); - method public void removeNetworkCapability(int); - method public void removeTransportType(int); - method public void setLinkDownstreamBandwidthKbps(int); - method public void setLinkUpstreamBandwidthKbps(int); method public void writeToParcel(android.os.Parcel, int); field public static final android.os.Parcelable.Creator CREATOR; field public static final int NET_CAPABILITY_CBS = 5; // 0x5 @@ -16358,7 +16349,15 @@ package android.net { method public int describeContents(); method public void writeToParcel(android.os.Parcel, int); field public static final android.os.Parcelable.Creator CREATOR; - field public final android.net.NetworkCapabilities networkCapabilities; + } + + public static class NetworkRequest.Builder { + ctor public NetworkRequest.Builder(); + method public android.net.NetworkRequest.Builder addCapability(int); + method public android.net.NetworkRequest.Builder addTransportType(int); + method public android.net.NetworkRequest build(); + method public android.net.NetworkRequest.Builder removeCapability(int); + method public android.net.NetworkRequest.Builder removeTransportType(int); } public class ParseException extends java.lang.RuntimeException { diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 27402668..ff90e78 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -429,6 +429,11 @@ public class ConnectivityManager { */ public final static int INVALID_NET_ID = 0; + /** + * @hide + */ + public final static int REQUEST_ID_UNSET = 0; + private final IConnectivityManager mService; private final String mPackageName; @@ -883,8 +888,8 @@ public class ConnectivityManager { * @hide */ public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) { - for (Integer capability : nc.getNetworkCapabilities()) { - switch (capability.intValue()) { + for (int capability : nc.getCapabilities()) { + switch (capability) { case NetworkCapabilities.NET_CAPABILITY_CBS: case NetworkCapabilities.NET_CAPABILITY_DUN: case NetworkCapabilities.NET_CAPABILITY_EIMS: @@ -903,7 +908,7 @@ public class ConnectivityManager { } // All the capabilities are typically provided by restricted networks. // 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) { @@ -927,15 +932,14 @@ public class ConnectivityManager { return null; } NetworkCapabilities netCap = new NetworkCapabilities(); - netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); - netCap.addNetworkCapability(cap); + netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap); maybeMarkCapabilitiesRestricted(netCap); return netCap; } else if (networkType == TYPE_WIFI) { if ("p2p".equals(feature)) { NetworkCapabilities netCap = new NetworkCapabilities(); netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); - netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); + netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); maybeMarkCapabilitiesRestricted(netCap); return netCap; } diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java index 35274f1..fe96287 100644 --- a/core/java/android/net/NetworkCapabilities.java +++ b/core/java/android/net/NetworkCapabilities.java @@ -44,6 +44,9 @@ public final class NetworkCapabilities implements Parcelable { private static final String TAG = "NetworkCapabilities"; private static final boolean DBG = false; + /** + * @hide + */ public NetworkCapabilities() { } @@ -154,58 +157,64 @@ public final class NetworkCapabilities implements Parcelable { * Multiple capabilities may be applied sequentially. Note that when searching * 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) { - if (networkCapability < MIN_NET_CAPABILITY || - networkCapability > MAX_NET_CAPABILITY) { + public NetworkCapabilities addCapability(int capability) { + if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { 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. * - * @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) { - if (networkCapability < MIN_NET_CAPABILITY || - networkCapability > MAX_NET_CAPABILITY) { + public NetworkCapabilities removeCapability(int capability) { + if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { 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. * - * @return a {@link Collection} of {@code NetworkCapabilities.NET_CAPABILITY_*} values + * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values * for this instance. + * @hide */ - public Collection<Integer> getNetworkCapabilities() { + public int[] getCapabilities() { return enumerateBits(mNetworkCapabilities); } /** * 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. */ - public boolean hasCapability(int networkCapability) { - if (networkCapability < MIN_NET_CAPABILITY || - networkCapability > MAX_NET_CAPABILITY) { + public boolean hasCapability(int capability) { + if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { return false; } - return ((mNetworkCapabilities & (1 << networkCapability)) != 0); + return ((mNetworkCapabilities & (1 << capability)) != 0); } - private Collection<Integer> enumerateBits(long val) { - ArrayList<Integer> result = new ArrayList<Integer>(); + private int[] enumerateBits(long val) { + int size = Long.bitCount(val); + int[] result = new int[size]; + int index = 0; int resource = 0; while (val > 0) { - if ((val & 1) == 1) result.add(resource); + if ((val & 1) == 1) result[index++] = resource; val = val >> 1; resource++; } @@ -265,33 +274,40 @@ public final class NetworkCapabilities implements Parcelable { * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. * * @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) { throw new IllegalArgumentException("TransportType out of range"); } mTransportTypes |= 1 << transportType; + return this; } /** * Removes (if found) the given transport from this {@code NetworkCapability} instance. * * @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) { throw new IllegalArgumentException("TransportType out of range"); } mTransportTypes &= ~(1 << transportType); + return this; } /** * 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. + * @hide */ - public Collection<Integer> getTransportTypes() { + public int[] getTransportTypes() { return enumerateBits(mTransportTypes); } @@ -340,6 +356,7 @@ public final class NetworkCapabilities implements Parcelable { * fast backhauls and slow backhauls. * * @param upKbps the estimated first hop upstream (device to network) bandwidth. + * @hide */ public void setLinkUpstreamBandwidthKbps(int upKbps) { mLinkUpBandwidthKbps = upKbps; @@ -368,6 +385,7 @@ public final class NetworkCapabilities implements Parcelable { * fast backhauls and slow backhauls. * * @param downKbps the estimated first hop downstream (network to device) bandwidth. + * @hide */ public void setLinkDownstreamBandwidthKbps(int downKbps) { mLinkDownBandwidthKbps = downKbps; @@ -464,24 +482,22 @@ public final class NetworkCapabilities implements Parcelable { }; public String toString() { - Collection<Integer> types = getTransportTypes(); - String transports = (types.size() > 0 ? " Transports: " : ""); - Iterator<Integer> i = types.iterator(); - while (i.hasNext()) { - switch (i.next()) { + int[] types = getTransportTypes(); + String transports = (types.length > 0 ? " Transports: " : ""); + for (int i = 0; i < types.length;) { + switch (types[i]) { case TRANSPORT_CELLULAR: transports += "CELLULAR"; break; case TRANSPORT_WIFI: transports += "WIFI"; break; case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break; case TRANSPORT_ETHERNET: transports += "ETHERNET"; break; } - if (i.hasNext()) transports += "|"; + if (++i < types.length) transports += "|"; } - types = getNetworkCapabilities(); - String capabilities = (types.size() > 0 ? " Capabilities: " : ""); - i = types.iterator(); - while (i.hasNext()) { - switch (i.next().intValue()) { + types = getCapabilities(); + String capabilities = (types.length > 0 ? " Capabilities: " : ""); + for (int i = 0; i < types.length; ) { + switch (types[i]) { case NET_CAPABILITY_MMS: capabilities += "MMS"; break; case NET_CAPABILITY_SUPL: capabilities += "SUPL"; 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_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break; } - if (i.hasNext()) capabilities += "&"; + if (++i < types.length) capabilities += "&"; } String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + diff --git a/core/java/android/net/NetworkRequest.java b/core/java/android/net/NetworkRequest.java index 47377e9..7911c72 100644 --- a/core/java/android/net/NetworkRequest.java +++ b/core/java/android/net/NetworkRequest.java @@ -22,19 +22,14 @@ import android.os.Parcelable; import java.util.concurrent.atomic.AtomicInteger; /** - * Defines a request for a network, made by calling {@link ConnectivityManager#requestNetwork} - * or {@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. + * Defines a request for a network, made through {@link NetworkRequest.Builder} and used + * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes + * via {@link ConnectivityManager#listenForNetwork}. */ public class NetworkRequest implements Parcelable { /** - * The {@link NetworkCapabilities} that define this request. This should not be modified. - * The networkCapabilities of the request are set when - * {@link ConnectivityManager#requestNetwork} is called and the value is presented here - * as a convenient reminder of what was requested. + * The {@link NetworkCapabilities} that define this request. + * @hide */ public final NetworkCapabilities networkCapabilities; @@ -71,6 +66,95 @@ public class NetworkRequest implements Parcelable { 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 public int describeContents() { return 0; diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 9087726..657d5ec 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -630,8 +630,8 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (DBG) log("ConnectivityService starting up"); NetworkCapabilities netCap = new NetworkCapabilities(); - netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); - netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); + netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); + netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); mDefaultRequest = new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), NetworkRequestInfo.REQUEST); |
