blob: c4dfafa7ccb4be2e28f48001e5af69ce474c5e9a [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;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060020import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070021import android.os.Parcel;
22import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090023import android.util.ArraySet;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070024
25import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090026import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090027import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070028
Jeff Sharkeyde570312017-10-24 21:25:50 -060029import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070031import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090032import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090033import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070034
35/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070036 * Representation of the capabilities of an active network. Instances are
37 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060038 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
39 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060040 * <p>
41 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
42 * network selection. Rather than indicate a need for Wi-Fi because an
43 * application needs high bandwidth and risk obsolescence when a new, fast
44 * network appears (like LTE), the application should specify it needs high
45 * bandwidth. Similarly if an application needs an unmetered network for a bulk
46 * transfer it can specify that rather than assuming all cellular based
47 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070048 */
49public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070050 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090051 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070052
Robert Greenwalt7569f182014-06-08 16:42:59 -070053 /**
54 * @hide
55 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070056 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090057 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090058 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070059 }
60
61 public NetworkCapabilities(NetworkCapabilities nc) {
62 if (nc != null) {
63 mNetworkCapabilities = nc.mNetworkCapabilities;
64 mTransportTypes = nc.mTransportTypes;
65 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
66 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070067 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090068 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090069 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090070 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
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() {
80 mNetworkCapabilities = mTransportTypes = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070081 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090082 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090083 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090084 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090085 mEstablishingVpnAppUid = INVALID_UID;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090086 }
87
88 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070089 * Represents the network's capabilities. If any are specified they will be satisfied
90 * by any Network that matches all of them.
91 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090092 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070093
Jeff Sharkeyde570312017-10-24 21:25:50 -060094 /** @hide */
95 @Retention(RetentionPolicy.SOURCE)
96 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
97 NET_CAPABILITY_MMS,
98 NET_CAPABILITY_SUPL,
99 NET_CAPABILITY_DUN,
100 NET_CAPABILITY_FOTA,
101 NET_CAPABILITY_IMS,
102 NET_CAPABILITY_CBS,
103 NET_CAPABILITY_WIFI_P2P,
104 NET_CAPABILITY_IA,
105 NET_CAPABILITY_RCS,
106 NET_CAPABILITY_XCAP,
107 NET_CAPABILITY_EIMS,
108 NET_CAPABILITY_NOT_METERED,
109 NET_CAPABILITY_INTERNET,
110 NET_CAPABILITY_NOT_RESTRICTED,
111 NET_CAPABILITY_TRUSTED,
112 NET_CAPABILITY_NOT_VPN,
113 NET_CAPABILITY_VALIDATED,
114 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600115 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600116 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900117 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900118 NET_CAPABILITY_NOT_SUSPENDED,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600119 })
120 public @interface NetCapability { }
121
Robert Greenwalt1448f052014-04-08 13:41:39 -0700122 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700123 * Indicates this is a network that has the ability to reach the
124 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700125 */
126 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700127
128 /**
129 * Indicates this is a network that has the ability to reach the carrier's
130 * SUPL server, used to retrieve GPS information.
131 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700132 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700133
134 /**
135 * Indicates this is a network that has the ability to reach the carrier's
136 * DUN or tethering gateway.
137 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700139
140 /**
141 * Indicates this is a network that has the ability to reach the carrier's
142 * FOTA portal, used for over the air updates.
143 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700144 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700145
146 /**
147 * Indicates this is a network that has the ability to reach the carrier's
148 * IMS servers, used for network registration and signaling.
149 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700150 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700151
152 /**
153 * Indicates this is a network that has the ability to reach the carrier's
154 * CBS servers, used for carrier specific services.
155 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700156 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700157
158 /**
159 * Indicates this is a network that has the ability to reach a Wi-Fi direct
160 * peer.
161 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700162 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700163
164 /**
165 * Indicates this is a network that has the ability to reach a carrier's
166 * Initial Attach servers.
167 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700168 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700169
170 /**
171 * Indicates this is a network that has the ability to reach a carrier's
172 * RCS servers, used for Rich Communication Services.
173 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700174 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700175
176 /**
177 * Indicates this is a network that has the ability to reach a carrier's
178 * XCAP servers, used for configuration and control.
179 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700184 * Emergency IMS servers or other services, used for network signaling
185 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700187 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700188
189 /**
190 * Indicates that this network is unmetered.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates that this network should be able to reach the internet.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Indicates that this network is available for general use. If this is not set
201 * applications should not attempt to communicate on this network. Note that this
202 * is simply informative and not enforcement - enforcement is handled via other means.
203 * Set by default.
204 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700205 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
206
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700207 /**
208 * Indicates that the user has indicated implicit trust of this network. This
209 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
210 * BT device or a wifi the user asked to connect to. Untrusted networks
211 * are probably limited to unknown wifi AP. Set by default.
212 */
213 public static final int NET_CAPABILITY_TRUSTED = 14;
214
Paul Jensen76b610a2015-03-18 09:33:07 -0400215 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400216 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400217 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400218 */
219 public static final int NET_CAPABILITY_NOT_VPN = 15;
220
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900221 /**
222 * Indicates that connectivity on this network was successfully validated. For example, for a
223 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
224 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900225 */
226 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700227
Paul Jensen3d194ea2015-06-16 14:27:36 -0400228 /**
229 * Indicates that this network was found to have a captive portal in place last time it was
230 * probed.
231 */
232 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
233
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900234 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600235 * Indicates that this network is not roaming.
236 */
237 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
238
239 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900240 * Indicates that this network is available for use by apps, and not a network that is being
241 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900242 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600243 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900244
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900245 /**
246 * Indicates that this network is not congested.
247 * <p>
248 * When a network is congested, the device should defer network traffic that
249 * can be done at a later time without breaking developer contracts.
250 * @hide
251 */
252 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
253
Chalard Jean804b8fb2018-01-30 22:41:41 +0900254 /**
255 * Indicates that this network is not currently suspended.
256 * <p>
257 * When a network is suspended, the network's IP addresses and any connections
258 * established on the network remain valid, but the network is temporarily unable
259 * to transfer data. This can happen, for example, if a cellular network experiences
260 * a temporary loss of signal, such as when driving through a tunnel, etc.
261 * A network with this capability is not suspended, so is expected to be able to
262 * transfer data.
263 */
264 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
265
Robert Greenwalt1448f052014-04-08 13:41:39 -0700266 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Chalard Jean804b8fb2018-01-30 22:41:41 +0900267 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_SUSPENDED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700268
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700269 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900270 * Network capabilities that are expected to be mutable, i.e., can change while a particular
271 * network is connected.
272 */
273 private static final long MUTABLE_CAPABILITIES =
274 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
275 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900276 (1 << NET_CAPABILITY_TRUSTED)
277 | (1 << NET_CAPABILITY_VALIDATED)
278 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
279 | (1 << NET_CAPABILITY_NOT_ROAMING)
280 | (1 << NET_CAPABILITY_FOREGROUND)
281 | (1 << NET_CAPABILITY_NOT_CONGESTED)
282 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900283
284 /**
285 * Network capabilities that are not allowed in NetworkRequests. This exists because the
286 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
287 * capability's presence cannot be known in advance. If such a capability is requested, then we
288 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
289 * get immediately torn down because they do not have the requested capability.
290 */
291 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900292 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900293
294 /**
295 * Capabilities that are set by default when the object is constructed.
296 */
297 private static final long DEFAULT_CAPABILITIES =
298 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
299 (1 << NET_CAPABILITY_TRUSTED) |
300 (1 << NET_CAPABILITY_NOT_VPN);
301
302 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400303 * Capabilities that suggest that a network is restricted.
304 * {@see #maybeMarkCapabilitiesRestricted}.
305 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700306 @VisibleForTesting
307 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400308 (1 << NET_CAPABILITY_CBS) |
309 (1 << NET_CAPABILITY_DUN) |
310 (1 << NET_CAPABILITY_EIMS) |
311 (1 << NET_CAPABILITY_FOTA) |
312 (1 << NET_CAPABILITY_IA) |
313 (1 << NET_CAPABILITY_IMS) |
314 (1 << NET_CAPABILITY_RCS) |
315 (1 << NET_CAPABILITY_XCAP);
316
317 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700318 * Capabilities that suggest that a network is unrestricted.
319 * {@see #maybeMarkCapabilitiesRestricted}.
320 */
321 @VisibleForTesting
322 /* package */ static final long UNRESTRICTED_CAPABILITIES =
323 (1 << NET_CAPABILITY_INTERNET) |
324 (1 << NET_CAPABILITY_MMS) |
325 (1 << NET_CAPABILITY_SUPL) |
326 (1 << NET_CAPABILITY_WIFI_P2P);
327
328 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700329 * Adds the given capability to this {@code NetworkCapability} instance.
330 * Multiple capabilities may be applied sequentially. Note that when searching
331 * for a network to satisfy a request, all capabilities requested must be satisfied.
332 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600333 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900334 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700335 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700336 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600337 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700338 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339 throw new IllegalArgumentException("NetworkCapability out of range");
340 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700341 mNetworkCapabilities |= 1 << capability;
342 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700343 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700344
345 /**
346 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
347 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600348 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900349 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700350 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700351 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600352 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700353 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700354 throw new IllegalArgumentException("NetworkCapability out of range");
355 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700356 mNetworkCapabilities &= ~(1 << capability);
357 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700358 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700359
360 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600361 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
362 * instance.
363 *
364 * @hide
365 */
366 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
367 if (value) {
368 addCapability(capability);
369 } else {
370 removeCapability(capability);
371 }
372 return this;
373 }
374
375 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700376 * Gets all the capabilities set on this {@code NetworkCapability} instance.
377 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600378 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700379 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700380 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600381 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900382 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700383 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384
385 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600386 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700387 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600388 *
389 * @hide
390 */
391 public void setCapabilities(@NetCapability int[] capabilities) {
392 mNetworkCapabilities = BitUtils.packBits(capabilities);
393 }
394
395 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700396 * Tests for the presence of a capabilitity on this instance.
397 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600398 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700399 * @return {@code true} if set on this instance.
400 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600401 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700402 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700403 return false;
404 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700405 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700406 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700407
Robert Greenwalt1448f052014-04-08 13:41:39 -0700408 private void combineNetCapabilities(NetworkCapabilities nc) {
409 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
410 }
411
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900412 /**
413 * Convenience function that returns a human-readable description of the first mutable
414 * capability we find. Used to present an error message to apps that request mutable
415 * capabilities.
416 *
417 * @hide
418 */
419 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900420 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
421 if (nonRequestable != 0) {
422 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900423 }
424 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900425 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900426 return null;
427 }
428
429 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
430 long networkCapabilities = this.mNetworkCapabilities;
431 if (onlyImmutable) {
432 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
433 }
434 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700435 }
436
Robert Greenwalt06314e42014-10-29 14:04:06 -0700437 /** @hide */
438 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700439 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
440 }
441
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900442 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
443 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
444 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
445 }
446
Robert Greenwalt1448f052014-04-08 13:41:39 -0700447 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400448 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
449 * typically provided by restricted networks.
450 *
451 * TODO: consider:
452 * - Renaming it to guessRestrictedCapability and make it set the
453 * restricted capability bit in addition to clearing it.
454 * @hide
455 */
456 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700457 // Verify there aren't any unrestricted capabilities. If there are we say
458 // the whole thing is unrestricted.
459 final boolean hasUnrestrictedCapabilities =
460 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
461
462 // Must have at least some restricted capabilities.
463 final boolean hasRestrictedCapabilities =
464 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
465
466 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400467 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400468 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400469 }
470
471 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700472 * Representing the transport type. Apps should generally not care about transport. A
473 * request for a fast internet connection could be satisfied by a number of different
474 * transports. If any are specified here it will be satisfied a Network that matches
475 * any of them. If a caller doesn't care about the transport it should not specify any.
476 */
477 private long mTransportTypes;
478
Jeff Sharkeyde570312017-10-24 21:25:50 -0600479 /** @hide */
480 @Retention(RetentionPolicy.SOURCE)
481 @IntDef(prefix = { "TRANSPORT_" }, value = {
482 TRANSPORT_CELLULAR,
483 TRANSPORT_WIFI,
484 TRANSPORT_BLUETOOTH,
485 TRANSPORT_ETHERNET,
486 TRANSPORT_VPN,
487 TRANSPORT_WIFI_AWARE,
488 TRANSPORT_LOWPAN,
489 })
490 public @interface Transport { }
491
Robert Greenwalt1448f052014-04-08 13:41:39 -0700492 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700493 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700494 */
495 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700496
497 /**
498 * Indicates this network uses a Wi-Fi transport.
499 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700500 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700501
502 /**
503 * Indicates this network uses a Bluetooth transport.
504 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700505 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700506
507 /**
508 * Indicates this network uses an Ethernet transport.
509 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700510 public static final int TRANSPORT_ETHERNET = 3;
511
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400512 /**
513 * Indicates this network uses a VPN transport.
514 */
515 public static final int TRANSPORT_VPN = 4;
516
Etan Cohen305ea282016-06-20 09:27:12 -0700517 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700518 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700519 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700520 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700521
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700522 /**
523 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700524 */
525 public static final int TRANSPORT_LOWPAN = 6;
526
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900527 /** @hide */
528 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
529 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700530 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700531
Hugo Benichi16f0a942017-06-20 14:07:59 +0900532 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600533 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900534 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
535 }
536
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900537 private static final String[] TRANSPORT_NAMES = {
538 "CELLULAR",
539 "WIFI",
540 "BLUETOOTH",
541 "ETHERNET",
542 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700543 "WIFI_AWARE",
544 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900545 };
546
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700547 /**
548 * Adds the given transport type to this {@code NetworkCapability} instance.
549 * Multiple transports may be applied sequentially. Note that when searching
550 * for a network to satisfy a request, any listed in the request will satisfy the request.
551 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
552 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
553 * to be selected. This is logically different than
554 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
555 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600556 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900557 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700558 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700559 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600560 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900561 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700562 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700563 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700564 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700565 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700566
567 /**
568 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
569 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600570 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900571 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700572 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700573 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600574 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900575 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700576 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700577 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700578 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700579 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700580
581 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600582 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
583 * instance.
584 *
585 * @hide
586 */
587 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
588 if (value) {
589 addTransportType(transportType);
590 } else {
591 removeTransportType(transportType);
592 }
593 return this;
594 }
595
596 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700597 * Gets all the transports set on this {@code NetworkCapability} instance.
598 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600599 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700600 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700601 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600602 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900603 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700604 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700605
606 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600607 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700608 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600609 *
610 * @hide
611 */
612 public void setTransportTypes(@Transport int[] transportTypes) {
613 mTransportTypes = BitUtils.packBits(transportTypes);
614 }
615
616 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700617 * Tests for the presence of a transport on this instance.
618 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600619 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700620 * @return {@code true} if set on this instance.
621 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600622 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900623 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700624 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700625
626 private void combineTransportTypes(NetworkCapabilities nc) {
627 this.mTransportTypes |= nc.mTransportTypes;
628 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900629
Robert Greenwalt1448f052014-04-08 13:41:39 -0700630 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
631 return ((this.mTransportTypes == 0) ||
632 ((this.mTransportTypes & nc.mTransportTypes) != 0));
633 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900634
Robert Greenwalt06314e42014-10-29 14:04:06 -0700635 /** @hide */
636 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700637 return (nc.mTransportTypes == this.mTransportTypes);
638 }
639
640 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900641 * UID of the app that manages this network, or INVALID_UID if none/unknown.
642 *
643 * This field keeps track of the UID of the app that created this network and is in charge
644 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
645 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
646 * to create networks.
647 *
648 * Because this field is only used in the services side (and to avoid apps being able to
649 * set this to whatever they want), this field is not parcelled and will not be conserved
650 * across the IPC boundary.
651 * @hide
652 */
653 private int mEstablishingVpnAppUid = INVALID_UID;
654
655 /**
656 * Set the UID of the managing app.
657 * @hide
658 */
659 public void setEstablishingVpnAppUid(final int uid) {
660 mEstablishingVpnAppUid = uid;
661 }
662
663 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600664 * Value indicating that link bandwidth is unspecified.
665 * @hide
666 */
667 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
668
669 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700670 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
671 * for the first hop on the given transport. It is not measured, but may take into account
672 * link parameters (Radio technology, allocated channels, etc).
673 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600674 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
675 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700676
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700677 /**
678 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
679 * the estimated first hop transport bandwidth.
680 * <p>
681 * Note that when used to request a network, this specifies the minimum acceptable.
682 * When received as the state of an existing network this specifies the typical
683 * first hop bandwidth expected. This is never measured, but rather is inferred
684 * from technology type and other link parameters. It could be used to differentiate
685 * between very slow 1xRTT cellular links and other faster networks or even between
686 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
687 * fast backhauls and slow backhauls.
688 *
689 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700690 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700691 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600692 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700693 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600694 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700695 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700696
697 /**
698 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
699 * the estimated first hop transport bandwidth.
700 *
701 * @return The estimated first hop upstream (device to network) bandwidth.
702 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700703 public int getLinkUpstreamBandwidthKbps() {
704 return mLinkUpBandwidthKbps;
705 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700706
707 /**
708 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
709 * the estimated first hop transport bandwidth.
710 * <p>
711 * Note that when used to request a network, this specifies the minimum acceptable.
712 * When received as the state of an existing network this specifies the typical
713 * first hop bandwidth expected. This is never measured, but rather is inferred
714 * from technology type and other link parameters. It could be used to differentiate
715 * between very slow 1xRTT cellular links and other faster networks or even between
716 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
717 * fast backhauls and slow backhauls.
718 *
719 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700720 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700721 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600722 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700723 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600724 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700725 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700726
727 /**
728 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
729 * the estimated first hop transport bandwidth.
730 *
731 * @return The estimated first hop downstream (network to device) bandwidth.
732 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700733 public int getLinkDownstreamBandwidthKbps() {
734 return mLinkDownBandwidthKbps;
735 }
736
737 private void combineLinkBandwidths(NetworkCapabilities nc) {
738 this.mLinkUpBandwidthKbps =
739 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
740 this.mLinkDownBandwidthKbps =
741 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
742 }
743 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
744 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
745 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
746 }
747 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
748 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
749 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
750 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600751 /** @hide */
752 public static int minBandwidth(int a, int b) {
753 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
754 return b;
755 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
756 return a;
757 } else {
758 return Math.min(a, b);
759 }
760 }
761 /** @hide */
762 public static int maxBandwidth(int a, int b) {
763 return Math.max(a, b);
764 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700765
Etan Cohena7434272017-04-03 12:17:51 -0700766 private NetworkSpecifier mNetworkSpecifier = null;
767
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700768 /**
769 * Sets the optional bearer specific network specifier.
770 * This has no meaning if a single transport is also not specified, so calling
771 * this without a single transport set will generate an exception, as will
772 * subsequently adding or removing transports after this is set.
773 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700774 *
Etan Cohena7434272017-04-03 12:17:51 -0700775 * @param networkSpecifier A concrete, parcelable framework class that extends
776 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900777 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700778 * @hide
779 */
Etan Cohena7434272017-04-03 12:17:51 -0700780 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
781 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700782 throw new IllegalStateException("Must have a single transport specified to use " +
783 "setNetworkSpecifier");
784 }
Etan Cohena7434272017-04-03 12:17:51 -0700785
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700786 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700787
Pierre Imaic8419a82016-03-22 17:54:54 +0900788 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700789 }
790
791 /**
792 * Gets the optional bearer specific network specifier.
793 *
Etan Cohena7434272017-04-03 12:17:51 -0700794 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
795 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700796 * @hide
797 */
Etan Cohena7434272017-04-03 12:17:51 -0700798 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700799 return mNetworkSpecifier;
800 }
801
802 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700803 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700804 throw new IllegalStateException("Can't combine two networkSpecifiers");
805 }
Etan Cohena7434272017-04-03 12:17:51 -0700806 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700807 }
Etan Cohena7434272017-04-03 12:17:51 -0700808
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700809 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700810 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
811 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700812 }
Etan Cohena7434272017-04-03 12:17:51 -0700813
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700814 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700815 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700816 }
817
Robert Greenwalt1448f052014-04-08 13:41:39 -0700818 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900819 * Magic value that indicates no signal strength provided. A request specifying this value is
820 * always satisfied.
821 *
822 * @hide
823 */
824 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
825
826 /**
827 * Signal strength. This is a signed integer, and higher values indicate better signal.
828 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
829 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700830 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900831
832 /**
833 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
834 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
835 * reported by WifiManager.
836 * <p>
837 * Note that when used to register a network callback, this specifies the minimum acceptable
838 * signal strength. When received as the state of an existing network it specifies the current
839 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
840 * effect when requesting a callback.
841 *
842 * @param signalStrength the bearer-specific signal strength.
843 * @hide
844 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600845 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900846 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600847 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900848 }
849
850 /**
851 * Returns {@code true} if this object specifies a signal strength.
852 *
853 * @hide
854 */
855 public boolean hasSignalStrength() {
856 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
857 }
858
859 /**
860 * Retrieves the signal strength.
861 *
862 * @return The bearer-specific signal strength.
863 * @hide
864 */
865 public int getSignalStrength() {
866 return mSignalStrength;
867 }
868
869 private void combineSignalStrength(NetworkCapabilities nc) {
870 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
871 }
872
873 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
874 return this.mSignalStrength <= nc.mSignalStrength;
875 }
876
877 private boolean equalsSignalStrength(NetworkCapabilities nc) {
878 return this.mSignalStrength == nc.mSignalStrength;
879 }
880
881 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900882 * List of UIDs this network applies to. No restriction if null.
883 * <p>
884 * This is typically (and at this time, only) used by VPN. This network is only available to
885 * the UIDs in this list, and it is their default network. Apps in this list that wish to
886 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
887 * member is null, then the network is not restricted by app UID. If it's an empty list, then
888 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900889 * As a special exception, the app managing this network (as identified by its UID stored in
890 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
891 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
892 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900893 * <p>
894 * Please note that in principle a single app can be associated with multiple UIDs because
895 * each app will have a different UID when it's run as a different (macro-)user. A single
896 * macro user can only have a single active VPN app at any given time however.
897 * <p>
898 * Also please be aware this class does not try to enforce any normalization on this. Callers
899 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
900 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
901 * their own (like requiring sortedness or no overlap) they need to enforce it
902 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
903 * or overlapping ranges are present.
904 *
905 * @hide
906 */
Chalard Jeancf8c3fe2018-01-25 09:41:51 +0900907 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900908
909 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900910 * Convenience method to set the UIDs this network applies to to a single UID.
911 * @hide
912 */
913 public NetworkCapabilities setSingleUid(int uid) {
914 final ArraySet<UidRange> identity = new ArraySet<>(1);
915 identity.add(new UidRange(uid, uid));
916 setUids(identity);
917 return this;
918 }
919
920 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900921 * Set the list of UIDs this network applies to.
922 * This makes a copy of the set so that callers can't modify it after the call.
923 * @hide
924 */
925 public NetworkCapabilities setUids(Set<UidRange> uids) {
926 if (null == uids) {
927 mUids = null;
928 } else {
929 mUids = new ArraySet<>(uids);
930 }
931 return this;
932 }
933
934 /**
935 * Get the list of UIDs this network applies to.
936 * This returns a copy of the set so that callers can't modify the original object.
937 * @hide
938 */
939 public Set<UidRange> getUids() {
940 return null == mUids ? null : new ArraySet<>(mUids);
941 }
942
943 /**
944 * Test whether this network applies to this UID.
945 * @hide
946 */
947 public boolean appliesToUid(int uid) {
948 if (null == mUids) return true;
949 for (UidRange range : mUids) {
950 if (range.contains(uid)) {
951 return true;
952 }
953 }
954 return false;
955 }
956
957 /**
958 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
959 * <p>
960 * This test only checks whether equal range objects are in both sets. It will
961 * return false if the ranges are not exactly the same, even if the covered UIDs
962 * are for an equivalent result.
963 * <p>
964 * Note that this method is not very optimized, which is fine as long as it's not used very
965 * often.
966 * <p>
967 * nc is assumed nonnull.
968 *
969 * @hide
970 */
971 @VisibleForTesting
972 public boolean equalsUids(NetworkCapabilities nc) {
973 Set<UidRange> comparedUids = nc.mUids;
974 if (null == comparedUids) return null == mUids;
975 if (null == mUids) return false;
976 // Make a copy so it can be mutated to check that all ranges in mUids
977 // also are in uids.
978 final Set<UidRange> uids = new ArraySet<>(mUids);
979 for (UidRange range : comparedUids) {
980 if (!uids.contains(range)) {
981 return false;
982 }
983 uids.remove(range);
984 }
985 return uids.isEmpty();
986 }
987
988 /**
989 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
990 *
Chalard Jeanf474fc32018-01-17 15:10:05 +0900991 * This method is called on the NetworkCapabilities embedded in a request with the
992 * capabilities of an available network. It checks whether all the UIDs from this listen
993 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
994 * in the passed nc (representing the UIDs that this network is available to).
995 * <p>
996 * As a special exception, the UID that created the passed network (as represented by its
997 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
998 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
999 * can see its own network when it listens for it.
1000 * <p>
1001 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001002 * @see #appliesToUid
1003 * @hide
1004 */
1005 public boolean satisfiedByUids(NetworkCapabilities nc) {
1006 if (null == nc.mUids) return true; // The network satisfies everything.
1007 if (null == mUids) return false; // Not everything allowed but requires everything
1008 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001009 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001010 if (!nc.appliesToUidRange(requiredRange)) {
1011 return false;
1012 }
1013 }
1014 return true;
1015 }
1016
1017 /**
1018 * Returns whether this network applies to the passed ranges.
1019 * This assumes that to apply, the passed range has to be entirely contained
1020 * within one of the ranges this network applies to. If the ranges are not normalized,
1021 * this method may return false even though all required UIDs are covered because no
1022 * single range contained them all.
1023 * @hide
1024 */
1025 @VisibleForTesting
1026 public boolean appliesToUidRange(UidRange requiredRange) {
1027 if (null == mUids) return true;
1028 for (UidRange uidRange : mUids) {
1029 if (uidRange.containsRange(requiredRange)) {
1030 return true;
1031 }
1032 }
1033 return false;
1034 }
1035
1036 /**
1037 * Combine the UIDs this network currently applies to with the UIDs the passed
1038 * NetworkCapabilities apply to.
1039 * nc is assumed nonnull.
1040 */
1041 private void combineUids(NetworkCapabilities nc) {
1042 if (null == nc.mUids || null == mUids) {
1043 mUids = null;
1044 return;
1045 }
1046 mUids.addAll(nc.mUids);
1047 }
1048
1049 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001050 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001051 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001052 */
1053 public void combineCapabilities(NetworkCapabilities nc) {
1054 combineNetCapabilities(nc);
1055 combineTransportTypes(nc);
1056 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001057 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001058 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001059 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001060 }
1061
1062 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001063 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1064 *
1065 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1066 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1067 * bandwidth, signal strength, or validation / captive portal status.
1068 *
1069 * @hide
1070 */
1071 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001072 return (nc != null
1073 && satisfiedByNetCapabilities(nc, onlyImmutable)
1074 && satisfiedByTransportTypes(nc)
1075 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1076 && satisfiedBySpecifier(nc)
1077 && (onlyImmutable || satisfiedBySignalStrength(nc))
1078 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001079 }
1080
1081 /**
1082 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1083 *
1084 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1085 *
1086 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001087 */
1088 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001089 return satisfiedByNetworkCapabilities(nc, false);
1090 }
1091
1092 /**
1093 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1094 *
1095 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1096 *
1097 * @hide
1098 */
1099 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1100 return satisfiedByNetworkCapabilities(nc, true);
1101 }
1102
1103 /**
1104 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001105 * {@code NetworkCapabilities} and return a String describing any difference.
1106 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001107 *
1108 * @hide
1109 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001110 public String describeImmutableDifferences(NetworkCapabilities that) {
1111 if (that == null) {
1112 return "other NetworkCapabilities was null";
1113 }
1114
1115 StringJoiner joiner = new StringJoiner(", ");
1116
Hugo Benichieae7a222017-07-25 11:40:56 +09001117 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1118 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001119 // Ignore DUN being added or removed. http://b/65257223.
1120 final long mask = ~MUTABLE_CAPABILITIES
1121 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001122 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1123 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1124 if (oldImmutableCapabilities != newImmutableCapabilities) {
1125 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1126 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1127 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1128 }
1129
1130 if (!equalsSpecifier(that)) {
1131 NetworkSpecifier before = this.getNetworkSpecifier();
1132 NetworkSpecifier after = that.getNetworkSpecifier();
1133 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1134 }
1135
1136 if (!equalsTransportTypes(that)) {
1137 String before = transportNamesOf(this.getTransportTypes());
1138 String after = transportNamesOf(that.getTransportTypes());
1139 joiner.add(String.format("transports changed: %s -> %s", before, after));
1140 }
1141
1142 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001143 }
1144
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001145 /**
1146 * Checks that our requestable capabilities are the same as those of the given
1147 * {@code NetworkCapabilities}.
1148 *
1149 * @hide
1150 */
1151 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1152 if (nc == null) return false;
1153 return (equalsNetCapabilitiesRequestable(nc) &&
1154 equalsTransportTypes(nc) &&
1155 equalsSpecifier(nc));
1156 }
1157
Robert Greenwalt1448f052014-04-08 13:41:39 -07001158 @Override
1159 public boolean equals(Object obj) {
1160 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001161 NetworkCapabilities that = (NetworkCapabilities) obj;
1162 return (equalsNetCapabilities(that)
1163 && equalsTransportTypes(that)
1164 && equalsLinkBandwidths(that)
1165 && equalsSignalStrength(that)
1166 && equalsSpecifier(that)
1167 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001168 }
1169
1170 @Override
1171 public int hashCode() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001172 return ((int) (mNetworkCapabilities & 0xFFFFFFFF)
1173 + ((int) (mNetworkCapabilities >> 32) * 3)
1174 + ((int) (mTransportTypes & 0xFFFFFFFF) * 5)
1175 + ((int) (mTransportTypes >> 32) * 7)
1176 + (mLinkUpBandwidthKbps * 11)
1177 + (mLinkDownBandwidthKbps * 13)
1178 + Objects.hashCode(mNetworkSpecifier) * 17
1179 + (mSignalStrength * 19)
1180 + Objects.hashCode(mUids) * 23);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001181 }
1182
Wink Saville4e2dea72014-09-20 11:04:03 -07001183 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001184 public int describeContents() {
1185 return 0;
1186 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001187 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001188 public void writeToParcel(Parcel dest, int flags) {
1189 dest.writeLong(mNetworkCapabilities);
1190 dest.writeLong(mTransportTypes);
1191 dest.writeInt(mLinkUpBandwidthKbps);
1192 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001193 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001194 dest.writeInt(mSignalStrength);
Chalard Jeancf8c3fe2018-01-25 09:41:51 +09001195 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001196 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001197
Robert Greenwalt1448f052014-04-08 13:41:39 -07001198 public static final Creator<NetworkCapabilities> CREATOR =
1199 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001200 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001201 public NetworkCapabilities createFromParcel(Parcel in) {
1202 NetworkCapabilities netCap = new NetworkCapabilities();
1203
1204 netCap.mNetworkCapabilities = in.readLong();
1205 netCap.mTransportTypes = in.readLong();
1206 netCap.mLinkUpBandwidthKbps = in.readInt();
1207 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001208 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001209 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001210 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1211 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001212 return netCap;
1213 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001214 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001215 public NetworkCapabilities[] newArray(int size) {
1216 return new NetworkCapabilities[size];
1217 }
1218 };
1219
Wink Saville4e2dea72014-09-20 11:04:03 -07001220 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001221 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001222 final StringBuilder sb = new StringBuilder("[");
1223 if (0 != mTransportTypes) {
1224 sb.append(" Transports: ");
1225 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1226 NetworkCapabilities::transportNameOf, "|");
1227 }
1228 if (0 != mNetworkCapabilities) {
1229 sb.append(" Capabilities: ");
1230 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1231 NetworkCapabilities::capabilityNameOf, "&");
1232 }
1233 if (mLinkUpBandwidthKbps > 0) {
1234 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1235 }
1236 if (mLinkDownBandwidthKbps > 0) {
1237 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1238 }
1239 if (mNetworkSpecifier != null) {
1240 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1241 }
1242 if (hasSignalStrength()) {
1243 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001244 }
1245
Chalard Jean07ace0f2018-02-26 19:00:45 +09001246 if (null != mUids) {
1247 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1248 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1249 } else {
1250 sb.append(" Uids: <").append(mUids).append(">");
1251 }
1252 }
1253 if (mEstablishingVpnAppUid != INVALID_UID) {
1254 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1255 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001256
Chalard Jean07ace0f2018-02-26 19:00:45 +09001257 sb.append("]");
1258 return sb.toString();
1259 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001260
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001261
Chalard Jean07ace0f2018-02-26 19:00:45 +09001262 private interface NameOf {
1263 String nameOf(int value);
1264 }
1265 /**
1266 * @hide
1267 */
1268 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1269 long bitMask, NameOf nameFetcher, String separator) {
1270 int bitPos = 0;
1271 boolean firstElementAdded = false;
1272 while (bitMask != 0) {
1273 if ((bitMask & 1) != 0) {
1274 if (firstElementAdded) {
1275 sb.append(separator);
1276 } else {
1277 firstElementAdded = true;
1278 }
1279 sb.append(nameFetcher.nameOf(bitPos));
1280 }
1281 bitMask >>= 1;
1282 ++bitPos;
1283 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001284 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001285
1286 /**
1287 * @hide
1288 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001289 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001290 StringJoiner joiner = new StringJoiner("|");
1291 if (capabilities != null) {
1292 for (int c : capabilities) {
1293 joiner.add(capabilityNameOf(c));
1294 }
1295 }
1296 return joiner.toString();
1297 }
1298
1299 /**
1300 * @hide
1301 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001302 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001303 switch (capability) {
1304 case NET_CAPABILITY_MMS: return "MMS";
1305 case NET_CAPABILITY_SUPL: return "SUPL";
1306 case NET_CAPABILITY_DUN: return "DUN";
1307 case NET_CAPABILITY_FOTA: return "FOTA";
1308 case NET_CAPABILITY_IMS: return "IMS";
1309 case NET_CAPABILITY_CBS: return "CBS";
1310 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1311 case NET_CAPABILITY_IA: return "IA";
1312 case NET_CAPABILITY_RCS: return "RCS";
1313 case NET_CAPABILITY_XCAP: return "XCAP";
1314 case NET_CAPABILITY_EIMS: return "EIMS";
1315 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1316 case NET_CAPABILITY_INTERNET: return "INTERNET";
1317 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1318 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1319 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1320 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1321 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001322 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001323 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001324 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001325 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Hugo Benichieae7a222017-07-25 11:40:56 +09001326 default: return Integer.toString(capability);
1327 }
1328 }
1329
1330 /**
1331 * @hide
1332 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001333 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001334 StringJoiner joiner = new StringJoiner("|");
1335 if (types != null) {
1336 for (int t : types) {
1337 joiner.add(transportNameOf(t));
1338 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001339 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001340 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001341 }
1342
1343 /**
1344 * @hide
1345 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001346 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001347 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001348 return "UNKNOWN";
1349 }
1350 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001351 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001352
Jeff Sharkeyde570312017-10-24 21:25:50 -06001353 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001354 Preconditions.checkArgument(
1355 isValidTransport(transport), "Invalid TransportType " + transport);
1356 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001357}