blob: 2fc8c6857b750a1e56c8fa84be115f98c03ac003 [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>
384 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
385 * {@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
Robert Greenwalt1448f052014-04-08 13:41:39 -0700473 private void combineNetCapabilities(NetworkCapabilities nc) {
474 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800475 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700476 }
477
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900478 /**
479 * Convenience function that returns a human-readable description of the first mutable
480 * capability we find. Used to present an error message to apps that request mutable
481 * capabilities.
482 *
483 * @hide
484 */
485 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800486 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
487 & NON_REQUESTABLE_CAPABILITIES;
488
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900489 if (nonRequestable != 0) {
490 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900491 }
492 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900493 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900494 return null;
495 }
496
497 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800498 long requestedCapabilities = mNetworkCapabilities;
499 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
500 long providedCapabilities = nc.mNetworkCapabilities;
501
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900502 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800503 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
504 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900505 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800506 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
507 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700508 }
509
Robert Greenwalt06314e42014-10-29 14:04:06 -0700510 /** @hide */
511 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800512 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
513 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700514 }
515
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900516 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
517 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800518 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
519 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
520 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900521 }
522
Robert Greenwalt1448f052014-04-08 13:41:39 -0700523 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400524 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
525 * typically provided by restricted networks.
526 *
527 * TODO: consider:
528 * - Renaming it to guessRestrictedCapability and make it set the
529 * restricted capability bit in addition to clearing it.
530 * @hide
531 */
532 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700533 // Verify there aren't any unrestricted capabilities. If there are we say
534 // the whole thing is unrestricted.
535 final boolean hasUnrestrictedCapabilities =
536 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
537
538 // Must have at least some restricted capabilities.
539 final boolean hasRestrictedCapabilities =
540 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
541
542 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400543 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400544 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400545 }
546
547 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700548 * Representing the transport type. Apps should generally not care about transport. A
549 * request for a fast internet connection could be satisfied by a number of different
550 * transports. If any are specified here it will be satisfied a Network that matches
551 * any of them. If a caller doesn't care about the transport it should not specify any.
552 */
553 private long mTransportTypes;
554
Jeff Sharkeyde570312017-10-24 21:25:50 -0600555 /** @hide */
556 @Retention(RetentionPolicy.SOURCE)
557 @IntDef(prefix = { "TRANSPORT_" }, value = {
558 TRANSPORT_CELLULAR,
559 TRANSPORT_WIFI,
560 TRANSPORT_BLUETOOTH,
561 TRANSPORT_ETHERNET,
562 TRANSPORT_VPN,
563 TRANSPORT_WIFI_AWARE,
564 TRANSPORT_LOWPAN,
565 })
566 public @interface Transport { }
567
Robert Greenwalt1448f052014-04-08 13:41:39 -0700568 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700569 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700570 */
571 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700572
573 /**
574 * Indicates this network uses a Wi-Fi transport.
575 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700576 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700577
578 /**
579 * Indicates this network uses a Bluetooth transport.
580 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700581 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700582
583 /**
584 * Indicates this network uses an Ethernet transport.
585 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700586 public static final int TRANSPORT_ETHERNET = 3;
587
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400588 /**
589 * Indicates this network uses a VPN transport.
590 */
591 public static final int TRANSPORT_VPN = 4;
592
Etan Cohen305ea282016-06-20 09:27:12 -0700593 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700594 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700595 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700596 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700597
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700598 /**
599 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700600 */
601 public static final int TRANSPORT_LOWPAN = 6;
602
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900603 /** @hide */
604 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
605 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700606 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700607
Hugo Benichi16f0a942017-06-20 14:07:59 +0900608 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600609 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900610 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
611 }
612
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900613 private static final String[] TRANSPORT_NAMES = {
614 "CELLULAR",
615 "WIFI",
616 "BLUETOOTH",
617 "ETHERNET",
618 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700619 "WIFI_AWARE",
620 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900621 };
622
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700623 /**
624 * Adds the given transport type to this {@code NetworkCapability} instance.
625 * Multiple transports may be applied sequentially. Note that when searching
626 * for a network to satisfy a request, any listed in the request will satisfy the request.
627 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
628 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
629 * to be selected. This is logically different than
630 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
631 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600632 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900633 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700634 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700635 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600636 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900637 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700638 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700639 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700640 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700641 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700642
643 /**
644 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
645 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600646 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900647 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700648 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700649 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600650 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900651 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700652 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700653 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700654 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700655 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700656
657 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600658 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
659 * instance.
660 *
661 * @hide
662 */
663 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
664 if (value) {
665 addTransportType(transportType);
666 } else {
667 removeTransportType(transportType);
668 }
669 return this;
670 }
671
672 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700673 * Gets all the transports set on this {@code NetworkCapability} instance.
674 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600675 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700676 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700677 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600678 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900679 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700680 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700681
682 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600683 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700684 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600685 *
686 * @hide
687 */
688 public void setTransportTypes(@Transport int[] transportTypes) {
689 mTransportTypes = BitUtils.packBits(transportTypes);
690 }
691
692 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700693 * Tests for the presence of a transport on this instance.
694 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600695 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700696 * @return {@code true} if set on this instance.
697 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600698 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900699 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700700 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700701
702 private void combineTransportTypes(NetworkCapabilities nc) {
703 this.mTransportTypes |= nc.mTransportTypes;
704 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900705
Robert Greenwalt1448f052014-04-08 13:41:39 -0700706 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
707 return ((this.mTransportTypes == 0) ||
708 ((this.mTransportTypes & nc.mTransportTypes) != 0));
709 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900710
Robert Greenwalt06314e42014-10-29 14:04:06 -0700711 /** @hide */
712 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700713 return (nc.mTransportTypes == this.mTransportTypes);
714 }
715
716 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900717 * UID of the app that manages this network, or INVALID_UID if none/unknown.
718 *
719 * This field keeps track of the UID of the app that created this network and is in charge
720 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
721 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
722 * to create networks.
723 *
724 * Because this field is only used in the services side (and to avoid apps being able to
725 * set this to whatever they want), this field is not parcelled and will not be conserved
726 * across the IPC boundary.
727 * @hide
728 */
729 private int mEstablishingVpnAppUid = INVALID_UID;
730
731 /**
732 * Set the UID of the managing app.
733 * @hide
734 */
735 public void setEstablishingVpnAppUid(final int uid) {
736 mEstablishingVpnAppUid = uid;
737 }
738
739 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600740 * Value indicating that link bandwidth is unspecified.
741 * @hide
742 */
743 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
744
745 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700746 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
747 * for the first hop on the given transport. It is not measured, but may take into account
748 * link parameters (Radio technology, allocated channels, etc).
749 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600750 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
751 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700752
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700753 /**
754 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
755 * the estimated first hop transport bandwidth.
756 * <p>
757 * Note that when used to request a network, this specifies the minimum acceptable.
758 * When received as the state of an existing network this specifies the typical
759 * first hop bandwidth expected. This is never measured, but rather is inferred
760 * from technology type and other link parameters. It could be used to differentiate
761 * between very slow 1xRTT cellular links and other faster networks or even between
762 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
763 * fast backhauls and slow backhauls.
764 *
765 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700766 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700767 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600768 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700769 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600770 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700771 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700772
773 /**
774 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
775 * the estimated first hop transport bandwidth.
776 *
777 * @return The estimated first hop upstream (device to network) bandwidth.
778 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700779 public int getLinkUpstreamBandwidthKbps() {
780 return mLinkUpBandwidthKbps;
781 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700782
783 /**
784 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
785 * the estimated first hop transport bandwidth.
786 * <p>
787 * Note that when used to request a network, this specifies the minimum acceptable.
788 * When received as the state of an existing network this specifies the typical
789 * first hop bandwidth expected. This is never measured, but rather is inferred
790 * from technology type and other link parameters. It could be used to differentiate
791 * between very slow 1xRTT cellular links and other faster networks or even between
792 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
793 * fast backhauls and slow backhauls.
794 *
795 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700796 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700797 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600798 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700799 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600800 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700801 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700802
803 /**
804 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
805 * the estimated first hop transport bandwidth.
806 *
807 * @return The estimated first hop downstream (network to device) bandwidth.
808 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700809 public int getLinkDownstreamBandwidthKbps() {
810 return mLinkDownBandwidthKbps;
811 }
812
813 private void combineLinkBandwidths(NetworkCapabilities nc) {
814 this.mLinkUpBandwidthKbps =
815 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
816 this.mLinkDownBandwidthKbps =
817 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
818 }
819 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
820 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
821 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
822 }
823 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
824 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
825 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
826 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600827 /** @hide */
828 public static int minBandwidth(int a, int b) {
829 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
830 return b;
831 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
832 return a;
833 } else {
834 return Math.min(a, b);
835 }
836 }
837 /** @hide */
838 public static int maxBandwidth(int a, int b) {
839 return Math.max(a, b);
840 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700841
Etan Cohena7434272017-04-03 12:17:51 -0700842 private NetworkSpecifier mNetworkSpecifier = null;
843
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700844 /**
845 * Sets the optional bearer specific network specifier.
846 * This has no meaning if a single transport is also not specified, so calling
847 * this without a single transport set will generate an exception, as will
848 * subsequently adding or removing transports after this is set.
849 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700850 *
Etan Cohena7434272017-04-03 12:17:51 -0700851 * @param networkSpecifier A concrete, parcelable framework class that extends
852 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900853 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700854 * @hide
855 */
Etan Cohena7434272017-04-03 12:17:51 -0700856 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
857 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700858 throw new IllegalStateException("Must have a single transport specified to use " +
859 "setNetworkSpecifier");
860 }
Etan Cohena7434272017-04-03 12:17:51 -0700861
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700862 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700863
Pierre Imaic8419a82016-03-22 17:54:54 +0900864 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700865 }
866
867 /**
868 * Gets the optional bearer specific network specifier.
869 *
Etan Cohena7434272017-04-03 12:17:51 -0700870 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
871 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700872 * @hide
873 */
Etan Cohena7434272017-04-03 12:17:51 -0700874 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700875 return mNetworkSpecifier;
876 }
877
878 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700879 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700880 throw new IllegalStateException("Can't combine two networkSpecifiers");
881 }
Etan Cohena7434272017-04-03 12:17:51 -0700882 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700883 }
Etan Cohena7434272017-04-03 12:17:51 -0700884
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700885 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700886 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
887 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700888 }
Etan Cohena7434272017-04-03 12:17:51 -0700889
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700890 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700891 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700892 }
893
Robert Greenwalt1448f052014-04-08 13:41:39 -0700894 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900895 * Magic value that indicates no signal strength provided. A request specifying this value is
896 * always satisfied.
897 *
898 * @hide
899 */
900 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
901
902 /**
903 * Signal strength. This is a signed integer, and higher values indicate better signal.
904 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
905 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700906 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900907
908 /**
909 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
910 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
911 * reported by WifiManager.
912 * <p>
913 * Note that when used to register a network callback, this specifies the minimum acceptable
914 * signal strength. When received as the state of an existing network it specifies the current
915 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
916 * effect when requesting a callback.
917 *
918 * @param signalStrength the bearer-specific signal strength.
919 * @hide
920 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600921 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900922 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600923 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900924 }
925
926 /**
927 * Returns {@code true} if this object specifies a signal strength.
928 *
929 * @hide
930 */
931 public boolean hasSignalStrength() {
932 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
933 }
934
935 /**
936 * Retrieves the signal strength.
937 *
938 * @return The bearer-specific signal strength.
939 * @hide
940 */
941 public int getSignalStrength() {
942 return mSignalStrength;
943 }
944
945 private void combineSignalStrength(NetworkCapabilities nc) {
946 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
947 }
948
949 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
950 return this.mSignalStrength <= nc.mSignalStrength;
951 }
952
953 private boolean equalsSignalStrength(NetworkCapabilities nc) {
954 return this.mSignalStrength == nc.mSignalStrength;
955 }
956
957 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900958 * List of UIDs this network applies to. No restriction if null.
959 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900960 * For networks, mUids represent the list of network this applies to, and null means this
961 * network applies to all UIDs.
962 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
963 * must be included in a network so that they match. As an exception to the general rule,
964 * a null mUids field for requests mean "no requirements" rather than what the general rule
965 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
966 * of this API expect in practice. A network that must match all UIDs can still be
967 * expressed with a set ranging the entire set of possible UIDs.
968 * <p>
969 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900970 * the UIDs in this list, and it is their default network. Apps in this list that wish to
971 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
972 * member is null, then the network is not restricted by app UID. If it's an empty list, then
973 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900974 * As a special exception, the app managing this network (as identified by its UID stored in
975 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
976 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
977 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900978 * <p>
979 * Please note that in principle a single app can be associated with multiple UIDs because
980 * each app will have a different UID when it's run as a different (macro-)user. A single
981 * macro user can only have a single active VPN app at any given time however.
982 * <p>
983 * Also please be aware this class does not try to enforce any normalization on this. Callers
984 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
985 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
986 * their own (like requiring sortedness or no overlap) they need to enforce it
987 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
988 * or overlapping ranges are present.
989 *
990 * @hide
991 */
Chalard Jeancf8c3fe2018-01-25 09:41:51 +0900992 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900993
994 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900995 * Convenience method to set the UIDs this network applies to to a single UID.
996 * @hide
997 */
998 public NetworkCapabilities setSingleUid(int uid) {
999 final ArraySet<UidRange> identity = new ArraySet<>(1);
1000 identity.add(new UidRange(uid, uid));
1001 setUids(identity);
1002 return this;
1003 }
1004
1005 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001006 * Set the list of UIDs this network applies to.
1007 * This makes a copy of the set so that callers can't modify it after the call.
1008 * @hide
1009 */
1010 public NetworkCapabilities setUids(Set<UidRange> uids) {
1011 if (null == uids) {
1012 mUids = null;
1013 } else {
1014 mUids = new ArraySet<>(uids);
1015 }
1016 return this;
1017 }
1018
1019 /**
1020 * Get the list of UIDs this network applies to.
1021 * This returns a copy of the set so that callers can't modify the original object.
1022 * @hide
1023 */
1024 public Set<UidRange> getUids() {
1025 return null == mUids ? null : new ArraySet<>(mUids);
1026 }
1027
1028 /**
1029 * Test whether this network applies to this UID.
1030 * @hide
1031 */
1032 public boolean appliesToUid(int uid) {
1033 if (null == mUids) return true;
1034 for (UidRange range : mUids) {
1035 if (range.contains(uid)) {
1036 return true;
1037 }
1038 }
1039 return false;
1040 }
1041
1042 /**
1043 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
1044 * <p>
1045 * This test only checks whether equal range objects are in both sets. It will
1046 * return false if the ranges are not exactly the same, even if the covered UIDs
1047 * are for an equivalent result.
1048 * <p>
1049 * Note that this method is not very optimized, which is fine as long as it's not used very
1050 * often.
1051 * <p>
1052 * nc is assumed nonnull.
1053 *
1054 * @hide
1055 */
1056 @VisibleForTesting
1057 public boolean equalsUids(NetworkCapabilities nc) {
1058 Set<UidRange> comparedUids = nc.mUids;
1059 if (null == comparedUids) return null == mUids;
1060 if (null == mUids) return false;
1061 // Make a copy so it can be mutated to check that all ranges in mUids
1062 // also are in uids.
1063 final Set<UidRange> uids = new ArraySet<>(mUids);
1064 for (UidRange range : comparedUids) {
1065 if (!uids.contains(range)) {
1066 return false;
1067 }
1068 uids.remove(range);
1069 }
1070 return uids.isEmpty();
1071 }
1072
1073 /**
1074 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1075 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001076 * This method is called on the NetworkCapabilities embedded in a request with the
1077 * capabilities of an available network. It checks whether all the UIDs from this listen
1078 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1079 * in the passed nc (representing the UIDs that this network is available to).
1080 * <p>
1081 * As a special exception, the UID that created the passed network (as represented by its
1082 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1083 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1084 * can see its own network when it listens for it.
1085 * <p>
1086 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001087 * @see #appliesToUid
1088 * @hide
1089 */
1090 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001091 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001092 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001093 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001094 if (!nc.appliesToUidRange(requiredRange)) {
1095 return false;
1096 }
1097 }
1098 return true;
1099 }
1100
1101 /**
1102 * Returns whether this network applies to the passed ranges.
1103 * This assumes that to apply, the passed range has to be entirely contained
1104 * within one of the ranges this network applies to. If the ranges are not normalized,
1105 * this method may return false even though all required UIDs are covered because no
1106 * single range contained them all.
1107 * @hide
1108 */
1109 @VisibleForTesting
1110 public boolean appliesToUidRange(UidRange requiredRange) {
1111 if (null == mUids) return true;
1112 for (UidRange uidRange : mUids) {
1113 if (uidRange.containsRange(requiredRange)) {
1114 return true;
1115 }
1116 }
1117 return false;
1118 }
1119
1120 /**
1121 * Combine the UIDs this network currently applies to with the UIDs the passed
1122 * NetworkCapabilities apply to.
1123 * nc is assumed nonnull.
1124 */
1125 private void combineUids(NetworkCapabilities nc) {
1126 if (null == nc.mUids || null == mUids) {
1127 mUids = null;
1128 return;
1129 }
1130 mUids.addAll(nc.mUids);
1131 }
1132
1133 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001134 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001135 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001136 */
1137 public void combineCapabilities(NetworkCapabilities nc) {
1138 combineNetCapabilities(nc);
1139 combineTransportTypes(nc);
1140 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001141 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001142 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001143 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001144 }
1145
1146 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001147 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1148 *
1149 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1150 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1151 * bandwidth, signal strength, or validation / captive portal status.
1152 *
1153 * @hide
1154 */
1155 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001156 return (nc != null
1157 && satisfiedByNetCapabilities(nc, onlyImmutable)
1158 && satisfiedByTransportTypes(nc)
1159 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1160 && satisfiedBySpecifier(nc)
1161 && (onlyImmutable || satisfiedBySignalStrength(nc))
1162 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001163 }
1164
1165 /**
1166 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1167 *
1168 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1169 *
1170 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001171 */
1172 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001173 return satisfiedByNetworkCapabilities(nc, false);
1174 }
1175
1176 /**
1177 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1178 *
1179 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1180 *
1181 * @hide
1182 */
1183 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1184 return satisfiedByNetworkCapabilities(nc, true);
1185 }
1186
1187 /**
1188 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001189 * {@code NetworkCapabilities} and return a String describing any difference.
1190 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001191 *
1192 * @hide
1193 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001194 public String describeImmutableDifferences(NetworkCapabilities that) {
1195 if (that == null) {
1196 return "other NetworkCapabilities was null";
1197 }
1198
1199 StringJoiner joiner = new StringJoiner(", ");
1200
Hugo Benichieae7a222017-07-25 11:40:56 +09001201 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1202 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001203 // Ignore DUN being added or removed. http://b/65257223.
1204 final long mask = ~MUTABLE_CAPABILITIES
1205 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001206 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1207 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1208 if (oldImmutableCapabilities != newImmutableCapabilities) {
1209 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1210 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1211 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1212 }
1213
1214 if (!equalsSpecifier(that)) {
1215 NetworkSpecifier before = this.getNetworkSpecifier();
1216 NetworkSpecifier after = that.getNetworkSpecifier();
1217 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1218 }
1219
1220 if (!equalsTransportTypes(that)) {
1221 String before = transportNamesOf(this.getTransportTypes());
1222 String after = transportNamesOf(that.getTransportTypes());
1223 joiner.add(String.format("transports changed: %s -> %s", before, after));
1224 }
1225
1226 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001227 }
1228
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001229 /**
1230 * Checks that our requestable capabilities are the same as those of the given
1231 * {@code NetworkCapabilities}.
1232 *
1233 * @hide
1234 */
1235 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1236 if (nc == null) return false;
1237 return (equalsNetCapabilitiesRequestable(nc) &&
1238 equalsTransportTypes(nc) &&
1239 equalsSpecifier(nc));
1240 }
1241
Robert Greenwalt1448f052014-04-08 13:41:39 -07001242 @Override
1243 public boolean equals(Object obj) {
1244 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001245 NetworkCapabilities that = (NetworkCapabilities) obj;
1246 return (equalsNetCapabilities(that)
1247 && equalsTransportTypes(that)
1248 && equalsLinkBandwidths(that)
1249 && equalsSignalStrength(that)
1250 && equalsSpecifier(that)
1251 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001252 }
1253
1254 @Override
1255 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001256 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001257 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001258 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1259 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1260 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1261 + ((int) (mTransportTypes >> 32) * 13)
1262 + (mLinkUpBandwidthKbps * 17)
1263 + (mLinkDownBandwidthKbps * 19)
1264 + Objects.hashCode(mNetworkSpecifier) * 23
1265 + (mSignalStrength * 29)
1266 + Objects.hashCode(mUids) * 31;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001267 }
1268
Wink Saville4e2dea72014-09-20 11:04:03 -07001269 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001270 public int describeContents() {
1271 return 0;
1272 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001273 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001274 public void writeToParcel(Parcel dest, int flags) {
1275 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001276 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001277 dest.writeLong(mTransportTypes);
1278 dest.writeInt(mLinkUpBandwidthKbps);
1279 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001280 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001281 dest.writeInt(mSignalStrength);
Chalard Jeancf8c3fe2018-01-25 09:41:51 +09001282 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001283 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001284
Robert Greenwalt1448f052014-04-08 13:41:39 -07001285 public static final Creator<NetworkCapabilities> CREATOR =
1286 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001287 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001288 public NetworkCapabilities createFromParcel(Parcel in) {
1289 NetworkCapabilities netCap = new NetworkCapabilities();
1290
1291 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001292 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001293 netCap.mTransportTypes = in.readLong();
1294 netCap.mLinkUpBandwidthKbps = in.readInt();
1295 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001296 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001297 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001298 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1299 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001300 return netCap;
1301 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001302 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001303 public NetworkCapabilities[] newArray(int size) {
1304 return new NetworkCapabilities[size];
1305 }
1306 };
1307
Wink Saville4e2dea72014-09-20 11:04:03 -07001308 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001309 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001310 final StringBuilder sb = new StringBuilder("[");
1311 if (0 != mTransportTypes) {
1312 sb.append(" Transports: ");
1313 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1314 NetworkCapabilities::transportNameOf, "|");
1315 }
1316 if (0 != mNetworkCapabilities) {
1317 sb.append(" Capabilities: ");
1318 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1319 NetworkCapabilities::capabilityNameOf, "&");
1320 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001321 if (0 != mNetworkCapabilities) {
1322 sb.append(" Unwanted: ");
1323 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1324 NetworkCapabilities::capabilityNameOf, "&");
1325 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001326 if (mLinkUpBandwidthKbps > 0) {
1327 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1328 }
1329 if (mLinkDownBandwidthKbps > 0) {
1330 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1331 }
1332 if (mNetworkSpecifier != null) {
1333 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1334 }
1335 if (hasSignalStrength()) {
1336 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001337 }
1338
Chalard Jean07ace0f2018-02-26 19:00:45 +09001339 if (null != mUids) {
1340 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1341 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1342 } else {
1343 sb.append(" Uids: <").append(mUids).append(">");
1344 }
1345 }
1346 if (mEstablishingVpnAppUid != INVALID_UID) {
1347 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1348 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001349
Chalard Jean07ace0f2018-02-26 19:00:45 +09001350 sb.append("]");
1351 return sb.toString();
1352 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001353
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001354
Chalard Jean07ace0f2018-02-26 19:00:45 +09001355 private interface NameOf {
1356 String nameOf(int value);
1357 }
1358 /**
1359 * @hide
1360 */
1361 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1362 long bitMask, NameOf nameFetcher, String separator) {
1363 int bitPos = 0;
1364 boolean firstElementAdded = false;
1365 while (bitMask != 0) {
1366 if ((bitMask & 1) != 0) {
1367 if (firstElementAdded) {
1368 sb.append(separator);
1369 } else {
1370 firstElementAdded = true;
1371 }
1372 sb.append(nameFetcher.nameOf(bitPos));
1373 }
1374 bitMask >>= 1;
1375 ++bitPos;
1376 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001377 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001378
1379 /**
1380 * @hide
1381 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001382 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001383 StringJoiner joiner = new StringJoiner("|");
1384 if (capabilities != null) {
1385 for (int c : capabilities) {
1386 joiner.add(capabilityNameOf(c));
1387 }
1388 }
1389 return joiner.toString();
1390 }
1391
1392 /**
1393 * @hide
1394 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001395 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001396 switch (capability) {
1397 case NET_CAPABILITY_MMS: return "MMS";
1398 case NET_CAPABILITY_SUPL: return "SUPL";
1399 case NET_CAPABILITY_DUN: return "DUN";
1400 case NET_CAPABILITY_FOTA: return "FOTA";
1401 case NET_CAPABILITY_IMS: return "IMS";
1402 case NET_CAPABILITY_CBS: return "CBS";
1403 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1404 case NET_CAPABILITY_IA: return "IA";
1405 case NET_CAPABILITY_RCS: return "RCS";
1406 case NET_CAPABILITY_XCAP: return "XCAP";
1407 case NET_CAPABILITY_EIMS: return "EIMS";
1408 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1409 case NET_CAPABILITY_INTERNET: return "INTERNET";
1410 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1411 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1412 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1413 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1414 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001415 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001416 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001417 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001418 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001419 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001420 default: return Integer.toString(capability);
1421 }
1422 }
1423
1424 /**
1425 * @hide
1426 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001427 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001428 StringJoiner joiner = new StringJoiner("|");
1429 if (types != null) {
1430 for (int t : types) {
1431 joiner.add(transportNameOf(t));
1432 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001433 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001434 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001435 }
1436
1437 /**
1438 * @hide
1439 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001440 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001441 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001442 return "UNKNOWN";
1443 }
1444 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001445 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001446
Jeff Sharkeyde570312017-10-24 21:25:50 -06001447 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001448 Preconditions.checkArgument(
1449 isValidTransport(transport), "Invalid TransportType " + transport);
1450 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001451
1452 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1453 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1454 }
1455
1456 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1457 Preconditions.checkArgument(isValidCapability(capability),
1458 "NetworkCapability " + capability + "out of range");
1459 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001460}