blob: e3a110706c453fa41e33023ea17e4ab68fafa0ec [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.
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000331 * {@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) |
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000342 (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>
403 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
404 * {@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
Robert Greenwalt1448f052014-04-08 13:41:39 -0700493 private void combineNetCapabilities(NetworkCapabilities nc) {
494 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800495 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700496 }
497
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900498 /**
499 * Convenience function that returns a human-readable description of the first mutable
500 * capability we find. Used to present an error message to apps that request mutable
501 * capabilities.
502 *
503 * @hide
504 */
505 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800506 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
507 & NON_REQUESTABLE_CAPABILITIES;
508
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900509 if (nonRequestable != 0) {
510 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900511 }
512 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900513 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900514 return null;
515 }
516
517 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800518 long requestedCapabilities = mNetworkCapabilities;
519 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
520 long providedCapabilities = nc.mNetworkCapabilities;
521
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900522 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800523 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
524 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900525 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800526 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
527 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700528 }
529
Robert Greenwalt06314e42014-10-29 14:04:06 -0700530 /** @hide */
531 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800532 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
533 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700534 }
535
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900536 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
537 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800538 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
539 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
540 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900541 }
542
Robert Greenwalt1448f052014-04-08 13:41:39 -0700543 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400544 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
545 * typically provided by restricted networks.
546 *
547 * TODO: consider:
548 * - Renaming it to guessRestrictedCapability and make it set the
549 * restricted capability bit in addition to clearing it.
550 * @hide
551 */
552 public void maybeMarkCapabilitiesRestricted() {
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000553 // Check if we have any capability that forces the network to be restricted.
554 final boolean forceRestrictedCapability =
555 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
556
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700557 // Verify there aren't any unrestricted capabilities. If there are we say
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000558 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700559 final boolean hasUnrestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000560 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700561
562 // Must have at least some restricted capabilities.
563 final boolean hasRestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000564 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700565
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000566 if (forceRestrictedCapability
567 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400568 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400569 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400570 }
571
572 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700573 * Representing the transport type. Apps should generally not care about transport. A
574 * request for a fast internet connection could be satisfied by a number of different
575 * transports. If any are specified here it will be satisfied a Network that matches
576 * any of them. If a caller doesn't care about the transport it should not specify any.
577 */
578 private long mTransportTypes;
579
Jeff Sharkeyde570312017-10-24 21:25:50 -0600580 /** @hide */
581 @Retention(RetentionPolicy.SOURCE)
582 @IntDef(prefix = { "TRANSPORT_" }, value = {
583 TRANSPORT_CELLULAR,
584 TRANSPORT_WIFI,
585 TRANSPORT_BLUETOOTH,
586 TRANSPORT_ETHERNET,
587 TRANSPORT_VPN,
588 TRANSPORT_WIFI_AWARE,
589 TRANSPORT_LOWPAN,
590 })
591 public @interface Transport { }
592
Robert Greenwalt1448f052014-04-08 13:41:39 -0700593 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700594 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700595 */
596 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700597
598 /**
599 * Indicates this network uses a Wi-Fi transport.
600 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700601 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700602
603 /**
604 * Indicates this network uses a Bluetooth transport.
605 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700607
608 /**
609 * Indicates this network uses an Ethernet transport.
610 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700611 public static final int TRANSPORT_ETHERNET = 3;
612
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400613 /**
614 * Indicates this network uses a VPN transport.
615 */
616 public static final int TRANSPORT_VPN = 4;
617
Etan Cohen305ea282016-06-20 09:27:12 -0700618 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700619 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700620 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700621 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700622
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700623 /**
624 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700625 */
626 public static final int TRANSPORT_LOWPAN = 6;
627
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900628 /** @hide */
629 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
630 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700631 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700632
Hugo Benichi16f0a942017-06-20 14:07:59 +0900633 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600634 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900635 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
636 }
637
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900638 private static final String[] TRANSPORT_NAMES = {
639 "CELLULAR",
640 "WIFI",
641 "BLUETOOTH",
642 "ETHERNET",
643 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700644 "WIFI_AWARE",
645 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900646 };
647
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700648 /**
649 * Adds the given transport type to this {@code NetworkCapability} instance.
650 * Multiple transports may be applied sequentially. Note that when searching
651 * for a network to satisfy a request, any listed in the request will satisfy the request.
652 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
653 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
654 * to be selected. This is logically different than
655 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
656 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600657 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900658 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700659 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700660 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600661 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900662 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700663 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700664 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700665 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700666 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700667
668 /**
669 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
670 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600671 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900672 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700673 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600675 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900676 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700677 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700678 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700679 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700680 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700681
682 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600683 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
684 * instance.
685 *
686 * @hide
687 */
688 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
689 if (value) {
690 addTransportType(transportType);
691 } else {
692 removeTransportType(transportType);
693 }
694 return this;
695 }
696
697 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700698 * Gets all the transports set on this {@code NetworkCapability} instance.
699 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600700 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700701 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700702 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600703 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600704 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900705 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700706 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700707
708 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600709 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700710 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600711 *
712 * @hide
713 */
714 public void setTransportTypes(@Transport int[] transportTypes) {
715 mTransportTypes = BitUtils.packBits(transportTypes);
716 }
717
718 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700719 * Tests for the presence of a transport on this instance.
720 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600721 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700722 * @return {@code true} if set on this instance.
723 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600724 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900725 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700726 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700727
728 private void combineTransportTypes(NetworkCapabilities nc) {
729 this.mTransportTypes |= nc.mTransportTypes;
730 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900731
Robert Greenwalt1448f052014-04-08 13:41:39 -0700732 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
733 return ((this.mTransportTypes == 0) ||
734 ((this.mTransportTypes & nc.mTransportTypes) != 0));
735 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900736
Robert Greenwalt06314e42014-10-29 14:04:06 -0700737 /** @hide */
738 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700739 return (nc.mTransportTypes == this.mTransportTypes);
740 }
741
742 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900743 * UID of the app that manages this network, or INVALID_UID if none/unknown.
744 *
745 * This field keeps track of the UID of the app that created this network and is in charge
746 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
747 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
748 * to create networks.
749 *
750 * Because this field is only used in the services side (and to avoid apps being able to
751 * set this to whatever they want), this field is not parcelled and will not be conserved
752 * across the IPC boundary.
753 * @hide
754 */
755 private int mEstablishingVpnAppUid = INVALID_UID;
756
757 /**
758 * Set the UID of the managing app.
759 * @hide
760 */
761 public void setEstablishingVpnAppUid(final int uid) {
762 mEstablishingVpnAppUid = uid;
763 }
764
765 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600766 * Value indicating that link bandwidth is unspecified.
767 * @hide
768 */
769 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
770
771 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
773 * for the first hop on the given transport. It is not measured, but may take into account
774 * link parameters (Radio technology, allocated channels, etc).
775 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600776 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
777 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700778
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700779 /**
780 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
781 * the estimated first hop transport bandwidth.
782 * <p>
783 * Note that when used to request a network, this specifies the minimum acceptable.
784 * When received as the state of an existing network this specifies the typical
785 * first hop bandwidth expected. This is never measured, but rather is inferred
786 * from technology type and other link parameters. It could be used to differentiate
787 * between very slow 1xRTT cellular links and other faster networks or even between
788 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
789 * fast backhauls and slow backhauls.
790 *
791 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700792 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700793 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600794 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700795 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600796 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700798
799 /**
800 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
801 * the estimated first hop transport bandwidth.
802 *
803 * @return The estimated first hop upstream (device to network) bandwidth.
804 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700805 public int getLinkUpstreamBandwidthKbps() {
806 return mLinkUpBandwidthKbps;
807 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700808
809 /**
810 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
811 * the estimated first hop transport bandwidth.
812 * <p>
813 * Note that when used to request a network, this specifies the minimum acceptable.
814 * When received as the state of an existing network this specifies the typical
815 * first hop bandwidth expected. This is never measured, but rather is inferred
816 * from technology type and other link parameters. It could be used to differentiate
817 * between very slow 1xRTT cellular links and other faster networks or even between
818 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
819 * fast backhauls and slow backhauls.
820 *
821 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700822 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700823 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600824 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700825 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600826 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700828
829 /**
830 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
831 * the estimated first hop transport bandwidth.
832 *
833 * @return The estimated first hop downstream (network to device) bandwidth.
834 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700835 public int getLinkDownstreamBandwidthKbps() {
836 return mLinkDownBandwidthKbps;
837 }
838
839 private void combineLinkBandwidths(NetworkCapabilities nc) {
840 this.mLinkUpBandwidthKbps =
841 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
842 this.mLinkDownBandwidthKbps =
843 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
844 }
845 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
846 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
847 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
848 }
849 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
850 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
851 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
852 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600853 /** @hide */
854 public static int minBandwidth(int a, int b) {
855 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
856 return b;
857 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
858 return a;
859 } else {
860 return Math.min(a, b);
861 }
862 }
863 /** @hide */
864 public static int maxBandwidth(int a, int b) {
865 return Math.max(a, b);
866 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700867
Etan Cohena7434272017-04-03 12:17:51 -0700868 private NetworkSpecifier mNetworkSpecifier = null;
869
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700870 /**
871 * Sets the optional bearer specific network specifier.
872 * This has no meaning if a single transport is also not specified, so calling
873 * this without a single transport set will generate an exception, as will
874 * subsequently adding or removing transports after this is set.
875 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700876 *
Etan Cohena7434272017-04-03 12:17:51 -0700877 * @param networkSpecifier A concrete, parcelable framework class that extends
878 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900879 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700880 * @hide
881 */
Etan Cohena7434272017-04-03 12:17:51 -0700882 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
883 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700884 throw new IllegalStateException("Must have a single transport specified to use " +
885 "setNetworkSpecifier");
886 }
Etan Cohena7434272017-04-03 12:17:51 -0700887
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700888 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700889
Pierre Imaic8419a82016-03-22 17:54:54 +0900890 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700891 }
892
893 /**
894 * Gets the optional bearer specific network specifier.
895 *
Etan Cohena7434272017-04-03 12:17:51 -0700896 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
897 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700898 * @hide
899 */
Etan Cohena7434272017-04-03 12:17:51 -0700900 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700901 return mNetworkSpecifier;
902 }
903
904 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700905 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700906 throw new IllegalStateException("Can't combine two networkSpecifiers");
907 }
Etan Cohena7434272017-04-03 12:17:51 -0700908 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700909 }
Etan Cohena7434272017-04-03 12:17:51 -0700910
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700911 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700912 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
913 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700914 }
Etan Cohena7434272017-04-03 12:17:51 -0700915
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700916 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700917 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700918 }
919
Robert Greenwalt1448f052014-04-08 13:41:39 -0700920 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900921 * Magic value that indicates no signal strength provided. A request specifying this value is
922 * always satisfied.
923 *
924 * @hide
925 */
926 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
927
928 /**
929 * Signal strength. This is a signed integer, and higher values indicate better signal.
930 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
931 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700932 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900933
934 /**
935 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
936 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900937 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900938 * <p>
939 * Note that when used to register a network callback, this specifies the minimum acceptable
940 * signal strength. When received as the state of an existing network it specifies the current
941 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
942 * effect when requesting a callback.
943 *
944 * @param signalStrength the bearer-specific signal strength.
945 * @hide
946 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600947 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900948 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600949 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900950 }
951
952 /**
953 * Returns {@code true} if this object specifies a signal strength.
954 *
955 * @hide
956 */
957 public boolean hasSignalStrength() {
958 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
959 }
960
961 /**
962 * Retrieves the signal strength.
963 *
964 * @return The bearer-specific signal strength.
965 * @hide
966 */
967 public int getSignalStrength() {
968 return mSignalStrength;
969 }
970
971 private void combineSignalStrength(NetworkCapabilities nc) {
972 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
973 }
974
975 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
976 return this.mSignalStrength <= nc.mSignalStrength;
977 }
978
979 private boolean equalsSignalStrength(NetworkCapabilities nc) {
980 return this.mSignalStrength == nc.mSignalStrength;
981 }
982
983 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900984 * List of UIDs this network applies to. No restriction if null.
985 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900986 * For networks, mUids represent the list of network this applies to, and null means this
987 * network applies to all UIDs.
988 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
989 * must be included in a network so that they match. As an exception to the general rule,
990 * a null mUids field for requests mean "no requirements" rather than what the general rule
991 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
992 * of this API expect in practice. A network that must match all UIDs can still be
993 * expressed with a set ranging the entire set of possible UIDs.
994 * <p>
995 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900996 * the UIDs in this list, and it is their default network. Apps in this list that wish to
997 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
998 * member is null, then the network is not restricted by app UID. If it's an empty list, then
999 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001000 * As a special exception, the app managing this network (as identified by its UID stored in
1001 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1002 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1003 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001004 * <p>
1005 * Please note that in principle a single app can be associated with multiple UIDs because
1006 * each app will have a different UID when it's run as a different (macro-)user. A single
1007 * macro user can only have a single active VPN app at any given time however.
1008 * <p>
1009 * Also please be aware this class does not try to enforce any normalization on this. Callers
1010 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1011 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1012 * their own (like requiring sortedness or no overlap) they need to enforce it
1013 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1014 * or overlapping ranges are present.
1015 *
1016 * @hide
1017 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001018 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001019
1020 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001021 * Convenience method to set the UIDs this network applies to to a single UID.
1022 * @hide
1023 */
1024 public NetworkCapabilities setSingleUid(int uid) {
1025 final ArraySet<UidRange> identity = new ArraySet<>(1);
1026 identity.add(new UidRange(uid, uid));
1027 setUids(identity);
1028 return this;
1029 }
1030
1031 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001032 * Set the list of UIDs this network applies to.
1033 * This makes a copy of the set so that callers can't modify it after the call.
1034 * @hide
1035 */
1036 public NetworkCapabilities setUids(Set<UidRange> uids) {
1037 if (null == uids) {
1038 mUids = null;
1039 } else {
1040 mUids = new ArraySet<>(uids);
1041 }
1042 return this;
1043 }
1044
1045 /**
1046 * Get the list of UIDs this network applies to.
1047 * This returns a copy of the set so that callers can't modify the original object.
1048 * @hide
1049 */
1050 public Set<UidRange> getUids() {
1051 return null == mUids ? null : new ArraySet<>(mUids);
1052 }
1053
1054 /**
1055 * Test whether this network applies to this UID.
1056 * @hide
1057 */
1058 public boolean appliesToUid(int uid) {
1059 if (null == mUids) return true;
1060 for (UidRange range : mUids) {
1061 if (range.contains(uid)) {
1062 return true;
1063 }
1064 }
1065 return false;
1066 }
1067
1068 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001069 * 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 +09001070 * <p>
1071 * This test only checks whether equal range objects are in both sets. It will
1072 * return false if the ranges are not exactly the same, even if the covered UIDs
1073 * are for an equivalent result.
1074 * <p>
1075 * Note that this method is not very optimized, which is fine as long as it's not used very
1076 * often.
1077 * <p>
1078 * nc is assumed nonnull.
1079 *
1080 * @hide
1081 */
1082 @VisibleForTesting
1083 public boolean equalsUids(NetworkCapabilities nc) {
1084 Set<UidRange> comparedUids = nc.mUids;
1085 if (null == comparedUids) return null == mUids;
1086 if (null == mUids) return false;
1087 // Make a copy so it can be mutated to check that all ranges in mUids
1088 // also are in uids.
1089 final Set<UidRange> uids = new ArraySet<>(mUids);
1090 for (UidRange range : comparedUids) {
1091 if (!uids.contains(range)) {
1092 return false;
1093 }
1094 uids.remove(range);
1095 }
1096 return uids.isEmpty();
1097 }
1098
1099 /**
1100 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1101 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001102 * This method is called on the NetworkCapabilities embedded in a request with the
1103 * capabilities of an available network. It checks whether all the UIDs from this listen
1104 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1105 * in the passed nc (representing the UIDs that this network is available to).
1106 * <p>
1107 * As a special exception, the UID that created the passed network (as represented by its
1108 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1109 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1110 * can see its own network when it listens for it.
1111 * <p>
1112 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001113 * @see #appliesToUid
1114 * @hide
1115 */
1116 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001117 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001118 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001119 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001120 if (!nc.appliesToUidRange(requiredRange)) {
1121 return false;
1122 }
1123 }
1124 return true;
1125 }
1126
1127 /**
1128 * Returns whether this network applies to the passed ranges.
1129 * This assumes that to apply, the passed range has to be entirely contained
1130 * within one of the ranges this network applies to. If the ranges are not normalized,
1131 * this method may return false even though all required UIDs are covered because no
1132 * single range contained them all.
1133 * @hide
1134 */
1135 @VisibleForTesting
1136 public boolean appliesToUidRange(UidRange requiredRange) {
1137 if (null == mUids) return true;
1138 for (UidRange uidRange : mUids) {
1139 if (uidRange.containsRange(requiredRange)) {
1140 return true;
1141 }
1142 }
1143 return false;
1144 }
1145
1146 /**
1147 * Combine the UIDs this network currently applies to with the UIDs the passed
1148 * NetworkCapabilities apply to.
1149 * nc is assumed nonnull.
1150 */
1151 private void combineUids(NetworkCapabilities nc) {
1152 if (null == nc.mUids || null == mUids) {
1153 mUids = null;
1154 return;
1155 }
1156 mUids.addAll(nc.mUids);
1157 }
1158
Chalard Jeanb03a6222018-04-11 21:09:10 +09001159
1160 /**
1161 * The SSID of the network, or null if not applicable or unknown.
1162 * <p>
1163 * This is filled in by wifi code.
1164 * @hide
1165 */
1166 private String mSSID;
1167
1168 /**
1169 * Sets the SSID of this network.
1170 * @hide
1171 */
1172 public NetworkCapabilities setSSID(String ssid) {
1173 mSSID = ssid;
1174 return this;
1175 }
1176
1177 /**
1178 * Gets the SSID of this network, or null if none or unknown.
1179 * @hide
1180 */
1181 public String getSSID() {
1182 return mSSID;
1183 }
1184
1185 /**
1186 * Tests if the SSID of this network is the same as the SSID of the passed network.
1187 * @hide
1188 */
1189 public boolean equalsSSID(NetworkCapabilities nc) {
1190 return Objects.equals(mSSID, nc.mSSID);
1191 }
1192
1193 /**
1194 * Check if the SSID requirements of this object are matched by the passed object.
1195 * @hide
1196 */
1197 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1198 return mSSID == null || mSSID.equals(nc.mSSID);
1199 }
1200
1201 /**
1202 * Combine SSIDs of the capabilities.
1203 * <p>
1204 * This is only legal if either the SSID of this object is null, or both SSIDs are
1205 * equal.
1206 * @hide
1207 */
1208 private void combineSSIDs(NetworkCapabilities nc) {
1209 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1210 throw new IllegalStateException("Can't combine two SSIDs");
1211 }
1212 setSSID(nc.mSSID);
1213 }
1214
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001215 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001216 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001217 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001218 */
1219 public void combineCapabilities(NetworkCapabilities nc) {
1220 combineNetCapabilities(nc);
1221 combineTransportTypes(nc);
1222 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001223 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001224 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001225 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001226 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001227 }
1228
1229 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001230 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1231 *
1232 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1233 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1234 * bandwidth, signal strength, or validation / captive portal status.
1235 *
1236 * @hide
1237 */
1238 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001239 return (nc != null
1240 && satisfiedByNetCapabilities(nc, onlyImmutable)
1241 && satisfiedByTransportTypes(nc)
1242 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1243 && satisfiedBySpecifier(nc)
1244 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001245 && (onlyImmutable || satisfiedByUids(nc))
1246 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001247 }
1248
1249 /**
1250 * Check if our 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
Robert Greenwalt1448f052014-04-08 13:41:39 -07001255 */
1256 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001257 return satisfiedByNetworkCapabilities(nc, false);
1258 }
1259
1260 /**
1261 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1262 *
1263 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1264 *
1265 * @hide
1266 */
1267 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1268 return satisfiedByNetworkCapabilities(nc, true);
1269 }
1270
1271 /**
1272 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001273 * {@code NetworkCapabilities} and return a String describing any difference.
1274 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001275 *
1276 * @hide
1277 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001278 public String describeImmutableDifferences(NetworkCapabilities that) {
1279 if (that == null) {
1280 return "other NetworkCapabilities was null";
1281 }
1282
1283 StringJoiner joiner = new StringJoiner(", ");
1284
Hugo Benichieae7a222017-07-25 11:40:56 +09001285 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1286 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001287 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001288 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1289 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1290 if (oldImmutableCapabilities != newImmutableCapabilities) {
1291 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1292 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1293 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1294 }
1295
1296 if (!equalsSpecifier(that)) {
1297 NetworkSpecifier before = this.getNetworkSpecifier();
1298 NetworkSpecifier after = that.getNetworkSpecifier();
1299 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1300 }
1301
1302 if (!equalsTransportTypes(that)) {
1303 String before = transportNamesOf(this.getTransportTypes());
1304 String after = transportNamesOf(that.getTransportTypes());
1305 joiner.add(String.format("transports changed: %s -> %s", before, after));
1306 }
1307
1308 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001309 }
1310
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001311 /**
1312 * Checks that our requestable capabilities are the same as those of the given
1313 * {@code NetworkCapabilities}.
1314 *
1315 * @hide
1316 */
1317 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1318 if (nc == null) return false;
1319 return (equalsNetCapabilitiesRequestable(nc) &&
1320 equalsTransportTypes(nc) &&
1321 equalsSpecifier(nc));
1322 }
1323
Robert Greenwalt1448f052014-04-08 13:41:39 -07001324 @Override
1325 public boolean equals(Object obj) {
1326 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001327 NetworkCapabilities that = (NetworkCapabilities) obj;
1328 return (equalsNetCapabilities(that)
1329 && equalsTransportTypes(that)
1330 && equalsLinkBandwidths(that)
1331 && equalsSignalStrength(that)
1332 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001333 && equalsUids(that)
1334 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001335 }
1336
1337 @Override
1338 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001339 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001340 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001341 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1342 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1343 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1344 + ((int) (mTransportTypes >> 32) * 13)
1345 + (mLinkUpBandwidthKbps * 17)
1346 + (mLinkDownBandwidthKbps * 19)
1347 + Objects.hashCode(mNetworkSpecifier) * 23
1348 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001349 + Objects.hashCode(mUids) * 31
1350 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001351 }
1352
Wink Saville4e2dea72014-09-20 11:04:03 -07001353 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001354 public int describeContents() {
1355 return 0;
1356 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001357 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001358 public void writeToParcel(Parcel dest, int flags) {
1359 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001360 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001361 dest.writeLong(mTransportTypes);
1362 dest.writeInt(mLinkUpBandwidthKbps);
1363 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001364 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001365 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001366 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001367 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001368 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001369
Robert Greenwalt1448f052014-04-08 13:41:39 -07001370 public static final Creator<NetworkCapabilities> CREATOR =
1371 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001372 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001373 public NetworkCapabilities createFromParcel(Parcel in) {
1374 NetworkCapabilities netCap = new NetworkCapabilities();
1375
1376 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001377 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 netCap.mTransportTypes = in.readLong();
1379 netCap.mLinkUpBandwidthKbps = in.readInt();
1380 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001381 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001382 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001383 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1384 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001385 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001386 return netCap;
1387 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001388 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001389 public NetworkCapabilities[] newArray(int size) {
1390 return new NetworkCapabilities[size];
1391 }
1392 };
1393
Wink Saville4e2dea72014-09-20 11:04:03 -07001394 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001395 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001396 final StringBuilder sb = new StringBuilder("[");
1397 if (0 != mTransportTypes) {
1398 sb.append(" Transports: ");
1399 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1400 NetworkCapabilities::transportNameOf, "|");
1401 }
1402 if (0 != mNetworkCapabilities) {
1403 sb.append(" Capabilities: ");
1404 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1405 NetworkCapabilities::capabilityNameOf, "&");
1406 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001407 if (0 != mNetworkCapabilities) {
1408 sb.append(" Unwanted: ");
1409 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1410 NetworkCapabilities::capabilityNameOf, "&");
1411 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001412 if (mLinkUpBandwidthKbps > 0) {
1413 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1414 }
1415 if (mLinkDownBandwidthKbps > 0) {
1416 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1417 }
1418 if (mNetworkSpecifier != null) {
1419 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1420 }
1421 if (hasSignalStrength()) {
1422 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001423 }
1424
Chalard Jean07ace0f2018-02-26 19:00:45 +09001425 if (null != mUids) {
1426 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1427 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1428 } else {
1429 sb.append(" Uids: <").append(mUids).append(">");
1430 }
1431 }
1432 if (mEstablishingVpnAppUid != INVALID_UID) {
1433 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1434 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001435
Chalard Jeanb03a6222018-04-11 21:09:10 +09001436 if (null != mSSID) {
1437 sb.append(" SSID: ").append(mSSID);
1438 }
1439
Chalard Jean07ace0f2018-02-26 19:00:45 +09001440 sb.append("]");
1441 return sb.toString();
1442 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001443
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001444
Chalard Jean07ace0f2018-02-26 19:00:45 +09001445 private interface NameOf {
1446 String nameOf(int value);
1447 }
1448 /**
1449 * @hide
1450 */
1451 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1452 long bitMask, NameOf nameFetcher, String separator) {
1453 int bitPos = 0;
1454 boolean firstElementAdded = false;
1455 while (bitMask != 0) {
1456 if ((bitMask & 1) != 0) {
1457 if (firstElementAdded) {
1458 sb.append(separator);
1459 } else {
1460 firstElementAdded = true;
1461 }
1462 sb.append(nameFetcher.nameOf(bitPos));
1463 }
1464 bitMask >>= 1;
1465 ++bitPos;
1466 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001467 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001468
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001469 /** @hide */
1470 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1471 final long token = proto.start(fieldId);
1472
1473 for (int transport : getTransportTypes()) {
1474 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1475 }
1476
1477 for (int capability : getCapabilities()) {
1478 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1479 }
1480
1481 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1482 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1483
1484 if (mNetworkSpecifier != null) {
1485 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1486 }
1487
1488 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1489 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1490
1491 proto.end(token);
1492 }
1493
Hugo Benichi5df9d722016-04-25 17:16:35 +09001494 /**
1495 * @hide
1496 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001497 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001498 StringJoiner joiner = new StringJoiner("|");
1499 if (capabilities != null) {
1500 for (int c : capabilities) {
1501 joiner.add(capabilityNameOf(c));
1502 }
1503 }
1504 return joiner.toString();
1505 }
1506
1507 /**
1508 * @hide
1509 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001510 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001511 switch (capability) {
1512 case NET_CAPABILITY_MMS: return "MMS";
1513 case NET_CAPABILITY_SUPL: return "SUPL";
1514 case NET_CAPABILITY_DUN: return "DUN";
1515 case NET_CAPABILITY_FOTA: return "FOTA";
1516 case NET_CAPABILITY_IMS: return "IMS";
1517 case NET_CAPABILITY_CBS: return "CBS";
1518 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1519 case NET_CAPABILITY_IA: return "IA";
1520 case NET_CAPABILITY_RCS: return "RCS";
1521 case NET_CAPABILITY_XCAP: return "XCAP";
1522 case NET_CAPABILITY_EIMS: return "EIMS";
1523 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1524 case NET_CAPABILITY_INTERNET: return "INTERNET";
1525 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1526 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1527 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1528 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1529 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001530 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001531 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001532 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001533 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001534 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001535 default: return Integer.toString(capability);
1536 }
1537 }
1538
1539 /**
1540 * @hide
1541 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001542 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001543 StringJoiner joiner = new StringJoiner("|");
1544 if (types != null) {
1545 for (int t : types) {
1546 joiner.add(transportNameOf(t));
1547 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001548 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001549 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001550 }
1551
1552 /**
1553 * @hide
1554 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001555 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001556 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001557 return "UNKNOWN";
1558 }
1559 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001560 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001561
Jeff Sharkeyde570312017-10-24 21:25:50 -06001562 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001563 Preconditions.checkArgument(
1564 isValidTransport(transport), "Invalid TransportType " + transport);
1565 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001566
1567 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1568 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1569 }
1570
1571 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1572 Preconditions.checkArgument(isValidCapability(capability),
1573 "NetworkCapability " + capability + "out of range");
1574 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001575}