blob: 72b4bfd57d96994cae371fb491db3c3d506f6462 [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;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060022import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070023import android.os.Parcel;
24import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090025import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080026import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070027
28import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090029import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090030import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070031
Jeff Sharkeyde570312017-10-24 21:25:50 -060032import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070034import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090035import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090036import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070037
38/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070039 * Representation of the capabilities of an active network. Instances are
40 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060041 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
42 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060043 * <p>
44 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
45 * network selection. Rather than indicate a need for Wi-Fi because an
46 * application needs high bandwidth and risk obsolescence when a new, fast
47 * network appears (like LTE), the application should specify it needs high
48 * bandwidth. Similarly if an application needs an unmetered network for a bulk
49 * transfer it can specify that rather than assuming all cellular based
50 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070051 */
52public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070053 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090054 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070055
Robert Greenwalt7569f182014-06-08 16:42:59 -070056 /**
57 * @hide
58 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070059 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090060 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090061 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070062 }
63
64 public NetworkCapabilities(NetworkCapabilities nc) {
65 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090066 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070067 }
68 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070069
70 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090071 * Completely clears the contents of this object, removing even the capabilities that are set
72 * by default when the object is constructed.
73 * @hide
74 */
75 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080076 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070077 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090078 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090079 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090080 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090081 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090082 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090083 }
84
85 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090086 * Set all contents of this object to the contents of a NetworkCapabilities.
87 * @hide
88 */
89 public void set(NetworkCapabilities nc) {
90 mNetworkCapabilities = nc.mNetworkCapabilities;
91 mTransportTypes = nc.mTransportTypes;
92 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
93 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
94 mNetworkSpecifier = nc.mNetworkSpecifier;
95 mSignalStrength = nc.mSignalStrength;
96 setUids(nc.mUids); // Will make the defensive copy
97 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
98 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
99 mSSID = nc.mSSID;
100 }
101
102 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700103 * Represents the network's capabilities. If any are specified they will be satisfied
104 * by any Network that matches all of them.
105 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900106 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700107
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800108 /**
109 * If any capabilities specified here they must not exist in the matching Network.
110 */
111 private long mUnwantedNetworkCapabilities;
112
Jeff Sharkeyde570312017-10-24 21:25:50 -0600113 /** @hide */
114 @Retention(RetentionPolicy.SOURCE)
115 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
116 NET_CAPABILITY_MMS,
117 NET_CAPABILITY_SUPL,
118 NET_CAPABILITY_DUN,
119 NET_CAPABILITY_FOTA,
120 NET_CAPABILITY_IMS,
121 NET_CAPABILITY_CBS,
122 NET_CAPABILITY_WIFI_P2P,
123 NET_CAPABILITY_IA,
124 NET_CAPABILITY_RCS,
125 NET_CAPABILITY_XCAP,
126 NET_CAPABILITY_EIMS,
127 NET_CAPABILITY_NOT_METERED,
128 NET_CAPABILITY_INTERNET,
129 NET_CAPABILITY_NOT_RESTRICTED,
130 NET_CAPABILITY_TRUSTED,
131 NET_CAPABILITY_NOT_VPN,
132 NET_CAPABILITY_VALIDATED,
133 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600134 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600135 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900136 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900137 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800138 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600139 })
140 public @interface NetCapability { }
141
Robert Greenwalt1448f052014-04-08 13:41:39 -0700142 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700143 * Indicates this is a network that has the ability to reach the
144 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700145 */
146 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700147
148 /**
149 * Indicates this is a network that has the ability to reach the carrier's
150 * SUPL server, used to retrieve GPS information.
151 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700152 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700153
154 /**
155 * Indicates this is a network that has the ability to reach the carrier's
156 * DUN or tethering gateway.
157 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700158 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700159
160 /**
161 * Indicates this is a network that has the ability to reach the carrier's
162 * FOTA portal, used for over the air updates.
163 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700164 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700165
166 /**
167 * Indicates this is a network that has the ability to reach the carrier's
168 * IMS servers, used for network registration and signaling.
169 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700170 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700171
172 /**
173 * Indicates this is a network that has the ability to reach the carrier's
174 * CBS servers, used for carrier specific services.
175 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700177
178 /**
179 * Indicates this is a network that has the ability to reach a Wi-Fi direct
180 * peer.
181 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700182 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700183
184 /**
185 * Indicates this is a network that has the ability to reach a carrier's
186 * Initial Attach servers.
187 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700188 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700189
190 /**
191 * Indicates this is a network that has the ability to reach a carrier's
192 * RCS servers, used for Rich Communication Services.
193 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195
196 /**
197 * Indicates this is a network that has the ability to reach a carrier's
198 * XCAP servers, used for configuration and control.
199 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700200 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700201
202 /**
203 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700204 * Emergency IMS servers or other services, used for network signaling
205 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700206 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700207 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700208
209 /**
210 * Indicates that this network is unmetered.
211 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700212 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700213
214 /**
215 * Indicates that this network should be able to reach the internet.
216 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700217 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700218
219 /**
220 * Indicates that this network is available for general use. If this is not set
221 * applications should not attempt to communicate on this network. Note that this
222 * is simply informative and not enforcement - enforcement is handled via other means.
223 * Set by default.
224 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700225 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
226
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700227 /**
228 * Indicates that the user has indicated implicit trust of this network. This
229 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
230 * BT device or a wifi the user asked to connect to. Untrusted networks
231 * are probably limited to unknown wifi AP. Set by default.
232 */
233 public static final int NET_CAPABILITY_TRUSTED = 14;
234
Paul Jensen76b610a2015-03-18 09:33:07 -0400235 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400236 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400237 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400238 */
239 public static final int NET_CAPABILITY_NOT_VPN = 15;
240
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900241 /**
242 * Indicates that connectivity on this network was successfully validated. For example, for a
243 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
244 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900245 */
246 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700247
Paul Jensen3d194ea2015-06-16 14:27:36 -0400248 /**
249 * Indicates that this network was found to have a captive portal in place last time it was
250 * probed.
251 */
252 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
253
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900254 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600255 * Indicates that this network is not roaming.
256 */
257 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
258
259 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900260 * Indicates that this network is available for use by apps, and not a network that is being
261 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900262 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600263 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900264
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900265 /**
266 * Indicates that this network is not congested.
267 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600268 * When a network is congested, applications should defer network traffic
269 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900270 */
271 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
272
Chalard Jean804b8fb2018-01-30 22:41:41 +0900273 /**
274 * Indicates that this network is not currently suspended.
275 * <p>
276 * When a network is suspended, the network's IP addresses and any connections
277 * established on the network remain valid, but the network is temporarily unable
278 * to transfer data. This can happen, for example, if a cellular network experiences
279 * a temporary loss of signal, such as when driving through a tunnel, etc.
280 * A network with this capability is not suspended, so is expected to be able to
281 * transfer data.
282 */
283 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
284
Pavel Maltsev43403202018-01-30 17:19:44 -0800285 /**
286 * Indicates that traffic that goes through this network is paid by oem. For example,
287 * this network can be used by system apps to upload telemetry data.
288 * @hide
289 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700290 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800291 public static final int NET_CAPABILITY_OEM_PAID = 22;
292
Robert Greenwalt1448f052014-04-08 13:41:39 -0700293 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800294 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700295
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700296 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900297 * Network capabilities that are expected to be mutable, i.e., can change while a particular
298 * network is connected.
299 */
300 private static final long MUTABLE_CAPABILITIES =
301 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
302 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900303 (1 << NET_CAPABILITY_TRUSTED)
304 | (1 << NET_CAPABILITY_VALIDATED)
305 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
306 | (1 << NET_CAPABILITY_NOT_ROAMING)
307 | (1 << NET_CAPABILITY_FOREGROUND)
308 | (1 << NET_CAPABILITY_NOT_CONGESTED)
309 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900310
311 /**
312 * Network capabilities that are not allowed in NetworkRequests. This exists because the
313 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
314 * capability's presence cannot be known in advance. If such a capability is requested, then we
315 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
316 * get immediately torn down because they do not have the requested capability.
317 */
318 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900319 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900320
321 /**
322 * Capabilities that are set by default when the object is constructed.
323 */
324 private static final long DEFAULT_CAPABILITIES =
325 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
326 (1 << NET_CAPABILITY_TRUSTED) |
327 (1 << NET_CAPABILITY_NOT_VPN);
328
329 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400330 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800331 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400332 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700333 @VisibleForTesting
334 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400335 (1 << NET_CAPABILITY_CBS) |
336 (1 << NET_CAPABILITY_DUN) |
337 (1 << NET_CAPABILITY_EIMS) |
338 (1 << NET_CAPABILITY_FOTA) |
339 (1 << NET_CAPABILITY_IA) |
340 (1 << NET_CAPABILITY_IMS) |
341 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev4af91072018-03-07 14:33:22 -0800342 (1 << NET_CAPABILITY_XCAP);
343
344 /**
345 * Capabilities that force network to be restricted.
346 * {@see #maybeMarkCapabilitiesRestricted}.
347 */
348 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800349 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400350
351 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700352 * Capabilities that suggest that a network is unrestricted.
353 * {@see #maybeMarkCapabilitiesRestricted}.
354 */
355 @VisibleForTesting
356 /* package */ static final long UNRESTRICTED_CAPABILITIES =
357 (1 << NET_CAPABILITY_INTERNET) |
358 (1 << NET_CAPABILITY_MMS) |
359 (1 << NET_CAPABILITY_SUPL) |
360 (1 << NET_CAPABILITY_WIFI_P2P);
361
362 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700363 * Adds the given capability to this {@code NetworkCapability} instance.
364 * Multiple capabilities may be applied sequentially. Note that when searching
365 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800366 * <p>
367 * If the given capability was previously added to the list of unwanted capabilities
368 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700369 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600370 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900371 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700372 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700373 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600374 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800375 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700376 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800377 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700378 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700379 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700380
381 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800382 * Adds the given capability to the list of unwanted capabilities of this
383 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
384 * sequentially. Note that when searching for a network to satisfy a request, the network
385 * must not contain any capability from unwanted capability list.
386 * <p>
387 * If the capability was previously added to the list of required capabilities (for
388 * example, it was there by default or added using {@link #addCapability(int)} method), then
389 * it will be removed from the list of required capabilities as well.
390 *
391 * @see #addCapability(int)
392 * @hide
393 */
394 public void addUnwantedCapability(@NetCapability int capability) {
395 checkValidCapability(capability);
396 mUnwantedNetworkCapabilities |= 1 << capability;
397 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
398 }
399
400 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700401 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800402 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800403 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800404 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700405 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600406 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900407 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700408 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700409 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600410 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800411 checkValidCapability(capability);
412 final long mask = ~(1 << capability);
413 mNetworkCapabilities &= mask;
414 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700415 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700416 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700417
418 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600419 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
420 * instance.
421 *
422 * @hide
423 */
424 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
425 if (value) {
426 addCapability(capability);
427 } else {
428 removeCapability(capability);
429 }
430 return this;
431 }
432
433 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700434 * Gets all the capabilities set on this {@code NetworkCapability} instance.
435 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600436 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700437 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700438 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600439 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600440 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900441 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700442 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700443
444 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800445 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
446 *
447 * @return an array of unwanted capability values for this instance.
448 * @hide
449 */
450 public @NetCapability int[] getUnwantedCapabilities() {
451 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
452 }
453
454
455 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600456 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700457 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600458 *
459 * @hide
460 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800461 public void setCapabilities(@NetCapability int[] capabilities,
462 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600463 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800464 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600465 }
466
467 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800468 * @deprecated use {@link #setCapabilities(int[], int[])}
469 * @hide
470 */
471 @Deprecated
472 public void setCapabilities(@NetCapability int[] capabilities) {
473 setCapabilities(capabilities, new int[] {});
474 }
475
476 /**
477 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600479 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700480 * @return {@code true} if set on this instance.
481 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600482 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800483 return isValidCapability(capability)
484 && ((mNetworkCapabilities & (1 << capability)) != 0);
485 }
486
487 /** @hide */
488 public boolean hasUnwantedCapability(@NetCapability int capability) {
489 return isValidCapability(capability)
490 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700491 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700492
Pavel Maltseve18ef262018-03-07 11:13:04 -0800493 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700494 private void combineNetCapabilities(NetworkCapabilities nc) {
495 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800496 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700497 }
498
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900499 /**
500 * Convenience function that returns a human-readable description of the first mutable
501 * capability we find. Used to present an error message to apps that request mutable
502 * capabilities.
503 *
504 * @hide
505 */
506 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800507 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
508 & NON_REQUESTABLE_CAPABILITIES;
509
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900510 if (nonRequestable != 0) {
511 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900512 }
513 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900514 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900515 return null;
516 }
517
518 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800519 long requestedCapabilities = mNetworkCapabilities;
520 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
521 long providedCapabilities = nc.mNetworkCapabilities;
522
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900523 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800524 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
525 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900526 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800527 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
528 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700529 }
530
Robert Greenwalt06314e42014-10-29 14:04:06 -0700531 /** @hide */
532 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800533 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
534 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700535 }
536
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900537 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
538 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800539 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
540 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
541 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900542 }
543
Robert Greenwalt1448f052014-04-08 13:41:39 -0700544 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400545 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
546 * typically provided by restricted networks.
547 *
548 * TODO: consider:
549 * - Renaming it to guessRestrictedCapability and make it set the
550 * restricted capability bit in addition to clearing it.
551 * @hide
552 */
553 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800554 // Check if we have any capability that forces the network to be restricted.
555 final boolean forceRestrictedCapability =
556 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
557
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700558 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800559 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700560 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800561 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700562
563 // Must have at least some restricted capabilities.
564 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800565 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700566
Pavel Maltsev4af91072018-03-07 14:33:22 -0800567 if (forceRestrictedCapability
568 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400569 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400570 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400571 }
572
573 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700574 * Representing the transport type. Apps should generally not care about transport. A
575 * request for a fast internet connection could be satisfied by a number of different
576 * transports. If any are specified here it will be satisfied a Network that matches
577 * any of them. If a caller doesn't care about the transport it should not specify any.
578 */
579 private long mTransportTypes;
580
Jeff Sharkeyde570312017-10-24 21:25:50 -0600581 /** @hide */
582 @Retention(RetentionPolicy.SOURCE)
583 @IntDef(prefix = { "TRANSPORT_" }, value = {
584 TRANSPORT_CELLULAR,
585 TRANSPORT_WIFI,
586 TRANSPORT_BLUETOOTH,
587 TRANSPORT_ETHERNET,
588 TRANSPORT_VPN,
589 TRANSPORT_WIFI_AWARE,
590 TRANSPORT_LOWPAN,
591 })
592 public @interface Transport { }
593
Robert Greenwalt1448f052014-04-08 13:41:39 -0700594 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700595 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700596 */
597 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700598
599 /**
600 * Indicates this network uses a Wi-Fi transport.
601 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700602 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700603
604 /**
605 * Indicates this network uses a Bluetooth transport.
606 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700607 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700608
609 /**
610 * Indicates this network uses an Ethernet transport.
611 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 public static final int TRANSPORT_ETHERNET = 3;
613
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400614 /**
615 * Indicates this network uses a VPN transport.
616 */
617 public static final int TRANSPORT_VPN = 4;
618
Etan Cohen305ea282016-06-20 09:27:12 -0700619 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700620 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700621 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700622 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700623
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700624 /**
625 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700626 */
627 public static final int TRANSPORT_LOWPAN = 6;
628
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900629 /** @hide */
630 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
631 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700632 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700633
Hugo Benichi16f0a942017-06-20 14:07:59 +0900634 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600635 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900636 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
637 }
638
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900639 private static final String[] TRANSPORT_NAMES = {
640 "CELLULAR",
641 "WIFI",
642 "BLUETOOTH",
643 "ETHERNET",
644 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700645 "WIFI_AWARE",
646 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900647 };
648
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700649 /**
650 * Adds the given transport type to this {@code NetworkCapability} instance.
651 * Multiple transports may be applied sequentially. Note that when searching
652 * for a network to satisfy a request, any listed in the request will satisfy the request.
653 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
654 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
655 * to be selected. This is logically different than
656 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
657 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600658 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900659 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700660 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700661 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600662 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900663 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700664 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700665 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700666 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700667 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700668
669 /**
670 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
671 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600672 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900673 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700674 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700675 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600676 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900677 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700678 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700679 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700680 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700681 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700682
683 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600684 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
685 * instance.
686 *
687 * @hide
688 */
689 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
690 if (value) {
691 addTransportType(transportType);
692 } else {
693 removeTransportType(transportType);
694 }
695 return this;
696 }
697
698 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700699 * Gets all the transports set on this {@code NetworkCapability} instance.
700 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600701 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700702 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700703 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600704 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600705 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900706 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700707 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700708
709 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600710 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700711 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600712 *
713 * @hide
714 */
715 public void setTransportTypes(@Transport int[] transportTypes) {
716 mTransportTypes = BitUtils.packBits(transportTypes);
717 }
718
719 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700720 * Tests for the presence of a transport on this instance.
721 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600722 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700723 * @return {@code true} if set on this instance.
724 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600725 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900726 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700727 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700728
729 private void combineTransportTypes(NetworkCapabilities nc) {
730 this.mTransportTypes |= nc.mTransportTypes;
731 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900732
Robert Greenwalt1448f052014-04-08 13:41:39 -0700733 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
734 return ((this.mTransportTypes == 0) ||
735 ((this.mTransportTypes & nc.mTransportTypes) != 0));
736 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900737
Robert Greenwalt06314e42014-10-29 14:04:06 -0700738 /** @hide */
739 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700740 return (nc.mTransportTypes == this.mTransportTypes);
741 }
742
743 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900744 * UID of the app that manages this network, or INVALID_UID if none/unknown.
745 *
746 * This field keeps track of the UID of the app that created this network and is in charge
747 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
748 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
749 * to create networks.
750 *
751 * Because this field is only used in the services side (and to avoid apps being able to
752 * set this to whatever they want), this field is not parcelled and will not be conserved
753 * across the IPC boundary.
754 * @hide
755 */
756 private int mEstablishingVpnAppUid = INVALID_UID;
757
758 /**
759 * Set the UID of the managing app.
760 * @hide
761 */
762 public void setEstablishingVpnAppUid(final int uid) {
763 mEstablishingVpnAppUid = uid;
764 }
765
766 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600767 * Value indicating that link bandwidth is unspecified.
768 * @hide
769 */
770 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
771
772 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700773 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
774 * for the first hop on the given transport. It is not measured, but may take into account
775 * link parameters (Radio technology, allocated channels, etc).
776 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600777 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
778 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700779
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700780 /**
781 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
782 * the estimated first hop transport bandwidth.
783 * <p>
784 * Note that when used to request a network, this specifies the minimum acceptable.
785 * When received as the state of an existing network this specifies the typical
786 * first hop bandwidth expected. This is never measured, but rather is inferred
787 * from technology type and other link parameters. It could be used to differentiate
788 * between very slow 1xRTT cellular links and other faster networks or even between
789 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
790 * fast backhauls and slow backhauls.
791 *
792 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700793 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700794 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600795 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700796 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600797 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700798 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700799
800 /**
801 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
802 * the estimated first hop transport bandwidth.
803 *
804 * @return The estimated first hop upstream (device to network) bandwidth.
805 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700806 public int getLinkUpstreamBandwidthKbps() {
807 return mLinkUpBandwidthKbps;
808 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700809
810 /**
811 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
812 * the estimated first hop transport bandwidth.
813 * <p>
814 * Note that when used to request a network, this specifies the minimum acceptable.
815 * When received as the state of an existing network this specifies the typical
816 * first hop bandwidth expected. This is never measured, but rather is inferred
817 * from technology type and other link parameters. It could be used to differentiate
818 * between very slow 1xRTT cellular links and other faster networks or even between
819 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
820 * fast backhauls and slow backhauls.
821 *
822 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700823 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700824 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600825 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700826 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600827 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700828 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700829
830 /**
831 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
832 * the estimated first hop transport bandwidth.
833 *
834 * @return The estimated first hop downstream (network to device) bandwidth.
835 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700836 public int getLinkDownstreamBandwidthKbps() {
837 return mLinkDownBandwidthKbps;
838 }
839
840 private void combineLinkBandwidths(NetworkCapabilities nc) {
841 this.mLinkUpBandwidthKbps =
842 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
843 this.mLinkDownBandwidthKbps =
844 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
845 }
846 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
847 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
848 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
849 }
850 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
851 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
852 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
853 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600854 /** @hide */
855 public static int minBandwidth(int a, int b) {
856 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
857 return b;
858 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
859 return a;
860 } else {
861 return Math.min(a, b);
862 }
863 }
864 /** @hide */
865 public static int maxBandwidth(int a, int b) {
866 return Math.max(a, b);
867 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700868
Etan Cohena7434272017-04-03 12:17:51 -0700869 private NetworkSpecifier mNetworkSpecifier = null;
870
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700871 /**
872 * Sets the optional bearer specific network specifier.
873 * This has no meaning if a single transport is also not specified, so calling
874 * this without a single transport set will generate an exception, as will
875 * subsequently adding or removing transports after this is set.
876 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700877 *
Etan Cohena7434272017-04-03 12:17:51 -0700878 * @param networkSpecifier A concrete, parcelable framework class that extends
879 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900880 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700881 * @hide
882 */
Etan Cohena7434272017-04-03 12:17:51 -0700883 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
884 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700885 throw new IllegalStateException("Must have a single transport specified to use " +
886 "setNetworkSpecifier");
887 }
Etan Cohena7434272017-04-03 12:17:51 -0700888
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700889 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700890
Pierre Imaic8419a82016-03-22 17:54:54 +0900891 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700892 }
893
894 /**
895 * Gets the optional bearer specific network specifier.
896 *
Etan Cohena7434272017-04-03 12:17:51 -0700897 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
898 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700899 * @hide
900 */
Etan Cohena7434272017-04-03 12:17:51 -0700901 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700902 return mNetworkSpecifier;
903 }
904
905 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700906 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700907 throw new IllegalStateException("Can't combine two networkSpecifiers");
908 }
Etan Cohena7434272017-04-03 12:17:51 -0700909 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700910 }
Etan Cohena7434272017-04-03 12:17:51 -0700911
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700912 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700913 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
914 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700915 }
Etan Cohena7434272017-04-03 12:17:51 -0700916
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700917 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700918 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700919 }
920
Robert Greenwalt1448f052014-04-08 13:41:39 -0700921 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900922 * Magic value that indicates no signal strength provided. A request specifying this value is
923 * always satisfied.
924 *
925 * @hide
926 */
927 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
928
929 /**
930 * Signal strength. This is a signed integer, and higher values indicate better signal.
931 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
932 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700933 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900934
935 /**
936 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
937 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900938 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900939 * <p>
940 * Note that when used to register a network callback, this specifies the minimum acceptable
941 * signal strength. When received as the state of an existing network it specifies the current
942 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
943 * effect when requesting a callback.
944 *
945 * @param signalStrength the bearer-specific signal strength.
946 * @hide
947 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600948 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900949 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600950 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900951 }
952
953 /**
954 * Returns {@code true} if this object specifies a signal strength.
955 *
956 * @hide
957 */
958 public boolean hasSignalStrength() {
959 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
960 }
961
962 /**
963 * Retrieves the signal strength.
964 *
965 * @return The bearer-specific signal strength.
966 * @hide
967 */
968 public int getSignalStrength() {
969 return mSignalStrength;
970 }
971
972 private void combineSignalStrength(NetworkCapabilities nc) {
973 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
974 }
975
976 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
977 return this.mSignalStrength <= nc.mSignalStrength;
978 }
979
980 private boolean equalsSignalStrength(NetworkCapabilities nc) {
981 return this.mSignalStrength == nc.mSignalStrength;
982 }
983
984 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900985 * List of UIDs this network applies to. No restriction if null.
986 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900987 * For networks, mUids represent the list of network this applies to, and null means this
988 * network applies to all UIDs.
989 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
990 * must be included in a network so that they match. As an exception to the general rule,
991 * a null mUids field for requests mean "no requirements" rather than what the general rule
992 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
993 * of this API expect in practice. A network that must match all UIDs can still be
994 * expressed with a set ranging the entire set of possible UIDs.
995 * <p>
996 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900997 * the UIDs in this list, and it is their default network. Apps in this list that wish to
998 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
999 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1000 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001001 * As a special exception, the app managing this network (as identified by its UID stored in
1002 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1003 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1004 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001005 * <p>
1006 * Please note that in principle a single app can be associated with multiple UIDs because
1007 * each app will have a different UID when it's run as a different (macro-)user. A single
1008 * macro user can only have a single active VPN app at any given time however.
1009 * <p>
1010 * Also please be aware this class does not try to enforce any normalization on this. Callers
1011 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1012 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1013 * their own (like requiring sortedness or no overlap) they need to enforce it
1014 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1015 * or overlapping ranges are present.
1016 *
1017 * @hide
1018 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001019 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001020
1021 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001022 * Convenience method to set the UIDs this network applies to to a single UID.
1023 * @hide
1024 */
1025 public NetworkCapabilities setSingleUid(int uid) {
1026 final ArraySet<UidRange> identity = new ArraySet<>(1);
1027 identity.add(new UidRange(uid, uid));
1028 setUids(identity);
1029 return this;
1030 }
1031
1032 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001033 * Set the list of UIDs this network applies to.
1034 * This makes a copy of the set so that callers can't modify it after the call.
1035 * @hide
1036 */
1037 public NetworkCapabilities setUids(Set<UidRange> uids) {
1038 if (null == uids) {
1039 mUids = null;
1040 } else {
1041 mUids = new ArraySet<>(uids);
1042 }
1043 return this;
1044 }
1045
1046 /**
1047 * Get the list of UIDs this network applies to.
1048 * This returns a copy of the set so that callers can't modify the original object.
1049 * @hide
1050 */
1051 public Set<UidRange> getUids() {
1052 return null == mUids ? null : new ArraySet<>(mUids);
1053 }
1054
1055 /**
1056 * Test whether this network applies to this UID.
1057 * @hide
1058 */
1059 public boolean appliesToUid(int uid) {
1060 if (null == mUids) return true;
1061 for (UidRange range : mUids) {
1062 if (range.contains(uid)) {
1063 return true;
1064 }
1065 }
1066 return false;
1067 }
1068
1069 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001070 * 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 +09001071 * <p>
1072 * This test only checks whether equal range objects are in both sets. It will
1073 * return false if the ranges are not exactly the same, even if the covered UIDs
1074 * are for an equivalent result.
1075 * <p>
1076 * Note that this method is not very optimized, which is fine as long as it's not used very
1077 * often.
1078 * <p>
1079 * nc is assumed nonnull.
1080 *
1081 * @hide
1082 */
1083 @VisibleForTesting
1084 public boolean equalsUids(NetworkCapabilities nc) {
1085 Set<UidRange> comparedUids = nc.mUids;
1086 if (null == comparedUids) return null == mUids;
1087 if (null == mUids) return false;
1088 // Make a copy so it can be mutated to check that all ranges in mUids
1089 // also are in uids.
1090 final Set<UidRange> uids = new ArraySet<>(mUids);
1091 for (UidRange range : comparedUids) {
1092 if (!uids.contains(range)) {
1093 return false;
1094 }
1095 uids.remove(range);
1096 }
1097 return uids.isEmpty();
1098 }
1099
1100 /**
1101 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1102 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001103 * This method is called on the NetworkCapabilities embedded in a request with the
1104 * capabilities of an available network. It checks whether all the UIDs from this listen
1105 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1106 * in the passed nc (representing the UIDs that this network is available to).
1107 * <p>
1108 * As a special exception, the UID that created the passed network (as represented by its
1109 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1110 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1111 * can see its own network when it listens for it.
1112 * <p>
1113 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001114 * @see #appliesToUid
1115 * @hide
1116 */
1117 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001118 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001119 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001120 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001121 if (!nc.appliesToUidRange(requiredRange)) {
1122 return false;
1123 }
1124 }
1125 return true;
1126 }
1127
1128 /**
1129 * Returns whether this network applies to the passed ranges.
1130 * This assumes that to apply, the passed range has to be entirely contained
1131 * within one of the ranges this network applies to. If the ranges are not normalized,
1132 * this method may return false even though all required UIDs are covered because no
1133 * single range contained them all.
1134 * @hide
1135 */
1136 @VisibleForTesting
1137 public boolean appliesToUidRange(UidRange requiredRange) {
1138 if (null == mUids) return true;
1139 for (UidRange uidRange : mUids) {
1140 if (uidRange.containsRange(requiredRange)) {
1141 return true;
1142 }
1143 }
1144 return false;
1145 }
1146
1147 /**
1148 * Combine the UIDs this network currently applies to with the UIDs the passed
1149 * NetworkCapabilities apply to.
1150 * nc is assumed nonnull.
1151 */
1152 private void combineUids(NetworkCapabilities nc) {
1153 if (null == nc.mUids || null == mUids) {
1154 mUids = null;
1155 return;
1156 }
1157 mUids.addAll(nc.mUids);
1158 }
1159
Chalard Jeanb03a6222018-04-11 21:09:10 +09001160
1161 /**
1162 * The SSID of the network, or null if not applicable or unknown.
1163 * <p>
1164 * This is filled in by wifi code.
1165 * @hide
1166 */
1167 private String mSSID;
1168
1169 /**
1170 * Sets the SSID of this network.
1171 * @hide
1172 */
1173 public NetworkCapabilities setSSID(String ssid) {
1174 mSSID = ssid;
1175 return this;
1176 }
1177
1178 /**
1179 * Gets the SSID of this network, or null if none or unknown.
1180 * @hide
1181 */
1182 public String getSSID() {
1183 return mSSID;
1184 }
1185
1186 /**
1187 * Tests if the SSID of this network is the same as the SSID of the passed network.
1188 * @hide
1189 */
1190 public boolean equalsSSID(NetworkCapabilities nc) {
1191 return Objects.equals(mSSID, nc.mSSID);
1192 }
1193
1194 /**
1195 * Check if the SSID requirements of this object are matched by the passed object.
1196 * @hide
1197 */
1198 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1199 return mSSID == null || mSSID.equals(nc.mSSID);
1200 }
1201
1202 /**
1203 * Combine SSIDs of the capabilities.
1204 * <p>
1205 * This is only legal if either the SSID of this object is null, or both SSIDs are
1206 * equal.
1207 * @hide
1208 */
1209 private void combineSSIDs(NetworkCapabilities nc) {
1210 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1211 throw new IllegalStateException("Can't combine two SSIDs");
1212 }
1213 setSSID(nc.mSSID);
1214 }
1215
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001216 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001217 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1218 * <p>
1219 * Note that this method may break an invariant of having a particular capability in either
1220 * wanted or unwanted lists but never in both. Requests that have the same capability in
1221 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001222 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001223 */
1224 public void combineCapabilities(NetworkCapabilities nc) {
1225 combineNetCapabilities(nc);
1226 combineTransportTypes(nc);
1227 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001228 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001229 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001230 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001231 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001232 }
1233
1234 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001235 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1236 *
1237 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1238 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1239 * bandwidth, signal strength, or validation / captive portal status.
1240 *
1241 * @hide
1242 */
1243 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001244 return (nc != null
1245 && satisfiedByNetCapabilities(nc, onlyImmutable)
1246 && satisfiedByTransportTypes(nc)
1247 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1248 && satisfiedBySpecifier(nc)
1249 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001250 && (onlyImmutable || satisfiedByUids(nc))
1251 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001252 }
1253
1254 /**
1255 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1256 *
1257 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1258 *
1259 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001260 */
1261 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001262 return satisfiedByNetworkCapabilities(nc, false);
1263 }
1264
1265 /**
1266 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1267 *
1268 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1269 *
1270 * @hide
1271 */
1272 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1273 return satisfiedByNetworkCapabilities(nc, true);
1274 }
1275
1276 /**
1277 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001278 * {@code NetworkCapabilities} and return a String describing any difference.
1279 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001280 *
1281 * @hide
1282 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001283 public String describeImmutableDifferences(NetworkCapabilities that) {
1284 if (that == null) {
1285 return "other NetworkCapabilities was null";
1286 }
1287
1288 StringJoiner joiner = new StringJoiner(", ");
1289
Hugo Benichieae7a222017-07-25 11:40:56 +09001290 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1291 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001292 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001293 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1294 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1295 if (oldImmutableCapabilities != newImmutableCapabilities) {
1296 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1297 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1298 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1299 }
1300
1301 if (!equalsSpecifier(that)) {
1302 NetworkSpecifier before = this.getNetworkSpecifier();
1303 NetworkSpecifier after = that.getNetworkSpecifier();
1304 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1305 }
1306
1307 if (!equalsTransportTypes(that)) {
1308 String before = transportNamesOf(this.getTransportTypes());
1309 String after = transportNamesOf(that.getTransportTypes());
1310 joiner.add(String.format("transports changed: %s -> %s", before, after));
1311 }
1312
1313 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001314 }
1315
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001316 /**
1317 * Checks that our requestable capabilities are the same as those of the given
1318 * {@code NetworkCapabilities}.
1319 *
1320 * @hide
1321 */
1322 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1323 if (nc == null) return false;
1324 return (equalsNetCapabilitiesRequestable(nc) &&
1325 equalsTransportTypes(nc) &&
1326 equalsSpecifier(nc));
1327 }
1328
Robert Greenwalt1448f052014-04-08 13:41:39 -07001329 @Override
1330 public boolean equals(Object obj) {
1331 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001332 NetworkCapabilities that = (NetworkCapabilities) obj;
1333 return (equalsNetCapabilities(that)
1334 && equalsTransportTypes(that)
1335 && equalsLinkBandwidths(that)
1336 && equalsSignalStrength(that)
1337 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001338 && equalsUids(that)
1339 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001340 }
1341
1342 @Override
1343 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001344 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001345 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001346 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1347 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1348 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1349 + ((int) (mTransportTypes >> 32) * 13)
1350 + (mLinkUpBandwidthKbps * 17)
1351 + (mLinkDownBandwidthKbps * 19)
1352 + Objects.hashCode(mNetworkSpecifier) * 23
1353 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001354 + Objects.hashCode(mUids) * 31
1355 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001356 }
1357
Wink Saville4e2dea72014-09-20 11:04:03 -07001358 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001359 public int describeContents() {
1360 return 0;
1361 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001362 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001363 public void writeToParcel(Parcel dest, int flags) {
1364 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001365 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001366 dest.writeLong(mTransportTypes);
1367 dest.writeInt(mLinkUpBandwidthKbps);
1368 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001369 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001370 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001371 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001372 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001373 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001374
Robert Greenwalt1448f052014-04-08 13:41:39 -07001375 public static final Creator<NetworkCapabilities> CREATOR =
1376 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001377 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 public NetworkCapabilities createFromParcel(Parcel in) {
1379 NetworkCapabilities netCap = new NetworkCapabilities();
1380
1381 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001382 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001383 netCap.mTransportTypes = in.readLong();
1384 netCap.mLinkUpBandwidthKbps = in.readInt();
1385 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001386 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001387 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001388 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1389 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001390 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001391 return netCap;
1392 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001393 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001394 public NetworkCapabilities[] newArray(int size) {
1395 return new NetworkCapabilities[size];
1396 }
1397 };
1398
Wink Saville4e2dea72014-09-20 11:04:03 -07001399 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001400 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001401 final StringBuilder sb = new StringBuilder("[");
1402 if (0 != mTransportTypes) {
1403 sb.append(" Transports: ");
1404 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1405 NetworkCapabilities::transportNameOf, "|");
1406 }
1407 if (0 != mNetworkCapabilities) {
1408 sb.append(" Capabilities: ");
1409 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1410 NetworkCapabilities::capabilityNameOf, "&");
1411 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001412 if (0 != mNetworkCapabilities) {
1413 sb.append(" Unwanted: ");
1414 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1415 NetworkCapabilities::capabilityNameOf, "&");
1416 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001417 if (mLinkUpBandwidthKbps > 0) {
1418 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1419 }
1420 if (mLinkDownBandwidthKbps > 0) {
1421 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1422 }
1423 if (mNetworkSpecifier != null) {
1424 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1425 }
1426 if (hasSignalStrength()) {
1427 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001428 }
1429
Chalard Jean07ace0f2018-02-26 19:00:45 +09001430 if (null != mUids) {
1431 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1432 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1433 } else {
1434 sb.append(" Uids: <").append(mUids).append(">");
1435 }
1436 }
1437 if (mEstablishingVpnAppUid != INVALID_UID) {
1438 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1439 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001440
Chalard Jeanb03a6222018-04-11 21:09:10 +09001441 if (null != mSSID) {
1442 sb.append(" SSID: ").append(mSSID);
1443 }
1444
Chalard Jean07ace0f2018-02-26 19:00:45 +09001445 sb.append("]");
1446 return sb.toString();
1447 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001448
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001449
Chalard Jean07ace0f2018-02-26 19:00:45 +09001450 private interface NameOf {
1451 String nameOf(int value);
1452 }
1453 /**
1454 * @hide
1455 */
1456 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1457 long bitMask, NameOf nameFetcher, String separator) {
1458 int bitPos = 0;
1459 boolean firstElementAdded = false;
1460 while (bitMask != 0) {
1461 if ((bitMask & 1) != 0) {
1462 if (firstElementAdded) {
1463 sb.append(separator);
1464 } else {
1465 firstElementAdded = true;
1466 }
1467 sb.append(nameFetcher.nameOf(bitPos));
1468 }
1469 bitMask >>= 1;
1470 ++bitPos;
1471 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001472 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001473
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001474 /** @hide */
1475 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1476 final long token = proto.start(fieldId);
1477
1478 for (int transport : getTransportTypes()) {
1479 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1480 }
1481
1482 for (int capability : getCapabilities()) {
1483 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1484 }
1485
1486 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1487 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1488
1489 if (mNetworkSpecifier != null) {
1490 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1491 }
1492
1493 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1494 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1495
1496 proto.end(token);
1497 }
1498
Hugo Benichi5df9d722016-04-25 17:16:35 +09001499 /**
1500 * @hide
1501 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001502 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001503 StringJoiner joiner = new StringJoiner("|");
1504 if (capabilities != null) {
1505 for (int c : capabilities) {
1506 joiner.add(capabilityNameOf(c));
1507 }
1508 }
1509 return joiner.toString();
1510 }
1511
1512 /**
1513 * @hide
1514 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001515 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001516 switch (capability) {
1517 case NET_CAPABILITY_MMS: return "MMS";
1518 case NET_CAPABILITY_SUPL: return "SUPL";
1519 case NET_CAPABILITY_DUN: return "DUN";
1520 case NET_CAPABILITY_FOTA: return "FOTA";
1521 case NET_CAPABILITY_IMS: return "IMS";
1522 case NET_CAPABILITY_CBS: return "CBS";
1523 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1524 case NET_CAPABILITY_IA: return "IA";
1525 case NET_CAPABILITY_RCS: return "RCS";
1526 case NET_CAPABILITY_XCAP: return "XCAP";
1527 case NET_CAPABILITY_EIMS: return "EIMS";
1528 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1529 case NET_CAPABILITY_INTERNET: return "INTERNET";
1530 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1531 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1532 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1533 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1534 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001535 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001536 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001537 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001538 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001539 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001540 default: return Integer.toString(capability);
1541 }
1542 }
1543
1544 /**
1545 * @hide
1546 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001547 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001548 StringJoiner joiner = new StringJoiner("|");
1549 if (types != null) {
1550 for (int t : types) {
1551 joiner.add(transportNameOf(t));
1552 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001553 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001554 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001555 }
1556
1557 /**
1558 * @hide
1559 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001560 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001561 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001562 return "UNKNOWN";
1563 }
1564 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001565 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001566
Jeff Sharkeyde570312017-10-24 21:25:50 -06001567 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001568 Preconditions.checkArgument(
1569 isValidTransport(transport), "Invalid TransportType " + transport);
1570 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001571
1572 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1573 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1574 }
1575
1576 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1577 Preconditions.checkArgument(isValidCapability(capability),
1578 "NetworkCapability " + capability + "out of range");
1579 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001580}