blob: 0bdfca7f5025dbbad23d90a42835f1d86b5a9714 [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;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070020import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060021import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010022import android.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060023import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010024import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070025import android.os.Parcel;
26import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090027import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080028import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070029
30import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090031import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090032import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070033
Jeff Sharkeyde570312017-10-24 21:25:50 -060034import java.lang.annotation.Retention;
35import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070036import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090037import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090038import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070039
40/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070041 * Representation of the capabilities of an active network. Instances are
42 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060043 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
44 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060045 * <p>
46 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
47 * network selection. Rather than indicate a need for Wi-Fi because an
48 * application needs high bandwidth and risk obsolescence when a new, fast
49 * network appears (like LTE), the application should specify it needs high
50 * bandwidth. Similarly if an application needs an unmetered network for a bulk
51 * transfer it can specify that rather than assuming all cellular based
52 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070053 */
54public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070055 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090056 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070057
Robert Greenwalt7569f182014-06-08 16:42:59 -070058 /**
59 * @hide
60 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010061 @UnsupportedAppUsage
Robert Greenwalt01d004e2014-05-18 15:24:21 -070062 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090063 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090064 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070065 }
66
67 public NetworkCapabilities(NetworkCapabilities nc) {
68 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090069 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070070 }
71 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070072
73 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090074 * Completely clears the contents of this object, removing even the capabilities that are set
75 * by default when the object is constructed.
76 * @hide
77 */
78 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080079 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070080 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090081 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090082 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090083 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090084 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090085 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090086 }
87
88 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090089 * Set all contents of this object to the contents of a NetworkCapabilities.
90 * @hide
91 */
92 public void set(NetworkCapabilities nc) {
93 mNetworkCapabilities = nc.mNetworkCapabilities;
94 mTransportTypes = nc.mTransportTypes;
95 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
96 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
97 mNetworkSpecifier = nc.mNetworkSpecifier;
98 mSignalStrength = nc.mSignalStrength;
99 setUids(nc.mUids); // Will make the defensive copy
100 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
101 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
102 mSSID = nc.mSSID;
103 }
104
105 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700106 * Represents the network's capabilities. If any are specified they will be satisfied
107 * by any Network that matches all of them.
108 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100109 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900110 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700111
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800112 /**
113 * If any capabilities specified here they must not exist in the matching Network.
114 */
115 private long mUnwantedNetworkCapabilities;
116
Jeff Sharkeyde570312017-10-24 21:25:50 -0600117 /** @hide */
118 @Retention(RetentionPolicy.SOURCE)
119 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
120 NET_CAPABILITY_MMS,
121 NET_CAPABILITY_SUPL,
122 NET_CAPABILITY_DUN,
123 NET_CAPABILITY_FOTA,
124 NET_CAPABILITY_IMS,
125 NET_CAPABILITY_CBS,
126 NET_CAPABILITY_WIFI_P2P,
127 NET_CAPABILITY_IA,
128 NET_CAPABILITY_RCS,
129 NET_CAPABILITY_XCAP,
130 NET_CAPABILITY_EIMS,
131 NET_CAPABILITY_NOT_METERED,
132 NET_CAPABILITY_INTERNET,
133 NET_CAPABILITY_NOT_RESTRICTED,
134 NET_CAPABILITY_TRUSTED,
135 NET_CAPABILITY_NOT_VPN,
136 NET_CAPABILITY_VALIDATED,
137 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600138 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600139 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900140 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900141 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800142 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600143 })
144 public @interface NetCapability { }
145
Robert Greenwalt1448f052014-04-08 13:41:39 -0700146 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700147 * Indicates this is a network that has the ability to reach the
148 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 */
150 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700151
152 /**
153 * Indicates this is a network that has the ability to reach the carrier's
154 * SUPL server, used to retrieve GPS information.
155 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700156 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700157
158 /**
159 * Indicates this is a network that has the ability to reach the carrier's
160 * DUN or tethering gateway.
161 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700162 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700163
164 /**
165 * Indicates this is a network that has the ability to reach the carrier's
166 * FOTA portal, used for over the air updates.
167 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700168 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700169
170 /**
171 * Indicates this is a network that has the ability to reach the carrier's
172 * IMS servers, used for network registration and signaling.
173 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700174 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700175
176 /**
177 * Indicates this is a network that has the ability to reach the carrier's
178 * CBS servers, used for carrier specific services.
179 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach a Wi-Fi direct
184 * peer.
185 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Indicates this is a network that has the ability to reach a carrier's
190 * Initial Attach servers.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates this is a network that has the ability to reach a carrier's
196 * RCS servers, used for Rich Communication Services.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates this is a network that has the ability to reach a carrier's
202 * XCAP servers, used for configuration and control.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700208 * Emergency IMS servers or other services, used for network signaling
209 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700210 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700211 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700212
213 /**
214 * Indicates that this network is unmetered.
215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700217
218 /**
219 * Indicates that this network should be able to reach the internet.
220 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700221 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700222
223 /**
224 * Indicates that this network is available for general use. If this is not set
225 * applications should not attempt to communicate on this network. Note that this
226 * is simply informative and not enforcement - enforcement is handled via other means.
227 * Set by default.
228 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700229 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
230
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700231 /**
232 * Indicates that the user has indicated implicit trust of this network. This
233 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
234 * BT device or a wifi the user asked to connect to. Untrusted networks
235 * are probably limited to unknown wifi AP. Set by default.
236 */
237 public static final int NET_CAPABILITY_TRUSTED = 14;
238
Paul Jensen76b610a2015-03-18 09:33:07 -0400239 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400240 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400241 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400242 */
243 public static final int NET_CAPABILITY_NOT_VPN = 15;
244
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900245 /**
246 * Indicates that connectivity on this network was successfully validated. For example, for a
247 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
248 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900249 */
250 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700251
Paul Jensen3d194ea2015-06-16 14:27:36 -0400252 /**
253 * Indicates that this network was found to have a captive portal in place last time it was
254 * probed.
255 */
256 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
257
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900258 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600259 * Indicates that this network is not roaming.
260 */
261 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
262
263 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900264 * Indicates that this network is available for use by apps, and not a network that is being
265 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900266 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600267 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900268
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900269 /**
270 * Indicates that this network is not congested.
271 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600272 * When a network is congested, applications should defer network traffic
273 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900274 */
275 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
276
Chalard Jean804b8fb2018-01-30 22:41:41 +0900277 /**
278 * Indicates that this network is not currently suspended.
279 * <p>
280 * When a network is suspended, the network's IP addresses and any connections
281 * established on the network remain valid, but the network is temporarily unable
282 * to transfer data. This can happen, for example, if a cellular network experiences
283 * a temporary loss of signal, such as when driving through a tunnel, etc.
284 * A network with this capability is not suspended, so is expected to be able to
285 * transfer data.
286 */
287 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
288
Pavel Maltsev43403202018-01-30 17:19:44 -0800289 /**
290 * Indicates that traffic that goes through this network is paid by oem. For example,
291 * this network can be used by system apps to upload telemetry data.
292 * @hide
293 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700294 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800295 public static final int NET_CAPABILITY_OEM_PAID = 22;
296
Robert Greenwalt1448f052014-04-08 13:41:39 -0700297 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800298 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700299
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700300 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900301 * Network capabilities that are expected to be mutable, i.e., can change while a particular
302 * network is connected.
303 */
304 private static final long MUTABLE_CAPABILITIES =
305 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
306 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900307 (1 << NET_CAPABILITY_TRUSTED)
308 | (1 << NET_CAPABILITY_VALIDATED)
309 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
310 | (1 << NET_CAPABILITY_NOT_ROAMING)
311 | (1 << NET_CAPABILITY_FOREGROUND)
312 | (1 << NET_CAPABILITY_NOT_CONGESTED)
313 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900314
315 /**
316 * Network capabilities that are not allowed in NetworkRequests. This exists because the
317 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
318 * capability's presence cannot be known in advance. If such a capability is requested, then we
319 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
320 * get immediately torn down because they do not have the requested capability.
321 */
322 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900323 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900324
325 /**
326 * Capabilities that are set by default when the object is constructed.
327 */
328 private static final long DEFAULT_CAPABILITIES =
329 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
330 (1 << NET_CAPABILITY_TRUSTED) |
331 (1 << NET_CAPABILITY_NOT_VPN);
332
333 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400334 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800335 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400336 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700337 @VisibleForTesting
338 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400339 (1 << NET_CAPABILITY_CBS) |
340 (1 << NET_CAPABILITY_DUN) |
341 (1 << NET_CAPABILITY_EIMS) |
342 (1 << NET_CAPABILITY_FOTA) |
343 (1 << NET_CAPABILITY_IA) |
344 (1 << NET_CAPABILITY_IMS) |
345 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev4af91072018-03-07 14:33:22 -0800346 (1 << NET_CAPABILITY_XCAP);
347
348 /**
349 * Capabilities that force network to be restricted.
350 * {@see #maybeMarkCapabilitiesRestricted}.
351 */
352 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800353 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400354
355 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700356 * Capabilities that suggest that a network is unrestricted.
357 * {@see #maybeMarkCapabilitiesRestricted}.
358 */
359 @VisibleForTesting
360 /* package */ static final long UNRESTRICTED_CAPABILITIES =
361 (1 << NET_CAPABILITY_INTERNET) |
362 (1 << NET_CAPABILITY_MMS) |
363 (1 << NET_CAPABILITY_SUPL) |
364 (1 << NET_CAPABILITY_WIFI_P2P);
365
366 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700367 * Adds the given capability to this {@code NetworkCapability} instance.
368 * Multiple capabilities may be applied sequentially. Note that when searching
369 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800370 * <p>
371 * If the given capability was previously added to the list of unwanted capabilities
372 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700373 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600374 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900375 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700376 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700377 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100378 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600379 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800380 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700381 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800382 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700383 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700384 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700385
386 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800387 * Adds the given capability to the list of unwanted capabilities of this
388 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
389 * sequentially. Note that when searching for a network to satisfy a request, the network
390 * must not contain any capability from unwanted capability list.
391 * <p>
392 * If the capability was previously added to the list of required capabilities (for
393 * example, it was there by default or added using {@link #addCapability(int)} method), then
394 * it will be removed from the list of required capabilities as well.
395 *
396 * @see #addCapability(int)
397 * @hide
398 */
399 public void addUnwantedCapability(@NetCapability int capability) {
400 checkValidCapability(capability);
401 mUnwantedNetworkCapabilities |= 1 << capability;
402 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
403 }
404
405 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700406 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800407 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800408 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800409 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700410 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600411 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900412 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700413 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700414 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100415 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600416 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800417 checkValidCapability(capability);
418 final long mask = ~(1 << capability);
419 mNetworkCapabilities &= mask;
420 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700421 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700422 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700423
424 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600425 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
426 * instance.
427 *
428 * @hide
429 */
430 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
431 if (value) {
432 addCapability(capability);
433 } else {
434 removeCapability(capability);
435 }
436 return this;
437 }
438
439 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700440 * Gets all the capabilities set on this {@code NetworkCapability} instance.
441 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600442 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700443 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700444 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600445 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600446 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900447 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700448 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700449
450 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800451 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
452 *
453 * @return an array of unwanted capability values for this instance.
454 * @hide
455 */
456 public @NetCapability int[] getUnwantedCapabilities() {
457 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
458 }
459
460
461 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600462 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700463 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600464 *
465 * @hide
466 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800467 public void setCapabilities(@NetCapability int[] capabilities,
468 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600469 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800470 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600471 }
472
473 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800474 * @deprecated use {@link #setCapabilities(int[], int[])}
475 * @hide
476 */
477 @Deprecated
478 public void setCapabilities(@NetCapability int[] capabilities) {
479 setCapabilities(capabilities, new int[] {});
480 }
481
482 /**
483 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700484 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600485 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700486 * @return {@code true} if set on this instance.
487 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600488 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800489 return isValidCapability(capability)
490 && ((mNetworkCapabilities & (1 << capability)) != 0);
491 }
492
493 /** @hide */
494 public boolean hasUnwantedCapability(@NetCapability int capability) {
495 return isValidCapability(capability)
496 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700497 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700498
Pavel Maltseve18ef262018-03-07 11:13:04 -0800499 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700500 private void combineNetCapabilities(NetworkCapabilities nc) {
501 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800502 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700503 }
504
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900505 /**
506 * Convenience function that returns a human-readable description of the first mutable
507 * capability we find. Used to present an error message to apps that request mutable
508 * capabilities.
509 *
510 * @hide
511 */
512 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800513 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
514 & NON_REQUESTABLE_CAPABILITIES;
515
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900516 if (nonRequestable != 0) {
517 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900518 }
519 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900520 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900521 return null;
522 }
523
524 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800525 long requestedCapabilities = mNetworkCapabilities;
526 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
527 long providedCapabilities = nc.mNetworkCapabilities;
528
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900529 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800530 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
531 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900532 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800533 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
534 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700535 }
536
Robert Greenwalt06314e42014-10-29 14:04:06 -0700537 /** @hide */
538 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800539 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
540 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700541 }
542
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900543 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
544 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800545 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
546 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
547 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900548 }
549
Robert Greenwalt1448f052014-04-08 13:41:39 -0700550 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400551 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
552 * typically provided by restricted networks.
553 *
554 * TODO: consider:
555 * - Renaming it to guessRestrictedCapability and make it set the
556 * restricted capability bit in addition to clearing it.
557 * @hide
558 */
559 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800560 // Check if we have any capability that forces the network to be restricted.
561 final boolean forceRestrictedCapability =
562 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
563
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700564 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800565 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700566 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800567 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700568
569 // Must have at least some restricted capabilities.
570 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800571 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700572
Pavel Maltsev4af91072018-03-07 14:33:22 -0800573 if (forceRestrictedCapability
574 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400575 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400576 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400577 }
578
579 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700580 * Representing the transport type. Apps should generally not care about transport. A
581 * request for a fast internet connection could be satisfied by a number of different
582 * transports. If any are specified here it will be satisfied a Network that matches
583 * any of them. If a caller doesn't care about the transport it should not specify any.
584 */
585 private long mTransportTypes;
586
Jeff Sharkeyde570312017-10-24 21:25:50 -0600587 /** @hide */
588 @Retention(RetentionPolicy.SOURCE)
589 @IntDef(prefix = { "TRANSPORT_" }, value = {
590 TRANSPORT_CELLULAR,
591 TRANSPORT_WIFI,
592 TRANSPORT_BLUETOOTH,
593 TRANSPORT_ETHERNET,
594 TRANSPORT_VPN,
595 TRANSPORT_WIFI_AWARE,
596 TRANSPORT_LOWPAN,
597 })
598 public @interface Transport { }
599
Robert Greenwalt1448f052014-04-08 13:41:39 -0700600 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700601 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700602 */
603 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700604
605 /**
606 * Indicates this network uses a Wi-Fi transport.
607 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700608 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700609
610 /**
611 * Indicates this network uses a Bluetooth transport.
612 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700614
615 /**
616 * Indicates this network uses an Ethernet transport.
617 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700618 public static final int TRANSPORT_ETHERNET = 3;
619
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400620 /**
621 * Indicates this network uses a VPN transport.
622 */
623 public static final int TRANSPORT_VPN = 4;
624
Etan Cohen305ea282016-06-20 09:27:12 -0700625 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700626 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700627 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700628 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700629
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700630 /**
631 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700632 */
633 public static final int TRANSPORT_LOWPAN = 6;
634
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900635 /** @hide */
636 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
637 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700638 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639
Hugo Benichi16f0a942017-06-20 14:07:59 +0900640 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600641 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900642 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
643 }
644
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900645 private static final String[] TRANSPORT_NAMES = {
646 "CELLULAR",
647 "WIFI",
648 "BLUETOOTH",
649 "ETHERNET",
650 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700651 "WIFI_AWARE",
652 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900653 };
654
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700655 /**
656 * Adds the given transport type to this {@code NetworkCapability} instance.
657 * Multiple transports may be applied sequentially. Note that when searching
658 * for a network to satisfy a request, any listed in the request will satisfy the request.
659 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
660 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
661 * to be selected. This is logically different than
662 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
663 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600664 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900665 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700666 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700667 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100668 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600669 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900670 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700671 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700672 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700673 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700674 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700675
676 /**
677 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
678 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600679 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900680 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700681 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700682 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600683 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900684 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700685 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700686 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700687 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700688 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700689
690 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600691 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
692 * instance.
693 *
694 * @hide
695 */
696 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
697 if (value) {
698 addTransportType(transportType);
699 } else {
700 removeTransportType(transportType);
701 }
702 return this;
703 }
704
705 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700706 * Gets all the transports set on this {@code NetworkCapability} instance.
707 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600708 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700709 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700710 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600711 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600712 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900713 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700715
716 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600717 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700718 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600719 *
720 * @hide
721 */
722 public void setTransportTypes(@Transport int[] transportTypes) {
723 mTransportTypes = BitUtils.packBits(transportTypes);
724 }
725
726 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700727 * Tests for the presence of a transport on this instance.
728 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600729 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700730 * @return {@code true} if set on this instance.
731 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600732 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900733 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700734 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700735
736 private void combineTransportTypes(NetworkCapabilities nc) {
737 this.mTransportTypes |= nc.mTransportTypes;
738 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900739
Robert Greenwalt1448f052014-04-08 13:41:39 -0700740 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
741 return ((this.mTransportTypes == 0) ||
742 ((this.mTransportTypes & nc.mTransportTypes) != 0));
743 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900744
Robert Greenwalt06314e42014-10-29 14:04:06 -0700745 /** @hide */
746 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700747 return (nc.mTransportTypes == this.mTransportTypes);
748 }
749
750 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900751 * UID of the app that manages this network, or INVALID_UID if none/unknown.
752 *
753 * This field keeps track of the UID of the app that created this network and is in charge
754 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
755 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
756 * to create networks.
757 *
758 * Because this field is only used in the services side (and to avoid apps being able to
759 * set this to whatever they want), this field is not parcelled and will not be conserved
760 * across the IPC boundary.
761 * @hide
762 */
763 private int mEstablishingVpnAppUid = INVALID_UID;
764
765 /**
766 * Set the UID of the managing app.
767 * @hide
768 */
769 public void setEstablishingVpnAppUid(final int uid) {
770 mEstablishingVpnAppUid = uid;
771 }
772
773 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600774 * Value indicating that link bandwidth is unspecified.
775 * @hide
776 */
777 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
778
779 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
781 * for the first hop on the given transport. It is not measured, but may take into account
782 * link parameters (Radio technology, allocated channels, etc).
783 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600784 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
785 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700786
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700787 /**
788 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
789 * the estimated first hop transport bandwidth.
790 * <p>
791 * Note that when used to request a network, this specifies the minimum acceptable.
792 * When received as the state of an existing network this specifies the typical
793 * first hop bandwidth expected. This is never measured, but rather is inferred
794 * from technology type and other link parameters. It could be used to differentiate
795 * between very slow 1xRTT cellular links and other faster networks or even between
796 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
797 * fast backhauls and slow backhauls.
798 *
799 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700800 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700801 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600802 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700803 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600804 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700805 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700806
807 /**
808 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
809 * the estimated first hop transport bandwidth.
810 *
811 * @return The estimated first hop upstream (device to network) bandwidth.
812 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700813 public int getLinkUpstreamBandwidthKbps() {
814 return mLinkUpBandwidthKbps;
815 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700816
817 /**
818 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
819 * the estimated first hop transport bandwidth.
820 * <p>
821 * Note that when used to request a network, this specifies the minimum acceptable.
822 * When received as the state of an existing network this specifies the typical
823 * first hop bandwidth expected. This is never measured, but rather is inferred
824 * from technology type and other link parameters. It could be used to differentiate
825 * between very slow 1xRTT cellular links and other faster networks or even between
826 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
827 * fast backhauls and slow backhauls.
828 *
829 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700830 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700831 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600832 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700833 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600834 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700835 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700836
837 /**
838 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
839 * the estimated first hop transport bandwidth.
840 *
841 * @return The estimated first hop downstream (network to device) bandwidth.
842 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700843 public int getLinkDownstreamBandwidthKbps() {
844 return mLinkDownBandwidthKbps;
845 }
846
847 private void combineLinkBandwidths(NetworkCapabilities nc) {
848 this.mLinkUpBandwidthKbps =
849 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
850 this.mLinkDownBandwidthKbps =
851 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
852 }
853 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
854 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
855 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
856 }
857 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
858 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
859 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
860 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600861 /** @hide */
862 public static int minBandwidth(int a, int b) {
863 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
864 return b;
865 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
866 return a;
867 } else {
868 return Math.min(a, b);
869 }
870 }
871 /** @hide */
872 public static int maxBandwidth(int a, int b) {
873 return Math.max(a, b);
874 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700875
Etan Cohena7434272017-04-03 12:17:51 -0700876 private NetworkSpecifier mNetworkSpecifier = null;
877
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700878 /**
879 * Sets the optional bearer specific network specifier.
880 * This has no meaning if a single transport is also not specified, so calling
881 * this without a single transport set will generate an exception, as will
882 * subsequently adding or removing transports after this is set.
883 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700884 *
Etan Cohena7434272017-04-03 12:17:51 -0700885 * @param networkSpecifier A concrete, parcelable framework class that extends
886 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900887 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700888 * @hide
889 */
Etan Cohena7434272017-04-03 12:17:51 -0700890 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
891 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700892 throw new IllegalStateException("Must have a single transport specified to use " +
893 "setNetworkSpecifier");
894 }
Etan Cohena7434272017-04-03 12:17:51 -0700895
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700896 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700897
Pierre Imaic8419a82016-03-22 17:54:54 +0900898 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700899 }
900
901 /**
902 * Gets the optional bearer specific network specifier.
903 *
Etan Cohena7434272017-04-03 12:17:51 -0700904 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
905 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700906 * @hide
907 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100908 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Etan Cohena7434272017-04-03 12:17:51 -0700909 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700910 return mNetworkSpecifier;
911 }
912
913 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700914 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700915 throw new IllegalStateException("Can't combine two networkSpecifiers");
916 }
Etan Cohena7434272017-04-03 12:17:51 -0700917 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700918 }
Etan Cohena7434272017-04-03 12:17:51 -0700919
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700920 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700921 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
922 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700923 }
Etan Cohena7434272017-04-03 12:17:51 -0700924
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700925 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700926 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700927 }
928
Robert Greenwalt1448f052014-04-08 13:41:39 -0700929 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900930 * Magic value that indicates no signal strength provided. A request specifying this value is
931 * always satisfied.
932 *
933 * @hide
934 */
935 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
936
937 /**
938 * Signal strength. This is a signed integer, and higher values indicate better signal.
939 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
940 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100941 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700942 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900943
944 /**
945 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
946 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900947 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900948 * <p>
949 * Note that when used to register a network callback, this specifies the minimum acceptable
950 * signal strength. When received as the state of an existing network it specifies the current
951 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
952 * effect when requesting a callback.
953 *
954 * @param signalStrength the bearer-specific signal strength.
955 * @hide
956 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100957 @UnsupportedAppUsage
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600958 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900959 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600960 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900961 }
962
963 /**
964 * Returns {@code true} if this object specifies a signal strength.
965 *
966 * @hide
967 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100968 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900969 public boolean hasSignalStrength() {
970 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
971 }
972
973 /**
974 * Retrieves the signal strength.
975 *
976 * @return The bearer-specific signal strength.
977 * @hide
978 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100979 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900980 public int getSignalStrength() {
981 return mSignalStrength;
982 }
983
984 private void combineSignalStrength(NetworkCapabilities nc) {
985 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
986 }
987
988 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
989 return this.mSignalStrength <= nc.mSignalStrength;
990 }
991
992 private boolean equalsSignalStrength(NetworkCapabilities nc) {
993 return this.mSignalStrength == nc.mSignalStrength;
994 }
995
996 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900997 * List of UIDs this network applies to. No restriction if null.
998 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900999 * For networks, mUids represent the list of network this applies to, and null means this
1000 * network applies to all UIDs.
1001 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1002 * must be included in a network so that they match. As an exception to the general rule,
1003 * a null mUids field for requests mean "no requirements" rather than what the general rule
1004 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1005 * of this API expect in practice. A network that must match all UIDs can still be
1006 * expressed with a set ranging the entire set of possible UIDs.
1007 * <p>
1008 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001009 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1010 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1011 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1012 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001013 * As a special exception, the app managing this network (as identified by its UID stored in
1014 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1015 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1016 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001017 * <p>
1018 * Please note that in principle a single app can be associated with multiple UIDs because
1019 * each app will have a different UID when it's run as a different (macro-)user. A single
1020 * macro user can only have a single active VPN app at any given time however.
1021 * <p>
1022 * Also please be aware this class does not try to enforce any normalization on this. Callers
1023 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1024 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1025 * their own (like requiring sortedness or no overlap) they need to enforce it
1026 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1027 * or overlapping ranges are present.
1028 *
1029 * @hide
1030 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001031 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001032
1033 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001034 * Convenience method to set the UIDs this network applies to to a single UID.
1035 * @hide
1036 */
1037 public NetworkCapabilities setSingleUid(int uid) {
1038 final ArraySet<UidRange> identity = new ArraySet<>(1);
1039 identity.add(new UidRange(uid, uid));
1040 setUids(identity);
1041 return this;
1042 }
1043
1044 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001045 * Set the list of UIDs this network applies to.
1046 * This makes a copy of the set so that callers can't modify it after the call.
1047 * @hide
1048 */
1049 public NetworkCapabilities setUids(Set<UidRange> uids) {
1050 if (null == uids) {
1051 mUids = null;
1052 } else {
1053 mUids = new ArraySet<>(uids);
1054 }
1055 return this;
1056 }
1057
1058 /**
1059 * Get the list of UIDs this network applies to.
1060 * This returns a copy of the set so that callers can't modify the original object.
1061 * @hide
1062 */
1063 public Set<UidRange> getUids() {
1064 return null == mUids ? null : new ArraySet<>(mUids);
1065 }
1066
1067 /**
1068 * Test whether this network applies to this UID.
1069 * @hide
1070 */
1071 public boolean appliesToUid(int uid) {
1072 if (null == mUids) return true;
1073 for (UidRange range : mUids) {
1074 if (range.contains(uid)) {
1075 return true;
1076 }
1077 }
1078 return false;
1079 }
1080
1081 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001082 * Tests if the set of UIDs that this network applies to is the same as the passed network.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001083 * <p>
1084 * This test only checks whether equal range objects are in both sets. It will
1085 * return false if the ranges are not exactly the same, even if the covered UIDs
1086 * are for an equivalent result.
1087 * <p>
1088 * Note that this method is not very optimized, which is fine as long as it's not used very
1089 * often.
1090 * <p>
1091 * nc is assumed nonnull.
1092 *
1093 * @hide
1094 */
1095 @VisibleForTesting
1096 public boolean equalsUids(NetworkCapabilities nc) {
1097 Set<UidRange> comparedUids = nc.mUids;
1098 if (null == comparedUids) return null == mUids;
1099 if (null == mUids) return false;
1100 // Make a copy so it can be mutated to check that all ranges in mUids
1101 // also are in uids.
1102 final Set<UidRange> uids = new ArraySet<>(mUids);
1103 for (UidRange range : comparedUids) {
1104 if (!uids.contains(range)) {
1105 return false;
1106 }
1107 uids.remove(range);
1108 }
1109 return uids.isEmpty();
1110 }
1111
1112 /**
1113 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1114 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001115 * This method is called on the NetworkCapabilities embedded in a request with the
1116 * capabilities of an available network. It checks whether all the UIDs from this listen
1117 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1118 * in the passed nc (representing the UIDs that this network is available to).
1119 * <p>
1120 * As a special exception, the UID that created the passed network (as represented by its
1121 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1122 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1123 * can see its own network when it listens for it.
1124 * <p>
1125 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001126 * @see #appliesToUid
1127 * @hide
1128 */
1129 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001130 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001131 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001132 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001133 if (!nc.appliesToUidRange(requiredRange)) {
1134 return false;
1135 }
1136 }
1137 return true;
1138 }
1139
1140 /**
1141 * Returns whether this network applies to the passed ranges.
1142 * This assumes that to apply, the passed range has to be entirely contained
1143 * within one of the ranges this network applies to. If the ranges are not normalized,
1144 * this method may return false even though all required UIDs are covered because no
1145 * single range contained them all.
1146 * @hide
1147 */
1148 @VisibleForTesting
1149 public boolean appliesToUidRange(UidRange requiredRange) {
1150 if (null == mUids) return true;
1151 for (UidRange uidRange : mUids) {
1152 if (uidRange.containsRange(requiredRange)) {
1153 return true;
1154 }
1155 }
1156 return false;
1157 }
1158
1159 /**
1160 * Combine the UIDs this network currently applies to with the UIDs the passed
1161 * NetworkCapabilities apply to.
1162 * nc is assumed nonnull.
1163 */
1164 private void combineUids(NetworkCapabilities nc) {
1165 if (null == nc.mUids || null == mUids) {
1166 mUids = null;
1167 return;
1168 }
1169 mUids.addAll(nc.mUids);
1170 }
1171
Chalard Jeanb03a6222018-04-11 21:09:10 +09001172
1173 /**
1174 * The SSID of the network, or null if not applicable or unknown.
1175 * <p>
1176 * This is filled in by wifi code.
1177 * @hide
1178 */
1179 private String mSSID;
1180
1181 /**
1182 * Sets the SSID of this network.
1183 * @hide
1184 */
1185 public NetworkCapabilities setSSID(String ssid) {
1186 mSSID = ssid;
1187 return this;
1188 }
1189
1190 /**
1191 * Gets the SSID of this network, or null if none or unknown.
1192 * @hide
1193 */
1194 public String getSSID() {
1195 return mSSID;
1196 }
1197
1198 /**
1199 * Tests if the SSID of this network is the same as the SSID of the passed network.
1200 * @hide
1201 */
1202 public boolean equalsSSID(NetworkCapabilities nc) {
1203 return Objects.equals(mSSID, nc.mSSID);
1204 }
1205
1206 /**
1207 * Check if the SSID requirements of this object are matched by the passed object.
1208 * @hide
1209 */
1210 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1211 return mSSID == null || mSSID.equals(nc.mSSID);
1212 }
1213
1214 /**
1215 * Combine SSIDs of the capabilities.
1216 * <p>
1217 * This is only legal if either the SSID of this object is null, or both SSIDs are
1218 * equal.
1219 * @hide
1220 */
1221 private void combineSSIDs(NetworkCapabilities nc) {
1222 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1223 throw new IllegalStateException("Can't combine two SSIDs");
1224 }
1225 setSSID(nc.mSSID);
1226 }
1227
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001228 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001229 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1230 * <p>
1231 * Note that this method may break an invariant of having a particular capability in either
1232 * wanted or unwanted lists but never in both. Requests that have the same capability in
1233 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001234 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001235 */
1236 public void combineCapabilities(NetworkCapabilities nc) {
1237 combineNetCapabilities(nc);
1238 combineTransportTypes(nc);
1239 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001240 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001241 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001242 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001243 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001244 }
1245
1246 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001247 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1248 *
1249 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1250 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1251 * bandwidth, signal strength, or validation / captive portal status.
1252 *
1253 * @hide
1254 */
1255 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001256 return (nc != null
1257 && satisfiedByNetCapabilities(nc, onlyImmutable)
1258 && satisfiedByTransportTypes(nc)
1259 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1260 && satisfiedBySpecifier(nc)
1261 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001262 && (onlyImmutable || satisfiedByUids(nc))
1263 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001264 }
1265
1266 /**
1267 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1268 *
1269 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1270 *
1271 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001272 */
1273 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001274 return satisfiedByNetworkCapabilities(nc, false);
1275 }
1276
1277 /**
1278 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1279 *
1280 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1281 *
1282 * @hide
1283 */
1284 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1285 return satisfiedByNetworkCapabilities(nc, true);
1286 }
1287
1288 /**
1289 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001290 * {@code NetworkCapabilities} and return a String describing any difference.
1291 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001292 *
1293 * @hide
1294 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001295 public String describeImmutableDifferences(NetworkCapabilities that) {
1296 if (that == null) {
1297 return "other NetworkCapabilities was null";
1298 }
1299
1300 StringJoiner joiner = new StringJoiner(", ");
1301
Hugo Benichieae7a222017-07-25 11:40:56 +09001302 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1303 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001304 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001305 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1306 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1307 if (oldImmutableCapabilities != newImmutableCapabilities) {
1308 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1309 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1310 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1311 }
1312
1313 if (!equalsSpecifier(that)) {
1314 NetworkSpecifier before = this.getNetworkSpecifier();
1315 NetworkSpecifier after = that.getNetworkSpecifier();
1316 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1317 }
1318
1319 if (!equalsTransportTypes(that)) {
1320 String before = transportNamesOf(this.getTransportTypes());
1321 String after = transportNamesOf(that.getTransportTypes());
1322 joiner.add(String.format("transports changed: %s -> %s", before, after));
1323 }
1324
1325 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001326 }
1327
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001328 /**
1329 * Checks that our requestable capabilities are the same as those of the given
1330 * {@code NetworkCapabilities}.
1331 *
1332 * @hide
1333 */
1334 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1335 if (nc == null) return false;
1336 return (equalsNetCapabilitiesRequestable(nc) &&
1337 equalsTransportTypes(nc) &&
1338 equalsSpecifier(nc));
1339 }
1340
Robert Greenwalt1448f052014-04-08 13:41:39 -07001341 @Override
1342 public boolean equals(Object obj) {
1343 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001344 NetworkCapabilities that = (NetworkCapabilities) obj;
1345 return (equalsNetCapabilities(that)
1346 && equalsTransportTypes(that)
1347 && equalsLinkBandwidths(that)
1348 && equalsSignalStrength(that)
1349 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001350 && equalsUids(that)
1351 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001352 }
1353
1354 @Override
1355 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001356 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001357 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001358 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1359 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1360 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1361 + ((int) (mTransportTypes >> 32) * 13)
1362 + (mLinkUpBandwidthKbps * 17)
1363 + (mLinkDownBandwidthKbps * 19)
1364 + Objects.hashCode(mNetworkSpecifier) * 23
1365 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001366 + Objects.hashCode(mUids) * 31
1367 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001368 }
1369
Wink Saville4e2dea72014-09-20 11:04:03 -07001370 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001371 public int describeContents() {
1372 return 0;
1373 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001374 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001375 public void writeToParcel(Parcel dest, int flags) {
1376 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001377 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 dest.writeLong(mTransportTypes);
1379 dest.writeInt(mLinkUpBandwidthKbps);
1380 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001381 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001382 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001383 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001384 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001385 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001386
Robert Greenwalt1448f052014-04-08 13:41:39 -07001387 public static final Creator<NetworkCapabilities> CREATOR =
1388 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001389 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001390 public NetworkCapabilities createFromParcel(Parcel in) {
1391 NetworkCapabilities netCap = new NetworkCapabilities();
1392
1393 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001394 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001395 netCap.mTransportTypes = in.readLong();
1396 netCap.mLinkUpBandwidthKbps = in.readInt();
1397 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001398 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001399 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001400 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1401 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001402 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001403 return netCap;
1404 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001405 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001406 public NetworkCapabilities[] newArray(int size) {
1407 return new NetworkCapabilities[size];
1408 }
1409 };
1410
Wink Saville4e2dea72014-09-20 11:04:03 -07001411 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001412 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001413 final StringBuilder sb = new StringBuilder("[");
1414 if (0 != mTransportTypes) {
1415 sb.append(" Transports: ");
1416 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1417 NetworkCapabilities::transportNameOf, "|");
1418 }
1419 if (0 != mNetworkCapabilities) {
1420 sb.append(" Capabilities: ");
1421 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1422 NetworkCapabilities::capabilityNameOf, "&");
1423 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001424 if (0 != mNetworkCapabilities) {
1425 sb.append(" Unwanted: ");
1426 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1427 NetworkCapabilities::capabilityNameOf, "&");
1428 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001429 if (mLinkUpBandwidthKbps > 0) {
1430 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1431 }
1432 if (mLinkDownBandwidthKbps > 0) {
1433 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1434 }
1435 if (mNetworkSpecifier != null) {
1436 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1437 }
1438 if (hasSignalStrength()) {
1439 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001440 }
1441
Chalard Jean07ace0f2018-02-26 19:00:45 +09001442 if (null != mUids) {
1443 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1444 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1445 } else {
1446 sb.append(" Uids: <").append(mUids).append(">");
1447 }
1448 }
1449 if (mEstablishingVpnAppUid != INVALID_UID) {
1450 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1451 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001452
Chalard Jeanb03a6222018-04-11 21:09:10 +09001453 if (null != mSSID) {
1454 sb.append(" SSID: ").append(mSSID);
1455 }
1456
Chalard Jean07ace0f2018-02-26 19:00:45 +09001457 sb.append("]");
1458 return sb.toString();
1459 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001460
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001461
Chalard Jean07ace0f2018-02-26 19:00:45 +09001462 private interface NameOf {
1463 String nameOf(int value);
1464 }
1465 /**
1466 * @hide
1467 */
1468 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1469 long bitMask, NameOf nameFetcher, String separator) {
1470 int bitPos = 0;
1471 boolean firstElementAdded = false;
1472 while (bitMask != 0) {
1473 if ((bitMask & 1) != 0) {
1474 if (firstElementAdded) {
1475 sb.append(separator);
1476 } else {
1477 firstElementAdded = true;
1478 }
1479 sb.append(nameFetcher.nameOf(bitPos));
1480 }
1481 bitMask >>= 1;
1482 ++bitPos;
1483 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001484 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001485
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001486 /** @hide */
1487 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1488 final long token = proto.start(fieldId);
1489
1490 for (int transport : getTransportTypes()) {
1491 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1492 }
1493
1494 for (int capability : getCapabilities()) {
1495 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1496 }
1497
1498 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1499 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1500
1501 if (mNetworkSpecifier != null) {
1502 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1503 }
1504
1505 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1506 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1507
1508 proto.end(token);
1509 }
1510
Hugo Benichi5df9d722016-04-25 17:16:35 +09001511 /**
1512 * @hide
1513 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001514 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001515 StringJoiner joiner = new StringJoiner("|");
1516 if (capabilities != null) {
1517 for (int c : capabilities) {
1518 joiner.add(capabilityNameOf(c));
1519 }
1520 }
1521 return joiner.toString();
1522 }
1523
1524 /**
1525 * @hide
1526 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001527 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001528 switch (capability) {
1529 case NET_CAPABILITY_MMS: return "MMS";
1530 case NET_CAPABILITY_SUPL: return "SUPL";
1531 case NET_CAPABILITY_DUN: return "DUN";
1532 case NET_CAPABILITY_FOTA: return "FOTA";
1533 case NET_CAPABILITY_IMS: return "IMS";
1534 case NET_CAPABILITY_CBS: return "CBS";
1535 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1536 case NET_CAPABILITY_IA: return "IA";
1537 case NET_CAPABILITY_RCS: return "RCS";
1538 case NET_CAPABILITY_XCAP: return "XCAP";
1539 case NET_CAPABILITY_EIMS: return "EIMS";
1540 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1541 case NET_CAPABILITY_INTERNET: return "INTERNET";
1542 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1543 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1544 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1545 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1546 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001547 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001548 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001549 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001550 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001551 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001552 default: return Integer.toString(capability);
1553 }
1554 }
1555
1556 /**
1557 * @hide
1558 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001559 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -06001560 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001561 StringJoiner joiner = new StringJoiner("|");
1562 if (types != null) {
1563 for (int t : types) {
1564 joiner.add(transportNameOf(t));
1565 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001566 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001567 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001568 }
1569
1570 /**
1571 * @hide
1572 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001573 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001574 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001575 return "UNKNOWN";
1576 }
1577 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001578 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001579
Jeff Sharkeyde570312017-10-24 21:25:50 -06001580 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001581 Preconditions.checkArgument(
1582 isValidTransport(transport), "Invalid TransportType " + transport);
1583 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001584
1585 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1586 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1587 }
1588
1589 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1590 Preconditions.checkArgument(isValidCapability(capability),
1591 "NetworkCapability " + capability + "out of range");
1592 }
junyulai05986c62018-08-07 19:50:45 +08001593
1594 /**
1595 * Check if this {@code NetworkCapability} instance is metered.
1596 *
1597 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1598 * @hide
1599 */
1600 public boolean isMetered() {
1601 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1602 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001603}