blob: 374b3abcb641c56432a35ac98e11c011b78f0899 [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;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070020import android.annotation.SystemApi;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060021import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070022import android.os.Parcel;
23import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090024import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080025import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070026
27import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090028import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090029import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070030
Jeff Sharkeyde570312017-10-24 21:25:50 -060031import java.lang.annotation.Retention;
32import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070033import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090034import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090035import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070036
37/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070038 * Representation of the capabilities of an active network. Instances are
39 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060040 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
41 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060042 * <p>
43 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
44 * network selection. Rather than indicate a need for Wi-Fi because an
45 * application needs high bandwidth and risk obsolescence when a new, fast
46 * network appears (like LTE), the application should specify it needs high
47 * bandwidth. Similarly if an application needs an unmetered network for a bulk
48 * transfer it can specify that rather than assuming all cellular based
49 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070050 */
51public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070052 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090053 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070054
Robert Greenwalt7569f182014-06-08 16:42:59 -070055 /**
56 * @hide
57 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070058 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090059 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090060 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070061 }
62
63 public NetworkCapabilities(NetworkCapabilities nc) {
64 if (nc != null) {
65 mNetworkCapabilities = nc.mNetworkCapabilities;
66 mTransportTypes = nc.mTransportTypes;
67 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
68 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070069 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090070 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090071 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090072 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080073 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070074 }
75 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070076
77 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090078 * Completely clears the contents of this object, removing even the capabilities that are set
79 * by default when the object is constructed.
80 * @hide
81 */
82 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080083 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070084 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090085 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090086 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090087 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090088 mEstablishingVpnAppUid = INVALID_UID;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090089 }
90
91 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070092 * Represents the network's capabilities. If any are specified they will be satisfied
93 * by any Network that matches all of them.
94 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090095 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070096
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080097 /**
98 * If any capabilities specified here they must not exist in the matching Network.
99 */
100 private long mUnwantedNetworkCapabilities;
101
Jeff Sharkeyde570312017-10-24 21:25:50 -0600102 /** @hide */
103 @Retention(RetentionPolicy.SOURCE)
104 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
105 NET_CAPABILITY_MMS,
106 NET_CAPABILITY_SUPL,
107 NET_CAPABILITY_DUN,
108 NET_CAPABILITY_FOTA,
109 NET_CAPABILITY_IMS,
110 NET_CAPABILITY_CBS,
111 NET_CAPABILITY_WIFI_P2P,
112 NET_CAPABILITY_IA,
113 NET_CAPABILITY_RCS,
114 NET_CAPABILITY_XCAP,
115 NET_CAPABILITY_EIMS,
116 NET_CAPABILITY_NOT_METERED,
117 NET_CAPABILITY_INTERNET,
118 NET_CAPABILITY_NOT_RESTRICTED,
119 NET_CAPABILITY_TRUSTED,
120 NET_CAPABILITY_NOT_VPN,
121 NET_CAPABILITY_VALIDATED,
122 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600123 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600124 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900125 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900126 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800127 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600128 })
129 public @interface NetCapability { }
130
Robert Greenwalt1448f052014-04-08 13:41:39 -0700131 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700132 * Indicates this is a network that has the ability to reach the
133 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700134 */
135 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700136
137 /**
138 * Indicates this is a network that has the ability to reach the carrier's
139 * SUPL server, used to retrieve GPS information.
140 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700141 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700142
143 /**
144 * Indicates this is a network that has the ability to reach the carrier's
145 * DUN or tethering gateway.
146 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700147 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700148
149 /**
150 * Indicates this is a network that has the ability to reach the carrier's
151 * FOTA portal, used for over the air updates.
152 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700153 public static final int NET_CAPABILITY_FOTA = 3;
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 * IMS servers, used for network registration and signaling.
158 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700159 public static final int NET_CAPABILITY_IMS = 4;
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 * CBS servers, used for carrier specific services.
164 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700165 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700166
167 /**
168 * Indicates this is a network that has the ability to reach a Wi-Fi direct
169 * peer.
170 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700171 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700172
173 /**
174 * Indicates this is a network that has the ability to reach a carrier's
175 * Initial Attach servers.
176 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700177 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178
179 /**
180 * Indicates this is a network that has the ability to reach a carrier's
181 * RCS servers, used for Rich Communication Services.
182 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700183 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700184
185 /**
186 * Indicates this is a network that has the ability to reach a carrier's
187 * XCAP servers, used for configuration and control.
188 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700189 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190
191 /**
192 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700193 * Emergency IMS servers or other services, used for network signaling
194 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700196 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197
198 /**
199 * Indicates that this network is unmetered.
200 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700201 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700202
203 /**
204 * Indicates that this network should be able to reach the internet.
205 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700206 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700207
208 /**
209 * Indicates that this network is available for general use. If this is not set
210 * applications should not attempt to communicate on this network. Note that this
211 * is simply informative and not enforcement - enforcement is handled via other means.
212 * Set by default.
213 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700214 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
215
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700216 /**
217 * Indicates that the user has indicated implicit trust of this network. This
218 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
219 * BT device or a wifi the user asked to connect to. Untrusted networks
220 * are probably limited to unknown wifi AP. Set by default.
221 */
222 public static final int NET_CAPABILITY_TRUSTED = 14;
223
Paul Jensen76b610a2015-03-18 09:33:07 -0400224 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400225 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400226 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400227 */
228 public static final int NET_CAPABILITY_NOT_VPN = 15;
229
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900230 /**
231 * Indicates that connectivity on this network was successfully validated. For example, for a
232 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
233 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900234 */
235 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700236
Paul Jensen3d194ea2015-06-16 14:27:36 -0400237 /**
238 * Indicates that this network was found to have a captive portal in place last time it was
239 * probed.
240 */
241 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
242
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900243 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600244 * Indicates that this network is not roaming.
245 */
246 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
247
248 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900249 * Indicates that this network is available for use by apps, and not a network that is being
250 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900251 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600252 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900253
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900254 /**
255 * Indicates that this network is not congested.
256 * <p>
257 * When a network is congested, the device should defer network traffic that
258 * can be done at a later time without breaking developer contracts.
259 * @hide
260 */
261 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
262
Chalard Jean804b8fb2018-01-30 22:41:41 +0900263 /**
264 * Indicates that this network is not currently suspended.
265 * <p>
266 * When a network is suspended, the network's IP addresses and any connections
267 * established on the network remain valid, but the network is temporarily unable
268 * to transfer data. This can happen, for example, if a cellular network experiences
269 * a temporary loss of signal, such as when driving through a tunnel, etc.
270 * A network with this capability is not suspended, so is expected to be able to
271 * transfer data.
272 */
273 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
274
Pavel Maltsev43403202018-01-30 17:19:44 -0800275 /**
276 * Indicates that traffic that goes through this network is paid by oem. For example,
277 * this network can be used by system apps to upload telemetry data.
278 * @hide
279 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700280 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800281 public static final int NET_CAPABILITY_OEM_PAID = 22;
282
Robert Greenwalt1448f052014-04-08 13:41:39 -0700283 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800284 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700285
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700286 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900287 * Network capabilities that are expected to be mutable, i.e., can change while a particular
288 * network is connected.
289 */
290 private static final long MUTABLE_CAPABILITIES =
291 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
292 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900293 (1 << NET_CAPABILITY_TRUSTED)
294 | (1 << NET_CAPABILITY_VALIDATED)
295 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
296 | (1 << NET_CAPABILITY_NOT_ROAMING)
297 | (1 << NET_CAPABILITY_FOREGROUND)
298 | (1 << NET_CAPABILITY_NOT_CONGESTED)
299 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900300
301 /**
302 * Network capabilities that are not allowed in NetworkRequests. This exists because the
303 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
304 * capability's presence cannot be known in advance. If such a capability is requested, then we
305 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
306 * get immediately torn down because they do not have the requested capability.
307 */
308 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900309 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900310
311 /**
312 * Capabilities that are set by default when the object is constructed.
313 */
314 private static final long DEFAULT_CAPABILITIES =
315 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
316 (1 << NET_CAPABILITY_TRUSTED) |
317 (1 << NET_CAPABILITY_NOT_VPN);
318
319 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400320 * Capabilities that suggest that a network is restricted.
321 * {@see #maybeMarkCapabilitiesRestricted}.
322 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700323 @VisibleForTesting
324 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400325 (1 << NET_CAPABILITY_CBS) |
326 (1 << NET_CAPABILITY_DUN) |
327 (1 << NET_CAPABILITY_EIMS) |
328 (1 << NET_CAPABILITY_FOTA) |
329 (1 << NET_CAPABILITY_IA) |
330 (1 << NET_CAPABILITY_IMS) |
331 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev43403202018-01-30 17:19:44 -0800332 (1 << NET_CAPABILITY_XCAP) |
333 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400334
335 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700336 * Capabilities that suggest that a network is unrestricted.
337 * {@see #maybeMarkCapabilitiesRestricted}.
338 */
339 @VisibleForTesting
340 /* package */ static final long UNRESTRICTED_CAPABILITIES =
341 (1 << NET_CAPABILITY_INTERNET) |
342 (1 << NET_CAPABILITY_MMS) |
343 (1 << NET_CAPABILITY_SUPL) |
344 (1 << NET_CAPABILITY_WIFI_P2P);
345
346 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700347 * Adds the given capability to this {@code NetworkCapability} instance.
348 * Multiple capabilities may be applied sequentially. Note that when searching
349 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800350 * <p>
351 * If the given capability was previously added to the list of unwanted capabilities
352 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700353 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600354 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900355 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700356 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700357 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600358 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800359 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700360 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800361 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700362 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700363 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700364
365 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800366 * Adds the given capability to the list of unwanted capabilities of this
367 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
368 * sequentially. Note that when searching for a network to satisfy a request, the network
369 * must not contain any capability from unwanted capability list.
370 * <p>
371 * If the capability was previously added to the list of required capabilities (for
372 * example, it was there by default or added using {@link #addCapability(int)} method), then
373 * it will be removed from the list of required capabilities as well.
374 *
375 * @see #addCapability(int)
376 * @hide
377 */
378 public void addUnwantedCapability(@NetCapability int capability) {
379 checkValidCapability(capability);
380 mUnwantedNetworkCapabilities |= 1 << capability;
381 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
382 }
383
384 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700385 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800386 * <p>
387 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
388 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700389 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600390 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900391 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700392 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700393 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600394 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800395 checkValidCapability(capability);
396 final long mask = ~(1 << capability);
397 mNetworkCapabilities &= mask;
398 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700399 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700400 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700401
402 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600403 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
404 * instance.
405 *
406 * @hide
407 */
408 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
409 if (value) {
410 addCapability(capability);
411 } else {
412 removeCapability(capability);
413 }
414 return this;
415 }
416
417 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700418 * Gets all the capabilities set on this {@code NetworkCapability} instance.
419 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600420 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700421 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700422 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600423 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900424 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700425 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700426
427 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800428 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
429 *
430 * @return an array of unwanted capability values for this instance.
431 * @hide
432 */
433 public @NetCapability int[] getUnwantedCapabilities() {
434 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
435 }
436
437
438 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600439 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700440 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600441 *
442 * @hide
443 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800444 public void setCapabilities(@NetCapability int[] capabilities,
445 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600446 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800447 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600448 }
449
450 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800451 * @deprecated use {@link #setCapabilities(int[], int[])}
452 * @hide
453 */
454 @Deprecated
455 public void setCapabilities(@NetCapability int[] capabilities) {
456 setCapabilities(capabilities, new int[] {});
457 }
458
459 /**
460 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700461 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600462 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700463 * @return {@code true} if set on this instance.
464 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600465 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800466 return isValidCapability(capability)
467 && ((mNetworkCapabilities & (1 << capability)) != 0);
468 }
469
470 /** @hide */
471 public boolean hasUnwantedCapability(@NetCapability int capability) {
472 return isValidCapability(capability)
473 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700474 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700475
Robert Greenwalt1448f052014-04-08 13:41:39 -0700476 private void combineNetCapabilities(NetworkCapabilities nc) {
477 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800478 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700479 }
480
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900481 /**
482 * Convenience function that returns a human-readable description of the first mutable
483 * capability we find. Used to present an error message to apps that request mutable
484 * capabilities.
485 *
486 * @hide
487 */
488 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800489 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
490 & NON_REQUESTABLE_CAPABILITIES;
491
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900492 if (nonRequestable != 0) {
493 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900494 }
495 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900496 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900497 return null;
498 }
499
500 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800501 long requestedCapabilities = mNetworkCapabilities;
502 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
503 long providedCapabilities = nc.mNetworkCapabilities;
504
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900505 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800506 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
507 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900508 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800509 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
510 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 }
512
Robert Greenwalt06314e42014-10-29 14:04:06 -0700513 /** @hide */
514 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800515 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
516 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700517 }
518
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900519 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
520 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800521 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
522 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
523 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900524 }
525
Robert Greenwalt1448f052014-04-08 13:41:39 -0700526 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400527 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
528 * typically provided by restricted networks.
529 *
530 * TODO: consider:
531 * - Renaming it to guessRestrictedCapability and make it set the
532 * restricted capability bit in addition to clearing it.
533 * @hide
534 */
535 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700536 // Verify there aren't any unrestricted capabilities. If there are we say
537 // the whole thing is unrestricted.
538 final boolean hasUnrestrictedCapabilities =
539 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
540
541 // Must have at least some restricted capabilities.
542 final boolean hasRestrictedCapabilities =
543 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
544
545 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400546 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400547 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400548 }
549
550 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700551 * Representing the transport type. Apps should generally not care about transport. A
552 * request for a fast internet connection could be satisfied by a number of different
553 * transports. If any are specified here it will be satisfied a Network that matches
554 * any of them. If a caller doesn't care about the transport it should not specify any.
555 */
556 private long mTransportTypes;
557
Jeff Sharkeyde570312017-10-24 21:25:50 -0600558 /** @hide */
559 @Retention(RetentionPolicy.SOURCE)
560 @IntDef(prefix = { "TRANSPORT_" }, value = {
561 TRANSPORT_CELLULAR,
562 TRANSPORT_WIFI,
563 TRANSPORT_BLUETOOTH,
564 TRANSPORT_ETHERNET,
565 TRANSPORT_VPN,
566 TRANSPORT_WIFI_AWARE,
567 TRANSPORT_LOWPAN,
568 })
569 public @interface Transport { }
570
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700572 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700573 */
574 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700575
576 /**
577 * Indicates this network uses a Wi-Fi transport.
578 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700579 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700580
581 /**
582 * Indicates this network uses a Bluetooth transport.
583 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700585
586 /**
587 * Indicates this network uses an Ethernet transport.
588 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700589 public static final int TRANSPORT_ETHERNET = 3;
590
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400591 /**
592 * Indicates this network uses a VPN transport.
593 */
594 public static final int TRANSPORT_VPN = 4;
595
Etan Cohen305ea282016-06-20 09:27:12 -0700596 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700597 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700598 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700599 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700600
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700601 /**
602 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700603 */
604 public static final int TRANSPORT_LOWPAN = 6;
605
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900606 /** @hide */
607 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
608 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700609 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700610
Hugo Benichi16f0a942017-06-20 14:07:59 +0900611 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600612 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900613 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
614 }
615
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900616 private static final String[] TRANSPORT_NAMES = {
617 "CELLULAR",
618 "WIFI",
619 "BLUETOOTH",
620 "ETHERNET",
621 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700622 "WIFI_AWARE",
623 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900624 };
625
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700626 /**
627 * Adds the given transport type to this {@code NetworkCapability} instance.
628 * Multiple transports may be applied sequentially. Note that when searching
629 * for a network to satisfy a request, any listed in the request will satisfy the request.
630 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
631 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
632 * to be selected. This is logically different than
633 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
634 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600635 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900636 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700637 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700638 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600639 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900640 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700641 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700642 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700643 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700644 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700645
646 /**
647 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
648 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600649 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900650 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700651 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700652 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600653 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900654 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700655 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700656 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700657 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700658 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700659
660 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600661 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
662 * instance.
663 *
664 * @hide
665 */
666 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
667 if (value) {
668 addTransportType(transportType);
669 } else {
670 removeTransportType(transportType);
671 }
672 return this;
673 }
674
675 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700676 * Gets all the transports set on this {@code NetworkCapability} instance.
677 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600678 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700679 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700680 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600681 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900682 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700683 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700684
685 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600686 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700687 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600688 *
689 * @hide
690 */
691 public void setTransportTypes(@Transport int[] transportTypes) {
692 mTransportTypes = BitUtils.packBits(transportTypes);
693 }
694
695 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700696 * Tests for the presence of a transport on this instance.
697 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600698 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700699 * @return {@code true} if set on this instance.
700 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600701 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900702 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700703 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700704
705 private void combineTransportTypes(NetworkCapabilities nc) {
706 this.mTransportTypes |= nc.mTransportTypes;
707 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900708
Robert Greenwalt1448f052014-04-08 13:41:39 -0700709 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
710 return ((this.mTransportTypes == 0) ||
711 ((this.mTransportTypes & nc.mTransportTypes) != 0));
712 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900713
Robert Greenwalt06314e42014-10-29 14:04:06 -0700714 /** @hide */
715 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700716 return (nc.mTransportTypes == this.mTransportTypes);
717 }
718
719 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900720 * UID of the app that manages this network, or INVALID_UID if none/unknown.
721 *
722 * This field keeps track of the UID of the app that created this network and is in charge
723 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
724 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
725 * to create networks.
726 *
727 * Because this field is only used in the services side (and to avoid apps being able to
728 * set this to whatever they want), this field is not parcelled and will not be conserved
729 * across the IPC boundary.
730 * @hide
731 */
732 private int mEstablishingVpnAppUid = INVALID_UID;
733
734 /**
735 * Set the UID of the managing app.
736 * @hide
737 */
738 public void setEstablishingVpnAppUid(final int uid) {
739 mEstablishingVpnAppUid = uid;
740 }
741
742 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600743 * Value indicating that link bandwidth is unspecified.
744 * @hide
745 */
746 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
747
748 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700749 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
750 * for the first hop on the given transport. It is not measured, but may take into account
751 * link parameters (Radio technology, allocated channels, etc).
752 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600753 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
754 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700755
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700756 /**
757 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
758 * the estimated first hop transport bandwidth.
759 * <p>
760 * Note that when used to request a network, this specifies the minimum acceptable.
761 * When received as the state of an existing network this specifies the typical
762 * first hop bandwidth expected. This is never measured, but rather is inferred
763 * from technology type and other link parameters. It could be used to differentiate
764 * between very slow 1xRTT cellular links and other faster networks or even between
765 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
766 * fast backhauls and slow backhauls.
767 *
768 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700769 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700770 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600771 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600773 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700774 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700775
776 /**
777 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
778 * the estimated first hop transport bandwidth.
779 *
780 * @return The estimated first hop upstream (device to network) bandwidth.
781 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700782 public int getLinkUpstreamBandwidthKbps() {
783 return mLinkUpBandwidthKbps;
784 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700785
786 /**
787 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
788 * the estimated first hop transport bandwidth.
789 * <p>
790 * Note that when used to request a network, this specifies the minimum acceptable.
791 * When received as the state of an existing network this specifies the typical
792 * first hop bandwidth expected. This is never measured, but rather is inferred
793 * from technology type and other link parameters. It could be used to differentiate
794 * between very slow 1xRTT cellular links and other faster networks or even between
795 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
796 * fast backhauls and slow backhauls.
797 *
798 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700799 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700800 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600801 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700802 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600803 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700804 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700805
806 /**
807 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
808 * the estimated first hop transport bandwidth.
809 *
810 * @return The estimated first hop downstream (network to device) bandwidth.
811 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700812 public int getLinkDownstreamBandwidthKbps() {
813 return mLinkDownBandwidthKbps;
814 }
815
816 private void combineLinkBandwidths(NetworkCapabilities nc) {
817 this.mLinkUpBandwidthKbps =
818 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
819 this.mLinkDownBandwidthKbps =
820 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
821 }
822 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
823 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
824 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
825 }
826 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
827 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
828 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
829 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600830 /** @hide */
831 public static int minBandwidth(int a, int b) {
832 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
833 return b;
834 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
835 return a;
836 } else {
837 return Math.min(a, b);
838 }
839 }
840 /** @hide */
841 public static int maxBandwidth(int a, int b) {
842 return Math.max(a, b);
843 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700844
Etan Cohena7434272017-04-03 12:17:51 -0700845 private NetworkSpecifier mNetworkSpecifier = null;
846
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700847 /**
848 * Sets the optional bearer specific network specifier.
849 * This has no meaning if a single transport is also not specified, so calling
850 * this without a single transport set will generate an exception, as will
851 * subsequently adding or removing transports after this is set.
852 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700853 *
Etan Cohena7434272017-04-03 12:17:51 -0700854 * @param networkSpecifier A concrete, parcelable framework class that extends
855 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900856 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700857 * @hide
858 */
Etan Cohena7434272017-04-03 12:17:51 -0700859 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
860 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700861 throw new IllegalStateException("Must have a single transport specified to use " +
862 "setNetworkSpecifier");
863 }
Etan Cohena7434272017-04-03 12:17:51 -0700864
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700865 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700866
Pierre Imaic8419a82016-03-22 17:54:54 +0900867 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700868 }
869
870 /**
871 * Gets the optional bearer specific network specifier.
872 *
Etan Cohena7434272017-04-03 12:17:51 -0700873 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
874 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700875 * @hide
876 */
Etan Cohena7434272017-04-03 12:17:51 -0700877 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700878 return mNetworkSpecifier;
879 }
880
881 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700882 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700883 throw new IllegalStateException("Can't combine two networkSpecifiers");
884 }
Etan Cohena7434272017-04-03 12:17:51 -0700885 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700886 }
Etan Cohena7434272017-04-03 12:17:51 -0700887
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700888 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700889 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
890 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700891 }
Etan Cohena7434272017-04-03 12:17:51 -0700892
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700894 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700895 }
896
Robert Greenwalt1448f052014-04-08 13:41:39 -0700897 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900898 * Magic value that indicates no signal strength provided. A request specifying this value is
899 * always satisfied.
900 *
901 * @hide
902 */
903 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
904
905 /**
906 * Signal strength. This is a signed integer, and higher values indicate better signal.
907 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
908 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700909 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900910
911 /**
912 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
913 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
914 * reported by WifiManager.
915 * <p>
916 * Note that when used to register a network callback, this specifies the minimum acceptable
917 * signal strength. When received as the state of an existing network it specifies the current
918 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
919 * effect when requesting a callback.
920 *
921 * @param signalStrength the bearer-specific signal strength.
922 * @hide
923 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600924 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900925 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600926 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900927 }
928
929 /**
930 * Returns {@code true} if this object specifies a signal strength.
931 *
932 * @hide
933 */
934 public boolean hasSignalStrength() {
935 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
936 }
937
938 /**
939 * Retrieves the signal strength.
940 *
941 * @return The bearer-specific signal strength.
942 * @hide
943 */
944 public int getSignalStrength() {
945 return mSignalStrength;
946 }
947
948 private void combineSignalStrength(NetworkCapabilities nc) {
949 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
950 }
951
952 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
953 return this.mSignalStrength <= nc.mSignalStrength;
954 }
955
956 private boolean equalsSignalStrength(NetworkCapabilities nc) {
957 return this.mSignalStrength == nc.mSignalStrength;
958 }
959
960 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900961 * List of UIDs this network applies to. No restriction if null.
962 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900963 * For networks, mUids represent the list of network this applies to, and null means this
964 * network applies to all UIDs.
965 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
966 * must be included in a network so that they match. As an exception to the general rule,
967 * a null mUids field for requests mean "no requirements" rather than what the general rule
968 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
969 * of this API expect in practice. A network that must match all UIDs can still be
970 * expressed with a set ranging the entire set of possible UIDs.
971 * <p>
972 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900973 * the UIDs in this list, and it is their default network. Apps in this list that wish to
974 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
975 * member is null, then the network is not restricted by app UID. If it's an empty list, then
976 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900977 * As a special exception, the app managing this network (as identified by its UID stored in
978 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
979 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
980 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900981 * <p>
982 * Please note that in principle a single app can be associated with multiple UIDs because
983 * each app will have a different UID when it's run as a different (macro-)user. A single
984 * macro user can only have a single active VPN app at any given time however.
985 * <p>
986 * Also please be aware this class does not try to enforce any normalization on this. Callers
987 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
988 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
989 * their own (like requiring sortedness or no overlap) they need to enforce it
990 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
991 * or overlapping ranges are present.
992 *
993 * @hide
994 */
Chalard Jean477e36c2018-01-25 09:41:51 +0900995 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900996
997 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900998 * Convenience method to set the UIDs this network applies to to a single UID.
999 * @hide
1000 */
1001 public NetworkCapabilities setSingleUid(int uid) {
1002 final ArraySet<UidRange> identity = new ArraySet<>(1);
1003 identity.add(new UidRange(uid, uid));
1004 setUids(identity);
1005 return this;
1006 }
1007
1008 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001009 * Set the list of UIDs this network applies to.
1010 * This makes a copy of the set so that callers can't modify it after the call.
1011 * @hide
1012 */
1013 public NetworkCapabilities setUids(Set<UidRange> uids) {
1014 if (null == uids) {
1015 mUids = null;
1016 } else {
1017 mUids = new ArraySet<>(uids);
1018 }
1019 return this;
1020 }
1021
1022 /**
1023 * Get the list of UIDs this network applies to.
1024 * This returns a copy of the set so that callers can't modify the original object.
1025 * @hide
1026 */
1027 public Set<UidRange> getUids() {
1028 return null == mUids ? null : new ArraySet<>(mUids);
1029 }
1030
1031 /**
1032 * Test whether this network applies to this UID.
1033 * @hide
1034 */
1035 public boolean appliesToUid(int uid) {
1036 if (null == mUids) return true;
1037 for (UidRange range : mUids) {
1038 if (range.contains(uid)) {
1039 return true;
1040 }
1041 }
1042 return false;
1043 }
1044
1045 /**
1046 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
1047 * <p>
1048 * This test only checks whether equal range objects are in both sets. It will
1049 * return false if the ranges are not exactly the same, even if the covered UIDs
1050 * are for an equivalent result.
1051 * <p>
1052 * Note that this method is not very optimized, which is fine as long as it's not used very
1053 * often.
1054 * <p>
1055 * nc is assumed nonnull.
1056 *
1057 * @hide
1058 */
1059 @VisibleForTesting
1060 public boolean equalsUids(NetworkCapabilities nc) {
1061 Set<UidRange> comparedUids = nc.mUids;
1062 if (null == comparedUids) return null == mUids;
1063 if (null == mUids) return false;
1064 // Make a copy so it can be mutated to check that all ranges in mUids
1065 // also are in uids.
1066 final Set<UidRange> uids = new ArraySet<>(mUids);
1067 for (UidRange range : comparedUids) {
1068 if (!uids.contains(range)) {
1069 return false;
1070 }
1071 uids.remove(range);
1072 }
1073 return uids.isEmpty();
1074 }
1075
1076 /**
1077 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1078 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001079 * This method is called on the NetworkCapabilities embedded in a request with the
1080 * capabilities of an available network. It checks whether all the UIDs from this listen
1081 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1082 * in the passed nc (representing the UIDs that this network is available to).
1083 * <p>
1084 * As a special exception, the UID that created the passed network (as represented by its
1085 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1086 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1087 * can see its own network when it listens for it.
1088 * <p>
1089 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001090 * @see #appliesToUid
1091 * @hide
1092 */
1093 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001094 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001095 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001096 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001097 if (!nc.appliesToUidRange(requiredRange)) {
1098 return false;
1099 }
1100 }
1101 return true;
1102 }
1103
1104 /**
1105 * Returns whether this network applies to the passed ranges.
1106 * This assumes that to apply, the passed range has to be entirely contained
1107 * within one of the ranges this network applies to. If the ranges are not normalized,
1108 * this method may return false even though all required UIDs are covered because no
1109 * single range contained them all.
1110 * @hide
1111 */
1112 @VisibleForTesting
1113 public boolean appliesToUidRange(UidRange requiredRange) {
1114 if (null == mUids) return true;
1115 for (UidRange uidRange : mUids) {
1116 if (uidRange.containsRange(requiredRange)) {
1117 return true;
1118 }
1119 }
1120 return false;
1121 }
1122
1123 /**
1124 * Combine the UIDs this network currently applies to with the UIDs the passed
1125 * NetworkCapabilities apply to.
1126 * nc is assumed nonnull.
1127 */
1128 private void combineUids(NetworkCapabilities nc) {
1129 if (null == nc.mUids || null == mUids) {
1130 mUids = null;
1131 return;
1132 }
1133 mUids.addAll(nc.mUids);
1134 }
1135
1136 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001137 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001138 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001139 */
1140 public void combineCapabilities(NetworkCapabilities nc) {
1141 combineNetCapabilities(nc);
1142 combineTransportTypes(nc);
1143 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001144 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001145 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001146 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001147 }
1148
1149 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001150 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1151 *
1152 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1153 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1154 * bandwidth, signal strength, or validation / captive portal status.
1155 *
1156 * @hide
1157 */
1158 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001159 return (nc != null
1160 && satisfiedByNetCapabilities(nc, onlyImmutable)
1161 && satisfiedByTransportTypes(nc)
1162 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1163 && satisfiedBySpecifier(nc)
1164 && (onlyImmutable || satisfiedBySignalStrength(nc))
1165 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001166 }
1167
1168 /**
1169 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1170 *
1171 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1172 *
1173 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001174 */
1175 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001176 return satisfiedByNetworkCapabilities(nc, false);
1177 }
1178
1179 /**
1180 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1181 *
1182 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1183 *
1184 * @hide
1185 */
1186 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1187 return satisfiedByNetworkCapabilities(nc, true);
1188 }
1189
1190 /**
1191 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001192 * {@code NetworkCapabilities} and return a String describing any difference.
1193 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001194 *
1195 * @hide
1196 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001197 public String describeImmutableDifferences(NetworkCapabilities that) {
1198 if (that == null) {
1199 return "other NetworkCapabilities was null";
1200 }
1201
1202 StringJoiner joiner = new StringJoiner(", ");
1203
Hugo Benichieae7a222017-07-25 11:40:56 +09001204 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1205 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001206 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001207 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1208 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1209 if (oldImmutableCapabilities != newImmutableCapabilities) {
1210 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1211 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1212 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1213 }
1214
1215 if (!equalsSpecifier(that)) {
1216 NetworkSpecifier before = this.getNetworkSpecifier();
1217 NetworkSpecifier after = that.getNetworkSpecifier();
1218 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1219 }
1220
1221 if (!equalsTransportTypes(that)) {
1222 String before = transportNamesOf(this.getTransportTypes());
1223 String after = transportNamesOf(that.getTransportTypes());
1224 joiner.add(String.format("transports changed: %s -> %s", before, after));
1225 }
1226
1227 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001228 }
1229
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001230 /**
1231 * Checks that our requestable capabilities are the same as those of the given
1232 * {@code NetworkCapabilities}.
1233 *
1234 * @hide
1235 */
1236 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1237 if (nc == null) return false;
1238 return (equalsNetCapabilitiesRequestable(nc) &&
1239 equalsTransportTypes(nc) &&
1240 equalsSpecifier(nc));
1241 }
1242
Robert Greenwalt1448f052014-04-08 13:41:39 -07001243 @Override
1244 public boolean equals(Object obj) {
1245 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001246 NetworkCapabilities that = (NetworkCapabilities) obj;
1247 return (equalsNetCapabilities(that)
1248 && equalsTransportTypes(that)
1249 && equalsLinkBandwidths(that)
1250 && equalsSignalStrength(that)
1251 && equalsSpecifier(that)
1252 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001253 }
1254
1255 @Override
1256 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001257 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001258 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001259 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1260 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1261 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1262 + ((int) (mTransportTypes >> 32) * 13)
1263 + (mLinkUpBandwidthKbps * 17)
1264 + (mLinkDownBandwidthKbps * 19)
1265 + Objects.hashCode(mNetworkSpecifier) * 23
1266 + (mSignalStrength * 29)
1267 + Objects.hashCode(mUids) * 31;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001268 }
1269
Wink Saville4e2dea72014-09-20 11:04:03 -07001270 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001271 public int describeContents() {
1272 return 0;
1273 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001274 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001275 public void writeToParcel(Parcel dest, int flags) {
1276 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001277 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001278 dest.writeLong(mTransportTypes);
1279 dest.writeInt(mLinkUpBandwidthKbps);
1280 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001281 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001282 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001283 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001284 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001285
Robert Greenwalt1448f052014-04-08 13:41:39 -07001286 public static final Creator<NetworkCapabilities> CREATOR =
1287 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001288 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001289 public NetworkCapabilities createFromParcel(Parcel in) {
1290 NetworkCapabilities netCap = new NetworkCapabilities();
1291
1292 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001293 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001294 netCap.mTransportTypes = in.readLong();
1295 netCap.mLinkUpBandwidthKbps = in.readInt();
1296 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001297 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001298 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001299 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1300 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001301 return netCap;
1302 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001303 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001304 public NetworkCapabilities[] newArray(int size) {
1305 return new NetworkCapabilities[size];
1306 }
1307 };
1308
Wink Saville4e2dea72014-09-20 11:04:03 -07001309 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001310 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001311 final StringBuilder sb = new StringBuilder("[");
1312 if (0 != mTransportTypes) {
1313 sb.append(" Transports: ");
1314 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1315 NetworkCapabilities::transportNameOf, "|");
1316 }
1317 if (0 != mNetworkCapabilities) {
1318 sb.append(" Capabilities: ");
1319 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1320 NetworkCapabilities::capabilityNameOf, "&");
1321 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001322 if (0 != mNetworkCapabilities) {
1323 sb.append(" Unwanted: ");
1324 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1325 NetworkCapabilities::capabilityNameOf, "&");
1326 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001327 if (mLinkUpBandwidthKbps > 0) {
1328 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1329 }
1330 if (mLinkDownBandwidthKbps > 0) {
1331 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1332 }
1333 if (mNetworkSpecifier != null) {
1334 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1335 }
1336 if (hasSignalStrength()) {
1337 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001338 }
1339
Chalard Jean07ace0f2018-02-26 19:00:45 +09001340 if (null != mUids) {
1341 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1342 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1343 } else {
1344 sb.append(" Uids: <").append(mUids).append(">");
1345 }
1346 }
1347 if (mEstablishingVpnAppUid != INVALID_UID) {
1348 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1349 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001350
Chalard Jean07ace0f2018-02-26 19:00:45 +09001351 sb.append("]");
1352 return sb.toString();
1353 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001354
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001355
Chalard Jean07ace0f2018-02-26 19:00:45 +09001356 private interface NameOf {
1357 String nameOf(int value);
1358 }
1359 /**
1360 * @hide
1361 */
1362 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1363 long bitMask, NameOf nameFetcher, String separator) {
1364 int bitPos = 0;
1365 boolean firstElementAdded = false;
1366 while (bitMask != 0) {
1367 if ((bitMask & 1) != 0) {
1368 if (firstElementAdded) {
1369 sb.append(separator);
1370 } else {
1371 firstElementAdded = true;
1372 }
1373 sb.append(nameFetcher.nameOf(bitPos));
1374 }
1375 bitMask >>= 1;
1376 ++bitPos;
1377 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001379
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001380 /** @hide */
1381 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1382 final long token = proto.start(fieldId);
1383
1384 for (int transport : getTransportTypes()) {
1385 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1386 }
1387
1388 for (int capability : getCapabilities()) {
1389 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1390 }
1391
1392 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1393 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1394
1395 if (mNetworkSpecifier != null) {
1396 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1397 }
1398
1399 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1400 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1401
1402 proto.end(token);
1403 }
1404
Hugo Benichi5df9d722016-04-25 17:16:35 +09001405 /**
1406 * @hide
1407 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001408 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001409 StringJoiner joiner = new StringJoiner("|");
1410 if (capabilities != null) {
1411 for (int c : capabilities) {
1412 joiner.add(capabilityNameOf(c));
1413 }
1414 }
1415 return joiner.toString();
1416 }
1417
1418 /**
1419 * @hide
1420 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001421 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001422 switch (capability) {
1423 case NET_CAPABILITY_MMS: return "MMS";
1424 case NET_CAPABILITY_SUPL: return "SUPL";
1425 case NET_CAPABILITY_DUN: return "DUN";
1426 case NET_CAPABILITY_FOTA: return "FOTA";
1427 case NET_CAPABILITY_IMS: return "IMS";
1428 case NET_CAPABILITY_CBS: return "CBS";
1429 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1430 case NET_CAPABILITY_IA: return "IA";
1431 case NET_CAPABILITY_RCS: return "RCS";
1432 case NET_CAPABILITY_XCAP: return "XCAP";
1433 case NET_CAPABILITY_EIMS: return "EIMS";
1434 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1435 case NET_CAPABILITY_INTERNET: return "INTERNET";
1436 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1437 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1438 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1439 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1440 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001441 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001442 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001443 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001444 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001445 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001446 default: return Integer.toString(capability);
1447 }
1448 }
1449
1450 /**
1451 * @hide
1452 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001453 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001454 StringJoiner joiner = new StringJoiner("|");
1455 if (types != null) {
1456 for (int t : types) {
1457 joiner.add(transportNameOf(t));
1458 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001459 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001460 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001461 }
1462
1463 /**
1464 * @hide
1465 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001466 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001467 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001468 return "UNKNOWN";
1469 }
1470 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001471 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001472
Jeff Sharkeyde570312017-10-24 21:25:50 -06001473 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001474 Preconditions.checkArgument(
1475 isValidTransport(transport), "Invalid TransportType " + transport);
1476 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001477
1478 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1479 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1480 }
1481
1482 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1483 Preconditions.checkArgument(isValidCapability(capability),
1484 "NetworkCapability " + capability + "out of range");
1485 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001486}