blob: 65dfb132378f5e65f48cba6913dc2e9549337109 [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 Sharkey72f9c422017-10-27 17:22:59 -060021import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070022import android.os.Parcel;
23import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090024import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080025import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070026
27import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090028import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090029import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070030
Jeff Sharkeyde570312017-10-24 21:25:50 -060031import java.lang.annotation.Retention;
32import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070033import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090034import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090035import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070036
37/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070038 * Representation of the capabilities of an active network. Instances are
39 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060040 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
41 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060042 * <p>
43 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
44 * network selection. Rather than indicate a need for Wi-Fi because an
45 * application needs high bandwidth and risk obsolescence when a new, fast
46 * network appears (like LTE), the application should specify it needs high
47 * bandwidth. Similarly if an application needs an unmetered network for a bulk
48 * transfer it can specify that rather than assuming all cellular based
49 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070050 */
51public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070052 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090053 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070054
Robert Greenwalt7569f182014-06-08 16:42:59 -070055 /**
56 * @hide
57 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070058 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090059 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090060 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070061 }
62
63 public NetworkCapabilities(NetworkCapabilities nc) {
64 if (nc != null) {
65 mNetworkCapabilities = nc.mNetworkCapabilities;
66 mTransportTypes = nc.mTransportTypes;
67 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
68 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070069 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090070 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090071 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090072 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080073 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
Chalard Jeanb03a6222018-04-11 21:09:10 +090074 mSSID = nc.mSSID;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070075 }
76 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070077
78 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090079 * Completely clears the contents of this object, removing even the capabilities that are set
80 * by default when the object is constructed.
81 * @hide
82 */
83 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080084 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070085 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090086 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090087 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090088 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090089 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090090 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 }
92
93 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070094 * Represents the network's capabilities. If any are specified they will be satisfied
95 * by any Network that matches all of them.
96 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090097 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070098
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080099 /**
100 * If any capabilities specified here they must not exist in the matching Network.
101 */
102 private long mUnwantedNetworkCapabilities;
103
Jeff Sharkeyde570312017-10-24 21:25:50 -0600104 /** @hide */
105 @Retention(RetentionPolicy.SOURCE)
106 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
107 NET_CAPABILITY_MMS,
108 NET_CAPABILITY_SUPL,
109 NET_CAPABILITY_DUN,
110 NET_CAPABILITY_FOTA,
111 NET_CAPABILITY_IMS,
112 NET_CAPABILITY_CBS,
113 NET_CAPABILITY_WIFI_P2P,
114 NET_CAPABILITY_IA,
115 NET_CAPABILITY_RCS,
116 NET_CAPABILITY_XCAP,
117 NET_CAPABILITY_EIMS,
118 NET_CAPABILITY_NOT_METERED,
119 NET_CAPABILITY_INTERNET,
120 NET_CAPABILITY_NOT_RESTRICTED,
121 NET_CAPABILITY_TRUSTED,
122 NET_CAPABILITY_NOT_VPN,
123 NET_CAPABILITY_VALIDATED,
124 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600125 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600126 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900127 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900128 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800129 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600130 })
131 public @interface NetCapability { }
132
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700134 * Indicates this is a network that has the ability to reach the
135 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700136 */
137 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700138
139 /**
140 * Indicates this is a network that has the ability to reach the carrier's
141 * SUPL server, used to retrieve GPS information.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates this is a network that has the ability to reach the carrier's
147 * DUN or tethering gateway.
148 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150
151 /**
152 * Indicates this is a network that has the ability to reach the carrier's
153 * FOTA portal, used for over the air updates.
154 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700155 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700156
157 /**
158 * Indicates this is a network that has the ability to reach the carrier's
159 * IMS servers, used for network registration and signaling.
160 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700161 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700162
163 /**
164 * Indicates this is a network that has the ability to reach the carrier's
165 * CBS servers, used for carrier specific services.
166 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700167 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700168
169 /**
170 * Indicates this is a network that has the ability to reach a Wi-Fi direct
171 * peer.
172 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174
175 /**
176 * Indicates this is a network that has the ability to reach a carrier's
177 * Initial Attach servers.
178 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach a carrier's
183 * RCS servers, used for Rich Communication Services.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach a carrier's
189 * XCAP servers, used for configuration and control.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700195 * Emergency IMS servers or other services, used for network signaling
196 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates that this network is unmetered.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates that this network should be able to reach the internet.
207 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700208 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700209
210 /**
211 * Indicates that this network is available for general use. If this is not set
212 * applications should not attempt to communicate on this network. Note that this
213 * is simply informative and not enforcement - enforcement is handled via other means.
214 * Set by default.
215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
217
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700218 /**
219 * Indicates that the user has indicated implicit trust of this network. This
220 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
221 * BT device or a wifi the user asked to connect to. Untrusted networks
222 * are probably limited to unknown wifi AP. Set by default.
223 */
224 public static final int NET_CAPABILITY_TRUSTED = 14;
225
Paul Jensen76b610a2015-03-18 09:33:07 -0400226 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400227 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400228 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400229 */
230 public static final int NET_CAPABILITY_NOT_VPN = 15;
231
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900232 /**
233 * Indicates that connectivity on this network was successfully validated. For example, for a
234 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
235 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900236 */
237 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700238
Paul Jensen3d194ea2015-06-16 14:27:36 -0400239 /**
240 * Indicates that this network was found to have a captive portal in place last time it was
241 * probed.
242 */
243 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
244
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900245 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600246 * Indicates that this network is not roaming.
247 */
248 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
249
250 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900251 * Indicates that this network is available for use by apps, and not a network that is being
252 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900253 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600254 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900255
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900256 /**
257 * Indicates that this network is not congested.
258 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600259 * When a network is congested, applications should defer network traffic
260 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900261 */
262 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
263
Chalard Jean804b8fb2018-01-30 22:41:41 +0900264 /**
265 * Indicates that this network is not currently suspended.
266 * <p>
267 * When a network is suspended, the network's IP addresses and any connections
268 * established on the network remain valid, but the network is temporarily unable
269 * to transfer data. This can happen, for example, if a cellular network experiences
270 * a temporary loss of signal, such as when driving through a tunnel, etc.
271 * A network with this capability is not suspended, so is expected to be able to
272 * transfer data.
273 */
274 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
275
Pavel Maltsev43403202018-01-30 17:19:44 -0800276 /**
277 * Indicates that traffic that goes through this network is paid by oem. For example,
278 * this network can be used by system apps to upload telemetry data.
279 * @hide
280 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700281 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800282 public static final int NET_CAPABILITY_OEM_PAID = 22;
283
Robert Greenwalt1448f052014-04-08 13:41:39 -0700284 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800285 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700286
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700287 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900288 * Network capabilities that are expected to be mutable, i.e., can change while a particular
289 * network is connected.
290 */
291 private static final long MUTABLE_CAPABILITIES =
292 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
293 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900294 (1 << NET_CAPABILITY_TRUSTED)
295 | (1 << NET_CAPABILITY_VALIDATED)
296 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
297 | (1 << NET_CAPABILITY_NOT_ROAMING)
298 | (1 << NET_CAPABILITY_FOREGROUND)
299 | (1 << NET_CAPABILITY_NOT_CONGESTED)
300 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900301
302 /**
303 * Network capabilities that are not allowed in NetworkRequests. This exists because the
304 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
305 * capability's presence cannot be known in advance. If such a capability is requested, then we
306 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
307 * get immediately torn down because they do not have the requested capability.
308 */
309 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900310 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900311
312 /**
313 * Capabilities that are set by default when the object is constructed.
314 */
315 private static final long DEFAULT_CAPABILITIES =
316 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
317 (1 << NET_CAPABILITY_TRUSTED) |
318 (1 << NET_CAPABILITY_NOT_VPN);
319
320 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400321 * Capabilities that suggest that a network is restricted.
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000322 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400323 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700324 @VisibleForTesting
325 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400326 (1 << NET_CAPABILITY_CBS) |
327 (1 << NET_CAPABILITY_DUN) |
328 (1 << NET_CAPABILITY_EIMS) |
329 (1 << NET_CAPABILITY_FOTA) |
330 (1 << NET_CAPABILITY_IA) |
331 (1 << NET_CAPABILITY_IMS) |
332 (1 << NET_CAPABILITY_RCS) |
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000333 (1 << NET_CAPABILITY_XCAP);
334
335 /**
336 * Capabilities that force network to be restricted.
337 * {@see #maybeMarkCapabilitiesRestricted}.
338 */
339 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800340 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400341
342 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700343 * Capabilities that suggest that a network is unrestricted.
344 * {@see #maybeMarkCapabilitiesRestricted}.
345 */
346 @VisibleForTesting
347 /* package */ static final long UNRESTRICTED_CAPABILITIES =
348 (1 << NET_CAPABILITY_INTERNET) |
349 (1 << NET_CAPABILITY_MMS) |
350 (1 << NET_CAPABILITY_SUPL) |
351 (1 << NET_CAPABILITY_WIFI_P2P);
352
353 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700354 * Adds the given capability to this {@code NetworkCapability} instance.
355 * Multiple capabilities may be applied sequentially. Note that when searching
356 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800357 * <p>
358 * If the given capability was previously added to the list of unwanted capabilities
359 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700360 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600361 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900362 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700363 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700364 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600365 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800366 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700367 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800368 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700369 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700370 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700371
372 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800373 * Adds the given capability to the list of unwanted capabilities of this
374 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
375 * sequentially. Note that when searching for a network to satisfy a request, the network
376 * must not contain any capability from unwanted capability list.
377 * <p>
378 * If the capability was previously added to the list of required capabilities (for
379 * example, it was there by default or added using {@link #addCapability(int)} method), then
380 * it will be removed from the list of required capabilities as well.
381 *
382 * @see #addCapability(int)
383 * @hide
384 */
385 public void addUnwantedCapability(@NetCapability int capability) {
386 checkValidCapability(capability);
387 mUnwantedNetworkCapabilities |= 1 << capability;
388 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
389 }
390
391 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700392 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800393 * <p>
394 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
395 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700396 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600397 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900398 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700399 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700400 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600401 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800402 checkValidCapability(capability);
403 final long mask = ~(1 << capability);
404 mNetworkCapabilities &= mask;
405 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700406 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700407 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700408
409 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600410 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
411 * instance.
412 *
413 * @hide
414 */
415 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
416 if (value) {
417 addCapability(capability);
418 } else {
419 removeCapability(capability);
420 }
421 return this;
422 }
423
424 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700425 * Gets all the capabilities set on this {@code NetworkCapability} instance.
426 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600427 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700428 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700429 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600430 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900431 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700432 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700433
434 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800435 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
436 *
437 * @return an array of unwanted capability values for this instance.
438 * @hide
439 */
440 public @NetCapability int[] getUnwantedCapabilities() {
441 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
442 }
443
444
445 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600446 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700447 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600448 *
449 * @hide
450 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800451 public void setCapabilities(@NetCapability int[] capabilities,
452 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600453 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800454 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600455 }
456
457 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800458 * @deprecated use {@link #setCapabilities(int[], int[])}
459 * @hide
460 */
461 @Deprecated
462 public void setCapabilities(@NetCapability int[] capabilities) {
463 setCapabilities(capabilities, new int[] {});
464 }
465
466 /**
467 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700468 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600469 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700470 * @return {@code true} if set on this instance.
471 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600472 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800473 return isValidCapability(capability)
474 && ((mNetworkCapabilities & (1 << capability)) != 0);
475 }
476
477 /** @hide */
478 public boolean hasUnwantedCapability(@NetCapability int capability) {
479 return isValidCapability(capability)
480 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700481 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700482
Robert Greenwalt1448f052014-04-08 13:41:39 -0700483 private void combineNetCapabilities(NetworkCapabilities nc) {
484 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800485 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700486 }
487
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900488 /**
489 * Convenience function that returns a human-readable description of the first mutable
490 * capability we find. Used to present an error message to apps that request mutable
491 * capabilities.
492 *
493 * @hide
494 */
495 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800496 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
497 & NON_REQUESTABLE_CAPABILITIES;
498
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900499 if (nonRequestable != 0) {
500 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900501 }
502 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900503 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900504 return null;
505 }
506
507 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800508 long requestedCapabilities = mNetworkCapabilities;
509 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
510 long providedCapabilities = nc.mNetworkCapabilities;
511
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900512 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800513 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
514 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900515 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800516 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
517 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700518 }
519
Robert Greenwalt06314e42014-10-29 14:04:06 -0700520 /** @hide */
521 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800522 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
523 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700524 }
525
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900526 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
527 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800528 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
529 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
530 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900531 }
532
Robert Greenwalt1448f052014-04-08 13:41:39 -0700533 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400534 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
535 * typically provided by restricted networks.
536 *
537 * TODO: consider:
538 * - Renaming it to guessRestrictedCapability and make it set the
539 * restricted capability bit in addition to clearing it.
540 * @hide
541 */
542 public void maybeMarkCapabilitiesRestricted() {
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000543 // Check if we have any capability that forces the network to be restricted.
544 final boolean forceRestrictedCapability =
545 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
546
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700547 // Verify there aren't any unrestricted capabilities. If there are we say
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000548 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700549 final boolean hasUnrestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000550 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700551
552 // Must have at least some restricted capabilities.
553 final boolean hasRestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000554 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700555
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000556 if (forceRestrictedCapability
557 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400558 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400559 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400560 }
561
562 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700563 * Representing the transport type. Apps should generally not care about transport. A
564 * request for a fast internet connection could be satisfied by a number of different
565 * transports. If any are specified here it will be satisfied a Network that matches
566 * any of them. If a caller doesn't care about the transport it should not specify any.
567 */
568 private long mTransportTypes;
569
Jeff Sharkeyde570312017-10-24 21:25:50 -0600570 /** @hide */
571 @Retention(RetentionPolicy.SOURCE)
572 @IntDef(prefix = { "TRANSPORT_" }, value = {
573 TRANSPORT_CELLULAR,
574 TRANSPORT_WIFI,
575 TRANSPORT_BLUETOOTH,
576 TRANSPORT_ETHERNET,
577 TRANSPORT_VPN,
578 TRANSPORT_WIFI_AWARE,
579 TRANSPORT_LOWPAN,
580 })
581 public @interface Transport { }
582
Robert Greenwalt1448f052014-04-08 13:41:39 -0700583 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700584 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700585 */
586 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700587
588 /**
589 * Indicates this network uses a Wi-Fi transport.
590 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700591 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700592
593 /**
594 * Indicates this network uses a Bluetooth transport.
595 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700596 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700597
598 /**
599 * Indicates this network uses an Ethernet transport.
600 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700601 public static final int TRANSPORT_ETHERNET = 3;
602
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400603 /**
604 * Indicates this network uses a VPN transport.
605 */
606 public static final int TRANSPORT_VPN = 4;
607
Etan Cohen305ea282016-06-20 09:27:12 -0700608 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700609 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700610 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700611 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700612
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700613 /**
614 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700615 */
616 public static final int TRANSPORT_LOWPAN = 6;
617
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900618 /** @hide */
619 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
620 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700621 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700622
Hugo Benichi16f0a942017-06-20 14:07:59 +0900623 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600624 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900625 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
626 }
627
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900628 private static final String[] TRANSPORT_NAMES = {
629 "CELLULAR",
630 "WIFI",
631 "BLUETOOTH",
632 "ETHERNET",
633 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700634 "WIFI_AWARE",
635 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900636 };
637
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700638 /**
639 * Adds the given transport type to this {@code NetworkCapability} instance.
640 * Multiple transports may be applied sequentially. Note that when searching
641 * for a network to satisfy a request, any listed in the request will satisfy the request.
642 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
643 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
644 * to be selected. This is logically different than
645 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
646 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600647 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900648 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700649 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700650 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600651 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900652 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700653 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700654 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700655 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700656 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700657
658 /**
659 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
660 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600661 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900662 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700663 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700664 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600665 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900666 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700667 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700668 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700669 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700670 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700671
672 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600673 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
674 * instance.
675 *
676 * @hide
677 */
678 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
679 if (value) {
680 addTransportType(transportType);
681 } else {
682 removeTransportType(transportType);
683 }
684 return this;
685 }
686
687 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700688 * Gets all the transports set on this {@code NetworkCapability} instance.
689 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600690 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700691 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700692 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600693 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900694 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700695 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700696
697 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600698 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700699 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600700 *
701 * @hide
702 */
703 public void setTransportTypes(@Transport int[] transportTypes) {
704 mTransportTypes = BitUtils.packBits(transportTypes);
705 }
706
707 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700708 * Tests for the presence of a transport on this instance.
709 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600710 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700711 * @return {@code true} if set on this instance.
712 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600713 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900714 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700715 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700716
717 private void combineTransportTypes(NetworkCapabilities nc) {
718 this.mTransportTypes |= nc.mTransportTypes;
719 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900720
Robert Greenwalt1448f052014-04-08 13:41:39 -0700721 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
722 return ((this.mTransportTypes == 0) ||
723 ((this.mTransportTypes & nc.mTransportTypes) != 0));
724 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900725
Robert Greenwalt06314e42014-10-29 14:04:06 -0700726 /** @hide */
727 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700728 return (nc.mTransportTypes == this.mTransportTypes);
729 }
730
731 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900732 * UID of the app that manages this network, or INVALID_UID if none/unknown.
733 *
734 * This field keeps track of the UID of the app that created this network and is in charge
735 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
736 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
737 * to create networks.
738 *
739 * Because this field is only used in the services side (and to avoid apps being able to
740 * set this to whatever they want), this field is not parcelled and will not be conserved
741 * across the IPC boundary.
742 * @hide
743 */
744 private int mEstablishingVpnAppUid = INVALID_UID;
745
746 /**
747 * Set the UID of the managing app.
748 * @hide
749 */
750 public void setEstablishingVpnAppUid(final int uid) {
751 mEstablishingVpnAppUid = uid;
752 }
753
754 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600755 * Value indicating that link bandwidth is unspecified.
756 * @hide
757 */
758 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
759
760 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700761 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
762 * for the first hop on the given transport. It is not measured, but may take into account
763 * link parameters (Radio technology, allocated channels, etc).
764 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600765 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
766 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700767
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700768 /**
769 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
770 * the estimated first hop transport bandwidth.
771 * <p>
772 * Note that when used to request a network, this specifies the minimum acceptable.
773 * When received as the state of an existing network this specifies the typical
774 * first hop bandwidth expected. This is never measured, but rather is inferred
775 * from technology type and other link parameters. It could be used to differentiate
776 * between very slow 1xRTT cellular links and other faster networks or even between
777 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
778 * fast backhauls and slow backhauls.
779 *
780 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700781 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700782 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600783 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700784 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600785 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700786 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700787
788 /**
789 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
790 * the estimated first hop transport bandwidth.
791 *
792 * @return The estimated first hop upstream (device to network) bandwidth.
793 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700794 public int getLinkUpstreamBandwidthKbps() {
795 return mLinkUpBandwidthKbps;
796 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700797
798 /**
799 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
800 * the estimated first hop transport bandwidth.
801 * <p>
802 * Note that when used to request a network, this specifies the minimum acceptable.
803 * When received as the state of an existing network this specifies the typical
804 * first hop bandwidth expected. This is never measured, but rather is inferred
805 * from technology type and other link parameters. It could be used to differentiate
806 * between very slow 1xRTT cellular links and other faster networks or even between
807 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
808 * fast backhauls and slow backhauls.
809 *
810 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700811 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700812 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600813 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700814 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600815 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700816 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700817
818 /**
819 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
820 * the estimated first hop transport bandwidth.
821 *
822 * @return The estimated first hop downstream (network to device) bandwidth.
823 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700824 public int getLinkDownstreamBandwidthKbps() {
825 return mLinkDownBandwidthKbps;
826 }
827
828 private void combineLinkBandwidths(NetworkCapabilities nc) {
829 this.mLinkUpBandwidthKbps =
830 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
831 this.mLinkDownBandwidthKbps =
832 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
833 }
834 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
835 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
836 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
837 }
838 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
839 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
840 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
841 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600842 /** @hide */
843 public static int minBandwidth(int a, int b) {
844 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
845 return b;
846 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
847 return a;
848 } else {
849 return Math.min(a, b);
850 }
851 }
852 /** @hide */
853 public static int maxBandwidth(int a, int b) {
854 return Math.max(a, b);
855 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700856
Etan Cohena7434272017-04-03 12:17:51 -0700857 private NetworkSpecifier mNetworkSpecifier = null;
858
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700859 /**
860 * Sets the optional bearer specific network specifier.
861 * This has no meaning if a single transport is also not specified, so calling
862 * this without a single transport set will generate an exception, as will
863 * subsequently adding or removing transports after this is set.
864 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700865 *
Etan Cohena7434272017-04-03 12:17:51 -0700866 * @param networkSpecifier A concrete, parcelable framework class that extends
867 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900868 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700869 * @hide
870 */
Etan Cohena7434272017-04-03 12:17:51 -0700871 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
872 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700873 throw new IllegalStateException("Must have a single transport specified to use " +
874 "setNetworkSpecifier");
875 }
Etan Cohena7434272017-04-03 12:17:51 -0700876
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700877 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700878
Pierre Imaic8419a82016-03-22 17:54:54 +0900879 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700880 }
881
882 /**
883 * Gets the optional bearer specific network specifier.
884 *
Etan Cohena7434272017-04-03 12:17:51 -0700885 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
886 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700887 * @hide
888 */
Etan Cohena7434272017-04-03 12:17:51 -0700889 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700890 return mNetworkSpecifier;
891 }
892
893 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700894 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700895 throw new IllegalStateException("Can't combine two networkSpecifiers");
896 }
Etan Cohena7434272017-04-03 12:17:51 -0700897 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700898 }
Etan Cohena7434272017-04-03 12:17:51 -0700899
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700900 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700901 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
902 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700903 }
Etan Cohena7434272017-04-03 12:17:51 -0700904
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700905 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700906 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700907 }
908
Robert Greenwalt1448f052014-04-08 13:41:39 -0700909 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900910 * Magic value that indicates no signal strength provided. A request specifying this value is
911 * always satisfied.
912 *
913 * @hide
914 */
915 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
916
917 /**
918 * Signal strength. This is a signed integer, and higher values indicate better signal.
919 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
920 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700921 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900922
923 /**
924 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
925 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900926 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900927 * <p>
928 * Note that when used to register a network callback, this specifies the minimum acceptable
929 * signal strength. When received as the state of an existing network it specifies the current
930 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
931 * effect when requesting a callback.
932 *
933 * @param signalStrength the bearer-specific signal strength.
934 * @hide
935 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600936 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900937 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600938 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900939 }
940
941 /**
942 * Returns {@code true} if this object specifies a signal strength.
943 *
944 * @hide
945 */
946 public boolean hasSignalStrength() {
947 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
948 }
949
950 /**
951 * Retrieves the signal strength.
952 *
953 * @return The bearer-specific signal strength.
954 * @hide
955 */
956 public int getSignalStrength() {
957 return mSignalStrength;
958 }
959
960 private void combineSignalStrength(NetworkCapabilities nc) {
961 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
962 }
963
964 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
965 return this.mSignalStrength <= nc.mSignalStrength;
966 }
967
968 private boolean equalsSignalStrength(NetworkCapabilities nc) {
969 return this.mSignalStrength == nc.mSignalStrength;
970 }
971
972 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900973 * List of UIDs this network applies to. No restriction if null.
974 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900975 * For networks, mUids represent the list of network this applies to, and null means this
976 * network applies to all UIDs.
977 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
978 * must be included in a network so that they match. As an exception to the general rule,
979 * a null mUids field for requests mean "no requirements" rather than what the general rule
980 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
981 * of this API expect in practice. A network that must match all UIDs can still be
982 * expressed with a set ranging the entire set of possible UIDs.
983 * <p>
984 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900985 * the UIDs in this list, and it is their default network. Apps in this list that wish to
986 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
987 * member is null, then the network is not restricted by app UID. If it's an empty list, then
988 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900989 * As a special exception, the app managing this network (as identified by its UID stored in
990 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
991 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
992 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900993 * <p>
994 * Please note that in principle a single app can be associated with multiple UIDs because
995 * each app will have a different UID when it's run as a different (macro-)user. A single
996 * macro user can only have a single active VPN app at any given time however.
997 * <p>
998 * Also please be aware this class does not try to enforce any normalization on this. Callers
999 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1000 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1001 * their own (like requiring sortedness or no overlap) they need to enforce it
1002 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1003 * or overlapping ranges are present.
1004 *
1005 * @hide
1006 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001007 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001008
1009 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001010 * Convenience method to set the UIDs this network applies to to a single UID.
1011 * @hide
1012 */
1013 public NetworkCapabilities setSingleUid(int uid) {
1014 final ArraySet<UidRange> identity = new ArraySet<>(1);
1015 identity.add(new UidRange(uid, uid));
1016 setUids(identity);
1017 return this;
1018 }
1019
1020 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001021 * Set the list of UIDs this network applies to.
1022 * This makes a copy of the set so that callers can't modify it after the call.
1023 * @hide
1024 */
1025 public NetworkCapabilities setUids(Set<UidRange> uids) {
1026 if (null == uids) {
1027 mUids = null;
1028 } else {
1029 mUids = new ArraySet<>(uids);
1030 }
1031 return this;
1032 }
1033
1034 /**
1035 * Get the list of UIDs this network applies to.
1036 * This returns a copy of the set so that callers can't modify the original object.
1037 * @hide
1038 */
1039 public Set<UidRange> getUids() {
1040 return null == mUids ? null : new ArraySet<>(mUids);
1041 }
1042
1043 /**
1044 * Test whether this network applies to this UID.
1045 * @hide
1046 */
1047 public boolean appliesToUid(int uid) {
1048 if (null == mUids) return true;
1049 for (UidRange range : mUids) {
1050 if (range.contains(uid)) {
1051 return true;
1052 }
1053 }
1054 return false;
1055 }
1056
1057 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001058 * 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 +09001059 * <p>
1060 * This test only checks whether equal range objects are in both sets. It will
1061 * return false if the ranges are not exactly the same, even if the covered UIDs
1062 * are for an equivalent result.
1063 * <p>
1064 * Note that this method is not very optimized, which is fine as long as it's not used very
1065 * often.
1066 * <p>
1067 * nc is assumed nonnull.
1068 *
1069 * @hide
1070 */
1071 @VisibleForTesting
1072 public boolean equalsUids(NetworkCapabilities nc) {
1073 Set<UidRange> comparedUids = nc.mUids;
1074 if (null == comparedUids) return null == mUids;
1075 if (null == mUids) return false;
1076 // Make a copy so it can be mutated to check that all ranges in mUids
1077 // also are in uids.
1078 final Set<UidRange> uids = new ArraySet<>(mUids);
1079 for (UidRange range : comparedUids) {
1080 if (!uids.contains(range)) {
1081 return false;
1082 }
1083 uids.remove(range);
1084 }
1085 return uids.isEmpty();
1086 }
1087
1088 /**
1089 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1090 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001091 * This method is called on the NetworkCapabilities embedded in a request with the
1092 * capabilities of an available network. It checks whether all the UIDs from this listen
1093 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1094 * in the passed nc (representing the UIDs that this network is available to).
1095 * <p>
1096 * As a special exception, the UID that created the passed network (as represented by its
1097 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1098 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1099 * can see its own network when it listens for it.
1100 * <p>
1101 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001102 * @see #appliesToUid
1103 * @hide
1104 */
1105 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001106 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001107 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001108 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001109 if (!nc.appliesToUidRange(requiredRange)) {
1110 return false;
1111 }
1112 }
1113 return true;
1114 }
1115
1116 /**
1117 * Returns whether this network applies to the passed ranges.
1118 * This assumes that to apply, the passed range has to be entirely contained
1119 * within one of the ranges this network applies to. If the ranges are not normalized,
1120 * this method may return false even though all required UIDs are covered because no
1121 * single range contained them all.
1122 * @hide
1123 */
1124 @VisibleForTesting
1125 public boolean appliesToUidRange(UidRange requiredRange) {
1126 if (null == mUids) return true;
1127 for (UidRange uidRange : mUids) {
1128 if (uidRange.containsRange(requiredRange)) {
1129 return true;
1130 }
1131 }
1132 return false;
1133 }
1134
1135 /**
1136 * Combine the UIDs this network currently applies to with the UIDs the passed
1137 * NetworkCapabilities apply to.
1138 * nc is assumed nonnull.
1139 */
1140 private void combineUids(NetworkCapabilities nc) {
1141 if (null == nc.mUids || null == mUids) {
1142 mUids = null;
1143 return;
1144 }
1145 mUids.addAll(nc.mUids);
1146 }
1147
Chalard Jeanb03a6222018-04-11 21:09:10 +09001148
1149 /**
1150 * The SSID of the network, or null if not applicable or unknown.
1151 * <p>
1152 * This is filled in by wifi code.
1153 * @hide
1154 */
1155 private String mSSID;
1156
1157 /**
1158 * Sets the SSID of this network.
1159 * @hide
1160 */
1161 public NetworkCapabilities setSSID(String ssid) {
1162 mSSID = ssid;
1163 return this;
1164 }
1165
1166 /**
1167 * Gets the SSID of this network, or null if none or unknown.
1168 * @hide
1169 */
1170 public String getSSID() {
1171 return mSSID;
1172 }
1173
1174 /**
1175 * Tests if the SSID of this network is the same as the SSID of the passed network.
1176 * @hide
1177 */
1178 public boolean equalsSSID(NetworkCapabilities nc) {
1179 return Objects.equals(mSSID, nc.mSSID);
1180 }
1181
1182 /**
1183 * Check if the SSID requirements of this object are matched by the passed object.
1184 * @hide
1185 */
1186 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1187 return mSSID == null || mSSID.equals(nc.mSSID);
1188 }
1189
1190 /**
1191 * Combine SSIDs of the capabilities.
1192 * <p>
1193 * This is only legal if either the SSID of this object is null, or both SSIDs are
1194 * equal.
1195 * @hide
1196 */
1197 private void combineSSIDs(NetworkCapabilities nc) {
1198 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1199 throw new IllegalStateException("Can't combine two SSIDs");
1200 }
1201 setSSID(nc.mSSID);
1202 }
1203
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001204 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001205 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001206 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001207 */
1208 public void combineCapabilities(NetworkCapabilities nc) {
1209 combineNetCapabilities(nc);
1210 combineTransportTypes(nc);
1211 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001212 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001213 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001214 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001215 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001216 }
1217
1218 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001219 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1220 *
1221 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1222 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1223 * bandwidth, signal strength, or validation / captive portal status.
1224 *
1225 * @hide
1226 */
1227 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001228 return (nc != null
1229 && satisfiedByNetCapabilities(nc, onlyImmutable)
1230 && satisfiedByTransportTypes(nc)
1231 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1232 && satisfiedBySpecifier(nc)
1233 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001234 && (onlyImmutable || satisfiedByUids(nc))
1235 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001236 }
1237
1238 /**
1239 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1240 *
1241 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1242 *
1243 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001244 */
1245 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001246 return satisfiedByNetworkCapabilities(nc, false);
1247 }
1248
1249 /**
1250 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1251 *
1252 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1253 *
1254 * @hide
1255 */
1256 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1257 return satisfiedByNetworkCapabilities(nc, true);
1258 }
1259
1260 /**
1261 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001262 * {@code NetworkCapabilities} and return a String describing any difference.
1263 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001264 *
1265 * @hide
1266 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001267 public String describeImmutableDifferences(NetworkCapabilities that) {
1268 if (that == null) {
1269 return "other NetworkCapabilities was null";
1270 }
1271
1272 StringJoiner joiner = new StringJoiner(", ");
1273
Hugo Benichieae7a222017-07-25 11:40:56 +09001274 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1275 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001276 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001277 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1278 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1279 if (oldImmutableCapabilities != newImmutableCapabilities) {
1280 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1281 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1282 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1283 }
1284
1285 if (!equalsSpecifier(that)) {
1286 NetworkSpecifier before = this.getNetworkSpecifier();
1287 NetworkSpecifier after = that.getNetworkSpecifier();
1288 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1289 }
1290
1291 if (!equalsTransportTypes(that)) {
1292 String before = transportNamesOf(this.getTransportTypes());
1293 String after = transportNamesOf(that.getTransportTypes());
1294 joiner.add(String.format("transports changed: %s -> %s", before, after));
1295 }
1296
1297 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001298 }
1299
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001300 /**
1301 * Checks that our requestable capabilities are the same as those of the given
1302 * {@code NetworkCapabilities}.
1303 *
1304 * @hide
1305 */
1306 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1307 if (nc == null) return false;
1308 return (equalsNetCapabilitiesRequestable(nc) &&
1309 equalsTransportTypes(nc) &&
1310 equalsSpecifier(nc));
1311 }
1312
Robert Greenwalt1448f052014-04-08 13:41:39 -07001313 @Override
1314 public boolean equals(Object obj) {
1315 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001316 NetworkCapabilities that = (NetworkCapabilities) obj;
1317 return (equalsNetCapabilities(that)
1318 && equalsTransportTypes(that)
1319 && equalsLinkBandwidths(that)
1320 && equalsSignalStrength(that)
1321 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001322 && equalsUids(that)
1323 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001324 }
1325
1326 @Override
1327 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001328 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001329 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001330 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1331 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1332 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1333 + ((int) (mTransportTypes >> 32) * 13)
1334 + (mLinkUpBandwidthKbps * 17)
1335 + (mLinkDownBandwidthKbps * 19)
1336 + Objects.hashCode(mNetworkSpecifier) * 23
1337 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001338 + Objects.hashCode(mUids) * 31
1339 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001340 }
1341
Wink Saville4e2dea72014-09-20 11:04:03 -07001342 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001343 public int describeContents() {
1344 return 0;
1345 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001346 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001347 public void writeToParcel(Parcel dest, int flags) {
1348 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001349 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001350 dest.writeLong(mTransportTypes);
1351 dest.writeInt(mLinkUpBandwidthKbps);
1352 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001353 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001354 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001355 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001356 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001357 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001358
Robert Greenwalt1448f052014-04-08 13:41:39 -07001359 public static final Creator<NetworkCapabilities> CREATOR =
1360 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001361 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001362 public NetworkCapabilities createFromParcel(Parcel in) {
1363 NetworkCapabilities netCap = new NetworkCapabilities();
1364
1365 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001366 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001367 netCap.mTransportTypes = in.readLong();
1368 netCap.mLinkUpBandwidthKbps = in.readInt();
1369 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001370 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001371 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001372 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1373 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001374 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001375 return netCap;
1376 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001377 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 public NetworkCapabilities[] newArray(int size) {
1379 return new NetworkCapabilities[size];
1380 }
1381 };
1382
Wink Saville4e2dea72014-09-20 11:04:03 -07001383 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001384 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001385 final StringBuilder sb = new StringBuilder("[");
1386 if (0 != mTransportTypes) {
1387 sb.append(" Transports: ");
1388 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1389 NetworkCapabilities::transportNameOf, "|");
1390 }
1391 if (0 != mNetworkCapabilities) {
1392 sb.append(" Capabilities: ");
1393 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1394 NetworkCapabilities::capabilityNameOf, "&");
1395 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001396 if (0 != mNetworkCapabilities) {
1397 sb.append(" Unwanted: ");
1398 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1399 NetworkCapabilities::capabilityNameOf, "&");
1400 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001401 if (mLinkUpBandwidthKbps > 0) {
1402 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1403 }
1404 if (mLinkDownBandwidthKbps > 0) {
1405 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1406 }
1407 if (mNetworkSpecifier != null) {
1408 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1409 }
1410 if (hasSignalStrength()) {
1411 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001412 }
1413
Chalard Jean07ace0f2018-02-26 19:00:45 +09001414 if (null != mUids) {
1415 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1416 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1417 } else {
1418 sb.append(" Uids: <").append(mUids).append(">");
1419 }
1420 }
1421 if (mEstablishingVpnAppUid != INVALID_UID) {
1422 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1423 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001424
Chalard Jeanb03a6222018-04-11 21:09:10 +09001425 if (null != mSSID) {
1426 sb.append(" SSID: ").append(mSSID);
1427 }
1428
Chalard Jean07ace0f2018-02-26 19:00:45 +09001429 sb.append("]");
1430 return sb.toString();
1431 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001432
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001433
Chalard Jean07ace0f2018-02-26 19:00:45 +09001434 private interface NameOf {
1435 String nameOf(int value);
1436 }
1437 /**
1438 * @hide
1439 */
1440 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1441 long bitMask, NameOf nameFetcher, String separator) {
1442 int bitPos = 0;
1443 boolean firstElementAdded = false;
1444 while (bitMask != 0) {
1445 if ((bitMask & 1) != 0) {
1446 if (firstElementAdded) {
1447 sb.append(separator);
1448 } else {
1449 firstElementAdded = true;
1450 }
1451 sb.append(nameFetcher.nameOf(bitPos));
1452 }
1453 bitMask >>= 1;
1454 ++bitPos;
1455 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001456 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001457
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001458 /** @hide */
1459 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1460 final long token = proto.start(fieldId);
1461
1462 for (int transport : getTransportTypes()) {
1463 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1464 }
1465
1466 for (int capability : getCapabilities()) {
1467 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1468 }
1469
1470 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1471 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1472
1473 if (mNetworkSpecifier != null) {
1474 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1475 }
1476
1477 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1478 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1479
1480 proto.end(token);
1481 }
1482
Hugo Benichi5df9d722016-04-25 17:16:35 +09001483 /**
1484 * @hide
1485 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001486 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001487 StringJoiner joiner = new StringJoiner("|");
1488 if (capabilities != null) {
1489 for (int c : capabilities) {
1490 joiner.add(capabilityNameOf(c));
1491 }
1492 }
1493 return joiner.toString();
1494 }
1495
1496 /**
1497 * @hide
1498 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001499 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001500 switch (capability) {
1501 case NET_CAPABILITY_MMS: return "MMS";
1502 case NET_CAPABILITY_SUPL: return "SUPL";
1503 case NET_CAPABILITY_DUN: return "DUN";
1504 case NET_CAPABILITY_FOTA: return "FOTA";
1505 case NET_CAPABILITY_IMS: return "IMS";
1506 case NET_CAPABILITY_CBS: return "CBS";
1507 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1508 case NET_CAPABILITY_IA: return "IA";
1509 case NET_CAPABILITY_RCS: return "RCS";
1510 case NET_CAPABILITY_XCAP: return "XCAP";
1511 case NET_CAPABILITY_EIMS: return "EIMS";
1512 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1513 case NET_CAPABILITY_INTERNET: return "INTERNET";
1514 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1515 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1516 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1517 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1518 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001519 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001520 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001521 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001522 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001523 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001524 default: return Integer.toString(capability);
1525 }
1526 }
1527
1528 /**
1529 * @hide
1530 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001531 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001532 StringJoiner joiner = new StringJoiner("|");
1533 if (types != null) {
1534 for (int t : types) {
1535 joiner.add(transportNameOf(t));
1536 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001537 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001538 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001539 }
1540
1541 /**
1542 * @hide
1543 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001544 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001545 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001546 return "UNKNOWN";
1547 }
1548 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001549 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001550
Jeff Sharkeyde570312017-10-24 21:25:50 -06001551 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001552 Preconditions.checkArgument(
1553 isValidTransport(transport), "Invalid TransportType " + transport);
1554 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001555
1556 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1557 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1558 }
1559
1560 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1561 Preconditions.checkArgument(isValidCapability(capability),
1562 "NetworkCapability " + capability + "out of range");
1563 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001564}