blob: ee75fd4430522bcd86bd57d72438043dd7365b57 [file] [log] [blame]
Robert Greenwalt1448f052014-04-08 13:41:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.net;
18
Jeff Sharkeyde570312017-10-24 21:25:50 -060019import android.annotation.IntDef;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060020import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070021import android.os.Parcel;
22import android.os.Parcelable;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070023
24import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090025import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090026import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070027
Jeff Sharkeyde570312017-10-24 21:25:50 -060028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070030import java.util.Objects;
Hugo Benichieae7a222017-07-25 11:40:56 +090031import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070032
33/**
Jeff Sharkey72f9c422017-10-27 17:22:59 -060034 * Representation of the capabilities of a network. This object serves two
35 * purposes:
36 * <ul>
37 * <li>An expression of the current capabilities of an active network, typically
38 * expressed through
39 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
40 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
41 * <li>An expression of the future capabilities of a desired network, typically
42 * expressed through {@link NetworkRequest}.
43 * </ul>
44 * <p>
45 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
46 * network selection. Rather than indicate a need for Wi-Fi because an
47 * application needs high bandwidth and risk obsolescence when a new, fast
48 * network appears (like LTE), the application should specify it needs high
49 * bandwidth. Similarly if an application needs an unmetered network for a bulk
50 * transfer it can specify that rather than assuming all cellular based
51 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070052 */
53public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070054 private static final String TAG = "NetworkCapabilities";
55
Robert Greenwalt7569f182014-06-08 16:42:59 -070056 /**
57 * @hide
58 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070059 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090060 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090061 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070062 }
63
64 public NetworkCapabilities(NetworkCapabilities nc) {
65 if (nc != null) {
66 mNetworkCapabilities = nc.mNetworkCapabilities;
67 mTransportTypes = nc.mTransportTypes;
68 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
69 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070070 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090071 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070072 }
73 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070074
75 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090076 * Completely clears the contents of this object, removing even the capabilities that are set
77 * by default when the object is constructed.
78 * @hide
79 */
80 public void clearAll() {
81 mNetworkCapabilities = mTransportTypes = 0;
82 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
83 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090084 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090085 }
86
87 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070088 * Represents the network's capabilities. If any are specified they will be satisfied
89 * by any Network that matches all of them.
90 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070092
Jeff Sharkeyde570312017-10-24 21:25:50 -060093 /** @hide */
94 @Retention(RetentionPolicy.SOURCE)
95 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
96 NET_CAPABILITY_MMS,
97 NET_CAPABILITY_SUPL,
98 NET_CAPABILITY_DUN,
99 NET_CAPABILITY_FOTA,
100 NET_CAPABILITY_IMS,
101 NET_CAPABILITY_CBS,
102 NET_CAPABILITY_WIFI_P2P,
103 NET_CAPABILITY_IA,
104 NET_CAPABILITY_RCS,
105 NET_CAPABILITY_XCAP,
106 NET_CAPABILITY_EIMS,
107 NET_CAPABILITY_NOT_METERED,
108 NET_CAPABILITY_INTERNET,
109 NET_CAPABILITY_NOT_RESTRICTED,
110 NET_CAPABILITY_TRUSTED,
111 NET_CAPABILITY_NOT_VPN,
112 NET_CAPABILITY_VALIDATED,
113 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600114 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600115 NET_CAPABILITY_FOREGROUND,
116 })
117 public @interface NetCapability { }
118
Robert Greenwalt1448f052014-04-08 13:41:39 -0700119 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700120 * Indicates this is a network that has the ability to reach the
121 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700122 */
123 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700124
125 /**
126 * Indicates this is a network that has the ability to reach the carrier's
127 * SUPL server, used to retrieve GPS information.
128 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700129 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700130
131 /**
132 * Indicates this is a network that has the ability to reach the carrier's
133 * DUN or tethering gateway.
134 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700135 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700136
137 /**
138 * Indicates this is a network that has the ability to reach the carrier's
139 * FOTA portal, used for over the air updates.
140 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700141 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700142
143 /**
144 * Indicates this is a network that has the ability to reach the carrier's
145 * IMS servers, used for network registration and signaling.
146 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700147 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700148
149 /**
150 * Indicates this is a network that has the ability to reach the carrier's
151 * CBS servers, used for carrier specific services.
152 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700153 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700154
155 /**
156 * Indicates this is a network that has the ability to reach a Wi-Fi direct
157 * peer.
158 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700159 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700160
161 /**
162 * Indicates this is a network that has the ability to reach a carrier's
163 * Initial Attach servers.
164 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700165 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700166
167 /**
168 * Indicates this is a network that has the ability to reach a carrier's
169 * RCS servers, used for Rich Communication Services.
170 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700171 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700172
173 /**
174 * Indicates this is a network that has the ability to reach a carrier's
175 * XCAP servers, used for configuration and control.
176 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700177 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178
179 /**
180 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700181 * Emergency IMS servers or other services, used for network signaling
182 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700183 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700184 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700185
186 /**
187 * Indicates that this network is unmetered.
188 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700189 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190
191 /**
192 * Indicates that this network should be able to reach the internet.
193 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195
196 /**
197 * Indicates that this network is available for general use. If this is not set
198 * applications should not attempt to communicate on this network. Note that this
199 * is simply informative and not enforcement - enforcement is handled via other means.
200 * Set by default.
201 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700202 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
203
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700204 /**
205 * Indicates that the user has indicated implicit trust of this network. This
206 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
207 * BT device or a wifi the user asked to connect to. Untrusted networks
208 * are probably limited to unknown wifi AP. Set by default.
209 */
210 public static final int NET_CAPABILITY_TRUSTED = 14;
211
Paul Jensen76b610a2015-03-18 09:33:07 -0400212 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400213 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400214 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400215 */
216 public static final int NET_CAPABILITY_NOT_VPN = 15;
217
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900218 /**
219 * Indicates that connectivity on this network was successfully validated. For example, for a
220 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
221 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900222 */
223 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700224
Paul Jensen3d194ea2015-06-16 14:27:36 -0400225 /**
226 * Indicates that this network was found to have a captive portal in place last time it was
227 * probed.
228 */
229 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
230
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900231 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600232 * Indicates that this network is not roaming.
233 */
234 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
235
236 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900237 * Indicates that this network is available for use by apps, and not a network that is being
238 * kept up in the background to facilitate fast network switching.
239 * @hide
240 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600241 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900242
Robert Greenwalt1448f052014-04-08 13:41:39 -0700243 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900244 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700245
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700246 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900247 * Network capabilities that are expected to be mutable, i.e., can change while a particular
248 * network is connected.
249 */
250 private static final long MUTABLE_CAPABILITIES =
251 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
252 // http://b/18206275
253 (1 << NET_CAPABILITY_TRUSTED) |
254 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900255 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600256 (1 << NET_CAPABILITY_NOT_ROAMING) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900257 (1 << NET_CAPABILITY_FOREGROUND);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900258
259 /**
260 * Network capabilities that are not allowed in NetworkRequests. This exists because the
261 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
262 * capability's presence cannot be known in advance. If such a capability is requested, then we
263 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
264 * get immediately torn down because they do not have the requested capability.
265 */
266 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900267 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900268
269 /**
270 * Capabilities that are set by default when the object is constructed.
271 */
272 private static final long DEFAULT_CAPABILITIES =
273 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
274 (1 << NET_CAPABILITY_TRUSTED) |
275 (1 << NET_CAPABILITY_NOT_VPN);
276
277 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400278 * Capabilities that suggest that a network is restricted.
279 * {@see #maybeMarkCapabilitiesRestricted}.
280 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700281 @VisibleForTesting
282 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400283 (1 << NET_CAPABILITY_CBS) |
284 (1 << NET_CAPABILITY_DUN) |
285 (1 << NET_CAPABILITY_EIMS) |
286 (1 << NET_CAPABILITY_FOTA) |
287 (1 << NET_CAPABILITY_IA) |
288 (1 << NET_CAPABILITY_IMS) |
289 (1 << NET_CAPABILITY_RCS) |
290 (1 << NET_CAPABILITY_XCAP);
291
292 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700293 * Capabilities that suggest that a network is unrestricted.
294 * {@see #maybeMarkCapabilitiesRestricted}.
295 */
296 @VisibleForTesting
297 /* package */ static final long UNRESTRICTED_CAPABILITIES =
298 (1 << NET_CAPABILITY_INTERNET) |
299 (1 << NET_CAPABILITY_MMS) |
300 (1 << NET_CAPABILITY_SUPL) |
301 (1 << NET_CAPABILITY_WIFI_P2P);
302
303 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700304 * Adds the given capability to this {@code NetworkCapability} instance.
305 * Multiple capabilities may be applied sequentially. Note that when searching
306 * for a network to satisfy a request, all capabilities requested must be satisfied.
307 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600308 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900309 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700310 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700311 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600312 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700313 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700314 throw new IllegalArgumentException("NetworkCapability out of range");
315 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700316 mNetworkCapabilities |= 1 << capability;
317 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700318 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700319
320 /**
321 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
322 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600323 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900324 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700325 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700326 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600327 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700328 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700329 throw new IllegalArgumentException("NetworkCapability out of range");
330 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700331 mNetworkCapabilities &= ~(1 << capability);
332 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700333 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700334
335 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600336 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
337 * instance.
338 *
339 * @hide
340 */
341 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
342 if (value) {
343 addCapability(capability);
344 } else {
345 removeCapability(capability);
346 }
347 return this;
348 }
349
350 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700351 * Gets all the capabilities set on this {@code NetworkCapability} instance.
352 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600353 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700354 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700355 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600356 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900357 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700358 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700359
360 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600361 * Sets all the capabilities set on this {@code NetworkCapability} instance.
362 *
363 * @hide
364 */
365 public void setCapabilities(@NetCapability int[] capabilities) {
366 mNetworkCapabilities = BitUtils.packBits(capabilities);
367 }
368
369 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700370 * Tests for the presence of a capabilitity on this instance.
371 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600372 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700373 * @return {@code true} if set on this instance.
374 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600375 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700376 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700377 return false;
378 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700379 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700380 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700381
Robert Greenwalt1448f052014-04-08 13:41:39 -0700382 private void combineNetCapabilities(NetworkCapabilities nc) {
383 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
384 }
385
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900386 /**
387 * Convenience function that returns a human-readable description of the first mutable
388 * capability we find. Used to present an error message to apps that request mutable
389 * capabilities.
390 *
391 * @hide
392 */
393 public String describeFirstNonRequestableCapability() {
394 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
395 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900396 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900397 // This cannot happen unless the preceding checks are incomplete.
398 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
399 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
400 }
401 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900402 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900403 return null;
404 }
405
406 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
407 long networkCapabilities = this.mNetworkCapabilities;
408 if (onlyImmutable) {
409 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
410 }
411 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700412 }
413
Robert Greenwalt06314e42014-10-29 14:04:06 -0700414 /** @hide */
415 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700416 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
417 }
418
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900419 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
420 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
421 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
422 }
423
Robert Greenwalt1448f052014-04-08 13:41:39 -0700424 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400425 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
426 * typically provided by restricted networks.
427 *
428 * TODO: consider:
429 * - Renaming it to guessRestrictedCapability and make it set the
430 * restricted capability bit in addition to clearing it.
431 * @hide
432 */
433 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700434 // Verify there aren't any unrestricted capabilities. If there are we say
435 // the whole thing is unrestricted.
436 final boolean hasUnrestrictedCapabilities =
437 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
438
439 // Must have at least some restricted capabilities.
440 final boolean hasRestrictedCapabilities =
441 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
442
443 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400444 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400445 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400446 }
447
448 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700449 * Representing the transport type. Apps should generally not care about transport. A
450 * request for a fast internet connection could be satisfied by a number of different
451 * transports. If any are specified here it will be satisfied a Network that matches
452 * any of them. If a caller doesn't care about the transport it should not specify any.
453 */
454 private long mTransportTypes;
455
Jeff Sharkeyde570312017-10-24 21:25:50 -0600456 /** @hide */
457 @Retention(RetentionPolicy.SOURCE)
458 @IntDef(prefix = { "TRANSPORT_" }, value = {
459 TRANSPORT_CELLULAR,
460 TRANSPORT_WIFI,
461 TRANSPORT_BLUETOOTH,
462 TRANSPORT_ETHERNET,
463 TRANSPORT_VPN,
464 TRANSPORT_WIFI_AWARE,
465 TRANSPORT_LOWPAN,
466 })
467 public @interface Transport { }
468
Robert Greenwalt1448f052014-04-08 13:41:39 -0700469 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700470 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700471 */
472 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473
474 /**
475 * Indicates this network uses a Wi-Fi transport.
476 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700477 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478
479 /**
480 * Indicates this network uses a Bluetooth transport.
481 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700482 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700483
484 /**
485 * Indicates this network uses an Ethernet transport.
486 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700487 public static final int TRANSPORT_ETHERNET = 3;
488
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400489 /**
490 * Indicates this network uses a VPN transport.
491 */
492 public static final int TRANSPORT_VPN = 4;
493
Etan Cohen305ea282016-06-20 09:27:12 -0700494 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700495 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700496 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700497 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700498
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700499 /**
500 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700501 */
502 public static final int TRANSPORT_LOWPAN = 6;
503
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900504 /** @hide */
505 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
506 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700507 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700508
Hugo Benichi16f0a942017-06-20 14:07:59 +0900509 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600510 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900511 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
512 }
513
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900514 private static final String[] TRANSPORT_NAMES = {
515 "CELLULAR",
516 "WIFI",
517 "BLUETOOTH",
518 "ETHERNET",
519 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700520 "WIFI_AWARE",
521 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900522 };
523
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700524 /**
525 * Adds the given transport type to this {@code NetworkCapability} instance.
526 * Multiple transports may be applied sequentially. Note that when searching
527 * for a network to satisfy a request, any listed in the request will satisfy the request.
528 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
529 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
530 * to be selected. This is logically different than
531 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
532 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600533 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900534 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700535 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700536 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600537 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900538 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700539 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700540 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700541 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700542 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700543
544 /**
545 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
546 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600547 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900548 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700549 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700550 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600551 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900552 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700553 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700554 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700555 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700556 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700557
558 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600559 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
560 * instance.
561 *
562 * @hide
563 */
564 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
565 if (value) {
566 addTransportType(transportType);
567 } else {
568 removeTransportType(transportType);
569 }
570 return this;
571 }
572
573 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700574 * Gets all the transports set on this {@code NetworkCapability} instance.
575 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600576 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700577 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700578 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600579 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900580 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700581 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700582
583 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600584 * Sets all the transports set on this {@code NetworkCapability} instance.
585 *
586 * @hide
587 */
588 public void setTransportTypes(@Transport int[] transportTypes) {
589 mTransportTypes = BitUtils.packBits(transportTypes);
590 }
591
592 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700593 * Tests for the presence of a transport on this instance.
594 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600595 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700596 * @return {@code true} if set on this instance.
597 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600598 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900599 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700600 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700601
602 private void combineTransportTypes(NetworkCapabilities nc) {
603 this.mTransportTypes |= nc.mTransportTypes;
604 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900605
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
607 return ((this.mTransportTypes == 0) ||
608 ((this.mTransportTypes & nc.mTransportTypes) != 0));
609 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900610
Robert Greenwalt06314e42014-10-29 14:04:06 -0700611 /** @hide */
612 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613 return (nc.mTransportTypes == this.mTransportTypes);
614 }
615
616 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600617 * Value indicating that link bandwidth is unspecified.
618 * @hide
619 */
620 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
621
622 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700623 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
624 * for the first hop on the given transport. It is not measured, but may take into account
625 * link parameters (Radio technology, allocated channels, etc).
626 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600627 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
628 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700629
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700630 /**
631 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
632 * the estimated first hop transport bandwidth.
633 * <p>
634 * Note that when used to request a network, this specifies the minimum acceptable.
635 * When received as the state of an existing network this specifies the typical
636 * first hop bandwidth expected. This is never measured, but rather is inferred
637 * from technology type and other link parameters. It could be used to differentiate
638 * between very slow 1xRTT cellular links and other faster networks or even between
639 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
640 * fast backhauls and slow backhauls.
641 *
642 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700643 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700644 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600645 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700646 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600647 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700648 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700649
650 /**
651 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
652 * the estimated first hop transport bandwidth.
653 *
654 * @return The estimated first hop upstream (device to network) bandwidth.
655 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700656 public int getLinkUpstreamBandwidthKbps() {
657 return mLinkUpBandwidthKbps;
658 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700659
660 /**
661 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
662 * the estimated first hop transport bandwidth.
663 * <p>
664 * Note that when used to request a network, this specifies the minimum acceptable.
665 * When received as the state of an existing network this specifies the typical
666 * first hop bandwidth expected. This is never measured, but rather is inferred
667 * from technology type and other link parameters. It could be used to differentiate
668 * between very slow 1xRTT cellular links and other faster networks or even between
669 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
670 * fast backhauls and slow backhauls.
671 *
672 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700673 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600675 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700676 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600677 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700678 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700679
680 /**
681 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
682 * the estimated first hop transport bandwidth.
683 *
684 * @return The estimated first hop downstream (network to device) bandwidth.
685 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700686 public int getLinkDownstreamBandwidthKbps() {
687 return mLinkDownBandwidthKbps;
688 }
689
690 private void combineLinkBandwidths(NetworkCapabilities nc) {
691 this.mLinkUpBandwidthKbps =
692 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
693 this.mLinkDownBandwidthKbps =
694 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
695 }
696 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
697 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
698 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
699 }
700 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
701 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
702 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
703 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600704 /** @hide */
705 public static int minBandwidth(int a, int b) {
706 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
707 return b;
708 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
709 return a;
710 } else {
711 return Math.min(a, b);
712 }
713 }
714 /** @hide */
715 public static int maxBandwidth(int a, int b) {
716 return Math.max(a, b);
717 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700718
Etan Cohena7434272017-04-03 12:17:51 -0700719 private NetworkSpecifier mNetworkSpecifier = null;
720
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700721 /**
722 * Sets the optional bearer specific network specifier.
723 * This has no meaning if a single transport is also not specified, so calling
724 * this without a single transport set will generate an exception, as will
725 * subsequently adding or removing transports after this is set.
726 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700727 *
Etan Cohena7434272017-04-03 12:17:51 -0700728 * @param networkSpecifier A concrete, parcelable framework class that extends
729 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900730 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700731 * @hide
732 */
Etan Cohena7434272017-04-03 12:17:51 -0700733 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
734 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700735 throw new IllegalStateException("Must have a single transport specified to use " +
736 "setNetworkSpecifier");
737 }
Etan Cohena7434272017-04-03 12:17:51 -0700738
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700739 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700740
Pierre Imaic8419a82016-03-22 17:54:54 +0900741 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700742 }
743
744 /**
745 * Gets the optional bearer specific network specifier.
746 *
Etan Cohena7434272017-04-03 12:17:51 -0700747 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
748 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700749 * @hide
750 */
Etan Cohena7434272017-04-03 12:17:51 -0700751 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700752 return mNetworkSpecifier;
753 }
754
755 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700756 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700757 throw new IllegalStateException("Can't combine two networkSpecifiers");
758 }
Etan Cohena7434272017-04-03 12:17:51 -0700759 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700760 }
Etan Cohena7434272017-04-03 12:17:51 -0700761
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700762 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700763 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
764 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700765 }
Etan Cohena7434272017-04-03 12:17:51 -0700766
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700767 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700768 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700769 }
770
Robert Greenwalt1448f052014-04-08 13:41:39 -0700771 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900772 * Magic value that indicates no signal strength provided. A request specifying this value is
773 * always satisfied.
774 *
775 * @hide
776 */
777 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
778
779 /**
780 * Signal strength. This is a signed integer, and higher values indicate better signal.
781 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
782 */
783 private int mSignalStrength;
784
785 /**
786 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
787 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
788 * reported by WifiManager.
789 * <p>
790 * Note that when used to register a network callback, this specifies the minimum acceptable
791 * signal strength. When received as the state of an existing network it specifies the current
792 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
793 * effect when requesting a callback.
794 *
795 * @param signalStrength the bearer-specific signal strength.
796 * @hide
797 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600798 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900799 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600800 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900801 }
802
803 /**
804 * Returns {@code true} if this object specifies a signal strength.
805 *
806 * @hide
807 */
808 public boolean hasSignalStrength() {
809 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
810 }
811
812 /**
813 * Retrieves the signal strength.
814 *
815 * @return The bearer-specific signal strength.
816 * @hide
817 */
818 public int getSignalStrength() {
819 return mSignalStrength;
820 }
821
822 private void combineSignalStrength(NetworkCapabilities nc) {
823 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
824 }
825
826 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
827 return this.mSignalStrength <= nc.mSignalStrength;
828 }
829
830 private boolean equalsSignalStrength(NetworkCapabilities nc) {
831 return this.mSignalStrength == nc.mSignalStrength;
832 }
833
834 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700835 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900836 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700837 */
838 public void combineCapabilities(NetworkCapabilities nc) {
839 combineNetCapabilities(nc);
840 combineTransportTypes(nc);
841 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700842 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900843 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700844 }
845
846 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900847 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
848 *
849 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
850 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
851 * bandwidth, signal strength, or validation / captive portal status.
852 *
853 * @hide
854 */
855 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
856 return (nc != null &&
857 satisfiedByNetCapabilities(nc, onlyImmutable) &&
858 satisfiedByTransportTypes(nc) &&
859 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900860 satisfiedBySpecifier(nc) &&
861 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900862 }
863
864 /**
865 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
866 *
867 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
868 *
869 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700870 */
871 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900872 return satisfiedByNetworkCapabilities(nc, false);
873 }
874
875 /**
876 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
877 *
878 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
879 *
880 * @hide
881 */
882 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
883 return satisfiedByNetworkCapabilities(nc, true);
884 }
885
886 /**
887 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +0900888 * {@code NetworkCapabilities} and return a String describing any difference.
889 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900890 *
891 * @hide
892 */
Hugo Benichieae7a222017-07-25 11:40:56 +0900893 public String describeImmutableDifferences(NetworkCapabilities that) {
894 if (that == null) {
895 return "other NetworkCapabilities was null";
896 }
897
898 StringJoiner joiner = new StringJoiner(", ");
899
Hugo Benichieae7a222017-07-25 11:40:56 +0900900 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
901 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +0900902 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +0900903 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
904 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
905 if (oldImmutableCapabilities != newImmutableCapabilities) {
906 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
907 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
908 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
909 }
910
911 if (!equalsSpecifier(that)) {
912 NetworkSpecifier before = this.getNetworkSpecifier();
913 NetworkSpecifier after = that.getNetworkSpecifier();
914 joiner.add(String.format("specifier changed: %s -> %s", before, after));
915 }
916
917 if (!equalsTransportTypes(that)) {
918 String before = transportNamesOf(this.getTransportTypes());
919 String after = transportNamesOf(that.getTransportTypes());
920 joiner.add(String.format("transports changed: %s -> %s", before, after));
921 }
922
923 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700924 }
925
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900926 /**
927 * Checks that our requestable capabilities are the same as those of the given
928 * {@code NetworkCapabilities}.
929 *
930 * @hide
931 */
932 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
933 if (nc == null) return false;
934 return (equalsNetCapabilitiesRequestable(nc) &&
935 equalsTransportTypes(nc) &&
936 equalsSpecifier(nc));
937 }
938
Robert Greenwalt1448f052014-04-08 13:41:39 -0700939 @Override
940 public boolean equals(Object obj) {
941 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
942 NetworkCapabilities that = (NetworkCapabilities)obj;
943 return (equalsNetCapabilities(that) &&
944 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700945 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900946 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700947 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700948 }
949
950 @Override
951 public int hashCode() {
952 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
953 ((int)(mNetworkCapabilities >> 32) * 3) +
954 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
955 ((int)(mTransportTypes >> 32) * 7) +
956 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700957 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700958 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900959 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700960 }
961
Wink Saville4e2dea72014-09-20 11:04:03 -0700962 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700963 public int describeContents() {
964 return 0;
965 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700966 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700967 public void writeToParcel(Parcel dest, int flags) {
968 dest.writeLong(mNetworkCapabilities);
969 dest.writeLong(mTransportTypes);
970 dest.writeInt(mLinkUpBandwidthKbps);
971 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700972 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900973 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700974 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900975
Robert Greenwalt1448f052014-04-08 13:41:39 -0700976 public static final Creator<NetworkCapabilities> CREATOR =
977 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700978 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700979 public NetworkCapabilities createFromParcel(Parcel in) {
980 NetworkCapabilities netCap = new NetworkCapabilities();
981
982 netCap.mNetworkCapabilities = in.readLong();
983 netCap.mTransportTypes = in.readLong();
984 netCap.mLinkUpBandwidthKbps = in.readInt();
985 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700986 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900987 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700988 return netCap;
989 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700990 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700991 public NetworkCapabilities[] newArray(int size) {
992 return new NetworkCapabilities[size];
993 }
994 };
995
Wink Saville4e2dea72014-09-20 11:04:03 -0700996 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700997 public String toString() {
Hugo Benichieae7a222017-07-25 11:40:56 +0900998 // TODO: enumerate bits for transports and capabilities instead of creating arrays.
999 // TODO: use a StringBuilder instead of string concatenation.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001000 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +09001001 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001002
Robert Greenwalt7569f182014-06-08 16:42:59 -07001003 types = getCapabilities();
1004 String capabilities = (types.length > 0 ? " Capabilities: " : "");
1005 for (int i = 0; i < types.length; ) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001006 capabilities += capabilityNameOf(types[i]);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001007 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001008 }
1009
1010 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
1011 mLinkUpBandwidthKbps + "Kbps" : "");
1012 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
1013 mLinkDownBandwidthKbps + "Kbps" : "");
1014
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001015 String specifier = (mNetworkSpecifier == null ?
1016 "" : " Specifier: <" + mNetworkSpecifier + ">");
1017
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001018 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
1019
1020 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001021 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001022
1023 /**
1024 * @hide
1025 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001026 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001027 StringJoiner joiner = new StringJoiner("|");
1028 if (capabilities != null) {
1029 for (int c : capabilities) {
1030 joiner.add(capabilityNameOf(c));
1031 }
1032 }
1033 return joiner.toString();
1034 }
1035
1036 /**
1037 * @hide
1038 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001039 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001040 switch (capability) {
1041 case NET_CAPABILITY_MMS: return "MMS";
1042 case NET_CAPABILITY_SUPL: return "SUPL";
1043 case NET_CAPABILITY_DUN: return "DUN";
1044 case NET_CAPABILITY_FOTA: return "FOTA";
1045 case NET_CAPABILITY_IMS: return "IMS";
1046 case NET_CAPABILITY_CBS: return "CBS";
1047 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1048 case NET_CAPABILITY_IA: return "IA";
1049 case NET_CAPABILITY_RCS: return "RCS";
1050 case NET_CAPABILITY_XCAP: return "XCAP";
1051 case NET_CAPABILITY_EIMS: return "EIMS";
1052 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1053 case NET_CAPABILITY_INTERNET: return "INTERNET";
1054 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1055 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1056 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1057 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1058 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001059 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001060 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1061 default: return Integer.toString(capability);
1062 }
1063 }
1064
1065 /**
1066 * @hide
1067 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001068 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001069 StringJoiner joiner = new StringJoiner("|");
1070 if (types != null) {
1071 for (int t : types) {
1072 joiner.add(transportNameOf(t));
1073 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001074 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001075 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001076 }
1077
1078 /**
1079 * @hide
1080 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001081 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001082 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001083 return "UNKNOWN";
1084 }
1085 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001086 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001087
Jeff Sharkeyde570312017-10-24 21:25:50 -06001088 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001089 Preconditions.checkArgument(
1090 isValidTransport(transport), "Invalid TransportType " + transport);
1091 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001092}