blob: 74cd1433f574619122751b05a48602d6509e88df [file] [log] [blame]
Robert Greenwalt1448f052014-04-08 13:41:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Jeff Sharkeyde570312017-10-24 21:25:50 -060019import android.annotation.IntDef;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070020import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060021import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010022import android.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060023import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070024import android.os.Parcel;
25import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090026import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080027import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070028
29import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090030import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090031import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070032
Jeff Sharkeyde570312017-10-24 21:25:50 -060033import java.lang.annotation.Retention;
34import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070035import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090036import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090037import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070038
39/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070040 * Representation of the capabilities of an active network. Instances are
41 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060042 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
43 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060044 * <p>
45 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
46 * network selection. Rather than indicate a need for Wi-Fi because an
47 * application needs high bandwidth and risk obsolescence when a new, fast
48 * network appears (like LTE), the application should specify it needs high
49 * bandwidth. Similarly if an application needs an unmetered network for a bulk
50 * transfer it can specify that rather than assuming all cellular based
51 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070052 */
53public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070054 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090055 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070056
Robert Greenwalt7569f182014-06-08 16:42:59 -070057 /**
58 * @hide
59 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010060 @UnsupportedAppUsage
Robert Greenwalt01d004e2014-05-18 15:24:21 -070061 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090062 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090063 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070064 }
65
66 public NetworkCapabilities(NetworkCapabilities nc) {
67 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090068 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070069 }
70 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070071
72 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090073 * Completely clears the contents of this object, removing even the capabilities that are set
74 * by default when the object is constructed.
75 * @hide
76 */
77 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080078 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070079 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090081 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090082 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090083 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090084 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090085 }
86
87 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090088 * Set all contents of this object to the contents of a NetworkCapabilities.
89 * @hide
90 */
91 public void set(NetworkCapabilities nc) {
92 mNetworkCapabilities = nc.mNetworkCapabilities;
93 mTransportTypes = nc.mTransportTypes;
94 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
95 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
96 mNetworkSpecifier = nc.mNetworkSpecifier;
97 mSignalStrength = nc.mSignalStrength;
98 setUids(nc.mUids); // Will make the defensive copy
99 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
100 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
101 mSSID = nc.mSSID;
102 }
103
104 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700105 * Represents the network's capabilities. If any are specified they will be satisfied
106 * by any Network that matches all of them.
107 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100108 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900109 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700110
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800111 /**
112 * If any capabilities specified here they must not exist in the matching Network.
113 */
114 private long mUnwantedNetworkCapabilities;
115
Jeff Sharkeyde570312017-10-24 21:25:50 -0600116 /** @hide */
117 @Retention(RetentionPolicy.SOURCE)
118 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
119 NET_CAPABILITY_MMS,
120 NET_CAPABILITY_SUPL,
121 NET_CAPABILITY_DUN,
122 NET_CAPABILITY_FOTA,
123 NET_CAPABILITY_IMS,
124 NET_CAPABILITY_CBS,
125 NET_CAPABILITY_WIFI_P2P,
126 NET_CAPABILITY_IA,
127 NET_CAPABILITY_RCS,
128 NET_CAPABILITY_XCAP,
129 NET_CAPABILITY_EIMS,
130 NET_CAPABILITY_NOT_METERED,
131 NET_CAPABILITY_INTERNET,
132 NET_CAPABILITY_NOT_RESTRICTED,
133 NET_CAPABILITY_TRUSTED,
134 NET_CAPABILITY_NOT_VPN,
135 NET_CAPABILITY_VALIDATED,
136 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600137 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600138 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900139 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900140 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800141 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600142 })
143 public @interface NetCapability { }
144
Robert Greenwalt1448f052014-04-08 13:41:39 -0700145 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700146 * Indicates this is a network that has the ability to reach the
147 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700148 */
149 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150
151 /**
152 * Indicates this is a network that has the ability to reach the carrier's
153 * SUPL server, used to retrieve GPS information.
154 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700155 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700156
157 /**
158 * Indicates this is a network that has the ability to reach the carrier's
159 * DUN or tethering gateway.
160 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700161 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700162
163 /**
164 * Indicates this is a network that has the ability to reach the carrier's
165 * FOTA portal, used for over the air updates.
166 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700167 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700168
169 /**
170 * Indicates this is a network that has the ability to reach the carrier's
171 * IMS servers, used for network registration and signaling.
172 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174
175 /**
176 * Indicates this is a network that has the ability to reach the carrier's
177 * CBS servers, used for carrier specific services.
178 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach a Wi-Fi direct
183 * peer.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach a carrier's
189 * Initial Attach servers.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach a carrier's
195 * RCS servers, used for Rich Communication Services.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Indicates this is a network that has the ability to reach a carrier's
201 * XCAP servers, used for configuration and control.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700207 * Emergency IMS servers or other services, used for network signaling
208 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700209 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700210 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700211
212 /**
213 * Indicates that this network is unmetered.
214 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216
217 /**
218 * Indicates that this network should be able to reach the internet.
219 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700220 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700221
222 /**
223 * Indicates that this network is available for general use. If this is not set
224 * applications should not attempt to communicate on this network. Note that this
225 * is simply informative and not enforcement - enforcement is handled via other means.
226 * Set by default.
227 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700228 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
229
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700230 /**
231 * Indicates that the user has indicated implicit trust of this network. This
232 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
233 * BT device or a wifi the user asked to connect to. Untrusted networks
234 * are probably limited to unknown wifi AP. Set by default.
235 */
236 public static final int NET_CAPABILITY_TRUSTED = 14;
237
Paul Jensen76b610a2015-03-18 09:33:07 -0400238 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400239 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400240 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400241 */
242 public static final int NET_CAPABILITY_NOT_VPN = 15;
243
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900244 /**
245 * Indicates that connectivity on this network was successfully validated. For example, for a
246 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
247 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900248 */
249 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700250
Paul Jensen3d194ea2015-06-16 14:27:36 -0400251 /**
252 * Indicates that this network was found to have a captive portal in place last time it was
253 * probed.
254 */
255 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
256
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900257 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600258 * Indicates that this network is not roaming.
259 */
260 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
261
262 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900263 * Indicates that this network is available for use by apps, and not a network that is being
264 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900265 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600266 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900267
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900268 /**
269 * Indicates that this network is not congested.
270 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600271 * When a network is congested, applications should defer network traffic
272 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900273 */
274 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900276 /**
277 * Indicates that this network is not currently suspended.
278 * <p>
279 * When a network is suspended, the network's IP addresses and any connections
280 * established on the network remain valid, but the network is temporarily unable
281 * to transfer data. This can happen, for example, if a cellular network experiences
282 * a temporary loss of signal, such as when driving through a tunnel, etc.
283 * A network with this capability is not suspended, so is expected to be able to
284 * transfer data.
285 */
286 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
287
Pavel Maltsev43403202018-01-30 17:19:44 -0800288 /**
289 * Indicates that traffic that goes through this network is paid by oem. For example,
290 * this network can be used by system apps to upload telemetry data.
291 * @hide
292 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700293 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800294 public static final int NET_CAPABILITY_OEM_PAID = 22;
295
Robert Greenwalt1448f052014-04-08 13:41:39 -0700296 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800297 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700298
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700299 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900300 * Network capabilities that are expected to be mutable, i.e., can change while a particular
301 * network is connected.
302 */
303 private static final long MUTABLE_CAPABILITIES =
304 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
305 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900306 (1 << NET_CAPABILITY_TRUSTED)
307 | (1 << NET_CAPABILITY_VALIDATED)
308 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
309 | (1 << NET_CAPABILITY_NOT_ROAMING)
310 | (1 << NET_CAPABILITY_FOREGROUND)
311 | (1 << NET_CAPABILITY_NOT_CONGESTED)
312 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900313
314 /**
315 * Network capabilities that are not allowed in NetworkRequests. This exists because the
316 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
317 * capability's presence cannot be known in advance. If such a capability is requested, then we
318 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
319 * get immediately torn down because they do not have the requested capability.
320 */
321 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900322 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900323
324 /**
325 * Capabilities that are set by default when the object is constructed.
326 */
327 private static final long DEFAULT_CAPABILITIES =
328 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
329 (1 << NET_CAPABILITY_TRUSTED) |
330 (1 << NET_CAPABILITY_NOT_VPN);
331
332 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400333 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800334 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400335 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700336 @VisibleForTesting
337 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400338 (1 << NET_CAPABILITY_CBS) |
339 (1 << NET_CAPABILITY_DUN) |
340 (1 << NET_CAPABILITY_EIMS) |
341 (1 << NET_CAPABILITY_FOTA) |
342 (1 << NET_CAPABILITY_IA) |
343 (1 << NET_CAPABILITY_IMS) |
344 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev4af91072018-03-07 14:33:22 -0800345 (1 << NET_CAPABILITY_XCAP);
346
347 /**
348 * Capabilities that force network to be restricted.
349 * {@see #maybeMarkCapabilitiesRestricted}.
350 */
351 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800352 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400353
354 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700355 * Capabilities that suggest that a network is unrestricted.
356 * {@see #maybeMarkCapabilitiesRestricted}.
357 */
358 @VisibleForTesting
359 /* package */ static final long UNRESTRICTED_CAPABILITIES =
360 (1 << NET_CAPABILITY_INTERNET) |
361 (1 << NET_CAPABILITY_MMS) |
362 (1 << NET_CAPABILITY_SUPL) |
363 (1 << NET_CAPABILITY_WIFI_P2P);
364
365 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700366 * Adds the given capability to this {@code NetworkCapability} instance.
367 * Multiple capabilities may be applied sequentially. Note that when searching
368 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800369 * <p>
370 * If the given capability was previously added to the list of unwanted capabilities
371 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700372 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600373 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900374 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700375 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700376 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100377 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600378 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800379 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700380 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800381 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700382 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700383 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384
385 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800386 * Adds the given capability to the list of unwanted capabilities of this
387 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
388 * sequentially. Note that when searching for a network to satisfy a request, the network
389 * must not contain any capability from unwanted capability list.
390 * <p>
391 * If the capability was previously added to the list of required capabilities (for
392 * example, it was there by default or added using {@link #addCapability(int)} method), then
393 * it will be removed from the list of required capabilities as well.
394 *
395 * @see #addCapability(int)
396 * @hide
397 */
398 public void addUnwantedCapability(@NetCapability int capability) {
399 checkValidCapability(capability);
400 mUnwantedNetworkCapabilities |= 1 << capability;
401 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
402 }
403
404 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700405 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800406 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800407 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800408 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700409 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600410 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900411 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700412 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700413 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100414 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600415 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800416 checkValidCapability(capability);
417 final long mask = ~(1 << capability);
418 mNetworkCapabilities &= mask;
419 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700420 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700421 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700422
423 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600424 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
425 * instance.
426 *
427 * @hide
428 */
429 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
430 if (value) {
431 addCapability(capability);
432 } else {
433 removeCapability(capability);
434 }
435 return this;
436 }
437
438 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700439 * Gets all the capabilities set on this {@code NetworkCapability} instance.
440 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600441 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700442 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700443 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600444 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600445 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900446 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700447 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700448
449 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800450 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
451 *
452 * @return an array of unwanted capability values for this instance.
453 * @hide
454 */
455 public @NetCapability int[] getUnwantedCapabilities() {
456 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
457 }
458
459
460 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600461 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700462 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600463 *
464 * @hide
465 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800466 public void setCapabilities(@NetCapability int[] capabilities,
467 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600468 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800469 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600470 }
471
472 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800473 * @deprecated use {@link #setCapabilities(int[], int[])}
474 * @hide
475 */
476 @Deprecated
477 public void setCapabilities(@NetCapability int[] capabilities) {
478 setCapabilities(capabilities, new int[] {});
479 }
480
481 /**
482 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700483 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600484 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700485 * @return {@code true} if set on this instance.
486 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600487 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800488 return isValidCapability(capability)
489 && ((mNetworkCapabilities & (1 << capability)) != 0);
490 }
491
492 /** @hide */
493 public boolean hasUnwantedCapability(@NetCapability int capability) {
494 return isValidCapability(capability)
495 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700496 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700497
Pavel Maltseve18ef262018-03-07 11:13:04 -0800498 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700499 private void combineNetCapabilities(NetworkCapabilities nc) {
500 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800501 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700502 }
503
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900504 /**
505 * Convenience function that returns a human-readable description of the first mutable
506 * capability we find. Used to present an error message to apps that request mutable
507 * capabilities.
508 *
509 * @hide
510 */
511 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800512 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
513 & NON_REQUESTABLE_CAPABILITIES;
514
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900515 if (nonRequestable != 0) {
516 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900517 }
518 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900519 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900520 return null;
521 }
522
523 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800524 long requestedCapabilities = mNetworkCapabilities;
525 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
526 long providedCapabilities = nc.mNetworkCapabilities;
527
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900528 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800529 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
530 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900531 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800532 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
533 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700534 }
535
Robert Greenwalt06314e42014-10-29 14:04:06 -0700536 /** @hide */
537 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800538 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
539 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700540 }
541
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900542 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
543 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800544 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
545 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
546 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900547 }
548
Robert Greenwalt1448f052014-04-08 13:41:39 -0700549 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400550 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
551 * typically provided by restricted networks.
552 *
553 * TODO: consider:
554 * - Renaming it to guessRestrictedCapability and make it set the
555 * restricted capability bit in addition to clearing it.
556 * @hide
557 */
558 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800559 // Check if we have any capability that forces the network to be restricted.
560 final boolean forceRestrictedCapability =
561 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
562
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700563 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800564 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700565 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800566 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700567
568 // Must have at least some restricted capabilities.
569 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800570 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700571
Pavel Maltsev4af91072018-03-07 14:33:22 -0800572 if (forceRestrictedCapability
573 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400574 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400575 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400576 }
577
578 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700579 * Representing the transport type. Apps should generally not care about transport. A
580 * request for a fast internet connection could be satisfied by a number of different
581 * transports. If any are specified here it will be satisfied a Network that matches
582 * any of them. If a caller doesn't care about the transport it should not specify any.
583 */
584 private long mTransportTypes;
585
Jeff Sharkeyde570312017-10-24 21:25:50 -0600586 /** @hide */
587 @Retention(RetentionPolicy.SOURCE)
588 @IntDef(prefix = { "TRANSPORT_" }, value = {
589 TRANSPORT_CELLULAR,
590 TRANSPORT_WIFI,
591 TRANSPORT_BLUETOOTH,
592 TRANSPORT_ETHERNET,
593 TRANSPORT_VPN,
594 TRANSPORT_WIFI_AWARE,
595 TRANSPORT_LOWPAN,
596 })
597 public @interface Transport { }
598
Robert Greenwalt1448f052014-04-08 13:41:39 -0700599 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700600 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700601 */
602 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700603
604 /**
605 * Indicates this network uses a Wi-Fi transport.
606 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700607 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700608
609 /**
610 * Indicates this network uses a Bluetooth transport.
611 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700613
614 /**
615 * Indicates this network uses an Ethernet transport.
616 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700617 public static final int TRANSPORT_ETHERNET = 3;
618
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400619 /**
620 * Indicates this network uses a VPN transport.
621 */
622 public static final int TRANSPORT_VPN = 4;
623
Etan Cohen305ea282016-06-20 09:27:12 -0700624 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700625 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700626 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700627 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700628
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700629 /**
630 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700631 */
632 public static final int TRANSPORT_LOWPAN = 6;
633
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900634 /** @hide */
635 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
636 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700637 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700638
Hugo Benichi16f0a942017-06-20 14:07:59 +0900639 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600640 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900641 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
642 }
643
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900644 private static final String[] TRANSPORT_NAMES = {
645 "CELLULAR",
646 "WIFI",
647 "BLUETOOTH",
648 "ETHERNET",
649 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700650 "WIFI_AWARE",
651 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900652 };
653
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700654 /**
655 * Adds the given transport type to this {@code NetworkCapability} instance.
656 * Multiple transports may be applied sequentially. Note that when searching
657 * for a network to satisfy a request, any listed in the request will satisfy the request.
658 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
659 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
660 * to be selected. This is logically different than
661 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
662 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600663 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900664 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700665 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700666 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100667 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600668 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900669 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700670 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700671 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700672 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700673 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674
675 /**
676 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
677 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600678 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900679 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700680 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700681 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600682 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900683 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700684 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700685 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700686 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700687 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700688
689 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600690 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
691 * instance.
692 *
693 * @hide
694 */
695 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
696 if (value) {
697 addTransportType(transportType);
698 } else {
699 removeTransportType(transportType);
700 }
701 return this;
702 }
703
704 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700705 * Gets all the transports set on this {@code NetworkCapability} instance.
706 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600707 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700708 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700709 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600710 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900712 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700713 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700714
715 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600716 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700717 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600718 *
719 * @hide
720 */
721 public void setTransportTypes(@Transport int[] transportTypes) {
722 mTransportTypes = BitUtils.packBits(transportTypes);
723 }
724
725 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700726 * Tests for the presence of a transport on this instance.
727 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600728 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700729 * @return {@code true} if set on this instance.
730 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600731 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900732 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700733 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700734
735 private void combineTransportTypes(NetworkCapabilities nc) {
736 this.mTransportTypes |= nc.mTransportTypes;
737 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900738
Robert Greenwalt1448f052014-04-08 13:41:39 -0700739 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
740 return ((this.mTransportTypes == 0) ||
741 ((this.mTransportTypes & nc.mTransportTypes) != 0));
742 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900743
Robert Greenwalt06314e42014-10-29 14:04:06 -0700744 /** @hide */
745 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700746 return (nc.mTransportTypes == this.mTransportTypes);
747 }
748
749 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900750 * UID of the app that manages this network, or INVALID_UID if none/unknown.
751 *
752 * This field keeps track of the UID of the app that created this network and is in charge
753 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
754 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
755 * to create networks.
756 *
757 * Because this field is only used in the services side (and to avoid apps being able to
758 * set this to whatever they want), this field is not parcelled and will not be conserved
759 * across the IPC boundary.
760 * @hide
761 */
762 private int mEstablishingVpnAppUid = INVALID_UID;
763
764 /**
765 * Set the UID of the managing app.
766 * @hide
767 */
768 public void setEstablishingVpnAppUid(final int uid) {
769 mEstablishingVpnAppUid = uid;
770 }
771
772 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600773 * Value indicating that link bandwidth is unspecified.
774 * @hide
775 */
776 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
777
778 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700779 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
780 * for the first hop on the given transport. It is not measured, but may take into account
781 * link parameters (Radio technology, allocated channels, etc).
782 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600783 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
784 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700785
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700786 /**
787 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
788 * the estimated first hop transport bandwidth.
789 * <p>
790 * Note that when used to request a network, this specifies the minimum acceptable.
791 * When received as the state of an existing network this specifies the typical
792 * first hop bandwidth expected. This is never measured, but rather is inferred
793 * from technology type and other link parameters. It could be used to differentiate
794 * between very slow 1xRTT cellular links and other faster networks or even between
795 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
796 * fast backhauls and slow backhauls.
797 *
798 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700799 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700800 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600801 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700802 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600803 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700804 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700805
806 /**
807 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
808 * the estimated first hop transport bandwidth.
809 *
810 * @return The estimated first hop upstream (device to network) bandwidth.
811 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700812 public int getLinkUpstreamBandwidthKbps() {
813 return mLinkUpBandwidthKbps;
814 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700815
816 /**
817 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
818 * the estimated first hop transport bandwidth.
819 * <p>
820 * Note that when used to request a network, this specifies the minimum acceptable.
821 * When received as the state of an existing network this specifies the typical
822 * first hop bandwidth expected. This is never measured, but rather is inferred
823 * from technology type and other link parameters. It could be used to differentiate
824 * between very slow 1xRTT cellular links and other faster networks or even between
825 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
826 * fast backhauls and slow backhauls.
827 *
828 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700829 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700830 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600831 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700832 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600833 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700834 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700835
836 /**
837 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
838 * the estimated first hop transport bandwidth.
839 *
840 * @return The estimated first hop downstream (network to device) bandwidth.
841 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700842 public int getLinkDownstreamBandwidthKbps() {
843 return mLinkDownBandwidthKbps;
844 }
845
846 private void combineLinkBandwidths(NetworkCapabilities nc) {
847 this.mLinkUpBandwidthKbps =
848 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
849 this.mLinkDownBandwidthKbps =
850 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
851 }
852 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
853 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
854 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
855 }
856 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
857 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
858 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
859 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600860 /** @hide */
861 public static int minBandwidth(int a, int b) {
862 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
863 return b;
864 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
865 return a;
866 } else {
867 return Math.min(a, b);
868 }
869 }
870 /** @hide */
871 public static int maxBandwidth(int a, int b) {
872 return Math.max(a, b);
873 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700874
Etan Cohena7434272017-04-03 12:17:51 -0700875 private NetworkSpecifier mNetworkSpecifier = null;
876
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700877 /**
878 * Sets the optional bearer specific network specifier.
879 * This has no meaning if a single transport is also not specified, so calling
880 * this without a single transport set will generate an exception, as will
881 * subsequently adding or removing transports after this is set.
882 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700883 *
Etan Cohena7434272017-04-03 12:17:51 -0700884 * @param networkSpecifier A concrete, parcelable framework class that extends
885 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900886 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700887 * @hide
888 */
Etan Cohena7434272017-04-03 12:17:51 -0700889 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
890 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700891 throw new IllegalStateException("Must have a single transport specified to use " +
892 "setNetworkSpecifier");
893 }
Etan Cohena7434272017-04-03 12:17:51 -0700894
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700895 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700896
Pierre Imaic8419a82016-03-22 17:54:54 +0900897 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700898 }
899
900 /**
901 * Gets the optional bearer specific network specifier.
902 *
Etan Cohena7434272017-04-03 12:17:51 -0700903 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
904 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700905 * @hide
906 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100907 @UnsupportedAppUsage
Etan Cohena7434272017-04-03 12:17:51 -0700908 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700909 return mNetworkSpecifier;
910 }
911
912 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700913 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700914 throw new IllegalStateException("Can't combine two networkSpecifiers");
915 }
Etan Cohena7434272017-04-03 12:17:51 -0700916 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700917 }
Etan Cohena7434272017-04-03 12:17:51 -0700918
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700919 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700920 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
921 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700922 }
Etan Cohena7434272017-04-03 12:17:51 -0700923
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700924 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700925 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700926 }
927
Robert Greenwalt1448f052014-04-08 13:41:39 -0700928 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900929 * Magic value that indicates no signal strength provided. A request specifying this value is
930 * always satisfied.
931 *
932 * @hide
933 */
934 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
935
936 /**
937 * Signal strength. This is a signed integer, and higher values indicate better signal.
938 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
939 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100940 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700941 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900942
943 /**
944 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
945 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900946 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900947 * <p>
948 * Note that when used to register a network callback, this specifies the minimum acceptable
949 * signal strength. When received as the state of an existing network it specifies the current
950 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
951 * effect when requesting a callback.
952 *
953 * @param signalStrength the bearer-specific signal strength.
954 * @hide
955 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100956 @UnsupportedAppUsage
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600957 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900958 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600959 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900960 }
961
962 /**
963 * Returns {@code true} if this object specifies a signal strength.
964 *
965 * @hide
966 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100967 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900968 public boolean hasSignalStrength() {
969 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
970 }
971
972 /**
973 * Retrieves the signal strength.
974 *
975 * @return The bearer-specific signal strength.
976 * @hide
977 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100978 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900979 public int getSignalStrength() {
980 return mSignalStrength;
981 }
982
983 private void combineSignalStrength(NetworkCapabilities nc) {
984 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
985 }
986
987 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
988 return this.mSignalStrength <= nc.mSignalStrength;
989 }
990
991 private boolean equalsSignalStrength(NetworkCapabilities nc) {
992 return this.mSignalStrength == nc.mSignalStrength;
993 }
994
995 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900996 * List of UIDs this network applies to. No restriction if null.
997 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900998 * For networks, mUids represent the list of network this applies to, and null means this
999 * network applies to all UIDs.
1000 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1001 * must be included in a network so that they match. As an exception to the general rule,
1002 * a null mUids field for requests mean "no requirements" rather than what the general rule
1003 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1004 * of this API expect in practice. A network that must match all UIDs can still be
1005 * expressed with a set ranging the entire set of possible UIDs.
1006 * <p>
1007 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001008 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1009 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1010 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1011 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001012 * As a special exception, the app managing this network (as identified by its UID stored in
1013 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1014 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1015 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001016 * <p>
1017 * Please note that in principle a single app can be associated with multiple UIDs because
1018 * each app will have a different UID when it's run as a different (macro-)user. A single
1019 * macro user can only have a single active VPN app at any given time however.
1020 * <p>
1021 * Also please be aware this class does not try to enforce any normalization on this. Callers
1022 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1023 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1024 * their own (like requiring sortedness or no overlap) they need to enforce it
1025 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1026 * or overlapping ranges are present.
1027 *
1028 * @hide
1029 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001030 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001031
1032 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001033 * Convenience method to set the UIDs this network applies to to a single UID.
1034 * @hide
1035 */
1036 public NetworkCapabilities setSingleUid(int uid) {
1037 final ArraySet<UidRange> identity = new ArraySet<>(1);
1038 identity.add(new UidRange(uid, uid));
1039 setUids(identity);
1040 return this;
1041 }
1042
1043 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001044 * Set the list of UIDs this network applies to.
1045 * This makes a copy of the set so that callers can't modify it after the call.
1046 * @hide
1047 */
1048 public NetworkCapabilities setUids(Set<UidRange> uids) {
1049 if (null == uids) {
1050 mUids = null;
1051 } else {
1052 mUids = new ArraySet<>(uids);
1053 }
1054 return this;
1055 }
1056
1057 /**
1058 * Get the list of UIDs this network applies to.
1059 * This returns a copy of the set so that callers can't modify the original object.
1060 * @hide
1061 */
1062 public Set<UidRange> getUids() {
1063 return null == mUids ? null : new ArraySet<>(mUids);
1064 }
1065
1066 /**
1067 * Test whether this network applies to this UID.
1068 * @hide
1069 */
1070 public boolean appliesToUid(int uid) {
1071 if (null == mUids) return true;
1072 for (UidRange range : mUids) {
1073 if (range.contains(uid)) {
1074 return true;
1075 }
1076 }
1077 return false;
1078 }
1079
1080 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001081 * 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 +09001082 * <p>
1083 * This test only checks whether equal range objects are in both sets. It will
1084 * return false if the ranges are not exactly the same, even if the covered UIDs
1085 * are for an equivalent result.
1086 * <p>
1087 * Note that this method is not very optimized, which is fine as long as it's not used very
1088 * often.
1089 * <p>
1090 * nc is assumed nonnull.
1091 *
1092 * @hide
1093 */
1094 @VisibleForTesting
1095 public boolean equalsUids(NetworkCapabilities nc) {
1096 Set<UidRange> comparedUids = nc.mUids;
1097 if (null == comparedUids) return null == mUids;
1098 if (null == mUids) return false;
1099 // Make a copy so it can be mutated to check that all ranges in mUids
1100 // also are in uids.
1101 final Set<UidRange> uids = new ArraySet<>(mUids);
1102 for (UidRange range : comparedUids) {
1103 if (!uids.contains(range)) {
1104 return false;
1105 }
1106 uids.remove(range);
1107 }
1108 return uids.isEmpty();
1109 }
1110
1111 /**
1112 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1113 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001114 * This method is called on the NetworkCapabilities embedded in a request with the
1115 * capabilities of an available network. It checks whether all the UIDs from this listen
1116 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1117 * in the passed nc (representing the UIDs that this network is available to).
1118 * <p>
1119 * As a special exception, the UID that created the passed network (as represented by its
1120 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1121 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1122 * can see its own network when it listens for it.
1123 * <p>
1124 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001125 * @see #appliesToUid
1126 * @hide
1127 */
1128 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001129 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001130 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001131 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001132 if (!nc.appliesToUidRange(requiredRange)) {
1133 return false;
1134 }
1135 }
1136 return true;
1137 }
1138
1139 /**
1140 * Returns whether this network applies to the passed ranges.
1141 * This assumes that to apply, the passed range has to be entirely contained
1142 * within one of the ranges this network applies to. If the ranges are not normalized,
1143 * this method may return false even though all required UIDs are covered because no
1144 * single range contained them all.
1145 * @hide
1146 */
1147 @VisibleForTesting
1148 public boolean appliesToUidRange(UidRange requiredRange) {
1149 if (null == mUids) return true;
1150 for (UidRange uidRange : mUids) {
1151 if (uidRange.containsRange(requiredRange)) {
1152 return true;
1153 }
1154 }
1155 return false;
1156 }
1157
1158 /**
1159 * Combine the UIDs this network currently applies to with the UIDs the passed
1160 * NetworkCapabilities apply to.
1161 * nc is assumed nonnull.
1162 */
1163 private void combineUids(NetworkCapabilities nc) {
1164 if (null == nc.mUids || null == mUids) {
1165 mUids = null;
1166 return;
1167 }
1168 mUids.addAll(nc.mUids);
1169 }
1170
Chalard Jeanb03a6222018-04-11 21:09:10 +09001171
1172 /**
1173 * The SSID of the network, or null if not applicable or unknown.
1174 * <p>
1175 * This is filled in by wifi code.
1176 * @hide
1177 */
1178 private String mSSID;
1179
1180 /**
1181 * Sets the SSID of this network.
1182 * @hide
1183 */
1184 public NetworkCapabilities setSSID(String ssid) {
1185 mSSID = ssid;
1186 return this;
1187 }
1188
1189 /**
1190 * Gets the SSID of this network, or null if none or unknown.
1191 * @hide
1192 */
1193 public String getSSID() {
1194 return mSSID;
1195 }
1196
1197 /**
1198 * Tests if the SSID of this network is the same as the SSID of the passed network.
1199 * @hide
1200 */
1201 public boolean equalsSSID(NetworkCapabilities nc) {
1202 return Objects.equals(mSSID, nc.mSSID);
1203 }
1204
1205 /**
1206 * Check if the SSID requirements of this object are matched by the passed object.
1207 * @hide
1208 */
1209 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1210 return mSSID == null || mSSID.equals(nc.mSSID);
1211 }
1212
1213 /**
1214 * Combine SSIDs of the capabilities.
1215 * <p>
1216 * This is only legal if either the SSID of this object is null, or both SSIDs are
1217 * equal.
1218 * @hide
1219 */
1220 private void combineSSIDs(NetworkCapabilities nc) {
1221 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1222 throw new IllegalStateException("Can't combine two SSIDs");
1223 }
1224 setSSID(nc.mSSID);
1225 }
1226
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001227 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001228 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1229 * <p>
1230 * Note that this method may break an invariant of having a particular capability in either
1231 * wanted or unwanted lists but never in both. Requests that have the same capability in
1232 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001233 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001234 */
1235 public void combineCapabilities(NetworkCapabilities nc) {
1236 combineNetCapabilities(nc);
1237 combineTransportTypes(nc);
1238 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001239 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001240 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001241 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001242 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001243 }
1244
1245 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001246 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1247 *
1248 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1249 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1250 * bandwidth, signal strength, or validation / captive portal status.
1251 *
1252 * @hide
1253 */
1254 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001255 return (nc != null
1256 && satisfiedByNetCapabilities(nc, onlyImmutable)
1257 && satisfiedByTransportTypes(nc)
1258 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1259 && satisfiedBySpecifier(nc)
1260 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001261 && (onlyImmutable || satisfiedByUids(nc))
1262 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001263 }
1264
1265 /**
1266 * Check if our 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
Robert Greenwalt1448f052014-04-08 13:41:39 -07001271 */
1272 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001273 return satisfiedByNetworkCapabilities(nc, false);
1274 }
1275
1276 /**
1277 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1278 *
1279 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1280 *
1281 * @hide
1282 */
1283 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1284 return satisfiedByNetworkCapabilities(nc, true);
1285 }
1286
1287 /**
1288 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001289 * {@code NetworkCapabilities} and return a String describing any difference.
1290 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001291 *
1292 * @hide
1293 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001294 public String describeImmutableDifferences(NetworkCapabilities that) {
1295 if (that == null) {
1296 return "other NetworkCapabilities was null";
1297 }
1298
1299 StringJoiner joiner = new StringJoiner(", ");
1300
Hugo Benichieae7a222017-07-25 11:40:56 +09001301 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1302 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001303 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001304 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1305 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1306 if (oldImmutableCapabilities != newImmutableCapabilities) {
1307 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1308 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1309 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1310 }
1311
1312 if (!equalsSpecifier(that)) {
1313 NetworkSpecifier before = this.getNetworkSpecifier();
1314 NetworkSpecifier after = that.getNetworkSpecifier();
1315 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1316 }
1317
1318 if (!equalsTransportTypes(that)) {
1319 String before = transportNamesOf(this.getTransportTypes());
1320 String after = transportNamesOf(that.getTransportTypes());
1321 joiner.add(String.format("transports changed: %s -> %s", before, after));
1322 }
1323
1324 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001325 }
1326
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001327 /**
1328 * Checks that our requestable capabilities are the same as those of the given
1329 * {@code NetworkCapabilities}.
1330 *
1331 * @hide
1332 */
1333 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1334 if (nc == null) return false;
1335 return (equalsNetCapabilitiesRequestable(nc) &&
1336 equalsTransportTypes(nc) &&
1337 equalsSpecifier(nc));
1338 }
1339
Robert Greenwalt1448f052014-04-08 13:41:39 -07001340 @Override
1341 public boolean equals(Object obj) {
1342 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001343 NetworkCapabilities that = (NetworkCapabilities) obj;
1344 return (equalsNetCapabilities(that)
1345 && equalsTransportTypes(that)
1346 && equalsLinkBandwidths(that)
1347 && equalsSignalStrength(that)
1348 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001349 && equalsUids(that)
1350 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001351 }
1352
1353 @Override
1354 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001355 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001356 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001357 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1358 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1359 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1360 + ((int) (mTransportTypes >> 32) * 13)
1361 + (mLinkUpBandwidthKbps * 17)
1362 + (mLinkDownBandwidthKbps * 19)
1363 + Objects.hashCode(mNetworkSpecifier) * 23
1364 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001365 + Objects.hashCode(mUids) * 31
1366 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001367 }
1368
Wink Saville4e2dea72014-09-20 11:04:03 -07001369 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001370 public int describeContents() {
1371 return 0;
1372 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001373 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001374 public void writeToParcel(Parcel dest, int flags) {
1375 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001376 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001377 dest.writeLong(mTransportTypes);
1378 dest.writeInt(mLinkUpBandwidthKbps);
1379 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001380 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001381 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001382 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001383 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001384 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001385
Robert Greenwalt1448f052014-04-08 13:41:39 -07001386 public static final Creator<NetworkCapabilities> CREATOR =
1387 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001388 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001389 public NetworkCapabilities createFromParcel(Parcel in) {
1390 NetworkCapabilities netCap = new NetworkCapabilities();
1391
1392 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001393 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001394 netCap.mTransportTypes = in.readLong();
1395 netCap.mLinkUpBandwidthKbps = in.readInt();
1396 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001397 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001398 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001399 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1400 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001401 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001402 return netCap;
1403 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001404 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001405 public NetworkCapabilities[] newArray(int size) {
1406 return new NetworkCapabilities[size];
1407 }
1408 };
1409
Wink Saville4e2dea72014-09-20 11:04:03 -07001410 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001411 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001412 final StringBuilder sb = new StringBuilder("[");
1413 if (0 != mTransportTypes) {
1414 sb.append(" Transports: ");
1415 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1416 NetworkCapabilities::transportNameOf, "|");
1417 }
1418 if (0 != mNetworkCapabilities) {
1419 sb.append(" Capabilities: ");
1420 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1421 NetworkCapabilities::capabilityNameOf, "&");
1422 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001423 if (0 != mNetworkCapabilities) {
1424 sb.append(" Unwanted: ");
1425 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1426 NetworkCapabilities::capabilityNameOf, "&");
1427 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001428 if (mLinkUpBandwidthKbps > 0) {
1429 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1430 }
1431 if (mLinkDownBandwidthKbps > 0) {
1432 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1433 }
1434 if (mNetworkSpecifier != null) {
1435 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1436 }
1437 if (hasSignalStrength()) {
1438 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001439 }
1440
Chalard Jean07ace0f2018-02-26 19:00:45 +09001441 if (null != mUids) {
1442 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1443 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1444 } else {
1445 sb.append(" Uids: <").append(mUids).append(">");
1446 }
1447 }
1448 if (mEstablishingVpnAppUid != INVALID_UID) {
1449 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1450 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001451
Chalard Jeanb03a6222018-04-11 21:09:10 +09001452 if (null != mSSID) {
1453 sb.append(" SSID: ").append(mSSID);
1454 }
1455
Chalard Jean07ace0f2018-02-26 19:00:45 +09001456 sb.append("]");
1457 return sb.toString();
1458 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001459
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001460
Chalard Jean07ace0f2018-02-26 19:00:45 +09001461 private interface NameOf {
1462 String nameOf(int value);
1463 }
1464 /**
1465 * @hide
1466 */
1467 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1468 long bitMask, NameOf nameFetcher, String separator) {
1469 int bitPos = 0;
1470 boolean firstElementAdded = false;
1471 while (bitMask != 0) {
1472 if ((bitMask & 1) != 0) {
1473 if (firstElementAdded) {
1474 sb.append(separator);
1475 } else {
1476 firstElementAdded = true;
1477 }
1478 sb.append(nameFetcher.nameOf(bitPos));
1479 }
1480 bitMask >>= 1;
1481 ++bitPos;
1482 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001483 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001484
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001485 /** @hide */
1486 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1487 final long token = proto.start(fieldId);
1488
1489 for (int transport : getTransportTypes()) {
1490 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1491 }
1492
1493 for (int capability : getCapabilities()) {
1494 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1495 }
1496
1497 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1498 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1499
1500 if (mNetworkSpecifier != null) {
1501 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1502 }
1503
1504 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1505 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1506
1507 proto.end(token);
1508 }
1509
Hugo Benichi5df9d722016-04-25 17:16:35 +09001510 /**
1511 * @hide
1512 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001513 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001514 StringJoiner joiner = new StringJoiner("|");
1515 if (capabilities != null) {
1516 for (int c : capabilities) {
1517 joiner.add(capabilityNameOf(c));
1518 }
1519 }
1520 return joiner.toString();
1521 }
1522
1523 /**
1524 * @hide
1525 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001526 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001527 switch (capability) {
1528 case NET_CAPABILITY_MMS: return "MMS";
1529 case NET_CAPABILITY_SUPL: return "SUPL";
1530 case NET_CAPABILITY_DUN: return "DUN";
1531 case NET_CAPABILITY_FOTA: return "FOTA";
1532 case NET_CAPABILITY_IMS: return "IMS";
1533 case NET_CAPABILITY_CBS: return "CBS";
1534 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1535 case NET_CAPABILITY_IA: return "IA";
1536 case NET_CAPABILITY_RCS: return "RCS";
1537 case NET_CAPABILITY_XCAP: return "XCAP";
1538 case NET_CAPABILITY_EIMS: return "EIMS";
1539 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1540 case NET_CAPABILITY_INTERNET: return "INTERNET";
1541 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1542 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1543 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1544 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1545 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001546 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001547 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001548 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001549 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001550 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001551 default: return Integer.toString(capability);
1552 }
1553 }
1554
1555 /**
1556 * @hide
1557 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001558 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -06001559 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001560 StringJoiner joiner = new StringJoiner("|");
1561 if (types != null) {
1562 for (int t : types) {
1563 joiner.add(transportNameOf(t));
1564 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001565 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001566 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001567 }
1568
1569 /**
1570 * @hide
1571 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001572 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001573 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001574 return "UNKNOWN";
1575 }
1576 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001577 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001578
Jeff Sharkeyde570312017-10-24 21:25:50 -06001579 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001580 Preconditions.checkArgument(
1581 isValidTransport(transport), "Invalid TransportType " + transport);
1582 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001583
1584 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1585 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1586 }
1587
1588 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1589 Preconditions.checkArgument(isValidCapability(capability),
1590 "NetworkCapability " + capability + "out of range");
1591 }
junyulai05986c62018-08-07 19:50:45 +08001592
1593 /**
1594 * Check if this {@code NetworkCapability} instance is metered.
1595 *
1596 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1597 * @hide
1598 */
1599 public boolean isMetered() {
1600 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1601 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001602}