blob: 6bcaffd97a4923bb7e284776f6885066140a00d7 [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;
Chalard Jeance1a9d82018-01-23 21:25:37 +090023import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070024
25import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090026import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090027import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070028
Jeff Sharkeyde570312017-10-24 21:25:50 -060029import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070031import java.util.Objects;
Hugo Benichieae7a222017-07-25 11:40:56 +090032import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070033
34/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070035 * Representation of the capabilities of an active network. Instances are
36 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060037 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
38 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060039 * <p>
40 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
41 * network selection. Rather than indicate a need for Wi-Fi because an
42 * application needs high bandwidth and risk obsolescence when a new, fast
43 * network appears (like LTE), the application should specify it needs high
44 * bandwidth. Similarly if an application needs an unmetered network for a bulk
45 * transfer it can specify that rather than assuming all cellular based
46 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070047 */
48public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070049 private static final String TAG = "NetworkCapabilities";
50
Robert Greenwalt7569f182014-06-08 16:42:59 -070051 /**
52 * @hide
53 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070054 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090055 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090056 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070057 }
58
59 public NetworkCapabilities(NetworkCapabilities nc) {
60 if (nc != null) {
61 mNetworkCapabilities = nc.mNetworkCapabilities;
62 mTransportTypes = nc.mTransportTypes;
63 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
64 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070065 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090066 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070067 }
68 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070069
70 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090071 * Completely clears the contents of this object, removing even the capabilities that are set
72 * by default when the object is constructed.
73 * @hide
74 */
75 public void clearAll() {
76 mNetworkCapabilities = mTransportTypes = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070077 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090078 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090079 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 }
81
82 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070083 * Represents the network's capabilities. If any are specified they will be satisfied
84 * by any Network that matches all of them.
85 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090086 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070087
Jeff Sharkeyde570312017-10-24 21:25:50 -060088 /** @hide */
89 @Retention(RetentionPolicy.SOURCE)
90 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
91 NET_CAPABILITY_MMS,
92 NET_CAPABILITY_SUPL,
93 NET_CAPABILITY_DUN,
94 NET_CAPABILITY_FOTA,
95 NET_CAPABILITY_IMS,
96 NET_CAPABILITY_CBS,
97 NET_CAPABILITY_WIFI_P2P,
98 NET_CAPABILITY_IA,
99 NET_CAPABILITY_RCS,
100 NET_CAPABILITY_XCAP,
101 NET_CAPABILITY_EIMS,
102 NET_CAPABILITY_NOT_METERED,
103 NET_CAPABILITY_INTERNET,
104 NET_CAPABILITY_NOT_RESTRICTED,
105 NET_CAPABILITY_TRUSTED,
106 NET_CAPABILITY_NOT_VPN,
107 NET_CAPABILITY_VALIDATED,
108 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600109 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600110 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900111 NET_CAPABILITY_NOT_CONGESTED,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600112 })
113 public @interface NetCapability { }
114
Robert Greenwalt1448f052014-04-08 13:41:39 -0700115 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700116 * Indicates this is a network that has the ability to reach the
117 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700118 */
119 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700120
121 /**
122 * Indicates this is a network that has the ability to reach the carrier's
123 * SUPL server, used to retrieve GPS information.
124 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700125 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700126
127 /**
128 * Indicates this is a network that has the ability to reach the carrier's
129 * DUN or tethering gateway.
130 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700131 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700132
133 /**
134 * Indicates this is a network that has the ability to reach the carrier's
135 * FOTA portal, used for over the air updates.
136 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700137 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700138
139 /**
140 * Indicates this is a network that has the ability to reach the carrier's
141 * IMS servers, used for network registration and signaling.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates this is a network that has the ability to reach the carrier's
147 * CBS servers, used for carrier specific services.
148 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150
151 /**
152 * Indicates this is a network that has the ability to reach a Wi-Fi direct
153 * peer.
154 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700155 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700156
157 /**
158 * Indicates this is a network that has the ability to reach a carrier's
159 * Initial Attach servers.
160 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700161 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700162
163 /**
164 * Indicates this is a network that has the ability to reach a carrier's
165 * RCS servers, used for Rich Communication Services.
166 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700167 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700168
169 /**
170 * Indicates this is a network that has the ability to reach a carrier's
171 * XCAP servers, used for configuration and control.
172 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174
175 /**
176 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700177 * Emergency IMS servers or other services, used for network signaling
178 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700179 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates that this network is unmetered.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates that this network should be able to reach the internet.
189 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700190 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700191
192 /**
193 * Indicates that this network is available for general use. If this is not set
194 * applications should not attempt to communicate on this network. Note that this
195 * is simply informative and not enforcement - enforcement is handled via other means.
196 * Set by default.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
199
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700200 /**
201 * Indicates that the user has indicated implicit trust of this network. This
202 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
203 * BT device or a wifi the user asked to connect to. Untrusted networks
204 * are probably limited to unknown wifi AP. Set by default.
205 */
206 public static final int NET_CAPABILITY_TRUSTED = 14;
207
Paul Jensen76b610a2015-03-18 09:33:07 -0400208 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400209 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400210 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400211 */
212 public static final int NET_CAPABILITY_NOT_VPN = 15;
213
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900214 /**
215 * Indicates that connectivity on this network was successfully validated. For example, for a
216 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
217 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900218 */
219 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700220
Paul Jensen3d194ea2015-06-16 14:27:36 -0400221 /**
222 * Indicates that this network was found to have a captive portal in place last time it was
223 * probed.
224 */
225 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
226
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900227 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600228 * Indicates that this network is not roaming.
229 */
230 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
231
232 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900233 * Indicates that this network is available for use by apps, and not a network that is being
234 * kept up in the background to facilitate fast network switching.
235 * @hide
236 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600237 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900238
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900239 /**
240 * Indicates that this network is not congested.
241 * <p>
242 * When a network is congested, the device should defer network traffic that
243 * can be done at a later time without breaking developer contracts.
244 * @hide
245 */
246 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
247
Robert Greenwalt1448f052014-04-08 13:41:39 -0700248 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900249 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_CONGESTED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700250
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700251 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900252 * Network capabilities that are expected to be mutable, i.e., can change while a particular
253 * network is connected.
254 */
255 private static final long MUTABLE_CAPABILITIES =
256 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
257 // http://b/18206275
258 (1 << NET_CAPABILITY_TRUSTED) |
259 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900260 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600261 (1 << NET_CAPABILITY_NOT_ROAMING) |
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900262 (1 << NET_CAPABILITY_FOREGROUND) |
263 (1 << NET_CAPABILITY_NOT_CONGESTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900264
265 /**
266 * Network capabilities that are not allowed in NetworkRequests. This exists because the
267 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
268 * capability's presence cannot be known in advance. If such a capability is requested, then we
269 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
270 * get immediately torn down because they do not have the requested capability.
271 */
272 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900273 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900274
275 /**
276 * Capabilities that are set by default when the object is constructed.
277 */
278 private static final long DEFAULT_CAPABILITIES =
279 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
280 (1 << NET_CAPABILITY_TRUSTED) |
281 (1 << NET_CAPABILITY_NOT_VPN);
282
283 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400284 * Capabilities that suggest that a network is restricted.
285 * {@see #maybeMarkCapabilitiesRestricted}.
286 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700287 @VisibleForTesting
288 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400289 (1 << NET_CAPABILITY_CBS) |
290 (1 << NET_CAPABILITY_DUN) |
291 (1 << NET_CAPABILITY_EIMS) |
292 (1 << NET_CAPABILITY_FOTA) |
293 (1 << NET_CAPABILITY_IA) |
294 (1 << NET_CAPABILITY_IMS) |
295 (1 << NET_CAPABILITY_RCS) |
296 (1 << NET_CAPABILITY_XCAP);
297
298 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700299 * Capabilities that suggest that a network is unrestricted.
300 * {@see #maybeMarkCapabilitiesRestricted}.
301 */
302 @VisibleForTesting
303 /* package */ static final long UNRESTRICTED_CAPABILITIES =
304 (1 << NET_CAPABILITY_INTERNET) |
305 (1 << NET_CAPABILITY_MMS) |
306 (1 << NET_CAPABILITY_SUPL) |
307 (1 << NET_CAPABILITY_WIFI_P2P);
308
309 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700310 * Adds the given capability to this {@code NetworkCapability} instance.
311 * Multiple capabilities may be applied sequentially. Note that when searching
312 * for a network to satisfy a request, all capabilities requested must be satisfied.
313 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600314 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900315 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700316 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700317 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600318 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700319 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700320 throw new IllegalArgumentException("NetworkCapability out of range");
321 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700322 mNetworkCapabilities |= 1 << capability;
323 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700324 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700325
326 /**
327 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
328 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600329 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900330 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700331 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700332 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600333 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700334 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700335 throw new IllegalArgumentException("NetworkCapability out of range");
336 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700337 mNetworkCapabilities &= ~(1 << capability);
338 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700340
341 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600342 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
343 * instance.
344 *
345 * @hide
346 */
347 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
348 if (value) {
349 addCapability(capability);
350 } else {
351 removeCapability(capability);
352 }
353 return this;
354 }
355
356 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700357 * Gets all the capabilities set on this {@code NetworkCapability} instance.
358 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600359 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700360 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700361 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600362 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900363 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700364 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700365
366 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600367 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700368 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600369 *
370 * @hide
371 */
372 public void setCapabilities(@NetCapability int[] capabilities) {
373 mNetworkCapabilities = BitUtils.packBits(capabilities);
374 }
375
376 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700377 * Tests for the presence of a capabilitity on this instance.
378 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600379 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700380 * @return {@code true} if set on this instance.
381 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600382 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700383 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700384 return false;
385 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700386 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700387 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700388
Robert Greenwalt1448f052014-04-08 13:41:39 -0700389 private void combineNetCapabilities(NetworkCapabilities nc) {
390 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
391 }
392
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900393 /**
394 * Convenience function that returns a human-readable description of the first mutable
395 * capability we find. Used to present an error message to apps that request mutable
396 * capabilities.
397 *
398 * @hide
399 */
400 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900401 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
402 if (nonRequestable != 0) {
403 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900404 }
405 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900406 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900407 return null;
408 }
409
410 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
411 long networkCapabilities = this.mNetworkCapabilities;
412 if (onlyImmutable) {
413 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
414 }
415 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700416 }
417
Robert Greenwalt06314e42014-10-29 14:04:06 -0700418 /** @hide */
419 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700420 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
421 }
422
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900423 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
424 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
425 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
426 }
427
Robert Greenwalt1448f052014-04-08 13:41:39 -0700428 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400429 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
430 * typically provided by restricted networks.
431 *
432 * TODO: consider:
433 * - Renaming it to guessRestrictedCapability and make it set the
434 * restricted capability bit in addition to clearing it.
435 * @hide
436 */
437 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700438 // Verify there aren't any unrestricted capabilities. If there are we say
439 // the whole thing is unrestricted.
440 final boolean hasUnrestrictedCapabilities =
441 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
442
443 // Must have at least some restricted capabilities.
444 final boolean hasRestrictedCapabilities =
445 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
446
447 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400448 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400449 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400450 }
451
452 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700453 * Representing the transport type. Apps should generally not care about transport. A
454 * request for a fast internet connection could be satisfied by a number of different
455 * transports. If any are specified here it will be satisfied a Network that matches
456 * any of them. If a caller doesn't care about the transport it should not specify any.
457 */
458 private long mTransportTypes;
459
Jeff Sharkeyde570312017-10-24 21:25:50 -0600460 /** @hide */
461 @Retention(RetentionPolicy.SOURCE)
462 @IntDef(prefix = { "TRANSPORT_" }, value = {
463 TRANSPORT_CELLULAR,
464 TRANSPORT_WIFI,
465 TRANSPORT_BLUETOOTH,
466 TRANSPORT_ETHERNET,
467 TRANSPORT_VPN,
468 TRANSPORT_WIFI_AWARE,
469 TRANSPORT_LOWPAN,
470 })
471 public @interface Transport { }
472
Robert Greenwalt1448f052014-04-08 13:41:39 -0700473 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700474 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700475 */
476 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700477
478 /**
479 * Indicates this network uses a Wi-Fi transport.
480 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700481 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700482
483 /**
484 * Indicates this network uses a Bluetooth transport.
485 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700486 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700487
488 /**
489 * Indicates this network uses an Ethernet transport.
490 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700491 public static final int TRANSPORT_ETHERNET = 3;
492
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400493 /**
494 * Indicates this network uses a VPN transport.
495 */
496 public static final int TRANSPORT_VPN = 4;
497
Etan Cohen305ea282016-06-20 09:27:12 -0700498 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700499 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700500 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700501 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700502
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700503 /**
504 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700505 */
506 public static final int TRANSPORT_LOWPAN = 6;
507
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900508 /** @hide */
509 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
510 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700511 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700512
Hugo Benichi16f0a942017-06-20 14:07:59 +0900513 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600514 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900515 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
516 }
517
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900518 private static final String[] TRANSPORT_NAMES = {
519 "CELLULAR",
520 "WIFI",
521 "BLUETOOTH",
522 "ETHERNET",
523 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700524 "WIFI_AWARE",
525 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900526 };
527
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700528 /**
529 * Adds the given transport type to this {@code NetworkCapability} instance.
530 * Multiple transports may be applied sequentially. Note that when searching
531 * for a network to satisfy a request, any listed in the request will satisfy the request.
532 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
533 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
534 * to be selected. This is logically different than
535 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
536 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600537 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900538 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700539 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700540 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600541 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900542 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700543 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700544 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700545 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700546 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700547
548 /**
549 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
550 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600551 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900552 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700553 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700554 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600555 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900556 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700557 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700558 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700559 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700560 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700561
562 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600563 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
564 * instance.
565 *
566 * @hide
567 */
568 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
569 if (value) {
570 addTransportType(transportType);
571 } else {
572 removeTransportType(transportType);
573 }
574 return this;
575 }
576
577 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700578 * Gets all the transports set on this {@code NetworkCapability} instance.
579 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600580 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700581 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700582 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600583 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900584 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700585 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700586
587 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600588 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700589 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600590 *
591 * @hide
592 */
593 public void setTransportTypes(@Transport int[] transportTypes) {
594 mTransportTypes = BitUtils.packBits(transportTypes);
595 }
596
597 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700598 * Tests for the presence of a transport on this instance.
599 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600600 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700601 * @return {@code true} if set on this instance.
602 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600603 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900604 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700605 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606
607 private void combineTransportTypes(NetworkCapabilities nc) {
608 this.mTransportTypes |= nc.mTransportTypes;
609 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900610
Robert Greenwalt1448f052014-04-08 13:41:39 -0700611 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
612 return ((this.mTransportTypes == 0) ||
613 ((this.mTransportTypes & nc.mTransportTypes) != 0));
614 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900615
Robert Greenwalt06314e42014-10-29 14:04:06 -0700616 /** @hide */
617 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700618 return (nc.mTransportTypes == this.mTransportTypes);
619 }
620
621 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600622 * Value indicating that link bandwidth is unspecified.
623 * @hide
624 */
625 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
626
627 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700628 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
629 * for the first hop on the given transport. It is not measured, but may take into account
630 * link parameters (Radio technology, allocated channels, etc).
631 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600632 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
633 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700634
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700635 /**
636 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
637 * the estimated first hop transport bandwidth.
638 * <p>
639 * Note that when used to request a network, this specifies the minimum acceptable.
640 * When received as the state of an existing network this specifies the typical
641 * first hop bandwidth expected. This is never measured, but rather is inferred
642 * from technology type and other link parameters. It could be used to differentiate
643 * between very slow 1xRTT cellular links and other faster networks or even between
644 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
645 * fast backhauls and slow backhauls.
646 *
647 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700648 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700649 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600650 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700651 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600652 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700653 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700654
655 /**
656 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
657 * the estimated first hop transport bandwidth.
658 *
659 * @return The estimated first hop upstream (device to network) bandwidth.
660 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700661 public int getLinkUpstreamBandwidthKbps() {
662 return mLinkUpBandwidthKbps;
663 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700664
665 /**
666 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
667 * the estimated first hop transport bandwidth.
668 * <p>
669 * Note that when used to request a network, this specifies the minimum acceptable.
670 * When received as the state of an existing network this specifies the typical
671 * first hop bandwidth expected. This is never measured, but rather is inferred
672 * from technology type and other link parameters. It could be used to differentiate
673 * between very slow 1xRTT cellular links and other faster networks or even between
674 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
675 * fast backhauls and slow backhauls.
676 *
677 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700678 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700679 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600680 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700681 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600682 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700683 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700684
685 /**
686 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
687 * the estimated first hop transport bandwidth.
688 *
689 * @return The estimated first hop downstream (network to device) bandwidth.
690 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700691 public int getLinkDownstreamBandwidthKbps() {
692 return mLinkDownBandwidthKbps;
693 }
694
695 private void combineLinkBandwidths(NetworkCapabilities nc) {
696 this.mLinkUpBandwidthKbps =
697 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
698 this.mLinkDownBandwidthKbps =
699 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
700 }
701 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
702 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
703 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
704 }
705 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
706 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
707 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
708 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600709 /** @hide */
710 public static int minBandwidth(int a, int b) {
711 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
712 return b;
713 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
714 return a;
715 } else {
716 return Math.min(a, b);
717 }
718 }
719 /** @hide */
720 public static int maxBandwidth(int a, int b) {
721 return Math.max(a, b);
722 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700723
Etan Cohena7434272017-04-03 12:17:51 -0700724 private NetworkSpecifier mNetworkSpecifier = null;
725
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700726 /**
727 * Sets the optional bearer specific network specifier.
728 * This has no meaning if a single transport is also not specified, so calling
729 * this without a single transport set will generate an exception, as will
730 * subsequently adding or removing transports after this is set.
731 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700732 *
Etan Cohena7434272017-04-03 12:17:51 -0700733 * @param networkSpecifier A concrete, parcelable framework class that extends
734 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900735 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700736 * @hide
737 */
Etan Cohena7434272017-04-03 12:17:51 -0700738 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
739 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700740 throw new IllegalStateException("Must have a single transport specified to use " +
741 "setNetworkSpecifier");
742 }
Etan Cohena7434272017-04-03 12:17:51 -0700743
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700744 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700745
Pierre Imaic8419a82016-03-22 17:54:54 +0900746 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700747 }
748
749 /**
750 * Gets the optional bearer specific network specifier.
751 *
Etan Cohena7434272017-04-03 12:17:51 -0700752 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
753 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700754 * @hide
755 */
Etan Cohena7434272017-04-03 12:17:51 -0700756 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700757 return mNetworkSpecifier;
758 }
759
760 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700761 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700762 throw new IllegalStateException("Can't combine two networkSpecifiers");
763 }
Etan Cohena7434272017-04-03 12:17:51 -0700764 setNetworkSpecifier(nc.mNetworkSpecifier);
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 satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700768 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
769 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700770 }
Etan Cohena7434272017-04-03 12:17:51 -0700771
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700772 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700773 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700774 }
775
Robert Greenwalt1448f052014-04-08 13:41:39 -0700776 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900777 * Magic value that indicates no signal strength provided. A request specifying this value is
778 * always satisfied.
779 *
780 * @hide
781 */
782 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
783
784 /**
785 * Signal strength. This is a signed integer, and higher values indicate better signal.
786 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
787 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700788 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900789
790 /**
791 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
792 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
793 * reported by WifiManager.
794 * <p>
795 * Note that when used to register a network callback, this specifies the minimum acceptable
796 * signal strength. When received as the state of an existing network it specifies the current
797 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
798 * effect when requesting a callback.
799 *
800 * @param signalStrength the bearer-specific signal strength.
801 * @hide
802 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600803 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900804 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600805 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900806 }
807
808 /**
809 * Returns {@code true} if this object specifies a signal strength.
810 *
811 * @hide
812 */
813 public boolean hasSignalStrength() {
814 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
815 }
816
817 /**
818 * Retrieves the signal strength.
819 *
820 * @return The bearer-specific signal strength.
821 * @hide
822 */
823 public int getSignalStrength() {
824 return mSignalStrength;
825 }
826
827 private void combineSignalStrength(NetworkCapabilities nc) {
828 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
829 }
830
831 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
832 return this.mSignalStrength <= nc.mSignalStrength;
833 }
834
835 private boolean equalsSignalStrength(NetworkCapabilities nc) {
836 return this.mSignalStrength == nc.mSignalStrength;
837 }
838
839 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700840 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900841 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700842 */
843 public void combineCapabilities(NetworkCapabilities nc) {
844 combineNetCapabilities(nc);
845 combineTransportTypes(nc);
846 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700847 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900848 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700849 }
850
851 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900852 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
853 *
854 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
855 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
856 * bandwidth, signal strength, or validation / captive portal status.
857 *
858 * @hide
859 */
860 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
861 return (nc != null &&
862 satisfiedByNetCapabilities(nc, onlyImmutable) &&
863 satisfiedByTransportTypes(nc) &&
864 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900865 satisfiedBySpecifier(nc) &&
866 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900867 }
868
869 /**
870 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
871 *
872 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
873 *
874 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700875 */
876 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900877 return satisfiedByNetworkCapabilities(nc, false);
878 }
879
880 /**
881 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
882 *
883 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
884 *
885 * @hide
886 */
887 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
888 return satisfiedByNetworkCapabilities(nc, true);
889 }
890
891 /**
892 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +0900893 * {@code NetworkCapabilities} and return a String describing any difference.
894 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900895 *
896 * @hide
897 */
Hugo Benichieae7a222017-07-25 11:40:56 +0900898 public String describeImmutableDifferences(NetworkCapabilities that) {
899 if (that == null) {
900 return "other NetworkCapabilities was null";
901 }
902
903 StringJoiner joiner = new StringJoiner(", ");
904
Hugo Benichieae7a222017-07-25 11:40:56 +0900905 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
906 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +0900907 // Ignore DUN being added or removed. http://b/65257223.
908 final long mask = ~MUTABLE_CAPABILITIES
909 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +0900910 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
911 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
912 if (oldImmutableCapabilities != newImmutableCapabilities) {
913 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
914 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
915 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
916 }
917
918 if (!equalsSpecifier(that)) {
919 NetworkSpecifier before = this.getNetworkSpecifier();
920 NetworkSpecifier after = that.getNetworkSpecifier();
921 joiner.add(String.format("specifier changed: %s -> %s", before, after));
922 }
923
924 if (!equalsTransportTypes(that)) {
925 String before = transportNamesOf(this.getTransportTypes());
926 String after = transportNamesOf(that.getTransportTypes());
927 joiner.add(String.format("transports changed: %s -> %s", before, after));
928 }
929
930 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700931 }
932
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900933 /**
934 * Checks that our requestable capabilities are the same as those of the given
935 * {@code NetworkCapabilities}.
936 *
937 * @hide
938 */
939 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
940 if (nc == null) return false;
941 return (equalsNetCapabilitiesRequestable(nc) &&
942 equalsTransportTypes(nc) &&
943 equalsSpecifier(nc));
944 }
945
Robert Greenwalt1448f052014-04-08 13:41:39 -0700946 @Override
947 public boolean equals(Object obj) {
948 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
949 NetworkCapabilities that = (NetworkCapabilities)obj;
950 return (equalsNetCapabilities(that) &&
951 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700952 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900953 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700954 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700955 }
956
957 @Override
958 public int hashCode() {
959 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
960 ((int)(mNetworkCapabilities >> 32) * 3) +
961 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
962 ((int)(mTransportTypes >> 32) * 7) +
963 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700964 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700965 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900966 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700967 }
968
Wink Saville4e2dea72014-09-20 11:04:03 -0700969 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700970 public int describeContents() {
971 return 0;
972 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700973 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700974 public void writeToParcel(Parcel dest, int flags) {
975 dest.writeLong(mNetworkCapabilities);
976 dest.writeLong(mTransportTypes);
977 dest.writeInt(mLinkUpBandwidthKbps);
978 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700979 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900980 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700981 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900982
Robert Greenwalt1448f052014-04-08 13:41:39 -0700983 public static final Creator<NetworkCapabilities> CREATOR =
984 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700985 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700986 public NetworkCapabilities createFromParcel(Parcel in) {
987 NetworkCapabilities netCap = new NetworkCapabilities();
988
989 netCap.mNetworkCapabilities = in.readLong();
990 netCap.mTransportTypes = in.readLong();
991 netCap.mLinkUpBandwidthKbps = in.readInt();
992 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700993 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900994 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700995 return netCap;
996 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700997 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700998 public NetworkCapabilities[] newArray(int size) {
999 return new NetworkCapabilities[size];
1000 }
1001 };
1002
Wink Saville4e2dea72014-09-20 11:04:03 -07001003 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001004 public String toString() {
Hugo Benichieae7a222017-07-25 11:40:56 +09001005 // TODO: enumerate bits for transports and capabilities instead of creating arrays.
1006 // TODO: use a StringBuilder instead of string concatenation.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001007 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +09001008 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001009
Robert Greenwalt7569f182014-06-08 16:42:59 -07001010 types = getCapabilities();
1011 String capabilities = (types.length > 0 ? " Capabilities: " : "");
1012 for (int i = 0; i < types.length; ) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001013 capabilities += capabilityNameOf(types[i]);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001014 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001015 }
1016
1017 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
1018 mLinkUpBandwidthKbps + "Kbps" : "");
1019 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
1020 mLinkDownBandwidthKbps + "Kbps" : "");
1021
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001022 String specifier = (mNetworkSpecifier == null ?
1023 "" : " Specifier: <" + mNetworkSpecifier + ">");
1024
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001025 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
1026
1027 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001028 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001029
1030 /**
1031 * @hide
1032 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001033 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001034 StringJoiner joiner = new StringJoiner("|");
1035 if (capabilities != null) {
1036 for (int c : capabilities) {
1037 joiner.add(capabilityNameOf(c));
1038 }
1039 }
1040 return joiner.toString();
1041 }
1042
1043 /**
1044 * @hide
1045 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001046 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001047 switch (capability) {
1048 case NET_CAPABILITY_MMS: return "MMS";
1049 case NET_CAPABILITY_SUPL: return "SUPL";
1050 case NET_CAPABILITY_DUN: return "DUN";
1051 case NET_CAPABILITY_FOTA: return "FOTA";
1052 case NET_CAPABILITY_IMS: return "IMS";
1053 case NET_CAPABILITY_CBS: return "CBS";
1054 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1055 case NET_CAPABILITY_IA: return "IA";
1056 case NET_CAPABILITY_RCS: return "RCS";
1057 case NET_CAPABILITY_XCAP: return "XCAP";
1058 case NET_CAPABILITY_EIMS: return "EIMS";
1059 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1060 case NET_CAPABILITY_INTERNET: return "INTERNET";
1061 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1062 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1063 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1064 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1065 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001066 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001067 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001068 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Hugo Benichieae7a222017-07-25 11:40:56 +09001069 default: return Integer.toString(capability);
1070 }
1071 }
1072
1073 /**
1074 * @hide
1075 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001076 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001077 StringJoiner joiner = new StringJoiner("|");
1078 if (types != null) {
1079 for (int t : types) {
1080 joiner.add(transportNameOf(t));
1081 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001082 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001083 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001084 }
1085
1086 /**
1087 * @hide
1088 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001089 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001090 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001091 return "UNKNOWN";
1092 }
1093 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001094 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001095
Jeff Sharkeyde570312017-10-24 21:25:50 -06001096 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001097 Preconditions.checkArgument(
1098 isValidTransport(transport), "Invalid TransportType " + transport);
1099 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001100}