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;
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.internal.util.BitUtils;
24 import com.android.internal.util.Preconditions;
26 import java.util.Objects;
29 * This class represents the capabilities of a network. This is used both to specify
30 * needs to {@link ConnectivityManager} and when inspecting a network.
32 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
33 * of network selection. Rather than indicate a need for Wi-Fi because an application
34 * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE),
35 * the application should specify it needs high bandwidth. Similarly if an application
36 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
37 * all cellular based connections are metered and all Wi-Fi based connections are not.
39 public final class NetworkCapabilities implements Parcelable {
40 private static final String TAG = "NetworkCapabilities";
45 public NetworkCapabilities() {
47 mNetworkCapabilities = DEFAULT_CAPABILITIES;
50 public NetworkCapabilities(NetworkCapabilities nc) {
52 mNetworkCapabilities = nc.mNetworkCapabilities;
53 mTransportTypes = nc.mTransportTypes;
54 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
55 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
56 mNetworkSpecifier = nc.mNetworkSpecifier;
57 mSignalStrength = nc.mSignalStrength;
62 * Completely clears the contents of this object, removing even the capabilities that are set
63 * by default when the object is constructed.
66 public void clearAll() {
67 mNetworkCapabilities = mTransportTypes = 0;
68 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
69 mNetworkSpecifier = null;
70 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
74 * Represents the network's capabilities. If any are specified they will be satisfied
75 * by any Network that matches all of them.
77 private long mNetworkCapabilities;
80 * Indicates this is a network that has the ability to reach the
81 * carrier's MMSC for sending and receiving MMS messages.
83 public static final int NET_CAPABILITY_MMS = 0;
86 * Indicates this is a network that has the ability to reach the carrier's
87 * SUPL server, used to retrieve GPS information.
89 public static final int NET_CAPABILITY_SUPL = 1;
92 * Indicates this is a network that has the ability to reach the carrier's
93 * DUN or tethering gateway.
95 public static final int NET_CAPABILITY_DUN = 2;
98 * Indicates this is a network that has the ability to reach the carrier's
99 * FOTA portal, used for over the air updates.
101 public static final int NET_CAPABILITY_FOTA = 3;
104 * Indicates this is a network that has the ability to reach the carrier's
105 * IMS servers, used for network registration and signaling.
107 public static final int NET_CAPABILITY_IMS = 4;
110 * Indicates this is a network that has the ability to reach the carrier's
111 * CBS servers, used for carrier specific services.
113 public static final int NET_CAPABILITY_CBS = 5;
116 * Indicates this is a network that has the ability to reach a Wi-Fi direct
119 public static final int NET_CAPABILITY_WIFI_P2P = 6;
122 * Indicates this is a network that has the ability to reach a carrier's
123 * Initial Attach servers.
125 public static final int NET_CAPABILITY_IA = 7;
128 * Indicates this is a network that has the ability to reach a carrier's
129 * RCS servers, used for Rich Communication Services.
131 public static final int NET_CAPABILITY_RCS = 8;
134 * Indicates this is a network that has the ability to reach a carrier's
135 * XCAP servers, used for configuration and control.
137 public static final int NET_CAPABILITY_XCAP = 9;
140 * Indicates this is a network that has the ability to reach a carrier's
141 * Emergency IMS servers or other services, used for network signaling
142 * during emergency calls.
144 public static final int NET_CAPABILITY_EIMS = 10;
147 * Indicates that this network is unmetered.
149 public static final int NET_CAPABILITY_NOT_METERED = 11;
152 * Indicates that this network should be able to reach the internet.
154 public static final int NET_CAPABILITY_INTERNET = 12;
157 * Indicates that this network is available for general use. If this is not set
158 * applications should not attempt to communicate on this network. Note that this
159 * is simply informative and not enforcement - enforcement is handled via other means.
162 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
165 * Indicates that the user has indicated implicit trust of this network. This
166 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
167 * BT device or a wifi the user asked to connect to. Untrusted networks
168 * are probably limited to unknown wifi AP. Set by default.
170 public static final int NET_CAPABILITY_TRUSTED = 14;
173 * Indicates that this network is not a VPN. This capability is set by default and should be
174 * explicitly cleared for VPN networks.
176 public static final int NET_CAPABILITY_NOT_VPN = 15;
179 * Indicates that connectivity on this network was successfully validated. For example, for a
180 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
183 public static final int NET_CAPABILITY_VALIDATED = 16;
186 * Indicates that this network was found to have a captive portal in place last time it was
189 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
192 * Indicates that this network is available for use by apps, and not a network that is being
193 * kept up in the background to facilitate fast network switching.
196 public static final int NET_CAPABILITY_FOREGROUND = 18;
198 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
199 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
202 * Network capabilities that are expected to be mutable, i.e., can change while a particular
203 * network is connected.
205 private static final long MUTABLE_CAPABILITIES =
206 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
208 (1 << NET_CAPABILITY_TRUSTED) |
209 (1 << NET_CAPABILITY_VALIDATED) |
210 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
211 (1 << NET_CAPABILITY_FOREGROUND);
214 * Network capabilities that are not allowed in NetworkRequests. This exists because the
215 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
216 * capability's presence cannot be known in advance. If such a capability is requested, then we
217 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
218 * get immediately torn down because they do not have the requested capability.
220 private static final long NON_REQUESTABLE_CAPABILITIES =
221 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
224 * Capabilities that are set by default when the object is constructed.
226 private static final long DEFAULT_CAPABILITIES =
227 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
228 (1 << NET_CAPABILITY_TRUSTED) |
229 (1 << NET_CAPABILITY_NOT_VPN);
232 * Capabilities that suggest that a network is restricted.
233 * {@see #maybeMarkCapabilitiesRestricted}.
236 /* package */ static final long RESTRICTED_CAPABILITIES =
237 (1 << NET_CAPABILITY_CBS) |
238 (1 << NET_CAPABILITY_DUN) |
239 (1 << NET_CAPABILITY_EIMS) |
240 (1 << NET_CAPABILITY_FOTA) |
241 (1 << NET_CAPABILITY_IA) |
242 (1 << NET_CAPABILITY_IMS) |
243 (1 << NET_CAPABILITY_RCS) |
244 (1 << NET_CAPABILITY_XCAP);
247 * Capabilities that suggest that a network is unrestricted.
248 * {@see #maybeMarkCapabilitiesRestricted}.
251 /* package */ static final long UNRESTRICTED_CAPABILITIES =
252 (1 << NET_CAPABILITY_INTERNET) |
253 (1 << NET_CAPABILITY_MMS) |
254 (1 << NET_CAPABILITY_SUPL) |
255 (1 << NET_CAPABILITY_WIFI_P2P);
258 * Adds the given capability to this {@code NetworkCapability} instance.
259 * Multiple capabilities may be applied sequentially. Note that when searching
260 * for a network to satisfy a request, all capabilities requested must be satisfied.
262 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
263 * @return This NetworkCapabilities instance, to facilitate chaining.
266 public NetworkCapabilities addCapability(int capability) {
267 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
268 throw new IllegalArgumentException("NetworkCapability out of range");
270 mNetworkCapabilities |= 1 << capability;
275 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
277 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
278 * @return This NetworkCapabilities instance, to facilitate chaining.
281 public NetworkCapabilities removeCapability(int capability) {
282 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
283 throw new IllegalArgumentException("NetworkCapability out of range");
285 mNetworkCapabilities &= ~(1 << capability);
290 * Gets all the capabilities set on this {@code NetworkCapability} instance.
292 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
296 public int[] getCapabilities() {
297 return BitUtils.unpackBits(mNetworkCapabilities);
301 * Tests for the presence of a capabilitity on this instance.
303 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
304 * @return {@code true} if set on this instance.
306 public boolean hasCapability(int capability) {
307 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
310 return ((mNetworkCapabilities & (1 << capability)) != 0);
313 private void combineNetCapabilities(NetworkCapabilities nc) {
314 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
318 * Convenience function that returns a human-readable description of the first mutable
319 * capability we find. Used to present an error message to apps that request mutable
324 public String describeFirstNonRequestableCapability() {
325 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
326 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
327 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
328 // This cannot happen unless the preceding checks are incomplete.
329 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
330 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
332 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
333 if (hasSignalStrength()) return "signalStrength";
337 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
338 long networkCapabilities = this.mNetworkCapabilities;
340 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
342 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
346 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
347 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
350 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
351 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
352 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
355 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
356 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
357 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
361 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
362 * typically provided by restricted networks.
365 * - Renaming it to guessRestrictedCapability and make it set the
366 * restricted capability bit in addition to clearing it.
369 public void maybeMarkCapabilitiesRestricted() {
370 // Verify there aren't any unrestricted capabilities. If there are we say
371 // the whole thing is unrestricted.
372 final boolean hasUnrestrictedCapabilities =
373 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
375 // Must have at least some restricted capabilities.
376 final boolean hasRestrictedCapabilities =
377 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
379 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
380 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
385 * Representing the transport type. Apps should generally not care about transport. A
386 * request for a fast internet connection could be satisfied by a number of different
387 * transports. If any are specified here it will be satisfied a Network that matches
388 * any of them. If a caller doesn't care about the transport it should not specify any.
390 private long mTransportTypes;
393 * Indicates this network uses a Cellular transport.
395 public static final int TRANSPORT_CELLULAR = 0;
398 * Indicates this network uses a Wi-Fi transport.
400 public static final int TRANSPORT_WIFI = 1;
403 * Indicates this network uses a Bluetooth transport.
405 public static final int TRANSPORT_BLUETOOTH = 2;
408 * Indicates this network uses an Ethernet transport.
410 public static final int TRANSPORT_ETHERNET = 3;
413 * Indicates this network uses a VPN transport.
415 public static final int TRANSPORT_VPN = 4;
418 * Indicates this network uses a Wi-Fi Aware transport.
420 public static final int TRANSPORT_WIFI_AWARE = 5;
423 * Indicates this network uses a LoWPAN transport.
426 public static final int TRANSPORT_LOWPAN = 6;
429 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
431 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
434 public static boolean isValidTransport(int transportType) {
435 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
438 private static final String[] TRANSPORT_NAMES = {
449 * Adds the given transport type to this {@code NetworkCapability} instance.
450 * Multiple transports may be applied sequentially. Note that when searching
451 * for a network to satisfy a request, any listed in the request will satisfy the request.
452 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
453 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
454 * to be selected. This is logically different than
455 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
457 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
458 * @return This NetworkCapabilities instance, to facilitate chaining.
461 public NetworkCapabilities addTransportType(int transportType) {
462 checkValidTransportType(transportType);
463 mTransportTypes |= 1 << transportType;
464 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
469 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
471 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
472 * @return This NetworkCapabilities instance, to facilitate chaining.
475 public NetworkCapabilities removeTransportType(int transportType) {
476 checkValidTransportType(transportType);
477 mTransportTypes &= ~(1 << transportType);
478 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
483 * Gets all the transports set on this {@code NetworkCapability} instance.
485 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
489 public int[] getTransportTypes() {
490 return BitUtils.unpackBits(mTransportTypes);
494 * Tests for the presence of a transport on this instance.
496 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
497 * @return {@code true} if set on this instance.
499 public boolean hasTransport(int transportType) {
500 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
503 private void combineTransportTypes(NetworkCapabilities nc) {
504 this.mTransportTypes |= nc.mTransportTypes;
506 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
507 return ((this.mTransportTypes == 0) ||
508 ((this.mTransportTypes & nc.mTransportTypes) != 0));
511 public boolean equalsTransportTypes(NetworkCapabilities nc) {
512 return (nc.mTransportTypes == this.mTransportTypes);
516 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
517 * for the first hop on the given transport. It is not measured, but may take into account
518 * link parameters (Radio technology, allocated channels, etc).
520 private int mLinkUpBandwidthKbps;
521 private int mLinkDownBandwidthKbps;
524 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
525 * the estimated first hop transport bandwidth.
527 * Note that when used to request a network, this specifies the minimum acceptable.
528 * When received as the state of an existing network this specifies the typical
529 * first hop bandwidth expected. This is never measured, but rather is inferred
530 * from technology type and other link parameters. It could be used to differentiate
531 * between very slow 1xRTT cellular links and other faster networks or even between
532 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
533 * fast backhauls and slow backhauls.
535 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
538 public void setLinkUpstreamBandwidthKbps(int upKbps) {
539 mLinkUpBandwidthKbps = upKbps;
543 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
544 * the estimated first hop transport bandwidth.
546 * @return The estimated first hop upstream (device to network) bandwidth.
548 public int getLinkUpstreamBandwidthKbps() {
549 return mLinkUpBandwidthKbps;
553 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
554 * the estimated first hop transport bandwidth.
556 * Note that when used to request a network, this specifies the minimum acceptable.
557 * When received as the state of an existing network this specifies the typical
558 * first hop bandwidth expected. This is never measured, but rather is inferred
559 * from technology type and other link parameters. It could be used to differentiate
560 * between very slow 1xRTT cellular links and other faster networks or even between
561 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
562 * fast backhauls and slow backhauls.
564 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
567 public void setLinkDownstreamBandwidthKbps(int downKbps) {
568 mLinkDownBandwidthKbps = downKbps;
572 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
573 * the estimated first hop transport bandwidth.
575 * @return The estimated first hop downstream (network to device) bandwidth.
577 public int getLinkDownstreamBandwidthKbps() {
578 return mLinkDownBandwidthKbps;
581 private void combineLinkBandwidths(NetworkCapabilities nc) {
582 this.mLinkUpBandwidthKbps =
583 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
584 this.mLinkDownBandwidthKbps =
585 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
587 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
588 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
589 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
591 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
592 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
593 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
596 private NetworkSpecifier mNetworkSpecifier = null;
599 * Sets the optional bearer specific network specifier.
600 * This has no meaning if a single transport is also not specified, so calling
601 * this without a single transport set will generate an exception, as will
602 * subsequently adding or removing transports after this is set.
605 * @param networkSpecifier A concrete, parcelable framework class that extends
607 * @return This NetworkCapabilities instance, to facilitate chaining.
610 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
611 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
612 throw new IllegalStateException("Must have a single transport specified to use " +
613 "setNetworkSpecifier");
616 mNetworkSpecifier = networkSpecifier;
622 * Gets the optional bearer specific network specifier.
624 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
625 * specifier. See {@link #setNetworkSpecifier}.
628 public NetworkSpecifier getNetworkSpecifier() {
629 return mNetworkSpecifier;
632 private void combineSpecifiers(NetworkCapabilities nc) {
633 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
634 throw new IllegalStateException("Can't combine two networkSpecifiers");
636 setNetworkSpecifier(nc.mNetworkSpecifier);
639 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
640 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
641 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
644 private boolean equalsSpecifier(NetworkCapabilities nc) {
645 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
649 * Magic value that indicates no signal strength provided. A request specifying this value is
654 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
657 * Signal strength. This is a signed integer, and higher values indicate better signal.
658 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
660 private int mSignalStrength;
663 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
664 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
665 * reported by WifiManager.
667 * Note that when used to register a network callback, this specifies the minimum acceptable
668 * signal strength. When received as the state of an existing network it specifies the current
669 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
670 * effect when requesting a callback.
672 * @param signalStrength the bearer-specific signal strength.
675 public void setSignalStrength(int signalStrength) {
676 mSignalStrength = signalStrength;
680 * Returns {@code true} if this object specifies a signal strength.
684 public boolean hasSignalStrength() {
685 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
689 * Retrieves the signal strength.
691 * @return The bearer-specific signal strength.
694 public int getSignalStrength() {
695 return mSignalStrength;
698 private void combineSignalStrength(NetworkCapabilities nc) {
699 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
702 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
703 return this.mSignalStrength <= nc.mSignalStrength;
706 private boolean equalsSignalStrength(NetworkCapabilities nc) {
707 return this.mSignalStrength == nc.mSignalStrength;
711 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
714 public void combineCapabilities(NetworkCapabilities nc) {
715 combineNetCapabilities(nc);
716 combineTransportTypes(nc);
717 combineLinkBandwidths(nc);
718 combineSpecifiers(nc);
719 combineSignalStrength(nc);
723 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
725 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
726 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
727 * bandwidth, signal strength, or validation / captive portal status.
731 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
732 return (nc != null &&
733 satisfiedByNetCapabilities(nc, onlyImmutable) &&
734 satisfiedByTransportTypes(nc) &&
735 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
736 satisfiedBySpecifier(nc) &&
737 (onlyImmutable || satisfiedBySignalStrength(nc)));
741 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
743 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
747 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
748 return satisfiedByNetworkCapabilities(nc, false);
752 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
754 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
758 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
759 return satisfiedByNetworkCapabilities(nc, true);
763 * Checks that our immutable capabilities are the same as those of the given
764 * {@code NetworkCapabilities}.
768 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
769 if (nc == null) return false;
770 return (equalsNetCapabilitiesImmutable(nc) &&
771 equalsTransportTypes(nc) &&
772 equalsSpecifier(nc));
776 * Checks that our requestable capabilities are the same as those of the given
777 * {@code NetworkCapabilities}.
781 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
782 if (nc == null) return false;
783 return (equalsNetCapabilitiesRequestable(nc) &&
784 equalsTransportTypes(nc) &&
785 equalsSpecifier(nc));
789 public boolean equals(Object obj) {
790 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
791 NetworkCapabilities that = (NetworkCapabilities)obj;
792 return (equalsNetCapabilities(that) &&
793 equalsTransportTypes(that) &&
794 equalsLinkBandwidths(that) &&
795 equalsSignalStrength(that) &&
796 equalsSpecifier(that));
800 public int hashCode() {
801 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
802 ((int)(mNetworkCapabilities >> 32) * 3) +
803 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
804 ((int)(mTransportTypes >> 32) * 7) +
805 (mLinkUpBandwidthKbps * 11) +
806 (mLinkDownBandwidthKbps * 13) +
807 Objects.hashCode(mNetworkSpecifier) * 17 +
808 (mSignalStrength * 19));
812 public int describeContents() {
816 public void writeToParcel(Parcel dest, int flags) {
817 dest.writeLong(mNetworkCapabilities);
818 dest.writeLong(mTransportTypes);
819 dest.writeInt(mLinkUpBandwidthKbps);
820 dest.writeInt(mLinkDownBandwidthKbps);
821 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
822 dest.writeInt(mSignalStrength);
825 public static final Creator<NetworkCapabilities> CREATOR =
826 new Creator<NetworkCapabilities>() {
828 public NetworkCapabilities createFromParcel(Parcel in) {
829 NetworkCapabilities netCap = new NetworkCapabilities();
831 netCap.mNetworkCapabilities = in.readLong();
832 netCap.mTransportTypes = in.readLong();
833 netCap.mLinkUpBandwidthKbps = in.readInt();
834 netCap.mLinkDownBandwidthKbps = in.readInt();
835 netCap.mNetworkSpecifier = in.readParcelable(null);
836 netCap.mSignalStrength = in.readInt();
840 public NetworkCapabilities[] newArray(int size) {
841 return new NetworkCapabilities[size];
846 public String toString() {
847 int[] types = getTransportTypes();
848 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
850 types = getCapabilities();
851 String capabilities = (types.length > 0 ? " Capabilities: " : "");
852 for (int i = 0; i < types.length; ) {
854 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
855 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
856 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
857 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
858 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
859 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
860 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
861 case NET_CAPABILITY_IA: capabilities += "IA"; break;
862 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
863 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
864 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
865 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
866 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
867 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
868 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
869 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
870 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
871 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
872 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
874 if (++i < types.length) capabilities += "&";
877 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
878 mLinkUpBandwidthKbps + "Kbps" : "");
879 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
880 mLinkDownBandwidthKbps + "Kbps" : "");
882 String specifier = (mNetworkSpecifier == null ?
883 "" : " Specifier: <" + mNetworkSpecifier + ">");
885 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
887 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
893 public static String transportNamesOf(int[] types) {
894 if (types == null || types.length == 0) {
897 StringBuilder transports = new StringBuilder();
898 for (int t : types) {
899 transports.append("|").append(transportNameOf(t));
901 return transports.substring(1);
907 public static String transportNameOf(int transport) {
908 if (!isValidTransport(transport)) {
911 return TRANSPORT_NAMES[transport];
914 private static void checkValidTransportType(int transport) {
915 Preconditions.checkArgument(
916 isValidTransport(transport), "Invalid TransportType " + transport);