blob: e1cfe991de8da80a7d800929639ac1330b7da130 [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;
Etan Cohenca9fb562018-11-27 07:32:39 -080020import android.annotation.Nullable;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070021import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060022import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010023import android.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060024import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010025import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070026import android.os.Parcel;
27import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090028import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080029import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070030
31import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090032import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090033import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070034
Jeff Sharkeyde570312017-10-24 21:25:50 -060035import java.lang.annotation.Retention;
36import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070037import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090038import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090039import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070040
41/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070042 * Representation of the capabilities of an active network. Instances are
43 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060044 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
45 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060046 * <p>
47 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
48 * network selection. Rather than indicate a need for Wi-Fi because an
49 * application needs high bandwidth and risk obsolescence when a new, fast
50 * network appears (like LTE), the application should specify it needs high
51 * bandwidth. Similarly if an application needs an unmetered network for a bulk
52 * transfer it can specify that rather than assuming all cellular based
53 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070054 */
55public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070056 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090057 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070058
Robert Greenwalt7569f182014-06-08 16:42:59 -070059 /**
60 * @hide
61 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010062 @UnsupportedAppUsage
Robert Greenwalt01d004e2014-05-18 15:24:21 -070063 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090064 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090065 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070066 }
67
68 public NetworkCapabilities(NetworkCapabilities nc) {
69 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090070 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070071 }
72 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070073
74 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090075 * Completely clears the contents of this object, removing even the capabilities that are set
76 * by default when the object is constructed.
77 * @hide
78 */
79 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080080 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070081 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090082 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080083 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090084 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090085 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090086 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090087 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090088 }
89
90 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090091 * Set all contents of this object to the contents of a NetworkCapabilities.
92 * @hide
93 */
94 public void set(NetworkCapabilities nc) {
95 mNetworkCapabilities = nc.mNetworkCapabilities;
96 mTransportTypes = nc.mTransportTypes;
97 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
98 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
99 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800100 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900101 mSignalStrength = nc.mSignalStrength;
102 setUids(nc.mUids); // Will make the defensive copy
103 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
104 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
105 mSSID = nc.mSSID;
106 }
107
108 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700109 * Represents the network's capabilities. If any are specified they will be satisfied
110 * by any Network that matches all of them.
111 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100112 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900113 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700114
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800115 /**
116 * If any capabilities specified here they must not exist in the matching Network.
117 */
118 private long mUnwantedNetworkCapabilities;
119
Jeff Sharkeyde570312017-10-24 21:25:50 -0600120 /** @hide */
121 @Retention(RetentionPolicy.SOURCE)
122 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
123 NET_CAPABILITY_MMS,
124 NET_CAPABILITY_SUPL,
125 NET_CAPABILITY_DUN,
126 NET_CAPABILITY_FOTA,
127 NET_CAPABILITY_IMS,
128 NET_CAPABILITY_CBS,
129 NET_CAPABILITY_WIFI_P2P,
130 NET_CAPABILITY_IA,
131 NET_CAPABILITY_RCS,
132 NET_CAPABILITY_XCAP,
133 NET_CAPABILITY_EIMS,
134 NET_CAPABILITY_NOT_METERED,
135 NET_CAPABILITY_INTERNET,
136 NET_CAPABILITY_NOT_RESTRICTED,
137 NET_CAPABILITY_TRUSTED,
138 NET_CAPABILITY_NOT_VPN,
139 NET_CAPABILITY_VALIDATED,
140 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600141 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600142 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900143 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900144 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800145 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800146 NET_CAPABILITY_MCX,
147 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600148 })
149 public @interface NetCapability { }
150
Robert Greenwalt1448f052014-04-08 13:41:39 -0700151 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700152 * Indicates this is a network that has the ability to reach the
153 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700154 */
155 public static final int NET_CAPABILITY_MMS = 0;
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 * SUPL server, used to retrieve GPS information.
160 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700161 public static final int NET_CAPABILITY_SUPL = 1;
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 * DUN or tethering gateway.
166 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700167 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700168
169 /**
170 * Indicates this is a network that has the ability to reach the carrier's
171 * FOTA portal, used for over the air updates.
172 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174
175 /**
176 * Indicates this is a network that has the ability to reach the carrier's
177 * IMS servers, used for network registration and signaling.
178 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach the carrier's
183 * CBS servers, used for carrier specific services.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach a Wi-Fi direct
189 * peer.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach a carrier's
195 * Initial Attach servers.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Indicates this is a network that has the ability to reach a carrier's
201 * RCS servers, used for Rich Communication Services.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates this is a network that has the ability to reach a carrier's
207 * XCAP servers, used for configuration and control.
208 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700209 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700210
211 /**
212 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700213 * Emergency IMS servers or other services, used for network signaling
214 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700217
218 /**
219 * Indicates that this network is unmetered.
220 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700221 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700222
223 /**
224 * Indicates that this network should be able to reach the internet.
225 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700226 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700227
228 /**
229 * Indicates that this network is available for general use. If this is not set
230 * applications should not attempt to communicate on this network. Note that this
231 * is simply informative and not enforcement - enforcement is handled via other means.
232 * Set by default.
233 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700234 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
235
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700236 /**
237 * Indicates that the user has indicated implicit trust of this network. This
238 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
239 * BT device or a wifi the user asked to connect to. Untrusted networks
240 * are probably limited to unknown wifi AP. Set by default.
241 */
242 public static final int NET_CAPABILITY_TRUSTED = 14;
243
Paul Jensen76b610a2015-03-18 09:33:07 -0400244 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400245 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400246 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400247 */
248 public static final int NET_CAPABILITY_NOT_VPN = 15;
249
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900250 /**
251 * Indicates that connectivity on this network was successfully validated. For example, for a
252 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
253 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900254 */
255 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700256
Paul Jensen3d194ea2015-06-16 14:27:36 -0400257 /**
258 * Indicates that this network was found to have a captive portal in place last time it was
259 * probed.
260 */
261 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
262
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900263 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600264 * Indicates that this network is not roaming.
265 */
266 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
267
268 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900269 * Indicates that this network is available for use by apps, and not a network that is being
270 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900271 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600272 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900273
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900274 /**
275 * Indicates that this network is not congested.
276 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600277 * When a network is congested, applications should defer network traffic
278 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900279 */
280 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
281
Chalard Jean804b8fb2018-01-30 22:41:41 +0900282 /**
283 * Indicates that this network is not currently suspended.
284 * <p>
285 * When a network is suspended, the network's IP addresses and any connections
286 * established on the network remain valid, but the network is temporarily unable
287 * to transfer data. This can happen, for example, if a cellular network experiences
288 * a temporary loss of signal, such as when driving through a tunnel, etc.
289 * A network with this capability is not suspended, so is expected to be able to
290 * transfer data.
291 */
292 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
293
Pavel Maltsev43403202018-01-30 17:19:44 -0800294 /**
295 * Indicates that traffic that goes through this network is paid by oem. For example,
296 * this network can be used by system apps to upload telemetry data.
297 * @hide
298 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700299 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800300 public static final int NET_CAPABILITY_OEM_PAID = 22;
301
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800302 /**
303 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
304 * servers.
305 */
306 public static final int NET_CAPABILITY_MCX = 23;
307
lucasline252a742019-03-12 13:08:03 +0800308 /**
309 * Indicates that this network was tested to only provide partial connectivity.
310 * @hide
311 */
312 @SystemApi
313 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
314
Robert Greenwalt1448f052014-04-08 13:41:39 -0700315 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800316 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700317
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700318 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900319 * Network capabilities that are expected to be mutable, i.e., can change while a particular
320 * network is connected.
321 */
322 private static final long MUTABLE_CAPABILITIES =
323 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
324 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900325 (1 << NET_CAPABILITY_TRUSTED)
326 | (1 << NET_CAPABILITY_VALIDATED)
327 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
328 | (1 << NET_CAPABILITY_NOT_ROAMING)
329 | (1 << NET_CAPABILITY_FOREGROUND)
330 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800331 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
332 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900333
334 /**
335 * Network capabilities that are not allowed in NetworkRequests. This exists because the
336 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
337 * capability's presence cannot be known in advance. If such a capability is requested, then we
338 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
339 * get immediately torn down because they do not have the requested capability.
340 */
341 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900342 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900343
344 /**
345 * Capabilities that are set by default when the object is constructed.
346 */
347 private static final long DEFAULT_CAPABILITIES =
348 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
349 (1 << NET_CAPABILITY_TRUSTED) |
350 (1 << NET_CAPABILITY_NOT_VPN);
351
352 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400353 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800354 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400355 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700356 @VisibleForTesting
357 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400358 (1 << NET_CAPABILITY_CBS) |
359 (1 << NET_CAPABILITY_DUN) |
360 (1 << NET_CAPABILITY_EIMS) |
361 (1 << NET_CAPABILITY_FOTA) |
362 (1 << NET_CAPABILITY_IA) |
363 (1 << NET_CAPABILITY_IMS) |
364 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800365 (1 << NET_CAPABILITY_XCAP) |
366 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800367
368 /**
369 * Capabilities that force network to be restricted.
370 * {@see #maybeMarkCapabilitiesRestricted}.
371 */
372 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800373 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400374
375 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700376 * Capabilities that suggest that a network is unrestricted.
377 * {@see #maybeMarkCapabilitiesRestricted}.
378 */
379 @VisibleForTesting
380 /* package */ static final long UNRESTRICTED_CAPABILITIES =
381 (1 << NET_CAPABILITY_INTERNET) |
382 (1 << NET_CAPABILITY_MMS) |
383 (1 << NET_CAPABILITY_SUPL) |
384 (1 << NET_CAPABILITY_WIFI_P2P);
385
386 /**
lucasline252a742019-03-12 13:08:03 +0800387 * Capabilities that are managed by ConnectivityService.
388 */
389 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
390 (1 << NET_CAPABILITY_VALIDATED)
391 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
392 | (1 << NET_CAPABILITY_FOREGROUND)
393 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
394
395 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700396 * Adds the given capability to this {@code NetworkCapability} instance.
397 * Multiple capabilities may be applied sequentially. Note that when searching
398 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800399 * <p>
400 * If the given capability was previously added to the list of unwanted capabilities
401 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700402 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600403 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900404 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700405 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700406 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100407 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600408 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800409 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700410 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800411 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700412 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700413 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700414
415 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800416 * Adds the given capability to the list of unwanted capabilities of this
417 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
418 * sequentially. Note that when searching for a network to satisfy a request, the network
419 * must not contain any capability from unwanted capability list.
420 * <p>
421 * If the capability was previously added to the list of required capabilities (for
422 * example, it was there by default or added using {@link #addCapability(int)} method), then
423 * it will be removed from the list of required capabilities as well.
424 *
425 * @see #addCapability(int)
426 * @hide
427 */
428 public void addUnwantedCapability(@NetCapability int capability) {
429 checkValidCapability(capability);
430 mUnwantedNetworkCapabilities |= 1 << capability;
431 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
432 }
433
434 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700435 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800436 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800437 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800438 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700439 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600440 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900441 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700442 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700443 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100444 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600445 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800446 checkValidCapability(capability);
447 final long mask = ~(1 << capability);
448 mNetworkCapabilities &= mask;
449 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700450 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700451 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452
453 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600454 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
455 * instance.
456 *
457 * @hide
458 */
459 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
460 if (value) {
461 addCapability(capability);
462 } else {
463 removeCapability(capability);
464 }
465 return this;
466 }
467
468 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700469 * Gets all the capabilities set on this {@code NetworkCapability} instance.
470 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600471 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700472 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600474 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600475 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900476 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700477 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478
479 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800480 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
481 *
482 * @return an array of unwanted capability values for this instance.
483 * @hide
484 */
485 public @NetCapability int[] getUnwantedCapabilities() {
486 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
487 }
488
489
490 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600491 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700492 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600493 *
494 * @hide
495 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800496 public void setCapabilities(@NetCapability int[] capabilities,
497 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600498 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800499 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600500 }
501
502 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800503 * @deprecated use {@link #setCapabilities(int[], int[])}
504 * @hide
505 */
506 @Deprecated
507 public void setCapabilities(@NetCapability int[] capabilities) {
508 setCapabilities(capabilities, new int[] {});
509 }
510
511 /**
512 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700513 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600514 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515 * @return {@code true} if set on this instance.
516 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600517 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800518 return isValidCapability(capability)
519 && ((mNetworkCapabilities & (1 << capability)) != 0);
520 }
521
522 /** @hide */
523 public boolean hasUnwantedCapability(@NetCapability int capability) {
524 return isValidCapability(capability)
525 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700526 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700527
lucasline252a742019-03-12 13:08:03 +0800528 /**
529 * Check if this NetworkCapabilities has system managed capabilities or not.
530 * @hide
531 */
532 public boolean hasConnectivityManagedCapability() {
533 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
534 }
535
Pavel Maltseve18ef262018-03-07 11:13:04 -0800536 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700537 private void combineNetCapabilities(NetworkCapabilities nc) {
538 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800539 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700540 }
541
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900542 /**
543 * Convenience function that returns a human-readable description of the first mutable
544 * capability we find. Used to present an error message to apps that request mutable
545 * capabilities.
546 *
547 * @hide
548 */
549 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800550 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
551 & NON_REQUESTABLE_CAPABILITIES;
552
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900553 if (nonRequestable != 0) {
554 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900555 }
556 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900557 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900558 return null;
559 }
560
561 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800562 long requestedCapabilities = mNetworkCapabilities;
563 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
564 long providedCapabilities = nc.mNetworkCapabilities;
565
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900566 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800567 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
568 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900569 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800570 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
571 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700572 }
573
Robert Greenwalt06314e42014-10-29 14:04:06 -0700574 /** @hide */
575 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800576 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
577 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700578 }
579
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900580 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
581 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800582 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
583 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
584 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900585 }
586
Robert Greenwalt1448f052014-04-08 13:41:39 -0700587 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400588 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
589 * typically provided by restricted networks.
590 *
591 * TODO: consider:
592 * - Renaming it to guessRestrictedCapability and make it set the
593 * restricted capability bit in addition to clearing it.
594 * @hide
595 */
596 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800597 // Check if we have any capability that forces the network to be restricted.
598 final boolean forceRestrictedCapability =
599 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
600
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700601 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800602 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700603 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800604 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700605
606 // Must have at least some restricted capabilities.
607 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800608 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700609
Pavel Maltsev4af91072018-03-07 14:33:22 -0800610 if (forceRestrictedCapability
611 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400612 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400613 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400614 }
615
616 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700617 * Representing the transport type. Apps should generally not care about transport. A
618 * request for a fast internet connection could be satisfied by a number of different
619 * transports. If any are specified here it will be satisfied a Network that matches
620 * any of them. If a caller doesn't care about the transport it should not specify any.
621 */
622 private long mTransportTypes;
623
Jeff Sharkeyde570312017-10-24 21:25:50 -0600624 /** @hide */
625 @Retention(RetentionPolicy.SOURCE)
626 @IntDef(prefix = { "TRANSPORT_" }, value = {
627 TRANSPORT_CELLULAR,
628 TRANSPORT_WIFI,
629 TRANSPORT_BLUETOOTH,
630 TRANSPORT_ETHERNET,
631 TRANSPORT_VPN,
632 TRANSPORT_WIFI_AWARE,
633 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800634 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600635 })
636 public @interface Transport { }
637
Robert Greenwalt1448f052014-04-08 13:41:39 -0700638 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700639 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700640 */
641 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700642
643 /**
644 * Indicates this network uses a Wi-Fi transport.
645 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700646 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700647
648 /**
649 * Indicates this network uses a Bluetooth transport.
650 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700651 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700652
653 /**
654 * Indicates this network uses an Ethernet transport.
655 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700656 public static final int TRANSPORT_ETHERNET = 3;
657
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400658 /**
659 * Indicates this network uses a VPN transport.
660 */
661 public static final int TRANSPORT_VPN = 4;
662
Etan Cohen305ea282016-06-20 09:27:12 -0700663 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700664 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700665 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700666 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700667
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700668 /**
669 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700670 */
671 public static final int TRANSPORT_LOWPAN = 6;
672
Benedict Wong89ce5e32018-11-14 17:40:55 -0800673 /**
674 * Indicates this network uses a Test-only virtual interface as a transport.
675 *
676 * @hide
677 */
678 @TestApi
679 public static final int TRANSPORT_TEST = 7;
680
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900681 /** @hide */
682 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
683 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800684 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700685
Hugo Benichi16f0a942017-06-20 14:07:59 +0900686 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600687 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900688 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
689 }
690
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900691 private static final String[] TRANSPORT_NAMES = {
692 "CELLULAR",
693 "WIFI",
694 "BLUETOOTH",
695 "ETHERNET",
696 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700697 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800698 "LOWPAN",
699 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900700 };
701
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700702 /**
703 * Adds the given transport type to this {@code NetworkCapability} instance.
704 * Multiple transports may be applied sequentially. Note that when searching
705 * for a network to satisfy a request, any listed in the request will satisfy the request.
706 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
707 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
708 * to be selected. This is logically different than
709 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
710 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900712 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700713 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700714 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100715 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600716 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900717 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700718 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700719 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700720 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700721 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700722
723 /**
724 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
725 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600726 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900727 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700728 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700729 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600730 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900731 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700732 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700733 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700734 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700735 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700736
737 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600738 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
739 * instance.
740 *
741 * @hide
742 */
743 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
744 if (value) {
745 addTransportType(transportType);
746 } else {
747 removeTransportType(transportType);
748 }
749 return this;
750 }
751
752 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700753 * Gets all the transports set on this {@code NetworkCapability} instance.
754 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600755 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700756 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700757 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600758 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900759 @SystemApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600760 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900761 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700762 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700763
764 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600765 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700766 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600767 *
768 * @hide
769 */
770 public void setTransportTypes(@Transport int[] transportTypes) {
771 mTransportTypes = BitUtils.packBits(transportTypes);
772 }
773
774 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700775 * Tests for the presence of a transport on this instance.
776 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600777 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700778 * @return {@code true} if set on this instance.
779 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600780 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900781 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700782 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700783
784 private void combineTransportTypes(NetworkCapabilities nc) {
785 this.mTransportTypes |= nc.mTransportTypes;
786 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900787
Robert Greenwalt1448f052014-04-08 13:41:39 -0700788 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
789 return ((this.mTransportTypes == 0) ||
790 ((this.mTransportTypes & nc.mTransportTypes) != 0));
791 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900792
Robert Greenwalt06314e42014-10-29 14:04:06 -0700793 /** @hide */
794 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700795 return (nc.mTransportTypes == this.mTransportTypes);
796 }
797
798 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900799 * UID of the app that manages this network, or INVALID_UID if none/unknown.
800 *
801 * This field keeps track of the UID of the app that created this network and is in charge
802 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
803 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
804 * to create networks.
805 *
806 * Because this field is only used in the services side (and to avoid apps being able to
807 * set this to whatever they want), this field is not parcelled and will not be conserved
808 * across the IPC boundary.
809 * @hide
810 */
811 private int mEstablishingVpnAppUid = INVALID_UID;
812
813 /**
814 * Set the UID of the managing app.
815 * @hide
816 */
817 public void setEstablishingVpnAppUid(final int uid) {
818 mEstablishingVpnAppUid = uid;
819 }
820
821 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600822 * Value indicating that link bandwidth is unspecified.
823 * @hide
824 */
825 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
826
827 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700828 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
829 * for the first hop on the given transport. It is not measured, but may take into account
830 * link parameters (Radio technology, allocated channels, etc).
831 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600832 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
833 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700834
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700835 /**
836 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
837 * the estimated first hop transport bandwidth.
838 * <p>
839 * Note that when used to request a network, this specifies the minimum acceptable.
840 * When received as the state of an existing network this specifies the typical
841 * first hop bandwidth expected. This is never measured, but rather is inferred
842 * from technology type and other link parameters. It could be used to differentiate
843 * between very slow 1xRTT cellular links and other faster networks or even between
844 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
845 * fast backhauls and slow backhauls.
846 *
847 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700848 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700849 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600850 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700851 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600852 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700853 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700854
855 /**
856 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
857 * the estimated first hop transport bandwidth.
858 *
859 * @return The estimated first hop upstream (device to network) bandwidth.
860 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700861 public int getLinkUpstreamBandwidthKbps() {
862 return mLinkUpBandwidthKbps;
863 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700864
865 /**
866 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
867 * the estimated first hop transport bandwidth.
868 * <p>
869 * Note that when used to request a network, this specifies the minimum acceptable.
870 * When received as the state of an existing network this specifies the typical
871 * first hop bandwidth expected. This is never measured, but rather is inferred
872 * from technology type and other link parameters. It could be used to differentiate
873 * between very slow 1xRTT cellular links and other faster networks or even between
874 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
875 * fast backhauls and slow backhauls.
876 *
877 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700878 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700879 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600880 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700881 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600882 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700883 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700884
885 /**
886 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
887 * the estimated first hop transport bandwidth.
888 *
889 * @return The estimated first hop downstream (network to device) bandwidth.
890 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700891 public int getLinkDownstreamBandwidthKbps() {
892 return mLinkDownBandwidthKbps;
893 }
894
895 private void combineLinkBandwidths(NetworkCapabilities nc) {
896 this.mLinkUpBandwidthKbps =
897 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
898 this.mLinkDownBandwidthKbps =
899 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
900 }
901 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
902 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
903 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
904 }
905 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
906 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
907 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
908 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600909 /** @hide */
910 public static int minBandwidth(int a, int b) {
911 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
912 return b;
913 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
914 return a;
915 } else {
916 return Math.min(a, b);
917 }
918 }
919 /** @hide */
920 public static int maxBandwidth(int a, int b) {
921 return Math.max(a, b);
922 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700923
Etan Cohena7434272017-04-03 12:17:51 -0700924 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -0800925 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -0700926
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700927 /**
928 * Sets the optional bearer specific network specifier.
929 * This has no meaning if a single transport is also not specified, so calling
930 * this without a single transport set will generate an exception, as will
931 * subsequently adding or removing transports after this is set.
932 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700933 *
Etan Cohena7434272017-04-03 12:17:51 -0700934 * @param networkSpecifier A concrete, parcelable framework class that extends
935 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900936 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700937 * @hide
938 */
Etan Cohena7434272017-04-03 12:17:51 -0700939 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
940 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700941 throw new IllegalStateException("Must have a single transport specified to use " +
942 "setNetworkSpecifier");
943 }
Etan Cohena7434272017-04-03 12:17:51 -0700944
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700945 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700946
Pierre Imaic8419a82016-03-22 17:54:54 +0900947 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700948 }
949
950 /**
Etan Cohenca9fb562018-11-27 07:32:39 -0800951 * Sets the optional transport specific information.
952 *
953 * @param transportInfo A concrete, parcelable framework class that extends
954 * {@link TransportInfo}.
955 * @return This NetworkCapabilities instance, to facilitate chaining.
956 * @hide
957 */
958 public NetworkCapabilities setTransportInfo(TransportInfo transportInfo) {
959 mTransportInfo = transportInfo;
960 return this;
961 }
962
963 /**
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700964 * Gets the optional bearer specific network specifier.
965 *
Etan Cohena7434272017-04-03 12:17:51 -0700966 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
967 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700968 * @hide
969 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100970 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Etan Cohena7434272017-04-03 12:17:51 -0700971 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700972 return mNetworkSpecifier;
973 }
974
Etan Cohenca9fb562018-11-27 07:32:39 -0800975 /**
976 * Returns a transport-specific information container. The application may cast this
977 * container to a concrete sub-class based on its knowledge of the network request. The
978 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -0800979 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -0800980 *
981 * @return A concrete implementation of the {@link TransportInfo} class or null if not
982 * available for the network.
983 */
984 @Nullable public TransportInfo getTransportInfo() {
985 return mTransportInfo;
986 }
987
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700988 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700989 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700990 throw new IllegalStateException("Can't combine two networkSpecifiers");
991 }
Etan Cohena7434272017-04-03 12:17:51 -0700992 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700993 }
Etan Cohena7434272017-04-03 12:17:51 -0700994
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700995 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700996 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
997 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700998 }
Etan Cohena7434272017-04-03 12:17:51 -0700999
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001000 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001001 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001002 }
1003
Etan Cohenca9fb562018-11-27 07:32:39 -08001004 private void combineTransportInfos(NetworkCapabilities nc) {
1005 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1006 throw new IllegalStateException("Can't combine two TransportInfos");
1007 }
1008 setTransportInfo(nc.mTransportInfo);
1009 }
1010
1011 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1012 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1013 }
1014
Robert Greenwalt1448f052014-04-08 13:41:39 -07001015 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001016 * Magic value that indicates no signal strength provided. A request specifying this value is
1017 * always satisfied.
1018 *
1019 * @hide
1020 */
1021 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1022
1023 /**
1024 * Signal strength. This is a signed integer, and higher values indicate better signal.
1025 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1026 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001027 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001028 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001029
1030 /**
1031 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1032 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001033 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001034 * <p>
1035 * Note that when used to register a network callback, this specifies the minimum acceptable
1036 * signal strength. When received as the state of an existing network it specifies the current
1037 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1038 * effect when requesting a callback.
1039 *
1040 * @param signalStrength the bearer-specific signal strength.
1041 * @hide
1042 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001043 @UnsupportedAppUsage
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001044 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001045 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001046 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001047 }
1048
1049 /**
1050 * Returns {@code true} if this object specifies a signal strength.
1051 *
1052 * @hide
1053 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001054 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001055 public boolean hasSignalStrength() {
1056 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1057 }
1058
1059 /**
1060 * Retrieves the signal strength.
1061 *
1062 * @return The bearer-specific signal strength.
1063 * @hide
1064 */
paulhu7610bc72018-12-12 17:52:57 +08001065 @SystemApi
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001066 public int getSignalStrength() {
1067 return mSignalStrength;
1068 }
1069
1070 private void combineSignalStrength(NetworkCapabilities nc) {
1071 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1072 }
1073
1074 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1075 return this.mSignalStrength <= nc.mSignalStrength;
1076 }
1077
1078 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1079 return this.mSignalStrength == nc.mSignalStrength;
1080 }
1081
1082 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001083 * List of UIDs this network applies to. No restriction if null.
1084 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001085 * For networks, mUids represent the list of network this applies to, and null means this
1086 * network applies to all UIDs.
1087 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1088 * must be included in a network so that they match. As an exception to the general rule,
1089 * a null mUids field for requests mean "no requirements" rather than what the general rule
1090 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1091 * of this API expect in practice. A network that must match all UIDs can still be
1092 * expressed with a set ranging the entire set of possible UIDs.
1093 * <p>
1094 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001095 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1096 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1097 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1098 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001099 * As a special exception, the app managing this network (as identified by its UID stored in
1100 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1101 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1102 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001103 * <p>
1104 * Please note that in principle a single app can be associated with multiple UIDs because
1105 * each app will have a different UID when it's run as a different (macro-)user. A single
1106 * macro user can only have a single active VPN app at any given time however.
1107 * <p>
1108 * Also please be aware this class does not try to enforce any normalization on this. Callers
1109 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1110 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1111 * their own (like requiring sortedness or no overlap) they need to enforce it
1112 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1113 * or overlapping ranges are present.
1114 *
1115 * @hide
1116 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001117 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001118
1119 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001120 * Convenience method to set the UIDs this network applies to to a single UID.
1121 * @hide
1122 */
1123 public NetworkCapabilities setSingleUid(int uid) {
1124 final ArraySet<UidRange> identity = new ArraySet<>(1);
1125 identity.add(new UidRange(uid, uid));
1126 setUids(identity);
1127 return this;
1128 }
1129
1130 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001131 * Set the list of UIDs this network applies to.
1132 * This makes a copy of the set so that callers can't modify it after the call.
1133 * @hide
1134 */
1135 public NetworkCapabilities setUids(Set<UidRange> uids) {
1136 if (null == uids) {
1137 mUids = null;
1138 } else {
1139 mUids = new ArraySet<>(uids);
1140 }
1141 return this;
1142 }
1143
1144 /**
1145 * Get the list of UIDs this network applies to.
1146 * This returns a copy of the set so that callers can't modify the original object.
1147 * @hide
1148 */
1149 public Set<UidRange> getUids() {
1150 return null == mUids ? null : new ArraySet<>(mUids);
1151 }
1152
1153 /**
1154 * Test whether this network applies to this UID.
1155 * @hide
1156 */
1157 public boolean appliesToUid(int uid) {
1158 if (null == mUids) return true;
1159 for (UidRange range : mUids) {
1160 if (range.contains(uid)) {
1161 return true;
1162 }
1163 }
1164 return false;
1165 }
1166
1167 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001168 * 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 +09001169 * <p>
1170 * This test only checks whether equal range objects are in both sets. It will
1171 * return false if the ranges are not exactly the same, even if the covered UIDs
1172 * are for an equivalent result.
1173 * <p>
1174 * Note that this method is not very optimized, which is fine as long as it's not used very
1175 * often.
1176 * <p>
1177 * nc is assumed nonnull.
1178 *
1179 * @hide
1180 */
1181 @VisibleForTesting
1182 public boolean equalsUids(NetworkCapabilities nc) {
1183 Set<UidRange> comparedUids = nc.mUids;
1184 if (null == comparedUids) return null == mUids;
1185 if (null == mUids) return false;
1186 // Make a copy so it can be mutated to check that all ranges in mUids
1187 // also are in uids.
1188 final Set<UidRange> uids = new ArraySet<>(mUids);
1189 for (UidRange range : comparedUids) {
1190 if (!uids.contains(range)) {
1191 return false;
1192 }
1193 uids.remove(range);
1194 }
1195 return uids.isEmpty();
1196 }
1197
1198 /**
1199 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1200 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001201 * This method is called on the NetworkCapabilities embedded in a request with the
1202 * capabilities of an available network. It checks whether all the UIDs from this listen
1203 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1204 * in the passed nc (representing the UIDs that this network is available to).
1205 * <p>
1206 * As a special exception, the UID that created the passed network (as represented by its
1207 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1208 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1209 * can see its own network when it listens for it.
1210 * <p>
1211 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001212 * @see #appliesToUid
1213 * @hide
1214 */
1215 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001216 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001217 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001218 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001219 if (!nc.appliesToUidRange(requiredRange)) {
1220 return false;
1221 }
1222 }
1223 return true;
1224 }
1225
1226 /**
1227 * Returns whether this network applies to the passed ranges.
1228 * This assumes that to apply, the passed range has to be entirely contained
1229 * within one of the ranges this network applies to. If the ranges are not normalized,
1230 * this method may return false even though all required UIDs are covered because no
1231 * single range contained them all.
1232 * @hide
1233 */
1234 @VisibleForTesting
1235 public boolean appliesToUidRange(UidRange requiredRange) {
1236 if (null == mUids) return true;
1237 for (UidRange uidRange : mUids) {
1238 if (uidRange.containsRange(requiredRange)) {
1239 return true;
1240 }
1241 }
1242 return false;
1243 }
1244
1245 /**
1246 * Combine the UIDs this network currently applies to with the UIDs the passed
1247 * NetworkCapabilities apply to.
1248 * nc is assumed nonnull.
1249 */
1250 private void combineUids(NetworkCapabilities nc) {
1251 if (null == nc.mUids || null == mUids) {
1252 mUids = null;
1253 return;
1254 }
1255 mUids.addAll(nc.mUids);
1256 }
1257
Chalard Jeanb03a6222018-04-11 21:09:10 +09001258
1259 /**
1260 * The SSID of the network, or null if not applicable or unknown.
1261 * <p>
1262 * This is filled in by wifi code.
1263 * @hide
1264 */
1265 private String mSSID;
1266
1267 /**
1268 * Sets the SSID of this network.
1269 * @hide
1270 */
1271 public NetworkCapabilities setSSID(String ssid) {
1272 mSSID = ssid;
1273 return this;
1274 }
1275
1276 /**
1277 * Gets the SSID of this network, or null if none or unknown.
1278 * @hide
1279 */
1280 public String getSSID() {
1281 return mSSID;
1282 }
1283
1284 /**
1285 * Tests if the SSID of this network is the same as the SSID of the passed network.
1286 * @hide
1287 */
1288 public boolean equalsSSID(NetworkCapabilities nc) {
1289 return Objects.equals(mSSID, nc.mSSID);
1290 }
1291
1292 /**
1293 * Check if the SSID requirements of this object are matched by the passed object.
1294 * @hide
1295 */
1296 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1297 return mSSID == null || mSSID.equals(nc.mSSID);
1298 }
1299
1300 /**
1301 * Combine SSIDs of the capabilities.
1302 * <p>
1303 * This is only legal if either the SSID of this object is null, or both SSIDs are
1304 * equal.
1305 * @hide
1306 */
1307 private void combineSSIDs(NetworkCapabilities nc) {
1308 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1309 throw new IllegalStateException("Can't combine two SSIDs");
1310 }
1311 setSSID(nc.mSSID);
1312 }
1313
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001314 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001315 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1316 * <p>
1317 * Note that this method may break an invariant of having a particular capability in either
1318 * wanted or unwanted lists but never in both. Requests that have the same capability in
1319 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001320 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001321 */
1322 public void combineCapabilities(NetworkCapabilities nc) {
1323 combineNetCapabilities(nc);
1324 combineTransportTypes(nc);
1325 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001326 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001327 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001328 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001329 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001330 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001331 }
1332
1333 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001334 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1335 *
1336 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1337 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1338 * bandwidth, signal strength, or validation / captive portal status.
1339 *
1340 * @hide
1341 */
1342 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001343 return (nc != null
1344 && satisfiedByNetCapabilities(nc, onlyImmutable)
1345 && satisfiedByTransportTypes(nc)
1346 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1347 && satisfiedBySpecifier(nc)
1348 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001349 && (onlyImmutable || satisfiedByUids(nc))
1350 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001351 }
1352
1353 /**
1354 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1355 *
1356 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1357 *
1358 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001359 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001360 @TestApi
1361 @SystemApi
Robert Greenwalt1448f052014-04-08 13:41:39 -07001362 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001363 return satisfiedByNetworkCapabilities(nc, false);
1364 }
1365
1366 /**
1367 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1368 *
1369 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1370 *
1371 * @hide
1372 */
1373 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1374 return satisfiedByNetworkCapabilities(nc, true);
1375 }
1376
1377 /**
1378 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001379 * {@code NetworkCapabilities} and return a String describing any difference.
1380 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001381 *
1382 * @hide
1383 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001384 public String describeImmutableDifferences(NetworkCapabilities that) {
1385 if (that == null) {
1386 return "other NetworkCapabilities was null";
1387 }
1388
1389 StringJoiner joiner = new StringJoiner(", ");
1390
Hugo Benichieae7a222017-07-25 11:40:56 +09001391 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1392 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001393 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001394 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1395 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1396 if (oldImmutableCapabilities != newImmutableCapabilities) {
1397 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1398 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1399 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1400 }
1401
1402 if (!equalsSpecifier(that)) {
1403 NetworkSpecifier before = this.getNetworkSpecifier();
1404 NetworkSpecifier after = that.getNetworkSpecifier();
1405 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1406 }
1407
1408 if (!equalsTransportTypes(that)) {
1409 String before = transportNamesOf(this.getTransportTypes());
1410 String after = transportNamesOf(that.getTransportTypes());
1411 joiner.add(String.format("transports changed: %s -> %s", before, after));
1412 }
1413
1414 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001415 }
1416
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001417 /**
1418 * Checks that our requestable capabilities are the same as those of the given
1419 * {@code NetworkCapabilities}.
1420 *
1421 * @hide
1422 */
1423 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1424 if (nc == null) return false;
1425 return (equalsNetCapabilitiesRequestable(nc) &&
1426 equalsTransportTypes(nc) &&
1427 equalsSpecifier(nc));
1428 }
1429
Robert Greenwalt1448f052014-04-08 13:41:39 -07001430 @Override
1431 public boolean equals(Object obj) {
1432 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001433 NetworkCapabilities that = (NetworkCapabilities) obj;
1434 return (equalsNetCapabilities(that)
1435 && equalsTransportTypes(that)
1436 && equalsLinkBandwidths(that)
1437 && equalsSignalStrength(that)
1438 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001439 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001440 && equalsUids(that)
1441 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001442 }
1443
1444 @Override
1445 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001446 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001447 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001448 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1449 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1450 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1451 + ((int) (mTransportTypes >> 32) * 13)
1452 + (mLinkUpBandwidthKbps * 17)
1453 + (mLinkDownBandwidthKbps * 19)
1454 + Objects.hashCode(mNetworkSpecifier) * 23
1455 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001456 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001457 + Objects.hashCode(mSSID) * 37
1458 + Objects.hashCode(mTransportInfo) * 41;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001459 }
1460
Wink Saville4e2dea72014-09-20 11:04:03 -07001461 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001462 public int describeContents() {
1463 return 0;
1464 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001465 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001466 public void writeToParcel(Parcel dest, int flags) {
1467 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001468 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001469 dest.writeLong(mTransportTypes);
1470 dest.writeInt(mLinkUpBandwidthKbps);
1471 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001472 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001473 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001474 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001475 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001476 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001477 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001478
Robert Greenwalt1448f052014-04-08 13:41:39 -07001479 public static final Creator<NetworkCapabilities> CREATOR =
1480 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001481 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001482 public NetworkCapabilities createFromParcel(Parcel in) {
1483 NetworkCapabilities netCap = new NetworkCapabilities();
1484
1485 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001486 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001487 netCap.mTransportTypes = in.readLong();
1488 netCap.mLinkUpBandwidthKbps = in.readInt();
1489 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001490 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001491 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001492 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001493 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1494 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001495 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001496 return netCap;
1497 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001498 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001499 public NetworkCapabilities[] newArray(int size) {
1500 return new NetworkCapabilities[size];
1501 }
1502 };
1503
Wink Saville4e2dea72014-09-20 11:04:03 -07001504 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001505 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001506 final StringBuilder sb = new StringBuilder("[");
1507 if (0 != mTransportTypes) {
1508 sb.append(" Transports: ");
1509 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1510 NetworkCapabilities::transportNameOf, "|");
1511 }
1512 if (0 != mNetworkCapabilities) {
1513 sb.append(" Capabilities: ");
1514 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1515 NetworkCapabilities::capabilityNameOf, "&");
1516 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001517 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001518 sb.append(" Unwanted: ");
1519 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1520 NetworkCapabilities::capabilityNameOf, "&");
1521 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001522 if (mLinkUpBandwidthKbps > 0) {
1523 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1524 }
1525 if (mLinkDownBandwidthKbps > 0) {
1526 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1527 }
1528 if (mNetworkSpecifier != null) {
1529 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1530 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001531 if (mTransportInfo != null) {
1532 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1533 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001534 if (hasSignalStrength()) {
1535 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001536 }
1537
Chalard Jean07ace0f2018-02-26 19:00:45 +09001538 if (null != mUids) {
1539 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1540 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1541 } else {
1542 sb.append(" Uids: <").append(mUids).append(">");
1543 }
1544 }
1545 if (mEstablishingVpnAppUid != INVALID_UID) {
1546 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1547 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001548
Chalard Jeanb03a6222018-04-11 21:09:10 +09001549 if (null != mSSID) {
1550 sb.append(" SSID: ").append(mSSID);
1551 }
1552
Chalard Jean07ace0f2018-02-26 19:00:45 +09001553 sb.append("]");
1554 return sb.toString();
1555 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001556
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001557
Chalard Jean07ace0f2018-02-26 19:00:45 +09001558 private interface NameOf {
1559 String nameOf(int value);
1560 }
1561 /**
1562 * @hide
1563 */
1564 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1565 long bitMask, NameOf nameFetcher, String separator) {
1566 int bitPos = 0;
1567 boolean firstElementAdded = false;
1568 while (bitMask != 0) {
1569 if ((bitMask & 1) != 0) {
1570 if (firstElementAdded) {
1571 sb.append(separator);
1572 } else {
1573 firstElementAdded = true;
1574 }
1575 sb.append(nameFetcher.nameOf(bitPos));
1576 }
1577 bitMask >>= 1;
1578 ++bitPos;
1579 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001580 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001581
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001582 /** @hide */
1583 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1584 final long token = proto.start(fieldId);
1585
1586 for (int transport : getTransportTypes()) {
1587 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1588 }
1589
1590 for (int capability : getCapabilities()) {
1591 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1592 }
1593
1594 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1595 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1596
1597 if (mNetworkSpecifier != null) {
1598 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1599 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001600 if (mTransportInfo != null) {
1601 // TODO b/120653863: write transport-specific info to proto?
1602 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001603
1604 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1605 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1606
1607 proto.end(token);
1608 }
1609
Hugo Benichi5df9d722016-04-25 17:16:35 +09001610 /**
1611 * @hide
1612 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001613 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001614 StringJoiner joiner = new StringJoiner("|");
1615 if (capabilities != null) {
1616 for (int c : capabilities) {
1617 joiner.add(capabilityNameOf(c));
1618 }
1619 }
1620 return joiner.toString();
1621 }
1622
1623 /**
1624 * @hide
1625 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001626 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001627 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001628 case NET_CAPABILITY_MMS: return "MMS";
1629 case NET_CAPABILITY_SUPL: return "SUPL";
1630 case NET_CAPABILITY_DUN: return "DUN";
1631 case NET_CAPABILITY_FOTA: return "FOTA";
1632 case NET_CAPABILITY_IMS: return "IMS";
1633 case NET_CAPABILITY_CBS: return "CBS";
1634 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1635 case NET_CAPABILITY_IA: return "IA";
1636 case NET_CAPABILITY_RCS: return "RCS";
1637 case NET_CAPABILITY_XCAP: return "XCAP";
1638 case NET_CAPABILITY_EIMS: return "EIMS";
1639 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1640 case NET_CAPABILITY_INTERNET: return "INTERNET";
1641 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1642 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1643 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1644 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1645 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1646 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1647 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1648 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1649 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1650 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1651 case NET_CAPABILITY_MCX: return "MCX";
1652 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1653 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001654 }
1655 }
1656
1657 /**
1658 * @hide
1659 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001660 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -06001661 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001662 StringJoiner joiner = new StringJoiner("|");
1663 if (types != null) {
1664 for (int t : types) {
1665 joiner.add(transportNameOf(t));
1666 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001667 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001668 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001669 }
1670
1671 /**
1672 * @hide
1673 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001674 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001675 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001676 return "UNKNOWN";
1677 }
1678 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001679 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001680
Jeff Sharkeyde570312017-10-24 21:25:50 -06001681 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001682 Preconditions.checkArgument(
1683 isValidTransport(transport), "Invalid TransportType " + transport);
1684 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001685
1686 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1687 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1688 }
1689
1690 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1691 Preconditions.checkArgument(isValidCapability(capability),
1692 "NetworkCapability " + capability + "out of range");
1693 }
junyulai05986c62018-08-07 19:50:45 +08001694
1695 /**
1696 * Check if this {@code NetworkCapability} instance is metered.
1697 *
1698 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1699 * @hide
1700 */
1701 public boolean isMetered() {
1702 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1703 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001704}