blob: a8e81791fe307be06c840cc37f6c3bf28005380a [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>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600257 * When a network is congested, applications should defer network traffic
258 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900259 */
260 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
261
Chalard Jean804b8fb2018-01-30 22:41:41 +0900262 /**
263 * Indicates that this network is not currently suspended.
264 * <p>
265 * When a network is suspended, the network's IP addresses and any connections
266 * established on the network remain valid, but the network is temporarily unable
267 * to transfer data. This can happen, for example, if a cellular network experiences
268 * a temporary loss of signal, such as when driving through a tunnel, etc.
269 * A network with this capability is not suspended, so is expected to be able to
270 * transfer data.
271 */
272 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
273
Pavel Maltsev43403202018-01-30 17:19:44 -0800274 /**
275 * Indicates that traffic that goes through this network is paid by oem. For example,
276 * this network can be used by system apps to upload telemetry data.
277 * @hide
278 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700279 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800280 public static final int NET_CAPABILITY_OEM_PAID = 22;
281
Robert Greenwalt1448f052014-04-08 13:41:39 -0700282 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800283 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700284
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700285 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900286 * Network capabilities that are expected to be mutable, i.e., can change while a particular
287 * network is connected.
288 */
289 private static final long MUTABLE_CAPABILITIES =
290 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
291 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900292 (1 << NET_CAPABILITY_TRUSTED)
293 | (1 << NET_CAPABILITY_VALIDATED)
294 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
295 | (1 << NET_CAPABILITY_NOT_ROAMING)
296 | (1 << NET_CAPABILITY_FOREGROUND)
297 | (1 << NET_CAPABILITY_NOT_CONGESTED)
298 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900299
300 /**
301 * Network capabilities that are not allowed in NetworkRequests. This exists because the
302 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
303 * capability's presence cannot be known in advance. If such a capability is requested, then we
304 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
305 * get immediately torn down because they do not have the requested capability.
306 */
307 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900308 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900309
310 /**
311 * Capabilities that are set by default when the object is constructed.
312 */
313 private static final long DEFAULT_CAPABILITIES =
314 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
315 (1 << NET_CAPABILITY_TRUSTED) |
316 (1 << NET_CAPABILITY_NOT_VPN);
317
318 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400319 * Capabilities that suggest that a network is restricted.
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000320 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400321 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700322 @VisibleForTesting
323 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400324 (1 << NET_CAPABILITY_CBS) |
325 (1 << NET_CAPABILITY_DUN) |
326 (1 << NET_CAPABILITY_EIMS) |
327 (1 << NET_CAPABILITY_FOTA) |
328 (1 << NET_CAPABILITY_IA) |
329 (1 << NET_CAPABILITY_IMS) |
330 (1 << NET_CAPABILITY_RCS) |
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000331 (1 << NET_CAPABILITY_XCAP);
332
333 /**
334 * Capabilities that force network to be restricted.
335 * {@see #maybeMarkCapabilitiesRestricted}.
336 */
337 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800338 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400339
340 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700341 * Capabilities that suggest that a network is unrestricted.
342 * {@see #maybeMarkCapabilitiesRestricted}.
343 */
344 @VisibleForTesting
345 /* package */ static final long UNRESTRICTED_CAPABILITIES =
346 (1 << NET_CAPABILITY_INTERNET) |
347 (1 << NET_CAPABILITY_MMS) |
348 (1 << NET_CAPABILITY_SUPL) |
349 (1 << NET_CAPABILITY_WIFI_P2P);
350
351 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700352 * Adds the given capability to this {@code NetworkCapability} instance.
353 * Multiple capabilities may be applied sequentially. Note that when searching
354 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800355 * <p>
356 * If the given capability was previously added to the list of unwanted capabilities
357 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700358 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600359 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900360 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700361 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700362 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600363 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800364 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700365 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800366 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700367 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700368 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700369
370 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800371 * Adds the given capability to the list of unwanted capabilities of this
372 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
373 * sequentially. Note that when searching for a network to satisfy a request, the network
374 * must not contain any capability from unwanted capability list.
375 * <p>
376 * If the capability was previously added to the list of required capabilities (for
377 * example, it was there by default or added using {@link #addCapability(int)} method), then
378 * it will be removed from the list of required capabilities as well.
379 *
380 * @see #addCapability(int)
381 * @hide
382 */
383 public void addUnwantedCapability(@NetCapability int capability) {
384 checkValidCapability(capability);
385 mUnwantedNetworkCapabilities |= 1 << capability;
386 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
387 }
388
389 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700390 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800391 * <p>
392 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
393 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700394 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600395 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900396 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700397 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700398 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600399 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800400 checkValidCapability(capability);
401 final long mask = ~(1 << capability);
402 mNetworkCapabilities &= mask;
403 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700404 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700405 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700406
407 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600408 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
409 * instance.
410 *
411 * @hide
412 */
413 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
414 if (value) {
415 addCapability(capability);
416 } else {
417 removeCapability(capability);
418 }
419 return this;
420 }
421
422 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700423 * Gets all the capabilities set on this {@code NetworkCapability} instance.
424 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600425 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700426 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700427 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600428 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900429 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700430 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700431
432 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800433 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
434 *
435 * @return an array of unwanted capability values for this instance.
436 * @hide
437 */
438 public @NetCapability int[] getUnwantedCapabilities() {
439 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
440 }
441
442
443 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600444 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700445 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600446 *
447 * @hide
448 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800449 public void setCapabilities(@NetCapability int[] capabilities,
450 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600451 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800452 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600453 }
454
455 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800456 * @deprecated use {@link #setCapabilities(int[], int[])}
457 * @hide
458 */
459 @Deprecated
460 public void setCapabilities(@NetCapability int[] capabilities) {
461 setCapabilities(capabilities, new int[] {});
462 }
463
464 /**
465 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700466 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600467 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700468 * @return {@code true} if set on this instance.
469 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600470 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800471 return isValidCapability(capability)
472 && ((mNetworkCapabilities & (1 << capability)) != 0);
473 }
474
475 /** @hide */
476 public boolean hasUnwantedCapability(@NetCapability int capability) {
477 return isValidCapability(capability)
478 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700479 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480
Robert Greenwalt1448f052014-04-08 13:41:39 -0700481 private void combineNetCapabilities(NetworkCapabilities nc) {
482 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800483 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700484 }
485
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900486 /**
487 * Convenience function that returns a human-readable description of the first mutable
488 * capability we find. Used to present an error message to apps that request mutable
489 * capabilities.
490 *
491 * @hide
492 */
493 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800494 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
495 & NON_REQUESTABLE_CAPABILITIES;
496
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900497 if (nonRequestable != 0) {
498 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900499 }
500 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900501 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900502 return null;
503 }
504
505 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800506 long requestedCapabilities = mNetworkCapabilities;
507 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
508 long providedCapabilities = nc.mNetworkCapabilities;
509
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900510 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800511 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
512 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900513 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800514 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
515 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700516 }
517
Robert Greenwalt06314e42014-10-29 14:04:06 -0700518 /** @hide */
519 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800520 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
521 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700522 }
523
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900524 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
525 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800526 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
527 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
528 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900529 }
530
Robert Greenwalt1448f052014-04-08 13:41:39 -0700531 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400532 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
533 * typically provided by restricted networks.
534 *
535 * TODO: consider:
536 * - Renaming it to guessRestrictedCapability and make it set the
537 * restricted capability bit in addition to clearing it.
538 * @hide
539 */
540 public void maybeMarkCapabilitiesRestricted() {
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000541 // Check if we have any capability that forces the network to be restricted.
542 final boolean forceRestrictedCapability =
543 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
544
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700545 // Verify there aren't any unrestricted capabilities. If there are we say
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000546 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700547 final boolean hasUnrestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000548 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700549
550 // Must have at least some restricted capabilities.
551 final boolean hasRestrictedCapabilities =
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000552 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700553
Lorenzo Colitti83c1e742018-03-30 09:21:27 +0000554 if (forceRestrictedCapability
555 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities)) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400556 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400557 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400558 }
559
560 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700561 * Representing the transport type. Apps should generally not care about transport. A
562 * request for a fast internet connection could be satisfied by a number of different
563 * transports. If any are specified here it will be satisfied a Network that matches
564 * any of them. If a caller doesn't care about the transport it should not specify any.
565 */
566 private long mTransportTypes;
567
Jeff Sharkeyde570312017-10-24 21:25:50 -0600568 /** @hide */
569 @Retention(RetentionPolicy.SOURCE)
570 @IntDef(prefix = { "TRANSPORT_" }, value = {
571 TRANSPORT_CELLULAR,
572 TRANSPORT_WIFI,
573 TRANSPORT_BLUETOOTH,
574 TRANSPORT_ETHERNET,
575 TRANSPORT_VPN,
576 TRANSPORT_WIFI_AWARE,
577 TRANSPORT_LOWPAN,
578 })
579 public @interface Transport { }
580
Robert Greenwalt1448f052014-04-08 13:41:39 -0700581 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700582 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700583 */
584 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700585
586 /**
587 * Indicates this network uses a Wi-Fi transport.
588 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700589 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700590
591 /**
592 * Indicates this network uses a Bluetooth transport.
593 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700594 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700595
596 /**
597 * Indicates this network uses an Ethernet transport.
598 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700599 public static final int TRANSPORT_ETHERNET = 3;
600
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400601 /**
602 * Indicates this network uses a VPN transport.
603 */
604 public static final int TRANSPORT_VPN = 4;
605
Etan Cohen305ea282016-06-20 09:27:12 -0700606 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700607 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700608 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700609 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700610
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700611 /**
612 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700613 */
614 public static final int TRANSPORT_LOWPAN = 6;
615
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900616 /** @hide */
617 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
618 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700619 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700620
Hugo Benichi16f0a942017-06-20 14:07:59 +0900621 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600622 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900623 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
624 }
625
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900626 private static final String[] TRANSPORT_NAMES = {
627 "CELLULAR",
628 "WIFI",
629 "BLUETOOTH",
630 "ETHERNET",
631 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700632 "WIFI_AWARE",
633 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900634 };
635
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700636 /**
637 * Adds the given transport type to this {@code NetworkCapability} instance.
638 * Multiple transports may be applied sequentially. Note that when searching
639 * for a network to satisfy a request, any listed in the request will satisfy the request.
640 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
641 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
642 * to be selected. This is logically different than
643 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
644 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600645 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900646 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700647 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700648 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600649 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900650 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700651 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700652 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700653 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700654 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700655
656 /**
657 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
658 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600659 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900660 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700661 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700662 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600663 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900664 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700665 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700666 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700667 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700668 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700669
670 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600671 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
672 * instance.
673 *
674 * @hide
675 */
676 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
677 if (value) {
678 addTransportType(transportType);
679 } else {
680 removeTransportType(transportType);
681 }
682 return this;
683 }
684
685 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700686 * Gets all the transports set on this {@code NetworkCapability} instance.
687 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600688 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700689 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700690 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600691 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900692 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700693 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700694
695 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600696 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700697 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600698 *
699 * @hide
700 */
701 public void setTransportTypes(@Transport int[] transportTypes) {
702 mTransportTypes = BitUtils.packBits(transportTypes);
703 }
704
705 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700706 * Tests for the presence of a transport on this instance.
707 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600708 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700709 * @return {@code true} if set on this instance.
710 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900712 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700713 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714
715 private void combineTransportTypes(NetworkCapabilities nc) {
716 this.mTransportTypes |= nc.mTransportTypes;
717 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900718
Robert Greenwalt1448f052014-04-08 13:41:39 -0700719 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
720 return ((this.mTransportTypes == 0) ||
721 ((this.mTransportTypes & nc.mTransportTypes) != 0));
722 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900723
Robert Greenwalt06314e42014-10-29 14:04:06 -0700724 /** @hide */
725 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700726 return (nc.mTransportTypes == this.mTransportTypes);
727 }
728
729 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900730 * UID of the app that manages this network, or INVALID_UID if none/unknown.
731 *
732 * This field keeps track of the UID of the app that created this network and is in charge
733 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
734 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
735 * to create networks.
736 *
737 * Because this field is only used in the services side (and to avoid apps being able to
738 * set this to whatever they want), this field is not parcelled and will not be conserved
739 * across the IPC boundary.
740 * @hide
741 */
742 private int mEstablishingVpnAppUid = INVALID_UID;
743
744 /**
745 * Set the UID of the managing app.
746 * @hide
747 */
748 public void setEstablishingVpnAppUid(final int uid) {
749 mEstablishingVpnAppUid = uid;
750 }
751
752 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600753 * Value indicating that link bandwidth is unspecified.
754 * @hide
755 */
756 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
757
758 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700759 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
760 * for the first hop on the given transport. It is not measured, but may take into account
761 * link parameters (Radio technology, allocated channels, etc).
762 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600763 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
764 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700765
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700766 /**
767 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
768 * the estimated first hop transport bandwidth.
769 * <p>
770 * Note that when used to request a network, this specifies the minimum acceptable.
771 * When received as the state of an existing network this specifies the typical
772 * first hop bandwidth expected. This is never measured, but rather is inferred
773 * from technology type and other link parameters. It could be used to differentiate
774 * between very slow 1xRTT cellular links and other faster networks or even between
775 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
776 * fast backhauls and slow backhauls.
777 *
778 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700779 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700780 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600781 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700782 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600783 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700784 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700785
786 /**
787 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
788 * the estimated first hop transport bandwidth.
789 *
790 * @return The estimated first hop upstream (device to network) bandwidth.
791 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700792 public int getLinkUpstreamBandwidthKbps() {
793 return mLinkUpBandwidthKbps;
794 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700795
796 /**
797 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
798 * the estimated first hop transport bandwidth.
799 * <p>
800 * Note that when used to request a network, this specifies the minimum acceptable.
801 * When received as the state of an existing network this specifies the typical
802 * first hop bandwidth expected. This is never measured, but rather is inferred
803 * from technology type and other link parameters. It could be used to differentiate
804 * between very slow 1xRTT cellular links and other faster networks or even between
805 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
806 * fast backhauls and slow backhauls.
807 *
808 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700809 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700810 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600811 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700812 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600813 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700814 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700815
816 /**
817 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
818 * the estimated first hop transport bandwidth.
819 *
820 * @return The estimated first hop downstream (network to device) bandwidth.
821 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700822 public int getLinkDownstreamBandwidthKbps() {
823 return mLinkDownBandwidthKbps;
824 }
825
826 private void combineLinkBandwidths(NetworkCapabilities nc) {
827 this.mLinkUpBandwidthKbps =
828 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
829 this.mLinkDownBandwidthKbps =
830 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
831 }
832 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
833 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
834 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
835 }
836 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
837 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
838 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
839 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600840 /** @hide */
841 public static int minBandwidth(int a, int b) {
842 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
843 return b;
844 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
845 return a;
846 } else {
847 return Math.min(a, b);
848 }
849 }
850 /** @hide */
851 public static int maxBandwidth(int a, int b) {
852 return Math.max(a, b);
853 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700854
Etan Cohena7434272017-04-03 12:17:51 -0700855 private NetworkSpecifier mNetworkSpecifier = null;
856
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700857 /**
858 * Sets the optional bearer specific network specifier.
859 * This has no meaning if a single transport is also not specified, so calling
860 * this without a single transport set will generate an exception, as will
861 * subsequently adding or removing transports after this is set.
862 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700863 *
Etan Cohena7434272017-04-03 12:17:51 -0700864 * @param networkSpecifier A concrete, parcelable framework class that extends
865 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900866 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700867 * @hide
868 */
Etan Cohena7434272017-04-03 12:17:51 -0700869 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
870 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700871 throw new IllegalStateException("Must have a single transport specified to use " +
872 "setNetworkSpecifier");
873 }
Etan Cohena7434272017-04-03 12:17:51 -0700874
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700875 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700876
Pierre Imaic8419a82016-03-22 17:54:54 +0900877 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700878 }
879
880 /**
881 * Gets the optional bearer specific network specifier.
882 *
Etan Cohena7434272017-04-03 12:17:51 -0700883 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
884 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700885 * @hide
886 */
Etan Cohena7434272017-04-03 12:17:51 -0700887 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700888 return mNetworkSpecifier;
889 }
890
891 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700892 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 throw new IllegalStateException("Can't combine two networkSpecifiers");
894 }
Etan Cohena7434272017-04-03 12:17:51 -0700895 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700896 }
Etan Cohena7434272017-04-03 12:17:51 -0700897
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700898 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700899 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
900 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700901 }
Etan Cohena7434272017-04-03 12:17:51 -0700902
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700903 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700904 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700905 }
906
Robert Greenwalt1448f052014-04-08 13:41:39 -0700907 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900908 * Magic value that indicates no signal strength provided. A request specifying this value is
909 * always satisfied.
910 *
911 * @hide
912 */
913 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
914
915 /**
916 * Signal strength. This is a signed integer, and higher values indicate better signal.
917 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
918 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700919 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900920
921 /**
922 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
923 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
924 * reported by WifiManager.
925 * <p>
926 * Note that when used to register a network callback, this specifies the minimum acceptable
927 * signal strength. When received as the state of an existing network it specifies the current
928 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
929 * effect when requesting a callback.
930 *
931 * @param signalStrength the bearer-specific signal strength.
932 * @hide
933 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600934 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900935 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600936 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900937 }
938
939 /**
940 * Returns {@code true} if this object specifies a signal strength.
941 *
942 * @hide
943 */
944 public boolean hasSignalStrength() {
945 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
946 }
947
948 /**
949 * Retrieves the signal strength.
950 *
951 * @return The bearer-specific signal strength.
952 * @hide
953 */
954 public int getSignalStrength() {
955 return mSignalStrength;
956 }
957
958 private void combineSignalStrength(NetworkCapabilities nc) {
959 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
960 }
961
962 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
963 return this.mSignalStrength <= nc.mSignalStrength;
964 }
965
966 private boolean equalsSignalStrength(NetworkCapabilities nc) {
967 return this.mSignalStrength == nc.mSignalStrength;
968 }
969
970 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900971 * List of UIDs this network applies to. No restriction if null.
972 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900973 * For networks, mUids represent the list of network this applies to, and null means this
974 * network applies to all UIDs.
975 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
976 * must be included in a network so that they match. As an exception to the general rule,
977 * a null mUids field for requests mean "no requirements" rather than what the general rule
978 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
979 * of this API expect in practice. A network that must match all UIDs can still be
980 * expressed with a set ranging the entire set of possible UIDs.
981 * <p>
982 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900983 * the UIDs in this list, and it is their default network. Apps in this list that wish to
984 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
985 * member is null, then the network is not restricted by app UID. If it's an empty list, then
986 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900987 * As a special exception, the app managing this network (as identified by its UID stored in
988 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
989 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
990 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900991 * <p>
992 * Please note that in principle a single app can be associated with multiple UIDs because
993 * each app will have a different UID when it's run as a different (macro-)user. A single
994 * macro user can only have a single active VPN app at any given time however.
995 * <p>
996 * Also please be aware this class does not try to enforce any normalization on this. Callers
997 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
998 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
999 * their own (like requiring sortedness or no overlap) they need to enforce it
1000 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1001 * or overlapping ranges are present.
1002 *
1003 * @hide
1004 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001005 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001006
1007 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001008 * Convenience method to set the UIDs this network applies to to a single UID.
1009 * @hide
1010 */
1011 public NetworkCapabilities setSingleUid(int uid) {
1012 final ArraySet<UidRange> identity = new ArraySet<>(1);
1013 identity.add(new UidRange(uid, uid));
1014 setUids(identity);
1015 return this;
1016 }
1017
1018 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001019 * Set the list of UIDs this network applies to.
1020 * This makes a copy of the set so that callers can't modify it after the call.
1021 * @hide
1022 */
1023 public NetworkCapabilities setUids(Set<UidRange> uids) {
1024 if (null == uids) {
1025 mUids = null;
1026 } else {
1027 mUids = new ArraySet<>(uids);
1028 }
1029 return this;
1030 }
1031
1032 /**
1033 * Get the list of UIDs this network applies to.
1034 * This returns a copy of the set so that callers can't modify the original object.
1035 * @hide
1036 */
1037 public Set<UidRange> getUids() {
1038 return null == mUids ? null : new ArraySet<>(mUids);
1039 }
1040
1041 /**
1042 * Test whether this network applies to this UID.
1043 * @hide
1044 */
1045 public boolean appliesToUid(int uid) {
1046 if (null == mUids) return true;
1047 for (UidRange range : mUids) {
1048 if (range.contains(uid)) {
1049 return true;
1050 }
1051 }
1052 return false;
1053 }
1054
1055 /**
1056 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
1057 * <p>
1058 * This test only checks whether equal range objects are in both sets. It will
1059 * return false if the ranges are not exactly the same, even if the covered UIDs
1060 * are for an equivalent result.
1061 * <p>
1062 * Note that this method is not very optimized, which is fine as long as it's not used very
1063 * often.
1064 * <p>
1065 * nc is assumed nonnull.
1066 *
1067 * @hide
1068 */
1069 @VisibleForTesting
1070 public boolean equalsUids(NetworkCapabilities nc) {
1071 Set<UidRange> comparedUids = nc.mUids;
1072 if (null == comparedUids) return null == mUids;
1073 if (null == mUids) return false;
1074 // Make a copy so it can be mutated to check that all ranges in mUids
1075 // also are in uids.
1076 final Set<UidRange> uids = new ArraySet<>(mUids);
1077 for (UidRange range : comparedUids) {
1078 if (!uids.contains(range)) {
1079 return false;
1080 }
1081 uids.remove(range);
1082 }
1083 return uids.isEmpty();
1084 }
1085
1086 /**
1087 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1088 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001089 * This method is called on the NetworkCapabilities embedded in a request with the
1090 * capabilities of an available network. It checks whether all the UIDs from this listen
1091 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1092 * in the passed nc (representing the UIDs that this network is available to).
1093 * <p>
1094 * As a special exception, the UID that created the passed network (as represented by its
1095 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1096 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1097 * can see its own network when it listens for it.
1098 * <p>
1099 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001100 * @see #appliesToUid
1101 * @hide
1102 */
1103 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001104 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001105 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001106 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001107 if (!nc.appliesToUidRange(requiredRange)) {
1108 return false;
1109 }
1110 }
1111 return true;
1112 }
1113
1114 /**
1115 * Returns whether this network applies to the passed ranges.
1116 * This assumes that to apply, the passed range has to be entirely contained
1117 * within one of the ranges this network applies to. If the ranges are not normalized,
1118 * this method may return false even though all required UIDs are covered because no
1119 * single range contained them all.
1120 * @hide
1121 */
1122 @VisibleForTesting
1123 public boolean appliesToUidRange(UidRange requiredRange) {
1124 if (null == mUids) return true;
1125 for (UidRange uidRange : mUids) {
1126 if (uidRange.containsRange(requiredRange)) {
1127 return true;
1128 }
1129 }
1130 return false;
1131 }
1132
1133 /**
1134 * Combine the UIDs this network currently applies to with the UIDs the passed
1135 * NetworkCapabilities apply to.
1136 * nc is assumed nonnull.
1137 */
1138 private void combineUids(NetworkCapabilities nc) {
1139 if (null == nc.mUids || null == mUids) {
1140 mUids = null;
1141 return;
1142 }
1143 mUids.addAll(nc.mUids);
1144 }
1145
1146 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001147 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001148 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001149 */
1150 public void combineCapabilities(NetworkCapabilities nc) {
1151 combineNetCapabilities(nc);
1152 combineTransportTypes(nc);
1153 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001154 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001155 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001156 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001157 }
1158
1159 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001160 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1161 *
1162 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1163 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1164 * bandwidth, signal strength, or validation / captive portal status.
1165 *
1166 * @hide
1167 */
1168 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001169 return (nc != null
1170 && satisfiedByNetCapabilities(nc, onlyImmutable)
1171 && satisfiedByTransportTypes(nc)
1172 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1173 && satisfiedBySpecifier(nc)
1174 && (onlyImmutable || satisfiedBySignalStrength(nc))
1175 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001176 }
1177
1178 /**
1179 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1180 *
1181 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1182 *
1183 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001184 */
1185 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001186 return satisfiedByNetworkCapabilities(nc, false);
1187 }
1188
1189 /**
1190 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1191 *
1192 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1193 *
1194 * @hide
1195 */
1196 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1197 return satisfiedByNetworkCapabilities(nc, true);
1198 }
1199
1200 /**
1201 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001202 * {@code NetworkCapabilities} and return a String describing any difference.
1203 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001204 *
1205 * @hide
1206 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001207 public String describeImmutableDifferences(NetworkCapabilities that) {
1208 if (that == null) {
1209 return "other NetworkCapabilities was null";
1210 }
1211
1212 StringJoiner joiner = new StringJoiner(", ");
1213
Hugo Benichieae7a222017-07-25 11:40:56 +09001214 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1215 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001216 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001217 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1218 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1219 if (oldImmutableCapabilities != newImmutableCapabilities) {
1220 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1221 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1222 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1223 }
1224
1225 if (!equalsSpecifier(that)) {
1226 NetworkSpecifier before = this.getNetworkSpecifier();
1227 NetworkSpecifier after = that.getNetworkSpecifier();
1228 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1229 }
1230
1231 if (!equalsTransportTypes(that)) {
1232 String before = transportNamesOf(this.getTransportTypes());
1233 String after = transportNamesOf(that.getTransportTypes());
1234 joiner.add(String.format("transports changed: %s -> %s", before, after));
1235 }
1236
1237 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001238 }
1239
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001240 /**
1241 * Checks that our requestable capabilities are the same as those of the given
1242 * {@code NetworkCapabilities}.
1243 *
1244 * @hide
1245 */
1246 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1247 if (nc == null) return false;
1248 return (equalsNetCapabilitiesRequestable(nc) &&
1249 equalsTransportTypes(nc) &&
1250 equalsSpecifier(nc));
1251 }
1252
Robert Greenwalt1448f052014-04-08 13:41:39 -07001253 @Override
1254 public boolean equals(Object obj) {
1255 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001256 NetworkCapabilities that = (NetworkCapabilities) obj;
1257 return (equalsNetCapabilities(that)
1258 && equalsTransportTypes(that)
1259 && equalsLinkBandwidths(that)
1260 && equalsSignalStrength(that)
1261 && equalsSpecifier(that)
1262 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001263 }
1264
1265 @Override
1266 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001267 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001268 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001269 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1270 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1271 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1272 + ((int) (mTransportTypes >> 32) * 13)
1273 + (mLinkUpBandwidthKbps * 17)
1274 + (mLinkDownBandwidthKbps * 19)
1275 + Objects.hashCode(mNetworkSpecifier) * 23
1276 + (mSignalStrength * 29)
1277 + Objects.hashCode(mUids) * 31;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001278 }
1279
Wink Saville4e2dea72014-09-20 11:04:03 -07001280 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001281 public int describeContents() {
1282 return 0;
1283 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001284 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001285 public void writeToParcel(Parcel dest, int flags) {
1286 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001287 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001288 dest.writeLong(mTransportTypes);
1289 dest.writeInt(mLinkUpBandwidthKbps);
1290 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001291 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001292 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001293 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001294 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001295
Robert Greenwalt1448f052014-04-08 13:41:39 -07001296 public static final Creator<NetworkCapabilities> CREATOR =
1297 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001298 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001299 public NetworkCapabilities createFromParcel(Parcel in) {
1300 NetworkCapabilities netCap = new NetworkCapabilities();
1301
1302 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001303 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001304 netCap.mTransportTypes = in.readLong();
1305 netCap.mLinkUpBandwidthKbps = in.readInt();
1306 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001307 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001308 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001309 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1310 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001311 return netCap;
1312 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001313 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001314 public NetworkCapabilities[] newArray(int size) {
1315 return new NetworkCapabilities[size];
1316 }
1317 };
1318
Wink Saville4e2dea72014-09-20 11:04:03 -07001319 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001320 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001321 final StringBuilder sb = new StringBuilder("[");
1322 if (0 != mTransportTypes) {
1323 sb.append(" Transports: ");
1324 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1325 NetworkCapabilities::transportNameOf, "|");
1326 }
1327 if (0 != mNetworkCapabilities) {
1328 sb.append(" Capabilities: ");
1329 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1330 NetworkCapabilities::capabilityNameOf, "&");
1331 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001332 if (0 != mNetworkCapabilities) {
1333 sb.append(" Unwanted: ");
1334 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1335 NetworkCapabilities::capabilityNameOf, "&");
1336 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001337 if (mLinkUpBandwidthKbps > 0) {
1338 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1339 }
1340 if (mLinkDownBandwidthKbps > 0) {
1341 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1342 }
1343 if (mNetworkSpecifier != null) {
1344 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1345 }
1346 if (hasSignalStrength()) {
1347 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001348 }
1349
Chalard Jean07ace0f2018-02-26 19:00:45 +09001350 if (null != mUids) {
1351 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1352 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1353 } else {
1354 sb.append(" Uids: <").append(mUids).append(">");
1355 }
1356 }
1357 if (mEstablishingVpnAppUid != INVALID_UID) {
1358 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1359 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001360
Chalard Jean07ace0f2018-02-26 19:00:45 +09001361 sb.append("]");
1362 return sb.toString();
1363 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001364
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001365
Chalard Jean07ace0f2018-02-26 19:00:45 +09001366 private interface NameOf {
1367 String nameOf(int value);
1368 }
1369 /**
1370 * @hide
1371 */
1372 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1373 long bitMask, NameOf nameFetcher, String separator) {
1374 int bitPos = 0;
1375 boolean firstElementAdded = false;
1376 while (bitMask != 0) {
1377 if ((bitMask & 1) != 0) {
1378 if (firstElementAdded) {
1379 sb.append(separator);
1380 } else {
1381 firstElementAdded = true;
1382 }
1383 sb.append(nameFetcher.nameOf(bitPos));
1384 }
1385 bitMask >>= 1;
1386 ++bitPos;
1387 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001388 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001389
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001390 /** @hide */
1391 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1392 final long token = proto.start(fieldId);
1393
1394 for (int transport : getTransportTypes()) {
1395 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1396 }
1397
1398 for (int capability : getCapabilities()) {
1399 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1400 }
1401
1402 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1403 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1404
1405 if (mNetworkSpecifier != null) {
1406 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1407 }
1408
1409 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1410 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1411
1412 proto.end(token);
1413 }
1414
Hugo Benichi5df9d722016-04-25 17:16:35 +09001415 /**
1416 * @hide
1417 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001418 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001419 StringJoiner joiner = new StringJoiner("|");
1420 if (capabilities != null) {
1421 for (int c : capabilities) {
1422 joiner.add(capabilityNameOf(c));
1423 }
1424 }
1425 return joiner.toString();
1426 }
1427
1428 /**
1429 * @hide
1430 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001431 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001432 switch (capability) {
1433 case NET_CAPABILITY_MMS: return "MMS";
1434 case NET_CAPABILITY_SUPL: return "SUPL";
1435 case NET_CAPABILITY_DUN: return "DUN";
1436 case NET_CAPABILITY_FOTA: return "FOTA";
1437 case NET_CAPABILITY_IMS: return "IMS";
1438 case NET_CAPABILITY_CBS: return "CBS";
1439 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1440 case NET_CAPABILITY_IA: return "IA";
1441 case NET_CAPABILITY_RCS: return "RCS";
1442 case NET_CAPABILITY_XCAP: return "XCAP";
1443 case NET_CAPABILITY_EIMS: return "EIMS";
1444 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1445 case NET_CAPABILITY_INTERNET: return "INTERNET";
1446 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1447 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1448 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1449 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1450 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001451 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001452 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001453 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001454 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001455 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001456 default: return Integer.toString(capability);
1457 }
1458 }
1459
1460 /**
1461 * @hide
1462 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001463 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001464 StringJoiner joiner = new StringJoiner("|");
1465 if (types != null) {
1466 for (int t : types) {
1467 joiner.add(transportNameOf(t));
1468 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001469 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001470 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001471 }
1472
1473 /**
1474 * @hide
1475 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001476 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001477 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001478 return "UNKNOWN";
1479 }
1480 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001481 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001482
Jeff Sharkeyde570312017-10-24 21:25:50 -06001483 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001484 Preconditions.checkArgument(
1485 isValidTransport(transport), "Invalid TransportType " + transport);
1486 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001487
1488 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1489 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1490 }
1491
1492 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1493 Preconditions.checkArgument(isValidCapability(capability),
1494 "NetworkCapability " + capability + "out of range");
1495 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001496}