blob: be85583a823f0e442ace0eb3dc83b565bfdf5e8e [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 Sharkey49bcd602017-11-09 13:11:50 -070034 * Representation of the capabilities of an active network. Instances are
35 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060036 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
37 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060038 * <p>
39 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
40 * network selection. Rather than indicate a need for Wi-Fi because an
41 * application needs high bandwidth and risk obsolescence when a new, fast
42 * network appears (like LTE), the application should specify it needs high
43 * bandwidth. Similarly if an application needs an unmetered network for a bulk
44 * transfer it can specify that rather than assuming all cellular based
45 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070046 */
47public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070048 private static final String TAG = "NetworkCapabilities";
49
Robert Greenwalt7569f182014-06-08 16:42:59 -070050 /**
51 * @hide
52 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070053 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090054 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090055 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070056 }
57
58 public NetworkCapabilities(NetworkCapabilities nc) {
59 if (nc != null) {
60 mNetworkCapabilities = nc.mNetworkCapabilities;
61 mTransportTypes = nc.mTransportTypes;
62 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
63 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070064 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090065 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070066 }
67 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070068
69 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090070 * Completely clears the contents of this object, removing even the capabilities that are set
71 * by default when the object is constructed.
72 * @hide
73 */
74 public void clearAll() {
75 mNetworkCapabilities = mTransportTypes = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070076 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090077 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090078 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090079 }
80
81 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070082 * Represents the network's capabilities. If any are specified they will be satisfied
83 * by any Network that matches all of them.
84 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090085 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070086
Jeff Sharkeyde570312017-10-24 21:25:50 -060087 /** @hide */
88 @Retention(RetentionPolicy.SOURCE)
89 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
90 NET_CAPABILITY_MMS,
91 NET_CAPABILITY_SUPL,
92 NET_CAPABILITY_DUN,
93 NET_CAPABILITY_FOTA,
94 NET_CAPABILITY_IMS,
95 NET_CAPABILITY_CBS,
96 NET_CAPABILITY_WIFI_P2P,
97 NET_CAPABILITY_IA,
98 NET_CAPABILITY_RCS,
99 NET_CAPABILITY_XCAP,
100 NET_CAPABILITY_EIMS,
101 NET_CAPABILITY_NOT_METERED,
102 NET_CAPABILITY_INTERNET,
103 NET_CAPABILITY_NOT_RESTRICTED,
104 NET_CAPABILITY_TRUSTED,
105 NET_CAPABILITY_NOT_VPN,
106 NET_CAPABILITY_VALIDATED,
107 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600108 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600109 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900110 NET_CAPABILITY_NOT_CONGESTED,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600111 })
112 public @interface NetCapability { }
113
Robert Greenwalt1448f052014-04-08 13:41:39 -0700114 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700115 * Indicates this is a network that has the ability to reach the
116 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700117 */
118 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700119
120 /**
121 * Indicates this is a network that has the ability to reach the carrier's
122 * SUPL server, used to retrieve GPS information.
123 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700124 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700125
126 /**
127 * Indicates this is a network that has the ability to reach the carrier's
128 * DUN or tethering gateway.
129 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700130 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700131
132 /**
133 * Indicates this is a network that has the ability to reach the carrier's
134 * FOTA portal, used for over the air updates.
135 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700136 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700137
138 /**
139 * Indicates this is a network that has the ability to reach the carrier's
140 * IMS servers, used for network registration and signaling.
141 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700142 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700143
144 /**
145 * Indicates this is a network that has the ability to reach the carrier's
146 * CBS servers, used for carrier specific services.
147 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700148 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700149
150 /**
151 * Indicates this is a network that has the ability to reach a Wi-Fi direct
152 * peer.
153 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700154 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700155
156 /**
157 * Indicates this is a network that has the ability to reach a carrier's
158 * Initial Attach servers.
159 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700160 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700161
162 /**
163 * Indicates this is a network that has the ability to reach a carrier's
164 * RCS servers, used for Rich Communication Services.
165 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700166 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700167
168 /**
169 * Indicates this is a network that has the ability to reach a carrier's
170 * XCAP servers, used for configuration and control.
171 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700172 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700173
174 /**
175 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700176 * Emergency IMS servers or other services, used for network signaling
177 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates that this network is unmetered.
183 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700184 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700185
186 /**
187 * Indicates that this network should be able to reach the internet.
188 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700189 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190
191 /**
192 * Indicates that this network is available for general use. If this is not set
193 * applications should not attempt to communicate on this network. Note that this
194 * is simply informative and not enforcement - enforcement is handled via other means.
195 * Set by default.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
198
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700199 /**
200 * Indicates that the user has indicated implicit trust of this network. This
201 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
202 * BT device or a wifi the user asked to connect to. Untrusted networks
203 * are probably limited to unknown wifi AP. Set by default.
204 */
205 public static final int NET_CAPABILITY_TRUSTED = 14;
206
Paul Jensen76b610a2015-03-18 09:33:07 -0400207 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400208 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400209 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400210 */
211 public static final int NET_CAPABILITY_NOT_VPN = 15;
212
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900213 /**
214 * Indicates that connectivity on this network was successfully validated. For example, for a
215 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
216 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900217 */
218 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700219
Paul Jensen3d194ea2015-06-16 14:27:36 -0400220 /**
221 * Indicates that this network was found to have a captive portal in place last time it was
222 * probed.
223 */
224 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
225
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900226 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600227 * Indicates that this network is not roaming.
228 */
229 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
230
231 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900232 * Indicates that this network is available for use by apps, and not a network that is being
233 * kept up in the background to facilitate fast network switching.
234 * @hide
235 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600236 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900237
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900238 /**
239 * Indicates that this network is not congested.
240 * <p>
241 * When a network is congested, the device should defer network traffic that
242 * can be done at a later time without breaking developer contracts.
243 * @hide
244 */
245 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
246
Robert Greenwalt1448f052014-04-08 13:41:39 -0700247 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900248 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_CONGESTED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700249
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700250 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900251 * Network capabilities that are expected to be mutable, i.e., can change while a particular
252 * network is connected.
253 */
254 private static final long MUTABLE_CAPABILITIES =
255 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
256 // http://b/18206275
257 (1 << NET_CAPABILITY_TRUSTED) |
258 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900259 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600260 (1 << NET_CAPABILITY_NOT_ROAMING) |
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900261 (1 << NET_CAPABILITY_FOREGROUND) |
262 (1 << NET_CAPABILITY_NOT_CONGESTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900263
264 /**
265 * Network capabilities that are not allowed in NetworkRequests. This exists because the
266 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
267 * capability's presence cannot be known in advance. If such a capability is requested, then we
268 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
269 * get immediately torn down because they do not have the requested capability.
270 */
271 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900272 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900273
274 /**
275 * Capabilities that are set by default when the object is constructed.
276 */
277 private static final long DEFAULT_CAPABILITIES =
278 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
279 (1 << NET_CAPABILITY_TRUSTED) |
280 (1 << NET_CAPABILITY_NOT_VPN);
281
282 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400283 * Capabilities that suggest that a network is restricted.
284 * {@see #maybeMarkCapabilitiesRestricted}.
285 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700286 @VisibleForTesting
287 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400288 (1 << NET_CAPABILITY_CBS) |
289 (1 << NET_CAPABILITY_DUN) |
290 (1 << NET_CAPABILITY_EIMS) |
291 (1 << NET_CAPABILITY_FOTA) |
292 (1 << NET_CAPABILITY_IA) |
293 (1 << NET_CAPABILITY_IMS) |
294 (1 << NET_CAPABILITY_RCS) |
295 (1 << NET_CAPABILITY_XCAP);
296
297 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700298 * Capabilities that suggest that a network is unrestricted.
299 * {@see #maybeMarkCapabilitiesRestricted}.
300 */
301 @VisibleForTesting
302 /* package */ static final long UNRESTRICTED_CAPABILITIES =
303 (1 << NET_CAPABILITY_INTERNET) |
304 (1 << NET_CAPABILITY_MMS) |
305 (1 << NET_CAPABILITY_SUPL) |
306 (1 << NET_CAPABILITY_WIFI_P2P);
307
308 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700309 * Adds the given capability to this {@code NetworkCapability} instance.
310 * Multiple capabilities may be applied sequentially. Note that when searching
311 * for a network to satisfy a request, all capabilities requested must be satisfied.
312 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600313 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900314 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700315 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700316 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600317 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700318 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700319 throw new IllegalArgumentException("NetworkCapability out of range");
320 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700321 mNetworkCapabilities |= 1 << capability;
322 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700323 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700324
325 /**
326 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
327 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600328 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900329 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700330 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700331 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600332 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700333 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700334 throw new IllegalArgumentException("NetworkCapability out of range");
335 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700336 mNetworkCapabilities &= ~(1 << capability);
337 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700338 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700339
340 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600341 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
342 * instance.
343 *
344 * @hide
345 */
346 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
347 if (value) {
348 addCapability(capability);
349 } else {
350 removeCapability(capability);
351 }
352 return this;
353 }
354
355 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700356 * Gets all the capabilities set on this {@code NetworkCapability} instance.
357 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600358 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700359 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700360 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600361 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900362 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700363 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700364
365 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600366 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700367 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600368 *
369 * @hide
370 */
371 public void setCapabilities(@NetCapability int[] capabilities) {
372 mNetworkCapabilities = BitUtils.packBits(capabilities);
373 }
374
375 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700376 * Tests for the presence of a capabilitity on this instance.
377 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600378 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700379 * @return {@code true} if set on this instance.
380 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600381 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700382 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700383 return false;
384 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700385 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700386 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700387
Robert Greenwalt1448f052014-04-08 13:41:39 -0700388 private void combineNetCapabilities(NetworkCapabilities nc) {
389 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
390 }
391
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900392 /**
393 * Convenience function that returns a human-readable description of the first mutable
394 * capability we find. Used to present an error message to apps that request mutable
395 * capabilities.
396 *
397 * @hide
398 */
399 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900400 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
401 if (nonRequestable != 0) {
402 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900403 }
404 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900405 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900406 return null;
407 }
408
409 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
410 long networkCapabilities = this.mNetworkCapabilities;
411 if (onlyImmutable) {
412 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
413 }
414 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700415 }
416
Robert Greenwalt06314e42014-10-29 14:04:06 -0700417 /** @hide */
418 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700419 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
420 }
421
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900422 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
423 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
424 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
425 }
426
Robert Greenwalt1448f052014-04-08 13:41:39 -0700427 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400428 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
429 * typically provided by restricted networks.
430 *
431 * TODO: consider:
432 * - Renaming it to guessRestrictedCapability and make it set the
433 * restricted capability bit in addition to clearing it.
434 * @hide
435 */
436 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700437 // Verify there aren't any unrestricted capabilities. If there are we say
438 // the whole thing is unrestricted.
439 final boolean hasUnrestrictedCapabilities =
440 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
441
442 // Must have at least some restricted capabilities.
443 final boolean hasRestrictedCapabilities =
444 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
445
446 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400447 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400448 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400449 }
450
451 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700452 * Representing the transport type. Apps should generally not care about transport. A
453 * request for a fast internet connection could be satisfied by a number of different
454 * transports. If any are specified here it will be satisfied a Network that matches
455 * any of them. If a caller doesn't care about the transport it should not specify any.
456 */
457 private long mTransportTypes;
458
Jeff Sharkeyde570312017-10-24 21:25:50 -0600459 /** @hide */
460 @Retention(RetentionPolicy.SOURCE)
461 @IntDef(prefix = { "TRANSPORT_" }, value = {
462 TRANSPORT_CELLULAR,
463 TRANSPORT_WIFI,
464 TRANSPORT_BLUETOOTH,
465 TRANSPORT_ETHERNET,
466 TRANSPORT_VPN,
467 TRANSPORT_WIFI_AWARE,
468 TRANSPORT_LOWPAN,
469 })
470 public @interface Transport { }
471
Robert Greenwalt1448f052014-04-08 13:41:39 -0700472 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700474 */
475 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700476
477 /**
478 * Indicates this network uses a Wi-Fi transport.
479 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700481
482 /**
483 * Indicates this network uses a Bluetooth transport.
484 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700485 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700486
487 /**
488 * Indicates this network uses an Ethernet transport.
489 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490 public static final int TRANSPORT_ETHERNET = 3;
491
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400492 /**
493 * Indicates this network uses a VPN transport.
494 */
495 public static final int TRANSPORT_VPN = 4;
496
Etan Cohen305ea282016-06-20 09:27:12 -0700497 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700498 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700499 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700500 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700501
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700502 /**
503 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700504 */
505 public static final int TRANSPORT_LOWPAN = 6;
506
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900507 /** @hide */
508 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
509 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700510 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511
Hugo Benichi16f0a942017-06-20 14:07:59 +0900512 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600513 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900514 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
515 }
516
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900517 private static final String[] TRANSPORT_NAMES = {
518 "CELLULAR",
519 "WIFI",
520 "BLUETOOTH",
521 "ETHERNET",
522 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700523 "WIFI_AWARE",
524 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900525 };
526
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700527 /**
528 * Adds the given transport type to this {@code NetworkCapability} instance.
529 * Multiple transports may be applied sequentially. Note that when searching
530 * for a network to satisfy a request, any listed in the request will satisfy the request.
531 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
532 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
533 * to be selected. This is logically different than
534 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
535 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600536 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900537 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700538 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700539 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600540 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900541 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700542 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700543 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700544 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700545 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700546
547 /**
548 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
549 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600550 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900551 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700552 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700553 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600554 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900555 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700556 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700557 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700558 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700559 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700560
561 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600562 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
563 * instance.
564 *
565 * @hide
566 */
567 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
568 if (value) {
569 addTransportType(transportType);
570 } else {
571 removeTransportType(transportType);
572 }
573 return this;
574 }
575
576 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700577 * Gets all the transports set on this {@code NetworkCapability} instance.
578 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600579 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700580 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700581 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600582 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900583 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700585
586 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600587 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700588 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600589 *
590 * @hide
591 */
592 public void setTransportTypes(@Transport int[] transportTypes) {
593 mTransportTypes = BitUtils.packBits(transportTypes);
594 }
595
596 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700597 * Tests for the presence of a transport on this instance.
598 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600599 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700600 * @return {@code true} if set on this instance.
601 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600602 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900603 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700604 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700605
606 private void combineTransportTypes(NetworkCapabilities nc) {
607 this.mTransportTypes |= nc.mTransportTypes;
608 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900609
Robert Greenwalt1448f052014-04-08 13:41:39 -0700610 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
611 return ((this.mTransportTypes == 0) ||
612 ((this.mTransportTypes & nc.mTransportTypes) != 0));
613 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900614
Robert Greenwalt06314e42014-10-29 14:04:06 -0700615 /** @hide */
616 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700617 return (nc.mTransportTypes == this.mTransportTypes);
618 }
619
620 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600621 * Value indicating that link bandwidth is unspecified.
622 * @hide
623 */
624 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
625
626 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700627 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
628 * for the first hop on the given transport. It is not measured, but may take into account
629 * link parameters (Radio technology, allocated channels, etc).
630 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600631 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
632 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700633
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700634 /**
635 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
636 * the estimated first hop transport bandwidth.
637 * <p>
638 * Note that when used to request a network, this specifies the minimum acceptable.
639 * When received as the state of an existing network this specifies the typical
640 * first hop bandwidth expected. This is never measured, but rather is inferred
641 * from technology type and other link parameters. It could be used to differentiate
642 * between very slow 1xRTT cellular links and other faster networks or even between
643 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
644 * fast backhauls and slow backhauls.
645 *
646 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700647 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700648 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600649 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700650 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600651 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700652 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700653
654 /**
655 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
656 * the estimated first hop transport bandwidth.
657 *
658 * @return The estimated first hop upstream (device to network) bandwidth.
659 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700660 public int getLinkUpstreamBandwidthKbps() {
661 return mLinkUpBandwidthKbps;
662 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700663
664 /**
665 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
666 * the estimated first hop transport bandwidth.
667 * <p>
668 * Note that when used to request a network, this specifies the minimum acceptable.
669 * When received as the state of an existing network this specifies the typical
670 * first hop bandwidth expected. This is never measured, but rather is inferred
671 * from technology type and other link parameters. It could be used to differentiate
672 * between very slow 1xRTT cellular links and other faster networks or even between
673 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
674 * fast backhauls and slow backhauls.
675 *
676 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700677 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700678 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600679 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700680 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600681 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700682 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700683
684 /**
685 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
686 * the estimated first hop transport bandwidth.
687 *
688 * @return The estimated first hop downstream (network to device) bandwidth.
689 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700690 public int getLinkDownstreamBandwidthKbps() {
691 return mLinkDownBandwidthKbps;
692 }
693
694 private void combineLinkBandwidths(NetworkCapabilities nc) {
695 this.mLinkUpBandwidthKbps =
696 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
697 this.mLinkDownBandwidthKbps =
698 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
699 }
700 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
701 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
702 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
703 }
704 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
705 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
706 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
707 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600708 /** @hide */
709 public static int minBandwidth(int a, int b) {
710 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
711 return b;
712 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
713 return a;
714 } else {
715 return Math.min(a, b);
716 }
717 }
718 /** @hide */
719 public static int maxBandwidth(int a, int b) {
720 return Math.max(a, b);
721 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700722
Etan Cohena7434272017-04-03 12:17:51 -0700723 private NetworkSpecifier mNetworkSpecifier = null;
724
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700725 /**
726 * Sets the optional bearer specific network specifier.
727 * This has no meaning if a single transport is also not specified, so calling
728 * this without a single transport set will generate an exception, as will
729 * subsequently adding or removing transports after this is set.
730 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700731 *
Etan Cohena7434272017-04-03 12:17:51 -0700732 * @param networkSpecifier A concrete, parcelable framework class that extends
733 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900734 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700735 * @hide
736 */
Etan Cohena7434272017-04-03 12:17:51 -0700737 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
738 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700739 throw new IllegalStateException("Must have a single transport specified to use " +
740 "setNetworkSpecifier");
741 }
Etan Cohena7434272017-04-03 12:17:51 -0700742
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700743 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700744
Pierre Imaic8419a82016-03-22 17:54:54 +0900745 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700746 }
747
748 /**
749 * Gets the optional bearer specific network specifier.
750 *
Etan Cohena7434272017-04-03 12:17:51 -0700751 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
752 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700753 * @hide
754 */
Etan Cohena7434272017-04-03 12:17:51 -0700755 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700756 return mNetworkSpecifier;
757 }
758
759 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700760 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700761 throw new IllegalStateException("Can't combine two networkSpecifiers");
762 }
Etan Cohena7434272017-04-03 12:17:51 -0700763 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700764 }
Etan Cohena7434272017-04-03 12:17:51 -0700765
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700766 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700767 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
768 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700769 }
Etan Cohena7434272017-04-03 12:17:51 -0700770
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700771 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700772 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700773 }
774
Robert Greenwalt1448f052014-04-08 13:41:39 -0700775 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900776 * Magic value that indicates no signal strength provided. A request specifying this value is
777 * always satisfied.
778 *
779 * @hide
780 */
781 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
782
783 /**
784 * Signal strength. This is a signed integer, and higher values indicate better signal.
785 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
786 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700787 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900788
789 /**
790 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
791 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
792 * reported by WifiManager.
793 * <p>
794 * Note that when used to register a network callback, this specifies the minimum acceptable
795 * signal strength. When received as the state of an existing network it specifies the current
796 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
797 * effect when requesting a callback.
798 *
799 * @param signalStrength the bearer-specific signal strength.
800 * @hide
801 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600802 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900803 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600804 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900805 }
806
807 /**
808 * Returns {@code true} if this object specifies a signal strength.
809 *
810 * @hide
811 */
812 public boolean hasSignalStrength() {
813 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
814 }
815
816 /**
817 * Retrieves the signal strength.
818 *
819 * @return The bearer-specific signal strength.
820 * @hide
821 */
822 public int getSignalStrength() {
823 return mSignalStrength;
824 }
825
826 private void combineSignalStrength(NetworkCapabilities nc) {
827 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
828 }
829
830 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
831 return this.mSignalStrength <= nc.mSignalStrength;
832 }
833
834 private boolean equalsSignalStrength(NetworkCapabilities nc) {
835 return this.mSignalStrength == nc.mSignalStrength;
836 }
837
838 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700839 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900840 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700841 */
842 public void combineCapabilities(NetworkCapabilities nc) {
843 combineNetCapabilities(nc);
844 combineTransportTypes(nc);
845 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700846 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900847 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700848 }
849
850 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900851 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
852 *
853 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
854 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
855 * bandwidth, signal strength, or validation / captive portal status.
856 *
857 * @hide
858 */
859 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
860 return (nc != null &&
861 satisfiedByNetCapabilities(nc, onlyImmutable) &&
862 satisfiedByTransportTypes(nc) &&
863 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900864 satisfiedBySpecifier(nc) &&
865 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900866 }
867
868 /**
869 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
870 *
871 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
872 *
873 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700874 */
875 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900876 return satisfiedByNetworkCapabilities(nc, false);
877 }
878
879 /**
880 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
881 *
882 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
883 *
884 * @hide
885 */
886 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
887 return satisfiedByNetworkCapabilities(nc, true);
888 }
889
890 /**
891 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +0900892 * {@code NetworkCapabilities} and return a String describing any difference.
893 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900894 *
895 * @hide
896 */
Hugo Benichieae7a222017-07-25 11:40:56 +0900897 public String describeImmutableDifferences(NetworkCapabilities that) {
898 if (that == null) {
899 return "other NetworkCapabilities was null";
900 }
901
902 StringJoiner joiner = new StringJoiner(", ");
903
Hugo Benichieae7a222017-07-25 11:40:56 +0900904 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
905 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +0900906 // Ignore DUN being added or removed. http://b/65257223.
907 final long mask = ~MUTABLE_CAPABILITIES
908 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +0900909 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
910 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
911 if (oldImmutableCapabilities != newImmutableCapabilities) {
912 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
913 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
914 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
915 }
916
917 if (!equalsSpecifier(that)) {
918 NetworkSpecifier before = this.getNetworkSpecifier();
919 NetworkSpecifier after = that.getNetworkSpecifier();
920 joiner.add(String.format("specifier changed: %s -> %s", before, after));
921 }
922
923 if (!equalsTransportTypes(that)) {
924 String before = transportNamesOf(this.getTransportTypes());
925 String after = transportNamesOf(that.getTransportTypes());
926 joiner.add(String.format("transports changed: %s -> %s", before, after));
927 }
928
929 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700930 }
931
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900932 /**
933 * Checks that our requestable capabilities are the same as those of the given
934 * {@code NetworkCapabilities}.
935 *
936 * @hide
937 */
938 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
939 if (nc == null) return false;
940 return (equalsNetCapabilitiesRequestable(nc) &&
941 equalsTransportTypes(nc) &&
942 equalsSpecifier(nc));
943 }
944
Robert Greenwalt1448f052014-04-08 13:41:39 -0700945 @Override
946 public boolean equals(Object obj) {
947 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
948 NetworkCapabilities that = (NetworkCapabilities)obj;
949 return (equalsNetCapabilities(that) &&
950 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700951 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900952 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700953 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700954 }
955
956 @Override
957 public int hashCode() {
958 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
959 ((int)(mNetworkCapabilities >> 32) * 3) +
960 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
961 ((int)(mTransportTypes >> 32) * 7) +
962 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700963 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700964 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900965 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700966 }
967
Wink Saville4e2dea72014-09-20 11:04:03 -0700968 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700969 public int describeContents() {
970 return 0;
971 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700972 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700973 public void writeToParcel(Parcel dest, int flags) {
974 dest.writeLong(mNetworkCapabilities);
975 dest.writeLong(mTransportTypes);
976 dest.writeInt(mLinkUpBandwidthKbps);
977 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700978 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900979 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700980 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900981
Robert Greenwalt1448f052014-04-08 13:41:39 -0700982 public static final Creator<NetworkCapabilities> CREATOR =
983 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700984 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700985 public NetworkCapabilities createFromParcel(Parcel in) {
986 NetworkCapabilities netCap = new NetworkCapabilities();
987
988 netCap.mNetworkCapabilities = in.readLong();
989 netCap.mTransportTypes = in.readLong();
990 netCap.mLinkUpBandwidthKbps = in.readInt();
991 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700992 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900993 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700994 return netCap;
995 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700996 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700997 public NetworkCapabilities[] newArray(int size) {
998 return new NetworkCapabilities[size];
999 }
1000 };
1001
Wink Saville4e2dea72014-09-20 11:04:03 -07001002 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001003 public String toString() {
Hugo Benichieae7a222017-07-25 11:40:56 +09001004 // TODO: enumerate bits for transports and capabilities instead of creating arrays.
1005 // TODO: use a StringBuilder instead of string concatenation.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001006 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +09001007 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001008
Robert Greenwalt7569f182014-06-08 16:42:59 -07001009 types = getCapabilities();
1010 String capabilities = (types.length > 0 ? " Capabilities: " : "");
1011 for (int i = 0; i < types.length; ) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001012 capabilities += capabilityNameOf(types[i]);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001013 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001014 }
1015
1016 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
1017 mLinkUpBandwidthKbps + "Kbps" : "");
1018 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
1019 mLinkDownBandwidthKbps + "Kbps" : "");
1020
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001021 String specifier = (mNetworkSpecifier == null ?
1022 "" : " Specifier: <" + mNetworkSpecifier + ">");
1023
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001024 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
1025
1026 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001027 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001028
1029 /**
1030 * @hide
1031 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001032 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001033 StringJoiner joiner = new StringJoiner("|");
1034 if (capabilities != null) {
1035 for (int c : capabilities) {
1036 joiner.add(capabilityNameOf(c));
1037 }
1038 }
1039 return joiner.toString();
1040 }
1041
1042 /**
1043 * @hide
1044 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001045 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001046 switch (capability) {
1047 case NET_CAPABILITY_MMS: return "MMS";
1048 case NET_CAPABILITY_SUPL: return "SUPL";
1049 case NET_CAPABILITY_DUN: return "DUN";
1050 case NET_CAPABILITY_FOTA: return "FOTA";
1051 case NET_CAPABILITY_IMS: return "IMS";
1052 case NET_CAPABILITY_CBS: return "CBS";
1053 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1054 case NET_CAPABILITY_IA: return "IA";
1055 case NET_CAPABILITY_RCS: return "RCS";
1056 case NET_CAPABILITY_XCAP: return "XCAP";
1057 case NET_CAPABILITY_EIMS: return "EIMS";
1058 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1059 case NET_CAPABILITY_INTERNET: return "INTERNET";
1060 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1061 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1062 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1063 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1064 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001065 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001066 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001067 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Hugo Benichieae7a222017-07-25 11:40:56 +09001068 default: return Integer.toString(capability);
1069 }
1070 }
1071
1072 /**
1073 * @hide
1074 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001075 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001076 StringJoiner joiner = new StringJoiner("|");
1077 if (types != null) {
1078 for (int t : types) {
1079 joiner.add(transportNameOf(t));
1080 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001081 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001082 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001083 }
1084
1085 /**
1086 * @hide
1087 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001088 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001089 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001090 return "UNKNOWN";
1091 }
1092 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001093 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001094
Jeff Sharkeyde570312017-10-24 21:25:50 -06001095 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001096 Preconditions.checkArgument(
1097 isValidTransport(transport), "Invalid TransportType " + transport);
1098 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001099}