blob: 6e80f024eeac23674777653339f528f5a7ee797f [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;
Chalard Jeance1a9d82018-01-23 21:25:37 +090024import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070025
26import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090027import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090028import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070029
Jeff Sharkeyde570312017-10-24 21:25:50 -060030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070032import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090033import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090034import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070035
36/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070037 * Representation of the capabilities of an active network. Instances are
38 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060039 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
40 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060041 * <p>
42 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
43 * network selection. Rather than indicate a need for Wi-Fi because an
44 * application needs high bandwidth and risk obsolescence when a new, fast
45 * network appears (like LTE), the application should specify it needs high
46 * bandwidth. Similarly if an application needs an unmetered network for a bulk
47 * transfer it can specify that rather than assuming all cellular based
48 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070049 */
50public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070051 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090052 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070053
Robert Greenwalt7569f182014-06-08 16:42:59 -070054 /**
55 * @hide
56 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070057 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090058 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090059 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070060 }
61
62 public NetworkCapabilities(NetworkCapabilities nc) {
63 if (nc != null) {
64 mNetworkCapabilities = nc.mNetworkCapabilities;
65 mTransportTypes = nc.mTransportTypes;
66 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
67 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070068 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090069 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090070 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090071 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070072 }
73 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070074
75 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090076 * Completely clears the contents of this object, removing even the capabilities that are set
77 * by default when the object is constructed.
78 * @hide
79 */
80 public void clearAll() {
81 mNetworkCapabilities = mTransportTypes = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070082 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090083 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090084 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090085 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090086 mEstablishingVpnAppUid = INVALID_UID;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090087 }
88
89 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070090 * Represents the network's capabilities. If any are specified they will be satisfied
91 * by any Network that matches all of them.
92 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090093 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070094
Jeff Sharkeyde570312017-10-24 21:25:50 -060095 /** @hide */
96 @Retention(RetentionPolicy.SOURCE)
97 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
98 NET_CAPABILITY_MMS,
99 NET_CAPABILITY_SUPL,
100 NET_CAPABILITY_DUN,
101 NET_CAPABILITY_FOTA,
102 NET_CAPABILITY_IMS,
103 NET_CAPABILITY_CBS,
104 NET_CAPABILITY_WIFI_P2P,
105 NET_CAPABILITY_IA,
106 NET_CAPABILITY_RCS,
107 NET_CAPABILITY_XCAP,
108 NET_CAPABILITY_EIMS,
109 NET_CAPABILITY_NOT_METERED,
110 NET_CAPABILITY_INTERNET,
111 NET_CAPABILITY_NOT_RESTRICTED,
112 NET_CAPABILITY_TRUSTED,
113 NET_CAPABILITY_NOT_VPN,
114 NET_CAPABILITY_VALIDATED,
115 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600116 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600117 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900118 NET_CAPABILITY_NOT_CONGESTED,
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.
242 * @hide
243 */
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
Robert Greenwalt1448f052014-04-08 13:41:39 -0700255 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900256 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_CONGESTED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700257
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700258 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900259 * Network capabilities that are expected to be mutable, i.e., can change while a particular
260 * network is connected.
261 */
262 private static final long MUTABLE_CAPABILITIES =
263 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
264 // http://b/18206275
265 (1 << NET_CAPABILITY_TRUSTED) |
266 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900267 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600268 (1 << NET_CAPABILITY_NOT_ROAMING) |
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900269 (1 << NET_CAPABILITY_FOREGROUND) |
270 (1 << NET_CAPABILITY_NOT_CONGESTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900271
272 /**
273 * Network capabilities that are not allowed in NetworkRequests. This exists because the
274 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
275 * capability's presence cannot be known in advance. If such a capability is requested, then we
276 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
277 * get immediately torn down because they do not have the requested capability.
278 */
279 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900280 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900281
282 /**
283 * Capabilities that are set by default when the object is constructed.
284 */
285 private static final long DEFAULT_CAPABILITIES =
286 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
287 (1 << NET_CAPABILITY_TRUSTED) |
288 (1 << NET_CAPABILITY_NOT_VPN);
289
290 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400291 * Capabilities that suggest that a network is restricted.
292 * {@see #maybeMarkCapabilitiesRestricted}.
293 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700294 @VisibleForTesting
295 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400296 (1 << NET_CAPABILITY_CBS) |
297 (1 << NET_CAPABILITY_DUN) |
298 (1 << NET_CAPABILITY_EIMS) |
299 (1 << NET_CAPABILITY_FOTA) |
300 (1 << NET_CAPABILITY_IA) |
301 (1 << NET_CAPABILITY_IMS) |
302 (1 << NET_CAPABILITY_RCS) |
303 (1 << NET_CAPABILITY_XCAP);
304
305 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700306 * Capabilities that suggest that a network is unrestricted.
307 * {@see #maybeMarkCapabilitiesRestricted}.
308 */
309 @VisibleForTesting
310 /* package */ static final long UNRESTRICTED_CAPABILITIES =
311 (1 << NET_CAPABILITY_INTERNET) |
312 (1 << NET_CAPABILITY_MMS) |
313 (1 << NET_CAPABILITY_SUPL) |
314 (1 << NET_CAPABILITY_WIFI_P2P);
315
316 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700317 * Adds the given capability to this {@code NetworkCapability} instance.
318 * Multiple capabilities may be applied sequentially. Note that when searching
319 * for a network to satisfy a request, all capabilities requested must be satisfied.
320 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600321 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900322 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700323 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700324 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600325 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700326 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700327 throw new IllegalArgumentException("NetworkCapability out of range");
328 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700329 mNetworkCapabilities |= 1 << capability;
330 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700331 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700332
333 /**
334 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
335 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600336 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900337 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700338 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700339 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600340 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700341 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700342 throw new IllegalArgumentException("NetworkCapability out of range");
343 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700344 mNetworkCapabilities &= ~(1 << capability);
345 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700346 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700347
348 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600349 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
350 * instance.
351 *
352 * @hide
353 */
354 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
355 if (value) {
356 addCapability(capability);
357 } else {
358 removeCapability(capability);
359 }
360 return this;
361 }
362
363 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700364 * Gets all the capabilities set on this {@code NetworkCapability} instance.
365 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600366 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700367 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700368 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600369 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900370 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700371 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700372
373 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600374 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700375 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600376 *
377 * @hide
378 */
379 public void setCapabilities(@NetCapability int[] capabilities) {
380 mNetworkCapabilities = BitUtils.packBits(capabilities);
381 }
382
383 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384 * Tests for the presence of a capabilitity on this instance.
385 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600386 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700387 * @return {@code true} if set on this instance.
388 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600389 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700390 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700391 return false;
392 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700393 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700394 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700395
Robert Greenwalt1448f052014-04-08 13:41:39 -0700396 private void combineNetCapabilities(NetworkCapabilities nc) {
397 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
398 }
399
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900400 /**
401 * Convenience function that returns a human-readable description of the first mutable
402 * capability we find. Used to present an error message to apps that request mutable
403 * capabilities.
404 *
405 * @hide
406 */
407 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900408 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
409 if (nonRequestable != 0) {
410 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900411 }
412 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900413 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900414 return null;
415 }
416
417 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
418 long networkCapabilities = this.mNetworkCapabilities;
419 if (onlyImmutable) {
420 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
421 }
422 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700423 }
424
Robert Greenwalt06314e42014-10-29 14:04:06 -0700425 /** @hide */
426 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700427 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
428 }
429
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900430 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
431 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
432 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
433 }
434
Robert Greenwalt1448f052014-04-08 13:41:39 -0700435 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400436 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
437 * typically provided by restricted networks.
438 *
439 * TODO: consider:
440 * - Renaming it to guessRestrictedCapability and make it set the
441 * restricted capability bit in addition to clearing it.
442 * @hide
443 */
444 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700445 // Verify there aren't any unrestricted capabilities. If there are we say
446 // the whole thing is unrestricted.
447 final boolean hasUnrestrictedCapabilities =
448 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
449
450 // Must have at least some restricted capabilities.
451 final boolean hasRestrictedCapabilities =
452 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
453
454 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400455 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400456 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400457 }
458
459 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700460 * Representing the transport type. Apps should generally not care about transport. A
461 * request for a fast internet connection could be satisfied by a number of different
462 * transports. If any are specified here it will be satisfied a Network that matches
463 * any of them. If a caller doesn't care about the transport it should not specify any.
464 */
465 private long mTransportTypes;
466
Jeff Sharkeyde570312017-10-24 21:25:50 -0600467 /** @hide */
468 @Retention(RetentionPolicy.SOURCE)
469 @IntDef(prefix = { "TRANSPORT_" }, value = {
470 TRANSPORT_CELLULAR,
471 TRANSPORT_WIFI,
472 TRANSPORT_BLUETOOTH,
473 TRANSPORT_ETHERNET,
474 TRANSPORT_VPN,
475 TRANSPORT_WIFI_AWARE,
476 TRANSPORT_LOWPAN,
477 })
478 public @interface Transport { }
479
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700481 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700482 */
483 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700484
485 /**
486 * Indicates this network uses a Wi-Fi transport.
487 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700488 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700489
490 /**
491 * Indicates this network uses a Bluetooth transport.
492 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700493 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700494
495 /**
496 * Indicates this network uses an Ethernet transport.
497 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700498 public static final int TRANSPORT_ETHERNET = 3;
499
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400500 /**
501 * Indicates this network uses a VPN transport.
502 */
503 public static final int TRANSPORT_VPN = 4;
504
Etan Cohen305ea282016-06-20 09:27:12 -0700505 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700506 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700507 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700508 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700509
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700510 /**
511 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700512 */
513 public static final int TRANSPORT_LOWPAN = 6;
514
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900515 /** @hide */
516 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
517 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700518 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700519
Hugo Benichi16f0a942017-06-20 14:07:59 +0900520 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600521 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900522 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
523 }
524
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900525 private static final String[] TRANSPORT_NAMES = {
526 "CELLULAR",
527 "WIFI",
528 "BLUETOOTH",
529 "ETHERNET",
530 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700531 "WIFI_AWARE",
532 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900533 };
534
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700535 /**
536 * Adds the given transport type to this {@code NetworkCapability} instance.
537 * Multiple transports may be applied sequentially. Note that when searching
538 * for a network to satisfy a request, any listed in the request will satisfy the request.
539 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
540 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
541 * to be selected. This is logically different than
542 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
543 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600544 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900545 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700546 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700547 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600548 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900549 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700550 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700551 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700552 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700553 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700554
555 /**
556 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
557 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600558 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900559 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700560 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700561 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600562 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900563 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700564 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700565 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700566 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700567 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700568
569 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600570 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
571 * instance.
572 *
573 * @hide
574 */
575 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
576 if (value) {
577 addTransportType(transportType);
578 } else {
579 removeTransportType(transportType);
580 }
581 return this;
582 }
583
584 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700585 * Gets all the transports set on this {@code NetworkCapability} instance.
586 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600587 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700588 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700589 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600590 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900591 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700592 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700593
594 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600595 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700596 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600597 *
598 * @hide
599 */
600 public void setTransportTypes(@Transport int[] transportTypes) {
601 mTransportTypes = BitUtils.packBits(transportTypes);
602 }
603
604 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700605 * Tests for the presence of a transport on this instance.
606 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600607 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700608 * @return {@code true} if set on this instance.
609 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600610 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900611 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700612 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613
614 private void combineTransportTypes(NetworkCapabilities nc) {
615 this.mTransportTypes |= nc.mTransportTypes;
616 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900617
Robert Greenwalt1448f052014-04-08 13:41:39 -0700618 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
619 return ((this.mTransportTypes == 0) ||
620 ((this.mTransportTypes & nc.mTransportTypes) != 0));
621 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900622
Robert Greenwalt06314e42014-10-29 14:04:06 -0700623 /** @hide */
624 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700625 return (nc.mTransportTypes == this.mTransportTypes);
626 }
627
628 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900629 * UID of the app that manages this network, or INVALID_UID if none/unknown.
630 *
631 * This field keeps track of the UID of the app that created this network and is in charge
632 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
633 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
634 * to create networks.
635 *
636 * Because this field is only used in the services side (and to avoid apps being able to
637 * set this to whatever they want), this field is not parcelled and will not be conserved
638 * across the IPC boundary.
639 * @hide
640 */
641 private int mEstablishingVpnAppUid = INVALID_UID;
642
643 /**
644 * Set the UID of the managing app.
645 * @hide
646 */
647 public void setEstablishingVpnAppUid(final int uid) {
648 mEstablishingVpnAppUid = uid;
649 }
650
651 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600652 * Value indicating that link bandwidth is unspecified.
653 * @hide
654 */
655 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
656
657 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700658 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
659 * for the first hop on the given transport. It is not measured, but may take into account
660 * link parameters (Radio technology, allocated channels, etc).
661 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600662 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
663 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700664
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700665 /**
666 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
667 * the estimated first hop transport bandwidth.
668 * <p>
669 * Note that when used to request a network, this specifies the minimum acceptable.
670 * When received as the state of an existing network this specifies the typical
671 * first hop bandwidth expected. This is never measured, but rather is inferred
672 * from technology type and other link parameters. It could be used to differentiate
673 * between very slow 1xRTT cellular links and other faster networks or even between
674 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
675 * fast backhauls and slow backhauls.
676 *
677 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700678 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700679 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600680 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700681 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600682 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700683 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700684
685 /**
686 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
687 * the estimated first hop transport bandwidth.
688 *
689 * @return The estimated first hop upstream (device to network) bandwidth.
690 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700691 public int getLinkUpstreamBandwidthKbps() {
692 return mLinkUpBandwidthKbps;
693 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700694
695 /**
696 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
697 * the estimated first hop transport bandwidth.
698 * <p>
699 * Note that when used to request a network, this specifies the minimum acceptable.
700 * When received as the state of an existing network this specifies the typical
701 * first hop bandwidth expected. This is never measured, but rather is inferred
702 * from technology type and other link parameters. It could be used to differentiate
703 * between very slow 1xRTT cellular links and other faster networks or even between
704 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
705 * fast backhauls and slow backhauls.
706 *
707 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700708 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700709 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600710 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700711 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600712 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700713 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700714
715 /**
716 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
717 * the estimated first hop transport bandwidth.
718 *
719 * @return The estimated first hop downstream (network to device) bandwidth.
720 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700721 public int getLinkDownstreamBandwidthKbps() {
722 return mLinkDownBandwidthKbps;
723 }
724
725 private void combineLinkBandwidths(NetworkCapabilities nc) {
726 this.mLinkUpBandwidthKbps =
727 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
728 this.mLinkDownBandwidthKbps =
729 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
730 }
731 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
732 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
733 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
734 }
735 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
736 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
737 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
738 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600739 /** @hide */
740 public static int minBandwidth(int a, int b) {
741 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
742 return b;
743 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
744 return a;
745 } else {
746 return Math.min(a, b);
747 }
748 }
749 /** @hide */
750 public static int maxBandwidth(int a, int b) {
751 return Math.max(a, b);
752 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700753
Etan Cohena7434272017-04-03 12:17:51 -0700754 private NetworkSpecifier mNetworkSpecifier = null;
755
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700756 /**
757 * Sets the optional bearer specific network specifier.
758 * This has no meaning if a single transport is also not specified, so calling
759 * this without a single transport set will generate an exception, as will
760 * subsequently adding or removing transports after this is set.
761 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700762 *
Etan Cohena7434272017-04-03 12:17:51 -0700763 * @param networkSpecifier A concrete, parcelable framework class that extends
764 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900765 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700766 * @hide
767 */
Etan Cohena7434272017-04-03 12:17:51 -0700768 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
769 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700770 throw new IllegalStateException("Must have a single transport specified to use " +
771 "setNetworkSpecifier");
772 }
Etan Cohena7434272017-04-03 12:17:51 -0700773
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700774 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700775
Pierre Imaic8419a82016-03-22 17:54:54 +0900776 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700777 }
778
779 /**
780 * Gets the optional bearer specific network specifier.
781 *
Etan Cohena7434272017-04-03 12:17:51 -0700782 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
783 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700784 * @hide
785 */
Etan Cohena7434272017-04-03 12:17:51 -0700786 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700787 return mNetworkSpecifier;
788 }
789
790 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700791 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700792 throw new IllegalStateException("Can't combine two networkSpecifiers");
793 }
Etan Cohena7434272017-04-03 12:17:51 -0700794 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700795 }
Etan Cohena7434272017-04-03 12:17:51 -0700796
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700797 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700798 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
799 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700800 }
Etan Cohena7434272017-04-03 12:17:51 -0700801
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700802 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700803 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700804 }
805
Robert Greenwalt1448f052014-04-08 13:41:39 -0700806 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900807 * Magic value that indicates no signal strength provided. A request specifying this value is
808 * always satisfied.
809 *
810 * @hide
811 */
812 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
813
814 /**
815 * Signal strength. This is a signed integer, and higher values indicate better signal.
816 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
817 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700818 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900819
820 /**
821 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
822 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
823 * reported by WifiManager.
824 * <p>
825 * Note that when used to register a network callback, this specifies the minimum acceptable
826 * signal strength. When received as the state of an existing network it specifies the current
827 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
828 * effect when requesting a callback.
829 *
830 * @param signalStrength the bearer-specific signal strength.
831 * @hide
832 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600833 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900834 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600835 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900836 }
837
838 /**
839 * Returns {@code true} if this object specifies a signal strength.
840 *
841 * @hide
842 */
843 public boolean hasSignalStrength() {
844 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
845 }
846
847 /**
848 * Retrieves the signal strength.
849 *
850 * @return The bearer-specific signal strength.
851 * @hide
852 */
853 public int getSignalStrength() {
854 return mSignalStrength;
855 }
856
857 private void combineSignalStrength(NetworkCapabilities nc) {
858 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
859 }
860
861 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
862 return this.mSignalStrength <= nc.mSignalStrength;
863 }
864
865 private boolean equalsSignalStrength(NetworkCapabilities nc) {
866 return this.mSignalStrength == nc.mSignalStrength;
867 }
868
869 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900870 * List of UIDs this network applies to. No restriction if null.
871 * <p>
872 * This is typically (and at this time, only) used by VPN. This network is only available to
873 * the UIDs in this list, and it is their default network. Apps in this list that wish to
874 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
875 * member is null, then the network is not restricted by app UID. If it's an empty list, then
876 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900877 * As a special exception, the app managing this network (as identified by its UID stored in
878 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
879 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
880 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900881 * <p>
882 * Please note that in principle a single app can be associated with multiple UIDs because
883 * each app will have a different UID when it's run as a different (macro-)user. A single
884 * macro user can only have a single active VPN app at any given time however.
885 * <p>
886 * Also please be aware this class does not try to enforce any normalization on this. Callers
887 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
888 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
889 * their own (like requiring sortedness or no overlap) they need to enforce it
890 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
891 * or overlapping ranges are present.
892 *
893 * @hide
894 */
895 private Set<UidRange> mUids = null;
896
897 /**
898 * Set the list of UIDs this network applies to.
899 * This makes a copy of the set so that callers can't modify it after the call.
900 * @hide
901 */
902 public NetworkCapabilities setUids(Set<UidRange> uids) {
903 if (null == uids) {
904 mUids = null;
905 } else {
906 mUids = new ArraySet<>(uids);
907 }
908 return this;
909 }
910
911 /**
912 * Get the list of UIDs this network applies to.
913 * This returns a copy of the set so that callers can't modify the original object.
914 * @hide
915 */
916 public Set<UidRange> getUids() {
917 return null == mUids ? null : new ArraySet<>(mUids);
918 }
919
920 /**
921 * Test whether this network applies to this UID.
922 * @hide
923 */
924 public boolean appliesToUid(int uid) {
925 if (null == mUids) return true;
926 for (UidRange range : mUids) {
927 if (range.contains(uid)) {
928 return true;
929 }
930 }
931 return false;
932 }
933
934 /**
935 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
936 * <p>
937 * This test only checks whether equal range objects are in both sets. It will
938 * return false if the ranges are not exactly the same, even if the covered UIDs
939 * are for an equivalent result.
940 * <p>
941 * Note that this method is not very optimized, which is fine as long as it's not used very
942 * often.
943 * <p>
944 * nc is assumed nonnull.
945 *
946 * @hide
947 */
948 @VisibleForTesting
949 public boolean equalsUids(NetworkCapabilities nc) {
950 Set<UidRange> comparedUids = nc.mUids;
951 if (null == comparedUids) return null == mUids;
952 if (null == mUids) return false;
953 // Make a copy so it can be mutated to check that all ranges in mUids
954 // also are in uids.
955 final Set<UidRange> uids = new ArraySet<>(mUids);
956 for (UidRange range : comparedUids) {
957 if (!uids.contains(range)) {
958 return false;
959 }
960 uids.remove(range);
961 }
962 return uids.isEmpty();
963 }
964
965 /**
966 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
967 *
Chalard Jeanf474fc32018-01-17 15:10:05 +0900968 * This method is called on the NetworkCapabilities embedded in a request with the
969 * capabilities of an available network. It checks whether all the UIDs from this listen
970 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
971 * in the passed nc (representing the UIDs that this network is available to).
972 * <p>
973 * As a special exception, the UID that created the passed network (as represented by its
974 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
975 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
976 * can see its own network when it listens for it.
977 * <p>
978 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900979 * @see #appliesToUid
980 * @hide
981 */
982 public boolean satisfiedByUids(NetworkCapabilities nc) {
983 if (null == nc.mUids) return true; // The network satisfies everything.
984 if (null == mUids) return false; // Not everything allowed but requires everything
985 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +0900986 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900987 if (!nc.appliesToUidRange(requiredRange)) {
988 return false;
989 }
990 }
991 return true;
992 }
993
994 /**
995 * Returns whether this network applies to the passed ranges.
996 * This assumes that to apply, the passed range has to be entirely contained
997 * within one of the ranges this network applies to. If the ranges are not normalized,
998 * this method may return false even though all required UIDs are covered because no
999 * single range contained them all.
1000 * @hide
1001 */
1002 @VisibleForTesting
1003 public boolean appliesToUidRange(UidRange requiredRange) {
1004 if (null == mUids) return true;
1005 for (UidRange uidRange : mUids) {
1006 if (uidRange.containsRange(requiredRange)) {
1007 return true;
1008 }
1009 }
1010 return false;
1011 }
1012
1013 /**
1014 * Combine the UIDs this network currently applies to with the UIDs the passed
1015 * NetworkCapabilities apply to.
1016 * nc is assumed nonnull.
1017 */
1018 private void combineUids(NetworkCapabilities nc) {
1019 if (null == nc.mUids || null == mUids) {
1020 mUids = null;
1021 return;
1022 }
1023 mUids.addAll(nc.mUids);
1024 }
1025
1026 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001027 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001028 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001029 */
1030 public void combineCapabilities(NetworkCapabilities nc) {
1031 combineNetCapabilities(nc);
1032 combineTransportTypes(nc);
1033 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001034 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001035 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001036 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001037 }
1038
1039 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001040 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1041 *
1042 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1043 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1044 * bandwidth, signal strength, or validation / captive portal status.
1045 *
1046 * @hide
1047 */
1048 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001049 return (nc != null
1050 && satisfiedByNetCapabilities(nc, onlyImmutable)
1051 && satisfiedByTransportTypes(nc)
1052 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1053 && satisfiedBySpecifier(nc)
1054 && (onlyImmutable || satisfiedBySignalStrength(nc))
1055 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001056 }
1057
1058 /**
1059 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1060 *
1061 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1062 *
1063 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001064 */
1065 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001066 return satisfiedByNetworkCapabilities(nc, false);
1067 }
1068
1069 /**
1070 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1071 *
1072 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1073 *
1074 * @hide
1075 */
1076 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1077 return satisfiedByNetworkCapabilities(nc, true);
1078 }
1079
1080 /**
1081 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001082 * {@code NetworkCapabilities} and return a String describing any difference.
1083 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001084 *
1085 * @hide
1086 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001087 public String describeImmutableDifferences(NetworkCapabilities that) {
1088 if (that == null) {
1089 return "other NetworkCapabilities was null";
1090 }
1091
1092 StringJoiner joiner = new StringJoiner(", ");
1093
Hugo Benichieae7a222017-07-25 11:40:56 +09001094 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1095 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001096 // Ignore DUN being added or removed. http://b/65257223.
1097 final long mask = ~MUTABLE_CAPABILITIES
1098 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001099 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1100 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1101 if (oldImmutableCapabilities != newImmutableCapabilities) {
1102 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1103 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1104 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1105 }
1106
1107 if (!equalsSpecifier(that)) {
1108 NetworkSpecifier before = this.getNetworkSpecifier();
1109 NetworkSpecifier after = that.getNetworkSpecifier();
1110 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1111 }
1112
1113 if (!equalsTransportTypes(that)) {
1114 String before = transportNamesOf(this.getTransportTypes());
1115 String after = transportNamesOf(that.getTransportTypes());
1116 joiner.add(String.format("transports changed: %s -> %s", before, after));
1117 }
1118
1119 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001120 }
1121
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001122 /**
1123 * Checks that our requestable capabilities are the same as those of the given
1124 * {@code NetworkCapabilities}.
1125 *
1126 * @hide
1127 */
1128 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1129 if (nc == null) return false;
1130 return (equalsNetCapabilitiesRequestable(nc) &&
1131 equalsTransportTypes(nc) &&
1132 equalsSpecifier(nc));
1133 }
1134
Robert Greenwalt1448f052014-04-08 13:41:39 -07001135 @Override
1136 public boolean equals(Object obj) {
1137 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001138 NetworkCapabilities that = (NetworkCapabilities) obj;
1139 return (equalsNetCapabilities(that)
1140 && equalsTransportTypes(that)
1141 && equalsLinkBandwidths(that)
1142 && equalsSignalStrength(that)
1143 && equalsSpecifier(that)
1144 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001145 }
1146
1147 @Override
1148 public int hashCode() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001149 return ((int) (mNetworkCapabilities & 0xFFFFFFFF)
1150 + ((int) (mNetworkCapabilities >> 32) * 3)
1151 + ((int) (mTransportTypes & 0xFFFFFFFF) * 5)
1152 + ((int) (mTransportTypes >> 32) * 7)
1153 + (mLinkUpBandwidthKbps * 11)
1154 + (mLinkDownBandwidthKbps * 13)
1155 + Objects.hashCode(mNetworkSpecifier) * 17
1156 + (mSignalStrength * 19)
1157 + Objects.hashCode(mUids) * 23);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001158 }
1159
Wink Saville4e2dea72014-09-20 11:04:03 -07001160 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001161 public int describeContents() {
1162 return 0;
1163 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001164 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001165 public void writeToParcel(Parcel dest, int flags) {
1166 dest.writeLong(mNetworkCapabilities);
1167 dest.writeLong(mTransportTypes);
1168 dest.writeInt(mLinkUpBandwidthKbps);
1169 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001170 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001171 dest.writeInt(mSignalStrength);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001172 dest.writeArraySet(new ArraySet<>(mUids));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001173 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001174
Robert Greenwalt1448f052014-04-08 13:41:39 -07001175 public static final Creator<NetworkCapabilities> CREATOR =
1176 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001177 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001178 public NetworkCapabilities createFromParcel(Parcel in) {
1179 NetworkCapabilities netCap = new NetworkCapabilities();
1180
1181 netCap.mNetworkCapabilities = in.readLong();
1182 netCap.mTransportTypes = in.readLong();
1183 netCap.mLinkUpBandwidthKbps = in.readInt();
1184 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001185 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001186 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001187 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1188 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001189 return netCap;
1190 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001191 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001192 public NetworkCapabilities[] newArray(int size) {
1193 return new NetworkCapabilities[size];
1194 }
1195 };
1196
Wink Saville4e2dea72014-09-20 11:04:03 -07001197 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001198 public String toString() {
Hugo Benichieae7a222017-07-25 11:40:56 +09001199 // TODO: enumerate bits for transports and capabilities instead of creating arrays.
1200 // TODO: use a StringBuilder instead of string concatenation.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001201 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +09001202 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001203
Robert Greenwalt7569f182014-06-08 16:42:59 -07001204 types = getCapabilities();
1205 String capabilities = (types.length > 0 ? " Capabilities: " : "");
1206 for (int i = 0; i < types.length; ) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001207 capabilities += capabilityNameOf(types[i]);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001208 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001209 }
1210
1211 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
1212 mLinkUpBandwidthKbps + "Kbps" : "");
1213 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
1214 mLinkDownBandwidthKbps + "Kbps" : "");
1215
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001216 String specifier = (mNetworkSpecifier == null ?
1217 "" : " Specifier: <" + mNetworkSpecifier + ">");
1218
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001219 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
1220
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001221 String uids = (null != mUids ? " Uids: <" + mUids + ">" : "");
1222
Chalard Jeanf474fc32018-01-17 15:10:05 +09001223 String establishingAppUid = " EstablishingAppUid: " + mEstablishingVpnAppUid;
1224
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001225 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength
Chalard Jeanf474fc32018-01-17 15:10:05 +09001226 + uids + establishingAppUid + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001227 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001228
1229 /**
1230 * @hide
1231 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001232 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001233 StringJoiner joiner = new StringJoiner("|");
1234 if (capabilities != null) {
1235 for (int c : capabilities) {
1236 joiner.add(capabilityNameOf(c));
1237 }
1238 }
1239 return joiner.toString();
1240 }
1241
1242 /**
1243 * @hide
1244 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001245 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001246 switch (capability) {
1247 case NET_CAPABILITY_MMS: return "MMS";
1248 case NET_CAPABILITY_SUPL: return "SUPL";
1249 case NET_CAPABILITY_DUN: return "DUN";
1250 case NET_CAPABILITY_FOTA: return "FOTA";
1251 case NET_CAPABILITY_IMS: return "IMS";
1252 case NET_CAPABILITY_CBS: return "CBS";
1253 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1254 case NET_CAPABILITY_IA: return "IA";
1255 case NET_CAPABILITY_RCS: return "RCS";
1256 case NET_CAPABILITY_XCAP: return "XCAP";
1257 case NET_CAPABILITY_EIMS: return "EIMS";
1258 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1259 case NET_CAPABILITY_INTERNET: return "INTERNET";
1260 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1261 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1262 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1263 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1264 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001265 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001266 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001267 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Hugo Benichieae7a222017-07-25 11:40:56 +09001268 default: return Integer.toString(capability);
1269 }
1270 }
1271
1272 /**
1273 * @hide
1274 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001275 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001276 StringJoiner joiner = new StringJoiner("|");
1277 if (types != null) {
1278 for (int t : types) {
1279 joiner.add(transportNameOf(t));
1280 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001281 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001282 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001283 }
1284
1285 /**
1286 * @hide
1287 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001288 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001289 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001290 return "UNKNOWN";
1291 }
1292 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001293 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001294
Jeff Sharkeyde570312017-10-24 21:25:50 -06001295 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001296 Preconditions.checkArgument(
1297 isValidTransport(transport), "Invalid TransportType " + transport);
1298 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001299}