blob: a808c6426945ce43d8b6f87191961cae0c8386b9 [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) {
66 mNetworkCapabilities = nc.mNetworkCapabilities;
67 mTransportTypes = nc.mTransportTypes;
68 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
69 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070070 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090071 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090072 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090073 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080074 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
Chalard Jeanb03a6222018-04-11 21:09:10 +090075 mSSID = nc.mSSID;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070076 }
77 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070078
79 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 * Completely clears the contents of this object, removing even the capabilities that are set
81 * by default when the object is constructed.
82 * @hide
83 */
84 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080085 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070086 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090087 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090088 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090089 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090090 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090091 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090092 }
93
94 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070095 * Represents the network's capabilities. If any are specified they will be satisfied
96 * by any Network that matches all of them.
97 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090098 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070099
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800100 /**
101 * If any capabilities specified here they must not exist in the matching Network.
102 */
103 private long mUnwantedNetworkCapabilities;
104
Jeff Sharkeyde570312017-10-24 21:25:50 -0600105 /** @hide */
106 @Retention(RetentionPolicy.SOURCE)
107 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
108 NET_CAPABILITY_MMS,
109 NET_CAPABILITY_SUPL,
110 NET_CAPABILITY_DUN,
111 NET_CAPABILITY_FOTA,
112 NET_CAPABILITY_IMS,
113 NET_CAPABILITY_CBS,
114 NET_CAPABILITY_WIFI_P2P,
115 NET_CAPABILITY_IA,
116 NET_CAPABILITY_RCS,
117 NET_CAPABILITY_XCAP,
118 NET_CAPABILITY_EIMS,
119 NET_CAPABILITY_NOT_METERED,
120 NET_CAPABILITY_INTERNET,
121 NET_CAPABILITY_NOT_RESTRICTED,
122 NET_CAPABILITY_TRUSTED,
123 NET_CAPABILITY_NOT_VPN,
124 NET_CAPABILITY_VALIDATED,
125 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600126 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600127 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900128 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900129 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800130 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600131 })
132 public @interface NetCapability { }
133
Robert Greenwalt1448f052014-04-08 13:41:39 -0700134 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700135 * Indicates this is a network that has the ability to reach the
136 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700137 */
138 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700139
140 /**
141 * Indicates this is a network that has the ability to reach the carrier's
142 * SUPL server, used to retrieve GPS information.
143 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700144 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700145
146 /**
147 * Indicates this is a network that has the ability to reach the carrier's
148 * DUN or tethering gateway.
149 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700150 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700151
152 /**
153 * Indicates this is a network that has the ability to reach the carrier's
154 * FOTA portal, used for over the air updates.
155 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700156 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700157
158 /**
159 * Indicates this is a network that has the ability to reach the carrier's
160 * IMS servers, used for network registration and signaling.
161 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700162 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700163
164 /**
165 * Indicates this is a network that has the ability to reach the carrier's
166 * CBS servers, used for carrier specific services.
167 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700168 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700169
170 /**
171 * Indicates this is a network that has the ability to reach a Wi-Fi direct
172 * peer.
173 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700174 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700175
176 /**
177 * Indicates this is a network that has the ability to reach a carrier's
178 * Initial Attach servers.
179 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach a carrier's
184 * RCS servers, used for Rich Communication Services.
185 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Indicates this is a network that has the ability to reach a carrier's
190 * XCAP servers, used for configuration and control.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700196 * Emergency IMS servers or other services, used for network signaling
197 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700199 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700200
201 /**
202 * Indicates that this network is unmetered.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates that this network should be able to reach the internet.
208 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700209 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700210
211 /**
212 * Indicates that this network is available for general use. If this is not set
213 * applications should not attempt to communicate on this network. Note that this
214 * is simply informative and not enforcement - enforcement is handled via other means.
215 * Set by default.
216 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700217 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
218
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700219 /**
220 * Indicates that the user has indicated implicit trust of this network. This
221 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
222 * BT device or a wifi the user asked to connect to. Untrusted networks
223 * are probably limited to unknown wifi AP. Set by default.
224 */
225 public static final int NET_CAPABILITY_TRUSTED = 14;
226
Paul Jensen76b610a2015-03-18 09:33:07 -0400227 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400228 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400229 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400230 */
231 public static final int NET_CAPABILITY_NOT_VPN = 15;
232
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900233 /**
234 * Indicates that connectivity on this network was successfully validated. For example, for a
235 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
236 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900237 */
238 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700239
Paul Jensen3d194ea2015-06-16 14:27:36 -0400240 /**
241 * Indicates that this network was found to have a captive portal in place last time it was
242 * probed.
243 */
244 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
245
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900246 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600247 * Indicates that this network is not roaming.
248 */
249 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
250
251 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900252 * Indicates that this network is available for use by apps, and not a network that is being
253 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900254 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600255 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900256
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900257 /**
258 * Indicates that this network is not congested.
259 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600260 * When a network is congested, applications should defer network traffic
261 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900262 */
263 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
264
Chalard Jean804b8fb2018-01-30 22:41:41 +0900265 /**
266 * Indicates that this network is not currently suspended.
267 * <p>
268 * When a network is suspended, the network's IP addresses and any connections
269 * established on the network remain valid, but the network is temporarily unable
270 * to transfer data. This can happen, for example, if a cellular network experiences
271 * a temporary loss of signal, such as when driving through a tunnel, etc.
272 * A network with this capability is not suspended, so is expected to be able to
273 * transfer data.
274 */
275 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
276
Pavel Maltsev43403202018-01-30 17:19:44 -0800277 /**
278 * Indicates that traffic that goes through this network is paid by oem. For example,
279 * this network can be used by system apps to upload telemetry data.
280 * @hide
281 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700282 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800283 public static final int NET_CAPABILITY_OEM_PAID = 22;
284
Robert Greenwalt1448f052014-04-08 13:41:39 -0700285 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800286 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700287
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700288 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900289 * Network capabilities that are expected to be mutable, i.e., can change while a particular
290 * network is connected.
291 */
292 private static final long MUTABLE_CAPABILITIES =
293 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
294 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900295 (1 << NET_CAPABILITY_TRUSTED)
296 | (1 << NET_CAPABILITY_VALIDATED)
297 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
298 | (1 << NET_CAPABILITY_NOT_ROAMING)
299 | (1 << NET_CAPABILITY_FOREGROUND)
300 | (1 << NET_CAPABILITY_NOT_CONGESTED)
301 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900302
303 /**
304 * Network capabilities that are not allowed in NetworkRequests. This exists because the
305 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
306 * capability's presence cannot be known in advance. If such a capability is requested, then we
307 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
308 * get immediately torn down because they do not have the requested capability.
309 */
310 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900311 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900312
313 /**
314 * Capabilities that are set by default when the object is constructed.
315 */
316 private static final long DEFAULT_CAPABILITIES =
317 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
318 (1 << NET_CAPABILITY_TRUSTED) |
319 (1 << NET_CAPABILITY_NOT_VPN);
320
321 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400322 * Capabilities that suggest that a network is restricted.
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000323 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400324 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700325 @VisibleForTesting
326 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400327 (1 << NET_CAPABILITY_CBS) |
328 (1 << NET_CAPABILITY_DUN) |
329 (1 << NET_CAPABILITY_EIMS) |
330 (1 << NET_CAPABILITY_FOTA) |
331 (1 << NET_CAPABILITY_IA) |
332 (1 << NET_CAPABILITY_IMS) |
333 (1 << NET_CAPABILITY_RCS) |
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000334 (1 << NET_CAPABILITY_XCAP);
335
336 /**
337 * Capabilities that force network to be restricted.
338 * {@see #maybeMarkCapabilitiesRestricted}.
339 */
340 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800341 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400342
343 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700344 * Capabilities that suggest that a network is unrestricted.
345 * {@see #maybeMarkCapabilitiesRestricted}.
346 */
347 @VisibleForTesting
348 /* package */ static final long UNRESTRICTED_CAPABILITIES =
349 (1 << NET_CAPABILITY_INTERNET) |
350 (1 << NET_CAPABILITY_MMS) |
351 (1 << NET_CAPABILITY_SUPL) |
352 (1 << NET_CAPABILITY_WIFI_P2P);
353
354 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700355 * Adds the given capability to this {@code NetworkCapability} instance.
356 * Multiple capabilities may be applied sequentially. Note that when searching
357 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800358 * <p>
359 * If the given capability was previously added to the list of unwanted capabilities
360 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700361 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600362 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900363 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700364 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700365 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600366 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800367 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700368 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800369 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700370 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700371 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700372
373 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800374 * Adds the given capability to the list of unwanted capabilities of this
375 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
376 * sequentially. Note that when searching for a network to satisfy a request, the network
377 * must not contain any capability from unwanted capability list.
378 * <p>
379 * If the capability was previously added to the list of required capabilities (for
380 * example, it was there by default or added using {@link #addCapability(int)} method), then
381 * it will be removed from the list of required capabilities as well.
382 *
383 * @see #addCapability(int)
384 * @hide
385 */
386 public void addUnwantedCapability(@NetCapability int capability) {
387 checkValidCapability(capability);
388 mUnwantedNetworkCapabilities |= 1 << capability;
389 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
390 }
391
392 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700393 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800394 * <p>
395 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
396 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700397 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600398 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900399 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700400 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700401 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600402 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800403 checkValidCapability(capability);
404 final long mask = ~(1 << capability);
405 mNetworkCapabilities &= mask;
406 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700407 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700408 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700409
410 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600411 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
412 * instance.
413 *
414 * @hide
415 */
416 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
417 if (value) {
418 addCapability(capability);
419 } else {
420 removeCapability(capability);
421 }
422 return this;
423 }
424
425 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700426 * Gets all the capabilities set on this {@code NetworkCapability} instance.
427 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600428 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700429 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700430 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600431 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600432 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900433 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700434 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700435
436 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800437 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
438 *
439 * @return an array of unwanted capability values for this instance.
440 * @hide
441 */
442 public @NetCapability int[] getUnwantedCapabilities() {
443 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
444 }
445
446
447 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600448 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700449 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600450 *
451 * @hide
452 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800453 public void setCapabilities(@NetCapability int[] capabilities,
454 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600455 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800456 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600457 }
458
459 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800460 * @deprecated use {@link #setCapabilities(int[], int[])}
461 * @hide
462 */
463 @Deprecated
464 public void setCapabilities(@NetCapability int[] capabilities) {
465 setCapabilities(capabilities, new int[] {});
466 }
467
468 /**
469 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700470 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600471 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700472 * @return {@code true} if set on this instance.
473 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600474 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800475 return isValidCapability(capability)
476 && ((mNetworkCapabilities & (1 << capability)) != 0);
477 }
478
479 /** @hide */
480 public boolean hasUnwantedCapability(@NetCapability int capability) {
481 return isValidCapability(capability)
482 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700483 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700484
Robert Greenwalt1448f052014-04-08 13:41:39 -0700485 private void combineNetCapabilities(NetworkCapabilities nc) {
486 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800487 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700488 }
489
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900490 /**
491 * Convenience function that returns a human-readable description of the first mutable
492 * capability we find. Used to present an error message to apps that request mutable
493 * capabilities.
494 *
495 * @hide
496 */
497 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800498 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
499 & NON_REQUESTABLE_CAPABILITIES;
500
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900501 if (nonRequestable != 0) {
502 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900503 }
504 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900505 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900506 return null;
507 }
508
509 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800510 long requestedCapabilities = mNetworkCapabilities;
511 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
512 long providedCapabilities = nc.mNetworkCapabilities;
513
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900514 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800515 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
516 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900517 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800518 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
519 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700520 }
521
Robert Greenwalt06314e42014-10-29 14:04:06 -0700522 /** @hide */
523 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800524 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
525 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700526 }
527
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900528 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
529 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800530 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
531 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
532 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900533 }
534
Robert Greenwalt1448f052014-04-08 13:41:39 -0700535 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400536 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
537 * typically provided by restricted networks.
538 *
539 * TODO: consider:
540 * - Renaming it to guessRestrictedCapability and make it set the
541 * restricted capability bit in addition to clearing it.
542 * @hide
543 */
544 public void maybeMarkCapabilitiesRestricted() {
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000545 // Check if we have any capability that forces the network to be restricted.
546 final boolean forceRestrictedCapability =
547 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
548
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700549 // Verify there aren't any unrestricted capabilities. If there are we say
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000550 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700551 final boolean hasUnrestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000552 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700553
554 // Must have at least some restricted capabilities.
555 final boolean hasRestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000556 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700557
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000558 if (forceRestrictedCapability
559 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400560 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400561 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400562 }
563
564 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700565 * Representing the transport type. Apps should generally not care about transport. A
566 * request for a fast internet connection could be satisfied by a number of different
567 * transports. If any are specified here it will be satisfied a Network that matches
568 * any of them. If a caller doesn't care about the transport it should not specify any.
569 */
570 private long mTransportTypes;
571
Jeff Sharkeyde570312017-10-24 21:25:50 -0600572 /** @hide */
573 @Retention(RetentionPolicy.SOURCE)
574 @IntDef(prefix = { "TRANSPORT_" }, value = {
575 TRANSPORT_CELLULAR,
576 TRANSPORT_WIFI,
577 TRANSPORT_BLUETOOTH,
578 TRANSPORT_ETHERNET,
579 TRANSPORT_VPN,
580 TRANSPORT_WIFI_AWARE,
581 TRANSPORT_LOWPAN,
582 })
583 public @interface Transport { }
584
Robert Greenwalt1448f052014-04-08 13:41:39 -0700585 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700586 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700587 */
588 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700589
590 /**
591 * Indicates this network uses a Wi-Fi transport.
592 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700593 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700594
595 /**
596 * Indicates this network uses a Bluetooth transport.
597 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700598 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700599
600 /**
601 * Indicates this network uses an Ethernet transport.
602 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700603 public static final int TRANSPORT_ETHERNET = 3;
604
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400605 /**
606 * Indicates this network uses a VPN transport.
607 */
608 public static final int TRANSPORT_VPN = 4;
609
Etan Cohen305ea282016-06-20 09:27:12 -0700610 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700611 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700612 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700613 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700614
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700615 /**
616 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700617 */
618 public static final int TRANSPORT_LOWPAN = 6;
619
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900620 /** @hide */
621 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
622 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700623 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700624
Hugo Benichi16f0a942017-06-20 14:07:59 +0900625 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600626 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900627 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
628 }
629
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900630 private static final String[] TRANSPORT_NAMES = {
631 "CELLULAR",
632 "WIFI",
633 "BLUETOOTH",
634 "ETHERNET",
635 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700636 "WIFI_AWARE",
637 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900638 };
639
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700640 /**
641 * Adds the given transport type to this {@code NetworkCapability} instance.
642 * Multiple transports may be applied sequentially. Note that when searching
643 * for a network to satisfy a request, any listed in the request will satisfy the request.
644 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
645 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
646 * to be selected. This is logically different than
647 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
648 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600649 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900650 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700651 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700652 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600653 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900654 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700655 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700656 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700657 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700658 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700659
660 /**
661 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
662 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600663 * @param transportType the transport type to be removed.
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 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600667 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900668 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700669 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700670 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700671 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700672 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700673
674 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600675 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
676 * instance.
677 *
678 * @hide
679 */
680 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
681 if (value) {
682 addTransportType(transportType);
683 } else {
684 removeTransportType(transportType);
685 }
686 return this;
687 }
688
689 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700690 * Gets all the transports set on this {@code NetworkCapability} instance.
691 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600692 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700693 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700694 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600695 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600696 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900697 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700698 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700699
700 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600701 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700702 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600703 *
704 * @hide
705 */
706 public void setTransportTypes(@Transport int[] transportTypes) {
707 mTransportTypes = BitUtils.packBits(transportTypes);
708 }
709
710 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700711 * Tests for the presence of a transport on this instance.
712 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600713 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700714 * @return {@code true} if set on this instance.
715 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600716 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900717 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700718 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700719
720 private void combineTransportTypes(NetworkCapabilities nc) {
721 this.mTransportTypes |= nc.mTransportTypes;
722 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900723
Robert Greenwalt1448f052014-04-08 13:41:39 -0700724 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
725 return ((this.mTransportTypes == 0) ||
726 ((this.mTransportTypes & nc.mTransportTypes) != 0));
727 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900728
Robert Greenwalt06314e42014-10-29 14:04:06 -0700729 /** @hide */
730 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700731 return (nc.mTransportTypes == this.mTransportTypes);
732 }
733
734 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900735 * UID of the app that manages this network, or INVALID_UID if none/unknown.
736 *
737 * This field keeps track of the UID of the app that created this network and is in charge
738 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
739 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
740 * to create networks.
741 *
742 * Because this field is only used in the services side (and to avoid apps being able to
743 * set this to whatever they want), this field is not parcelled and will not be conserved
744 * across the IPC boundary.
745 * @hide
746 */
747 private int mEstablishingVpnAppUid = INVALID_UID;
748
749 /**
750 * Set the UID of the managing app.
751 * @hide
752 */
753 public void setEstablishingVpnAppUid(final int uid) {
754 mEstablishingVpnAppUid = uid;
755 }
756
757 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600758 * Value indicating that link bandwidth is unspecified.
759 * @hide
760 */
761 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
762
763 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700764 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
765 * for the first hop on the given transport. It is not measured, but may take into account
766 * link parameters (Radio technology, allocated channels, etc).
767 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600768 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
769 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700770
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700771 /**
772 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
773 * the estimated first hop transport bandwidth.
774 * <p>
775 * Note that when used to request a network, this specifies the minimum acceptable.
776 * When received as the state of an existing network this specifies the typical
777 * first hop bandwidth expected. This is never measured, but rather is inferred
778 * from technology type and other link parameters. It could be used to differentiate
779 * between very slow 1xRTT cellular links and other faster networks or even between
780 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
781 * fast backhauls and slow backhauls.
782 *
783 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700784 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700785 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600786 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700787 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600788 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700789 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700790
791 /**
792 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
793 * the estimated first hop transport bandwidth.
794 *
795 * @return The estimated first hop upstream (device to network) bandwidth.
796 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797 public int getLinkUpstreamBandwidthKbps() {
798 return mLinkUpBandwidthKbps;
799 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700800
801 /**
802 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
803 * the estimated first hop transport bandwidth.
804 * <p>
805 * Note that when used to request a network, this specifies the minimum acceptable.
806 * When received as the state of an existing network this specifies the typical
807 * first hop bandwidth expected. This is never measured, but rather is inferred
808 * from technology type and other link parameters. It could be used to differentiate
809 * between very slow 1xRTT cellular links and other faster networks or even between
810 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
811 * fast backhauls and slow backhauls.
812 *
813 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700814 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700815 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600816 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700817 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600818 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700819 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700820
821 /**
822 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
823 * the estimated first hop transport bandwidth.
824 *
825 * @return The estimated first hop downstream (network to device) bandwidth.
826 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 public int getLinkDownstreamBandwidthKbps() {
828 return mLinkDownBandwidthKbps;
829 }
830
831 private void combineLinkBandwidths(NetworkCapabilities nc) {
832 this.mLinkUpBandwidthKbps =
833 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
834 this.mLinkDownBandwidthKbps =
835 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
836 }
837 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
838 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
839 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
840 }
841 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
842 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
843 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
844 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600845 /** @hide */
846 public static int minBandwidth(int a, int b) {
847 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
848 return b;
849 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
850 return a;
851 } else {
852 return Math.min(a, b);
853 }
854 }
855 /** @hide */
856 public static int maxBandwidth(int a, int b) {
857 return Math.max(a, b);
858 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700859
Etan Cohena7434272017-04-03 12:17:51 -0700860 private NetworkSpecifier mNetworkSpecifier = null;
861
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700862 /**
863 * Sets the optional bearer specific network specifier.
864 * This has no meaning if a single transport is also not specified, so calling
865 * this without a single transport set will generate an exception, as will
866 * subsequently adding or removing transports after this is set.
867 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700868 *
Etan Cohena7434272017-04-03 12:17:51 -0700869 * @param networkSpecifier A concrete, parcelable framework class that extends
870 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900871 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700872 * @hide
873 */
Etan Cohena7434272017-04-03 12:17:51 -0700874 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
875 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700876 throw new IllegalStateException("Must have a single transport specified to use " +
877 "setNetworkSpecifier");
878 }
Etan Cohena7434272017-04-03 12:17:51 -0700879
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700880 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700881
Pierre Imaic8419a82016-03-22 17:54:54 +0900882 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700883 }
884
885 /**
886 * Gets the optional bearer specific network specifier.
887 *
Etan Cohena7434272017-04-03 12:17:51 -0700888 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
889 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700890 * @hide
891 */
Etan Cohena7434272017-04-03 12:17:51 -0700892 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 return mNetworkSpecifier;
894 }
895
896 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700897 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700898 throw new IllegalStateException("Can't combine two networkSpecifiers");
899 }
Etan Cohena7434272017-04-03 12:17:51 -0700900 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700901 }
Etan Cohena7434272017-04-03 12:17:51 -0700902
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700903 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700904 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
905 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700906 }
Etan Cohena7434272017-04-03 12:17:51 -0700907
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700908 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700909 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700910 }
911
Robert Greenwalt1448f052014-04-08 13:41:39 -0700912 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900913 * Magic value that indicates no signal strength provided. A request specifying this value is
914 * always satisfied.
915 *
916 * @hide
917 */
918 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
919
920 /**
921 * Signal strength. This is a signed integer, and higher values indicate better signal.
922 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
923 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700924 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900925
926 /**
927 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
928 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900929 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900930 * <p>
931 * Note that when used to register a network callback, this specifies the minimum acceptable
932 * signal strength. When received as the state of an existing network it specifies the current
933 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
934 * effect when requesting a callback.
935 *
936 * @param signalStrength the bearer-specific signal strength.
937 * @hide
938 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600939 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900940 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600941 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900942 }
943
944 /**
945 * Returns {@code true} if this object specifies a signal strength.
946 *
947 * @hide
948 */
949 public boolean hasSignalStrength() {
950 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
951 }
952
953 /**
954 * Retrieves the signal strength.
955 *
956 * @return The bearer-specific signal strength.
957 * @hide
958 */
959 public int getSignalStrength() {
960 return mSignalStrength;
961 }
962
963 private void combineSignalStrength(NetworkCapabilities nc) {
964 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
965 }
966
967 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
968 return this.mSignalStrength <= nc.mSignalStrength;
969 }
970
971 private boolean equalsSignalStrength(NetworkCapabilities nc) {
972 return this.mSignalStrength == nc.mSignalStrength;
973 }
974
975 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900976 * List of UIDs this network applies to. No restriction if null.
977 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900978 * For networks, mUids represent the list of network this applies to, and null means this
979 * network applies to all UIDs.
980 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
981 * must be included in a network so that they match. As an exception to the general rule,
982 * a null mUids field for requests mean "no requirements" rather than what the general rule
983 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
984 * of this API expect in practice. A network that must match all UIDs can still be
985 * expressed with a set ranging the entire set of possible UIDs.
986 * <p>
987 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900988 * the UIDs in this list, and it is their default network. Apps in this list that wish to
989 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
990 * member is null, then the network is not restricted by app UID. If it's an empty list, then
991 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900992 * As a special exception, the app managing this network (as identified by its UID stored in
993 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
994 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
995 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900996 * <p>
997 * Please note that in principle a single app can be associated with multiple UIDs because
998 * each app will have a different UID when it's run as a different (macro-)user. A single
999 * macro user can only have a single active VPN app at any given time however.
1000 * <p>
1001 * Also please be aware this class does not try to enforce any normalization on this. Callers
1002 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1003 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1004 * their own (like requiring sortedness or no overlap) they need to enforce it
1005 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1006 * or overlapping ranges are present.
1007 *
1008 * @hide
1009 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001010 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001011
1012 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001013 * Convenience method to set the UIDs this network applies to to a single UID.
1014 * @hide
1015 */
1016 public NetworkCapabilities setSingleUid(int uid) {
1017 final ArraySet<UidRange> identity = new ArraySet<>(1);
1018 identity.add(new UidRange(uid, uid));
1019 setUids(identity);
1020 return this;
1021 }
1022
1023 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001024 * Set the list of UIDs this network applies to.
1025 * This makes a copy of the set so that callers can't modify it after the call.
1026 * @hide
1027 */
1028 public NetworkCapabilities setUids(Set<UidRange> uids) {
1029 if (null == uids) {
1030 mUids = null;
1031 } else {
1032 mUids = new ArraySet<>(uids);
1033 }
1034 return this;
1035 }
1036
1037 /**
1038 * Get the list of UIDs this network applies to.
1039 * This returns a copy of the set so that callers can't modify the original object.
1040 * @hide
1041 */
1042 public Set<UidRange> getUids() {
1043 return null == mUids ? null : new ArraySet<>(mUids);
1044 }
1045
1046 /**
1047 * Test whether this network applies to this UID.
1048 * @hide
1049 */
1050 public boolean appliesToUid(int uid) {
1051 if (null == mUids) return true;
1052 for (UidRange range : mUids) {
1053 if (range.contains(uid)) {
1054 return true;
1055 }
1056 }
1057 return false;
1058 }
1059
1060 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001061 * 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 +09001062 * <p>
1063 * This test only checks whether equal range objects are in both sets. It will
1064 * return false if the ranges are not exactly the same, even if the covered UIDs
1065 * are for an equivalent result.
1066 * <p>
1067 * Note that this method is not very optimized, which is fine as long as it's not used very
1068 * often.
1069 * <p>
1070 * nc is assumed nonnull.
1071 *
1072 * @hide
1073 */
1074 @VisibleForTesting
1075 public boolean equalsUids(NetworkCapabilities nc) {
1076 Set<UidRange> comparedUids = nc.mUids;
1077 if (null == comparedUids) return null == mUids;
1078 if (null == mUids) return false;
1079 // Make a copy so it can be mutated to check that all ranges in mUids
1080 // also are in uids.
1081 final Set<UidRange> uids = new ArraySet<>(mUids);
1082 for (UidRange range : comparedUids) {
1083 if (!uids.contains(range)) {
1084 return false;
1085 }
1086 uids.remove(range);
1087 }
1088 return uids.isEmpty();
1089 }
1090
1091 /**
1092 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1093 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001094 * This method is called on the NetworkCapabilities embedded in a request with the
1095 * capabilities of an available network. It checks whether all the UIDs from this listen
1096 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1097 * in the passed nc (representing the UIDs that this network is available to).
1098 * <p>
1099 * As a special exception, the UID that created the passed network (as represented by its
1100 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1101 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1102 * can see its own network when it listens for it.
1103 * <p>
1104 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001105 * @see #appliesToUid
1106 * @hide
1107 */
1108 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001109 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001110 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001111 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001112 if (!nc.appliesToUidRange(requiredRange)) {
1113 return false;
1114 }
1115 }
1116 return true;
1117 }
1118
1119 /**
1120 * Returns whether this network applies to the passed ranges.
1121 * This assumes that to apply, the passed range has to be entirely contained
1122 * within one of the ranges this network applies to. If the ranges are not normalized,
1123 * this method may return false even though all required UIDs are covered because no
1124 * single range contained them all.
1125 * @hide
1126 */
1127 @VisibleForTesting
1128 public boolean appliesToUidRange(UidRange requiredRange) {
1129 if (null == mUids) return true;
1130 for (UidRange uidRange : mUids) {
1131 if (uidRange.containsRange(requiredRange)) {
1132 return true;
1133 }
1134 }
1135 return false;
1136 }
1137
1138 /**
1139 * Combine the UIDs this network currently applies to with the UIDs the passed
1140 * NetworkCapabilities apply to.
1141 * nc is assumed nonnull.
1142 */
1143 private void combineUids(NetworkCapabilities nc) {
1144 if (null == nc.mUids || null == mUids) {
1145 mUids = null;
1146 return;
1147 }
1148 mUids.addAll(nc.mUids);
1149 }
1150
Chalard Jeanb03a6222018-04-11 21:09:10 +09001151
1152 /**
1153 * The SSID of the network, or null if not applicable or unknown.
1154 * <p>
1155 * This is filled in by wifi code.
1156 * @hide
1157 */
1158 private String mSSID;
1159
1160 /**
1161 * Sets the SSID of this network.
1162 * @hide
1163 */
1164 public NetworkCapabilities setSSID(String ssid) {
1165 mSSID = ssid;
1166 return this;
1167 }
1168
1169 /**
1170 * Gets the SSID of this network, or null if none or unknown.
1171 * @hide
1172 */
1173 public String getSSID() {
1174 return mSSID;
1175 }
1176
1177 /**
1178 * Tests if the SSID of this network is the same as the SSID of the passed network.
1179 * @hide
1180 */
1181 public boolean equalsSSID(NetworkCapabilities nc) {
1182 return Objects.equals(mSSID, nc.mSSID);
1183 }
1184
1185 /**
1186 * Check if the SSID requirements of this object are matched by the passed object.
1187 * @hide
1188 */
1189 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1190 return mSSID == null || mSSID.equals(nc.mSSID);
1191 }
1192
1193 /**
1194 * Combine SSIDs of the capabilities.
1195 * <p>
1196 * This is only legal if either the SSID of this object is null, or both SSIDs are
1197 * equal.
1198 * @hide
1199 */
1200 private void combineSSIDs(NetworkCapabilities nc) {
1201 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1202 throw new IllegalStateException("Can't combine two SSIDs");
1203 }
1204 setSSID(nc.mSSID);
1205 }
1206
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001207 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001208 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001209 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001210 */
1211 public void combineCapabilities(NetworkCapabilities nc) {
1212 combineNetCapabilities(nc);
1213 combineTransportTypes(nc);
1214 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001215 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001216 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001217 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001218 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001219 }
1220
1221 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001222 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1223 *
1224 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1225 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1226 * bandwidth, signal strength, or validation / captive portal status.
1227 *
1228 * @hide
1229 */
1230 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001231 return (nc != null
1232 && satisfiedByNetCapabilities(nc, onlyImmutable)
1233 && satisfiedByTransportTypes(nc)
1234 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1235 && satisfiedBySpecifier(nc)
1236 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001237 && (onlyImmutable || satisfiedByUids(nc))
1238 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001239 }
1240
1241 /**
1242 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1243 *
1244 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1245 *
1246 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001247 */
1248 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001249 return satisfiedByNetworkCapabilities(nc, false);
1250 }
1251
1252 /**
1253 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1254 *
1255 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1256 *
1257 * @hide
1258 */
1259 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1260 return satisfiedByNetworkCapabilities(nc, true);
1261 }
1262
1263 /**
1264 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001265 * {@code NetworkCapabilities} and return a String describing any difference.
1266 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001267 *
1268 * @hide
1269 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001270 public String describeImmutableDifferences(NetworkCapabilities that) {
1271 if (that == null) {
1272 return "other NetworkCapabilities was null";
1273 }
1274
1275 StringJoiner joiner = new StringJoiner(", ");
1276
Hugo Benichieae7a222017-07-25 11:40:56 +09001277 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1278 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001279 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001280 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1281 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1282 if (oldImmutableCapabilities != newImmutableCapabilities) {
1283 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1284 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1285 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1286 }
1287
1288 if (!equalsSpecifier(that)) {
1289 NetworkSpecifier before = this.getNetworkSpecifier();
1290 NetworkSpecifier after = that.getNetworkSpecifier();
1291 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1292 }
1293
1294 if (!equalsTransportTypes(that)) {
1295 String before = transportNamesOf(this.getTransportTypes());
1296 String after = transportNamesOf(that.getTransportTypes());
1297 joiner.add(String.format("transports changed: %s -> %s", before, after));
1298 }
1299
1300 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001301 }
1302
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001303 /**
1304 * Checks that our requestable capabilities are the same as those of the given
1305 * {@code NetworkCapabilities}.
1306 *
1307 * @hide
1308 */
1309 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1310 if (nc == null) return false;
1311 return (equalsNetCapabilitiesRequestable(nc) &&
1312 equalsTransportTypes(nc) &&
1313 equalsSpecifier(nc));
1314 }
1315
Robert Greenwalt1448f052014-04-08 13:41:39 -07001316 @Override
1317 public boolean equals(Object obj) {
1318 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001319 NetworkCapabilities that = (NetworkCapabilities) obj;
1320 return (equalsNetCapabilities(that)
1321 && equalsTransportTypes(that)
1322 && equalsLinkBandwidths(that)
1323 && equalsSignalStrength(that)
1324 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001325 && equalsUids(that)
1326 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001327 }
1328
1329 @Override
1330 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001331 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001332 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001333 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1334 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1335 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1336 + ((int) (mTransportTypes >> 32) * 13)
1337 + (mLinkUpBandwidthKbps * 17)
1338 + (mLinkDownBandwidthKbps * 19)
1339 + Objects.hashCode(mNetworkSpecifier) * 23
1340 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001341 + Objects.hashCode(mUids) * 31
1342 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001343 }
1344
Wink Saville4e2dea72014-09-20 11:04:03 -07001345 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001346 public int describeContents() {
1347 return 0;
1348 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001349 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001350 public void writeToParcel(Parcel dest, int flags) {
1351 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001352 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001353 dest.writeLong(mTransportTypes);
1354 dest.writeInt(mLinkUpBandwidthKbps);
1355 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001356 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001357 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001358 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001359 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001360 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001361
Robert Greenwalt1448f052014-04-08 13:41:39 -07001362 public static final Creator<NetworkCapabilities> CREATOR =
1363 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001364 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001365 public NetworkCapabilities createFromParcel(Parcel in) {
1366 NetworkCapabilities netCap = new NetworkCapabilities();
1367
1368 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001369 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001370 netCap.mTransportTypes = in.readLong();
1371 netCap.mLinkUpBandwidthKbps = in.readInt();
1372 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001373 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001374 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001375 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1376 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001377 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 return netCap;
1379 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001380 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001381 public NetworkCapabilities[] newArray(int size) {
1382 return new NetworkCapabilities[size];
1383 }
1384 };
1385
Wink Saville4e2dea72014-09-20 11:04:03 -07001386 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001387 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001388 final StringBuilder sb = new StringBuilder("[");
1389 if (0 != mTransportTypes) {
1390 sb.append(" Transports: ");
1391 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1392 NetworkCapabilities::transportNameOf, "|");
1393 }
1394 if (0 != mNetworkCapabilities) {
1395 sb.append(" Capabilities: ");
1396 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1397 NetworkCapabilities::capabilityNameOf, "&");
1398 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001399 if (0 != mNetworkCapabilities) {
1400 sb.append(" Unwanted: ");
1401 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1402 NetworkCapabilities::capabilityNameOf, "&");
1403 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001404 if (mLinkUpBandwidthKbps > 0) {
1405 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1406 }
1407 if (mLinkDownBandwidthKbps > 0) {
1408 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1409 }
1410 if (mNetworkSpecifier != null) {
1411 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1412 }
1413 if (hasSignalStrength()) {
1414 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001415 }
1416
Chalard Jean07ace0f2018-02-26 19:00:45 +09001417 if (null != mUids) {
1418 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1419 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1420 } else {
1421 sb.append(" Uids: <").append(mUids).append(">");
1422 }
1423 }
1424 if (mEstablishingVpnAppUid != INVALID_UID) {
1425 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1426 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001427
Chalard Jeanb03a6222018-04-11 21:09:10 +09001428 if (null != mSSID) {
1429 sb.append(" SSID: ").append(mSSID);
1430 }
1431
Chalard Jean07ace0f2018-02-26 19:00:45 +09001432 sb.append("]");
1433 return sb.toString();
1434 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001435
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001436
Chalard Jean07ace0f2018-02-26 19:00:45 +09001437 private interface NameOf {
1438 String nameOf(int value);
1439 }
1440 /**
1441 * @hide
1442 */
1443 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1444 long bitMask, NameOf nameFetcher, String separator) {
1445 int bitPos = 0;
1446 boolean firstElementAdded = false;
1447 while (bitMask != 0) {
1448 if ((bitMask & 1) != 0) {
1449 if (firstElementAdded) {
1450 sb.append(separator);
1451 } else {
1452 firstElementAdded = true;
1453 }
1454 sb.append(nameFetcher.nameOf(bitPos));
1455 }
1456 bitMask >>= 1;
1457 ++bitPos;
1458 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001459 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001460
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001461 /** @hide */
1462 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1463 final long token = proto.start(fieldId);
1464
1465 for (int transport : getTransportTypes()) {
1466 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1467 }
1468
1469 for (int capability : getCapabilities()) {
1470 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1471 }
1472
1473 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1474 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1475
1476 if (mNetworkSpecifier != null) {
1477 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1478 }
1479
1480 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1481 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1482
1483 proto.end(token);
1484 }
1485
Hugo Benichi5df9d722016-04-25 17:16:35 +09001486 /**
1487 * @hide
1488 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001489 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001490 StringJoiner joiner = new StringJoiner("|");
1491 if (capabilities != null) {
1492 for (int c : capabilities) {
1493 joiner.add(capabilityNameOf(c));
1494 }
1495 }
1496 return joiner.toString();
1497 }
1498
1499 /**
1500 * @hide
1501 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001502 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001503 switch (capability) {
1504 case NET_CAPABILITY_MMS: return "MMS";
1505 case NET_CAPABILITY_SUPL: return "SUPL";
1506 case NET_CAPABILITY_DUN: return "DUN";
1507 case NET_CAPABILITY_FOTA: return "FOTA";
1508 case NET_CAPABILITY_IMS: return "IMS";
1509 case NET_CAPABILITY_CBS: return "CBS";
1510 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1511 case NET_CAPABILITY_IA: return "IA";
1512 case NET_CAPABILITY_RCS: return "RCS";
1513 case NET_CAPABILITY_XCAP: return "XCAP";
1514 case NET_CAPABILITY_EIMS: return "EIMS";
1515 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1516 case NET_CAPABILITY_INTERNET: return "INTERNET";
1517 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1518 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1519 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1520 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1521 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001522 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001523 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001524 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001525 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001526 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001527 default: return Integer.toString(capability);
1528 }
1529 }
1530
1531 /**
1532 * @hide
1533 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001534 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001535 StringJoiner joiner = new StringJoiner("|");
1536 if (types != null) {
1537 for (int t : types) {
1538 joiner.add(transportNameOf(t));
1539 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001540 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001541 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001542 }
1543
1544 /**
1545 * @hide
1546 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001547 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001548 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001549 return "UNKNOWN";
1550 }
1551 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001552 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001553
Jeff Sharkeyde570312017-10-24 21:25:50 -06001554 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001555 Preconditions.checkArgument(
1556 isValidTransport(transport), "Invalid TransportType " + transport);
1557 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001558
1559 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1560 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1561 }
1562
1563 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1564 Preconditions.checkArgument(isValidCapability(capability),
1565 "NetworkCapability " + capability + "out of range");
1566 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001567}