blob: 22cffcc377f2bd5318a837e16ecbf04af3b927e9 [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 Jeanecacd5e2017-12-27 14:23:31 +090023import android.util.ArraySet;
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;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090032import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090033import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070034
35/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070036 * Representation of the capabilities of an active network. Instances are
37 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060038 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
39 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060040 * <p>
41 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
42 * network selection. Rather than indicate a need for Wi-Fi because an
43 * application needs high bandwidth and risk obsolescence when a new, fast
44 * network appears (like LTE), the application should specify it needs high
45 * bandwidth. Similarly if an application needs an unmetered network for a bulk
46 * transfer it can specify that rather than assuming all cellular based
47 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070048 */
49public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070050 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090051 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070052
Robert Greenwalt7569f182014-06-08 16:42:59 -070053 /**
54 * @hide
55 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070056 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090057 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090058 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070059 }
60
61 public NetworkCapabilities(NetworkCapabilities nc) {
62 if (nc != null) {
63 mNetworkCapabilities = nc.mNetworkCapabilities;
64 mTransportTypes = nc.mTransportTypes;
65 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
66 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070067 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090068 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090069 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090070 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080071 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070072 }
73 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070074
75 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090076 * Completely clears the contents of this object, removing even the capabilities that are set
77 * by default when the object is constructed.
78 * @hide
79 */
80 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080081 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070082 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090083 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090084 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090085 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090086 mEstablishingVpnAppUid = INVALID_UID;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090087 }
88
89 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070090 * Represents the network's capabilities. If any are specified they will be satisfied
91 * by any Network that matches all of them.
92 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090093 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070094
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080095 /**
96 * If any capabilities specified here they must not exist in the matching Network.
97 */
98 private long mUnwantedNetworkCapabilities;
99
Jeff Sharkeyde570312017-10-24 21:25:50 -0600100 /** @hide */
101 @Retention(RetentionPolicy.SOURCE)
102 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
103 NET_CAPABILITY_MMS,
104 NET_CAPABILITY_SUPL,
105 NET_CAPABILITY_DUN,
106 NET_CAPABILITY_FOTA,
107 NET_CAPABILITY_IMS,
108 NET_CAPABILITY_CBS,
109 NET_CAPABILITY_WIFI_P2P,
110 NET_CAPABILITY_IA,
111 NET_CAPABILITY_RCS,
112 NET_CAPABILITY_XCAP,
113 NET_CAPABILITY_EIMS,
114 NET_CAPABILITY_NOT_METERED,
115 NET_CAPABILITY_INTERNET,
116 NET_CAPABILITY_NOT_RESTRICTED,
117 NET_CAPABILITY_TRUSTED,
118 NET_CAPABILITY_NOT_VPN,
119 NET_CAPABILITY_VALIDATED,
120 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600121 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600122 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900123 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900124 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800125 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600126 })
127 public @interface NetCapability { }
128
Robert Greenwalt1448f052014-04-08 13:41:39 -0700129 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700130 * Indicates this is a network that has the ability to reach the
131 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700132 */
133 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700134
135 /**
136 * Indicates this is a network that has the ability to reach the carrier's
137 * SUPL server, used to retrieve GPS information.
138 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700139 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700140
141 /**
142 * Indicates this is a network that has the ability to reach the carrier's
143 * DUN or tethering gateway.
144 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700145 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700146
147 /**
148 * Indicates this is a network that has the ability to reach the carrier's
149 * FOTA portal, used for over the air updates.
150 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700151 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700152
153 /**
154 * Indicates this is a network that has the ability to reach the carrier's
155 * IMS servers, used for network registration and signaling.
156 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700157 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700158
159 /**
160 * Indicates this is a network that has the ability to reach the carrier's
161 * CBS servers, used for carrier specific services.
162 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700163 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700164
165 /**
166 * Indicates this is a network that has the ability to reach a Wi-Fi direct
167 * peer.
168 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700169 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700170
171 /**
172 * Indicates this is a network that has the ability to reach a carrier's
173 * Initial Attach servers.
174 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700175 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700176
177 /**
178 * Indicates this is a network that has the ability to reach a carrier's
179 * RCS servers, used for Rich Communication Services.
180 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700181 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700182
183 /**
184 * Indicates this is a network that has the ability to reach a carrier's
185 * XCAP servers, used for configuration and control.
186 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700187 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700188
189 /**
190 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700191 * Emergency IMS servers or other services, used for network signaling
192 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195
196 /**
197 * Indicates that this network is unmetered.
198 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700199 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700200
201 /**
202 * Indicates that this network should be able to reach the internet.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates that this network is available for general use. If this is not set
208 * applications should not attempt to communicate on this network. Note that this
209 * is simply informative and not enforcement - enforcement is handled via other means.
210 * Set by default.
211 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700212 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
213
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700214 /**
215 * Indicates that the user has indicated implicit trust of this network. This
216 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
217 * BT device or a wifi the user asked to connect to. Untrusted networks
218 * are probably limited to unknown wifi AP. Set by default.
219 */
220 public static final int NET_CAPABILITY_TRUSTED = 14;
221
Paul Jensen76b610a2015-03-18 09:33:07 -0400222 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400223 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400224 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400225 */
226 public static final int NET_CAPABILITY_NOT_VPN = 15;
227
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900228 /**
229 * Indicates that connectivity on this network was successfully validated. For example, for a
230 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
231 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900232 */
233 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700234
Paul Jensen3d194ea2015-06-16 14:27:36 -0400235 /**
236 * Indicates that this network was found to have a captive portal in place last time it was
237 * probed.
238 */
239 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
240
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900241 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600242 * Indicates that this network is not roaming.
243 */
244 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
245
246 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900247 * Indicates that this network is available for use by apps, and not a network that is being
248 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900249 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600250 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900251
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900252 /**
253 * Indicates that this network is not congested.
254 * <p>
255 * When a network is congested, the device should defer network traffic that
256 * can be done at a later time without breaking developer contracts.
257 * @hide
258 */
259 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
260
Chalard Jean804b8fb2018-01-30 22:41:41 +0900261 /**
262 * Indicates that this network is not currently suspended.
263 * <p>
264 * When a network is suspended, the network's IP addresses and any connections
265 * established on the network remain valid, but the network is temporarily unable
266 * to transfer data. This can happen, for example, if a cellular network experiences
267 * a temporary loss of signal, such as when driving through a tunnel, etc.
268 * A network with this capability is not suspended, so is expected to be able to
269 * transfer data.
270 */
271 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
272
Pavel Maltsev43403202018-01-30 17:19:44 -0800273 /**
274 * Indicates that traffic that goes through this network is paid by oem. For example,
275 * this network can be used by system apps to upload telemetry data.
276 * @hide
277 */
278 public static final int NET_CAPABILITY_OEM_PAID = 22;
279
Robert Greenwalt1448f052014-04-08 13:41:39 -0700280 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800281 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700282
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700283 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900284 * Network capabilities that are expected to be mutable, i.e., can change while a particular
285 * network is connected.
286 */
287 private static final long MUTABLE_CAPABILITIES =
288 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
289 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900290 (1 << NET_CAPABILITY_TRUSTED)
291 | (1 << NET_CAPABILITY_VALIDATED)
292 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
293 | (1 << NET_CAPABILITY_NOT_ROAMING)
294 | (1 << NET_CAPABILITY_FOREGROUND)
295 | (1 << NET_CAPABILITY_NOT_CONGESTED)
296 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900297
298 /**
299 * Network capabilities that are not allowed in NetworkRequests. This exists because the
300 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
301 * capability's presence cannot be known in advance. If such a capability is requested, then we
302 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
303 * get immediately torn down because they do not have the requested capability.
304 */
305 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900306 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900307
308 /**
309 * Capabilities that are set by default when the object is constructed.
310 */
311 private static final long DEFAULT_CAPABILITIES =
312 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
313 (1 << NET_CAPABILITY_TRUSTED) |
314 (1 << NET_CAPABILITY_NOT_VPN);
315
316 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400317 * Capabilities that suggest that a network is restricted.
318 * {@see #maybeMarkCapabilitiesRestricted}.
319 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700320 @VisibleForTesting
321 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400322 (1 << NET_CAPABILITY_CBS) |
323 (1 << NET_CAPABILITY_DUN) |
324 (1 << NET_CAPABILITY_EIMS) |
325 (1 << NET_CAPABILITY_FOTA) |
326 (1 << NET_CAPABILITY_IA) |
327 (1 << NET_CAPABILITY_IMS) |
328 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev43403202018-01-30 17:19:44 -0800329 (1 << NET_CAPABILITY_XCAP) |
330 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400331
332 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700333 * Capabilities that suggest that a network is unrestricted.
334 * {@see #maybeMarkCapabilitiesRestricted}.
335 */
336 @VisibleForTesting
337 /* package */ static final long UNRESTRICTED_CAPABILITIES =
338 (1 << NET_CAPABILITY_INTERNET) |
339 (1 << NET_CAPABILITY_MMS) |
340 (1 << NET_CAPABILITY_SUPL) |
341 (1 << NET_CAPABILITY_WIFI_P2P);
342
343 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700344 * Adds the given capability to this {@code NetworkCapability} instance.
345 * Multiple capabilities may be applied sequentially. Note that when searching
346 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800347 * <p>
348 * If the given capability was previously added to the list of unwanted capabilities
349 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700350 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600351 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900352 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700353 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700354 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600355 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800356 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700357 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800358 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700359 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700360 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700361
362 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800363 * Adds the given capability to the list of unwanted capabilities of this
364 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
365 * sequentially. Note that when searching for a network to satisfy a request, the network
366 * must not contain any capability from unwanted capability list.
367 * <p>
368 * If the capability was previously added to the list of required capabilities (for
369 * example, it was there by default or added using {@link #addCapability(int)} method), then
370 * it will be removed from the list of required capabilities as well.
371 *
372 * @see #addCapability(int)
373 * @hide
374 */
375 public void addUnwantedCapability(@NetCapability int capability) {
376 checkValidCapability(capability);
377 mUnwantedNetworkCapabilities |= 1 << capability;
378 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
379 }
380
381 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700382 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800383 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800384 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800385 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700386 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600387 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900388 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700389 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700390 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600391 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800392 checkValidCapability(capability);
393 final long mask = ~(1 << capability);
394 mNetworkCapabilities &= mask;
395 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700396 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700397 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700398
399 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600400 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
401 * instance.
402 *
403 * @hide
404 */
405 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
406 if (value) {
407 addCapability(capability);
408 } else {
409 removeCapability(capability);
410 }
411 return this;
412 }
413
414 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700415 * Gets all the capabilities set on this {@code NetworkCapability} instance.
416 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600417 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700418 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700419 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600420 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900421 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700422 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700423
424 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800425 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
426 *
427 * @return an array of unwanted capability values for this instance.
428 * @hide
429 */
430 public @NetCapability int[] getUnwantedCapabilities() {
431 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
432 }
433
434
435 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600436 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700437 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600438 *
439 * @hide
440 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800441 public void setCapabilities(@NetCapability int[] capabilities,
442 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600443 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800444 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600445 }
446
447 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800448 * @deprecated use {@link #setCapabilities(int[], int[])}
449 * @hide
450 */
451 @Deprecated
452 public void setCapabilities(@NetCapability int[] capabilities) {
453 setCapabilities(capabilities, new int[] {});
454 }
455
456 /**
457 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700458 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600459 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700460 * @return {@code true} if set on this instance.
461 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600462 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800463 return isValidCapability(capability)
464 && ((mNetworkCapabilities & (1 << capability)) != 0);
465 }
466
467 /** @hide */
468 public boolean hasUnwantedCapability(@NetCapability int capability) {
469 return isValidCapability(capability)
470 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700471 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700472
Pavel Maltseve18ef262018-03-07 11:13:04 -0800473 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700474 private void combineNetCapabilities(NetworkCapabilities nc) {
475 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800476 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700477 }
478
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900479 /**
480 * Convenience function that returns a human-readable description of the first mutable
481 * capability we find. Used to present an error message to apps that request mutable
482 * capabilities.
483 *
484 * @hide
485 */
486 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800487 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
488 & NON_REQUESTABLE_CAPABILITIES;
489
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900490 if (nonRequestable != 0) {
491 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900492 }
493 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900494 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900495 return null;
496 }
497
498 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800499 long requestedCapabilities = mNetworkCapabilities;
500 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
501 long providedCapabilities = nc.mNetworkCapabilities;
502
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900503 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800504 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
505 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900506 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800507 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
508 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700509 }
510
Robert Greenwalt06314e42014-10-29 14:04:06 -0700511 /** @hide */
512 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800513 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
514 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700515 }
516
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900517 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
518 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800519 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
520 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
521 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900522 }
523
Robert Greenwalt1448f052014-04-08 13:41:39 -0700524 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400525 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
526 * typically provided by restricted networks.
527 *
528 * TODO: consider:
529 * - Renaming it to guessRestrictedCapability and make it set the
530 * restricted capability bit in addition to clearing it.
531 * @hide
532 */
533 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700534 // Verify there aren't any unrestricted capabilities. If there are we say
535 // the whole thing is unrestricted.
536 final boolean hasUnrestrictedCapabilities =
537 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
538
539 // Must have at least some restricted capabilities.
540 final boolean hasRestrictedCapabilities =
541 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
542
543 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400544 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400545 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400546 }
547
548 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700549 * Representing the transport type. Apps should generally not care about transport. A
550 * request for a fast internet connection could be satisfied by a number of different
551 * transports. If any are specified here it will be satisfied a Network that matches
552 * any of them. If a caller doesn't care about the transport it should not specify any.
553 */
554 private long mTransportTypes;
555
Jeff Sharkeyde570312017-10-24 21:25:50 -0600556 /** @hide */
557 @Retention(RetentionPolicy.SOURCE)
558 @IntDef(prefix = { "TRANSPORT_" }, value = {
559 TRANSPORT_CELLULAR,
560 TRANSPORT_WIFI,
561 TRANSPORT_BLUETOOTH,
562 TRANSPORT_ETHERNET,
563 TRANSPORT_VPN,
564 TRANSPORT_WIFI_AWARE,
565 TRANSPORT_LOWPAN,
566 })
567 public @interface Transport { }
568
Robert Greenwalt1448f052014-04-08 13:41:39 -0700569 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700570 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 */
572 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700573
574 /**
575 * Indicates this network uses a Wi-Fi transport.
576 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700578
579 /**
580 * Indicates this network uses a Bluetooth transport.
581 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700582 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700583
584 /**
585 * Indicates this network uses an Ethernet transport.
586 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700587 public static final int TRANSPORT_ETHERNET = 3;
588
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400589 /**
590 * Indicates this network uses a VPN transport.
591 */
592 public static final int TRANSPORT_VPN = 4;
593
Etan Cohen305ea282016-06-20 09:27:12 -0700594 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700595 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700596 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700597 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700598
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700599 /**
600 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700601 */
602 public static final int TRANSPORT_LOWPAN = 6;
603
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900604 /** @hide */
605 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
606 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700607 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700608
Hugo Benichi16f0a942017-06-20 14:07:59 +0900609 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600610 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900611 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
612 }
613
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900614 private static final String[] TRANSPORT_NAMES = {
615 "CELLULAR",
616 "WIFI",
617 "BLUETOOTH",
618 "ETHERNET",
619 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700620 "WIFI_AWARE",
621 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900622 };
623
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700624 /**
625 * Adds the given transport type to this {@code NetworkCapability} instance.
626 * Multiple transports may be applied sequentially. Note that when searching
627 * for a network to satisfy a request, any listed in the request will satisfy the request.
628 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
629 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
630 * to be selected. This is logically different than
631 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
632 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600633 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900634 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700635 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700636 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600637 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900638 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700640 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700641 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700642 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700643
644 /**
645 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
646 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600647 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900648 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700649 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700650 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600651 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900652 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700653 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700654 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700655 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700656 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700657
658 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600659 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
660 * instance.
661 *
662 * @hide
663 */
664 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
665 if (value) {
666 addTransportType(transportType);
667 } else {
668 removeTransportType(transportType);
669 }
670 return this;
671 }
672
673 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674 * Gets all the transports set on this {@code NetworkCapability} instance.
675 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600676 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700677 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700678 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600679 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900680 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700681 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700682
683 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600684 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700685 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600686 *
687 * @hide
688 */
689 public void setTransportTypes(@Transport int[] transportTypes) {
690 mTransportTypes = BitUtils.packBits(transportTypes);
691 }
692
693 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700694 * Tests for the presence of a transport on this instance.
695 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600696 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700697 * @return {@code true} if set on this instance.
698 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600699 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900700 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700701 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700702
703 private void combineTransportTypes(NetworkCapabilities nc) {
704 this.mTransportTypes |= nc.mTransportTypes;
705 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900706
Robert Greenwalt1448f052014-04-08 13:41:39 -0700707 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
708 return ((this.mTransportTypes == 0) ||
709 ((this.mTransportTypes & nc.mTransportTypes) != 0));
710 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900711
Robert Greenwalt06314e42014-10-29 14:04:06 -0700712 /** @hide */
713 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714 return (nc.mTransportTypes == this.mTransportTypes);
715 }
716
717 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900718 * UID of the app that manages this network, or INVALID_UID if none/unknown.
719 *
720 * This field keeps track of the UID of the app that created this network and is in charge
721 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
722 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
723 * to create networks.
724 *
725 * Because this field is only used in the services side (and to avoid apps being able to
726 * set this to whatever they want), this field is not parcelled and will not be conserved
727 * across the IPC boundary.
728 * @hide
729 */
730 private int mEstablishingVpnAppUid = INVALID_UID;
731
732 /**
733 * Set the UID of the managing app.
734 * @hide
735 */
736 public void setEstablishingVpnAppUid(final int uid) {
737 mEstablishingVpnAppUid = uid;
738 }
739
740 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600741 * Value indicating that link bandwidth is unspecified.
742 * @hide
743 */
744 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
745
746 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700747 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
748 * for the first hop on the given transport. It is not measured, but may take into account
749 * link parameters (Radio technology, allocated channels, etc).
750 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600751 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
752 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700753
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700754 /**
755 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
756 * the estimated first hop transport bandwidth.
757 * <p>
758 * Note that when used to request a network, this specifies the minimum acceptable.
759 * When received as the state of an existing network this specifies the typical
760 * first hop bandwidth expected. This is never measured, but rather is inferred
761 * from technology type and other link parameters. It could be used to differentiate
762 * between very slow 1xRTT cellular links and other faster networks or even between
763 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
764 * fast backhauls and slow backhauls.
765 *
766 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700767 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700768 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600769 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700770 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600771 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700773
774 /**
775 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
776 * the estimated first hop transport bandwidth.
777 *
778 * @return The estimated first hop upstream (device to network) bandwidth.
779 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 public int getLinkUpstreamBandwidthKbps() {
781 return mLinkUpBandwidthKbps;
782 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700783
784 /**
785 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
786 * the estimated first hop transport bandwidth.
787 * <p>
788 * Note that when used to request a network, this specifies the minimum acceptable.
789 * When received as the state of an existing network this specifies the typical
790 * first hop bandwidth expected. This is never measured, but rather is inferred
791 * from technology type and other link parameters. It could be used to differentiate
792 * between very slow 1xRTT cellular links and other faster networks or even between
793 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
794 * fast backhauls and slow backhauls.
795 *
796 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700797 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700798 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600799 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700800 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600801 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700802 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700803
804 /**
805 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
806 * the estimated first hop transport bandwidth.
807 *
808 * @return The estimated first hop downstream (network to device) bandwidth.
809 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700810 public int getLinkDownstreamBandwidthKbps() {
811 return mLinkDownBandwidthKbps;
812 }
813
814 private void combineLinkBandwidths(NetworkCapabilities nc) {
815 this.mLinkUpBandwidthKbps =
816 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
817 this.mLinkDownBandwidthKbps =
818 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
819 }
820 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
821 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
822 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
823 }
824 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
825 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
826 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
827 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600828 /** @hide */
829 public static int minBandwidth(int a, int b) {
830 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
831 return b;
832 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
833 return a;
834 } else {
835 return Math.min(a, b);
836 }
837 }
838 /** @hide */
839 public static int maxBandwidth(int a, int b) {
840 return Math.max(a, b);
841 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700842
Etan Cohena7434272017-04-03 12:17:51 -0700843 private NetworkSpecifier mNetworkSpecifier = null;
844
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700845 /**
846 * Sets the optional bearer specific network specifier.
847 * This has no meaning if a single transport is also not specified, so calling
848 * this without a single transport set will generate an exception, as will
849 * subsequently adding or removing transports after this is set.
850 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700851 *
Etan Cohena7434272017-04-03 12:17:51 -0700852 * @param networkSpecifier A concrete, parcelable framework class that extends
853 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900854 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700855 * @hide
856 */
Etan Cohena7434272017-04-03 12:17:51 -0700857 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
858 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700859 throw new IllegalStateException("Must have a single transport specified to use " +
860 "setNetworkSpecifier");
861 }
Etan Cohena7434272017-04-03 12:17:51 -0700862
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700863 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700864
Pierre Imaic8419a82016-03-22 17:54:54 +0900865 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700866 }
867
868 /**
869 * Gets the optional bearer specific network specifier.
870 *
Etan Cohena7434272017-04-03 12:17:51 -0700871 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
872 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700873 * @hide
874 */
Etan Cohena7434272017-04-03 12:17:51 -0700875 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700876 return mNetworkSpecifier;
877 }
878
879 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700880 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700881 throw new IllegalStateException("Can't combine two networkSpecifiers");
882 }
Etan Cohena7434272017-04-03 12:17:51 -0700883 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700884 }
Etan Cohena7434272017-04-03 12:17:51 -0700885
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700886 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700887 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
888 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700889 }
Etan Cohena7434272017-04-03 12:17:51 -0700890
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700891 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700892 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 }
894
Robert Greenwalt1448f052014-04-08 13:41:39 -0700895 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900896 * Magic value that indicates no signal strength provided. A request specifying this value is
897 * always satisfied.
898 *
899 * @hide
900 */
901 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
902
903 /**
904 * Signal strength. This is a signed integer, and higher values indicate better signal.
905 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
906 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700907 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900908
909 /**
910 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
911 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
912 * reported by WifiManager.
913 * <p>
914 * Note that when used to register a network callback, this specifies the minimum acceptable
915 * signal strength. When received as the state of an existing network it specifies the current
916 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
917 * effect when requesting a callback.
918 *
919 * @param signalStrength the bearer-specific signal strength.
920 * @hide
921 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600922 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900923 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600924 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900925 }
926
927 /**
928 * Returns {@code true} if this object specifies a signal strength.
929 *
930 * @hide
931 */
932 public boolean hasSignalStrength() {
933 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
934 }
935
936 /**
937 * Retrieves the signal strength.
938 *
939 * @return The bearer-specific signal strength.
940 * @hide
941 */
942 public int getSignalStrength() {
943 return mSignalStrength;
944 }
945
946 private void combineSignalStrength(NetworkCapabilities nc) {
947 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
948 }
949
950 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
951 return this.mSignalStrength <= nc.mSignalStrength;
952 }
953
954 private boolean equalsSignalStrength(NetworkCapabilities nc) {
955 return this.mSignalStrength == nc.mSignalStrength;
956 }
957
958 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900959 * List of UIDs this network applies to. No restriction if null.
960 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900961 * For networks, mUids represent the list of network this applies to, and null means this
962 * network applies to all UIDs.
963 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
964 * must be included in a network so that they match. As an exception to the general rule,
965 * a null mUids field for requests mean "no requirements" rather than what the general rule
966 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
967 * of this API expect in practice. A network that must match all UIDs can still be
968 * expressed with a set ranging the entire set of possible UIDs.
969 * <p>
970 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900971 * the UIDs in this list, and it is their default network. Apps in this list that wish to
972 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
973 * member is null, then the network is not restricted by app UID. If it's an empty list, then
974 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900975 * As a special exception, the app managing this network (as identified by its UID stored in
976 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
977 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
978 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900979 * <p>
980 * Please note that in principle a single app can be associated with multiple UIDs because
981 * each app will have a different UID when it's run as a different (macro-)user. A single
982 * macro user can only have a single active VPN app at any given time however.
983 * <p>
984 * Also please be aware this class does not try to enforce any normalization on this. Callers
985 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
986 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
987 * their own (like requiring sortedness or no overlap) they need to enforce it
988 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
989 * or overlapping ranges are present.
990 *
991 * @hide
992 */
Chalard Jeancf8c3fe2018-01-25 09:41:51 +0900993 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900994
995 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900996 * Convenience method to set the UIDs this network applies to to a single UID.
997 * @hide
998 */
999 public NetworkCapabilities setSingleUid(int uid) {
1000 final ArraySet<UidRange> identity = new ArraySet<>(1);
1001 identity.add(new UidRange(uid, uid));
1002 setUids(identity);
1003 return this;
1004 }
1005
1006 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001007 * Set the list of UIDs this network applies to.
1008 * This makes a copy of the set so that callers can't modify it after the call.
1009 * @hide
1010 */
1011 public NetworkCapabilities setUids(Set<UidRange> uids) {
1012 if (null == uids) {
1013 mUids = null;
1014 } else {
1015 mUids = new ArraySet<>(uids);
1016 }
1017 return this;
1018 }
1019
1020 /**
1021 * Get the list of UIDs this network applies to.
1022 * This returns a copy of the set so that callers can't modify the original object.
1023 * @hide
1024 */
1025 public Set<UidRange> getUids() {
1026 return null == mUids ? null : new ArraySet<>(mUids);
1027 }
1028
1029 /**
1030 * Test whether this network applies to this UID.
1031 * @hide
1032 */
1033 public boolean appliesToUid(int uid) {
1034 if (null == mUids) return true;
1035 for (UidRange range : mUids) {
1036 if (range.contains(uid)) {
1037 return true;
1038 }
1039 }
1040 return false;
1041 }
1042
1043 /**
1044 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
1045 * <p>
1046 * This test only checks whether equal range objects are in both sets. It will
1047 * return false if the ranges are not exactly the same, even if the covered UIDs
1048 * are for an equivalent result.
1049 * <p>
1050 * Note that this method is not very optimized, which is fine as long as it's not used very
1051 * often.
1052 * <p>
1053 * nc is assumed nonnull.
1054 *
1055 * @hide
1056 */
1057 @VisibleForTesting
1058 public boolean equalsUids(NetworkCapabilities nc) {
1059 Set<UidRange> comparedUids = nc.mUids;
1060 if (null == comparedUids) return null == mUids;
1061 if (null == mUids) return false;
1062 // Make a copy so it can be mutated to check that all ranges in mUids
1063 // also are in uids.
1064 final Set<UidRange> uids = new ArraySet<>(mUids);
1065 for (UidRange range : comparedUids) {
1066 if (!uids.contains(range)) {
1067 return false;
1068 }
1069 uids.remove(range);
1070 }
1071 return uids.isEmpty();
1072 }
1073
1074 /**
1075 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1076 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001077 * This method is called on the NetworkCapabilities embedded in a request with the
1078 * capabilities of an available network. It checks whether all the UIDs from this listen
1079 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1080 * in the passed nc (representing the UIDs that this network is available to).
1081 * <p>
1082 * As a special exception, the UID that created the passed network (as represented by its
1083 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1084 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1085 * can see its own network when it listens for it.
1086 * <p>
1087 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001088 * @see #appliesToUid
1089 * @hide
1090 */
1091 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001092 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001093 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001094 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001095 if (!nc.appliesToUidRange(requiredRange)) {
1096 return false;
1097 }
1098 }
1099 return true;
1100 }
1101
1102 /**
1103 * Returns whether this network applies to the passed ranges.
1104 * This assumes that to apply, the passed range has to be entirely contained
1105 * within one of the ranges this network applies to. If the ranges are not normalized,
1106 * this method may return false even though all required UIDs are covered because no
1107 * single range contained them all.
1108 * @hide
1109 */
1110 @VisibleForTesting
1111 public boolean appliesToUidRange(UidRange requiredRange) {
1112 if (null == mUids) return true;
1113 for (UidRange uidRange : mUids) {
1114 if (uidRange.containsRange(requiredRange)) {
1115 return true;
1116 }
1117 }
1118 return false;
1119 }
1120
1121 /**
1122 * Combine the UIDs this network currently applies to with the UIDs the passed
1123 * NetworkCapabilities apply to.
1124 * nc is assumed nonnull.
1125 */
1126 private void combineUids(NetworkCapabilities nc) {
1127 if (null == nc.mUids || null == mUids) {
1128 mUids = null;
1129 return;
1130 }
1131 mUids.addAll(nc.mUids);
1132 }
1133
1134 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001135 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1136 * <p>
1137 * Note that this method may break an invariant of having a particular capability in either
1138 * wanted or unwanted lists but never in both. Requests that have the same capability in
1139 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001140 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001141 */
1142 public void combineCapabilities(NetworkCapabilities nc) {
1143 combineNetCapabilities(nc);
1144 combineTransportTypes(nc);
1145 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001146 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001147 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001148 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001149 }
1150
1151 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001152 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1153 *
1154 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1155 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1156 * bandwidth, signal strength, or validation / captive portal status.
1157 *
1158 * @hide
1159 */
1160 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001161 return (nc != null
1162 && satisfiedByNetCapabilities(nc, onlyImmutable)
1163 && satisfiedByTransportTypes(nc)
1164 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1165 && satisfiedBySpecifier(nc)
1166 && (onlyImmutable || satisfiedBySignalStrength(nc))
1167 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001168 }
1169
1170 /**
1171 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1172 *
1173 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1174 *
1175 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001176 */
1177 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001178 return satisfiedByNetworkCapabilities(nc, false);
1179 }
1180
1181 /**
1182 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1183 *
1184 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1185 *
1186 * @hide
1187 */
1188 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1189 return satisfiedByNetworkCapabilities(nc, true);
1190 }
1191
1192 /**
1193 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001194 * {@code NetworkCapabilities} and return a String describing any difference.
1195 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001196 *
1197 * @hide
1198 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001199 public String describeImmutableDifferences(NetworkCapabilities that) {
1200 if (that == null) {
1201 return "other NetworkCapabilities was null";
1202 }
1203
1204 StringJoiner joiner = new StringJoiner(", ");
1205
Hugo Benichieae7a222017-07-25 11:40:56 +09001206 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1207 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001208 // Ignore DUN being added or removed. http://b/65257223.
1209 final long mask = ~MUTABLE_CAPABILITIES
1210 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001211 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1212 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1213 if (oldImmutableCapabilities != newImmutableCapabilities) {
1214 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1215 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1216 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1217 }
1218
1219 if (!equalsSpecifier(that)) {
1220 NetworkSpecifier before = this.getNetworkSpecifier();
1221 NetworkSpecifier after = that.getNetworkSpecifier();
1222 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1223 }
1224
1225 if (!equalsTransportTypes(that)) {
1226 String before = transportNamesOf(this.getTransportTypes());
1227 String after = transportNamesOf(that.getTransportTypes());
1228 joiner.add(String.format("transports changed: %s -> %s", before, after));
1229 }
1230
1231 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001232 }
1233
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001234 /**
1235 * Checks that our requestable capabilities are the same as those of the given
1236 * {@code NetworkCapabilities}.
1237 *
1238 * @hide
1239 */
1240 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1241 if (nc == null) return false;
1242 return (equalsNetCapabilitiesRequestable(nc) &&
1243 equalsTransportTypes(nc) &&
1244 equalsSpecifier(nc));
1245 }
1246
Robert Greenwalt1448f052014-04-08 13:41:39 -07001247 @Override
1248 public boolean equals(Object obj) {
1249 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001250 NetworkCapabilities that = (NetworkCapabilities) obj;
1251 return (equalsNetCapabilities(that)
1252 && equalsTransportTypes(that)
1253 && equalsLinkBandwidths(that)
1254 && equalsSignalStrength(that)
1255 && equalsSpecifier(that)
1256 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001257 }
1258
1259 @Override
1260 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001261 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001262 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001263 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1264 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1265 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1266 + ((int) (mTransportTypes >> 32) * 13)
1267 + (mLinkUpBandwidthKbps * 17)
1268 + (mLinkDownBandwidthKbps * 19)
1269 + Objects.hashCode(mNetworkSpecifier) * 23
1270 + (mSignalStrength * 29)
1271 + Objects.hashCode(mUids) * 31;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001272 }
1273
Wink Saville4e2dea72014-09-20 11:04:03 -07001274 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001275 public int describeContents() {
1276 return 0;
1277 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001278 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001279 public void writeToParcel(Parcel dest, int flags) {
1280 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001281 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001282 dest.writeLong(mTransportTypes);
1283 dest.writeInt(mLinkUpBandwidthKbps);
1284 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001285 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001286 dest.writeInt(mSignalStrength);
Chalard Jeancf8c3fe2018-01-25 09:41:51 +09001287 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001288 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001289
Robert Greenwalt1448f052014-04-08 13:41:39 -07001290 public static final Creator<NetworkCapabilities> CREATOR =
1291 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001292 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001293 public NetworkCapabilities createFromParcel(Parcel in) {
1294 NetworkCapabilities netCap = new NetworkCapabilities();
1295
1296 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001297 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001298 netCap.mTransportTypes = in.readLong();
1299 netCap.mLinkUpBandwidthKbps = in.readInt();
1300 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001301 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001302 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001303 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1304 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001305 return netCap;
1306 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001307 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001308 public NetworkCapabilities[] newArray(int size) {
1309 return new NetworkCapabilities[size];
1310 }
1311 };
1312
Wink Saville4e2dea72014-09-20 11:04:03 -07001313 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001314 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001315 final StringBuilder sb = new StringBuilder("[");
1316 if (0 != mTransportTypes) {
1317 sb.append(" Transports: ");
1318 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1319 NetworkCapabilities::transportNameOf, "|");
1320 }
1321 if (0 != mNetworkCapabilities) {
1322 sb.append(" Capabilities: ");
1323 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1324 NetworkCapabilities::capabilityNameOf, "&");
1325 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001326 if (0 != mNetworkCapabilities) {
1327 sb.append(" Unwanted: ");
1328 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1329 NetworkCapabilities::capabilityNameOf, "&");
1330 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001331 if (mLinkUpBandwidthKbps > 0) {
1332 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1333 }
1334 if (mLinkDownBandwidthKbps > 0) {
1335 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1336 }
1337 if (mNetworkSpecifier != null) {
1338 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1339 }
1340 if (hasSignalStrength()) {
1341 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001342 }
1343
Chalard Jean07ace0f2018-02-26 19:00:45 +09001344 if (null != mUids) {
1345 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1346 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1347 } else {
1348 sb.append(" Uids: <").append(mUids).append(">");
1349 }
1350 }
1351 if (mEstablishingVpnAppUid != INVALID_UID) {
1352 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1353 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001354
Chalard Jean07ace0f2018-02-26 19:00:45 +09001355 sb.append("]");
1356 return sb.toString();
1357 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001358
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001359
Chalard Jean07ace0f2018-02-26 19:00:45 +09001360 private interface NameOf {
1361 String nameOf(int value);
1362 }
1363 /**
1364 * @hide
1365 */
1366 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1367 long bitMask, NameOf nameFetcher, String separator) {
1368 int bitPos = 0;
1369 boolean firstElementAdded = false;
1370 while (bitMask != 0) {
1371 if ((bitMask & 1) != 0) {
1372 if (firstElementAdded) {
1373 sb.append(separator);
1374 } else {
1375 firstElementAdded = true;
1376 }
1377 sb.append(nameFetcher.nameOf(bitPos));
1378 }
1379 bitMask >>= 1;
1380 ++bitPos;
1381 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001382 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001383
1384 /**
1385 * @hide
1386 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001387 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001388 StringJoiner joiner = new StringJoiner("|");
1389 if (capabilities != null) {
1390 for (int c : capabilities) {
1391 joiner.add(capabilityNameOf(c));
1392 }
1393 }
1394 return joiner.toString();
1395 }
1396
1397 /**
1398 * @hide
1399 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001400 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001401 switch (capability) {
1402 case NET_CAPABILITY_MMS: return "MMS";
1403 case NET_CAPABILITY_SUPL: return "SUPL";
1404 case NET_CAPABILITY_DUN: return "DUN";
1405 case NET_CAPABILITY_FOTA: return "FOTA";
1406 case NET_CAPABILITY_IMS: return "IMS";
1407 case NET_CAPABILITY_CBS: return "CBS";
1408 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1409 case NET_CAPABILITY_IA: return "IA";
1410 case NET_CAPABILITY_RCS: return "RCS";
1411 case NET_CAPABILITY_XCAP: return "XCAP";
1412 case NET_CAPABILITY_EIMS: return "EIMS";
1413 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1414 case NET_CAPABILITY_INTERNET: return "INTERNET";
1415 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1416 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1417 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1418 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1419 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001420 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001421 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001422 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001423 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001424 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001425 default: return Integer.toString(capability);
1426 }
1427 }
1428
1429 /**
1430 * @hide
1431 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001432 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001433 StringJoiner joiner = new StringJoiner("|");
1434 if (types != null) {
1435 for (int t : types) {
1436 joiner.add(transportNameOf(t));
1437 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001438 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001439 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001440 }
1441
1442 /**
1443 * @hide
1444 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001445 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001446 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001447 return "UNKNOWN";
1448 }
1449 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001450 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001451
Jeff Sharkeyde570312017-10-24 21:25:50 -06001452 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001453 Preconditions.checkArgument(
1454 isValidTransport(transport), "Invalid TransportType " + transport);
1455 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001456
1457 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1458 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1459 }
1460
1461 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1462 Preconditions.checkArgument(isValidCapability(capability),
1463 "NetworkCapability " + capability + "out of range");
1464 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001465}