blob: c314a358a24a5b7808e09641e29640a16ace7a28 [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.
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000321 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400322 */
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) |
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000332 (1 << NET_CAPABILITY_XCAP);
333
334 /**
335 * Capabilities that force network to be restricted.
336 * {@see #maybeMarkCapabilitiesRestricted}.
337 */
338 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800339 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400340
341 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700342 * Capabilities that suggest that a network is unrestricted.
343 * {@see #maybeMarkCapabilitiesRestricted}.
344 */
345 @VisibleForTesting
346 /* package */ static final long UNRESTRICTED_CAPABILITIES =
347 (1 << NET_CAPABILITY_INTERNET) |
348 (1 << NET_CAPABILITY_MMS) |
349 (1 << NET_CAPABILITY_SUPL) |
350 (1 << NET_CAPABILITY_WIFI_P2P);
351
352 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700353 * Adds the given capability to this {@code NetworkCapability} instance.
354 * Multiple capabilities may be applied sequentially. Note that when searching
355 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800356 * <p>
357 * If the given capability was previously added to the list of unwanted capabilities
358 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700359 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600360 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900361 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700362 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700363 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600364 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800365 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700366 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800367 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700368 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700369 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700370
371 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800372 * Adds the given capability to the list of unwanted capabilities of this
373 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
374 * sequentially. Note that when searching for a network to satisfy a request, the network
375 * must not contain any capability from unwanted capability list.
376 * <p>
377 * If the capability was previously added to the list of required capabilities (for
378 * example, it was there by default or added using {@link #addCapability(int)} method), then
379 * it will be removed from the list of required capabilities as well.
380 *
381 * @see #addCapability(int)
382 * @hide
383 */
384 public void addUnwantedCapability(@NetCapability int capability) {
385 checkValidCapability(capability);
386 mUnwantedNetworkCapabilities |= 1 << capability;
387 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
388 }
389
390 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700391 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800392 * <p>
393 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
394 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700395 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600396 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900397 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700398 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700399 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600400 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800401 checkValidCapability(capability);
402 final long mask = ~(1 << capability);
403 mNetworkCapabilities &= mask;
404 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700405 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700406 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700407
408 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600409 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
410 * instance.
411 *
412 * @hide
413 */
414 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
415 if (value) {
416 addCapability(capability);
417 } else {
418 removeCapability(capability);
419 }
420 return this;
421 }
422
423 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700424 * Gets all the capabilities set on this {@code NetworkCapability} instance.
425 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600426 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700427 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700428 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600429 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900430 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700431 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700432
433 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800434 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
435 *
436 * @return an array of unwanted capability values for this instance.
437 * @hide
438 */
439 public @NetCapability int[] getUnwantedCapabilities() {
440 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
441 }
442
443
444 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600445 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700446 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600447 *
448 * @hide
449 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800450 public void setCapabilities(@NetCapability int[] capabilities,
451 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600452 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800453 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600454 }
455
456 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800457 * @deprecated use {@link #setCapabilities(int[], int[])}
458 * @hide
459 */
460 @Deprecated
461 public void setCapabilities(@NetCapability int[] capabilities) {
462 setCapabilities(capabilities, new int[] {});
463 }
464
465 /**
466 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700467 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600468 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700469 * @return {@code true} if set on this instance.
470 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600471 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800472 return isValidCapability(capability)
473 && ((mNetworkCapabilities & (1 << capability)) != 0);
474 }
475
476 /** @hide */
477 public boolean hasUnwantedCapability(@NetCapability int capability) {
478 return isValidCapability(capability)
479 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700480 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700481
Robert Greenwalt1448f052014-04-08 13:41:39 -0700482 private void combineNetCapabilities(NetworkCapabilities nc) {
483 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800484 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700485 }
486
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900487 /**
488 * Convenience function that returns a human-readable description of the first mutable
489 * capability we find. Used to present an error message to apps that request mutable
490 * capabilities.
491 *
492 * @hide
493 */
494 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800495 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
496 & NON_REQUESTABLE_CAPABILITIES;
497
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900498 if (nonRequestable != 0) {
499 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900500 }
501 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900502 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900503 return null;
504 }
505
506 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800507 long requestedCapabilities = mNetworkCapabilities;
508 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
509 long providedCapabilities = nc.mNetworkCapabilities;
510
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900511 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800512 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
513 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900514 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800515 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
516 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700517 }
518
Robert Greenwalt06314e42014-10-29 14:04:06 -0700519 /** @hide */
520 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800521 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
522 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700523 }
524
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900525 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
526 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800527 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
528 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
529 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900530 }
531
Robert Greenwalt1448f052014-04-08 13:41:39 -0700532 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400533 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
534 * typically provided by restricted networks.
535 *
536 * TODO: consider:
537 * - Renaming it to guessRestrictedCapability and make it set the
538 * restricted capability bit in addition to clearing it.
539 * @hide
540 */
541 public void maybeMarkCapabilitiesRestricted() {
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000542 // Check if we have any capability that forces the network to be restricted.
543 final boolean forceRestrictedCapability =
544 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
545
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700546 // Verify there aren't any unrestricted capabilities. If there are we say
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000547 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700548 final boolean hasUnrestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000549 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700550
551 // Must have at least some restricted capabilities.
552 final boolean hasRestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000553 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700554
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000555 if (forceRestrictedCapability
556 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400557 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400558 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400559 }
560
561 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700562 * Representing the transport type. Apps should generally not care about transport. A
563 * request for a fast internet connection could be satisfied by a number of different
564 * transports. If any are specified here it will be satisfied a Network that matches
565 * any of them. If a caller doesn't care about the transport it should not specify any.
566 */
567 private long mTransportTypes;
568
Jeff Sharkeyde570312017-10-24 21:25:50 -0600569 /** @hide */
570 @Retention(RetentionPolicy.SOURCE)
571 @IntDef(prefix = { "TRANSPORT_" }, value = {
572 TRANSPORT_CELLULAR,
573 TRANSPORT_WIFI,
574 TRANSPORT_BLUETOOTH,
575 TRANSPORT_ETHERNET,
576 TRANSPORT_VPN,
577 TRANSPORT_WIFI_AWARE,
578 TRANSPORT_LOWPAN,
579 })
580 public @interface Transport { }
581
Robert Greenwalt1448f052014-04-08 13:41:39 -0700582 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700583 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 */
585 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700586
587 /**
588 * Indicates this network uses a Wi-Fi transport.
589 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700590 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700591
592 /**
593 * Indicates this network uses a Bluetooth transport.
594 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700595 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700596
597 /**
598 * Indicates this network uses an Ethernet transport.
599 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700600 public static final int TRANSPORT_ETHERNET = 3;
601
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400602 /**
603 * Indicates this network uses a VPN transport.
604 */
605 public static final int TRANSPORT_VPN = 4;
606
Etan Cohen305ea282016-06-20 09:27:12 -0700607 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700608 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700609 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700610 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700611
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700612 /**
613 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700614 */
615 public static final int TRANSPORT_LOWPAN = 6;
616
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900617 /** @hide */
618 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
619 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700620 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700621
Hugo Benichi16f0a942017-06-20 14:07:59 +0900622 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600623 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900624 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
625 }
626
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900627 private static final String[] TRANSPORT_NAMES = {
628 "CELLULAR",
629 "WIFI",
630 "BLUETOOTH",
631 "ETHERNET",
632 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700633 "WIFI_AWARE",
634 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900635 };
636
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700637 /**
638 * Adds the given transport type to this {@code NetworkCapability} instance.
639 * Multiple transports may be applied sequentially. Note that when searching
640 * for a network to satisfy a request, any listed in the request will satisfy the request.
641 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
642 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
643 * to be selected. This is logically different than
644 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
645 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600646 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900647 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700648 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700649 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600650 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900651 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700652 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700653 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700654 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700655 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700656
657 /**
658 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
659 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600660 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900661 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700662 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700663 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600664 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900665 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700666 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700667 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700668 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700669 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700670
671 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600672 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
673 * instance.
674 *
675 * @hide
676 */
677 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
678 if (value) {
679 addTransportType(transportType);
680 } else {
681 removeTransportType(transportType);
682 }
683 return this;
684 }
685
686 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700687 * Gets all the transports set on this {@code NetworkCapability} instance.
688 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600689 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700690 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700691 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600692 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900693 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700694 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700695
696 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600697 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700698 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600699 *
700 * @hide
701 */
702 public void setTransportTypes(@Transport int[] transportTypes) {
703 mTransportTypes = BitUtils.packBits(transportTypes);
704 }
705
706 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700707 * Tests for the presence of a transport on this instance.
708 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600709 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700710 * @return {@code true} if set on this instance.
711 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600712 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900713 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700714 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700715
716 private void combineTransportTypes(NetworkCapabilities nc) {
717 this.mTransportTypes |= nc.mTransportTypes;
718 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900719
Robert Greenwalt1448f052014-04-08 13:41:39 -0700720 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
721 return ((this.mTransportTypes == 0) ||
722 ((this.mTransportTypes & nc.mTransportTypes) != 0));
723 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900724
Robert Greenwalt06314e42014-10-29 14:04:06 -0700725 /** @hide */
726 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700727 return (nc.mTransportTypes == this.mTransportTypes);
728 }
729
730 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900731 * UID of the app that manages this network, or INVALID_UID if none/unknown.
732 *
733 * This field keeps track of the UID of the app that created this network and is in charge
734 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
735 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
736 * to create networks.
737 *
738 * Because this field is only used in the services side (and to avoid apps being able to
739 * set this to whatever they want), this field is not parcelled and will not be conserved
740 * across the IPC boundary.
741 * @hide
742 */
743 private int mEstablishingVpnAppUid = INVALID_UID;
744
745 /**
746 * Set the UID of the managing app.
747 * @hide
748 */
749 public void setEstablishingVpnAppUid(final int uid) {
750 mEstablishingVpnAppUid = uid;
751 }
752
753 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600754 * Value indicating that link bandwidth is unspecified.
755 * @hide
756 */
757 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
758
759 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700760 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
761 * for the first hop on the given transport. It is not measured, but may take into account
762 * link parameters (Radio technology, allocated channels, etc).
763 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600764 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
765 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700766
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700767 /**
768 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
769 * the estimated first hop transport bandwidth.
770 * <p>
771 * Note that when used to request a network, this specifies the minimum acceptable.
772 * When received as the state of an existing network this specifies the typical
773 * first hop bandwidth expected. This is never measured, but rather is inferred
774 * from technology type and other link parameters. It could be used to differentiate
775 * between very slow 1xRTT cellular links and other faster networks or even between
776 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
777 * fast backhauls and slow backhauls.
778 *
779 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700780 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700781 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600782 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700783 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600784 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700785 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700786
787 /**
788 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
789 * the estimated first hop transport bandwidth.
790 *
791 * @return The estimated first hop upstream (device to network) bandwidth.
792 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700793 public int getLinkUpstreamBandwidthKbps() {
794 return mLinkUpBandwidthKbps;
795 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700796
797 /**
798 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
799 * the estimated first hop transport bandwidth.
800 * <p>
801 * Note that when used to request a network, this specifies the minimum acceptable.
802 * When received as the state of an existing network this specifies the typical
803 * first hop bandwidth expected. This is never measured, but rather is inferred
804 * from technology type and other link parameters. It could be used to differentiate
805 * between very slow 1xRTT cellular links and other faster networks or even between
806 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
807 * fast backhauls and slow backhauls.
808 *
809 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700810 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700811 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600812 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700813 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600814 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700815 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700816
817 /**
818 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
819 * the estimated first hop transport bandwidth.
820 *
821 * @return The estimated first hop downstream (network to device) bandwidth.
822 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700823 public int getLinkDownstreamBandwidthKbps() {
824 return mLinkDownBandwidthKbps;
825 }
826
827 private void combineLinkBandwidths(NetworkCapabilities nc) {
828 this.mLinkUpBandwidthKbps =
829 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
830 this.mLinkDownBandwidthKbps =
831 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
832 }
833 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
834 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
835 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
836 }
837 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
838 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
839 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
840 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600841 /** @hide */
842 public static int minBandwidth(int a, int b) {
843 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
844 return b;
845 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
846 return a;
847 } else {
848 return Math.min(a, b);
849 }
850 }
851 /** @hide */
852 public static int maxBandwidth(int a, int b) {
853 return Math.max(a, b);
854 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700855
Etan Cohena7434272017-04-03 12:17:51 -0700856 private NetworkSpecifier mNetworkSpecifier = null;
857
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700858 /**
859 * Sets the optional bearer specific network specifier.
860 * This has no meaning if a single transport is also not specified, so calling
861 * this without a single transport set will generate an exception, as will
862 * subsequently adding or removing transports after this is set.
863 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700864 *
Etan Cohena7434272017-04-03 12:17:51 -0700865 * @param networkSpecifier A concrete, parcelable framework class that extends
866 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900867 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700868 * @hide
869 */
Etan Cohena7434272017-04-03 12:17:51 -0700870 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
871 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700872 throw new IllegalStateException("Must have a single transport specified to use " +
873 "setNetworkSpecifier");
874 }
Etan Cohena7434272017-04-03 12:17:51 -0700875
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700876 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700877
Pierre Imaic8419a82016-03-22 17:54:54 +0900878 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700879 }
880
881 /**
882 * Gets the optional bearer specific network specifier.
883 *
Etan Cohena7434272017-04-03 12:17:51 -0700884 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
885 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700886 * @hide
887 */
Etan Cohena7434272017-04-03 12:17:51 -0700888 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700889 return mNetworkSpecifier;
890 }
891
892 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700893 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700894 throw new IllegalStateException("Can't combine two networkSpecifiers");
895 }
Etan Cohena7434272017-04-03 12:17:51 -0700896 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700897 }
Etan Cohena7434272017-04-03 12:17:51 -0700898
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700899 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700900 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
901 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700902 }
Etan Cohena7434272017-04-03 12:17:51 -0700903
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700904 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700905 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700906 }
907
Robert Greenwalt1448f052014-04-08 13:41:39 -0700908 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900909 * Magic value that indicates no signal strength provided. A request specifying this value is
910 * always satisfied.
911 *
912 * @hide
913 */
914 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
915
916 /**
917 * Signal strength. This is a signed integer, and higher values indicate better signal.
918 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
919 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700920 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900921
922 /**
923 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
924 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
925 * reported by WifiManager.
926 * <p>
927 * Note that when used to register a network callback, this specifies the minimum acceptable
928 * signal strength. When received as the state of an existing network it specifies the current
929 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
930 * effect when requesting a callback.
931 *
932 * @param signalStrength the bearer-specific signal strength.
933 * @hide
934 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600935 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900936 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600937 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900938 }
939
940 /**
941 * Returns {@code true} if this object specifies a signal strength.
942 *
943 * @hide
944 */
945 public boolean hasSignalStrength() {
946 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
947 }
948
949 /**
950 * Retrieves the signal strength.
951 *
952 * @return The bearer-specific signal strength.
953 * @hide
954 */
955 public int getSignalStrength() {
956 return mSignalStrength;
957 }
958
959 private void combineSignalStrength(NetworkCapabilities nc) {
960 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
961 }
962
963 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
964 return this.mSignalStrength <= nc.mSignalStrength;
965 }
966
967 private boolean equalsSignalStrength(NetworkCapabilities nc) {
968 return this.mSignalStrength == nc.mSignalStrength;
969 }
970
971 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900972 * List of UIDs this network applies to. No restriction if null.
973 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900974 * For networks, mUids represent the list of network this applies to, and null means this
975 * network applies to all UIDs.
976 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
977 * must be included in a network so that they match. As an exception to the general rule,
978 * a null mUids field for requests mean "no requirements" rather than what the general rule
979 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
980 * of this API expect in practice. A network that must match all UIDs can still be
981 * expressed with a set ranging the entire set of possible UIDs.
982 * <p>
983 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900984 * the UIDs in this list, and it is their default network. Apps in this list that wish to
985 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
986 * member is null, then the network is not restricted by app UID. If it's an empty list, then
987 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900988 * As a special exception, the app managing this network (as identified by its UID stored in
989 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
990 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
991 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900992 * <p>
993 * Please note that in principle a single app can be associated with multiple UIDs because
994 * each app will have a different UID when it's run as a different (macro-)user. A single
995 * macro user can only have a single active VPN app at any given time however.
996 * <p>
997 * Also please be aware this class does not try to enforce any normalization on this. Callers
998 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
999 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1000 * their own (like requiring sortedness or no overlap) they need to enforce it
1001 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1002 * or overlapping ranges are present.
1003 *
1004 * @hide
1005 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001006 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001007
1008 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001009 * Convenience method to set the UIDs this network applies to to a single UID.
1010 * @hide
1011 */
1012 public NetworkCapabilities setSingleUid(int uid) {
1013 final ArraySet<UidRange> identity = new ArraySet<>(1);
1014 identity.add(new UidRange(uid, uid));
1015 setUids(identity);
1016 return this;
1017 }
1018
1019 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001020 * Set the list of UIDs this network applies to.
1021 * This makes a copy of the set so that callers can't modify it after the call.
1022 * @hide
1023 */
1024 public NetworkCapabilities setUids(Set<UidRange> uids) {
1025 if (null == uids) {
1026 mUids = null;
1027 } else {
1028 mUids = new ArraySet<>(uids);
1029 }
1030 return this;
1031 }
1032
1033 /**
1034 * Get the list of UIDs this network applies to.
1035 * This returns a copy of the set so that callers can't modify the original object.
1036 * @hide
1037 */
1038 public Set<UidRange> getUids() {
1039 return null == mUids ? null : new ArraySet<>(mUids);
1040 }
1041
1042 /**
1043 * Test whether this network applies to this UID.
1044 * @hide
1045 */
1046 public boolean appliesToUid(int uid) {
1047 if (null == mUids) return true;
1048 for (UidRange range : mUids) {
1049 if (range.contains(uid)) {
1050 return true;
1051 }
1052 }
1053 return false;
1054 }
1055
1056 /**
1057 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
1058 * <p>
1059 * This test only checks whether equal range objects are in both sets. It will
1060 * return false if the ranges are not exactly the same, even if the covered UIDs
1061 * are for an equivalent result.
1062 * <p>
1063 * Note that this method is not very optimized, which is fine as long as it's not used very
1064 * often.
1065 * <p>
1066 * nc is assumed nonnull.
1067 *
1068 * @hide
1069 */
1070 @VisibleForTesting
1071 public boolean equalsUids(NetworkCapabilities nc) {
1072 Set<UidRange> comparedUids = nc.mUids;
1073 if (null == comparedUids) return null == mUids;
1074 if (null == mUids) return false;
1075 // Make a copy so it can be mutated to check that all ranges in mUids
1076 // also are in uids.
1077 final Set<UidRange> uids = new ArraySet<>(mUids);
1078 for (UidRange range : comparedUids) {
1079 if (!uids.contains(range)) {
1080 return false;
1081 }
1082 uids.remove(range);
1083 }
1084 return uids.isEmpty();
1085 }
1086
1087 /**
1088 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1089 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001090 * This method is called on the NetworkCapabilities embedded in a request with the
1091 * capabilities of an available network. It checks whether all the UIDs from this listen
1092 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1093 * in the passed nc (representing the UIDs that this network is available to).
1094 * <p>
1095 * As a special exception, the UID that created the passed network (as represented by its
1096 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1097 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1098 * can see its own network when it listens for it.
1099 * <p>
1100 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001101 * @see #appliesToUid
1102 * @hide
1103 */
1104 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001105 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001106 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001107 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001108 if (!nc.appliesToUidRange(requiredRange)) {
1109 return false;
1110 }
1111 }
1112 return true;
1113 }
1114
1115 /**
1116 * Returns whether this network applies to the passed ranges.
1117 * This assumes that to apply, the passed range has to be entirely contained
1118 * within one of the ranges this network applies to. If the ranges are not normalized,
1119 * this method may return false even though all required UIDs are covered because no
1120 * single range contained them all.
1121 * @hide
1122 */
1123 @VisibleForTesting
1124 public boolean appliesToUidRange(UidRange requiredRange) {
1125 if (null == mUids) return true;
1126 for (UidRange uidRange : mUids) {
1127 if (uidRange.containsRange(requiredRange)) {
1128 return true;
1129 }
1130 }
1131 return false;
1132 }
1133
1134 /**
1135 * Combine the UIDs this network currently applies to with the UIDs the passed
1136 * NetworkCapabilities apply to.
1137 * nc is assumed nonnull.
1138 */
1139 private void combineUids(NetworkCapabilities nc) {
1140 if (null == nc.mUids || null == mUids) {
1141 mUids = null;
1142 return;
1143 }
1144 mUids.addAll(nc.mUids);
1145 }
1146
1147 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001148 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001149 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001150 */
1151 public void combineCapabilities(NetworkCapabilities nc) {
1152 combineNetCapabilities(nc);
1153 combineTransportTypes(nc);
1154 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001155 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001156 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001157 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001158 }
1159
1160 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001161 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1162 *
1163 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1164 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1165 * bandwidth, signal strength, or validation / captive portal status.
1166 *
1167 * @hide
1168 */
1169 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001170 return (nc != null
1171 && satisfiedByNetCapabilities(nc, onlyImmutable)
1172 && satisfiedByTransportTypes(nc)
1173 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1174 && satisfiedBySpecifier(nc)
1175 && (onlyImmutable || satisfiedBySignalStrength(nc))
1176 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001177 }
1178
1179 /**
1180 * Check if our 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
Robert Greenwalt1448f052014-04-08 13:41:39 -07001185 */
1186 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001187 return satisfiedByNetworkCapabilities(nc, false);
1188 }
1189
1190 /**
1191 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1192 *
1193 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1194 *
1195 * @hide
1196 */
1197 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1198 return satisfiedByNetworkCapabilities(nc, true);
1199 }
1200
1201 /**
1202 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001203 * {@code NetworkCapabilities} and return a String describing any difference.
1204 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001205 *
1206 * @hide
1207 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001208 public String describeImmutableDifferences(NetworkCapabilities that) {
1209 if (that == null) {
1210 return "other NetworkCapabilities was null";
1211 }
1212
1213 StringJoiner joiner = new StringJoiner(", ");
1214
Hugo Benichieae7a222017-07-25 11:40:56 +09001215 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1216 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001217 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001218 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1219 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1220 if (oldImmutableCapabilities != newImmutableCapabilities) {
1221 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1222 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1223 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1224 }
1225
1226 if (!equalsSpecifier(that)) {
1227 NetworkSpecifier before = this.getNetworkSpecifier();
1228 NetworkSpecifier after = that.getNetworkSpecifier();
1229 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1230 }
1231
1232 if (!equalsTransportTypes(that)) {
1233 String before = transportNamesOf(this.getTransportTypes());
1234 String after = transportNamesOf(that.getTransportTypes());
1235 joiner.add(String.format("transports changed: %s -> %s", before, after));
1236 }
1237
1238 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001239 }
1240
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001241 /**
1242 * Checks that our requestable capabilities are the same as those of the given
1243 * {@code NetworkCapabilities}.
1244 *
1245 * @hide
1246 */
1247 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1248 if (nc == null) return false;
1249 return (equalsNetCapabilitiesRequestable(nc) &&
1250 equalsTransportTypes(nc) &&
1251 equalsSpecifier(nc));
1252 }
1253
Robert Greenwalt1448f052014-04-08 13:41:39 -07001254 @Override
1255 public boolean equals(Object obj) {
1256 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001257 NetworkCapabilities that = (NetworkCapabilities) obj;
1258 return (equalsNetCapabilities(that)
1259 && equalsTransportTypes(that)
1260 && equalsLinkBandwidths(that)
1261 && equalsSignalStrength(that)
1262 && equalsSpecifier(that)
1263 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001264 }
1265
1266 @Override
1267 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001268 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001269 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001270 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1271 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1272 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1273 + ((int) (mTransportTypes >> 32) * 13)
1274 + (mLinkUpBandwidthKbps * 17)
1275 + (mLinkDownBandwidthKbps * 19)
1276 + Objects.hashCode(mNetworkSpecifier) * 23
1277 + (mSignalStrength * 29)
1278 + Objects.hashCode(mUids) * 31;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001279 }
1280
Wink Saville4e2dea72014-09-20 11:04:03 -07001281 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001282 public int describeContents() {
1283 return 0;
1284 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001285 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001286 public void writeToParcel(Parcel dest, int flags) {
1287 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001288 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001289 dest.writeLong(mTransportTypes);
1290 dest.writeInt(mLinkUpBandwidthKbps);
1291 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001292 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001293 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001294 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001295 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001296
Robert Greenwalt1448f052014-04-08 13:41:39 -07001297 public static final Creator<NetworkCapabilities> CREATOR =
1298 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001299 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001300 public NetworkCapabilities createFromParcel(Parcel in) {
1301 NetworkCapabilities netCap = new NetworkCapabilities();
1302
1303 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001304 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001305 netCap.mTransportTypes = in.readLong();
1306 netCap.mLinkUpBandwidthKbps = in.readInt();
1307 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001308 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001309 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001310 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1311 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001312 return netCap;
1313 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001314 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001315 public NetworkCapabilities[] newArray(int size) {
1316 return new NetworkCapabilities[size];
1317 }
1318 };
1319
Wink Saville4e2dea72014-09-20 11:04:03 -07001320 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001321 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001322 final StringBuilder sb = new StringBuilder("[");
1323 if (0 != mTransportTypes) {
1324 sb.append(" Transports: ");
1325 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1326 NetworkCapabilities::transportNameOf, "|");
1327 }
1328 if (0 != mNetworkCapabilities) {
1329 sb.append(" Capabilities: ");
1330 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1331 NetworkCapabilities::capabilityNameOf, "&");
1332 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001333 if (0 != mNetworkCapabilities) {
1334 sb.append(" Unwanted: ");
1335 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1336 NetworkCapabilities::capabilityNameOf, "&");
1337 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001338 if (mLinkUpBandwidthKbps > 0) {
1339 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1340 }
1341 if (mLinkDownBandwidthKbps > 0) {
1342 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1343 }
1344 if (mNetworkSpecifier != null) {
1345 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1346 }
1347 if (hasSignalStrength()) {
1348 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001349 }
1350
Chalard Jean07ace0f2018-02-26 19:00:45 +09001351 if (null != mUids) {
1352 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1353 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1354 } else {
1355 sb.append(" Uids: <").append(mUids).append(">");
1356 }
1357 }
1358 if (mEstablishingVpnAppUid != INVALID_UID) {
1359 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1360 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001361
Chalard Jean07ace0f2018-02-26 19:00:45 +09001362 sb.append("]");
1363 return sb.toString();
1364 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001365
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001366
Chalard Jean07ace0f2018-02-26 19:00:45 +09001367 private interface NameOf {
1368 String nameOf(int value);
1369 }
1370 /**
1371 * @hide
1372 */
1373 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1374 long bitMask, NameOf nameFetcher, String separator) {
1375 int bitPos = 0;
1376 boolean firstElementAdded = false;
1377 while (bitMask != 0) {
1378 if ((bitMask & 1) != 0) {
1379 if (firstElementAdded) {
1380 sb.append(separator);
1381 } else {
1382 firstElementAdded = true;
1383 }
1384 sb.append(nameFetcher.nameOf(bitPos));
1385 }
1386 bitMask >>= 1;
1387 ++bitPos;
1388 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001389 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001390
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001391 /** @hide */
1392 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1393 final long token = proto.start(fieldId);
1394
1395 for (int transport : getTransportTypes()) {
1396 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1397 }
1398
1399 for (int capability : getCapabilities()) {
1400 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1401 }
1402
1403 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1404 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1405
1406 if (mNetworkSpecifier != null) {
1407 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1408 }
1409
1410 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1411 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1412
1413 proto.end(token);
1414 }
1415
Hugo Benichi5df9d722016-04-25 17:16:35 +09001416 /**
1417 * @hide
1418 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001419 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001420 StringJoiner joiner = new StringJoiner("|");
1421 if (capabilities != null) {
1422 for (int c : capabilities) {
1423 joiner.add(capabilityNameOf(c));
1424 }
1425 }
1426 return joiner.toString();
1427 }
1428
1429 /**
1430 * @hide
1431 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001432 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001433 switch (capability) {
1434 case NET_CAPABILITY_MMS: return "MMS";
1435 case NET_CAPABILITY_SUPL: return "SUPL";
1436 case NET_CAPABILITY_DUN: return "DUN";
1437 case NET_CAPABILITY_FOTA: return "FOTA";
1438 case NET_CAPABILITY_IMS: return "IMS";
1439 case NET_CAPABILITY_CBS: return "CBS";
1440 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1441 case NET_CAPABILITY_IA: return "IA";
1442 case NET_CAPABILITY_RCS: return "RCS";
1443 case NET_CAPABILITY_XCAP: return "XCAP";
1444 case NET_CAPABILITY_EIMS: return "EIMS";
1445 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1446 case NET_CAPABILITY_INTERNET: return "INTERNET";
1447 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1448 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1449 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1450 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1451 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001452 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001453 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001454 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001455 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001456 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001457 default: return Integer.toString(capability);
1458 }
1459 }
1460
1461 /**
1462 * @hide
1463 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001464 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001465 StringJoiner joiner = new StringJoiner("|");
1466 if (types != null) {
1467 for (int t : types) {
1468 joiner.add(transportNameOf(t));
1469 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001470 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001471 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001472 }
1473
1474 /**
1475 * @hide
1476 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001477 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001478 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001479 return "UNKNOWN";
1480 }
1481 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001482 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001483
Jeff Sharkeyde570312017-10-24 21:25:50 -06001484 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001485 Preconditions.checkArgument(
1486 isValidTransport(transport), "Invalid TransportType " + transport);
1487 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001488
1489 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1490 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1491 }
1492
1493 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1494 Preconditions.checkArgument(isValidCapability(capability),
1495 "NetworkCapability " + capability + "out of range");
1496 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001497}