blob: 0a63e75d1a31024210b24f6a8f94ed17b88e9f23 [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;
Etan Cohenca9fb562018-11-27 07:32:39 -080020import android.annotation.Nullable;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070021import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060022import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010023import android.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060024import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010025import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070026import android.os.Parcel;
27import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090028import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080029import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070030
31import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090032import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090033import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070034
Jeff Sharkeyde570312017-10-24 21:25:50 -060035import java.lang.annotation.Retention;
36import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070037import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090038import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090039import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070040
41/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070042 * Representation of the capabilities of an active network. Instances are
43 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060044 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
45 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060046 * <p>
47 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
48 * network selection. Rather than indicate a need for Wi-Fi because an
49 * application needs high bandwidth and risk obsolescence when a new, fast
50 * network appears (like LTE), the application should specify it needs high
51 * bandwidth. Similarly if an application needs an unmetered network for a bulk
52 * transfer it can specify that rather than assuming all cellular based
53 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070054 */
55public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070056 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090057 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070058
Robert Greenwalt7569f182014-06-08 16:42:59 -070059 /**
60 * @hide
61 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010062 @UnsupportedAppUsage
Robert Greenwalt01d004e2014-05-18 15:24:21 -070063 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090064 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090065 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070066 }
67
68 public NetworkCapabilities(NetworkCapabilities nc) {
69 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090070 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070071 }
72 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070073
74 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090075 * Completely clears the contents of this object, removing even the capabilities that are set
76 * by default when the object is constructed.
77 * @hide
78 */
79 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080080 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070081 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090082 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080083 mTransportInfo = 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;
Chalard Jeanb03a6222018-04-11 21:09:10 +090087 mSSID = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090088 }
89
90 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090091 * Set all contents of this object to the contents of a NetworkCapabilities.
92 * @hide
93 */
94 public void set(NetworkCapabilities nc) {
95 mNetworkCapabilities = nc.mNetworkCapabilities;
96 mTransportTypes = nc.mTransportTypes;
97 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
98 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
99 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800100 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900101 mSignalStrength = nc.mSignalStrength;
102 setUids(nc.mUids); // Will make the defensive copy
103 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
104 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
105 mSSID = nc.mSSID;
106 }
107
108 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700109 * Represents the network's capabilities. If any are specified they will be satisfied
110 * by any Network that matches all of them.
111 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100112 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900113 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700114
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800115 /**
116 * If any capabilities specified here they must not exist in the matching Network.
117 */
118 private long mUnwantedNetworkCapabilities;
119
Jeff Sharkeyde570312017-10-24 21:25:50 -0600120 /** @hide */
121 @Retention(RetentionPolicy.SOURCE)
122 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
123 NET_CAPABILITY_MMS,
124 NET_CAPABILITY_SUPL,
125 NET_CAPABILITY_DUN,
126 NET_CAPABILITY_FOTA,
127 NET_CAPABILITY_IMS,
128 NET_CAPABILITY_CBS,
129 NET_CAPABILITY_WIFI_P2P,
130 NET_CAPABILITY_IA,
131 NET_CAPABILITY_RCS,
132 NET_CAPABILITY_XCAP,
133 NET_CAPABILITY_EIMS,
134 NET_CAPABILITY_NOT_METERED,
135 NET_CAPABILITY_INTERNET,
136 NET_CAPABILITY_NOT_RESTRICTED,
137 NET_CAPABILITY_TRUSTED,
138 NET_CAPABILITY_NOT_VPN,
139 NET_CAPABILITY_VALIDATED,
140 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600141 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600142 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900143 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900144 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800145 NET_CAPABILITY_OEM_PAID,
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800146 NET_CAPABILITY_MCX
Jeff Sharkeyde570312017-10-24 21:25:50 -0600147 })
148 public @interface NetCapability { }
149
Robert Greenwalt1448f052014-04-08 13:41:39 -0700150 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700151 * Indicates this is a network that has the ability to reach the
152 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700153 */
154 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700155
156 /**
157 * Indicates this is a network that has the ability to reach the carrier's
158 * SUPL server, used to retrieve GPS information.
159 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700160 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700161
162 /**
163 * Indicates this is a network that has the ability to reach the carrier's
164 * DUN or tethering gateway.
165 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700166 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700167
168 /**
169 * Indicates this is a network that has the ability to reach the carrier's
170 * FOTA portal, used for over the air updates.
171 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700172 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700173
174 /**
175 * Indicates this is a network that has the ability to reach the carrier's
176 * IMS servers, used for network registration and signaling.
177 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700178 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700179
180 /**
181 * Indicates this is a network that has the ability to reach the carrier's
182 * CBS servers, used for carrier specific services.
183 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700184 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700185
186 /**
187 * Indicates this is a network that has the ability to reach a Wi-Fi direct
188 * peer.
189 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700190 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700191
192 /**
193 * Indicates this is a network that has the ability to reach a carrier's
194 * Initial Attach servers.
195 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700196 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197
198 /**
199 * Indicates this is a network that has the ability to reach a carrier's
200 * RCS servers, used for Rich Communication Services.
201 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700202 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700203
204 /**
205 * Indicates this is a network that has the ability to reach a carrier's
206 * XCAP servers, used for configuration and control.
207 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700208 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700209
210 /**
211 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700212 * Emergency IMS servers or other services, used for network signaling
213 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700214 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216
217 /**
218 * Indicates that this network is unmetered.
219 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700220 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700221
222 /**
223 * Indicates that this network should be able to reach the internet.
224 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700225 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700226
227 /**
228 * Indicates that this network is available for general use. If this is not set
229 * applications should not attempt to communicate on this network. Note that this
230 * is simply informative and not enforcement - enforcement is handled via other means.
231 * Set by default.
232 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700233 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
234
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700235 /**
236 * Indicates that the user has indicated implicit trust of this network. This
237 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
238 * BT device or a wifi the user asked to connect to. Untrusted networks
239 * are probably limited to unknown wifi AP. Set by default.
240 */
241 public static final int NET_CAPABILITY_TRUSTED = 14;
242
Paul Jensen76b610a2015-03-18 09:33:07 -0400243 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400244 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400245 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400246 */
247 public static final int NET_CAPABILITY_NOT_VPN = 15;
248
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900249 /**
250 * Indicates that connectivity on this network was successfully validated. For example, for a
251 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
252 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900253 */
254 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700255
Paul Jensen3d194ea2015-06-16 14:27:36 -0400256 /**
257 * Indicates that this network was found to have a captive portal in place last time it was
258 * probed.
259 */
260 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
261
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900262 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600263 * Indicates that this network is not roaming.
264 */
265 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
266
267 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900268 * Indicates that this network is available for use by apps, and not a network that is being
269 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900270 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600271 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900272
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900273 /**
274 * Indicates that this network is not congested.
275 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600276 * When a network is congested, applications should defer network traffic
277 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900278 */
279 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
280
Chalard Jean804b8fb2018-01-30 22:41:41 +0900281 /**
282 * Indicates that this network is not currently suspended.
283 * <p>
284 * When a network is suspended, the network's IP addresses and any connections
285 * established on the network remain valid, but the network is temporarily unable
286 * to transfer data. This can happen, for example, if a cellular network experiences
287 * a temporary loss of signal, such as when driving through a tunnel, etc.
288 * A network with this capability is not suspended, so is expected to be able to
289 * transfer data.
290 */
291 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
292
Pavel Maltsev43403202018-01-30 17:19:44 -0800293 /**
294 * Indicates that traffic that goes through this network is paid by oem. For example,
295 * this network can be used by system apps to upload telemetry data.
296 * @hide
297 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700298 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800299 public static final int NET_CAPABILITY_OEM_PAID = 22;
300
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800301 /**
302 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
303 * servers.
304 */
305 public static final int NET_CAPABILITY_MCX = 23;
306
Robert Greenwalt1448f052014-04-08 13:41:39 -0700307 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800308 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_MCX;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700309
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700310 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900311 * Network capabilities that are expected to be mutable, i.e., can change while a particular
312 * network is connected.
313 */
314 private static final long MUTABLE_CAPABILITIES =
315 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
316 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900317 (1 << NET_CAPABILITY_TRUSTED)
318 | (1 << NET_CAPABILITY_VALIDATED)
319 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
320 | (1 << NET_CAPABILITY_NOT_ROAMING)
321 | (1 << NET_CAPABILITY_FOREGROUND)
322 | (1 << NET_CAPABILITY_NOT_CONGESTED)
323 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900324
325 /**
326 * Network capabilities that are not allowed in NetworkRequests. This exists because the
327 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
328 * capability's presence cannot be known in advance. If such a capability is requested, then we
329 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
330 * get immediately torn down because they do not have the requested capability.
331 */
332 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900333 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900334
335 /**
336 * Capabilities that are set by default when the object is constructed.
337 */
338 private static final long DEFAULT_CAPABILITIES =
339 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
340 (1 << NET_CAPABILITY_TRUSTED) |
341 (1 << NET_CAPABILITY_NOT_VPN);
342
343 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400344 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800345 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400346 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700347 @VisibleForTesting
348 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400349 (1 << NET_CAPABILITY_CBS) |
350 (1 << NET_CAPABILITY_DUN) |
351 (1 << NET_CAPABILITY_EIMS) |
352 (1 << NET_CAPABILITY_FOTA) |
353 (1 << NET_CAPABILITY_IA) |
354 (1 << NET_CAPABILITY_IMS) |
355 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800356 (1 << NET_CAPABILITY_XCAP) |
357 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800358
359 /**
360 * Capabilities that force network to be restricted.
361 * {@see #maybeMarkCapabilitiesRestricted}.
362 */
363 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800364 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400365
366 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700367 * Capabilities that suggest that a network is unrestricted.
368 * {@see #maybeMarkCapabilitiesRestricted}.
369 */
370 @VisibleForTesting
371 /* package */ static final long UNRESTRICTED_CAPABILITIES =
372 (1 << NET_CAPABILITY_INTERNET) |
373 (1 << NET_CAPABILITY_MMS) |
374 (1 << NET_CAPABILITY_SUPL) |
375 (1 << NET_CAPABILITY_WIFI_P2P);
376
377 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700378 * Adds the given capability to this {@code NetworkCapability} instance.
379 * Multiple capabilities may be applied sequentially. Note that when searching
380 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800381 * <p>
382 * If the given capability was previously added to the list of unwanted capabilities
383 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600385 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900386 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700387 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700388 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100389 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600390 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800391 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700392 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800393 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700394 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700395 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700396
397 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800398 * Adds the given capability to the list of unwanted capabilities of this
399 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
400 * sequentially. Note that when searching for a network to satisfy a request, the network
401 * must not contain any capability from unwanted capability list.
402 * <p>
403 * If the capability was previously added to the list of required capabilities (for
404 * example, it was there by default or added using {@link #addCapability(int)} method), then
405 * it will be removed from the list of required capabilities as well.
406 *
407 * @see #addCapability(int)
408 * @hide
409 */
410 public void addUnwantedCapability(@NetCapability int capability) {
411 checkValidCapability(capability);
412 mUnwantedNetworkCapabilities |= 1 << capability;
413 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
414 }
415
416 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700417 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800418 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800419 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800420 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700421 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600422 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900423 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700424 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700425 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100426 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600427 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800428 checkValidCapability(capability);
429 final long mask = ~(1 << capability);
430 mNetworkCapabilities &= mask;
431 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700432 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700433 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700434
435 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600436 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
437 * instance.
438 *
439 * @hide
440 */
441 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
442 if (value) {
443 addCapability(capability);
444 } else {
445 removeCapability(capability);
446 }
447 return this;
448 }
449
450 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700451 * Gets all the capabilities set on this {@code NetworkCapability} instance.
452 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600453 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700454 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700455 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600456 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600457 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900458 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700459 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700460
461 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800462 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
463 *
464 * @return an array of unwanted capability values for this instance.
465 * @hide
466 */
467 public @NetCapability int[] getUnwantedCapabilities() {
468 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
469 }
470
471
472 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600473 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700474 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600475 *
476 * @hide
477 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800478 public void setCapabilities(@NetCapability int[] capabilities,
479 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600480 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800481 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600482 }
483
484 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800485 * @deprecated use {@link #setCapabilities(int[], int[])}
486 * @hide
487 */
488 @Deprecated
489 public void setCapabilities(@NetCapability int[] capabilities) {
490 setCapabilities(capabilities, new int[] {});
491 }
492
493 /**
494 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700495 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600496 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700497 * @return {@code true} if set on this instance.
498 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600499 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800500 return isValidCapability(capability)
501 && ((mNetworkCapabilities & (1 << capability)) != 0);
502 }
503
504 /** @hide */
505 public boolean hasUnwantedCapability(@NetCapability int capability) {
506 return isValidCapability(capability)
507 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700508 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700509
Pavel Maltseve18ef262018-03-07 11:13:04 -0800510 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 private void combineNetCapabilities(NetworkCapabilities nc) {
512 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800513 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700514 }
515
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900516 /**
517 * Convenience function that returns a human-readable description of the first mutable
518 * capability we find. Used to present an error message to apps that request mutable
519 * capabilities.
520 *
521 * @hide
522 */
523 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800524 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
525 & NON_REQUESTABLE_CAPABILITIES;
526
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900527 if (nonRequestable != 0) {
528 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900529 }
530 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900531 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900532 return null;
533 }
534
535 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800536 long requestedCapabilities = mNetworkCapabilities;
537 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
538 long providedCapabilities = nc.mNetworkCapabilities;
539
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900540 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800541 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
542 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900543 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800544 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
545 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700546 }
547
Robert Greenwalt06314e42014-10-29 14:04:06 -0700548 /** @hide */
549 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800550 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
551 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700552 }
553
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900554 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
555 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800556 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
557 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
558 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900559 }
560
Robert Greenwalt1448f052014-04-08 13:41:39 -0700561 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400562 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
563 * typically provided by restricted networks.
564 *
565 * TODO: consider:
566 * - Renaming it to guessRestrictedCapability and make it set the
567 * restricted capability bit in addition to clearing it.
568 * @hide
569 */
570 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800571 // Check if we have any capability that forces the network to be restricted.
572 final boolean forceRestrictedCapability =
573 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
574
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700575 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800576 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700577 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800578 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700579
580 // Must have at least some restricted capabilities.
581 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800582 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700583
Pavel Maltsev4af91072018-03-07 14:33:22 -0800584 if (forceRestrictedCapability
585 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400586 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400587 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400588 }
589
590 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700591 * Representing the transport type. Apps should generally not care about transport. A
592 * request for a fast internet connection could be satisfied by a number of different
593 * transports. If any are specified here it will be satisfied a Network that matches
594 * any of them. If a caller doesn't care about the transport it should not specify any.
595 */
596 private long mTransportTypes;
597
Jeff Sharkeyde570312017-10-24 21:25:50 -0600598 /** @hide */
599 @Retention(RetentionPolicy.SOURCE)
600 @IntDef(prefix = { "TRANSPORT_" }, value = {
601 TRANSPORT_CELLULAR,
602 TRANSPORT_WIFI,
603 TRANSPORT_BLUETOOTH,
604 TRANSPORT_ETHERNET,
605 TRANSPORT_VPN,
606 TRANSPORT_WIFI_AWARE,
607 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800608 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600609 })
610 public @interface Transport { }
611
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700613 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700614 */
615 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700616
617 /**
618 * Indicates this network uses a Wi-Fi transport.
619 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700620 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700621
622 /**
623 * Indicates this network uses a Bluetooth transport.
624 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700625 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700626
627 /**
628 * Indicates this network uses an Ethernet transport.
629 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700630 public static final int TRANSPORT_ETHERNET = 3;
631
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400632 /**
633 * Indicates this network uses a VPN transport.
634 */
635 public static final int TRANSPORT_VPN = 4;
636
Etan Cohen305ea282016-06-20 09:27:12 -0700637 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700638 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700639 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700640 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700641
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700642 /**
643 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700644 */
645 public static final int TRANSPORT_LOWPAN = 6;
646
Benedict Wong89ce5e32018-11-14 17:40:55 -0800647 /**
648 * Indicates this network uses a Test-only virtual interface as a transport.
649 *
650 * @hide
651 */
652 @TestApi
653 public static final int TRANSPORT_TEST = 7;
654
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900655 /** @hide */
656 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
657 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800658 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700659
Hugo Benichi16f0a942017-06-20 14:07:59 +0900660 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600661 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900662 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
663 }
664
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900665 private static final String[] TRANSPORT_NAMES = {
666 "CELLULAR",
667 "WIFI",
668 "BLUETOOTH",
669 "ETHERNET",
670 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700671 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800672 "LOWPAN",
673 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900674 };
675
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700676 /**
677 * Adds the given transport type to this {@code NetworkCapability} instance.
678 * Multiple transports may be applied sequentially. Note that when searching
679 * for a network to satisfy a request, any listed in the request will satisfy the request.
680 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
681 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
682 * to be selected. This is logically different than
683 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
684 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600685 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900686 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700687 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700688 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100689 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600690 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900691 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700692 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700693 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700694 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700695 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700696
697 /**
698 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
699 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600700 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900701 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700702 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700703 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600704 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900705 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700706 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700707 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700708 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700709 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700710
711 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600712 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
713 * instance.
714 *
715 * @hide
716 */
717 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
718 if (value) {
719 addTransportType(transportType);
720 } else {
721 removeTransportType(transportType);
722 }
723 return this;
724 }
725
726 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700727 * Gets all the transports set on this {@code NetworkCapability} instance.
728 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600729 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700730 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700731 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600732 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900733 @SystemApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600734 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900735 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700736 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700737
738 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600739 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700740 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600741 *
742 * @hide
743 */
744 public void setTransportTypes(@Transport int[] transportTypes) {
745 mTransportTypes = BitUtils.packBits(transportTypes);
746 }
747
748 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700749 * Tests for the presence of a transport on this instance.
750 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600751 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700752 * @return {@code true} if set on this instance.
753 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600754 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900755 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700756 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700757
758 private void combineTransportTypes(NetworkCapabilities nc) {
759 this.mTransportTypes |= nc.mTransportTypes;
760 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900761
Robert Greenwalt1448f052014-04-08 13:41:39 -0700762 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
763 return ((this.mTransportTypes == 0) ||
764 ((this.mTransportTypes & nc.mTransportTypes) != 0));
765 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900766
Robert Greenwalt06314e42014-10-29 14:04:06 -0700767 /** @hide */
768 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700769 return (nc.mTransportTypes == this.mTransportTypes);
770 }
771
772 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900773 * UID of the app that manages this network, or INVALID_UID if none/unknown.
774 *
775 * This field keeps track of the UID of the app that created this network and is in charge
776 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
777 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
778 * to create networks.
779 *
780 * Because this field is only used in the services side (and to avoid apps being able to
781 * set this to whatever they want), this field is not parcelled and will not be conserved
782 * across the IPC boundary.
783 * @hide
784 */
785 private int mEstablishingVpnAppUid = INVALID_UID;
786
787 /**
788 * Set the UID of the managing app.
789 * @hide
790 */
791 public void setEstablishingVpnAppUid(final int uid) {
792 mEstablishingVpnAppUid = uid;
793 }
794
795 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600796 * Value indicating that link bandwidth is unspecified.
797 * @hide
798 */
799 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
800
801 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700802 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
803 * for the first hop on the given transport. It is not measured, but may take into account
804 * link parameters (Radio technology, allocated channels, etc).
805 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600806 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
807 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700808
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700809 /**
810 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
811 * the estimated first hop transport bandwidth.
812 * <p>
813 * Note that when used to request a network, this specifies the minimum acceptable.
814 * When received as the state of an existing network this specifies the typical
815 * first hop bandwidth expected. This is never measured, but rather is inferred
816 * from technology type and other link parameters. It could be used to differentiate
817 * between very slow 1xRTT cellular links and other faster networks or even between
818 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
819 * fast backhauls and slow backhauls.
820 *
821 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700822 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700823 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600824 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700825 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600826 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700828
829 /**
830 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
831 * the estimated first hop transport bandwidth.
832 *
833 * @return The estimated first hop upstream (device to network) bandwidth.
834 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700835 public int getLinkUpstreamBandwidthKbps() {
836 return mLinkUpBandwidthKbps;
837 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700838
839 /**
840 * Sets the downstream 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 downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700852 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700853 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600854 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700855 mLinkDownBandwidthKbps = downKbps;
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 downstream 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 downstream (network to device) bandwidth.
864 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700865 public int getLinkDownstreamBandwidthKbps() {
866 return mLinkDownBandwidthKbps;
867 }
868
869 private void combineLinkBandwidths(NetworkCapabilities nc) {
870 this.mLinkUpBandwidthKbps =
871 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
872 this.mLinkDownBandwidthKbps =
873 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
874 }
875 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
876 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
877 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
878 }
879 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
880 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
881 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
882 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600883 /** @hide */
884 public static int minBandwidth(int a, int b) {
885 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
886 return b;
887 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
888 return a;
889 } else {
890 return Math.min(a, b);
891 }
892 }
893 /** @hide */
894 public static int maxBandwidth(int a, int b) {
895 return Math.max(a, b);
896 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700897
Etan Cohena7434272017-04-03 12:17:51 -0700898 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -0800899 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -0700900
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700901 /**
902 * Sets the optional bearer specific network specifier.
903 * This has no meaning if a single transport is also not specified, so calling
904 * this without a single transport set will generate an exception, as will
905 * subsequently adding or removing transports after this is set.
906 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700907 *
Etan Cohena7434272017-04-03 12:17:51 -0700908 * @param networkSpecifier A concrete, parcelable framework class that extends
909 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900910 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700911 * @hide
912 */
Etan Cohena7434272017-04-03 12:17:51 -0700913 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
914 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700915 throw new IllegalStateException("Must have a single transport specified to use " +
916 "setNetworkSpecifier");
917 }
Etan Cohena7434272017-04-03 12:17:51 -0700918
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700919 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700920
Pierre Imaic8419a82016-03-22 17:54:54 +0900921 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700922 }
923
924 /**
Etan Cohenca9fb562018-11-27 07:32:39 -0800925 * Sets the optional transport specific information.
926 *
927 * @param transportInfo A concrete, parcelable framework class that extends
928 * {@link TransportInfo}.
929 * @return This NetworkCapabilities instance, to facilitate chaining.
930 * @hide
931 */
932 public NetworkCapabilities setTransportInfo(TransportInfo transportInfo) {
933 mTransportInfo = transportInfo;
934 return this;
935 }
936
937 /**
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700938 * Gets the optional bearer specific network specifier.
939 *
Etan Cohena7434272017-04-03 12:17:51 -0700940 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
941 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700942 * @hide
943 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100944 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Etan Cohena7434272017-04-03 12:17:51 -0700945 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700946 return mNetworkSpecifier;
947 }
948
Etan Cohenca9fb562018-11-27 07:32:39 -0800949 /**
950 * Returns a transport-specific information container. The application may cast this
951 * container to a concrete sub-class based on its knowledge of the network request. The
952 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -0800953 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -0800954 *
955 * @return A concrete implementation of the {@link TransportInfo} class or null if not
956 * available for the network.
957 */
958 @Nullable public TransportInfo getTransportInfo() {
959 return mTransportInfo;
960 }
961
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700962 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700963 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700964 throw new IllegalStateException("Can't combine two networkSpecifiers");
965 }
Etan Cohena7434272017-04-03 12:17:51 -0700966 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700967 }
Etan Cohena7434272017-04-03 12:17:51 -0700968
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700969 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700970 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
971 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700972 }
Etan Cohena7434272017-04-03 12:17:51 -0700973
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700974 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700975 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700976 }
977
Etan Cohenca9fb562018-11-27 07:32:39 -0800978 private void combineTransportInfos(NetworkCapabilities nc) {
979 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
980 throw new IllegalStateException("Can't combine two TransportInfos");
981 }
982 setTransportInfo(nc.mTransportInfo);
983 }
984
985 private boolean equalsTransportInfo(NetworkCapabilities nc) {
986 return Objects.equals(mTransportInfo, nc.mTransportInfo);
987 }
988
Robert Greenwalt1448f052014-04-08 13:41:39 -0700989 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900990 * Magic value that indicates no signal strength provided. A request specifying this value is
991 * always satisfied.
992 *
993 * @hide
994 */
995 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
996
997 /**
998 * Signal strength. This is a signed integer, and higher values indicate better signal.
999 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1000 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001001 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001002 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001003
1004 /**
1005 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1006 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001007 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001008 * <p>
1009 * Note that when used to register a network callback, this specifies the minimum acceptable
1010 * signal strength. When received as the state of an existing network it specifies the current
1011 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1012 * effect when requesting a callback.
1013 *
1014 * @param signalStrength the bearer-specific signal strength.
1015 * @hide
1016 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001017 @UnsupportedAppUsage
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001018 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001019 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001020 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001021 }
1022
1023 /**
1024 * Returns {@code true} if this object specifies a signal strength.
1025 *
1026 * @hide
1027 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001028 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001029 public boolean hasSignalStrength() {
1030 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1031 }
1032
1033 /**
1034 * Retrieves the signal strength.
1035 *
1036 * @return The bearer-specific signal strength.
1037 * @hide
1038 */
paulhu7610bc72018-12-12 17:52:57 +08001039 @SystemApi
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001040 public int getSignalStrength() {
1041 return mSignalStrength;
1042 }
1043
1044 private void combineSignalStrength(NetworkCapabilities nc) {
1045 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1046 }
1047
1048 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1049 return this.mSignalStrength <= nc.mSignalStrength;
1050 }
1051
1052 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1053 return this.mSignalStrength == nc.mSignalStrength;
1054 }
1055
1056 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001057 * List of UIDs this network applies to. No restriction if null.
1058 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001059 * For networks, mUids represent the list of network this applies to, and null means this
1060 * network applies to all UIDs.
1061 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1062 * must be included in a network so that they match. As an exception to the general rule,
1063 * a null mUids field for requests mean "no requirements" rather than what the general rule
1064 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1065 * of this API expect in practice. A network that must match all UIDs can still be
1066 * expressed with a set ranging the entire set of possible UIDs.
1067 * <p>
1068 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001069 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1070 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1071 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1072 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001073 * As a special exception, the app managing this network (as identified by its UID stored in
1074 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1075 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1076 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001077 * <p>
1078 * Please note that in principle a single app can be associated with multiple UIDs because
1079 * each app will have a different UID when it's run as a different (macro-)user. A single
1080 * macro user can only have a single active VPN app at any given time however.
1081 * <p>
1082 * Also please be aware this class does not try to enforce any normalization on this. Callers
1083 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1084 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1085 * their own (like requiring sortedness or no overlap) they need to enforce it
1086 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1087 * or overlapping ranges are present.
1088 *
1089 * @hide
1090 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001091 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001092
1093 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001094 * Convenience method to set the UIDs this network applies to to a single UID.
1095 * @hide
1096 */
1097 public NetworkCapabilities setSingleUid(int uid) {
1098 final ArraySet<UidRange> identity = new ArraySet<>(1);
1099 identity.add(new UidRange(uid, uid));
1100 setUids(identity);
1101 return this;
1102 }
1103
1104 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001105 * Set the list of UIDs this network applies to.
1106 * This makes a copy of the set so that callers can't modify it after the call.
1107 * @hide
1108 */
1109 public NetworkCapabilities setUids(Set<UidRange> uids) {
1110 if (null == uids) {
1111 mUids = null;
1112 } else {
1113 mUids = new ArraySet<>(uids);
1114 }
1115 return this;
1116 }
1117
1118 /**
1119 * Get the list of UIDs this network applies to.
1120 * This returns a copy of the set so that callers can't modify the original object.
1121 * @hide
1122 */
1123 public Set<UidRange> getUids() {
1124 return null == mUids ? null : new ArraySet<>(mUids);
1125 }
1126
1127 /**
1128 * Test whether this network applies to this UID.
1129 * @hide
1130 */
1131 public boolean appliesToUid(int uid) {
1132 if (null == mUids) return true;
1133 for (UidRange range : mUids) {
1134 if (range.contains(uid)) {
1135 return true;
1136 }
1137 }
1138 return false;
1139 }
1140
1141 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001142 * 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 +09001143 * <p>
1144 * This test only checks whether equal range objects are in both sets. It will
1145 * return false if the ranges are not exactly the same, even if the covered UIDs
1146 * are for an equivalent result.
1147 * <p>
1148 * Note that this method is not very optimized, which is fine as long as it's not used very
1149 * often.
1150 * <p>
1151 * nc is assumed nonnull.
1152 *
1153 * @hide
1154 */
1155 @VisibleForTesting
1156 public boolean equalsUids(NetworkCapabilities nc) {
1157 Set<UidRange> comparedUids = nc.mUids;
1158 if (null == comparedUids) return null == mUids;
1159 if (null == mUids) return false;
1160 // Make a copy so it can be mutated to check that all ranges in mUids
1161 // also are in uids.
1162 final Set<UidRange> uids = new ArraySet<>(mUids);
1163 for (UidRange range : comparedUids) {
1164 if (!uids.contains(range)) {
1165 return false;
1166 }
1167 uids.remove(range);
1168 }
1169 return uids.isEmpty();
1170 }
1171
1172 /**
1173 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1174 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001175 * This method is called on the NetworkCapabilities embedded in a request with the
1176 * capabilities of an available network. It checks whether all the UIDs from this listen
1177 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1178 * in the passed nc (representing the UIDs that this network is available to).
1179 * <p>
1180 * As a special exception, the UID that created the passed network (as represented by its
1181 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1182 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1183 * can see its own network when it listens for it.
1184 * <p>
1185 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001186 * @see #appliesToUid
1187 * @hide
1188 */
1189 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001190 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001191 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001192 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001193 if (!nc.appliesToUidRange(requiredRange)) {
1194 return false;
1195 }
1196 }
1197 return true;
1198 }
1199
1200 /**
1201 * Returns whether this network applies to the passed ranges.
1202 * This assumes that to apply, the passed range has to be entirely contained
1203 * within one of the ranges this network applies to. If the ranges are not normalized,
1204 * this method may return false even though all required UIDs are covered because no
1205 * single range contained them all.
1206 * @hide
1207 */
1208 @VisibleForTesting
1209 public boolean appliesToUidRange(UidRange requiredRange) {
1210 if (null == mUids) return true;
1211 for (UidRange uidRange : mUids) {
1212 if (uidRange.containsRange(requiredRange)) {
1213 return true;
1214 }
1215 }
1216 return false;
1217 }
1218
1219 /**
1220 * Combine the UIDs this network currently applies to with the UIDs the passed
1221 * NetworkCapabilities apply to.
1222 * nc is assumed nonnull.
1223 */
1224 private void combineUids(NetworkCapabilities nc) {
1225 if (null == nc.mUids || null == mUids) {
1226 mUids = null;
1227 return;
1228 }
1229 mUids.addAll(nc.mUids);
1230 }
1231
Chalard Jeanb03a6222018-04-11 21:09:10 +09001232
1233 /**
1234 * The SSID of the network, or null if not applicable or unknown.
1235 * <p>
1236 * This is filled in by wifi code.
1237 * @hide
1238 */
1239 private String mSSID;
1240
1241 /**
1242 * Sets the SSID of this network.
1243 * @hide
1244 */
1245 public NetworkCapabilities setSSID(String ssid) {
1246 mSSID = ssid;
1247 return this;
1248 }
1249
1250 /**
1251 * Gets the SSID of this network, or null if none or unknown.
1252 * @hide
1253 */
1254 public String getSSID() {
1255 return mSSID;
1256 }
1257
1258 /**
1259 * Tests if the SSID of this network is the same as the SSID of the passed network.
1260 * @hide
1261 */
1262 public boolean equalsSSID(NetworkCapabilities nc) {
1263 return Objects.equals(mSSID, nc.mSSID);
1264 }
1265
1266 /**
1267 * Check if the SSID requirements of this object are matched by the passed object.
1268 * @hide
1269 */
1270 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1271 return mSSID == null || mSSID.equals(nc.mSSID);
1272 }
1273
1274 /**
1275 * Combine SSIDs of the capabilities.
1276 * <p>
1277 * This is only legal if either the SSID of this object is null, or both SSIDs are
1278 * equal.
1279 * @hide
1280 */
1281 private void combineSSIDs(NetworkCapabilities nc) {
1282 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1283 throw new IllegalStateException("Can't combine two SSIDs");
1284 }
1285 setSSID(nc.mSSID);
1286 }
1287
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001288 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001289 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1290 * <p>
1291 * Note that this method may break an invariant of having a particular capability in either
1292 * wanted or unwanted lists but never in both. Requests that have the same capability in
1293 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001294 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001295 */
1296 public void combineCapabilities(NetworkCapabilities nc) {
1297 combineNetCapabilities(nc);
1298 combineTransportTypes(nc);
1299 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001300 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001301 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001302 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001303 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001304 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001305 }
1306
1307 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001308 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1309 *
1310 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1311 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1312 * bandwidth, signal strength, or validation / captive portal status.
1313 *
1314 * @hide
1315 */
1316 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001317 return (nc != null
1318 && satisfiedByNetCapabilities(nc, onlyImmutable)
1319 && satisfiedByTransportTypes(nc)
1320 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1321 && satisfiedBySpecifier(nc)
1322 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001323 && (onlyImmutable || satisfiedByUids(nc))
1324 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001325 }
1326
1327 /**
1328 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1329 *
1330 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1331 *
1332 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001333 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001334 @TestApi
1335 @SystemApi
Robert Greenwalt1448f052014-04-08 13:41:39 -07001336 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001337 return satisfiedByNetworkCapabilities(nc, false);
1338 }
1339
1340 /**
1341 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1342 *
1343 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1344 *
1345 * @hide
1346 */
1347 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1348 return satisfiedByNetworkCapabilities(nc, true);
1349 }
1350
1351 /**
1352 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001353 * {@code NetworkCapabilities} and return a String describing any difference.
1354 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001355 *
1356 * @hide
1357 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001358 public String describeImmutableDifferences(NetworkCapabilities that) {
1359 if (that == null) {
1360 return "other NetworkCapabilities was null";
1361 }
1362
1363 StringJoiner joiner = new StringJoiner(", ");
1364
Hugo Benichieae7a222017-07-25 11:40:56 +09001365 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1366 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001367 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001368 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1369 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1370 if (oldImmutableCapabilities != newImmutableCapabilities) {
1371 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1372 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1373 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1374 }
1375
1376 if (!equalsSpecifier(that)) {
1377 NetworkSpecifier before = this.getNetworkSpecifier();
1378 NetworkSpecifier after = that.getNetworkSpecifier();
1379 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1380 }
1381
1382 if (!equalsTransportTypes(that)) {
1383 String before = transportNamesOf(this.getTransportTypes());
1384 String after = transportNamesOf(that.getTransportTypes());
1385 joiner.add(String.format("transports changed: %s -> %s", before, after));
1386 }
1387
1388 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001389 }
1390
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001391 /**
1392 * Checks that our requestable capabilities are the same as those of the given
1393 * {@code NetworkCapabilities}.
1394 *
1395 * @hide
1396 */
1397 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1398 if (nc == null) return false;
1399 return (equalsNetCapabilitiesRequestable(nc) &&
1400 equalsTransportTypes(nc) &&
1401 equalsSpecifier(nc));
1402 }
1403
Robert Greenwalt1448f052014-04-08 13:41:39 -07001404 @Override
1405 public boolean equals(Object obj) {
1406 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001407 NetworkCapabilities that = (NetworkCapabilities) obj;
1408 return (equalsNetCapabilities(that)
1409 && equalsTransportTypes(that)
1410 && equalsLinkBandwidths(that)
1411 && equalsSignalStrength(that)
1412 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001413 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001414 && equalsUids(that)
1415 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001416 }
1417
1418 @Override
1419 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001420 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001421 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001422 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1423 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1424 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1425 + ((int) (mTransportTypes >> 32) * 13)
1426 + (mLinkUpBandwidthKbps * 17)
1427 + (mLinkDownBandwidthKbps * 19)
1428 + Objects.hashCode(mNetworkSpecifier) * 23
1429 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001430 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001431 + Objects.hashCode(mSSID) * 37
1432 + Objects.hashCode(mTransportInfo) * 41;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001433 }
1434
Wink Saville4e2dea72014-09-20 11:04:03 -07001435 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001436 public int describeContents() {
1437 return 0;
1438 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001439 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001440 public void writeToParcel(Parcel dest, int flags) {
1441 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001442 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001443 dest.writeLong(mTransportTypes);
1444 dest.writeInt(mLinkUpBandwidthKbps);
1445 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001446 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001447 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001448 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001449 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001450 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001451 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001452
Robert Greenwalt1448f052014-04-08 13:41:39 -07001453 public static final Creator<NetworkCapabilities> CREATOR =
1454 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001455 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001456 public NetworkCapabilities createFromParcel(Parcel in) {
1457 NetworkCapabilities netCap = new NetworkCapabilities();
1458
1459 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001460 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001461 netCap.mTransportTypes = in.readLong();
1462 netCap.mLinkUpBandwidthKbps = in.readInt();
1463 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001464 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001465 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001466 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001467 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1468 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001469 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001470 return netCap;
1471 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001472 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001473 public NetworkCapabilities[] newArray(int size) {
1474 return new NetworkCapabilities[size];
1475 }
1476 };
1477
Wink Saville4e2dea72014-09-20 11:04:03 -07001478 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001479 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001480 final StringBuilder sb = new StringBuilder("[");
1481 if (0 != mTransportTypes) {
1482 sb.append(" Transports: ");
1483 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1484 NetworkCapabilities::transportNameOf, "|");
1485 }
1486 if (0 != mNetworkCapabilities) {
1487 sb.append(" Capabilities: ");
1488 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1489 NetworkCapabilities::capabilityNameOf, "&");
1490 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001491 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001492 sb.append(" Unwanted: ");
1493 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1494 NetworkCapabilities::capabilityNameOf, "&");
1495 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001496 if (mLinkUpBandwidthKbps > 0) {
1497 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1498 }
1499 if (mLinkDownBandwidthKbps > 0) {
1500 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1501 }
1502 if (mNetworkSpecifier != null) {
1503 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1504 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001505 if (mTransportInfo != null) {
1506 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1507 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001508 if (hasSignalStrength()) {
1509 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001510 }
1511
Chalard Jean07ace0f2018-02-26 19:00:45 +09001512 if (null != mUids) {
1513 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1514 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1515 } else {
1516 sb.append(" Uids: <").append(mUids).append(">");
1517 }
1518 }
1519 if (mEstablishingVpnAppUid != INVALID_UID) {
1520 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1521 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001522
Chalard Jeanb03a6222018-04-11 21:09:10 +09001523 if (null != mSSID) {
1524 sb.append(" SSID: ").append(mSSID);
1525 }
1526
Chalard Jean07ace0f2018-02-26 19:00:45 +09001527 sb.append("]");
1528 return sb.toString();
1529 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001530
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001531
Chalard Jean07ace0f2018-02-26 19:00:45 +09001532 private interface NameOf {
1533 String nameOf(int value);
1534 }
1535 /**
1536 * @hide
1537 */
1538 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1539 long bitMask, NameOf nameFetcher, String separator) {
1540 int bitPos = 0;
1541 boolean firstElementAdded = false;
1542 while (bitMask != 0) {
1543 if ((bitMask & 1) != 0) {
1544 if (firstElementAdded) {
1545 sb.append(separator);
1546 } else {
1547 firstElementAdded = true;
1548 }
1549 sb.append(nameFetcher.nameOf(bitPos));
1550 }
1551 bitMask >>= 1;
1552 ++bitPos;
1553 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001554 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001555
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001556 /** @hide */
1557 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1558 final long token = proto.start(fieldId);
1559
1560 for (int transport : getTransportTypes()) {
1561 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1562 }
1563
1564 for (int capability : getCapabilities()) {
1565 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1566 }
1567
1568 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1569 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1570
1571 if (mNetworkSpecifier != null) {
1572 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1573 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001574 if (mTransportInfo != null) {
1575 // TODO b/120653863: write transport-specific info to proto?
1576 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001577
1578 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1579 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1580
1581 proto.end(token);
1582 }
1583
Hugo Benichi5df9d722016-04-25 17:16:35 +09001584 /**
1585 * @hide
1586 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001587 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001588 StringJoiner joiner = new StringJoiner("|");
1589 if (capabilities != null) {
1590 for (int c : capabilities) {
1591 joiner.add(capabilityNameOf(c));
1592 }
1593 }
1594 return joiner.toString();
1595 }
1596
1597 /**
1598 * @hide
1599 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001600 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001601 switch (capability) {
1602 case NET_CAPABILITY_MMS: return "MMS";
1603 case NET_CAPABILITY_SUPL: return "SUPL";
1604 case NET_CAPABILITY_DUN: return "DUN";
1605 case NET_CAPABILITY_FOTA: return "FOTA";
1606 case NET_CAPABILITY_IMS: return "IMS";
1607 case NET_CAPABILITY_CBS: return "CBS";
1608 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1609 case NET_CAPABILITY_IA: return "IA";
1610 case NET_CAPABILITY_RCS: return "RCS";
1611 case NET_CAPABILITY_XCAP: return "XCAP";
1612 case NET_CAPABILITY_EIMS: return "EIMS";
1613 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1614 case NET_CAPABILITY_INTERNET: return "INTERNET";
1615 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1616 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1617 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1618 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1619 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001620 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001621 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001622 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001623 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001624 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Amit Mahajanfd3ee572019-02-20 15:04:30 -08001625 case NET_CAPABILITY_MCX: return "MCX";
Hugo Benichieae7a222017-07-25 11:40:56 +09001626 default: return Integer.toString(capability);
1627 }
1628 }
1629
1630 /**
1631 * @hide
1632 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001633 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -06001634 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001635 StringJoiner joiner = new StringJoiner("|");
1636 if (types != null) {
1637 for (int t : types) {
1638 joiner.add(transportNameOf(t));
1639 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001640 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001641 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001642 }
1643
1644 /**
1645 * @hide
1646 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001647 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001648 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001649 return "UNKNOWN";
1650 }
1651 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001652 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001653
Jeff Sharkeyde570312017-10-24 21:25:50 -06001654 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001655 Preconditions.checkArgument(
1656 isValidTransport(transport), "Invalid TransportType " + transport);
1657 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001658
1659 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1660 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1661 }
1662
1663 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1664 Preconditions.checkArgument(isValidCapability(capability),
1665 "NetworkCapability " + capability + "out of range");
1666 }
junyulai05986c62018-08-07 19:50:45 +08001667
1668 /**
1669 * Check if this {@code NetworkCapability} instance is metered.
1670 *
1671 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1672 * @hide
1673 */
1674 public boolean isMetered() {
1675 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1676 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001677}