blob: a17ebcbedd1aca2e89d591a1a9718bc57d5e3991 [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,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600146 })
147 public @interface NetCapability { }
148
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150 * Indicates this is a network that has the ability to reach the
151 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700152 */
153 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700154
155 /**
156 * Indicates this is a network that has the ability to reach the carrier's
157 * SUPL server, used to retrieve GPS information.
158 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700159 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700160
161 /**
162 * Indicates this is a network that has the ability to reach the carrier's
163 * DUN or tethering gateway.
164 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700165 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700166
167 /**
168 * Indicates this is a network that has the ability to reach the carrier's
169 * FOTA portal, used for over the air updates.
170 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700171 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700172
173 /**
174 * Indicates this is a network that has the ability to reach the carrier's
175 * IMS servers, used for network registration and signaling.
176 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700177 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178
179 /**
180 * Indicates this is a network that has the ability to reach the carrier's
181 * CBS servers, used for carrier specific services.
182 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700183 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700184
185 /**
186 * Indicates this is a network that has the ability to reach a Wi-Fi direct
187 * peer.
188 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700189 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190
191 /**
192 * Indicates this is a network that has the ability to reach a carrier's
193 * Initial Attach servers.
194 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700195 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700196
197 /**
198 * Indicates this is a network that has the ability to reach a carrier's
199 * RCS servers, used for Rich Communication Services.
200 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700201 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700202
203 /**
204 * Indicates this is a network that has the ability to reach a carrier's
205 * XCAP servers, used for configuration and control.
206 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700207 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700208
209 /**
210 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700211 * Emergency IMS servers or other services, used for network signaling
212 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700213 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700214 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700215
216 /**
217 * Indicates that this network is unmetered.
218 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700219 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700220
221 /**
222 * Indicates that this network should be able to reach the internet.
223 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700224 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700225
226 /**
227 * Indicates that this network is available for general use. If this is not set
228 * applications should not attempt to communicate on this network. Note that this
229 * is simply informative and not enforcement - enforcement is handled via other means.
230 * Set by default.
231 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700232 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
233
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700234 /**
235 * Indicates that the user has indicated implicit trust of this network. This
236 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
237 * BT device or a wifi the user asked to connect to. Untrusted networks
238 * are probably limited to unknown wifi AP. Set by default.
239 */
240 public static final int NET_CAPABILITY_TRUSTED = 14;
241
Paul Jensen76b610a2015-03-18 09:33:07 -0400242 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400243 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400244 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400245 */
246 public static final int NET_CAPABILITY_NOT_VPN = 15;
247
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900248 /**
249 * Indicates that connectivity on this network was successfully validated. For example, for a
250 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
251 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900252 */
253 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700254
Paul Jensen3d194ea2015-06-16 14:27:36 -0400255 /**
256 * Indicates that this network was found to have a captive portal in place last time it was
257 * probed.
258 */
259 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
260
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900261 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600262 * Indicates that this network is not roaming.
263 */
264 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
265
266 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900267 * Indicates that this network is available for use by apps, and not a network that is being
268 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900269 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600270 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900271
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900272 /**
273 * Indicates that this network is not congested.
274 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600275 * When a network is congested, applications should defer network traffic
276 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900277 */
278 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
279
Chalard Jean804b8fb2018-01-30 22:41:41 +0900280 /**
281 * Indicates that this network is not currently suspended.
282 * <p>
283 * When a network is suspended, the network's IP addresses and any connections
284 * established on the network remain valid, but the network is temporarily unable
285 * to transfer data. This can happen, for example, if a cellular network experiences
286 * a temporary loss of signal, such as when driving through a tunnel, etc.
287 * A network with this capability is not suspended, so is expected to be able to
288 * transfer data.
289 */
290 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
291
Pavel Maltsev43403202018-01-30 17:19:44 -0800292 /**
293 * Indicates that traffic that goes through this network is paid by oem. For example,
294 * this network can be used by system apps to upload telemetry data.
295 * @hide
296 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700297 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800298 public static final int NET_CAPABILITY_OEM_PAID = 22;
299
Robert Greenwalt1448f052014-04-08 13:41:39 -0700300 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800301 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700302
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700303 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900304 * Network capabilities that are expected to be mutable, i.e., can change while a particular
305 * network is connected.
306 */
307 private static final long MUTABLE_CAPABILITIES =
308 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
309 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900310 (1 << NET_CAPABILITY_TRUSTED)
311 | (1 << NET_CAPABILITY_VALIDATED)
312 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
313 | (1 << NET_CAPABILITY_NOT_ROAMING)
314 | (1 << NET_CAPABILITY_FOREGROUND)
315 | (1 << NET_CAPABILITY_NOT_CONGESTED)
316 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900317
318 /**
319 * Network capabilities that are not allowed in NetworkRequests. This exists because the
320 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
321 * capability's presence cannot be known in advance. If such a capability is requested, then we
322 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
323 * get immediately torn down because they do not have the requested capability.
324 */
325 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900326 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900327
328 /**
329 * Capabilities that are set by default when the object is constructed.
330 */
331 private static final long DEFAULT_CAPABILITIES =
332 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
333 (1 << NET_CAPABILITY_TRUSTED) |
334 (1 << NET_CAPABILITY_NOT_VPN);
335
336 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400337 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800338 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400339 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700340 @VisibleForTesting
341 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400342 (1 << NET_CAPABILITY_CBS) |
343 (1 << NET_CAPABILITY_DUN) |
344 (1 << NET_CAPABILITY_EIMS) |
345 (1 << NET_CAPABILITY_FOTA) |
346 (1 << NET_CAPABILITY_IA) |
347 (1 << NET_CAPABILITY_IMS) |
348 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev4af91072018-03-07 14:33:22 -0800349 (1 << NET_CAPABILITY_XCAP);
350
351 /**
352 * Capabilities that force network to be restricted.
353 * {@see #maybeMarkCapabilitiesRestricted}.
354 */
355 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800356 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400357
358 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700359 * Capabilities that suggest that a network is unrestricted.
360 * {@see #maybeMarkCapabilitiesRestricted}.
361 */
362 @VisibleForTesting
363 /* package */ static final long UNRESTRICTED_CAPABILITIES =
364 (1 << NET_CAPABILITY_INTERNET) |
365 (1 << NET_CAPABILITY_MMS) |
366 (1 << NET_CAPABILITY_SUPL) |
367 (1 << NET_CAPABILITY_WIFI_P2P);
368
369 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700370 * Adds the given capability to this {@code NetworkCapability} instance.
371 * Multiple capabilities may be applied sequentially. Note that when searching
372 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800373 * <p>
374 * If the given capability was previously added to the list of unwanted capabilities
375 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700376 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600377 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900378 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700379 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700380 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100381 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600382 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800383 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700384 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800385 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700386 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700387 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700388
389 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800390 * Adds the given capability to the list of unwanted capabilities of this
391 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
392 * sequentially. Note that when searching for a network to satisfy a request, the network
393 * must not contain any capability from unwanted capability list.
394 * <p>
395 * If the capability was previously added to the list of required capabilities (for
396 * example, it was there by default or added using {@link #addCapability(int)} method), then
397 * it will be removed from the list of required capabilities as well.
398 *
399 * @see #addCapability(int)
400 * @hide
401 */
402 public void addUnwantedCapability(@NetCapability int capability) {
403 checkValidCapability(capability);
404 mUnwantedNetworkCapabilities |= 1 << capability;
405 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
406 }
407
408 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700409 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800410 * <p>
Pavel Maltseve18ef262018-03-07 11:13:04 -0800411 * Note that this method removes capabilities that were added via {@link #addCapability(int)},
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800412 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700413 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600414 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900415 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700416 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700417 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100418 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600419 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800420 checkValidCapability(capability);
421 final long mask = ~(1 << capability);
422 mNetworkCapabilities &= mask;
423 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700424 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700425 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700426
427 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600428 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
429 * instance.
430 *
431 * @hide
432 */
433 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
434 if (value) {
435 addCapability(capability);
436 } else {
437 removeCapability(capability);
438 }
439 return this;
440 }
441
442 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700443 * Gets all the capabilities set on this {@code NetworkCapability} instance.
444 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600445 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700446 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700447 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600448 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600449 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900450 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700451 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452
453 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800454 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
455 *
456 * @return an array of unwanted capability values for this instance.
457 * @hide
458 */
459 public @NetCapability int[] getUnwantedCapabilities() {
460 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
461 }
462
463
464 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600465 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700466 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600467 *
468 * @hide
469 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800470 public void setCapabilities(@NetCapability int[] capabilities,
471 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600472 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800473 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600474 }
475
476 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800477 * @deprecated use {@link #setCapabilities(int[], int[])}
478 * @hide
479 */
480 @Deprecated
481 public void setCapabilities(@NetCapability int[] capabilities) {
482 setCapabilities(capabilities, new int[] {});
483 }
484
485 /**
486 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700487 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600488 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700489 * @return {@code true} if set on this instance.
490 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600491 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800492 return isValidCapability(capability)
493 && ((mNetworkCapabilities & (1 << capability)) != 0);
494 }
495
496 /** @hide */
497 public boolean hasUnwantedCapability(@NetCapability int capability) {
498 return isValidCapability(capability)
499 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700500 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700501
Pavel Maltseve18ef262018-03-07 11:13:04 -0800502 /** Note this method may result in having the same capability in wanted and unwanted lists. */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700503 private void combineNetCapabilities(NetworkCapabilities nc) {
504 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800505 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700506 }
507
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900508 /**
509 * Convenience function that returns a human-readable description of the first mutable
510 * capability we find. Used to present an error message to apps that request mutable
511 * capabilities.
512 *
513 * @hide
514 */
515 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800516 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
517 & NON_REQUESTABLE_CAPABILITIES;
518
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900519 if (nonRequestable != 0) {
520 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900521 }
522 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900523 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900524 return null;
525 }
526
527 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800528 long requestedCapabilities = mNetworkCapabilities;
529 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
530 long providedCapabilities = nc.mNetworkCapabilities;
531
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900532 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800533 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
534 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900535 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800536 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
537 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700538 }
539
Robert Greenwalt06314e42014-10-29 14:04:06 -0700540 /** @hide */
541 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800542 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
543 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700544 }
545
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900546 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
547 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800548 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
549 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
550 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900551 }
552
Robert Greenwalt1448f052014-04-08 13:41:39 -0700553 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400554 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
555 * typically provided by restricted networks.
556 *
557 * TODO: consider:
558 * - Renaming it to guessRestrictedCapability and make it set the
559 * restricted capability bit in addition to clearing it.
560 * @hide
561 */
562 public void maybeMarkCapabilitiesRestricted() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800563 // Check if we have any capability that forces the network to be restricted.
564 final boolean forceRestrictedCapability =
565 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
566
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700567 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800568 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700569 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800570 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700571
572 // Must have at least some restricted capabilities.
573 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800574 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700575
Pavel Maltsev4af91072018-03-07 14:33:22 -0800576 if (forceRestrictedCapability
577 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400578 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400579 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400580 }
581
582 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700583 * Representing the transport type. Apps should generally not care about transport. A
584 * request for a fast internet connection could be satisfied by a number of different
585 * transports. If any are specified here it will be satisfied a Network that matches
586 * any of them. If a caller doesn't care about the transport it should not specify any.
587 */
588 private long mTransportTypes;
589
Jeff Sharkeyde570312017-10-24 21:25:50 -0600590 /** @hide */
591 @Retention(RetentionPolicy.SOURCE)
592 @IntDef(prefix = { "TRANSPORT_" }, value = {
593 TRANSPORT_CELLULAR,
594 TRANSPORT_WIFI,
595 TRANSPORT_BLUETOOTH,
596 TRANSPORT_ETHERNET,
597 TRANSPORT_VPN,
598 TRANSPORT_WIFI_AWARE,
599 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800600 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600601 })
602 public @interface Transport { }
603
Robert Greenwalt1448f052014-04-08 13:41:39 -0700604 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700605 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606 */
607 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700608
609 /**
610 * Indicates this network uses a Wi-Fi transport.
611 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700613
614 /**
615 * Indicates this network uses a Bluetooth transport.
616 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700617 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700618
619 /**
620 * Indicates this network uses an Ethernet transport.
621 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700622 public static final int TRANSPORT_ETHERNET = 3;
623
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400624 /**
625 * Indicates this network uses a VPN transport.
626 */
627 public static final int TRANSPORT_VPN = 4;
628
Etan Cohen305ea282016-06-20 09:27:12 -0700629 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700630 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700631 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700632 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700633
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700634 /**
635 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700636 */
637 public static final int TRANSPORT_LOWPAN = 6;
638
Benedict Wong89ce5e32018-11-14 17:40:55 -0800639 /**
640 * Indicates this network uses a Test-only virtual interface as a transport.
641 *
642 * @hide
643 */
644 @TestApi
645 public static final int TRANSPORT_TEST = 7;
646
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900647 /** @hide */
648 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
649 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800650 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700651
Hugo Benichi16f0a942017-06-20 14:07:59 +0900652 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600653 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900654 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
655 }
656
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900657 private static final String[] TRANSPORT_NAMES = {
658 "CELLULAR",
659 "WIFI",
660 "BLUETOOTH",
661 "ETHERNET",
662 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700663 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800664 "LOWPAN",
665 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900666 };
667
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700668 /**
669 * Adds the given transport type to this {@code NetworkCapability} instance.
670 * Multiple transports may be applied sequentially. Note that when searching
671 * for a network to satisfy a request, any listed in the request will satisfy the request.
672 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
673 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
674 * to be selected. This is logically different than
675 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
676 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600677 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900678 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700679 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700680 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100681 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600682 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900683 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700684 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700685 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700686 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700687 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700688
689 /**
690 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
691 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600692 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900693 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700694 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700695 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600696 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900697 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700698 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700699 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700700 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700701 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700702
703 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600704 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
705 * instance.
706 *
707 * @hide
708 */
709 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
710 if (value) {
711 addTransportType(transportType);
712 } else {
713 removeTransportType(transportType);
714 }
715 return this;
716 }
717
718 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700719 * Gets all the transports set on this {@code NetworkCapability} instance.
720 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600721 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700722 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700723 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600724 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900725 @SystemApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600726 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900727 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700728 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700729
730 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600731 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700732 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600733 *
734 * @hide
735 */
736 public void setTransportTypes(@Transport int[] transportTypes) {
737 mTransportTypes = BitUtils.packBits(transportTypes);
738 }
739
740 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700741 * Tests for the presence of a transport on this instance.
742 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600743 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700744 * @return {@code true} if set on this instance.
745 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600746 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900747 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700748 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700749
750 private void combineTransportTypes(NetworkCapabilities nc) {
751 this.mTransportTypes |= nc.mTransportTypes;
752 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900753
Robert Greenwalt1448f052014-04-08 13:41:39 -0700754 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
755 return ((this.mTransportTypes == 0) ||
756 ((this.mTransportTypes & nc.mTransportTypes) != 0));
757 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900758
Robert Greenwalt06314e42014-10-29 14:04:06 -0700759 /** @hide */
760 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700761 return (nc.mTransportTypes == this.mTransportTypes);
762 }
763
764 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900765 * UID of the app that manages this network, or INVALID_UID if none/unknown.
766 *
767 * This field keeps track of the UID of the app that created this network and is in charge
768 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
769 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
770 * to create networks.
771 *
772 * Because this field is only used in the services side (and to avoid apps being able to
773 * set this to whatever they want), this field is not parcelled and will not be conserved
774 * across the IPC boundary.
775 * @hide
776 */
777 private int mEstablishingVpnAppUid = INVALID_UID;
778
779 /**
780 * Set the UID of the managing app.
781 * @hide
782 */
783 public void setEstablishingVpnAppUid(final int uid) {
784 mEstablishingVpnAppUid = uid;
785 }
786
787 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600788 * Value indicating that link bandwidth is unspecified.
789 * @hide
790 */
791 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
792
793 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700794 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
795 * for the first hop on the given transport. It is not measured, but may take into account
796 * link parameters (Radio technology, allocated channels, etc).
797 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600798 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
799 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700800
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700801 /**
802 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
803 * the estimated first hop transport bandwidth.
804 * <p>
805 * Note that when used to request a network, this specifies the minimum acceptable.
806 * When received as the state of an existing network this specifies the typical
807 * first hop bandwidth expected. This is never measured, but rather is inferred
808 * from technology type and other link parameters. It could be used to differentiate
809 * between very slow 1xRTT cellular links and other faster networks or even between
810 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
811 * fast backhauls and slow backhauls.
812 *
813 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700814 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700815 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600816 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700817 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600818 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700819 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700820
821 /**
822 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
823 * the estimated first hop transport bandwidth.
824 *
825 * @return The estimated first hop upstream (device to network) bandwidth.
826 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 public int getLinkUpstreamBandwidthKbps() {
828 return mLinkUpBandwidthKbps;
829 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700830
831 /**
832 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
833 * the estimated first hop transport bandwidth.
834 * <p>
835 * Note that when used to request a network, this specifies the minimum acceptable.
836 * When received as the state of an existing network this specifies the typical
837 * first hop bandwidth expected. This is never measured, but rather is inferred
838 * from technology type and other link parameters. It could be used to differentiate
839 * between very slow 1xRTT cellular links and other faster networks or even between
840 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
841 * fast backhauls and slow backhauls.
842 *
843 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700844 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700845 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600846 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700847 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600848 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700849 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700850
851 /**
852 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
853 * the estimated first hop transport bandwidth.
854 *
855 * @return The estimated first hop downstream (network to device) bandwidth.
856 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700857 public int getLinkDownstreamBandwidthKbps() {
858 return mLinkDownBandwidthKbps;
859 }
860
861 private void combineLinkBandwidths(NetworkCapabilities nc) {
862 this.mLinkUpBandwidthKbps =
863 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
864 this.mLinkDownBandwidthKbps =
865 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
866 }
867 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
868 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
869 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
870 }
871 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
872 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
873 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
874 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600875 /** @hide */
876 public static int minBandwidth(int a, int b) {
877 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
878 return b;
879 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
880 return a;
881 } else {
882 return Math.min(a, b);
883 }
884 }
885 /** @hide */
886 public static int maxBandwidth(int a, int b) {
887 return Math.max(a, b);
888 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700889
Etan Cohena7434272017-04-03 12:17:51 -0700890 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -0800891 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -0700892
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 /**
894 * Sets the optional bearer specific network specifier.
895 * This has no meaning if a single transport is also not specified, so calling
896 * this without a single transport set will generate an exception, as will
897 * subsequently adding or removing transports after this is set.
898 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700899 *
Etan Cohena7434272017-04-03 12:17:51 -0700900 * @param networkSpecifier A concrete, parcelable framework class that extends
901 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900902 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700903 * @hide
904 */
Etan Cohena7434272017-04-03 12:17:51 -0700905 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
906 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700907 throw new IllegalStateException("Must have a single transport specified to use " +
908 "setNetworkSpecifier");
909 }
Etan Cohena7434272017-04-03 12:17:51 -0700910
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700911 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700912
Pierre Imaic8419a82016-03-22 17:54:54 +0900913 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700914 }
915
916 /**
Etan Cohenca9fb562018-11-27 07:32:39 -0800917 * Sets the optional transport specific information.
918 *
919 * @param transportInfo A concrete, parcelable framework class that extends
920 * {@link TransportInfo}.
921 * @return This NetworkCapabilities instance, to facilitate chaining.
922 * @hide
923 */
924 public NetworkCapabilities setTransportInfo(TransportInfo transportInfo) {
925 mTransportInfo = transportInfo;
926 return this;
927 }
928
929 /**
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700930 * Gets the optional bearer specific network specifier.
931 *
Etan Cohena7434272017-04-03 12:17:51 -0700932 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
933 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700934 * @hide
935 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100936 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Etan Cohena7434272017-04-03 12:17:51 -0700937 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700938 return mNetworkSpecifier;
939 }
940
Etan Cohenca9fb562018-11-27 07:32:39 -0800941 /**
942 * Returns a transport-specific information container. The application may cast this
943 * container to a concrete sub-class based on its knowledge of the network request. The
944 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -0800945 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -0800946 *
947 * @return A concrete implementation of the {@link TransportInfo} class or null if not
948 * available for the network.
949 */
950 @Nullable public TransportInfo getTransportInfo() {
951 return mTransportInfo;
952 }
953
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700954 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700955 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700956 throw new IllegalStateException("Can't combine two networkSpecifiers");
957 }
Etan Cohena7434272017-04-03 12:17:51 -0700958 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700959 }
Etan Cohena7434272017-04-03 12:17:51 -0700960
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700961 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700962 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
963 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700964 }
Etan Cohena7434272017-04-03 12:17:51 -0700965
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700966 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700967 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700968 }
969
Etan Cohenca9fb562018-11-27 07:32:39 -0800970 private void combineTransportInfos(NetworkCapabilities nc) {
971 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
972 throw new IllegalStateException("Can't combine two TransportInfos");
973 }
974 setTransportInfo(nc.mTransportInfo);
975 }
976
977 private boolean equalsTransportInfo(NetworkCapabilities nc) {
978 return Objects.equals(mTransportInfo, nc.mTransportInfo);
979 }
980
Robert Greenwalt1448f052014-04-08 13:41:39 -0700981 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900982 * Magic value that indicates no signal strength provided. A request specifying this value is
983 * always satisfied.
984 *
985 * @hide
986 */
987 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
988
989 /**
990 * Signal strength. This is a signed integer, and higher values indicate better signal.
991 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
992 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100993 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700994 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900995
996 /**
997 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
998 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900999 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001000 * <p>
1001 * Note that when used to register a network callback, this specifies the minimum acceptable
1002 * signal strength. When received as the state of an existing network it specifies the current
1003 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1004 * effect when requesting a callback.
1005 *
1006 * @param signalStrength the bearer-specific signal strength.
1007 * @hide
1008 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001009 @UnsupportedAppUsage
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001010 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001011 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001012 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001013 }
1014
1015 /**
1016 * Returns {@code true} if this object specifies a signal strength.
1017 *
1018 * @hide
1019 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001020 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001021 public boolean hasSignalStrength() {
1022 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1023 }
1024
1025 /**
1026 * Retrieves the signal strength.
1027 *
1028 * @return The bearer-specific signal strength.
1029 * @hide
1030 */
paulhu7610bc72018-12-12 17:52:57 +08001031 @SystemApi
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001032 public int getSignalStrength() {
1033 return mSignalStrength;
1034 }
1035
1036 private void combineSignalStrength(NetworkCapabilities nc) {
1037 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1038 }
1039
1040 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1041 return this.mSignalStrength <= nc.mSignalStrength;
1042 }
1043
1044 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1045 return this.mSignalStrength == nc.mSignalStrength;
1046 }
1047
1048 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001049 * List of UIDs this network applies to. No restriction if null.
1050 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001051 * For networks, mUids represent the list of network this applies to, and null means this
1052 * network applies to all UIDs.
1053 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1054 * must be included in a network so that they match. As an exception to the general rule,
1055 * a null mUids field for requests mean "no requirements" rather than what the general rule
1056 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1057 * of this API expect in practice. A network that must match all UIDs can still be
1058 * expressed with a set ranging the entire set of possible UIDs.
1059 * <p>
1060 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001061 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1062 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1063 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1064 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001065 * As a special exception, the app managing this network (as identified by its UID stored in
1066 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1067 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1068 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001069 * <p>
1070 * Please note that in principle a single app can be associated with multiple UIDs because
1071 * each app will have a different UID when it's run as a different (macro-)user. A single
1072 * macro user can only have a single active VPN app at any given time however.
1073 * <p>
1074 * Also please be aware this class does not try to enforce any normalization on this. Callers
1075 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1076 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1077 * their own (like requiring sortedness or no overlap) they need to enforce it
1078 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1079 * or overlapping ranges are present.
1080 *
1081 * @hide
1082 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001083 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001084
1085 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001086 * Convenience method to set the UIDs this network applies to to a single UID.
1087 * @hide
1088 */
1089 public NetworkCapabilities setSingleUid(int uid) {
1090 final ArraySet<UidRange> identity = new ArraySet<>(1);
1091 identity.add(new UidRange(uid, uid));
1092 setUids(identity);
1093 return this;
1094 }
1095
1096 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001097 * Set the list of UIDs this network applies to.
1098 * This makes a copy of the set so that callers can't modify it after the call.
1099 * @hide
1100 */
1101 public NetworkCapabilities setUids(Set<UidRange> uids) {
1102 if (null == uids) {
1103 mUids = null;
1104 } else {
1105 mUids = new ArraySet<>(uids);
1106 }
1107 return this;
1108 }
1109
1110 /**
1111 * Get the list of UIDs this network applies to.
1112 * This returns a copy of the set so that callers can't modify the original object.
1113 * @hide
1114 */
1115 public Set<UidRange> getUids() {
1116 return null == mUids ? null : new ArraySet<>(mUids);
1117 }
1118
1119 /**
1120 * Test whether this network applies to this UID.
1121 * @hide
1122 */
1123 public boolean appliesToUid(int uid) {
1124 if (null == mUids) return true;
1125 for (UidRange range : mUids) {
1126 if (range.contains(uid)) {
1127 return true;
1128 }
1129 }
1130 return false;
1131 }
1132
1133 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001134 * 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 +09001135 * <p>
1136 * This test only checks whether equal range objects are in both sets. It will
1137 * return false if the ranges are not exactly the same, even if the covered UIDs
1138 * are for an equivalent result.
1139 * <p>
1140 * Note that this method is not very optimized, which is fine as long as it's not used very
1141 * often.
1142 * <p>
1143 * nc is assumed nonnull.
1144 *
1145 * @hide
1146 */
1147 @VisibleForTesting
1148 public boolean equalsUids(NetworkCapabilities nc) {
1149 Set<UidRange> comparedUids = nc.mUids;
1150 if (null == comparedUids) return null == mUids;
1151 if (null == mUids) return false;
1152 // Make a copy so it can be mutated to check that all ranges in mUids
1153 // also are in uids.
1154 final Set<UidRange> uids = new ArraySet<>(mUids);
1155 for (UidRange range : comparedUids) {
1156 if (!uids.contains(range)) {
1157 return false;
1158 }
1159 uids.remove(range);
1160 }
1161 return uids.isEmpty();
1162 }
1163
1164 /**
1165 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1166 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001167 * This method is called on the NetworkCapabilities embedded in a request with the
1168 * capabilities of an available network. It checks whether all the UIDs from this listen
1169 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1170 * in the passed nc (representing the UIDs that this network is available to).
1171 * <p>
1172 * As a special exception, the UID that created the passed network (as represented by its
1173 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1174 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1175 * can see its own network when it listens for it.
1176 * <p>
1177 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001178 * @see #appliesToUid
1179 * @hide
1180 */
1181 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001182 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001183 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001184 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001185 if (!nc.appliesToUidRange(requiredRange)) {
1186 return false;
1187 }
1188 }
1189 return true;
1190 }
1191
1192 /**
1193 * Returns whether this network applies to the passed ranges.
1194 * This assumes that to apply, the passed range has to be entirely contained
1195 * within one of the ranges this network applies to. If the ranges are not normalized,
1196 * this method may return false even though all required UIDs are covered because no
1197 * single range contained them all.
1198 * @hide
1199 */
1200 @VisibleForTesting
1201 public boolean appliesToUidRange(UidRange requiredRange) {
1202 if (null == mUids) return true;
1203 for (UidRange uidRange : mUids) {
1204 if (uidRange.containsRange(requiredRange)) {
1205 return true;
1206 }
1207 }
1208 return false;
1209 }
1210
1211 /**
1212 * Combine the UIDs this network currently applies to with the UIDs the passed
1213 * NetworkCapabilities apply to.
1214 * nc is assumed nonnull.
1215 */
1216 private void combineUids(NetworkCapabilities nc) {
1217 if (null == nc.mUids || null == mUids) {
1218 mUids = null;
1219 return;
1220 }
1221 mUids.addAll(nc.mUids);
1222 }
1223
Chalard Jeanb03a6222018-04-11 21:09:10 +09001224
1225 /**
1226 * The SSID of the network, or null if not applicable or unknown.
1227 * <p>
1228 * This is filled in by wifi code.
1229 * @hide
1230 */
1231 private String mSSID;
1232
1233 /**
1234 * Sets the SSID of this network.
1235 * @hide
1236 */
1237 public NetworkCapabilities setSSID(String ssid) {
1238 mSSID = ssid;
1239 return this;
1240 }
1241
1242 /**
1243 * Gets the SSID of this network, or null if none or unknown.
1244 * @hide
1245 */
1246 public String getSSID() {
1247 return mSSID;
1248 }
1249
1250 /**
1251 * Tests if the SSID of this network is the same as the SSID of the passed network.
1252 * @hide
1253 */
1254 public boolean equalsSSID(NetworkCapabilities nc) {
1255 return Objects.equals(mSSID, nc.mSSID);
1256 }
1257
1258 /**
1259 * Check if the SSID requirements of this object are matched by the passed object.
1260 * @hide
1261 */
1262 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1263 return mSSID == null || mSSID.equals(nc.mSSID);
1264 }
1265
1266 /**
1267 * Combine SSIDs of the capabilities.
1268 * <p>
1269 * This is only legal if either the SSID of this object is null, or both SSIDs are
1270 * equal.
1271 * @hide
1272 */
1273 private void combineSSIDs(NetworkCapabilities nc) {
1274 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1275 throw new IllegalStateException("Can't combine two SSIDs");
1276 }
1277 setSSID(nc.mSSID);
1278 }
1279
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001280 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001281 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1282 * <p>
1283 * Note that this method may break an invariant of having a particular capability in either
1284 * wanted or unwanted lists but never in both. Requests that have the same capability in
1285 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001286 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001287 */
1288 public void combineCapabilities(NetworkCapabilities nc) {
1289 combineNetCapabilities(nc);
1290 combineTransportTypes(nc);
1291 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001292 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001293 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001294 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001295 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001296 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001297 }
1298
1299 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001300 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1301 *
1302 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1303 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1304 * bandwidth, signal strength, or validation / captive portal status.
1305 *
1306 * @hide
1307 */
1308 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001309 return (nc != null
1310 && satisfiedByNetCapabilities(nc, onlyImmutable)
1311 && satisfiedByTransportTypes(nc)
1312 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1313 && satisfiedBySpecifier(nc)
1314 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001315 && (onlyImmutable || satisfiedByUids(nc))
1316 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001317 }
1318
1319 /**
1320 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1321 *
1322 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1323 *
1324 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001325 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001326 @TestApi
1327 @SystemApi
Robert Greenwalt1448f052014-04-08 13:41:39 -07001328 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001329 return satisfiedByNetworkCapabilities(nc, false);
1330 }
1331
1332 /**
1333 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1334 *
1335 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1336 *
1337 * @hide
1338 */
1339 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1340 return satisfiedByNetworkCapabilities(nc, true);
1341 }
1342
1343 /**
1344 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001345 * {@code NetworkCapabilities} and return a String describing any difference.
1346 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001347 *
1348 * @hide
1349 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001350 public String describeImmutableDifferences(NetworkCapabilities that) {
1351 if (that == null) {
1352 return "other NetworkCapabilities was null";
1353 }
1354
1355 StringJoiner joiner = new StringJoiner(", ");
1356
Hugo Benichieae7a222017-07-25 11:40:56 +09001357 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1358 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001359 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001360 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1361 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1362 if (oldImmutableCapabilities != newImmutableCapabilities) {
1363 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1364 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1365 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1366 }
1367
1368 if (!equalsSpecifier(that)) {
1369 NetworkSpecifier before = this.getNetworkSpecifier();
1370 NetworkSpecifier after = that.getNetworkSpecifier();
1371 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1372 }
1373
1374 if (!equalsTransportTypes(that)) {
1375 String before = transportNamesOf(this.getTransportTypes());
1376 String after = transportNamesOf(that.getTransportTypes());
1377 joiner.add(String.format("transports changed: %s -> %s", before, after));
1378 }
1379
1380 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001381 }
1382
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001383 /**
1384 * Checks that our requestable capabilities are the same as those of the given
1385 * {@code NetworkCapabilities}.
1386 *
1387 * @hide
1388 */
1389 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1390 if (nc == null) return false;
1391 return (equalsNetCapabilitiesRequestable(nc) &&
1392 equalsTransportTypes(nc) &&
1393 equalsSpecifier(nc));
1394 }
1395
Robert Greenwalt1448f052014-04-08 13:41:39 -07001396 @Override
1397 public boolean equals(Object obj) {
1398 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001399 NetworkCapabilities that = (NetworkCapabilities) obj;
1400 return (equalsNetCapabilities(that)
1401 && equalsTransportTypes(that)
1402 && equalsLinkBandwidths(that)
1403 && equalsSignalStrength(that)
1404 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001405 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001406 && equalsUids(that)
1407 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001408 }
1409
1410 @Override
1411 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001412 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001413 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001414 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1415 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1416 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1417 + ((int) (mTransportTypes >> 32) * 13)
1418 + (mLinkUpBandwidthKbps * 17)
1419 + (mLinkDownBandwidthKbps * 19)
1420 + Objects.hashCode(mNetworkSpecifier) * 23
1421 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001422 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001423 + Objects.hashCode(mSSID) * 37
1424 + Objects.hashCode(mTransportInfo) * 41;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001425 }
1426
Wink Saville4e2dea72014-09-20 11:04:03 -07001427 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001428 public int describeContents() {
1429 return 0;
1430 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001431 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001432 public void writeToParcel(Parcel dest, int flags) {
1433 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001434 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001435 dest.writeLong(mTransportTypes);
1436 dest.writeInt(mLinkUpBandwidthKbps);
1437 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001438 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001439 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001440 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001441 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001442 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001443 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001444
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001445 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001446 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001447 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001448 public NetworkCapabilities createFromParcel(Parcel in) {
1449 NetworkCapabilities netCap = new NetworkCapabilities();
1450
1451 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001452 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001453 netCap.mTransportTypes = in.readLong();
1454 netCap.mLinkUpBandwidthKbps = in.readInt();
1455 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001456 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001457 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001458 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001459 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1460 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001461 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001462 return netCap;
1463 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001464 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001465 public NetworkCapabilities[] newArray(int size) {
1466 return new NetworkCapabilities[size];
1467 }
1468 };
1469
Wink Saville4e2dea72014-09-20 11:04:03 -07001470 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001471 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001472 final StringBuilder sb = new StringBuilder("[");
1473 if (0 != mTransportTypes) {
1474 sb.append(" Transports: ");
1475 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1476 NetworkCapabilities::transportNameOf, "|");
1477 }
1478 if (0 != mNetworkCapabilities) {
1479 sb.append(" Capabilities: ");
1480 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1481 NetworkCapabilities::capabilityNameOf, "&");
1482 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001483 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001484 sb.append(" Unwanted: ");
1485 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1486 NetworkCapabilities::capabilityNameOf, "&");
1487 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001488 if (mLinkUpBandwidthKbps > 0) {
1489 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1490 }
1491 if (mLinkDownBandwidthKbps > 0) {
1492 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1493 }
1494 if (mNetworkSpecifier != null) {
1495 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1496 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001497 if (mTransportInfo != null) {
1498 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1499 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001500 if (hasSignalStrength()) {
1501 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001502 }
1503
Chalard Jean07ace0f2018-02-26 19:00:45 +09001504 if (null != mUids) {
1505 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1506 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1507 } else {
1508 sb.append(" Uids: <").append(mUids).append(">");
1509 }
1510 }
1511 if (mEstablishingVpnAppUid != INVALID_UID) {
1512 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1513 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001514
Chalard Jeanb03a6222018-04-11 21:09:10 +09001515 if (null != mSSID) {
1516 sb.append(" SSID: ").append(mSSID);
1517 }
1518
Chalard Jean07ace0f2018-02-26 19:00:45 +09001519 sb.append("]");
1520 return sb.toString();
1521 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001522
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001523
Chalard Jean07ace0f2018-02-26 19:00:45 +09001524 private interface NameOf {
1525 String nameOf(int value);
1526 }
1527 /**
1528 * @hide
1529 */
1530 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1531 long bitMask, NameOf nameFetcher, String separator) {
1532 int bitPos = 0;
1533 boolean firstElementAdded = false;
1534 while (bitMask != 0) {
1535 if ((bitMask & 1) != 0) {
1536 if (firstElementAdded) {
1537 sb.append(separator);
1538 } else {
1539 firstElementAdded = true;
1540 }
1541 sb.append(nameFetcher.nameOf(bitPos));
1542 }
1543 bitMask >>= 1;
1544 ++bitPos;
1545 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001546 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001547
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001548 /** @hide */
1549 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1550 final long token = proto.start(fieldId);
1551
1552 for (int transport : getTransportTypes()) {
1553 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1554 }
1555
1556 for (int capability : getCapabilities()) {
1557 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1558 }
1559
1560 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1561 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1562
1563 if (mNetworkSpecifier != null) {
1564 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1565 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001566 if (mTransportInfo != null) {
1567 // TODO b/120653863: write transport-specific info to proto?
1568 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001569
1570 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1571 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1572
1573 proto.end(token);
1574 }
1575
Hugo Benichi5df9d722016-04-25 17:16:35 +09001576 /**
1577 * @hide
1578 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001579 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001580 StringJoiner joiner = new StringJoiner("|");
1581 if (capabilities != null) {
1582 for (int c : capabilities) {
1583 joiner.add(capabilityNameOf(c));
1584 }
1585 }
1586 return joiner.toString();
1587 }
1588
1589 /**
1590 * @hide
1591 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001592 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001593 switch (capability) {
1594 case NET_CAPABILITY_MMS: return "MMS";
1595 case NET_CAPABILITY_SUPL: return "SUPL";
1596 case NET_CAPABILITY_DUN: return "DUN";
1597 case NET_CAPABILITY_FOTA: return "FOTA";
1598 case NET_CAPABILITY_IMS: return "IMS";
1599 case NET_CAPABILITY_CBS: return "CBS";
1600 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1601 case NET_CAPABILITY_IA: return "IA";
1602 case NET_CAPABILITY_RCS: return "RCS";
1603 case NET_CAPABILITY_XCAP: return "XCAP";
1604 case NET_CAPABILITY_EIMS: return "EIMS";
1605 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1606 case NET_CAPABILITY_INTERNET: return "INTERNET";
1607 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1608 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1609 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1610 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1611 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001612 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001613 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001614 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001615 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001616 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001617 default: return Integer.toString(capability);
1618 }
1619 }
1620
1621 /**
1622 * @hide
1623 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001624 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -06001625 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001626 StringJoiner joiner = new StringJoiner("|");
1627 if (types != null) {
1628 for (int t : types) {
1629 joiner.add(transportNameOf(t));
1630 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001631 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001632 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001633 }
1634
1635 /**
1636 * @hide
1637 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001638 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001639 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001640 return "UNKNOWN";
1641 }
1642 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001643 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001644
Jeff Sharkeyde570312017-10-24 21:25:50 -06001645 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001646 Preconditions.checkArgument(
1647 isValidTransport(transport), "Invalid TransportType " + transport);
1648 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001649
1650 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1651 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1652 }
1653
1654 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1655 Preconditions.checkArgument(isValidCapability(capability),
1656 "NetworkCapability " + capability + "out of range");
1657 }
junyulai05986c62018-08-07 19:50:45 +08001658
1659 /**
1660 * Check if this {@code NetworkCapability} instance is metered.
1661 *
1662 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1663 * @hide
1664 */
1665 public boolean isMetered() {
1666 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1667 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001668}