blob: 0fd0a63d6446a37f5fd4d46cc71b1a0c025c3e8b [file] [log] [blame]
Robert Greenwalt1448f052014-04-08 13:41:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Jeff Sharkeyde570312017-10-24 21:25:50 -060019import android.annotation.IntDef;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070020import android.annotation.SystemApi;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060021import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070022import android.os.Parcel;
23import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090024import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080025import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070026
27import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090028import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090029import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070030
Jeff Sharkeyde570312017-10-24 21:25:50 -060031import java.lang.annotation.Retention;
32import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070033import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090034import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090035import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070036
37/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070038 * Representation of the capabilities of an active network. Instances are
39 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060040 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
41 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060042 * <p>
43 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
44 * network selection. Rather than indicate a need for Wi-Fi because an
45 * application needs high bandwidth and risk obsolescence when a new, fast
46 * network appears (like LTE), the application should specify it needs high
47 * bandwidth. Similarly if an application needs an unmetered network for a bulk
48 * transfer it can specify that rather than assuming all cellular based
49 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070050 */
51public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070052 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090053 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070054
Robert Greenwalt7569f182014-06-08 16:42:59 -070055 /**
56 * @hide
57 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070058 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090059 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090060 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070061 }
62
63 public NetworkCapabilities(NetworkCapabilities nc) {
64 if (nc != null) {
65 mNetworkCapabilities = nc.mNetworkCapabilities;
66 mTransportTypes = nc.mTransportTypes;
67 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
68 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070069 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090070 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090071 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090072 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080073 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
Chalard Jeanb03a6222018-04-11 21:09:10 +090074 mSSID = nc.mSSID;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070075 }
76 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070077
78 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090079 * Completely clears the contents of this object, removing even the capabilities that are set
80 * by default when the object is constructed.
81 * @hide
82 */
83 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080084 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070085 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090086 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090087 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090088 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090089 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090090 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 }
92
93 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070094 * Represents the network's capabilities. If any are specified they will be satisfied
95 * by any Network that matches all of them.
96 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090097 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070098
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080099 /**
100 * If any capabilities specified here they must not exist in the matching Network.
101 */
102 private long mUnwantedNetworkCapabilities;
103
Jeff Sharkeyde570312017-10-24 21:25:50 -0600104 /** @hide */
105 @Retention(RetentionPolicy.SOURCE)
106 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
107 NET_CAPABILITY_MMS,
108 NET_CAPABILITY_SUPL,
109 NET_CAPABILITY_DUN,
110 NET_CAPABILITY_FOTA,
111 NET_CAPABILITY_IMS,
112 NET_CAPABILITY_CBS,
113 NET_CAPABILITY_WIFI_P2P,
114 NET_CAPABILITY_IA,
115 NET_CAPABILITY_RCS,
116 NET_CAPABILITY_XCAP,
117 NET_CAPABILITY_EIMS,
118 NET_CAPABILITY_NOT_METERED,
119 NET_CAPABILITY_INTERNET,
120 NET_CAPABILITY_NOT_RESTRICTED,
121 NET_CAPABILITY_TRUSTED,
122 NET_CAPABILITY_NOT_VPN,
123 NET_CAPABILITY_VALIDATED,
124 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600125 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600126 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900127 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900128 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800129 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600130 })
131 public @interface NetCapability { }
132
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700134 * Indicates this is a network that has the ability to reach the
135 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700136 */
137 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700138
139 /**
140 * Indicates this is a network that has the ability to reach the carrier's
141 * SUPL server, used to retrieve GPS information.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates this is a network that has the ability to reach the carrier's
147 * DUN or tethering gateway.
148 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150
151 /**
152 * Indicates this is a network that has the ability to reach the carrier's
153 * FOTA portal, used for over the air updates.
154 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700155 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700156
157 /**
158 * Indicates this is a network that has the ability to reach the carrier's
159 * IMS servers, used for network registration and signaling.
160 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700161 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700162
163 /**
164 * Indicates this is a network that has the ability to reach the carrier's
165 * CBS servers, used for carrier specific services.
166 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700167 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700168
169 /**
170 * Indicates this is a network that has the ability to reach a Wi-Fi direct
171 * peer.
172 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174
175 /**
176 * Indicates this is a network that has the ability to reach a carrier's
177 * Initial Attach servers.
178 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach a carrier's
183 * RCS servers, used for Rich Communication Services.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach a carrier's
189 * XCAP servers, used for configuration and control.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700195 * Emergency IMS servers or other services, used for network signaling
196 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates that this network is unmetered.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates that this network should be able to reach the internet.
207 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700208 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700209
210 /**
211 * Indicates that this network is available for general use. If this is not set
212 * applications should not attempt to communicate on this network. Note that this
213 * is simply informative and not enforcement - enforcement is handled via other means.
214 * Set by default.
215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
217
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700218 /**
219 * Indicates that the user has indicated implicit trust of this network. This
220 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
221 * BT device or a wifi the user asked to connect to. Untrusted networks
222 * are probably limited to unknown wifi AP. Set by default.
223 */
224 public static final int NET_CAPABILITY_TRUSTED = 14;
225
Paul Jensen76b610a2015-03-18 09:33:07 -0400226 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400227 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400228 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400229 */
230 public static final int NET_CAPABILITY_NOT_VPN = 15;
231
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900232 /**
233 * Indicates that connectivity on this network was successfully validated. For example, for a
234 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
235 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900236 */
237 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700238
Paul Jensen3d194ea2015-06-16 14:27:36 -0400239 /**
240 * Indicates that this network was found to have a captive portal in place last time it was
241 * probed.
242 */
243 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
244
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900245 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600246 * Indicates that this network is not roaming.
247 */
248 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
249
250 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900251 * Indicates that this network is available for use by apps, and not a network that is being
252 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900253 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600254 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900255
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900256 /**
257 * Indicates that this network is not congested.
258 * <p>
259 * When a network is congested, the device should defer network traffic that
260 * can be done at a later time without breaking developer contracts.
261 * @hide
262 */
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 Sharkeyde570312017-10-24 21:25:50 -0600431 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900432 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700433 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700434
435 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800436 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
437 *
438 * @return an array of unwanted capability values for this instance.
439 * @hide
440 */
441 public @NetCapability int[] getUnwantedCapabilities() {
442 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
443 }
444
445
446 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600447 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700448 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600449 *
450 * @hide
451 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800452 public void setCapabilities(@NetCapability int[] capabilities,
453 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600454 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800455 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600456 }
457
458 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800459 * @deprecated use {@link #setCapabilities(int[], int[])}
460 * @hide
461 */
462 @Deprecated
463 public void setCapabilities(@NetCapability int[] capabilities) {
464 setCapabilities(capabilities, new int[] {});
465 }
466
467 /**
468 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700469 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600470 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700471 * @return {@code true} if set on this instance.
472 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600473 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800474 return isValidCapability(capability)
475 && ((mNetworkCapabilities & (1 << capability)) != 0);
476 }
477
478 /** @hide */
479 public boolean hasUnwantedCapability(@NetCapability int capability) {
480 return isValidCapability(capability)
481 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700482 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700483
Robert Greenwalt1448f052014-04-08 13:41:39 -0700484 private void combineNetCapabilities(NetworkCapabilities nc) {
485 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800486 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700487 }
488
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900489 /**
490 * Convenience function that returns a human-readable description of the first mutable
491 * capability we find. Used to present an error message to apps that request mutable
492 * capabilities.
493 *
494 * @hide
495 */
496 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800497 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
498 & NON_REQUESTABLE_CAPABILITIES;
499
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900500 if (nonRequestable != 0) {
501 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900502 }
503 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900504 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900505 return null;
506 }
507
508 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800509 long requestedCapabilities = mNetworkCapabilities;
510 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
511 long providedCapabilities = nc.mNetworkCapabilities;
512
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900513 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800514 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
515 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900516 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800517 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
518 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700519 }
520
Robert Greenwalt06314e42014-10-29 14:04:06 -0700521 /** @hide */
522 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800523 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
524 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700525 }
526
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900527 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
528 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800529 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
530 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
531 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900532 }
533
Robert Greenwalt1448f052014-04-08 13:41:39 -0700534 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400535 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
536 * typically provided by restricted networks.
537 *
538 * TODO: consider:
539 * - Renaming it to guessRestrictedCapability and make it set the
540 * restricted capability bit in addition to clearing it.
541 * @hide
542 */
543 public void maybeMarkCapabilitiesRestricted() {
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000544 // Check if we have any capability that forces the network to be restricted.
545 final boolean forceRestrictedCapability =
546 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
547
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700548 // Verify there aren't any unrestricted capabilities. If there are we say
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000549 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700550 final boolean hasUnrestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000551 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700552
553 // Must have at least some restricted capabilities.
554 final boolean hasRestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000555 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700556
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000557 if (forceRestrictedCapability
558 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400559 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400560 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400561 }
562
563 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700564 * Representing the transport type. Apps should generally not care about transport. A
565 * request for a fast internet connection could be satisfied by a number of different
566 * transports. If any are specified here it will be satisfied a Network that matches
567 * any of them. If a caller doesn't care about the transport it should not specify any.
568 */
569 private long mTransportTypes;
570
Jeff Sharkeyde570312017-10-24 21:25:50 -0600571 /** @hide */
572 @Retention(RetentionPolicy.SOURCE)
573 @IntDef(prefix = { "TRANSPORT_" }, value = {
574 TRANSPORT_CELLULAR,
575 TRANSPORT_WIFI,
576 TRANSPORT_BLUETOOTH,
577 TRANSPORT_ETHERNET,
578 TRANSPORT_VPN,
579 TRANSPORT_WIFI_AWARE,
580 TRANSPORT_LOWPAN,
581 })
582 public @interface Transport { }
583
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700585 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700586 */
587 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700588
589 /**
590 * Indicates this network uses a Wi-Fi transport.
591 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700592 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700593
594 /**
595 * Indicates this network uses a Bluetooth transport.
596 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700597 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700598
599 /**
600 * Indicates this network uses an Ethernet transport.
601 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700602 public static final int TRANSPORT_ETHERNET = 3;
603
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400604 /**
605 * Indicates this network uses a VPN transport.
606 */
607 public static final int TRANSPORT_VPN = 4;
608
Etan Cohen305ea282016-06-20 09:27:12 -0700609 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700610 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700611 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700612 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700613
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700614 /**
615 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700616 */
617 public static final int TRANSPORT_LOWPAN = 6;
618
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900619 /** @hide */
620 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
621 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700622 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700623
Hugo Benichi16f0a942017-06-20 14:07:59 +0900624 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600625 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900626 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
627 }
628
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900629 private static final String[] TRANSPORT_NAMES = {
630 "CELLULAR",
631 "WIFI",
632 "BLUETOOTH",
633 "ETHERNET",
634 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700635 "WIFI_AWARE",
636 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900637 };
638
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700639 /**
640 * Adds the given transport type to this {@code NetworkCapability} instance.
641 * Multiple transports may be applied sequentially. Note that when searching
642 * for a network to satisfy a request, any listed in the request will satisfy the request.
643 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
644 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
645 * to be selected. This is logically different than
646 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
647 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600648 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900649 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700650 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700651 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600652 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900653 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700654 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700655 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700656 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700657 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700658
659 /**
660 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
661 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600662 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900663 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700664 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700665 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600666 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900667 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700668 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700669 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700670 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700671 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700672
673 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600674 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
675 * instance.
676 *
677 * @hide
678 */
679 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
680 if (value) {
681 addTransportType(transportType);
682 } else {
683 removeTransportType(transportType);
684 }
685 return this;
686 }
687
688 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700689 * Gets all the transports set on this {@code NetworkCapability} instance.
690 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600691 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700692 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700693 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600694 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900695 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700696 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700697
698 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600699 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700700 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600701 *
702 * @hide
703 */
704 public void setTransportTypes(@Transport int[] transportTypes) {
705 mTransportTypes = BitUtils.packBits(transportTypes);
706 }
707
708 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700709 * Tests for the presence of a transport on this instance.
710 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700712 * @return {@code true} if set on this instance.
713 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600714 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900715 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700716 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700717
718 private void combineTransportTypes(NetworkCapabilities nc) {
719 this.mTransportTypes |= nc.mTransportTypes;
720 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900721
Robert Greenwalt1448f052014-04-08 13:41:39 -0700722 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
723 return ((this.mTransportTypes == 0) ||
724 ((this.mTransportTypes & nc.mTransportTypes) != 0));
725 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900726
Robert Greenwalt06314e42014-10-29 14:04:06 -0700727 /** @hide */
728 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700729 return (nc.mTransportTypes == this.mTransportTypes);
730 }
731
732 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900733 * UID of the app that manages this network, or INVALID_UID if none/unknown.
734 *
735 * This field keeps track of the UID of the app that created this network and is in charge
736 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
737 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
738 * to create networks.
739 *
740 * Because this field is only used in the services side (and to avoid apps being able to
741 * set this to whatever they want), this field is not parcelled and will not be conserved
742 * across the IPC boundary.
743 * @hide
744 */
745 private int mEstablishingVpnAppUid = INVALID_UID;
746
747 /**
748 * Set the UID of the managing app.
749 * @hide
750 */
751 public void setEstablishingVpnAppUid(final int uid) {
752 mEstablishingVpnAppUid = uid;
753 }
754
755 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600756 * Value indicating that link bandwidth is unspecified.
757 * @hide
758 */
759 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
760
761 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700762 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
763 * for the first hop on the given transport. It is not measured, but may take into account
764 * link parameters (Radio technology, allocated channels, etc).
765 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600766 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
767 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700768
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700769 /**
770 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
771 * the estimated first hop transport bandwidth.
772 * <p>
773 * Note that when used to request a network, this specifies the minimum acceptable.
774 * When received as the state of an existing network this specifies the typical
775 * first hop bandwidth expected. This is never measured, but rather is inferred
776 * from technology type and other link parameters. It could be used to differentiate
777 * between very slow 1xRTT cellular links and other faster networks or even between
778 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
779 * fast backhauls and slow backhauls.
780 *
781 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700782 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700783 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600784 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700785 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600786 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700787 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700788
789 /**
790 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
791 * the estimated first hop transport bandwidth.
792 *
793 * @return The estimated first hop upstream (device to network) bandwidth.
794 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700795 public int getLinkUpstreamBandwidthKbps() {
796 return mLinkUpBandwidthKbps;
797 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700798
799 /**
800 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
801 * the estimated first hop transport bandwidth.
802 * <p>
803 * Note that when used to request a network, this specifies the minimum acceptable.
804 * When received as the state of an existing network this specifies the typical
805 * first hop bandwidth expected. This is never measured, but rather is inferred
806 * from technology type and other link parameters. It could be used to differentiate
807 * between very slow 1xRTT cellular links and other faster networks or even between
808 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
809 * fast backhauls and slow backhauls.
810 *
811 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700812 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700813 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600814 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700815 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600816 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700817 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700818
819 /**
820 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
821 * the estimated first hop transport bandwidth.
822 *
823 * @return The estimated first hop downstream (network to device) bandwidth.
824 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700825 public int getLinkDownstreamBandwidthKbps() {
826 return mLinkDownBandwidthKbps;
827 }
828
829 private void combineLinkBandwidths(NetworkCapabilities nc) {
830 this.mLinkUpBandwidthKbps =
831 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
832 this.mLinkDownBandwidthKbps =
833 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
834 }
835 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
836 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
837 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
838 }
839 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
840 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
841 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
842 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600843 /** @hide */
844 public static int minBandwidth(int a, int b) {
845 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
846 return b;
847 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
848 return a;
849 } else {
850 return Math.min(a, b);
851 }
852 }
853 /** @hide */
854 public static int maxBandwidth(int a, int b) {
855 return Math.max(a, b);
856 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700857
Etan Cohena7434272017-04-03 12:17:51 -0700858 private NetworkSpecifier mNetworkSpecifier = null;
859
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700860 /**
861 * Sets the optional bearer specific network specifier.
862 * This has no meaning if a single transport is also not specified, so calling
863 * this without a single transport set will generate an exception, as will
864 * subsequently adding or removing transports after this is set.
865 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700866 *
Etan Cohena7434272017-04-03 12:17:51 -0700867 * @param networkSpecifier A concrete, parcelable framework class that extends
868 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900869 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700870 * @hide
871 */
Etan Cohena7434272017-04-03 12:17:51 -0700872 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
873 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700874 throw new IllegalStateException("Must have a single transport specified to use " +
875 "setNetworkSpecifier");
876 }
Etan Cohena7434272017-04-03 12:17:51 -0700877
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700878 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700879
Pierre Imaic8419a82016-03-22 17:54:54 +0900880 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700881 }
882
883 /**
884 * Gets the optional bearer specific network specifier.
885 *
Etan Cohena7434272017-04-03 12:17:51 -0700886 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
887 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700888 * @hide
889 */
Etan Cohena7434272017-04-03 12:17:51 -0700890 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700891 return mNetworkSpecifier;
892 }
893
894 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700895 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700896 throw new IllegalStateException("Can't combine two networkSpecifiers");
897 }
Etan Cohena7434272017-04-03 12:17:51 -0700898 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700899 }
Etan Cohena7434272017-04-03 12:17:51 -0700900
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700901 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700902 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
903 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700904 }
Etan Cohena7434272017-04-03 12:17:51 -0700905
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700906 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700907 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700908 }
909
Robert Greenwalt1448f052014-04-08 13:41:39 -0700910 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900911 * Magic value that indicates no signal strength provided. A request specifying this value is
912 * always satisfied.
913 *
914 * @hide
915 */
916 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
917
918 /**
919 * Signal strength. This is a signed integer, and higher values indicate better signal.
920 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
921 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700922 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900923
924 /**
925 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
926 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900927 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900928 * <p>
929 * Note that when used to register a network callback, this specifies the minimum acceptable
930 * signal strength. When received as the state of an existing network it specifies the current
931 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
932 * effect when requesting a callback.
933 *
934 * @param signalStrength the bearer-specific signal strength.
935 * @hide
936 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600937 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900938 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600939 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900940 }
941
942 /**
943 * Returns {@code true} if this object specifies a signal strength.
944 *
945 * @hide
946 */
947 public boolean hasSignalStrength() {
948 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
949 }
950
951 /**
952 * Retrieves the signal strength.
953 *
954 * @return The bearer-specific signal strength.
955 * @hide
956 */
957 public int getSignalStrength() {
958 return mSignalStrength;
959 }
960
961 private void combineSignalStrength(NetworkCapabilities nc) {
962 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
963 }
964
965 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
966 return this.mSignalStrength <= nc.mSignalStrength;
967 }
968
969 private boolean equalsSignalStrength(NetworkCapabilities nc) {
970 return this.mSignalStrength == nc.mSignalStrength;
971 }
972
973 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900974 * List of UIDs this network applies to. No restriction if null.
975 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900976 * For networks, mUids represent the list of network this applies to, and null means this
977 * network applies to all UIDs.
978 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
979 * must be included in a network so that they match. As an exception to the general rule,
980 * a null mUids field for requests mean "no requirements" rather than what the general rule
981 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
982 * of this API expect in practice. A network that must match all UIDs can still be
983 * expressed with a set ranging the entire set of possible UIDs.
984 * <p>
985 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900986 * the UIDs in this list, and it is their default network. Apps in this list that wish to
987 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
988 * member is null, then the network is not restricted by app UID. If it's an empty list, then
989 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900990 * As a special exception, the app managing this network (as identified by its UID stored in
991 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
992 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
993 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900994 * <p>
995 * Please note that in principle a single app can be associated with multiple UIDs because
996 * each app will have a different UID when it's run as a different (macro-)user. A single
997 * macro user can only have a single active VPN app at any given time however.
998 * <p>
999 * Also please be aware this class does not try to enforce any normalization on this. Callers
1000 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1001 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1002 * their own (like requiring sortedness or no overlap) they need to enforce it
1003 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1004 * or overlapping ranges are present.
1005 *
1006 * @hide
1007 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001008 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001009
1010 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001011 * Convenience method to set the UIDs this network applies to to a single UID.
1012 * @hide
1013 */
1014 public NetworkCapabilities setSingleUid(int uid) {
1015 final ArraySet<UidRange> identity = new ArraySet<>(1);
1016 identity.add(new UidRange(uid, uid));
1017 setUids(identity);
1018 return this;
1019 }
1020
1021 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001022 * Set the list of UIDs this network applies to.
1023 * This makes a copy of the set so that callers can't modify it after the call.
1024 * @hide
1025 */
1026 public NetworkCapabilities setUids(Set<UidRange> uids) {
1027 if (null == uids) {
1028 mUids = null;
1029 } else {
1030 mUids = new ArraySet<>(uids);
1031 }
1032 return this;
1033 }
1034
1035 /**
1036 * Get the list of UIDs this network applies to.
1037 * This returns a copy of the set so that callers can't modify the original object.
1038 * @hide
1039 */
1040 public Set<UidRange> getUids() {
1041 return null == mUids ? null : new ArraySet<>(mUids);
1042 }
1043
1044 /**
1045 * Test whether this network applies to this UID.
1046 * @hide
1047 */
1048 public boolean appliesToUid(int uid) {
1049 if (null == mUids) return true;
1050 for (UidRange range : mUids) {
1051 if (range.contains(uid)) {
1052 return true;
1053 }
1054 }
1055 return false;
1056 }
1057
1058 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001059 * 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 +09001060 * <p>
1061 * This test only checks whether equal range objects are in both sets. It will
1062 * return false if the ranges are not exactly the same, even if the covered UIDs
1063 * are for an equivalent result.
1064 * <p>
1065 * Note that this method is not very optimized, which is fine as long as it's not used very
1066 * often.
1067 * <p>
1068 * nc is assumed nonnull.
1069 *
1070 * @hide
1071 */
1072 @VisibleForTesting
1073 public boolean equalsUids(NetworkCapabilities nc) {
1074 Set<UidRange> comparedUids = nc.mUids;
1075 if (null == comparedUids) return null == mUids;
1076 if (null == mUids) return false;
1077 // Make a copy so it can be mutated to check that all ranges in mUids
1078 // also are in uids.
1079 final Set<UidRange> uids = new ArraySet<>(mUids);
1080 for (UidRange range : comparedUids) {
1081 if (!uids.contains(range)) {
1082 return false;
1083 }
1084 uids.remove(range);
1085 }
1086 return uids.isEmpty();
1087 }
1088
1089 /**
1090 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1091 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001092 * This method is called on the NetworkCapabilities embedded in a request with the
1093 * capabilities of an available network. It checks whether all the UIDs from this listen
1094 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1095 * in the passed nc (representing the UIDs that this network is available to).
1096 * <p>
1097 * As a special exception, the UID that created the passed network (as represented by its
1098 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1099 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1100 * can see its own network when it listens for it.
1101 * <p>
1102 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001103 * @see #appliesToUid
1104 * @hide
1105 */
1106 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001107 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001108 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001109 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001110 if (!nc.appliesToUidRange(requiredRange)) {
1111 return false;
1112 }
1113 }
1114 return true;
1115 }
1116
1117 /**
1118 * Returns whether this network applies to the passed ranges.
1119 * This assumes that to apply, the passed range has to be entirely contained
1120 * within one of the ranges this network applies to. If the ranges are not normalized,
1121 * this method may return false even though all required UIDs are covered because no
1122 * single range contained them all.
1123 * @hide
1124 */
1125 @VisibleForTesting
1126 public boolean appliesToUidRange(UidRange requiredRange) {
1127 if (null == mUids) return true;
1128 for (UidRange uidRange : mUids) {
1129 if (uidRange.containsRange(requiredRange)) {
1130 return true;
1131 }
1132 }
1133 return false;
1134 }
1135
1136 /**
1137 * Combine the UIDs this network currently applies to with the UIDs the passed
1138 * NetworkCapabilities apply to.
1139 * nc is assumed nonnull.
1140 */
1141 private void combineUids(NetworkCapabilities nc) {
1142 if (null == nc.mUids || null == mUids) {
1143 mUids = null;
1144 return;
1145 }
1146 mUids.addAll(nc.mUids);
1147 }
1148
Chalard Jeanb03a6222018-04-11 21:09:10 +09001149
1150 /**
1151 * The SSID of the network, or null if not applicable or unknown.
1152 * <p>
1153 * This is filled in by wifi code.
1154 * @hide
1155 */
1156 private String mSSID;
1157
1158 /**
1159 * Sets the SSID of this network.
1160 * @hide
1161 */
1162 public NetworkCapabilities setSSID(String ssid) {
1163 mSSID = ssid;
1164 return this;
1165 }
1166
1167 /**
1168 * Gets the SSID of this network, or null if none or unknown.
1169 * @hide
1170 */
1171 public String getSSID() {
1172 return mSSID;
1173 }
1174
1175 /**
1176 * Tests if the SSID of this network is the same as the SSID of the passed network.
1177 * @hide
1178 */
1179 public boolean equalsSSID(NetworkCapabilities nc) {
1180 return Objects.equals(mSSID, nc.mSSID);
1181 }
1182
1183 /**
1184 * Check if the SSID requirements of this object are matched by the passed object.
1185 * @hide
1186 */
1187 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1188 return mSSID == null || mSSID.equals(nc.mSSID);
1189 }
1190
1191 /**
1192 * Combine SSIDs of the capabilities.
1193 * <p>
1194 * This is only legal if either the SSID of this object is null, or both SSIDs are
1195 * equal.
1196 * @hide
1197 */
1198 private void combineSSIDs(NetworkCapabilities nc) {
1199 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1200 throw new IllegalStateException("Can't combine two SSIDs");
1201 }
1202 setSSID(nc.mSSID);
1203 }
1204
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001205 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001206 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001207 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001208 */
1209 public void combineCapabilities(NetworkCapabilities nc) {
1210 combineNetCapabilities(nc);
1211 combineTransportTypes(nc);
1212 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001213 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001214 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001215 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001216 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001217 }
1218
1219 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001220 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1221 *
1222 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1223 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1224 * bandwidth, signal strength, or validation / captive portal status.
1225 *
1226 * @hide
1227 */
1228 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001229 return (nc != null
1230 && satisfiedByNetCapabilities(nc, onlyImmutable)
1231 && satisfiedByTransportTypes(nc)
1232 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1233 && satisfiedBySpecifier(nc)
1234 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001235 && (onlyImmutable || satisfiedByUids(nc))
1236 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001237 }
1238
1239 /**
1240 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1241 *
1242 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1243 *
1244 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001245 */
1246 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001247 return satisfiedByNetworkCapabilities(nc, false);
1248 }
1249
1250 /**
1251 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1252 *
1253 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1254 *
1255 * @hide
1256 */
1257 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1258 return satisfiedByNetworkCapabilities(nc, true);
1259 }
1260
1261 /**
1262 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001263 * {@code NetworkCapabilities} and return a String describing any difference.
1264 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001265 *
1266 * @hide
1267 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001268 public String describeImmutableDifferences(NetworkCapabilities that) {
1269 if (that == null) {
1270 return "other NetworkCapabilities was null";
1271 }
1272
1273 StringJoiner joiner = new StringJoiner(", ");
1274
Hugo Benichieae7a222017-07-25 11:40:56 +09001275 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1276 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001277 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001278 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1279 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1280 if (oldImmutableCapabilities != newImmutableCapabilities) {
1281 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1282 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1283 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1284 }
1285
1286 if (!equalsSpecifier(that)) {
1287 NetworkSpecifier before = this.getNetworkSpecifier();
1288 NetworkSpecifier after = that.getNetworkSpecifier();
1289 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1290 }
1291
1292 if (!equalsTransportTypes(that)) {
1293 String before = transportNamesOf(this.getTransportTypes());
1294 String after = transportNamesOf(that.getTransportTypes());
1295 joiner.add(String.format("transports changed: %s -> %s", before, after));
1296 }
1297
1298 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001299 }
1300
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001301 /**
1302 * Checks that our requestable capabilities are the same as those of the given
1303 * {@code NetworkCapabilities}.
1304 *
1305 * @hide
1306 */
1307 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1308 if (nc == null) return false;
1309 return (equalsNetCapabilitiesRequestable(nc) &&
1310 equalsTransportTypes(nc) &&
1311 equalsSpecifier(nc));
1312 }
1313
Robert Greenwalt1448f052014-04-08 13:41:39 -07001314 @Override
1315 public boolean equals(Object obj) {
1316 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001317 NetworkCapabilities that = (NetworkCapabilities) obj;
1318 return (equalsNetCapabilities(that)
1319 && equalsTransportTypes(that)
1320 && equalsLinkBandwidths(that)
1321 && equalsSignalStrength(that)
1322 && equalsSpecifier(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001323 && equalsUids(that)
1324 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001325 }
1326
1327 @Override
1328 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001329 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001330 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001331 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1332 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1333 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1334 + ((int) (mTransportTypes >> 32) * 13)
1335 + (mLinkUpBandwidthKbps * 17)
1336 + (mLinkDownBandwidthKbps * 19)
1337 + Objects.hashCode(mNetworkSpecifier) * 23
1338 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001339 + Objects.hashCode(mUids) * 31
1340 + Objects.hashCode(mSSID) * 37;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001341 }
1342
Wink Saville4e2dea72014-09-20 11:04:03 -07001343 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001344 public int describeContents() {
1345 return 0;
1346 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001347 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001348 public void writeToParcel(Parcel dest, int flags) {
1349 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001350 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001351 dest.writeLong(mTransportTypes);
1352 dest.writeInt(mLinkUpBandwidthKbps);
1353 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001354 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001355 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001356 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001357 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001358 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001359
Robert Greenwalt1448f052014-04-08 13:41:39 -07001360 public static final Creator<NetworkCapabilities> CREATOR =
1361 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001362 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001363 public NetworkCapabilities createFromParcel(Parcel in) {
1364 NetworkCapabilities netCap = new NetworkCapabilities();
1365
1366 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001367 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001368 netCap.mTransportTypes = in.readLong();
1369 netCap.mLinkUpBandwidthKbps = in.readInt();
1370 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001371 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001372 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001373 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1374 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001375 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001376 return netCap;
1377 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001378 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001379 public NetworkCapabilities[] newArray(int size) {
1380 return new NetworkCapabilities[size];
1381 }
1382 };
1383
Wink Saville4e2dea72014-09-20 11:04:03 -07001384 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001385 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001386 final StringBuilder sb = new StringBuilder("[");
1387 if (0 != mTransportTypes) {
1388 sb.append(" Transports: ");
1389 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1390 NetworkCapabilities::transportNameOf, "|");
1391 }
1392 if (0 != mNetworkCapabilities) {
1393 sb.append(" Capabilities: ");
1394 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1395 NetworkCapabilities::capabilityNameOf, "&");
1396 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001397 if (0 != mNetworkCapabilities) {
1398 sb.append(" Unwanted: ");
1399 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1400 NetworkCapabilities::capabilityNameOf, "&");
1401 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001402 if (mLinkUpBandwidthKbps > 0) {
1403 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1404 }
1405 if (mLinkDownBandwidthKbps > 0) {
1406 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1407 }
1408 if (mNetworkSpecifier != null) {
1409 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1410 }
1411 if (hasSignalStrength()) {
1412 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001413 }
1414
Chalard Jean07ace0f2018-02-26 19:00:45 +09001415 if (null != mUids) {
1416 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1417 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1418 } else {
1419 sb.append(" Uids: <").append(mUids).append(">");
1420 }
1421 }
1422 if (mEstablishingVpnAppUid != INVALID_UID) {
1423 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1424 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001425
Chalard Jeanb03a6222018-04-11 21:09:10 +09001426 if (null != mSSID) {
1427 sb.append(" SSID: ").append(mSSID);
1428 }
1429
Chalard Jean07ace0f2018-02-26 19:00:45 +09001430 sb.append("]");
1431 return sb.toString();
1432 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001433
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001434
Chalard Jean07ace0f2018-02-26 19:00:45 +09001435 private interface NameOf {
1436 String nameOf(int value);
1437 }
1438 /**
1439 * @hide
1440 */
1441 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1442 long bitMask, NameOf nameFetcher, String separator) {
1443 int bitPos = 0;
1444 boolean firstElementAdded = false;
1445 while (bitMask != 0) {
1446 if ((bitMask & 1) != 0) {
1447 if (firstElementAdded) {
1448 sb.append(separator);
1449 } else {
1450 firstElementAdded = true;
1451 }
1452 sb.append(nameFetcher.nameOf(bitPos));
1453 }
1454 bitMask >>= 1;
1455 ++bitPos;
1456 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001457 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001458
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001459 /** @hide */
1460 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1461 final long token = proto.start(fieldId);
1462
1463 for (int transport : getTransportTypes()) {
1464 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1465 }
1466
1467 for (int capability : getCapabilities()) {
1468 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1469 }
1470
1471 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1472 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1473
1474 if (mNetworkSpecifier != null) {
1475 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1476 }
1477
1478 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1479 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1480
1481 proto.end(token);
1482 }
1483
Hugo Benichi5df9d722016-04-25 17:16:35 +09001484 /**
1485 * @hide
1486 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001487 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001488 StringJoiner joiner = new StringJoiner("|");
1489 if (capabilities != null) {
1490 for (int c : capabilities) {
1491 joiner.add(capabilityNameOf(c));
1492 }
1493 }
1494 return joiner.toString();
1495 }
1496
1497 /**
1498 * @hide
1499 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001500 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001501 switch (capability) {
1502 case NET_CAPABILITY_MMS: return "MMS";
1503 case NET_CAPABILITY_SUPL: return "SUPL";
1504 case NET_CAPABILITY_DUN: return "DUN";
1505 case NET_CAPABILITY_FOTA: return "FOTA";
1506 case NET_CAPABILITY_IMS: return "IMS";
1507 case NET_CAPABILITY_CBS: return "CBS";
1508 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1509 case NET_CAPABILITY_IA: return "IA";
1510 case NET_CAPABILITY_RCS: return "RCS";
1511 case NET_CAPABILITY_XCAP: return "XCAP";
1512 case NET_CAPABILITY_EIMS: return "EIMS";
1513 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1514 case NET_CAPABILITY_INTERNET: return "INTERNET";
1515 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1516 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1517 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1518 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1519 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001520 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001521 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001522 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001523 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001524 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001525 default: return Integer.toString(capability);
1526 }
1527 }
1528
1529 /**
1530 * @hide
1531 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001532 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001533 StringJoiner joiner = new StringJoiner("|");
1534 if (types != null) {
1535 for (int t : types) {
1536 joiner.add(transportNameOf(t));
1537 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001538 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001539 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001540 }
1541
1542 /**
1543 * @hide
1544 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001545 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001546 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001547 return "UNKNOWN";
1548 }
1549 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001550 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001551
Jeff Sharkeyde570312017-10-24 21:25:50 -06001552 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001553 Preconditions.checkArgument(
1554 isValidTransport(transport), "Invalid TransportType " + transport);
1555 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001556
1557 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1558 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1559 }
1560
1561 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1562 Preconditions.checkArgument(isValidCapability(capability),
1563 "NetworkCapability " + capability + "out of range");
1564 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001565}