blob: d8e17412ba7ec96aa0453002bca5efd93ef6af5d [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,
Pavel Maltsev43403202018-01-30 17:19:44 -0800119 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600120 })
121 public @interface NetCapability { }
122
Robert Greenwalt1448f052014-04-08 13:41:39 -0700123 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700124 * Indicates this is a network that has the ability to reach the
125 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700126 */
127 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700128
129 /**
130 * Indicates this is a network that has the ability to reach the carrier's
131 * SUPL server, used to retrieve GPS information.
132 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700134
135 /**
136 * Indicates this is a network that has the ability to reach the carrier's
137 * DUN or tethering gateway.
138 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700139 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700140
141 /**
142 * Indicates this is a network that has the ability to reach the carrier's
143 * FOTA portal, used for over the air updates.
144 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700145 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700146
147 /**
148 * Indicates this is a network that has the ability to reach the carrier's
149 * IMS servers, used for network registration and signaling.
150 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700151 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700152
153 /**
154 * Indicates this is a network that has the ability to reach the carrier's
155 * CBS servers, used for carrier specific services.
156 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700157 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700158
159 /**
160 * Indicates this is a network that has the ability to reach a Wi-Fi direct
161 * peer.
162 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700163 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700164
165 /**
166 * Indicates this is a network that has the ability to reach a carrier's
167 * Initial Attach servers.
168 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700169 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700170
171 /**
172 * Indicates this is a network that has the ability to reach a carrier's
173 * RCS servers, used for Rich Communication Services.
174 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700175 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700176
177 /**
178 * Indicates this is a network that has the ability to reach a carrier's
179 * XCAP servers, used for configuration and control.
180 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700181 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700182
183 /**
184 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700185 * Emergency IMS servers or other services, used for network signaling
186 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700188 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700189
190 /**
191 * Indicates that this network is unmetered.
192 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700193 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700194
195 /**
196 * Indicates that this network should be able to reach the internet.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates that this network is available for general use. If this is not set
202 * applications should not attempt to communicate on this network. Note that this
203 * is simply informative and not enforcement - enforcement is handled via other means.
204 * Set by default.
205 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700206 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
207
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700208 /**
209 * Indicates that the user has indicated implicit trust of this network. This
210 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
211 * BT device or a wifi the user asked to connect to. Untrusted networks
212 * are probably limited to unknown wifi AP. Set by default.
213 */
214 public static final int NET_CAPABILITY_TRUSTED = 14;
215
Paul Jensen76b610a2015-03-18 09:33:07 -0400216 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400217 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400218 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400219 */
220 public static final int NET_CAPABILITY_NOT_VPN = 15;
221
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900222 /**
223 * Indicates that connectivity on this network was successfully validated. For example, for a
224 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
225 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900226 */
227 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700228
Paul Jensen3d194ea2015-06-16 14:27:36 -0400229 /**
230 * Indicates that this network was found to have a captive portal in place last time it was
231 * probed.
232 */
233 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
234
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900235 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600236 * Indicates that this network is not roaming.
237 */
238 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
239
240 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900241 * Indicates that this network is available for use by apps, and not a network that is being
242 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900243 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600244 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900245
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900246 /**
247 * Indicates that this network is not congested.
248 * <p>
249 * When a network is congested, the device should defer network traffic that
250 * can be done at a later time without breaking developer contracts.
251 * @hide
252 */
253 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
254
Chalard Jean804b8fb2018-01-30 22:41:41 +0900255 /**
256 * Indicates that this network is not currently suspended.
257 * <p>
258 * When a network is suspended, the network's IP addresses and any connections
259 * established on the network remain valid, but the network is temporarily unable
260 * to transfer data. This can happen, for example, if a cellular network experiences
261 * a temporary loss of signal, such as when driving through a tunnel, etc.
262 * A network with this capability is not suspended, so is expected to be able to
263 * transfer data.
264 */
265 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
266
Pavel Maltsev43403202018-01-30 17:19:44 -0800267 /**
268 * Indicates that traffic that goes through this network is paid by oem. For example,
269 * this network can be used by system apps to upload telemetry data.
270 * @hide
271 */
272 public static final int NET_CAPABILITY_OEM_PAID = 22;
273
Robert Greenwalt1448f052014-04-08 13:41:39 -0700274 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800275 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700276
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700277 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900278 * Network capabilities that are expected to be mutable, i.e., can change while a particular
279 * network is connected.
280 */
281 private static final long MUTABLE_CAPABILITIES =
282 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
283 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900284 (1 << NET_CAPABILITY_TRUSTED)
285 | (1 << NET_CAPABILITY_VALIDATED)
286 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
287 | (1 << NET_CAPABILITY_NOT_ROAMING)
288 | (1 << NET_CAPABILITY_FOREGROUND)
289 | (1 << NET_CAPABILITY_NOT_CONGESTED)
290 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900291
292 /**
293 * Network capabilities that are not allowed in NetworkRequests. This exists because the
294 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
295 * capability's presence cannot be known in advance. If such a capability is requested, then we
296 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
297 * get immediately torn down because they do not have the requested capability.
298 */
299 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900300 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900301
302 /**
303 * Capabilities that are set by default when the object is constructed.
304 */
305 private static final long DEFAULT_CAPABILITIES =
306 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
307 (1 << NET_CAPABILITY_TRUSTED) |
308 (1 << NET_CAPABILITY_NOT_VPN);
309
310 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400311 * Capabilities that suggest that a network is restricted.
312 * {@see #maybeMarkCapabilitiesRestricted}.
313 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700314 @VisibleForTesting
315 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400316 (1 << NET_CAPABILITY_CBS) |
317 (1 << NET_CAPABILITY_DUN) |
318 (1 << NET_CAPABILITY_EIMS) |
319 (1 << NET_CAPABILITY_FOTA) |
320 (1 << NET_CAPABILITY_IA) |
321 (1 << NET_CAPABILITY_IMS) |
322 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev43403202018-01-30 17:19:44 -0800323 (1 << NET_CAPABILITY_XCAP) |
324 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400325
326 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700327 * Capabilities that suggest that a network is unrestricted.
328 * {@see #maybeMarkCapabilitiesRestricted}.
329 */
330 @VisibleForTesting
331 /* package */ static final long UNRESTRICTED_CAPABILITIES =
332 (1 << NET_CAPABILITY_INTERNET) |
333 (1 << NET_CAPABILITY_MMS) |
334 (1 << NET_CAPABILITY_SUPL) |
335 (1 << NET_CAPABILITY_WIFI_P2P);
336
337 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700338 * Adds the given capability to this {@code NetworkCapability} instance.
339 * Multiple capabilities may be applied sequentially. Note that when searching
340 * for a network to satisfy a request, all capabilities requested must be satisfied.
341 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600342 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900343 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700344 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700345 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600346 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700347 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700348 throw new IllegalArgumentException("NetworkCapability out of range");
349 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700350 mNetworkCapabilities |= 1 << capability;
351 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700352 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700353
354 /**
355 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
356 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600357 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900358 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700359 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700360 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600361 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700362 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700363 throw new IllegalArgumentException("NetworkCapability out of range");
364 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700365 mNetworkCapabilities &= ~(1 << capability);
366 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700367 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700368
369 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600370 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
371 * instance.
372 *
373 * @hide
374 */
375 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
376 if (value) {
377 addCapability(capability);
378 } else {
379 removeCapability(capability);
380 }
381 return this;
382 }
383
384 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700385 * Gets all the capabilities set on this {@code NetworkCapability} instance.
386 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600387 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700388 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700389 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600390 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900391 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700392 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700393
394 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600395 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700396 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600397 *
398 * @hide
399 */
400 public void setCapabilities(@NetCapability int[] capabilities) {
401 mNetworkCapabilities = BitUtils.packBits(capabilities);
402 }
403
404 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700405 * Tests for the presence of a capabilitity on this instance.
406 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600407 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700408 * @return {@code true} if set on this instance.
409 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600410 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700411 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700412 return false;
413 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700414 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700415 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700416
Robert Greenwalt1448f052014-04-08 13:41:39 -0700417 private void combineNetCapabilities(NetworkCapabilities nc) {
418 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
419 }
420
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900421 /**
422 * Convenience function that returns a human-readable description of the first mutable
423 * capability we find. Used to present an error message to apps that request mutable
424 * capabilities.
425 *
426 * @hide
427 */
428 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900429 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
430 if (nonRequestable != 0) {
431 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900432 }
433 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900434 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900435 return null;
436 }
437
438 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
439 long networkCapabilities = this.mNetworkCapabilities;
440 if (onlyImmutable) {
441 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
442 }
443 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700444 }
445
Robert Greenwalt06314e42014-10-29 14:04:06 -0700446 /** @hide */
447 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700448 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
449 }
450
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900451 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
452 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
453 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
454 }
455
Robert Greenwalt1448f052014-04-08 13:41:39 -0700456 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400457 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
458 * typically provided by restricted networks.
459 *
460 * TODO: consider:
461 * - Renaming it to guessRestrictedCapability and make it set the
462 * restricted capability bit in addition to clearing it.
463 * @hide
464 */
465 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700466 // Verify there aren't any unrestricted capabilities. If there are we say
467 // the whole thing is unrestricted.
468 final boolean hasUnrestrictedCapabilities =
469 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
470
471 // Must have at least some restricted capabilities.
472 final boolean hasRestrictedCapabilities =
473 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
474
475 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400476 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400477 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400478 }
479
480 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700481 * Representing the transport type. Apps should generally not care about transport. A
482 * request for a fast internet connection could be satisfied by a number of different
483 * transports. If any are specified here it will be satisfied a Network that matches
484 * any of them. If a caller doesn't care about the transport it should not specify any.
485 */
486 private long mTransportTypes;
487
Jeff Sharkeyde570312017-10-24 21:25:50 -0600488 /** @hide */
489 @Retention(RetentionPolicy.SOURCE)
490 @IntDef(prefix = { "TRANSPORT_" }, value = {
491 TRANSPORT_CELLULAR,
492 TRANSPORT_WIFI,
493 TRANSPORT_BLUETOOTH,
494 TRANSPORT_ETHERNET,
495 TRANSPORT_VPN,
496 TRANSPORT_WIFI_AWARE,
497 TRANSPORT_LOWPAN,
498 })
499 public @interface Transport { }
500
Robert Greenwalt1448f052014-04-08 13:41:39 -0700501 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700502 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700503 */
504 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700505
506 /**
507 * Indicates this network uses a Wi-Fi transport.
508 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700509 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700510
511 /**
512 * Indicates this network uses a Bluetooth transport.
513 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700514 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515
516 /**
517 * Indicates this network uses an Ethernet transport.
518 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700519 public static final int TRANSPORT_ETHERNET = 3;
520
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400521 /**
522 * Indicates this network uses a VPN transport.
523 */
524 public static final int TRANSPORT_VPN = 4;
525
Etan Cohen305ea282016-06-20 09:27:12 -0700526 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700527 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700528 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700529 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700530
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700531 /**
532 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700533 */
534 public static final int TRANSPORT_LOWPAN = 6;
535
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900536 /** @hide */
537 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
538 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700539 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700540
Hugo Benichi16f0a942017-06-20 14:07:59 +0900541 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600542 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900543 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
544 }
545
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900546 private static final String[] TRANSPORT_NAMES = {
547 "CELLULAR",
548 "WIFI",
549 "BLUETOOTH",
550 "ETHERNET",
551 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700552 "WIFI_AWARE",
553 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900554 };
555
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700556 /**
557 * Adds the given transport type to this {@code NetworkCapability} instance.
558 * Multiple transports may be applied sequentially. Note that when searching
559 * for a network to satisfy a request, any listed in the request will satisfy the request.
560 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
561 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
562 * to be selected. This is logically different than
563 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
564 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600565 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900566 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700567 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700568 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600569 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900570 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700572 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700573 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700574 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700575
576 /**
577 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
578 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600579 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900580 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700581 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700582 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600583 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900584 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700585 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700586 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700587 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700588 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700589
590 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600591 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
592 * instance.
593 *
594 * @hide
595 */
596 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
597 if (value) {
598 addTransportType(transportType);
599 } else {
600 removeTransportType(transportType);
601 }
602 return this;
603 }
604
605 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700606 * Gets all the transports set on this {@code NetworkCapability} instance.
607 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600608 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700609 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700610 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600611 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900612 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700614
615 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600616 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700617 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600618 *
619 * @hide
620 */
621 public void setTransportTypes(@Transport int[] transportTypes) {
622 mTransportTypes = BitUtils.packBits(transportTypes);
623 }
624
625 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700626 * Tests for the presence of a transport on this instance.
627 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600628 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700629 * @return {@code true} if set on this instance.
630 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600631 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900632 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700633 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700634
635 private void combineTransportTypes(NetworkCapabilities nc) {
636 this.mTransportTypes |= nc.mTransportTypes;
637 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900638
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
640 return ((this.mTransportTypes == 0) ||
641 ((this.mTransportTypes & nc.mTransportTypes) != 0));
642 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900643
Robert Greenwalt06314e42014-10-29 14:04:06 -0700644 /** @hide */
645 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700646 return (nc.mTransportTypes == this.mTransportTypes);
647 }
648
649 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900650 * UID of the app that manages this network, or INVALID_UID if none/unknown.
651 *
652 * This field keeps track of the UID of the app that created this network and is in charge
653 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
654 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
655 * to create networks.
656 *
657 * Because this field is only used in the services side (and to avoid apps being able to
658 * set this to whatever they want), this field is not parcelled and will not be conserved
659 * across the IPC boundary.
660 * @hide
661 */
662 private int mEstablishingVpnAppUid = INVALID_UID;
663
664 /**
665 * Set the UID of the managing app.
666 * @hide
667 */
668 public void setEstablishingVpnAppUid(final int uid) {
669 mEstablishingVpnAppUid = uid;
670 }
671
672 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600673 * Value indicating that link bandwidth is unspecified.
674 * @hide
675 */
676 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
677
678 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700679 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
680 * for the first hop on the given transport. It is not measured, but may take into account
681 * link parameters (Radio technology, allocated channels, etc).
682 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600683 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
684 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700685
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700686 /**
687 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
688 * the estimated first hop transport bandwidth.
689 * <p>
690 * Note that when used to request a network, this specifies the minimum acceptable.
691 * When received as the state of an existing network this specifies the typical
692 * first hop bandwidth expected. This is never measured, but rather is inferred
693 * from technology type and other link parameters. It could be used to differentiate
694 * between very slow 1xRTT cellular links and other faster networks or even between
695 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
696 * fast backhauls and slow backhauls.
697 *
698 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700699 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700700 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600701 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700702 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600703 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700704 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700705
706 /**
707 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
708 * the estimated first hop transport bandwidth.
709 *
710 * @return The estimated first hop upstream (device to network) bandwidth.
711 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700712 public int getLinkUpstreamBandwidthKbps() {
713 return mLinkUpBandwidthKbps;
714 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700715
716 /**
717 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
718 * the estimated first hop transport bandwidth.
719 * <p>
720 * Note that when used to request a network, this specifies the minimum acceptable.
721 * When received as the state of an existing network this specifies the typical
722 * first hop bandwidth expected. This is never measured, but rather is inferred
723 * from technology type and other link parameters. It could be used to differentiate
724 * between very slow 1xRTT cellular links and other faster networks or even between
725 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
726 * fast backhauls and slow backhauls.
727 *
728 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700729 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700730 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600731 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700732 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600733 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700734 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700735
736 /**
737 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
738 * the estimated first hop transport bandwidth.
739 *
740 * @return The estimated first hop downstream (network to device) bandwidth.
741 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700742 public int getLinkDownstreamBandwidthKbps() {
743 return mLinkDownBandwidthKbps;
744 }
745
746 private void combineLinkBandwidths(NetworkCapabilities nc) {
747 this.mLinkUpBandwidthKbps =
748 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
749 this.mLinkDownBandwidthKbps =
750 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
751 }
752 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
753 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
754 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
755 }
756 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
757 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
758 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
759 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600760 /** @hide */
761 public static int minBandwidth(int a, int b) {
762 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
763 return b;
764 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
765 return a;
766 } else {
767 return Math.min(a, b);
768 }
769 }
770 /** @hide */
771 public static int maxBandwidth(int a, int b) {
772 return Math.max(a, b);
773 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700774
Etan Cohena7434272017-04-03 12:17:51 -0700775 private NetworkSpecifier mNetworkSpecifier = null;
776
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700777 /**
778 * Sets the optional bearer specific network specifier.
779 * This has no meaning if a single transport is also not specified, so calling
780 * this without a single transport set will generate an exception, as will
781 * subsequently adding or removing transports after this is set.
782 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700783 *
Etan Cohena7434272017-04-03 12:17:51 -0700784 * @param networkSpecifier A concrete, parcelable framework class that extends
785 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900786 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700787 * @hide
788 */
Etan Cohena7434272017-04-03 12:17:51 -0700789 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
790 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700791 throw new IllegalStateException("Must have a single transport specified to use " +
792 "setNetworkSpecifier");
793 }
Etan Cohena7434272017-04-03 12:17:51 -0700794
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700795 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700796
Pierre Imaic8419a82016-03-22 17:54:54 +0900797 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700798 }
799
800 /**
801 * Gets the optional bearer specific network specifier.
802 *
Etan Cohena7434272017-04-03 12:17:51 -0700803 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
804 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700805 * @hide
806 */
Etan Cohena7434272017-04-03 12:17:51 -0700807 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700808 return mNetworkSpecifier;
809 }
810
811 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700812 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700813 throw new IllegalStateException("Can't combine two networkSpecifiers");
814 }
Etan Cohena7434272017-04-03 12:17:51 -0700815 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700816 }
Etan Cohena7434272017-04-03 12:17:51 -0700817
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700818 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700819 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
820 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700821 }
Etan Cohena7434272017-04-03 12:17:51 -0700822
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700823 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700824 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700825 }
826
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900828 * Magic value that indicates no signal strength provided. A request specifying this value is
829 * always satisfied.
830 *
831 * @hide
832 */
833 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
834
835 /**
836 * Signal strength. This is a signed integer, and higher values indicate better signal.
837 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
838 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700839 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900840
841 /**
842 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
843 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
844 * reported by WifiManager.
845 * <p>
846 * Note that when used to register a network callback, this specifies the minimum acceptable
847 * signal strength. When received as the state of an existing network it specifies the current
848 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
849 * effect when requesting a callback.
850 *
851 * @param signalStrength the bearer-specific signal strength.
852 * @hide
853 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600854 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900855 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600856 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900857 }
858
859 /**
860 * Returns {@code true} if this object specifies a signal strength.
861 *
862 * @hide
863 */
864 public boolean hasSignalStrength() {
865 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
866 }
867
868 /**
869 * Retrieves the signal strength.
870 *
871 * @return The bearer-specific signal strength.
872 * @hide
873 */
874 public int getSignalStrength() {
875 return mSignalStrength;
876 }
877
878 private void combineSignalStrength(NetworkCapabilities nc) {
879 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
880 }
881
882 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
883 return this.mSignalStrength <= nc.mSignalStrength;
884 }
885
886 private boolean equalsSignalStrength(NetworkCapabilities nc) {
887 return this.mSignalStrength == nc.mSignalStrength;
888 }
889
890 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900891 * List of UIDs this network applies to. No restriction if null.
892 * <p>
893 * This is typically (and at this time, only) used by VPN. This network is only available to
894 * the UIDs in this list, and it is their default network. Apps in this list that wish to
895 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
896 * member is null, then the network is not restricted by app UID. If it's an empty list, then
897 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900898 * As a special exception, the app managing this network (as identified by its UID stored in
899 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
900 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
901 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900902 * <p>
903 * Please note that in principle a single app can be associated with multiple UIDs because
904 * each app will have a different UID when it's run as a different (macro-)user. A single
905 * macro user can only have a single active VPN app at any given time however.
906 * <p>
907 * Also please be aware this class does not try to enforce any normalization on this. Callers
908 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
909 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
910 * their own (like requiring sortedness or no overlap) they need to enforce it
911 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
912 * or overlapping ranges are present.
913 *
914 * @hide
915 */
Chalard Jeancf8c3fe2018-01-25 09:41:51 +0900916 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900917
918 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900919 * Convenience method to set the UIDs this network applies to to a single UID.
920 * @hide
921 */
922 public NetworkCapabilities setSingleUid(int uid) {
923 final ArraySet<UidRange> identity = new ArraySet<>(1);
924 identity.add(new UidRange(uid, uid));
925 setUids(identity);
926 return this;
927 }
928
929 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900930 * Set the list of UIDs this network applies to.
931 * This makes a copy of the set so that callers can't modify it after the call.
932 * @hide
933 */
934 public NetworkCapabilities setUids(Set<UidRange> uids) {
935 if (null == uids) {
936 mUids = null;
937 } else {
938 mUids = new ArraySet<>(uids);
939 }
940 return this;
941 }
942
943 /**
944 * Get the list of UIDs this network applies to.
945 * This returns a copy of the set so that callers can't modify the original object.
946 * @hide
947 */
948 public Set<UidRange> getUids() {
949 return null == mUids ? null : new ArraySet<>(mUids);
950 }
951
952 /**
953 * Test whether this network applies to this UID.
954 * @hide
955 */
956 public boolean appliesToUid(int uid) {
957 if (null == mUids) return true;
958 for (UidRange range : mUids) {
959 if (range.contains(uid)) {
960 return true;
961 }
962 }
963 return false;
964 }
965
966 /**
967 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
968 * <p>
969 * This test only checks whether equal range objects are in both sets. It will
970 * return false if the ranges are not exactly the same, even if the covered UIDs
971 * are for an equivalent result.
972 * <p>
973 * Note that this method is not very optimized, which is fine as long as it's not used very
974 * often.
975 * <p>
976 * nc is assumed nonnull.
977 *
978 * @hide
979 */
980 @VisibleForTesting
981 public boolean equalsUids(NetworkCapabilities nc) {
982 Set<UidRange> comparedUids = nc.mUids;
983 if (null == comparedUids) return null == mUids;
984 if (null == mUids) return false;
985 // Make a copy so it can be mutated to check that all ranges in mUids
986 // also are in uids.
987 final Set<UidRange> uids = new ArraySet<>(mUids);
988 for (UidRange range : comparedUids) {
989 if (!uids.contains(range)) {
990 return false;
991 }
992 uids.remove(range);
993 }
994 return uids.isEmpty();
995 }
996
997 /**
998 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
999 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001000 * This method is called on the NetworkCapabilities embedded in a request with the
1001 * capabilities of an available network. It checks whether all the UIDs from this listen
1002 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1003 * in the passed nc (representing the UIDs that this network is available to).
1004 * <p>
1005 * As a special exception, the UID that created the passed network (as represented by its
1006 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1007 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1008 * can see its own network when it listens for it.
1009 * <p>
1010 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001011 * @see #appliesToUid
1012 * @hide
1013 */
1014 public boolean satisfiedByUids(NetworkCapabilities nc) {
1015 if (null == nc.mUids) return true; // The network satisfies everything.
1016 if (null == mUids) return false; // Not everything allowed but requires everything
1017 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001018 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001019 if (!nc.appliesToUidRange(requiredRange)) {
1020 return false;
1021 }
1022 }
1023 return true;
1024 }
1025
1026 /**
1027 * Returns whether this network applies to the passed ranges.
1028 * This assumes that to apply, the passed range has to be entirely contained
1029 * within one of the ranges this network applies to. If the ranges are not normalized,
1030 * this method may return false even though all required UIDs are covered because no
1031 * single range contained them all.
1032 * @hide
1033 */
1034 @VisibleForTesting
1035 public boolean appliesToUidRange(UidRange requiredRange) {
1036 if (null == mUids) return true;
1037 for (UidRange uidRange : mUids) {
1038 if (uidRange.containsRange(requiredRange)) {
1039 return true;
1040 }
1041 }
1042 return false;
1043 }
1044
1045 /**
1046 * Combine the UIDs this network currently applies to with the UIDs the passed
1047 * NetworkCapabilities apply to.
1048 * nc is assumed nonnull.
1049 */
1050 private void combineUids(NetworkCapabilities nc) {
1051 if (null == nc.mUids || null == mUids) {
1052 mUids = null;
1053 return;
1054 }
1055 mUids.addAll(nc.mUids);
1056 }
1057
1058 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001059 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001060 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001061 */
1062 public void combineCapabilities(NetworkCapabilities nc) {
1063 combineNetCapabilities(nc);
1064 combineTransportTypes(nc);
1065 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001066 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001067 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001068 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001069 }
1070
1071 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001072 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1073 *
1074 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1075 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1076 * bandwidth, signal strength, or validation / captive portal status.
1077 *
1078 * @hide
1079 */
1080 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001081 return (nc != null
1082 && satisfiedByNetCapabilities(nc, onlyImmutable)
1083 && satisfiedByTransportTypes(nc)
1084 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1085 && satisfiedBySpecifier(nc)
1086 && (onlyImmutable || satisfiedBySignalStrength(nc))
1087 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001088 }
1089
1090 /**
1091 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1092 *
1093 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1094 *
1095 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001096 */
1097 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001098 return satisfiedByNetworkCapabilities(nc, false);
1099 }
1100
1101 /**
1102 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1103 *
1104 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1105 *
1106 * @hide
1107 */
1108 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1109 return satisfiedByNetworkCapabilities(nc, true);
1110 }
1111
1112 /**
1113 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001114 * {@code NetworkCapabilities} and return a String describing any difference.
1115 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001116 *
1117 * @hide
1118 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001119 public String describeImmutableDifferences(NetworkCapabilities that) {
1120 if (that == null) {
1121 return "other NetworkCapabilities was null";
1122 }
1123
1124 StringJoiner joiner = new StringJoiner(", ");
1125
Hugo Benichieae7a222017-07-25 11:40:56 +09001126 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1127 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001128 // Ignore DUN being added or removed. http://b/65257223.
1129 final long mask = ~MUTABLE_CAPABILITIES
1130 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001131 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1132 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1133 if (oldImmutableCapabilities != newImmutableCapabilities) {
1134 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1135 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1136 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1137 }
1138
1139 if (!equalsSpecifier(that)) {
1140 NetworkSpecifier before = this.getNetworkSpecifier();
1141 NetworkSpecifier after = that.getNetworkSpecifier();
1142 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1143 }
1144
1145 if (!equalsTransportTypes(that)) {
1146 String before = transportNamesOf(this.getTransportTypes());
1147 String after = transportNamesOf(that.getTransportTypes());
1148 joiner.add(String.format("transports changed: %s -> %s", before, after));
1149 }
1150
1151 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001152 }
1153
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001154 /**
1155 * Checks that our requestable capabilities are the same as those of the given
1156 * {@code NetworkCapabilities}.
1157 *
1158 * @hide
1159 */
1160 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1161 if (nc == null) return false;
1162 return (equalsNetCapabilitiesRequestable(nc) &&
1163 equalsTransportTypes(nc) &&
1164 equalsSpecifier(nc));
1165 }
1166
Robert Greenwalt1448f052014-04-08 13:41:39 -07001167 @Override
1168 public boolean equals(Object obj) {
1169 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001170 NetworkCapabilities that = (NetworkCapabilities) obj;
1171 return (equalsNetCapabilities(that)
1172 && equalsTransportTypes(that)
1173 && equalsLinkBandwidths(that)
1174 && equalsSignalStrength(that)
1175 && equalsSpecifier(that)
1176 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001177 }
1178
1179 @Override
1180 public int hashCode() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001181 return ((int) (mNetworkCapabilities & 0xFFFFFFFF)
1182 + ((int) (mNetworkCapabilities >> 32) * 3)
1183 + ((int) (mTransportTypes & 0xFFFFFFFF) * 5)
1184 + ((int) (mTransportTypes >> 32) * 7)
1185 + (mLinkUpBandwidthKbps * 11)
1186 + (mLinkDownBandwidthKbps * 13)
1187 + Objects.hashCode(mNetworkSpecifier) * 17
1188 + (mSignalStrength * 19)
1189 + Objects.hashCode(mUids) * 23);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001190 }
1191
Wink Saville4e2dea72014-09-20 11:04:03 -07001192 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001193 public int describeContents() {
1194 return 0;
1195 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001196 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001197 public void writeToParcel(Parcel dest, int flags) {
1198 dest.writeLong(mNetworkCapabilities);
1199 dest.writeLong(mTransportTypes);
1200 dest.writeInt(mLinkUpBandwidthKbps);
1201 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001202 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001203 dest.writeInt(mSignalStrength);
Chalard Jeancf8c3fe2018-01-25 09:41:51 +09001204 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001205 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001206
Robert Greenwalt1448f052014-04-08 13:41:39 -07001207 public static final Creator<NetworkCapabilities> CREATOR =
1208 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001209 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001210 public NetworkCapabilities createFromParcel(Parcel in) {
1211 NetworkCapabilities netCap = new NetworkCapabilities();
1212
1213 netCap.mNetworkCapabilities = in.readLong();
1214 netCap.mTransportTypes = in.readLong();
1215 netCap.mLinkUpBandwidthKbps = in.readInt();
1216 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001217 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001218 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001219 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1220 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001221 return netCap;
1222 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001223 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001224 public NetworkCapabilities[] newArray(int size) {
1225 return new NetworkCapabilities[size];
1226 }
1227 };
1228
Wink Saville4e2dea72014-09-20 11:04:03 -07001229 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001230 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001231 final StringBuilder sb = new StringBuilder("[");
1232 if (0 != mTransportTypes) {
1233 sb.append(" Transports: ");
1234 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1235 NetworkCapabilities::transportNameOf, "|");
1236 }
1237 if (0 != mNetworkCapabilities) {
1238 sb.append(" Capabilities: ");
1239 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1240 NetworkCapabilities::capabilityNameOf, "&");
1241 }
1242 if (mLinkUpBandwidthKbps > 0) {
1243 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1244 }
1245 if (mLinkDownBandwidthKbps > 0) {
1246 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1247 }
1248 if (mNetworkSpecifier != null) {
1249 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1250 }
1251 if (hasSignalStrength()) {
1252 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001253 }
1254
Chalard Jean07ace0f2018-02-26 19:00:45 +09001255 if (null != mUids) {
1256 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1257 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1258 } else {
1259 sb.append(" Uids: <").append(mUids).append(">");
1260 }
1261 }
1262 if (mEstablishingVpnAppUid != INVALID_UID) {
1263 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1264 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001265
Chalard Jean07ace0f2018-02-26 19:00:45 +09001266 sb.append("]");
1267 return sb.toString();
1268 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001269
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001270
Chalard Jean07ace0f2018-02-26 19:00:45 +09001271 private interface NameOf {
1272 String nameOf(int value);
1273 }
1274 /**
1275 * @hide
1276 */
1277 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1278 long bitMask, NameOf nameFetcher, String separator) {
1279 int bitPos = 0;
1280 boolean firstElementAdded = false;
1281 while (bitMask != 0) {
1282 if ((bitMask & 1) != 0) {
1283 if (firstElementAdded) {
1284 sb.append(separator);
1285 } else {
1286 firstElementAdded = true;
1287 }
1288 sb.append(nameFetcher.nameOf(bitPos));
1289 }
1290 bitMask >>= 1;
1291 ++bitPos;
1292 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001293 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001294
1295 /**
1296 * @hide
1297 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001298 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001299 StringJoiner joiner = new StringJoiner("|");
1300 if (capabilities != null) {
1301 for (int c : capabilities) {
1302 joiner.add(capabilityNameOf(c));
1303 }
1304 }
1305 return joiner.toString();
1306 }
1307
1308 /**
1309 * @hide
1310 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001311 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001312 switch (capability) {
1313 case NET_CAPABILITY_MMS: return "MMS";
1314 case NET_CAPABILITY_SUPL: return "SUPL";
1315 case NET_CAPABILITY_DUN: return "DUN";
1316 case NET_CAPABILITY_FOTA: return "FOTA";
1317 case NET_CAPABILITY_IMS: return "IMS";
1318 case NET_CAPABILITY_CBS: return "CBS";
1319 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1320 case NET_CAPABILITY_IA: return "IA";
1321 case NET_CAPABILITY_RCS: return "RCS";
1322 case NET_CAPABILITY_XCAP: return "XCAP";
1323 case NET_CAPABILITY_EIMS: return "EIMS";
1324 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1325 case NET_CAPABILITY_INTERNET: return "INTERNET";
1326 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1327 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1328 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1329 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1330 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001331 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001332 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001333 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001334 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001335 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001336 default: return Integer.toString(capability);
1337 }
1338 }
1339
1340 /**
1341 * @hide
1342 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001343 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001344 StringJoiner joiner = new StringJoiner("|");
1345 if (types != null) {
1346 for (int t : types) {
1347 joiner.add(transportNameOf(t));
1348 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001349 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001350 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001351 }
1352
1353 /**
1354 * @hide
1355 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001356 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001357 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001358 return "UNKNOWN";
1359 }
1360 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001361 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001362
Jeff Sharkeyde570312017-10-24 21:25:50 -06001363 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001364 Preconditions.checkArgument(
1365 isValidTransport(transport), "Invalid TransportType " + transport);
1366 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001367}