2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.text.TextUtils;
22 import android.util.Log;
24 import java.lang.IllegalArgumentException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Iterator;
30 import java.util.Map.Entry;
34 * This class represents the capabilities of a network. This is used both to specify
35 * needs to {@link ConnectivityManager} and when inspecting a network.
37 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
38 * of network selection. Rather than indicate a need for Wi-Fi because an application
39 * needs high bandwidth and risk obselence when a new, fast network appears (like LTE),
40 * the application should specify it needs high bandwidth. Similarly if an application
41 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
42 * all cellular based connections are metered and all Wi-Fi based connections are not.
44 public final class NetworkCapabilities implements Parcelable {
45 private static final String TAG = "NetworkCapabilities";
46 private static final boolean DBG = false;
51 public NetworkCapabilities() {
54 public NetworkCapabilities(NetworkCapabilities nc) {
56 mNetworkCapabilities = nc.mNetworkCapabilities;
57 mTransportTypes = nc.mTransportTypes;
58 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
59 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
60 mNetworkSpecifier = nc.mNetworkSpecifier;
65 * Represents the network's capabilities. If any are specified they will be satisfied
66 * by any Network that matches all of them.
68 private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED) |
69 (1 << NET_CAPABILITY_TRUSTED) | (1 << NET_CAPABILITY_NOT_VPN);
72 * Indicates this is a network that has the ability to reach the
73 * carrier's MMSC for sending and receiving MMS messages.
75 public static final int NET_CAPABILITY_MMS = 0;
78 * Indicates this is a network that has the ability to reach the carrier's
79 * SUPL server, used to retrieve GPS information.
81 public static final int NET_CAPABILITY_SUPL = 1;
84 * Indicates this is a network that has the ability to reach the carrier's
85 * DUN or tethering gateway.
87 public static final int NET_CAPABILITY_DUN = 2;
90 * Indicates this is a network that has the ability to reach the carrier's
91 * FOTA portal, used for over the air updates.
93 public static final int NET_CAPABILITY_FOTA = 3;
96 * Indicates this is a network that has the ability to reach the carrier's
97 * IMS servers, used for network registration and signaling.
99 public static final int NET_CAPABILITY_IMS = 4;
102 * Indicates this is a network that has the ability to reach the carrier's
103 * CBS servers, used for carrier specific services.
105 public static final int NET_CAPABILITY_CBS = 5;
108 * Indicates this is a network that has the ability to reach a Wi-Fi direct
111 public static final int NET_CAPABILITY_WIFI_P2P = 6;
114 * Indicates this is a network that has the ability to reach a carrier's
115 * Initial Attach servers.
117 public static final int NET_CAPABILITY_IA = 7;
120 * Indicates this is a network that has the ability to reach a carrier's
121 * RCS servers, used for Rich Communication Services.
123 public static final int NET_CAPABILITY_RCS = 8;
126 * Indicates this is a network that has the ability to reach a carrier's
127 * XCAP servers, used for configuration and control.
129 public static final int NET_CAPABILITY_XCAP = 9;
132 * Indicates this is a network that has the ability to reach a carrier's
133 * Emergency IMS servers, used for network signaling during emergency calls.
135 public static final int NET_CAPABILITY_EIMS = 10;
138 * Indicates that this network is unmetered.
140 public static final int NET_CAPABILITY_NOT_METERED = 11;
143 * Indicates that this network should be able to reach the internet.
145 public static final int NET_CAPABILITY_INTERNET = 12;
148 * Indicates that this network is available for general use. If this is not set
149 * applications should not attempt to communicate on this network. Note that this
150 * is simply informative and not enforcement - enforcement is handled via other means.
153 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
156 * Indicates that the user has indicated implicit trust of this network. This
157 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
158 * BT device or a wifi the user asked to connect to. Untrusted networks
159 * are probably limited to unknown wifi AP. Set by default.
161 public static final int NET_CAPABILITY_TRUSTED = 14;
164 * Indicates that this network is not a VPN. This capability is set by default and should be
165 * explicitly cleared when creating VPN networks.
167 public static final int NET_CAPABILITY_NOT_VPN = 15;
170 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
171 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_VPN;
174 * Adds the given capability to this {@code NetworkCapability} instance.
175 * Multiple capabilities may be applied sequentially. Note that when searching
176 * for a network to satisfy a request, all capabilities requested must be satisfied.
178 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
179 * @return This NetworkCapability to facilitate chaining.
182 public NetworkCapabilities addCapability(int capability) {
183 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
184 throw new IllegalArgumentException("NetworkCapability out of range");
186 mNetworkCapabilities |= 1 << capability;
191 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
193 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
194 * @return This NetworkCapability to facilitate chaining.
197 public NetworkCapabilities removeCapability(int capability) {
198 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
199 throw new IllegalArgumentException("NetworkCapability out of range");
201 mNetworkCapabilities &= ~(1 << capability);
206 * Gets all the capabilities set on this {@code NetworkCapability} instance.
208 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
212 public int[] getCapabilities() {
213 return enumerateBits(mNetworkCapabilities);
217 * Tests for the presence of a capabilitity on this instance.
219 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
220 * @return {@code true} if set on this instance.
222 public boolean hasCapability(int capability) {
223 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
226 return ((mNetworkCapabilities & (1 << capability)) != 0);
229 private int[] enumerateBits(long val) {
230 int size = Long.bitCount(val);
231 int[] result = new int[size];
235 if ((val & 1) == 1) result[index++] = resource;
242 private void combineNetCapabilities(NetworkCapabilities nc) {
243 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
246 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
247 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
250 private boolean equalsNetCapabilities(NetworkCapabilities nc) {
251 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
255 * Representing the transport type. Apps should generally not care about transport. A
256 * request for a fast internet connection could be satisfied by a number of different
257 * transports. If any are specified here it will be satisfied a Network that matches
258 * any of them. If a caller doesn't care about the transport it should not specify any.
260 private long mTransportTypes;
263 * Indicates this network uses a Cellular transport.
265 public static final int TRANSPORT_CELLULAR = 0;
268 * Indicates this network uses a Wi-Fi transport.
270 public static final int TRANSPORT_WIFI = 1;
273 * Indicates this network uses a Bluetooth transport.
275 public static final int TRANSPORT_BLUETOOTH = 2;
278 * Indicates this network uses an Ethernet transport.
280 public static final int TRANSPORT_ETHERNET = 3;
283 * Indicates this network uses a VPN transport.
285 public static final int TRANSPORT_VPN = 4;
287 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
288 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
291 * Adds the given transport type to this {@code NetworkCapability} instance.
292 * Multiple transports may be applied sequentially. Note that when searching
293 * for a network to satisfy a request, any listed in the request will satisfy the request.
294 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
295 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
296 * to be selected. This is logically different than
297 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
299 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
300 * @return This NetworkCapability to facilitate chaining.
303 public NetworkCapabilities addTransportType(int transportType) {
304 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
305 throw new IllegalArgumentException("TransportType out of range");
307 mTransportTypes |= 1 << transportType;
308 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
313 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
315 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
316 * @return This NetworkCapability to facilitate chaining.
319 public NetworkCapabilities removeTransportType(int transportType) {
320 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
321 throw new IllegalArgumentException("TransportType out of range");
323 mTransportTypes &= ~(1 << transportType);
324 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
329 * Gets all the transports set on this {@code NetworkCapability} instance.
331 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
335 public int[] getTransportTypes() {
336 return enumerateBits(mTransportTypes);
340 * Tests for the presence of a transport on this instance.
342 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
343 * @return {@code true} if set on this instance.
345 public boolean hasTransport(int transportType) {
346 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
349 return ((mTransportTypes & (1 << transportType)) != 0);
352 private void combineTransportTypes(NetworkCapabilities nc) {
353 this.mTransportTypes |= nc.mTransportTypes;
355 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
356 return ((this.mTransportTypes == 0) ||
357 ((this.mTransportTypes & nc.mTransportTypes) != 0));
359 private boolean equalsTransportTypes(NetworkCapabilities nc) {
360 return (nc.mTransportTypes == this.mTransportTypes);
364 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
365 * for the first hop on the given transport. It is not measured, but may take into account
366 * link parameters (Radio technology, allocated channels, etc).
368 private int mLinkUpBandwidthKbps;
369 private int mLinkDownBandwidthKbps;
372 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
373 * the estimated first hop transport bandwidth.
375 * Note that when used to request a network, this specifies the minimum acceptable.
376 * When received as the state of an existing network this specifies the typical
377 * first hop bandwidth expected. This is never measured, but rather is inferred
378 * from technology type and other link parameters. It could be used to differentiate
379 * between very slow 1xRTT cellular links and other faster networks or even between
380 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
381 * fast backhauls and slow backhauls.
383 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
386 public void setLinkUpstreamBandwidthKbps(int upKbps) {
387 mLinkUpBandwidthKbps = upKbps;
391 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
392 * the estimated first hop transport bandwidth.
394 * @return The estimated first hop upstream (device to network) bandwidth.
396 public int getLinkUpstreamBandwidthKbps() {
397 return mLinkUpBandwidthKbps;
401 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
402 * the estimated first hop transport bandwidth.
404 * Note that when used to request a network, this specifies the minimum acceptable.
405 * When received as the state of an existing network this specifies the typical
406 * first hop bandwidth expected. This is never measured, but rather is inferred
407 * from technology type and other link parameters. It could be used to differentiate
408 * between very slow 1xRTT cellular links and other faster networks or even between
409 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
410 * fast backhauls and slow backhauls.
412 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
415 public void setLinkDownstreamBandwidthKbps(int downKbps) {
416 mLinkDownBandwidthKbps = downKbps;
420 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
421 * the estimated first hop transport bandwidth.
423 * @return The estimated first hop downstream (network to device) bandwidth.
425 public int getLinkDownstreamBandwidthKbps() {
426 return mLinkDownBandwidthKbps;
429 private void combineLinkBandwidths(NetworkCapabilities nc) {
430 this.mLinkUpBandwidthKbps =
431 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
432 this.mLinkDownBandwidthKbps =
433 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
435 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
436 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
437 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
439 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
440 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
441 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
444 private String mNetworkSpecifier;
446 * Sets the optional bearer specific network specifier.
447 * This has no meaning if a single transport is also not specified, so calling
448 * this without a single transport set will generate an exception, as will
449 * subsequently adding or removing transports after this is set.
451 * The interpretation of this {@code String} is bearer specific and bearers that use
452 * it should document their particulars. For example, Bluetooth may use some sort of
453 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
454 * or Subscription ID.
456 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
457 * specific network specifier where the bearer has a choice of
461 public void setNetworkSpecifier(String networkSpecifier) {
462 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
463 throw new IllegalStateException("Must have a single transport specified to use " +
464 "setNetworkSpecifier");
466 mNetworkSpecifier = networkSpecifier;
470 * Gets the optional bearer specific network specifier.
472 * @return The optional {@code String} specifying the bearer specific network specifier.
473 * See {@link #setNetworkSpecifier}.
476 public String getNetworkSpecifier() {
477 return mNetworkSpecifier;
480 private void combineSpecifiers(NetworkCapabilities nc) {
481 String otherSpecifier = nc.getNetworkSpecifier();
482 if (TextUtils.isEmpty(otherSpecifier)) return;
483 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
484 throw new IllegalStateException("Can't combine two networkSpecifiers");
486 setNetworkSpecifier(otherSpecifier);
488 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
489 return (TextUtils.isEmpty(mNetworkSpecifier) ||
490 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
492 private boolean equalsSpecifier(NetworkCapabilities nc) {
493 if (TextUtils.isEmpty(mNetworkSpecifier)) {
494 return TextUtils.isEmpty(nc.mNetworkSpecifier);
496 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
501 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
504 public void combineCapabilities(NetworkCapabilities nc) {
505 combineNetCapabilities(nc);
506 combineTransportTypes(nc);
507 combineLinkBandwidths(nc);
508 combineSpecifiers(nc);
512 * Check if our requirements are satisfied by the given Capabilities.
515 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
516 return (nc != null &&
517 satisfiedByNetCapabilities(nc) &&
518 satisfiedByTransportTypes(nc) &&
519 satisfiedByLinkBandwidths(nc) &&
520 satisfiedBySpecifier(nc));
524 public boolean equals(Object obj) {
525 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
526 NetworkCapabilities that = (NetworkCapabilities)obj;
527 return (equalsNetCapabilities(that) &&
528 equalsTransportTypes(that) &&
529 equalsLinkBandwidths(that) &&
530 equalsSpecifier(that));
534 public int hashCode() {
535 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
536 ((int)(mNetworkCapabilities >> 32) * 3) +
537 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
538 ((int)(mTransportTypes >> 32) * 7) +
539 (mLinkUpBandwidthKbps * 11) +
540 (mLinkDownBandwidthKbps * 13) +
541 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
544 public int describeContents() {
547 public void writeToParcel(Parcel dest, int flags) {
548 dest.writeLong(mNetworkCapabilities);
549 dest.writeLong(mTransportTypes);
550 dest.writeInt(mLinkUpBandwidthKbps);
551 dest.writeInt(mLinkDownBandwidthKbps);
552 dest.writeString(mNetworkSpecifier);
554 public static final Creator<NetworkCapabilities> CREATOR =
555 new Creator<NetworkCapabilities>() {
556 public NetworkCapabilities createFromParcel(Parcel in) {
557 NetworkCapabilities netCap = new NetworkCapabilities();
559 netCap.mNetworkCapabilities = in.readLong();
560 netCap.mTransportTypes = in.readLong();
561 netCap.mLinkUpBandwidthKbps = in.readInt();
562 netCap.mLinkDownBandwidthKbps = in.readInt();
563 netCap.mNetworkSpecifier = in.readString();
566 public NetworkCapabilities[] newArray(int size) {
567 return new NetworkCapabilities[size];
571 public String toString() {
572 int[] types = getTransportTypes();
573 String transports = (types.length > 0 ? " Transports: " : "");
574 for (int i = 0; i < types.length;) {
576 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
577 case TRANSPORT_WIFI: transports += "WIFI"; break;
578 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
579 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
580 case TRANSPORT_VPN: transports += "VPN"; break;
582 if (++i < types.length) transports += "|";
585 types = getCapabilities();
586 String capabilities = (types.length > 0 ? " Capabilities: " : "");
587 for (int i = 0; i < types.length; ) {
589 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
590 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
591 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
592 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
593 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
594 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
595 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
596 case NET_CAPABILITY_IA: capabilities += "IA"; break;
597 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
598 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
599 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
600 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
601 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
602 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
603 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
604 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
606 if (++i < types.length) capabilities += "&";
609 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
610 mLinkUpBandwidthKbps + "Kbps" : "");
611 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
612 mLinkDownBandwidthKbps + "Kbps" : "");
614 String specifier = (mNetworkSpecifier == null ?
615 "" : " Specifier: <" + mNetworkSpecifier + ">");
617 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";