blob: 7e9bda14b199f20b02c0033aa9b2ee7f2ed8dd1d [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,
600 })
601 public @interface Transport { }
602
Robert Greenwalt1448f052014-04-08 13:41:39 -0700603 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700604 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700605 */
606 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700607
608 /**
609 * Indicates this network uses a Wi-Fi transport.
610 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700611 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700612
613 /**
614 * Indicates this network uses a Bluetooth transport.
615 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700616 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700617
618 /**
619 * Indicates this network uses an Ethernet transport.
620 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700621 public static final int TRANSPORT_ETHERNET = 3;
622
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400623 /**
624 * Indicates this network uses a VPN transport.
625 */
626 public static final int TRANSPORT_VPN = 4;
627
Etan Cohen305ea282016-06-20 09:27:12 -0700628 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700629 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700630 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700631 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700632
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700633 /**
634 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700635 */
636 public static final int TRANSPORT_LOWPAN = 6;
637
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900638 /** @hide */
639 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
640 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700641 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700642
Hugo Benichi16f0a942017-06-20 14:07:59 +0900643 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600644 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900645 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
646 }
647
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900648 private static final String[] TRANSPORT_NAMES = {
649 "CELLULAR",
650 "WIFI",
651 "BLUETOOTH",
652 "ETHERNET",
653 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700654 "WIFI_AWARE",
655 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900656 };
657
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700658 /**
659 * Adds the given transport type to this {@code NetworkCapability} instance.
660 * Multiple transports may be applied sequentially. Note that when searching
661 * for a network to satisfy a request, any listed in the request will satisfy the request.
662 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
663 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
664 * to be selected. This is logically different than
665 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
666 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600667 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900668 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700669 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700670 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100671 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -0600672 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900673 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700674 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700675 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700676 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700677 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700678
679 /**
680 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
681 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600682 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900683 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700684 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700685 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600686 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900687 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700688 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700689 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700690 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700691 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700692
693 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600694 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
695 * instance.
696 *
697 * @hide
698 */
699 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
700 if (value) {
701 addTransportType(transportType);
702 } else {
703 removeTransportType(transportType);
704 }
705 return this;
706 }
707
708 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700709 * Gets all the transports set on this {@code NetworkCapability} instance.
710 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700712 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700713 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600714 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900715 @SystemApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600716 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900717 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700718 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700719
720 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600721 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700722 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600723 *
724 * @hide
725 */
726 public void setTransportTypes(@Transport int[] transportTypes) {
727 mTransportTypes = BitUtils.packBits(transportTypes);
728 }
729
730 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700731 * Tests for the presence of a transport on this instance.
732 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600733 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700734 * @return {@code true} if set on this instance.
735 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600736 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900737 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700738 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700739
740 private void combineTransportTypes(NetworkCapabilities nc) {
741 this.mTransportTypes |= nc.mTransportTypes;
742 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900743
Robert Greenwalt1448f052014-04-08 13:41:39 -0700744 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
745 return ((this.mTransportTypes == 0) ||
746 ((this.mTransportTypes & nc.mTransportTypes) != 0));
747 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900748
Robert Greenwalt06314e42014-10-29 14:04:06 -0700749 /** @hide */
750 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700751 return (nc.mTransportTypes == this.mTransportTypes);
752 }
753
754 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900755 * UID of the app that manages this network, or INVALID_UID if none/unknown.
756 *
757 * This field keeps track of the UID of the app that created this network and is in charge
758 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
759 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
760 * to create networks.
761 *
762 * Because this field is only used in the services side (and to avoid apps being able to
763 * set this to whatever they want), this field is not parcelled and will not be conserved
764 * across the IPC boundary.
765 * @hide
766 */
767 private int mEstablishingVpnAppUid = INVALID_UID;
768
769 /**
770 * Set the UID of the managing app.
771 * @hide
772 */
773 public void setEstablishingVpnAppUid(final int uid) {
774 mEstablishingVpnAppUid = uid;
775 }
776
777 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600778 * Value indicating that link bandwidth is unspecified.
779 * @hide
780 */
781 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
782
783 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700784 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
785 * for the first hop on the given transport. It is not measured, but may take into account
786 * link parameters (Radio technology, allocated channels, etc).
787 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600788 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
789 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700790
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700791 /**
792 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
793 * the estimated first hop transport bandwidth.
794 * <p>
795 * Note that when used to request a network, this specifies the minimum acceptable.
796 * When received as the state of an existing network this specifies the typical
797 * first hop bandwidth expected. This is never measured, but rather is inferred
798 * from technology type and other link parameters. It could be used to differentiate
799 * between very slow 1xRTT cellular links and other faster networks or even between
800 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
801 * fast backhauls and slow backhauls.
802 *
803 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700804 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700805 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600806 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700807 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600808 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700809 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700810
811 /**
812 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
813 * the estimated first hop transport bandwidth.
814 *
815 * @return The estimated first hop upstream (device to network) bandwidth.
816 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700817 public int getLinkUpstreamBandwidthKbps() {
818 return mLinkUpBandwidthKbps;
819 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700820
821 /**
822 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
823 * the estimated first hop transport bandwidth.
824 * <p>
825 * Note that when used to request a network, this specifies the minimum acceptable.
826 * When received as the state of an existing network this specifies the typical
827 * first hop bandwidth expected. This is never measured, but rather is inferred
828 * from technology type and other link parameters. It could be used to differentiate
829 * between very slow 1xRTT cellular links and other faster networks or even between
830 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
831 * fast backhauls and slow backhauls.
832 *
833 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700834 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700835 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600836 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700837 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600838 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700839 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700840
841 /**
842 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
843 * the estimated first hop transport bandwidth.
844 *
845 * @return The estimated first hop downstream (network to device) bandwidth.
846 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700847 public int getLinkDownstreamBandwidthKbps() {
848 return mLinkDownBandwidthKbps;
849 }
850
851 private void combineLinkBandwidths(NetworkCapabilities nc) {
852 this.mLinkUpBandwidthKbps =
853 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
854 this.mLinkDownBandwidthKbps =
855 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
856 }
857 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
858 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
859 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
860 }
861 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
862 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
863 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
864 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600865 /** @hide */
866 public static int minBandwidth(int a, int b) {
867 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
868 return b;
869 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
870 return a;
871 } else {
872 return Math.min(a, b);
873 }
874 }
875 /** @hide */
876 public static int maxBandwidth(int a, int b) {
877 return Math.max(a, b);
878 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700879
Etan Cohena7434272017-04-03 12:17:51 -0700880 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -0800881 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -0700882
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700883 /**
884 * Sets the optional bearer specific network specifier.
885 * This has no meaning if a single transport is also not specified, so calling
886 * this without a single transport set will generate an exception, as will
887 * subsequently adding or removing transports after this is set.
888 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700889 *
Etan Cohena7434272017-04-03 12:17:51 -0700890 * @param networkSpecifier A concrete, parcelable framework class that extends
891 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900892 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 * @hide
894 */
Etan Cohena7434272017-04-03 12:17:51 -0700895 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
896 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700897 throw new IllegalStateException("Must have a single transport specified to use " +
898 "setNetworkSpecifier");
899 }
Etan Cohena7434272017-04-03 12:17:51 -0700900
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700901 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700902
Pierre Imaic8419a82016-03-22 17:54:54 +0900903 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700904 }
905
906 /**
Etan Cohenca9fb562018-11-27 07:32:39 -0800907 * Sets the optional transport specific information.
908 *
909 * @param transportInfo A concrete, parcelable framework class that extends
910 * {@link TransportInfo}.
911 * @return This NetworkCapabilities instance, to facilitate chaining.
912 * @hide
913 */
914 public NetworkCapabilities setTransportInfo(TransportInfo transportInfo) {
915 mTransportInfo = transportInfo;
916 return this;
917 }
918
919 /**
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700920 * Gets the optional bearer specific network specifier.
921 *
Etan Cohena7434272017-04-03 12:17:51 -0700922 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
923 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700924 * @hide
925 */
Mathew Inwood45d2c252018-09-14 12:35:36 +0100926 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Etan Cohena7434272017-04-03 12:17:51 -0700927 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700928 return mNetworkSpecifier;
929 }
930
Etan Cohenca9fb562018-11-27 07:32:39 -0800931 /**
932 * Returns a transport-specific information container. The application may cast this
933 * container to a concrete sub-class based on its knowledge of the network request. The
934 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -0800935 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -0800936 *
937 * @return A concrete implementation of the {@link TransportInfo} class or null if not
938 * available for the network.
939 */
940 @Nullable public TransportInfo getTransportInfo() {
941 return mTransportInfo;
942 }
943
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700944 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700945 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700946 throw new IllegalStateException("Can't combine two networkSpecifiers");
947 }
Etan Cohena7434272017-04-03 12:17:51 -0700948 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700949 }
Etan Cohena7434272017-04-03 12:17:51 -0700950
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700951 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700952 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
953 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700954 }
Etan Cohena7434272017-04-03 12:17:51 -0700955
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700956 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700957 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700958 }
959
Etan Cohenca9fb562018-11-27 07:32:39 -0800960 private void combineTransportInfos(NetworkCapabilities nc) {
961 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
962 throw new IllegalStateException("Can't combine two TransportInfos");
963 }
964 setTransportInfo(nc.mTransportInfo);
965 }
966
967 private boolean equalsTransportInfo(NetworkCapabilities nc) {
968 return Objects.equals(mTransportInfo, nc.mTransportInfo);
969 }
970
Robert Greenwalt1448f052014-04-08 13:41:39 -0700971 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900972 * Magic value that indicates no signal strength provided. A request specifying this value is
973 * always satisfied.
974 *
975 * @hide
976 */
977 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
978
979 /**
980 * Signal strength. This is a signed integer, and higher values indicate better signal.
981 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
982 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100983 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700984 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900985
986 /**
987 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
988 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +0900989 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900990 * <p>
991 * Note that when used to register a network callback, this specifies the minimum acceptable
992 * signal strength. When received as the state of an existing network it specifies the current
993 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
994 * effect when requesting a callback.
995 *
996 * @param signalStrength the bearer-specific signal strength.
997 * @hide
998 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100999 @UnsupportedAppUsage
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001000 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001001 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001002 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001003 }
1004
1005 /**
1006 * Returns {@code true} if this object specifies a signal strength.
1007 *
1008 * @hide
1009 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001010 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001011 public boolean hasSignalStrength() {
1012 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1013 }
1014
1015 /**
1016 * Retrieves the signal strength.
1017 *
1018 * @return The bearer-specific signal strength.
1019 * @hide
1020 */
paulhu7610bc72018-12-12 17:52:57 +08001021 @SystemApi
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001022 public int getSignalStrength() {
1023 return mSignalStrength;
1024 }
1025
1026 private void combineSignalStrength(NetworkCapabilities nc) {
1027 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1028 }
1029
1030 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1031 return this.mSignalStrength <= nc.mSignalStrength;
1032 }
1033
1034 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1035 return this.mSignalStrength == nc.mSignalStrength;
1036 }
1037
1038 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001039 * List of UIDs this network applies to. No restriction if null.
1040 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001041 * For networks, mUids represent the list of network this applies to, and null means this
1042 * network applies to all UIDs.
1043 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1044 * must be included in a network so that they match. As an exception to the general rule,
1045 * a null mUids field for requests mean "no requirements" rather than what the general rule
1046 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1047 * of this API expect in practice. A network that must match all UIDs can still be
1048 * expressed with a set ranging the entire set of possible UIDs.
1049 * <p>
1050 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001051 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1052 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1053 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1054 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001055 * As a special exception, the app managing this network (as identified by its UID stored in
1056 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
1057 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1058 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001059 * <p>
1060 * Please note that in principle a single app can be associated with multiple UIDs because
1061 * each app will have a different UID when it's run as a different (macro-)user. A single
1062 * macro user can only have a single active VPN app at any given time however.
1063 * <p>
1064 * Also please be aware this class does not try to enforce any normalization on this. Callers
1065 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1066 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1067 * their own (like requiring sortedness or no overlap) they need to enforce it
1068 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1069 * or overlapping ranges are present.
1070 *
1071 * @hide
1072 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001073 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001074
1075 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001076 * Convenience method to set the UIDs this network applies to to a single UID.
1077 * @hide
1078 */
1079 public NetworkCapabilities setSingleUid(int uid) {
1080 final ArraySet<UidRange> identity = new ArraySet<>(1);
1081 identity.add(new UidRange(uid, uid));
1082 setUids(identity);
1083 return this;
1084 }
1085
1086 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001087 * Set the list of UIDs this network applies to.
1088 * This makes a copy of the set so that callers can't modify it after the call.
1089 * @hide
1090 */
1091 public NetworkCapabilities setUids(Set<UidRange> uids) {
1092 if (null == uids) {
1093 mUids = null;
1094 } else {
1095 mUids = new ArraySet<>(uids);
1096 }
1097 return this;
1098 }
1099
1100 /**
1101 * Get the list of UIDs this network applies to.
1102 * This returns a copy of the set so that callers can't modify the original object.
1103 * @hide
1104 */
1105 public Set<UidRange> getUids() {
1106 return null == mUids ? null : new ArraySet<>(mUids);
1107 }
1108
1109 /**
1110 * Test whether this network applies to this UID.
1111 * @hide
1112 */
1113 public boolean appliesToUid(int uid) {
1114 if (null == mUids) return true;
1115 for (UidRange range : mUids) {
1116 if (range.contains(uid)) {
1117 return true;
1118 }
1119 }
1120 return false;
1121 }
1122
1123 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001124 * 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 +09001125 * <p>
1126 * This test only checks whether equal range objects are in both sets. It will
1127 * return false if the ranges are not exactly the same, even if the covered UIDs
1128 * are for an equivalent result.
1129 * <p>
1130 * Note that this method is not very optimized, which is fine as long as it's not used very
1131 * often.
1132 * <p>
1133 * nc is assumed nonnull.
1134 *
1135 * @hide
1136 */
1137 @VisibleForTesting
1138 public boolean equalsUids(NetworkCapabilities nc) {
1139 Set<UidRange> comparedUids = nc.mUids;
1140 if (null == comparedUids) return null == mUids;
1141 if (null == mUids) return false;
1142 // Make a copy so it can be mutated to check that all ranges in mUids
1143 // also are in uids.
1144 final Set<UidRange> uids = new ArraySet<>(mUids);
1145 for (UidRange range : comparedUids) {
1146 if (!uids.contains(range)) {
1147 return false;
1148 }
1149 uids.remove(range);
1150 }
1151 return uids.isEmpty();
1152 }
1153
1154 /**
1155 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1156 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001157 * This method is called on the NetworkCapabilities embedded in a request with the
1158 * capabilities of an available network. It checks whether all the UIDs from this listen
1159 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1160 * in the passed nc (representing the UIDs that this network is available to).
1161 * <p>
1162 * As a special exception, the UID that created the passed network (as represented by its
1163 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1164 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1165 * can see its own network when it listens for it.
1166 * <p>
1167 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001168 * @see #appliesToUid
1169 * @hide
1170 */
1171 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001172 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001173 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001174 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001175 if (!nc.appliesToUidRange(requiredRange)) {
1176 return false;
1177 }
1178 }
1179 return true;
1180 }
1181
1182 /**
1183 * Returns whether this network applies to the passed ranges.
1184 * This assumes that to apply, the passed range has to be entirely contained
1185 * within one of the ranges this network applies to. If the ranges are not normalized,
1186 * this method may return false even though all required UIDs are covered because no
1187 * single range contained them all.
1188 * @hide
1189 */
1190 @VisibleForTesting
1191 public boolean appliesToUidRange(UidRange requiredRange) {
1192 if (null == mUids) return true;
1193 for (UidRange uidRange : mUids) {
1194 if (uidRange.containsRange(requiredRange)) {
1195 return true;
1196 }
1197 }
1198 return false;
1199 }
1200
1201 /**
1202 * Combine the UIDs this network currently applies to with the UIDs the passed
1203 * NetworkCapabilities apply to.
1204 * nc is assumed nonnull.
1205 */
1206 private void combineUids(NetworkCapabilities nc) {
1207 if (null == nc.mUids || null == mUids) {
1208 mUids = null;
1209 return;
1210 }
1211 mUids.addAll(nc.mUids);
1212 }
1213
Chalard Jeanb03a6222018-04-11 21:09:10 +09001214
1215 /**
1216 * The SSID of the network, or null if not applicable or unknown.
1217 * <p>
1218 * This is filled in by wifi code.
1219 * @hide
1220 */
1221 private String mSSID;
1222
1223 /**
1224 * Sets the SSID of this network.
1225 * @hide
1226 */
1227 public NetworkCapabilities setSSID(String ssid) {
1228 mSSID = ssid;
1229 return this;
1230 }
1231
1232 /**
1233 * Gets the SSID of this network, or null if none or unknown.
1234 * @hide
1235 */
1236 public String getSSID() {
1237 return mSSID;
1238 }
1239
1240 /**
1241 * Tests if the SSID of this network is the same as the SSID of the passed network.
1242 * @hide
1243 */
1244 public boolean equalsSSID(NetworkCapabilities nc) {
1245 return Objects.equals(mSSID, nc.mSSID);
1246 }
1247
1248 /**
1249 * Check if the SSID requirements of this object are matched by the passed object.
1250 * @hide
1251 */
1252 public boolean satisfiedBySSID(NetworkCapabilities nc) {
1253 return mSSID == null || mSSID.equals(nc.mSSID);
1254 }
1255
1256 /**
1257 * Combine SSIDs of the capabilities.
1258 * <p>
1259 * This is only legal if either the SSID of this object is null, or both SSIDs are
1260 * equal.
1261 * @hide
1262 */
1263 private void combineSSIDs(NetworkCapabilities nc) {
1264 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1265 throw new IllegalStateException("Can't combine two SSIDs");
1266 }
1267 setSSID(nc.mSSID);
1268 }
1269
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001270 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001271 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1272 * <p>
1273 * Note that this method may break an invariant of having a particular capability in either
1274 * wanted or unwanted lists but never in both. Requests that have the same capability in
1275 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001276 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001277 */
1278 public void combineCapabilities(NetworkCapabilities nc) {
1279 combineNetCapabilities(nc);
1280 combineTransportTypes(nc);
1281 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001282 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001283 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001284 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001285 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001286 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001287 }
1288
1289 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001290 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1291 *
1292 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1293 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1294 * bandwidth, signal strength, or validation / captive portal status.
1295 *
1296 * @hide
1297 */
1298 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001299 return (nc != null
1300 && satisfiedByNetCapabilities(nc, onlyImmutable)
1301 && satisfiedByTransportTypes(nc)
1302 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1303 && satisfiedBySpecifier(nc)
1304 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001305 && (onlyImmutable || satisfiedByUids(nc))
1306 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001307 }
1308
1309 /**
1310 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1311 *
1312 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1313 *
1314 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001315 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001316 @TestApi
1317 @SystemApi
Robert Greenwalt1448f052014-04-08 13:41:39 -07001318 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001319 return satisfiedByNetworkCapabilities(nc, false);
1320 }
1321
1322 /**
1323 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1324 *
1325 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1326 *
1327 * @hide
1328 */
1329 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1330 return satisfiedByNetworkCapabilities(nc, true);
1331 }
1332
1333 /**
1334 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001335 * {@code NetworkCapabilities} and return a String describing any difference.
1336 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001337 *
1338 * @hide
1339 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001340 public String describeImmutableDifferences(NetworkCapabilities that) {
1341 if (that == null) {
1342 return "other NetworkCapabilities was null";
1343 }
1344
1345 StringJoiner joiner = new StringJoiner(", ");
1346
Hugo Benichieae7a222017-07-25 11:40:56 +09001347 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1348 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001349 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001350 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1351 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1352 if (oldImmutableCapabilities != newImmutableCapabilities) {
1353 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1354 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1355 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1356 }
1357
1358 if (!equalsSpecifier(that)) {
1359 NetworkSpecifier before = this.getNetworkSpecifier();
1360 NetworkSpecifier after = that.getNetworkSpecifier();
1361 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1362 }
1363
1364 if (!equalsTransportTypes(that)) {
1365 String before = transportNamesOf(this.getTransportTypes());
1366 String after = transportNamesOf(that.getTransportTypes());
1367 joiner.add(String.format("transports changed: %s -> %s", before, after));
1368 }
1369
1370 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001371 }
1372
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001373 /**
1374 * Checks that our requestable capabilities are the same as those of the given
1375 * {@code NetworkCapabilities}.
1376 *
1377 * @hide
1378 */
1379 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1380 if (nc == null) return false;
1381 return (equalsNetCapabilitiesRequestable(nc) &&
1382 equalsTransportTypes(nc) &&
1383 equalsSpecifier(nc));
1384 }
1385
Robert Greenwalt1448f052014-04-08 13:41:39 -07001386 @Override
1387 public boolean equals(Object obj) {
1388 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001389 NetworkCapabilities that = (NetworkCapabilities) obj;
1390 return (equalsNetCapabilities(that)
1391 && equalsTransportTypes(that)
1392 && equalsLinkBandwidths(that)
1393 && equalsSignalStrength(that)
1394 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001395 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001396 && equalsUids(that)
1397 && equalsSSID(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001398 }
1399
1400 @Override
1401 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001402 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001403 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001404 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1405 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1406 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1407 + ((int) (mTransportTypes >> 32) * 13)
1408 + (mLinkUpBandwidthKbps * 17)
1409 + (mLinkDownBandwidthKbps * 19)
1410 + Objects.hashCode(mNetworkSpecifier) * 23
1411 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001412 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001413 + Objects.hashCode(mSSID) * 37
1414 + Objects.hashCode(mTransportInfo) * 41;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001415 }
1416
Wink Saville4e2dea72014-09-20 11:04:03 -07001417 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001418 public int describeContents() {
1419 return 0;
1420 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001421 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001422 public void writeToParcel(Parcel dest, int flags) {
1423 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001424 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001425 dest.writeLong(mTransportTypes);
1426 dest.writeInt(mLinkUpBandwidthKbps);
1427 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001428 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001429 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001430 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001431 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001432 dest.writeString(mSSID);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001433 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001434
Robert Greenwalt1448f052014-04-08 13:41:39 -07001435 public static final Creator<NetworkCapabilities> CREATOR =
1436 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001437 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001438 public NetworkCapabilities createFromParcel(Parcel in) {
1439 NetworkCapabilities netCap = new NetworkCapabilities();
1440
1441 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001442 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001443 netCap.mTransportTypes = in.readLong();
1444 netCap.mLinkUpBandwidthKbps = in.readInt();
1445 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001446 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001447 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001448 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001449 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1450 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001451 netCap.mSSID = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001452 return netCap;
1453 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001454 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001455 public NetworkCapabilities[] newArray(int size) {
1456 return new NetworkCapabilities[size];
1457 }
1458 };
1459
Wink Saville4e2dea72014-09-20 11:04:03 -07001460 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001461 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001462 final StringBuilder sb = new StringBuilder("[");
1463 if (0 != mTransportTypes) {
1464 sb.append(" Transports: ");
1465 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1466 NetworkCapabilities::transportNameOf, "|");
1467 }
1468 if (0 != mNetworkCapabilities) {
1469 sb.append(" Capabilities: ");
1470 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1471 NetworkCapabilities::capabilityNameOf, "&");
1472 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001473 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001474 sb.append(" Unwanted: ");
1475 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1476 NetworkCapabilities::capabilityNameOf, "&");
1477 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001478 if (mLinkUpBandwidthKbps > 0) {
1479 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1480 }
1481 if (mLinkDownBandwidthKbps > 0) {
1482 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1483 }
1484 if (mNetworkSpecifier != null) {
1485 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1486 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001487 if (mTransportInfo != null) {
1488 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1489 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001490 if (hasSignalStrength()) {
1491 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001492 }
1493
Chalard Jean07ace0f2018-02-26 19:00:45 +09001494 if (null != mUids) {
1495 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1496 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1497 } else {
1498 sb.append(" Uids: <").append(mUids).append(">");
1499 }
1500 }
1501 if (mEstablishingVpnAppUid != INVALID_UID) {
1502 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1503 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001504
Chalard Jeanb03a6222018-04-11 21:09:10 +09001505 if (null != mSSID) {
1506 sb.append(" SSID: ").append(mSSID);
1507 }
1508
Chalard Jean07ace0f2018-02-26 19:00:45 +09001509 sb.append("]");
1510 return sb.toString();
1511 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001512
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001513
Chalard Jean07ace0f2018-02-26 19:00:45 +09001514 private interface NameOf {
1515 String nameOf(int value);
1516 }
1517 /**
1518 * @hide
1519 */
1520 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1521 long bitMask, NameOf nameFetcher, String separator) {
1522 int bitPos = 0;
1523 boolean firstElementAdded = false;
1524 while (bitMask != 0) {
1525 if ((bitMask & 1) != 0) {
1526 if (firstElementAdded) {
1527 sb.append(separator);
1528 } else {
1529 firstElementAdded = true;
1530 }
1531 sb.append(nameFetcher.nameOf(bitPos));
1532 }
1533 bitMask >>= 1;
1534 ++bitPos;
1535 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001536 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001537
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001538 /** @hide */
1539 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1540 final long token = proto.start(fieldId);
1541
1542 for (int transport : getTransportTypes()) {
1543 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1544 }
1545
1546 for (int capability : getCapabilities()) {
1547 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1548 }
1549
1550 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1551 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1552
1553 if (mNetworkSpecifier != null) {
1554 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1555 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001556 if (mTransportInfo != null) {
1557 // TODO b/120653863: write transport-specific info to proto?
1558 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001559
1560 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1561 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1562
1563 proto.end(token);
1564 }
1565
Hugo Benichi5df9d722016-04-25 17:16:35 +09001566 /**
1567 * @hide
1568 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001569 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001570 StringJoiner joiner = new StringJoiner("|");
1571 if (capabilities != null) {
1572 for (int c : capabilities) {
1573 joiner.add(capabilityNameOf(c));
1574 }
1575 }
1576 return joiner.toString();
1577 }
1578
1579 /**
1580 * @hide
1581 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001582 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001583 switch (capability) {
1584 case NET_CAPABILITY_MMS: return "MMS";
1585 case NET_CAPABILITY_SUPL: return "SUPL";
1586 case NET_CAPABILITY_DUN: return "DUN";
1587 case NET_CAPABILITY_FOTA: return "FOTA";
1588 case NET_CAPABILITY_IMS: return "IMS";
1589 case NET_CAPABILITY_CBS: return "CBS";
1590 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1591 case NET_CAPABILITY_IA: return "IA";
1592 case NET_CAPABILITY_RCS: return "RCS";
1593 case NET_CAPABILITY_XCAP: return "XCAP";
1594 case NET_CAPABILITY_EIMS: return "EIMS";
1595 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1596 case NET_CAPABILITY_INTERNET: return "INTERNET";
1597 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1598 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1599 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1600 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1601 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001602 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001603 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001604 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001605 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001606 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001607 default: return Integer.toString(capability);
1608 }
1609 }
1610
1611 /**
1612 * @hide
1613 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001614 @UnsupportedAppUsage
Jeff Sharkeyde570312017-10-24 21:25:50 -06001615 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001616 StringJoiner joiner = new StringJoiner("|");
1617 if (types != null) {
1618 for (int t : types) {
1619 joiner.add(transportNameOf(t));
1620 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001621 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001622 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001623 }
1624
1625 /**
1626 * @hide
1627 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001628 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001629 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001630 return "UNKNOWN";
1631 }
1632 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001633 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001634
Jeff Sharkeyde570312017-10-24 21:25:50 -06001635 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001636 Preconditions.checkArgument(
1637 isValidTransport(transport), "Invalid TransportType " + transport);
1638 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001639
1640 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1641 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1642 }
1643
1644 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1645 Preconditions.checkArgument(isValidCapability(capability),
1646 "NetworkCapability " + capability + "out of range");
1647 }
junyulai05986c62018-08-07 19:50:45 +08001648
1649 /**
1650 * Check if this {@code NetworkCapability} instance is metered.
1651 *
1652 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1653 * @hide
1654 */
1655 public boolean isMetered() {
1656 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1657 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001658}