blob: bc4d9555c9e7f666d6cbcf895ebb52d3233cf395 [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,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900119 NET_CAPABILITY_NOT_SUSPENDED,
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
Robert Greenwalt1448f052014-04-08 13:41:39 -0700267 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Chalard Jean804b8fb2018-01-30 22:41:41 +0900268 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_SUSPENDED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700269
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700270 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900271 * Network capabilities that are expected to be mutable, i.e., can change while a particular
272 * network is connected.
273 */
274 private static final long MUTABLE_CAPABILITIES =
275 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
276 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900277 (1 << NET_CAPABILITY_TRUSTED)
278 | (1 << NET_CAPABILITY_VALIDATED)
279 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
280 | (1 << NET_CAPABILITY_NOT_ROAMING)
281 | (1 << NET_CAPABILITY_FOREGROUND)
282 | (1 << NET_CAPABILITY_NOT_CONGESTED)
283 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900284
285 /**
286 * Network capabilities that are not allowed in NetworkRequests. This exists because the
287 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
288 * capability's presence cannot be known in advance. If such a capability is requested, then we
289 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
290 * get immediately torn down because they do not have the requested capability.
291 */
292 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900293 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900294
295 /**
296 * Capabilities that are set by default when the object is constructed.
297 */
298 private static final long DEFAULT_CAPABILITIES =
299 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
300 (1 << NET_CAPABILITY_TRUSTED) |
301 (1 << NET_CAPABILITY_NOT_VPN);
302
303 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400304 * Capabilities that suggest that a network is restricted.
305 * {@see #maybeMarkCapabilitiesRestricted}.
306 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700307 @VisibleForTesting
308 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400309 (1 << NET_CAPABILITY_CBS) |
310 (1 << NET_CAPABILITY_DUN) |
311 (1 << NET_CAPABILITY_EIMS) |
312 (1 << NET_CAPABILITY_FOTA) |
313 (1 << NET_CAPABILITY_IA) |
314 (1 << NET_CAPABILITY_IMS) |
315 (1 << NET_CAPABILITY_RCS) |
316 (1 << NET_CAPABILITY_XCAP);
317
318 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700319 * Capabilities that suggest that a network is unrestricted.
320 * {@see #maybeMarkCapabilitiesRestricted}.
321 */
322 @VisibleForTesting
323 /* package */ static final long UNRESTRICTED_CAPABILITIES =
324 (1 << NET_CAPABILITY_INTERNET) |
325 (1 << NET_CAPABILITY_MMS) |
326 (1 << NET_CAPABILITY_SUPL) |
327 (1 << NET_CAPABILITY_WIFI_P2P);
328
329 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700330 * Adds the given capability to this {@code NetworkCapability} instance.
331 * Multiple capabilities may be applied sequentially. Note that when searching
332 * for a network to satisfy a request, all capabilities requested must be satisfied.
333 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600334 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900335 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700336 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700337 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600338 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700339 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700340 throw new IllegalArgumentException("NetworkCapability out of range");
341 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700342 mNetworkCapabilities |= 1 << capability;
343 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700344 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700345
346 /**
347 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
348 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600349 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900350 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700351 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700352 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600353 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700354 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700355 throw new IllegalArgumentException("NetworkCapability out of range");
356 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700357 mNetworkCapabilities &= ~(1 << capability);
358 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700359 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700360
361 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600362 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
363 * instance.
364 *
365 * @hide
366 */
367 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
368 if (value) {
369 addCapability(capability);
370 } else {
371 removeCapability(capability);
372 }
373 return this;
374 }
375
376 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700377 * Gets all the capabilities set on this {@code NetworkCapability} instance.
378 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600379 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700380 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700381 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600382 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900383 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700384 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700385
386 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600387 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700388 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600389 *
390 * @hide
391 */
392 public void setCapabilities(@NetCapability int[] capabilities) {
393 mNetworkCapabilities = BitUtils.packBits(capabilities);
394 }
395
396 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700397 * Tests for the presence of a capabilitity on this instance.
398 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600399 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700400 * @return {@code true} if set on this instance.
401 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600402 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700403 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700404 return false;
405 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700406 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700407 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700408
Robert Greenwalt1448f052014-04-08 13:41:39 -0700409 private void combineNetCapabilities(NetworkCapabilities nc) {
410 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
411 }
412
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900413 /**
414 * Convenience function that returns a human-readable description of the first mutable
415 * capability we find. Used to present an error message to apps that request mutable
416 * capabilities.
417 *
418 * @hide
419 */
420 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900421 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
422 if (nonRequestable != 0) {
423 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900424 }
425 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900426 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900427 return null;
428 }
429
430 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
431 long networkCapabilities = this.mNetworkCapabilities;
432 if (onlyImmutable) {
433 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
434 }
435 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700436 }
437
Robert Greenwalt06314e42014-10-29 14:04:06 -0700438 /** @hide */
439 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700440 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
441 }
442
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900443 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
444 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
445 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
446 }
447
Robert Greenwalt1448f052014-04-08 13:41:39 -0700448 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400449 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
450 * typically provided by restricted networks.
451 *
452 * TODO: consider:
453 * - Renaming it to guessRestrictedCapability and make it set the
454 * restricted capability bit in addition to clearing it.
455 * @hide
456 */
457 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700458 // Verify there aren't any unrestricted capabilities. If there are we say
459 // the whole thing is unrestricted.
460 final boolean hasUnrestrictedCapabilities =
461 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
462
463 // Must have at least some restricted capabilities.
464 final boolean hasRestrictedCapabilities =
465 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
466
467 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400468 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400469 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400470 }
471
472 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700473 * Representing the transport type. Apps should generally not care about transport. A
474 * request for a fast internet connection could be satisfied by a number of different
475 * transports. If any are specified here it will be satisfied a Network that matches
476 * any of them. If a caller doesn't care about the transport it should not specify any.
477 */
478 private long mTransportTypes;
479
Jeff Sharkeyde570312017-10-24 21:25:50 -0600480 /** @hide */
481 @Retention(RetentionPolicy.SOURCE)
482 @IntDef(prefix = { "TRANSPORT_" }, value = {
483 TRANSPORT_CELLULAR,
484 TRANSPORT_WIFI,
485 TRANSPORT_BLUETOOTH,
486 TRANSPORT_ETHERNET,
487 TRANSPORT_VPN,
488 TRANSPORT_WIFI_AWARE,
489 TRANSPORT_LOWPAN,
490 })
491 public @interface Transport { }
492
Robert Greenwalt1448f052014-04-08 13:41:39 -0700493 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700494 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700495 */
496 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700497
498 /**
499 * Indicates this network uses a Wi-Fi transport.
500 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700501 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700502
503 /**
504 * Indicates this network uses a Bluetooth transport.
505 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700506 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700507
508 /**
509 * Indicates this network uses an Ethernet transport.
510 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 public static final int TRANSPORT_ETHERNET = 3;
512
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400513 /**
514 * Indicates this network uses a VPN transport.
515 */
516 public static final int TRANSPORT_VPN = 4;
517
Etan Cohen305ea282016-06-20 09:27:12 -0700518 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700519 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700520 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700521 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700522
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700523 /**
524 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700525 */
526 public static final int TRANSPORT_LOWPAN = 6;
527
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900528 /** @hide */
529 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
530 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700531 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700532
Hugo Benichi16f0a942017-06-20 14:07:59 +0900533 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600534 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900535 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
536 }
537
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900538 private static final String[] TRANSPORT_NAMES = {
539 "CELLULAR",
540 "WIFI",
541 "BLUETOOTH",
542 "ETHERNET",
543 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700544 "WIFI_AWARE",
545 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900546 };
547
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700548 /**
549 * Adds the given transport type to this {@code NetworkCapability} instance.
550 * Multiple transports may be applied sequentially. Note that when searching
551 * for a network to satisfy a request, any listed in the request will satisfy the request.
552 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
553 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
554 * to be selected. This is logically different than
555 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
556 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600557 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900558 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700559 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700560 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600561 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900562 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700563 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700564 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700565 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700566 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700567
568 /**
569 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
570 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600571 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900572 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700573 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700574 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600575 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900576 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700578 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700579 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700580 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700581
582 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600583 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
584 * instance.
585 *
586 * @hide
587 */
588 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
589 if (value) {
590 addTransportType(transportType);
591 } else {
592 removeTransportType(transportType);
593 }
594 return this;
595 }
596
597 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700598 * Gets all the transports set on this {@code NetworkCapability} instance.
599 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600600 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700601 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700602 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600603 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900604 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700605 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700606
607 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600608 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700609 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600610 *
611 * @hide
612 */
613 public void setTransportTypes(@Transport int[] transportTypes) {
614 mTransportTypes = BitUtils.packBits(transportTypes);
615 }
616
617 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700618 * Tests for the presence of a transport on this instance.
619 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600620 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700621 * @return {@code true} if set on this instance.
622 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600623 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900624 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700625 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700626
627 private void combineTransportTypes(NetworkCapabilities nc) {
628 this.mTransportTypes |= nc.mTransportTypes;
629 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900630
Robert Greenwalt1448f052014-04-08 13:41:39 -0700631 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
632 return ((this.mTransportTypes == 0) ||
633 ((this.mTransportTypes & nc.mTransportTypes) != 0));
634 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900635
Robert Greenwalt06314e42014-10-29 14:04:06 -0700636 /** @hide */
637 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700638 return (nc.mTransportTypes == this.mTransportTypes);
639 }
640
641 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900642 * UID of the app that manages this network, or INVALID_UID if none/unknown.
643 *
644 * This field keeps track of the UID of the app that created this network and is in charge
645 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
646 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
647 * to create networks.
648 *
649 * Because this field is only used in the services side (and to avoid apps being able to
650 * set this to whatever they want), this field is not parcelled and will not be conserved
651 * across the IPC boundary.
652 * @hide
653 */
654 private int mEstablishingVpnAppUid = INVALID_UID;
655
656 /**
657 * Set the UID of the managing app.
658 * @hide
659 */
660 public void setEstablishingVpnAppUid(final int uid) {
661 mEstablishingVpnAppUid = uid;
662 }
663
664 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600665 * Value indicating that link bandwidth is unspecified.
666 * @hide
667 */
668 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
669
670 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700671 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
672 * for the first hop on the given transport. It is not measured, but may take into account
673 * link parameters (Radio technology, allocated channels, etc).
674 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600675 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
676 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700677
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700678 /**
679 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
680 * the estimated first hop transport bandwidth.
681 * <p>
682 * Note that when used to request a network, this specifies the minimum acceptable.
683 * When received as the state of an existing network this specifies the typical
684 * first hop bandwidth expected. This is never measured, but rather is inferred
685 * from technology type and other link parameters. It could be used to differentiate
686 * between very slow 1xRTT cellular links and other faster networks or even between
687 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
688 * fast backhauls and slow backhauls.
689 *
690 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700691 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700692 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600693 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700694 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600695 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700696 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700697
698 /**
699 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
700 * the estimated first hop transport bandwidth.
701 *
702 * @return The estimated first hop upstream (device to network) bandwidth.
703 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700704 public int getLinkUpstreamBandwidthKbps() {
705 return mLinkUpBandwidthKbps;
706 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700707
708 /**
709 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
710 * the estimated first hop transport bandwidth.
711 * <p>
712 * Note that when used to request a network, this specifies the minimum acceptable.
713 * When received as the state of an existing network this specifies the typical
714 * first hop bandwidth expected. This is never measured, but rather is inferred
715 * from technology type and other link parameters. It could be used to differentiate
716 * between very slow 1xRTT cellular links and other faster networks or even between
717 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
718 * fast backhauls and slow backhauls.
719 *
720 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700721 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700722 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600723 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700724 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600725 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700726 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700727
728 /**
729 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
730 * the estimated first hop transport bandwidth.
731 *
732 * @return The estimated first hop downstream (network to device) bandwidth.
733 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700734 public int getLinkDownstreamBandwidthKbps() {
735 return mLinkDownBandwidthKbps;
736 }
737
738 private void combineLinkBandwidths(NetworkCapabilities nc) {
739 this.mLinkUpBandwidthKbps =
740 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
741 this.mLinkDownBandwidthKbps =
742 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
743 }
744 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
745 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
746 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
747 }
748 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
749 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
750 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
751 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600752 /** @hide */
753 public static int minBandwidth(int a, int b) {
754 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
755 return b;
756 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
757 return a;
758 } else {
759 return Math.min(a, b);
760 }
761 }
762 /** @hide */
763 public static int maxBandwidth(int a, int b) {
764 return Math.max(a, b);
765 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700766
Etan Cohena7434272017-04-03 12:17:51 -0700767 private NetworkSpecifier mNetworkSpecifier = null;
768
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700769 /**
770 * Sets the optional bearer specific network specifier.
771 * This has no meaning if a single transport is also not specified, so calling
772 * this without a single transport set will generate an exception, as will
773 * subsequently adding or removing transports after this is set.
774 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700775 *
Etan Cohena7434272017-04-03 12:17:51 -0700776 * @param networkSpecifier A concrete, parcelable framework class that extends
777 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900778 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700779 * @hide
780 */
Etan Cohena7434272017-04-03 12:17:51 -0700781 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
782 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700783 throw new IllegalStateException("Must have a single transport specified to use " +
784 "setNetworkSpecifier");
785 }
Etan Cohena7434272017-04-03 12:17:51 -0700786
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700787 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700788
Pierre Imaic8419a82016-03-22 17:54:54 +0900789 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700790 }
791
792 /**
793 * Gets the optional bearer specific network specifier.
794 *
Etan Cohena7434272017-04-03 12:17:51 -0700795 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
796 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700797 * @hide
798 */
Etan Cohena7434272017-04-03 12:17:51 -0700799 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700800 return mNetworkSpecifier;
801 }
802
803 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700804 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700805 throw new IllegalStateException("Can't combine two networkSpecifiers");
806 }
Etan Cohena7434272017-04-03 12:17:51 -0700807 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700808 }
Etan Cohena7434272017-04-03 12:17:51 -0700809
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700810 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700811 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
812 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700813 }
Etan Cohena7434272017-04-03 12:17:51 -0700814
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700815 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700816 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700817 }
818
Robert Greenwalt1448f052014-04-08 13:41:39 -0700819 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900820 * Magic value that indicates no signal strength provided. A request specifying this value is
821 * always satisfied.
822 *
823 * @hide
824 */
825 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
826
827 /**
828 * Signal strength. This is a signed integer, and higher values indicate better signal.
829 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
830 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700831 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900832
833 /**
834 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
835 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
836 * reported by WifiManager.
837 * <p>
838 * Note that when used to register a network callback, this specifies the minimum acceptable
839 * signal strength. When received as the state of an existing network it specifies the current
840 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
841 * effect when requesting a callback.
842 *
843 * @param signalStrength the bearer-specific signal strength.
844 * @hide
845 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600846 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900847 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600848 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900849 }
850
851 /**
852 * Returns {@code true} if this object specifies a signal strength.
853 *
854 * @hide
855 */
856 public boolean hasSignalStrength() {
857 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
858 }
859
860 /**
861 * Retrieves the signal strength.
862 *
863 * @return The bearer-specific signal strength.
864 * @hide
865 */
866 public int getSignalStrength() {
867 return mSignalStrength;
868 }
869
870 private void combineSignalStrength(NetworkCapabilities nc) {
871 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
872 }
873
874 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
875 return this.mSignalStrength <= nc.mSignalStrength;
876 }
877
878 private boolean equalsSignalStrength(NetworkCapabilities nc) {
879 return this.mSignalStrength == nc.mSignalStrength;
880 }
881
882 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900883 * List of UIDs this network applies to. No restriction if null.
884 * <p>
885 * This is typically (and at this time, only) used by VPN. This network is only available to
886 * the UIDs in this list, and it is their default network. Apps in this list that wish to
887 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
888 * member is null, then the network is not restricted by app UID. If it's an empty list, then
889 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900890 * As a special exception, the app managing this network (as identified by its UID stored in
891 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
892 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
893 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900894 * <p>
895 * Please note that in principle a single app can be associated with multiple UIDs because
896 * each app will have a different UID when it's run as a different (macro-)user. A single
897 * macro user can only have a single active VPN app at any given time however.
898 * <p>
899 * Also please be aware this class does not try to enforce any normalization on this. Callers
900 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
901 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
902 * their own (like requiring sortedness or no overlap) they need to enforce it
903 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
904 * or overlapping ranges are present.
905 *
906 * @hide
907 */
Chalard Jeancf8c3fe2018-01-25 09:41:51 +0900908 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900909
910 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900911 * Convenience method to set the UIDs this network applies to to a single UID.
912 * @hide
913 */
914 public NetworkCapabilities setSingleUid(int uid) {
915 final ArraySet<UidRange> identity = new ArraySet<>(1);
916 identity.add(new UidRange(uid, uid));
917 setUids(identity);
918 return this;
919 }
920
921 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900922 * Set the list of UIDs this network applies to.
923 * This makes a copy of the set so that callers can't modify it after the call.
924 * @hide
925 */
926 public NetworkCapabilities setUids(Set<UidRange> uids) {
927 if (null == uids) {
928 mUids = null;
929 } else {
930 mUids = new ArraySet<>(uids);
931 }
932 return this;
933 }
934
935 /**
936 * Get the list of UIDs this network applies to.
937 * This returns a copy of the set so that callers can't modify the original object.
938 * @hide
939 */
940 public Set<UidRange> getUids() {
941 return null == mUids ? null : new ArraySet<>(mUids);
942 }
943
944 /**
945 * Test whether this network applies to this UID.
946 * @hide
947 */
948 public boolean appliesToUid(int uid) {
949 if (null == mUids) return true;
950 for (UidRange range : mUids) {
951 if (range.contains(uid)) {
952 return true;
953 }
954 }
955 return false;
956 }
957
958 /**
959 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
960 * <p>
961 * This test only checks whether equal range objects are in both sets. It will
962 * return false if the ranges are not exactly the same, even if the covered UIDs
963 * are for an equivalent result.
964 * <p>
965 * Note that this method is not very optimized, which is fine as long as it's not used very
966 * often.
967 * <p>
968 * nc is assumed nonnull.
969 *
970 * @hide
971 */
972 @VisibleForTesting
973 public boolean equalsUids(NetworkCapabilities nc) {
974 Set<UidRange> comparedUids = nc.mUids;
975 if (null == comparedUids) return null == mUids;
976 if (null == mUids) return false;
977 // Make a copy so it can be mutated to check that all ranges in mUids
978 // also are in uids.
979 final Set<UidRange> uids = new ArraySet<>(mUids);
980 for (UidRange range : comparedUids) {
981 if (!uids.contains(range)) {
982 return false;
983 }
984 uids.remove(range);
985 }
986 return uids.isEmpty();
987 }
988
989 /**
990 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
991 *
Chalard Jeanf474fc32018-01-17 15:10:05 +0900992 * This method is called on the NetworkCapabilities embedded in a request with the
993 * capabilities of an available network. It checks whether all the UIDs from this listen
994 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
995 * in the passed nc (representing the UIDs that this network is available to).
996 * <p>
997 * As a special exception, the UID that created the passed network (as represented by its
998 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
999 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1000 * can see its own network when it listens for it.
1001 * <p>
1002 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001003 * @see #appliesToUid
1004 * @hide
1005 */
1006 public boolean satisfiedByUids(NetworkCapabilities nc) {
1007 if (null == nc.mUids) return true; // The network satisfies everything.
1008 if (null == mUids) return false; // Not everything allowed but requires everything
1009 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001010 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001011 if (!nc.appliesToUidRange(requiredRange)) {
1012 return false;
1013 }
1014 }
1015 return true;
1016 }
1017
1018 /**
1019 * Returns whether this network applies to the passed ranges.
1020 * This assumes that to apply, the passed range has to be entirely contained
1021 * within one of the ranges this network applies to. If the ranges are not normalized,
1022 * this method may return false even though all required UIDs are covered because no
1023 * single range contained them all.
1024 * @hide
1025 */
1026 @VisibleForTesting
1027 public boolean appliesToUidRange(UidRange requiredRange) {
1028 if (null == mUids) return true;
1029 for (UidRange uidRange : mUids) {
1030 if (uidRange.containsRange(requiredRange)) {
1031 return true;
1032 }
1033 }
1034 return false;
1035 }
1036
1037 /**
1038 * Combine the UIDs this network currently applies to with the UIDs the passed
1039 * NetworkCapabilities apply to.
1040 * nc is assumed nonnull.
1041 */
1042 private void combineUids(NetworkCapabilities nc) {
1043 if (null == nc.mUids || null == mUids) {
1044 mUids = null;
1045 return;
1046 }
1047 mUids.addAll(nc.mUids);
1048 }
1049
1050 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001051 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001052 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001053 */
1054 public void combineCapabilities(NetworkCapabilities nc) {
1055 combineNetCapabilities(nc);
1056 combineTransportTypes(nc);
1057 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001058 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001059 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001060 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001061 }
1062
1063 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001064 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1065 *
1066 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1067 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1068 * bandwidth, signal strength, or validation / captive portal status.
1069 *
1070 * @hide
1071 */
1072 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001073 return (nc != null
1074 && satisfiedByNetCapabilities(nc, onlyImmutable)
1075 && satisfiedByTransportTypes(nc)
1076 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1077 && satisfiedBySpecifier(nc)
1078 && (onlyImmutable || satisfiedBySignalStrength(nc))
1079 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001080 }
1081
1082 /**
1083 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1084 *
1085 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1086 *
1087 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001088 */
1089 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001090 return satisfiedByNetworkCapabilities(nc, false);
1091 }
1092
1093 /**
1094 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1095 *
1096 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1097 *
1098 * @hide
1099 */
1100 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1101 return satisfiedByNetworkCapabilities(nc, true);
1102 }
1103
1104 /**
1105 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001106 * {@code NetworkCapabilities} and return a String describing any difference.
1107 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001108 *
1109 * @hide
1110 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001111 public String describeImmutableDifferences(NetworkCapabilities that) {
1112 if (that == null) {
1113 return "other NetworkCapabilities was null";
1114 }
1115
1116 StringJoiner joiner = new StringJoiner(", ");
1117
Hugo Benichieae7a222017-07-25 11:40:56 +09001118 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1119 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001120 // Ignore DUN being added or removed. http://b/65257223.
1121 final long mask = ~MUTABLE_CAPABILITIES
1122 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001123 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1124 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1125 if (oldImmutableCapabilities != newImmutableCapabilities) {
1126 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1127 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1128 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1129 }
1130
1131 if (!equalsSpecifier(that)) {
1132 NetworkSpecifier before = this.getNetworkSpecifier();
1133 NetworkSpecifier after = that.getNetworkSpecifier();
1134 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1135 }
1136
1137 if (!equalsTransportTypes(that)) {
1138 String before = transportNamesOf(this.getTransportTypes());
1139 String after = transportNamesOf(that.getTransportTypes());
1140 joiner.add(String.format("transports changed: %s -> %s", before, after));
1141 }
1142
1143 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001144 }
1145
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001146 /**
1147 * Checks that our requestable capabilities are the same as those of the given
1148 * {@code NetworkCapabilities}.
1149 *
1150 * @hide
1151 */
1152 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1153 if (nc == null) return false;
1154 return (equalsNetCapabilitiesRequestable(nc) &&
1155 equalsTransportTypes(nc) &&
1156 equalsSpecifier(nc));
1157 }
1158
Robert Greenwalt1448f052014-04-08 13:41:39 -07001159 @Override
1160 public boolean equals(Object obj) {
1161 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001162 NetworkCapabilities that = (NetworkCapabilities) obj;
1163 return (equalsNetCapabilities(that)
1164 && equalsTransportTypes(that)
1165 && equalsLinkBandwidths(that)
1166 && equalsSignalStrength(that)
1167 && equalsSpecifier(that)
1168 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001169 }
1170
1171 @Override
1172 public int hashCode() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001173 return ((int) (mNetworkCapabilities & 0xFFFFFFFF)
1174 + ((int) (mNetworkCapabilities >> 32) * 3)
1175 + ((int) (mTransportTypes & 0xFFFFFFFF) * 5)
1176 + ((int) (mTransportTypes >> 32) * 7)
1177 + (mLinkUpBandwidthKbps * 11)
1178 + (mLinkDownBandwidthKbps * 13)
1179 + Objects.hashCode(mNetworkSpecifier) * 17
1180 + (mSignalStrength * 19)
1181 + Objects.hashCode(mUids) * 23);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001182 }
1183
Wink Saville4e2dea72014-09-20 11:04:03 -07001184 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001185 public int describeContents() {
1186 return 0;
1187 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001188 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001189 public void writeToParcel(Parcel dest, int flags) {
1190 dest.writeLong(mNetworkCapabilities);
1191 dest.writeLong(mTransportTypes);
1192 dest.writeInt(mLinkUpBandwidthKbps);
1193 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001194 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001195 dest.writeInt(mSignalStrength);
Chalard Jeancf8c3fe2018-01-25 09:41:51 +09001196 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001197 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001198
Robert Greenwalt1448f052014-04-08 13:41:39 -07001199 public static final Creator<NetworkCapabilities> CREATOR =
1200 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001201 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001202 public NetworkCapabilities createFromParcel(Parcel in) {
1203 NetworkCapabilities netCap = new NetworkCapabilities();
1204
1205 netCap.mNetworkCapabilities = in.readLong();
1206 netCap.mTransportTypes = in.readLong();
1207 netCap.mLinkUpBandwidthKbps = in.readInt();
1208 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001209 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001210 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001211 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1212 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001213 return netCap;
1214 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001215 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001216 public NetworkCapabilities[] newArray(int size) {
1217 return new NetworkCapabilities[size];
1218 }
1219 };
1220
Wink Saville4e2dea72014-09-20 11:04:03 -07001221 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001222 public String toString() {
Hugo Benichieae7a222017-07-25 11:40:56 +09001223 // TODO: enumerate bits for transports and capabilities instead of creating arrays.
1224 // TODO: use a StringBuilder instead of string concatenation.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001225 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +09001226 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001227
Robert Greenwalt7569f182014-06-08 16:42:59 -07001228 types = getCapabilities();
1229 String capabilities = (types.length > 0 ? " Capabilities: " : "");
1230 for (int i = 0; i < types.length; ) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001231 capabilities += capabilityNameOf(types[i]);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001232 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001233 }
1234
1235 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
1236 mLinkUpBandwidthKbps + "Kbps" : "");
1237 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
1238 mLinkDownBandwidthKbps + "Kbps" : "");
1239
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001240 String specifier = (mNetworkSpecifier == null ?
1241 "" : " Specifier: <" + mNetworkSpecifier + ">");
1242
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001243 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
1244
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001245 String uids = (null != mUids ? " Uids: <" + mUids + ">" : "");
1246
Chalard Jeanf474fc32018-01-17 15:10:05 +09001247 String establishingAppUid = " EstablishingAppUid: " + mEstablishingVpnAppUid;
1248
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001249 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength
Chalard Jeanf474fc32018-01-17 15:10:05 +09001250 + uids + establishingAppUid + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001251 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001252
1253 /**
1254 * @hide
1255 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001256 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001257 StringJoiner joiner = new StringJoiner("|");
1258 if (capabilities != null) {
1259 for (int c : capabilities) {
1260 joiner.add(capabilityNameOf(c));
1261 }
1262 }
1263 return joiner.toString();
1264 }
1265
1266 /**
1267 * @hide
1268 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001269 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001270 switch (capability) {
1271 case NET_CAPABILITY_MMS: return "MMS";
1272 case NET_CAPABILITY_SUPL: return "SUPL";
1273 case NET_CAPABILITY_DUN: return "DUN";
1274 case NET_CAPABILITY_FOTA: return "FOTA";
1275 case NET_CAPABILITY_IMS: return "IMS";
1276 case NET_CAPABILITY_CBS: return "CBS";
1277 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1278 case NET_CAPABILITY_IA: return "IA";
1279 case NET_CAPABILITY_RCS: return "RCS";
1280 case NET_CAPABILITY_XCAP: return "XCAP";
1281 case NET_CAPABILITY_EIMS: return "EIMS";
1282 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1283 case NET_CAPABILITY_INTERNET: return "INTERNET";
1284 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1285 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1286 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1287 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1288 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001289 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001290 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001291 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001292 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Hugo Benichieae7a222017-07-25 11:40:56 +09001293 default: return Integer.toString(capability);
1294 }
1295 }
1296
1297 /**
1298 * @hide
1299 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001300 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001301 StringJoiner joiner = new StringJoiner("|");
1302 if (types != null) {
1303 for (int t : types) {
1304 joiner.add(transportNameOf(t));
1305 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001306 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001307 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001308 }
1309
1310 /**
1311 * @hide
1312 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001313 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001314 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001315 return "UNKNOWN";
1316 }
1317 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001318 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001319
Jeff Sharkeyde570312017-10-24 21:25:50 -06001320 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001321 Preconditions.checkArgument(
1322 isValidTransport(transport), "Invalid TransportType " + transport);
1323 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001324}