blob: 99375f842b4cd90efa707172f4c9d6d1e2b6d9d8 [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;
paulhud9736de2019-03-08 16:35:20 +080020import android.annotation.NonNull;
Etan Cohenca9fb562018-11-27 07:32:39 -080021import android.annotation.Nullable;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070022import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060023import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010024import android.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060025import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010026import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070027import android.os.Parcel;
28import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090029import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080030import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070031
32import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090033import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090034import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070035
Jeff Sharkeyde570312017-10-24 21:25:50 -060036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070038import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090039import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090040import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070041
42/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070043 * Representation of the capabilities of an active network. Instances are
44 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060045 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
46 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060047 * <p>
48 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
49 * network selection. Rather than indicate a need for Wi-Fi because an
50 * application needs high bandwidth and risk obsolescence when a new, fast
51 * network appears (like LTE), the application should specify it needs high
52 * bandwidth. Similarly if an application needs an unmetered network for a bulk
53 * transfer it can specify that rather than assuming all cellular based
54 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070055 */
56public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070057 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090058 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070059
Robert Greenwalt7569f182014-06-08 16:42:59 -070060 /**
61 * @hide
62 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010063 @UnsupportedAppUsage
Robert Greenwalt01d004e2014-05-18 15:24:21 -070064 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090065 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090066 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070067 }
68
69 public NetworkCapabilities(NetworkCapabilities nc) {
70 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090071 set(nc);
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() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080081 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070082 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090083 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080084 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090085 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090086 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090087 mEstablishingVpnAppUid = INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090088 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090089 }
90
91 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090092 * Set all contents of this object to the contents of a NetworkCapabilities.
93 * @hide
94 */
paulhud9736de2019-03-08 16:35:20 +080095 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090096 mNetworkCapabilities = nc.mNetworkCapabilities;
97 mTransportTypes = nc.mTransportTypes;
98 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
99 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
100 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800101 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900102 mSignalStrength = nc.mSignalStrength;
103 setUids(nc.mUids); // Will make the defensive copy
104 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
105 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
106 mSSID = nc.mSSID;
107 }
108
109 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700110 * Represents the network's capabilities. If any are specified they will be satisfied
111 * by any Network that matches all of them.
112 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100113 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900114 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700115
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800116 /**
117 * If any capabilities specified here they must not exist in the matching Network.
118 */
119 private long mUnwantedNetworkCapabilities;
120
Jeff Sharkeyde570312017-10-24 21:25:50 -0600121 /** @hide */
122 @Retention(RetentionPolicy.SOURCE)
123 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
124 NET_CAPABILITY_MMS,
125 NET_CAPABILITY_SUPL,
126 NET_CAPABILITY_DUN,
127 NET_CAPABILITY_FOTA,
128 NET_CAPABILITY_IMS,
129 NET_CAPABILITY_CBS,
130 NET_CAPABILITY_WIFI_P2P,
131 NET_CAPABILITY_IA,
132 NET_CAPABILITY_RCS,
133 NET_CAPABILITY_XCAP,
134 NET_CAPABILITY_EIMS,
135 NET_CAPABILITY_NOT_METERED,
136 NET_CAPABILITY_INTERNET,
137 NET_CAPABILITY_NOT_RESTRICTED,
138 NET_CAPABILITY_TRUSTED,
139 NET_CAPABILITY_NOT_VPN,
140 NET_CAPABILITY_VALIDATED,
141 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600142 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600143 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900144 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900145 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800146 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800147 NET_CAPABILITY_MCX,
148 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600149 })
150 public @interface NetCapability { }
151
Robert Greenwalt1448f052014-04-08 13:41:39 -0700152 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700153 * Indicates this is a network that has the ability to reach the
154 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700155 */
156 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700157
158 /**
159 * Indicates this is a network that has the ability to reach the carrier's
160 * SUPL server, used to retrieve GPS information.
161 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700162 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700163
164 /**
165 * Indicates this is a network that has the ability to reach the carrier's
166 * DUN or tethering gateway.
167 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700168 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700169
170 /**
171 * Indicates this is a network that has the ability to reach the carrier's
172 * FOTA portal, used for over the air updates.
173 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700174 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700175
176 /**
177 * Indicates this is a network that has the ability to reach the carrier's
178 * IMS servers, used for network registration and signaling.
179 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach the carrier's
184 * CBS servers, used for carrier specific services.
185 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Indicates this is a network that has the ability to reach a Wi-Fi direct
190 * peer.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates this is a network that has the ability to reach a carrier's
196 * Initial Attach servers.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates this is a network that has the ability to reach a carrier's
202 * RCS servers, used for Rich Communication Services.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates this is a network that has the ability to reach a carrier's
208 * XCAP servers, used for configuration and control.
209 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700210 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700211
212 /**
213 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700214 * Emergency IMS servers or other services, used for network signaling
215 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700217 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700218
219 /**
220 * Indicates that this network is unmetered.
221 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700222 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700223
224 /**
225 * Indicates that this network should be able to reach the internet.
226 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700227 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700228
229 /**
230 * Indicates that this network is available for general use. If this is not set
231 * applications should not attempt to communicate on this network. Note that this
232 * is simply informative and not enforcement - enforcement is handled via other means.
233 * Set by default.
234 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700235 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
236
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700237 /**
238 * Indicates that the user has indicated implicit trust of this network. This
239 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
240 * BT device or a wifi the user asked to connect to. Untrusted networks
241 * are probably limited to unknown wifi AP. Set by default.
242 */
243 public static final int NET_CAPABILITY_TRUSTED = 14;
244
Paul Jensen76b610a2015-03-18 09:33:07 -0400245 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400246 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400247 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400248 */
249 public static final int NET_CAPABILITY_NOT_VPN = 15;
250
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900251 /**
252 * Indicates that connectivity on this network was successfully validated. For example, for a
253 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
254 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900255 */
256 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700257
Paul Jensen3d194ea2015-06-16 14:27:36 -0400258 /**
259 * Indicates that this network was found to have a captive portal in place last time it was
260 * probed.
261 */
262 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
263
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900264 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600265 * Indicates that this network is not roaming.
266 */
267 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
268
269 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900270 * Indicates that this network is available for use by apps, and not a network that is being
271 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900272 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600273 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900274
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900275 /**
276 * Indicates that this network is not congested.
277 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600278 * When a network is congested, applications should defer network traffic
279 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900280 */
281 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
282
Chalard Jean804b8fb2018-01-30 22:41:41 +0900283 /**
284 * Indicates that this network is not currently suspended.
285 * <p>
286 * When a network is suspended, the network's IP addresses and any connections
287 * established on the network remain valid, but the network is temporarily unable
288 * to transfer data. This can happen, for example, if a cellular network experiences
289 * a temporary loss of signal, such as when driving through a tunnel, etc.
290 * A network with this capability is not suspended, so is expected to be able to
291 * transfer data.
292 */
293 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
294
Pavel Maltsev43403202018-01-30 17:19:44 -0800295 /**
296 * Indicates that traffic that goes through this network is paid by oem. For example,
297 * this network can be used by system apps to upload telemetry data.
298 * @hide
299 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700300 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800301 public static final int NET_CAPABILITY_OEM_PAID = 22;
302
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800303 /**
304 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
305 * servers.
306 */
307 public static final int NET_CAPABILITY_MCX = 23;
308
lucasline252a742019-03-12 13:08:03 +0800309 /**
310 * Indicates that this network was tested to only provide partial connectivity.
311 * @hide
312 */
313 @SystemApi
314 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
315
Robert Greenwalt1448f052014-04-08 13:41:39 -0700316 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800317 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700318
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700319 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900320 * Network capabilities that are expected to be mutable, i.e., can change while a particular
321 * network is connected.
322 */
323 private static final long MUTABLE_CAPABILITIES =
324 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
325 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900326 (1 << NET_CAPABILITY_TRUSTED)
327 | (1 << NET_CAPABILITY_VALIDATED)
328 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
329 | (1 << NET_CAPABILITY_NOT_ROAMING)
330 | (1 << NET_CAPABILITY_FOREGROUND)
331 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800332 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
333 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900334
335 /**
336 * Network capabilities that are not allowed in NetworkRequests. This exists because the
337 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
338 * capability's presence cannot be known in advance. If such a capability is requested, then we
339 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
340 * get immediately torn down because they do not have the requested capability.
341 */
342 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900343 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900344
345 /**
346 * Capabilities that are set by default when the object is constructed.
347 */
348 private static final long DEFAULT_CAPABILITIES =
349 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
350 (1 << NET_CAPABILITY_TRUSTED) |
351 (1 << NET_CAPABILITY_NOT_VPN);
352
353 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400354 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800355 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400356 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700357 @VisibleForTesting
358 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400359 (1 << NET_CAPABILITY_CBS) |
360 (1 << NET_CAPABILITY_DUN) |
361 (1 << NET_CAPABILITY_EIMS) |
362 (1 << NET_CAPABILITY_FOTA) |
363 (1 << NET_CAPABILITY_IA) |
364 (1 << NET_CAPABILITY_IMS) |
365 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800366 (1 << NET_CAPABILITY_XCAP) |
367 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800368
369 /**
370 * Capabilities that force network to be restricted.
371 * {@see #maybeMarkCapabilitiesRestricted}.
372 */
373 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800374 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400375
376 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700377 * Capabilities that suggest that a network is unrestricted.
378 * {@see #maybeMarkCapabilitiesRestricted}.
379 */
380 @VisibleForTesting
381 /* package */ static final long UNRESTRICTED_CAPABILITIES =
382 (1 << NET_CAPABILITY_INTERNET) |
383 (1 << NET_CAPABILITY_MMS) |
384 (1 << NET_CAPABILITY_SUPL) |
385 (1 << NET_CAPABILITY_WIFI_P2P);
386
387 /**
lucasline252a742019-03-12 13:08:03 +0800388 * Capabilities that are managed by ConnectivityService.
389 */
390 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
391 (1 << NET_CAPABILITY_VALIDATED)
392 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
393 | (1 << NET_CAPABILITY_FOREGROUND)
394 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
395
396 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700397 * Adds the given capability to this {@code NetworkCapability} instance.
398 * Multiple capabilities may be applied sequentially. Note that when searching
399 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800400 * <p>
401 * If the given capability was previously added to the list of unwanted capabilities
402 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700403 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600404 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900405 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700406 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700407 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100408 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +0800409 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800410 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700411 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800412 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700413 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700414 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700415
416 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800417 * Adds the given capability to the list of unwanted capabilities of this
418 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
419 * sequentially. Note that when searching for a network to satisfy a request, the network
420 * must not contain any capability from unwanted capability list.
421 * <p>
422 * If the capability was previously added to the list of required capabilities (for
423 * example, it was there by default or added using {@link #addCapability(int)} method), then
424 * it will be removed from the list of required capabilities as well.
425 *
426 * @see #addCapability(int)
427 * @hide
428 */
429 public void addUnwantedCapability(@NetCapability int capability) {
430 checkValidCapability(capability);
431 mUnwantedNetworkCapabilities |= 1 << capability;
432 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
433 }
434
435 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700436 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800437 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800438 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800439 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700440 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600441 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900442 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700443 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700444 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100445 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +0800446 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800447 checkValidCapability(capability);
448 final long mask = ~(1 << capability);
449 mNetworkCapabilities &= mask;
450 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700451 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700452 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700453
454 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600455 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
456 * instance.
457 *
458 * @hide
459 */
paulhud9736de2019-03-08 16:35:20 +0800460 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
461 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600462 if (value) {
463 addCapability(capability);
464 } else {
465 removeCapability(capability);
466 }
467 return this;
468 }
469
470 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700471 * Gets all the capabilities set on this {@code NetworkCapability} instance.
472 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600473 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700474 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700475 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600476 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600477 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900478 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700479 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700480
481 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800482 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
483 *
484 * @return an array of unwanted capability values for this instance.
485 * @hide
486 */
487 public @NetCapability int[] getUnwantedCapabilities() {
488 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
489 }
490
491
492 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600493 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700494 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600495 *
496 * @hide
497 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800498 public void setCapabilities(@NetCapability int[] capabilities,
499 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600500 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800501 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600502 }
503
504 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800505 * @deprecated use {@link #setCapabilities(int[], int[])}
506 * @hide
507 */
508 @Deprecated
509 public void setCapabilities(@NetCapability int[] capabilities) {
510 setCapabilities(capabilities, new int[] {});
511 }
512
513 /**
514 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600516 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700517 * @return {@code true} if set on this instance.
518 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600519 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800520 return isValidCapability(capability)
521 && ((mNetworkCapabilities & (1 << capability)) != 0);
522 }
523
524 /** @hide */
525 public boolean hasUnwantedCapability(@NetCapability int capability) {
526 return isValidCapability(capability)
527 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700528 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700529
lucasline252a742019-03-12 13:08:03 +0800530 /**
531 * Check if this NetworkCapabilities has system managed capabilities or not.
532 * @hide
533 */
534 public boolean hasConnectivityManagedCapability() {
535 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
536 }
537
Pavel Maltseve18ef262018-03-07 11:13:04 -0800538 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800539 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700540 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800541 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700542 }
543
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900544 /**
545 * Convenience function that returns a human-readable description of the first mutable
546 * capability we find. Used to present an error message to apps that request mutable
547 * capabilities.
548 *
549 * @hide
550 */
paulhud9736de2019-03-08 16:35:20 +0800551 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800552 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
553 & NON_REQUESTABLE_CAPABILITIES;
554
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900555 if (nonRequestable != 0) {
556 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900557 }
558 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900559 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900560 return null;
561 }
562
paulhud9736de2019-03-08 16:35:20 +0800563 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
564 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800565 long requestedCapabilities = mNetworkCapabilities;
566 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
567 long providedCapabilities = nc.mNetworkCapabilities;
568
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900569 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800570 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
571 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900572 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800573 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
574 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575 }
576
Robert Greenwalt06314e42014-10-29 14:04:06 -0700577 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800578 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800579 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
580 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700581 }
582
paulhud9736de2019-03-08 16:35:20 +0800583 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900584 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800585 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
586 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
587 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900588 }
589
Robert Greenwalt1448f052014-04-08 13:41:39 -0700590 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400591 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
592 * typically provided by restricted networks.
593 *
594 * TODO: consider:
595 * - Renaming it to guessRestrictedCapability and make it set the
596 * restricted capability bit in addition to clearing it.
597 * @hide
598 */
599 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800600 // Check if we have any capability that forces the network to be restricted.
601 final boolean forceRestrictedCapability =
602 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
603
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700604 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800605 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700606 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800607 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700608
609 // Must have at least some restricted capabilities.
610 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800611 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700612
Pavel Maltsev4af91072018-03-07 14:33:22 -0800613 if (forceRestrictedCapability
614 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400615 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400616 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400617 }
618
619 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700620 * Representing the transport type. Apps should generally not care about transport. A
621 * request for a fast internet connection could be satisfied by a number of different
622 * transports. If any are specified here it will be satisfied a Network that matches
623 * any of them. If a caller doesn't care about the transport it should not specify any.
624 */
625 private long mTransportTypes;
626
Jeff Sharkeyde570312017-10-24 21:25:50 -0600627 /** @hide */
628 @Retention(RetentionPolicy.SOURCE)
629 @IntDef(prefix = { "TRANSPORT_" }, value = {
630 TRANSPORT_CELLULAR,
631 TRANSPORT_WIFI,
632 TRANSPORT_BLUETOOTH,
633 TRANSPORT_ETHERNET,
634 TRANSPORT_VPN,
635 TRANSPORT_WIFI_AWARE,
636 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800637 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600638 })
639 public @interface Transport { }
640
Robert Greenwalt1448f052014-04-08 13:41:39 -0700641 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700642 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700643 */
644 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700645
646 /**
647 * Indicates this network uses a Wi-Fi transport.
648 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700649 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700650
651 /**
652 * Indicates this network uses a Bluetooth transport.
653 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700654 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700655
656 /**
657 * Indicates this network uses an Ethernet transport.
658 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700659 public static final int TRANSPORT_ETHERNET = 3;
660
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400661 /**
662 * Indicates this network uses a VPN transport.
663 */
664 public static final int TRANSPORT_VPN = 4;
665
Etan Cohen305ea282016-06-20 09:27:12 -0700666 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700667 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700668 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700669 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700670
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700671 /**
672 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700673 */
674 public static final int TRANSPORT_LOWPAN = 6;
675
Benedict Wong89ce5e32018-11-14 17:40:55 -0800676 /**
677 * Indicates this network uses a Test-only virtual interface as a transport.
678 *
679 * @hide
680 */
681 @TestApi
682 public static final int TRANSPORT_TEST = 7;
683
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900684 /** @hide */
685 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
686 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800687 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700688
Hugo Benichi16f0a942017-06-20 14:07:59 +0900689 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600690 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900691 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
692 }
693
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900694 private static final String[] TRANSPORT_NAMES = {
695 "CELLULAR",
696 "WIFI",
697 "BLUETOOTH",
698 "ETHERNET",
699 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700700 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800701 "LOWPAN",
702 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900703 };
704
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700705 /**
706 * Adds the given transport type to this {@code NetworkCapability} instance.
707 * Multiple transports may be applied sequentially. Note that when searching
708 * for a network to satisfy a request, any listed in the request will satisfy the request.
709 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
710 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
711 * to be selected. This is logically different than
712 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
713 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600714 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900715 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700716 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700717 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100718 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +0800719 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900720 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700721 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700722 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700723 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700724 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700725
726 /**
727 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
728 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600729 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900730 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700731 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700732 */
paulhud9736de2019-03-08 16:35:20 +0800733 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900734 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700735 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700736 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700737 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700738 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700739
740 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600741 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
742 * instance.
743 *
744 * @hide
745 */
paulhud9736de2019-03-08 16:35:20 +0800746 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
747 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600748 if (value) {
749 addTransportType(transportType);
750 } else {
751 removeTransportType(transportType);
752 }
753 return this;
754 }
755
756 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700757 * Gets all the transports set on this {@code NetworkCapability} instance.
758 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600759 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700760 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700761 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600762 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900763 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800764 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900765 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700766 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700767
768 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600769 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700770 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600771 *
772 * @hide
773 */
774 public void setTransportTypes(@Transport int[] transportTypes) {
775 mTransportTypes = BitUtils.packBits(transportTypes);
776 }
777
778 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700779 * Tests for the presence of a transport on this instance.
780 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600781 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700782 * @return {@code true} if set on this instance.
783 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600784 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900785 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700786 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700787
788 private void combineTransportTypes(NetworkCapabilities nc) {
789 this.mTransportTypes |= nc.mTransportTypes;
790 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900791
Robert Greenwalt1448f052014-04-08 13:41:39 -0700792 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
793 return ((this.mTransportTypes == 0) ||
794 ((this.mTransportTypes & nc.mTransportTypes) != 0));
795 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900796
Robert Greenwalt06314e42014-10-29 14:04:06 -0700797 /** @hide */
798 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700799 return (nc.mTransportTypes == this.mTransportTypes);
800 }
801
802 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900803 * UID of the app that manages this network, or INVALID_UID if none/unknown.
804 *
805 * This field keeps track of the UID of the app that created this network and is in charge
806 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
807 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
808 * to create networks.
809 *
810 * Because this field is only used in the services side (and to avoid apps being able to
811 * set this to whatever they want), this field is not parcelled and will not be conserved
812 * across the IPC boundary.
813 * @hide
814 */
815 private int mEstablishingVpnAppUid = INVALID_UID;
816
817 /**
818 * Set the UID of the managing app.
819 * @hide
820 */
821 public void setEstablishingVpnAppUid(final int uid) {
822 mEstablishingVpnAppUid = uid;
823 }
824
825 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600826 * Value indicating that link bandwidth is unspecified.
827 * @hide
828 */
829 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
830
831 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700832 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
833 * for the first hop on the given transport. It is not measured, but may take into account
834 * link parameters (Radio technology, allocated channels, etc).
835 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600836 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
837 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700838
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700839 /**
840 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
841 * the estimated first hop transport bandwidth.
842 * <p>
843 * Note that when used to request a network, this specifies the minimum acceptable.
844 * When received as the state of an existing network this specifies the typical
845 * first hop bandwidth expected. This is never measured, but rather is inferred
846 * from technology type and other link parameters. It could be used to differentiate
847 * between very slow 1xRTT cellular links and other faster networks or even between
848 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
849 * fast backhauls and slow backhauls.
850 *
851 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700852 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700853 */
paulhud9736de2019-03-08 16:35:20 +0800854 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700855 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600856 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700857 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700858
859 /**
860 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
861 * the estimated first hop transport bandwidth.
862 *
863 * @return The estimated first hop upstream (device to network) bandwidth.
864 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700865 public int getLinkUpstreamBandwidthKbps() {
866 return mLinkUpBandwidthKbps;
867 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700868
869 /**
870 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
871 * the estimated first hop transport bandwidth.
872 * <p>
873 * Note that when used to request a network, this specifies the minimum acceptable.
874 * When received as the state of an existing network this specifies the typical
875 * first hop bandwidth expected. This is never measured, but rather is inferred
876 * from technology type and other link parameters. It could be used to differentiate
877 * between very slow 1xRTT cellular links and other faster networks or even between
878 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
879 * fast backhauls and slow backhauls.
880 *
881 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700882 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700883 */
paulhud9736de2019-03-08 16:35:20 +0800884 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700885 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600886 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700887 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700888
889 /**
890 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
891 * the estimated first hop transport bandwidth.
892 *
893 * @return The estimated first hop downstream (network to device) bandwidth.
894 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700895 public int getLinkDownstreamBandwidthKbps() {
896 return mLinkDownBandwidthKbps;
897 }
898
899 private void combineLinkBandwidths(NetworkCapabilities nc) {
900 this.mLinkUpBandwidthKbps =
901 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
902 this.mLinkDownBandwidthKbps =
903 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
904 }
905 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
906 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
907 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
908 }
909 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
910 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
911 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
912 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600913 /** @hide */
914 public static int minBandwidth(int a, int b) {
915 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
916 return b;
917 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
918 return a;
919 } else {
920 return Math.min(a, b);
921 }
922 }
923 /** @hide */
924 public static int maxBandwidth(int a, int b) {
925 return Math.max(a, b);
926 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700927
Etan Cohena7434272017-04-03 12:17:51 -0700928 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -0800929 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -0700930
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700931 /**
932 * Sets the optional bearer specific network specifier.
933 * This has no meaning if a single transport is also not specified, so calling
934 * this without a single transport set will generate an exception, as will
935 * subsequently adding or removing transports after this is set.
936 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700937 *
Etan Cohena7434272017-04-03 12:17:51 -0700938 * @param networkSpecifier A concrete, parcelable framework class that extends
939 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900940 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700941 * @hide
942 */
paulhud9736de2019-03-08 16:35:20 +0800943 public @NonNull NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -0700944 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700945 throw new IllegalStateException("Must have a single transport specified to use " +
946 "setNetworkSpecifier");
947 }
Etan Cohena7434272017-04-03 12:17:51 -0700948
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700949 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700950
Pierre Imaic8419a82016-03-22 17:54:54 +0900951 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700952 }
953
954 /**
Etan Cohenca9fb562018-11-27 07:32:39 -0800955 * Sets the optional transport specific information.
956 *
957 * @param transportInfo A concrete, parcelable framework class that extends
958 * {@link TransportInfo}.
959 * @return This NetworkCapabilities instance, to facilitate chaining.
960 * @hide
961 */
paulhud9736de2019-03-08 16:35:20 +0800962 public @NonNull NetworkCapabilities setTransportInfo(TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -0800963 mTransportInfo = transportInfo;
964 return this;
965 }
966
967 /**
paulhud9736de2019-03-08 16:35:20 +0800968 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700969 *
Etan Cohena7434272017-04-03 12:17:51 -0700970 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
paulhud9736de2019-03-08 16:35:20 +0800971 * specifier or {@code null}. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700972 * @hide
973 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100974 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
paulhud9736de2019-03-08 16:35:20 +0800975 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700976 return mNetworkSpecifier;
977 }
978
Etan Cohenca9fb562018-11-27 07:32:39 -0800979 /**
980 * Returns a transport-specific information container. The application may cast this
981 * container to a concrete sub-class based on its knowledge of the network request. The
982 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -0800983 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -0800984 *
985 * @return A concrete implementation of the {@link TransportInfo} class or null if not
986 * available for the network.
987 */
988 @Nullable public TransportInfo getTransportInfo() {
989 return mTransportInfo;
990 }
991
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700992 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700993 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700994 throw new IllegalStateException("Can't combine two networkSpecifiers");
995 }
Etan Cohena7434272017-04-03 12:17:51 -0700996 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700997 }
Etan Cohena7434272017-04-03 12:17:51 -0700998
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700999 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001000 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
1001 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001002 }
Etan Cohena7434272017-04-03 12:17:51 -07001003
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001004 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001005 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001006 }
1007
Etan Cohenca9fb562018-11-27 07:32:39 -08001008 private void combineTransportInfos(NetworkCapabilities nc) {
1009 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1010 throw new IllegalStateException("Can't combine two TransportInfos");
1011 }
1012 setTransportInfo(nc.mTransportInfo);
1013 }
1014
1015 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1016 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1017 }
1018
Robert Greenwalt1448f052014-04-08 13:41:39 -07001019 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001020 * Magic value that indicates no signal strength provided. A request specifying this value is
1021 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001022 */
1023 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1024
1025 /**
1026 * Signal strength. This is a signed integer, and higher values indicate better signal.
1027 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1028 */
paulhud9736de2019-03-08 16:35:20 +08001029 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001030 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001031
1032 /**
1033 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1034 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001035 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001036 * <p>
1037 * Note that when used to register a network callback, this specifies the minimum acceptable
1038 * signal strength. When received as the state of an existing network it specifies the current
1039 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1040 * effect when requesting a callback.
1041 *
1042 * @param signalStrength the bearer-specific signal strength.
1043 * @hide
1044 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001045 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001046 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001047 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001048 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001049 }
1050
1051 /**
1052 * Returns {@code true} if this object specifies a signal strength.
1053 *
1054 * @hide
1055 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001056 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001057 public boolean hasSignalStrength() {
1058 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1059 }
1060
1061 /**
1062 * Retrieves the signal strength.
1063 *
1064 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001065 */
1066 public int getSignalStrength() {
1067 return mSignalStrength;
1068 }
1069
1070 private void combineSignalStrength(NetworkCapabilities nc) {
1071 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1072 }
1073
1074 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1075 return this.mSignalStrength <= nc.mSignalStrength;
1076 }
1077
1078 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1079 return this.mSignalStrength == nc.mSignalStrength;
1080 }
1081
1082 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001083 * List of UIDs this network applies to. No restriction if null.
1084 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001085 * For networks, mUids represent the list of network this applies to, and null means this
1086 * network applies to all UIDs.
1087 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1088 * must be included in a network so that they match. As an exception to the general rule,
1089 * a null mUids field for requests mean "no requirements" rather than what the general rule
1090 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1091 * of this API expect in practice. A network that must match all UIDs can still be
1092 * expressed with a set ranging the entire set of possible UIDs.
1093 * <p>
1094 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001095 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1096 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1097 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1098 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001099 * As a special exception, the app managing this network (as identified by its UID stored in
1100 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1101 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1102 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001103 * <p>
1104 * Please note that in principle a single app can be associated with multiple UIDs because
1105 * each app will have a different UID when it's run as a different (macro-)user. A single
1106 * macro user can only have a single active VPN app at any given time however.
1107 * <p>
1108 * Also please be aware this class does not try to enforce any normalization on this. Callers
1109 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1110 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1111 * their own (like requiring sortedness or no overlap) they need to enforce it
1112 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1113 * or overlapping ranges are present.
1114 *
1115 * @hide
1116 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001117 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001118
1119 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001120 * Convenience method to set the UIDs this network applies to to a single UID.
1121 * @hide
1122 */
paulhud9736de2019-03-08 16:35:20 +08001123 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001124 final ArraySet<UidRange> identity = new ArraySet<>(1);
1125 identity.add(new UidRange(uid, uid));
1126 setUids(identity);
1127 return this;
1128 }
1129
1130 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001131 * Set the list of UIDs this network applies to.
1132 * This makes a copy of the set so that callers can't modify it after the call.
1133 * @hide
1134 */
paulhud9736de2019-03-08 16:35:20 +08001135 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001136 if (null == uids) {
1137 mUids = null;
1138 } else {
1139 mUids = new ArraySet<>(uids);
1140 }
1141 return this;
1142 }
1143
1144 /**
1145 * Get the list of UIDs this network applies to.
1146 * This returns a copy of the set so that callers can't modify the original object.
1147 * @hide
1148 */
paulhud9736de2019-03-08 16:35:20 +08001149 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001150 return null == mUids ? null : new ArraySet<>(mUids);
1151 }
1152
1153 /**
1154 * Test whether this network applies to this UID.
1155 * @hide
1156 */
1157 public boolean appliesToUid(int uid) {
1158 if (null == mUids) return true;
1159 for (UidRange range : mUids) {
1160 if (range.contains(uid)) {
1161 return true;
1162 }
1163 }
1164 return false;
1165 }
1166
1167 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001168 * Tests if the set of UIDs that this network applies to is the same as the passed network.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001169 * <p>
1170 * This test only checks whether equal range objects are in both sets. It will
1171 * return false if the ranges are not exactly the same, even if the covered UIDs
1172 * are for an equivalent result.
1173 * <p>
1174 * Note that this method is not very optimized, which is fine as long as it's not used very
1175 * often.
1176 * <p>
1177 * nc is assumed nonnull.
1178 *
1179 * @hide
1180 */
1181 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001182 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001183 Set<UidRange> comparedUids = nc.mUids;
1184 if (null == comparedUids) return null == mUids;
1185 if (null == mUids) return false;
1186 // Make a copy so it can be mutated to check that all ranges in mUids
1187 // also are in uids.
1188 final Set<UidRange> uids = new ArraySet<>(mUids);
1189 for (UidRange range : comparedUids) {
1190 if (!uids.contains(range)) {
1191 return false;
1192 }
1193 uids.remove(range);
1194 }
1195 return uids.isEmpty();
1196 }
1197
1198 /**
1199 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1200 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001201 * This method is called on the NetworkCapabilities embedded in a request with the
1202 * capabilities of an available network. It checks whether all the UIDs from this listen
1203 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1204 * in the passed nc (representing the UIDs that this network is available to).
1205 * <p>
1206 * As a special exception, the UID that created the passed network (as represented by its
1207 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1208 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1209 * can see its own network when it listens for it.
1210 * <p>
1211 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001212 * @see #appliesToUid
1213 * @hide
1214 */
paulhud9736de2019-03-08 16:35:20 +08001215 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001216 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001217 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001218 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001219 if (!nc.appliesToUidRange(requiredRange)) {
1220 return false;
1221 }
1222 }
1223 return true;
1224 }
1225
1226 /**
1227 * Returns whether this network applies to the passed ranges.
1228 * This assumes that to apply, the passed range has to be entirely contained
1229 * within one of the ranges this network applies to. If the ranges are not normalized,
1230 * this method may return false even though all required UIDs are covered because no
1231 * single range contained them all.
1232 * @hide
1233 */
1234 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001235 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001236 if (null == mUids) return true;
1237 for (UidRange uidRange : mUids) {
1238 if (uidRange.containsRange(requiredRange)) {
1239 return true;
1240 }
1241 }
1242 return false;
1243 }
1244
1245 /**
1246 * Combine the UIDs this network currently applies to with the UIDs the passed
1247 * NetworkCapabilities apply to.
1248 * nc is assumed nonnull.
1249 */
paulhud9736de2019-03-08 16:35:20 +08001250 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001251 if (null == nc.mUids || null == mUids) {
1252 mUids = null;
1253 return;
1254 }
1255 mUids.addAll(nc.mUids);
1256 }
1257
Chalard Jeanb03a6222018-04-11 21:09:10 +09001258
1259 /**
1260 * The SSID of the network, or null if not applicable or unknown.
1261 * <p>
1262 * This is filled in by wifi code.
1263 * @hide
1264 */
1265 private String mSSID;
1266
1267 /**
1268 * Sets the SSID of this network.
1269 * @hide
1270 */
paulhud9736de2019-03-08 16:35:20 +08001271 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001272 mSSID = ssid;
1273 return this;
1274 }
1275
1276 /**
1277 * Gets the SSID of this network, or null if none or unknown.
1278 * @hide
1279 */
paulhud9736de2019-03-08 16:35:20 +08001280 public @Nullable String getSSID() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001281 return mSSID;
1282 }
1283
1284 /**
1285 * Tests if the SSID of this network is the same as the SSID of the passed network.
1286 * @hide
1287 */
paulhud9736de2019-03-08 16:35:20 +08001288 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001289 return Objects.equals(mSSID, nc.mSSID);
1290 }
1291
1292 /**
1293 * Check if the SSID requirements of this object are matched by the passed object.
1294 * @hide
1295 */
paulhud9736de2019-03-08 16:35:20 +08001296 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001297 return mSSID == null || mSSID.equals(nc.mSSID);
1298 }
1299
1300 /**
1301 * Combine SSIDs of the capabilities.
1302 * <p>
1303 * This is only legal if either the SSID of this object is null, or both SSIDs are
1304 * equal.
1305 * @hide
1306 */
paulhud9736de2019-03-08 16:35:20 +08001307 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001308 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1309 throw new IllegalStateException("Can't combine two SSIDs");
1310 }
1311 setSSID(nc.mSSID);
1312 }
1313
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001314 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001315 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1316 * <p>
1317 * Note that this method may break an invariant of having a particular capability in either
1318 * wanted or unwanted lists but never in both. Requests that have the same capability in
1319 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001320 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001321 */
paulhud9736de2019-03-08 16:35:20 +08001322 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001323 combineNetCapabilities(nc);
1324 combineTransportTypes(nc);
1325 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001326 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001327 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001328 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001329 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001330 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001331 }
1332
1333 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001334 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1335 *
1336 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1337 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1338 * bandwidth, signal strength, or validation / captive portal status.
1339 *
1340 * @hide
1341 */
1342 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001343 return (nc != null
1344 && satisfiedByNetCapabilities(nc, onlyImmutable)
1345 && satisfiedByTransportTypes(nc)
1346 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1347 && satisfiedBySpecifier(nc)
1348 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001349 && (onlyImmutable || satisfiedByUids(nc))
1350 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001351 }
1352
1353 /**
1354 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1355 *
1356 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1357 *
1358 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001359 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001360 @TestApi
1361 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001362 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001363 return satisfiedByNetworkCapabilities(nc, false);
1364 }
1365
1366 /**
1367 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1368 *
1369 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1370 *
1371 * @hide
1372 */
paulhud9736de2019-03-08 16:35:20 +08001373 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001374 return satisfiedByNetworkCapabilities(nc, true);
1375 }
1376
1377 /**
1378 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001379 * {@code NetworkCapabilities} and return a String describing any difference.
1380 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001381 *
1382 * @hide
1383 */
paulhud9736de2019-03-08 16:35:20 +08001384 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001385 if (that == null) {
1386 return "other NetworkCapabilities was null";
1387 }
1388
1389 StringJoiner joiner = new StringJoiner(", ");
1390
Hugo Benichieae7a222017-07-25 11:40:56 +09001391 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1392 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001393 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001394 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1395 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1396 if (oldImmutableCapabilities != newImmutableCapabilities) {
1397 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1398 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1399 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1400 }
1401
1402 if (!equalsSpecifier(that)) {
1403 NetworkSpecifier before = this.getNetworkSpecifier();
1404 NetworkSpecifier after = that.getNetworkSpecifier();
1405 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1406 }
1407
1408 if (!equalsTransportTypes(that)) {
1409 String before = transportNamesOf(this.getTransportTypes());
1410 String after = transportNamesOf(that.getTransportTypes());
1411 joiner.add(String.format("transports changed: %s -> %s", before, after));
1412 }
1413
1414 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001415 }
1416
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001417 /**
1418 * Checks that our requestable capabilities are the same as those of the given
1419 * {@code NetworkCapabilities}.
1420 *
1421 * @hide
1422 */
paulhud9736de2019-03-08 16:35:20 +08001423 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001424 if (nc == null) return false;
1425 return (equalsNetCapabilitiesRequestable(nc) &&
1426 equalsTransportTypes(nc) &&
1427 equalsSpecifier(nc));
1428 }
1429
Robert Greenwalt1448f052014-04-08 13:41:39 -07001430 @Override
paulhud9736de2019-03-08 16:35:20 +08001431 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001432 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001433 NetworkCapabilities that = (NetworkCapabilities) obj;
1434 return (equalsNetCapabilities(that)
1435 && equalsTransportTypes(that)
1436 && equalsLinkBandwidths(that)
1437 && equalsSignalStrength(that)
1438 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001439 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001440 && equalsUids(that)
1441 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001442 }
1443
1444 @Override
1445 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001446 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001447 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001448 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1449 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1450 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1451 + ((int) (mTransportTypes >> 32) * 13)
1452 + (mLinkUpBandwidthKbps * 17)
1453 + (mLinkDownBandwidthKbps * 19)
1454 + Objects.hashCode(mNetworkSpecifier) * 23
1455 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001456 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001457 + Objects.hashCode(mSSID) * 37
1458 + Objects.hashCode(mTransportInfo) * 41;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001459 }
1460
Wink Saville4e2dea72014-09-20 11:04:03 -07001461 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001462 public int describeContents() {
1463 return 0;
1464 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001465 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001466 public void writeToParcel(Parcel dest, int flags) {
1467 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001468 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001469 dest.writeLong(mTransportTypes);
1470 dest.writeInt(mLinkUpBandwidthKbps);
1471 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001472 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001473 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001474 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001475 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001476 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001477 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001478
Robert Greenwalt1448f052014-04-08 13:41:39 -07001479 public static final Creator<NetworkCapabilities> CREATOR =
1480 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001481 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001482 public NetworkCapabilities createFromParcel(Parcel in) {
1483 NetworkCapabilities netCap = new NetworkCapabilities();
1484
1485 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001486 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001487 netCap.mTransportTypes = in.readLong();
1488 netCap.mLinkUpBandwidthKbps = in.readInt();
1489 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001490 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001491 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001492 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001493 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1494 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001495 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001496 return netCap;
1497 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001498 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001499 public NetworkCapabilities[] newArray(int size) {
1500 return new NetworkCapabilities[size];
1501 }
1502 };
1503
Wink Saville4e2dea72014-09-20 11:04:03 -07001504 @Override
paulhud9736de2019-03-08 16:35:20 +08001505 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001506 final StringBuilder sb = new StringBuilder("[");
1507 if (0 != mTransportTypes) {
1508 sb.append(" Transports: ");
1509 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1510 NetworkCapabilities::transportNameOf, "|");
1511 }
1512 if (0 != mNetworkCapabilities) {
1513 sb.append(" Capabilities: ");
1514 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1515 NetworkCapabilities::capabilityNameOf, "&");
1516 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001517 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001518 sb.append(" Unwanted: ");
1519 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1520 NetworkCapabilities::capabilityNameOf, "&");
1521 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001522 if (mLinkUpBandwidthKbps > 0) {
1523 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1524 }
1525 if (mLinkDownBandwidthKbps > 0) {
1526 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1527 }
1528 if (mNetworkSpecifier != null) {
1529 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1530 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001531 if (mTransportInfo != null) {
1532 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1533 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001534 if (hasSignalStrength()) {
1535 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001536 }
1537
Chalard Jean07ace0f2018-02-26 19:00:45 +09001538 if (null != mUids) {
1539 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1540 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1541 } else {
1542 sb.append(" Uids: <").append(mUids).append(">");
1543 }
1544 }
1545 if (mEstablishingVpnAppUid != INVALID_UID) {
1546 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1547 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001548
Chalard Jeanb03a6222018-04-11 21:09:10 +09001549 if (null != mSSID) {
1550 sb.append(" SSID: ").append(mSSID);
1551 }
1552
Chalard Jean07ace0f2018-02-26 19:00:45 +09001553 sb.append("]");
1554 return sb.toString();
1555 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001556
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001557
Chalard Jean07ace0f2018-02-26 19:00:45 +09001558 private interface NameOf {
1559 String nameOf(int value);
1560 }
1561 /**
1562 * @hide
1563 */
paulhud9736de2019-03-08 16:35:20 +08001564 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1565 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001566 int bitPos = 0;
1567 boolean firstElementAdded = false;
1568 while (bitMask != 0) {
1569 if ((bitMask & 1) != 0) {
1570 if (firstElementAdded) {
1571 sb.append(separator);
1572 } else {
1573 firstElementAdded = true;
1574 }
1575 sb.append(nameFetcher.nameOf(bitPos));
1576 }
1577 bitMask >>= 1;
1578 ++bitPos;
1579 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001580 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001581
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001582 /** @hide */
paulhud9736de2019-03-08 16:35:20 +08001583 public void writeToProto(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001584 final long token = proto.start(fieldId);
1585
1586 for (int transport : getTransportTypes()) {
1587 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1588 }
1589
1590 for (int capability : getCapabilities()) {
1591 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1592 }
1593
1594 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1595 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1596
1597 if (mNetworkSpecifier != null) {
1598 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1599 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001600 if (mTransportInfo != null) {
1601 // TODO b/120653863: write transport-specific info to proto?
1602 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001603
1604 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1605 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1606
1607 proto.end(token);
1608 }
1609
Hugo Benichi5df9d722016-04-25 17:16:35 +09001610 /**
1611 * @hide
1612 */
paulhud9736de2019-03-08 16:35:20 +08001613 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001614 StringJoiner joiner = new StringJoiner("|");
1615 if (capabilities != null) {
1616 for (int c : capabilities) {
1617 joiner.add(capabilityNameOf(c));
1618 }
1619 }
1620 return joiner.toString();
1621 }
1622
1623 /**
1624 * @hide
1625 */
paulhud9736de2019-03-08 16:35:20 +08001626 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001627 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001628 case NET_CAPABILITY_MMS: return "MMS";
1629 case NET_CAPABILITY_SUPL: return "SUPL";
1630 case NET_CAPABILITY_DUN: return "DUN";
1631 case NET_CAPABILITY_FOTA: return "FOTA";
1632 case NET_CAPABILITY_IMS: return "IMS";
1633 case NET_CAPABILITY_CBS: return "CBS";
1634 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1635 case NET_CAPABILITY_IA: return "IA";
1636 case NET_CAPABILITY_RCS: return "RCS";
1637 case NET_CAPABILITY_XCAP: return "XCAP";
1638 case NET_CAPABILITY_EIMS: return "EIMS";
1639 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1640 case NET_CAPABILITY_INTERNET: return "INTERNET";
1641 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1642 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1643 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1644 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1645 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1646 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1647 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1648 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1649 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1650 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1651 case NET_CAPABILITY_MCX: return "MCX";
1652 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1653 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001654 }
1655 }
1656
1657 /**
1658 * @hide
1659 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001660 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001661 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001662 StringJoiner joiner = new StringJoiner("|");
1663 if (types != null) {
1664 for (int t : types) {
1665 joiner.add(transportNameOf(t));
1666 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001667 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001668 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001669 }
1670
1671 /**
1672 * @hide
1673 */
paulhud9736de2019-03-08 16:35:20 +08001674 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001675 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001676 return "UNKNOWN";
1677 }
1678 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001679 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001680
Jeff Sharkeyde570312017-10-24 21:25:50 -06001681 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001682 Preconditions.checkArgument(
1683 isValidTransport(transport), "Invalid TransportType " + transport);
1684 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001685
1686 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1687 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1688 }
1689
1690 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1691 Preconditions.checkArgument(isValidCapability(capability),
1692 "NetworkCapability " + capability + "out of range");
1693 }
junyulai05986c62018-08-07 19:50:45 +08001694
1695 /**
1696 * Check if this {@code NetworkCapability} instance is metered.
1697 *
1698 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1699 * @hide
1700 */
1701 public boolean isMetered() {
1702 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1703 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001704}