blob: ff5714b3821242dae3bb78d23cda93458917b45c [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;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060020import android.net.ConnectivityManager.NetworkCallback;
Robert Greenwalt1448f052014-04-08 13:41:39 -070021import android.os.Parcel;
22import android.os.Parcelable;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090023import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080024import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070025
26import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090027import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090028import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070029
Jeff Sharkeyde570312017-10-24 21:25:50 -060030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
Etan Cohena7434272017-04-03 12:17:51 -070032import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090033import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090034import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070035
36/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070037 * Representation of the capabilities of an active network. Instances are
38 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060039 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
40 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060041 * <p>
42 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
43 * network selection. Rather than indicate a need for Wi-Fi because an
44 * application needs high bandwidth and risk obsolescence when a new, fast
45 * network appears (like LTE), the application should specify it needs high
46 * bandwidth. Similarly if an application needs an unmetered network for a bulk
47 * transfer it can specify that rather than assuming all cellular based
48 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070049 */
50public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070051 private static final String TAG = "NetworkCapabilities";
Chalard Jeanf474fc32018-01-17 15:10:05 +090052 private static final int INVALID_UID = -1;
Etan Cohena7434272017-04-03 12:17:51 -070053
Robert Greenwalt7569f182014-06-08 16:42:59 -070054 /**
55 * @hide
56 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070057 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090058 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090059 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070060 }
61
62 public NetworkCapabilities(NetworkCapabilities nc) {
63 if (nc != null) {
64 mNetworkCapabilities = nc.mNetworkCapabilities;
65 mTransportTypes = nc.mTransportTypes;
66 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
67 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070068 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090069 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090070 mUids = nc.mUids;
Chalard Jeanf474fc32018-01-17 15:10:05 +090071 mEstablishingVpnAppUid = nc.mEstablishingVpnAppUid;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080072 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070073 }
74 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070075
76 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090077 * Completely clears the contents of this object, removing even the capabilities that are set
78 * by default when the object is constructed.
79 * @hide
80 */
81 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080082 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070083 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090084 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090085 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090086 mUids = null;
Chalard Jeanf474fc32018-01-17 15:10:05 +090087 mEstablishingVpnAppUid = INVALID_UID;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090088 }
89
90 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070091 * Represents the network's capabilities. If any are specified they will be satisfied
92 * by any Network that matches all of them.
93 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090094 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070095
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080096 /**
97 * If any capabilities specified here they must not exist in the matching Network.
98 */
99 private long mUnwantedNetworkCapabilities;
100
Jeff Sharkeyde570312017-10-24 21:25:50 -0600101 /** @hide */
102 @Retention(RetentionPolicy.SOURCE)
103 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
104 NET_CAPABILITY_MMS,
105 NET_CAPABILITY_SUPL,
106 NET_CAPABILITY_DUN,
107 NET_CAPABILITY_FOTA,
108 NET_CAPABILITY_IMS,
109 NET_CAPABILITY_CBS,
110 NET_CAPABILITY_WIFI_P2P,
111 NET_CAPABILITY_IA,
112 NET_CAPABILITY_RCS,
113 NET_CAPABILITY_XCAP,
114 NET_CAPABILITY_EIMS,
115 NET_CAPABILITY_NOT_METERED,
116 NET_CAPABILITY_INTERNET,
117 NET_CAPABILITY_NOT_RESTRICTED,
118 NET_CAPABILITY_TRUSTED,
119 NET_CAPABILITY_NOT_VPN,
120 NET_CAPABILITY_VALIDATED,
121 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600122 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600123 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900124 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900125 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800126 NET_CAPABILITY_OEM_PAID,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600127 })
128 public @interface NetCapability { }
129
Robert Greenwalt1448f052014-04-08 13:41:39 -0700130 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700131 * Indicates this is a network that has the ability to reach the
132 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 */
134 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700135
136 /**
137 * Indicates this is a network that has the ability to reach the carrier's
138 * SUPL server, used to retrieve GPS information.
139 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700140 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700141
142 /**
143 * Indicates this is a network that has the ability to reach the carrier's
144 * DUN or tethering gateway.
145 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700146 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700147
148 /**
149 * Indicates this is a network that has the ability to reach the carrier's
150 * FOTA portal, used for over the air updates.
151 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700152 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700153
154 /**
155 * Indicates this is a network that has the ability to reach the carrier's
156 * IMS servers, used for network registration and signaling.
157 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700158 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700159
160 /**
161 * Indicates this is a network that has the ability to reach the carrier's
162 * CBS servers, used for carrier specific services.
163 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700164 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700165
166 /**
167 * Indicates this is a network that has the ability to reach a Wi-Fi direct
168 * peer.
169 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700170 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700171
172 /**
173 * Indicates this is a network that has the ability to reach a carrier's
174 * Initial Attach servers.
175 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700177
178 /**
179 * Indicates this is a network that has the ability to reach a carrier's
180 * RCS servers, used for Rich Communication Services.
181 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700182 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700183
184 /**
185 * Indicates this is a network that has the ability to reach a carrier's
186 * XCAP servers, used for configuration and control.
187 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700188 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700189
190 /**
191 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700192 * Emergency IMS servers or other services, used for network signaling
193 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700194 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700195 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700196
197 /**
198 * Indicates that this network is unmetered.
199 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700200 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700201
202 /**
203 * Indicates that this network should be able to reach the internet.
204 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700205 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700206
207 /**
208 * Indicates that this network is available for general use. If this is not set
209 * applications should not attempt to communicate on this network. Note that this
210 * is simply informative and not enforcement - enforcement is handled via other means.
211 * Set by default.
212 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700213 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
214
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700215 /**
216 * Indicates that the user has indicated implicit trust of this network. This
217 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
218 * BT device or a wifi the user asked to connect to. Untrusted networks
219 * are probably limited to unknown wifi AP. Set by default.
220 */
221 public static final int NET_CAPABILITY_TRUSTED = 14;
222
Paul Jensen76b610a2015-03-18 09:33:07 -0400223 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400224 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400225 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400226 */
227 public static final int NET_CAPABILITY_NOT_VPN = 15;
228
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900229 /**
230 * Indicates that connectivity on this network was successfully validated. For example, for a
231 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
232 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900233 */
234 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700235
Paul Jensen3d194ea2015-06-16 14:27:36 -0400236 /**
237 * Indicates that this network was found to have a captive portal in place last time it was
238 * probed.
239 */
240 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
241
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900242 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600243 * Indicates that this network is not roaming.
244 */
245 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
246
247 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900248 * Indicates that this network is available for use by apps, and not a network that is being
249 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900250 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600251 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900252
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900253 /**
254 * Indicates that this network is not congested.
255 * <p>
256 * When a network is congested, the device should defer network traffic that
257 * can be done at a later time without breaking developer contracts.
258 * @hide
259 */
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 */
279 public static final int NET_CAPABILITY_OEM_PAID = 22;
280
Robert Greenwalt1448f052014-04-08 13:41:39 -0700281 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Pavel Maltsev43403202018-01-30 17:19:44 -0800282 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_OEM_PAID;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700283
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700284 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900285 * Network capabilities that are expected to be mutable, i.e., can change while a particular
286 * network is connected.
287 */
288 private static final long MUTABLE_CAPABILITIES =
289 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
290 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900291 (1 << NET_CAPABILITY_TRUSTED)
292 | (1 << NET_CAPABILITY_VALIDATED)
293 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
294 | (1 << NET_CAPABILITY_NOT_ROAMING)
295 | (1 << NET_CAPABILITY_FOREGROUND)
296 | (1 << NET_CAPABILITY_NOT_CONGESTED)
297 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900298
299 /**
300 * Network capabilities that are not allowed in NetworkRequests. This exists because the
301 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
302 * capability's presence cannot be known in advance. If such a capability is requested, then we
303 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
304 * get immediately torn down because they do not have the requested capability.
305 */
306 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900307 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900308
309 /**
310 * Capabilities that are set by default when the object is constructed.
311 */
312 private static final long DEFAULT_CAPABILITIES =
313 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
314 (1 << NET_CAPABILITY_TRUSTED) |
315 (1 << NET_CAPABILITY_NOT_VPN);
316
317 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400318 * Capabilities that suggest that a network is restricted.
319 * {@see #maybeMarkCapabilitiesRestricted}.
320 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700321 @VisibleForTesting
322 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400323 (1 << NET_CAPABILITY_CBS) |
324 (1 << NET_CAPABILITY_DUN) |
325 (1 << NET_CAPABILITY_EIMS) |
326 (1 << NET_CAPABILITY_FOTA) |
327 (1 << NET_CAPABILITY_IA) |
328 (1 << NET_CAPABILITY_IMS) |
329 (1 << NET_CAPABILITY_RCS) |
Pavel Maltsev43403202018-01-30 17:19:44 -0800330 (1 << NET_CAPABILITY_XCAP) |
331 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400332
333 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700334 * Capabilities that suggest that a network is unrestricted.
335 * {@see #maybeMarkCapabilitiesRestricted}.
336 */
337 @VisibleForTesting
338 /* package */ static final long UNRESTRICTED_CAPABILITIES =
339 (1 << NET_CAPABILITY_INTERNET) |
340 (1 << NET_CAPABILITY_MMS) |
341 (1 << NET_CAPABILITY_SUPL) |
342 (1 << NET_CAPABILITY_WIFI_P2P);
343
344 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700345 * Adds the given capability to this {@code NetworkCapability} instance.
346 * Multiple capabilities may be applied sequentially. Note that when searching
347 * for a network to satisfy a request, all capabilities requested must be satisfied.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800348 * <p>
349 * If the given capability was previously added to the list of unwanted capabilities
350 * then the capability will also be removed from the list of unwanted capabilities.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700351 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600352 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900353 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700354 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700355 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600356 public NetworkCapabilities addCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800357 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700358 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800359 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700360 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700361 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700362
363 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800364 * Adds the given capability to the list of unwanted capabilities of this
365 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
366 * sequentially. Note that when searching for a network to satisfy a request, the network
367 * must not contain any capability from unwanted capability list.
368 * <p>
369 * If the capability was previously added to the list of required capabilities (for
370 * example, it was there by default or added using {@link #addCapability(int)} method), then
371 * it will be removed from the list of required capabilities as well.
372 *
373 * @see #addCapability(int)
374 * @hide
375 */
376 public void addUnwantedCapability(@NetCapability int capability) {
377 checkValidCapability(capability);
378 mUnwantedNetworkCapabilities |= 1 << capability;
379 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
380 }
381
382 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700383 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800384 * <p>
385 * Note that this method removes capabilities that was added via {@link #addCapability(int)},
386 * {@link #addUnwantedCapability(int)} or {@link #setCapabilities(int[], int[])} .
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700387 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600388 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900389 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700390 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700391 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600392 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800393 checkValidCapability(capability);
394 final long mask = ~(1 << capability);
395 mNetworkCapabilities &= mask;
396 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700397 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700398 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700399
400 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600401 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
402 * instance.
403 *
404 * @hide
405 */
406 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
407 if (value) {
408 addCapability(capability);
409 } else {
410 removeCapability(capability);
411 }
412 return this;
413 }
414
415 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700416 * Gets all the capabilities set on this {@code NetworkCapability} instance.
417 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600418 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700419 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700420 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600421 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900422 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700423 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700424
425 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800426 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
427 *
428 * @return an array of unwanted capability values for this instance.
429 * @hide
430 */
431 public @NetCapability int[] getUnwantedCapabilities() {
432 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
433 }
434
435
436 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600437 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700438 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600439 *
440 * @hide
441 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800442 public void setCapabilities(@NetCapability int[] capabilities,
443 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600444 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800445 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600446 }
447
448 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800449 * @deprecated use {@link #setCapabilities(int[], int[])}
450 * @hide
451 */
452 @Deprecated
453 public void setCapabilities(@NetCapability int[] capabilities) {
454 setCapabilities(capabilities, new int[] {});
455 }
456
457 /**
458 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700459 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600460 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700461 * @return {@code true} if set on this instance.
462 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600463 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800464 return isValidCapability(capability)
465 && ((mNetworkCapabilities & (1 << capability)) != 0);
466 }
467
468 /** @hide */
469 public boolean hasUnwantedCapability(@NetCapability int capability) {
470 return isValidCapability(capability)
471 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700472 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700473
Robert Greenwalt1448f052014-04-08 13:41:39 -0700474 private void combineNetCapabilities(NetworkCapabilities nc) {
475 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800476 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700477 }
478
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900479 /**
480 * Convenience function that returns a human-readable description of the first mutable
481 * capability we find. Used to present an error message to apps that request mutable
482 * capabilities.
483 *
484 * @hide
485 */
486 public String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800487 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
488 & NON_REQUESTABLE_CAPABILITIES;
489
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900490 if (nonRequestable != 0) {
491 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900492 }
493 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900494 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900495 return null;
496 }
497
498 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800499 long requestedCapabilities = mNetworkCapabilities;
500 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
501 long providedCapabilities = nc.mNetworkCapabilities;
502
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900503 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800504 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
505 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900506 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800507 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
508 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700509 }
510
Robert Greenwalt06314e42014-10-29 14:04:06 -0700511 /** @hide */
512 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800513 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
514 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700515 }
516
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900517 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
518 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800519 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
520 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
521 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900522 }
523
Robert Greenwalt1448f052014-04-08 13:41:39 -0700524 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400525 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
526 * typically provided by restricted networks.
527 *
528 * TODO: consider:
529 * - Renaming it to guessRestrictedCapability and make it set the
530 * restricted capability bit in addition to clearing it.
531 * @hide
532 */
533 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700534 // Verify there aren't any unrestricted capabilities. If there are we say
535 // the whole thing is unrestricted.
536 final boolean hasUnrestrictedCapabilities =
537 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
538
539 // Must have at least some restricted capabilities.
540 final boolean hasRestrictedCapabilities =
541 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
542
543 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400544 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400545 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400546 }
547
548 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700549 * Representing the transport type. Apps should generally not care about transport. A
550 * request for a fast internet connection could be satisfied by a number of different
551 * transports. If any are specified here it will be satisfied a Network that matches
552 * any of them. If a caller doesn't care about the transport it should not specify any.
553 */
554 private long mTransportTypes;
555
Jeff Sharkeyde570312017-10-24 21:25:50 -0600556 /** @hide */
557 @Retention(RetentionPolicy.SOURCE)
558 @IntDef(prefix = { "TRANSPORT_" }, value = {
559 TRANSPORT_CELLULAR,
560 TRANSPORT_WIFI,
561 TRANSPORT_BLUETOOTH,
562 TRANSPORT_ETHERNET,
563 TRANSPORT_VPN,
564 TRANSPORT_WIFI_AWARE,
565 TRANSPORT_LOWPAN,
566 })
567 public @interface Transport { }
568
Robert Greenwalt1448f052014-04-08 13:41:39 -0700569 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700570 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 */
572 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700573
574 /**
575 * Indicates this network uses a Wi-Fi transport.
576 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700578
579 /**
580 * Indicates this network uses a Bluetooth transport.
581 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700582 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700583
584 /**
585 * Indicates this network uses an Ethernet transport.
586 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700587 public static final int TRANSPORT_ETHERNET = 3;
588
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400589 /**
590 * Indicates this network uses a VPN transport.
591 */
592 public static final int TRANSPORT_VPN = 4;
593
Etan Cohen305ea282016-06-20 09:27:12 -0700594 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700595 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700596 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700597 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700598
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700599 /**
600 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700601 */
602 public static final int TRANSPORT_LOWPAN = 6;
603
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900604 /** @hide */
605 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
606 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700607 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700608
Hugo Benichi16f0a942017-06-20 14:07:59 +0900609 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600610 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900611 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
612 }
613
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900614 private static final String[] TRANSPORT_NAMES = {
615 "CELLULAR",
616 "WIFI",
617 "BLUETOOTH",
618 "ETHERNET",
619 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700620 "WIFI_AWARE",
621 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900622 };
623
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700624 /**
625 * Adds the given transport type to this {@code NetworkCapability} instance.
626 * Multiple transports may be applied sequentially. Note that when searching
627 * for a network to satisfy a request, any listed in the request will satisfy the request.
628 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
629 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
630 * to be selected. This is logically different than
631 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
632 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600633 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900634 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700635 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700636 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600637 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900638 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700640 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700641 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700642 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700643
644 /**
645 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
646 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600647 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900648 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700649 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700650 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600651 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900652 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700653 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700654 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700655 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700656 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700657
658 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600659 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
660 * instance.
661 *
662 * @hide
663 */
664 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
665 if (value) {
666 addTransportType(transportType);
667 } else {
668 removeTransportType(transportType);
669 }
670 return this;
671 }
672
673 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674 * Gets all the transports set on this {@code NetworkCapability} instance.
675 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600676 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700677 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700678 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600679 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900680 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700681 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700682
683 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600684 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700685 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600686 *
687 * @hide
688 */
689 public void setTransportTypes(@Transport int[] transportTypes) {
690 mTransportTypes = BitUtils.packBits(transportTypes);
691 }
692
693 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700694 * Tests for the presence of a transport on this instance.
695 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600696 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700697 * @return {@code true} if set on this instance.
698 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600699 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900700 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700701 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700702
703 private void combineTransportTypes(NetworkCapabilities nc) {
704 this.mTransportTypes |= nc.mTransportTypes;
705 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900706
Robert Greenwalt1448f052014-04-08 13:41:39 -0700707 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
708 return ((this.mTransportTypes == 0) ||
709 ((this.mTransportTypes & nc.mTransportTypes) != 0));
710 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900711
Robert Greenwalt06314e42014-10-29 14:04:06 -0700712 /** @hide */
713 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714 return (nc.mTransportTypes == this.mTransportTypes);
715 }
716
717 /**
Chalard Jeanf474fc32018-01-17 15:10:05 +0900718 * UID of the app that manages this network, or INVALID_UID if none/unknown.
719 *
720 * This field keeps track of the UID of the app that created this network and is in charge
721 * of managing it. In the practice, it is used to store the UID of VPN apps so it is named
722 * accordingly, but it may be renamed if other mechanisms are offered for third party apps
723 * to create networks.
724 *
725 * Because this field is only used in the services side (and to avoid apps being able to
726 * set this to whatever they want), this field is not parcelled and will not be conserved
727 * across the IPC boundary.
728 * @hide
729 */
730 private int mEstablishingVpnAppUid = INVALID_UID;
731
732 /**
733 * Set the UID of the managing app.
734 * @hide
735 */
736 public void setEstablishingVpnAppUid(final int uid) {
737 mEstablishingVpnAppUid = uid;
738 }
739
740 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600741 * Value indicating that link bandwidth is unspecified.
742 * @hide
743 */
744 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
745
746 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700747 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
748 * for the first hop on the given transport. It is not measured, but may take into account
749 * link parameters (Radio technology, allocated channels, etc).
750 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600751 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
752 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700753
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700754 /**
755 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
756 * the estimated first hop transport bandwidth.
757 * <p>
758 * Note that when used to request a network, this specifies the minimum acceptable.
759 * When received as the state of an existing network this specifies the typical
760 * first hop bandwidth expected. This is never measured, but rather is inferred
761 * from technology type and other link parameters. It could be used to differentiate
762 * between very slow 1xRTT cellular links and other faster networks or even between
763 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
764 * fast backhauls and slow backhauls.
765 *
766 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700767 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700768 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600769 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700770 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600771 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700773
774 /**
775 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
776 * the estimated first hop transport bandwidth.
777 *
778 * @return The estimated first hop upstream (device to network) bandwidth.
779 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 public int getLinkUpstreamBandwidthKbps() {
781 return mLinkUpBandwidthKbps;
782 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700783
784 /**
785 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
786 * the estimated first hop transport bandwidth.
787 * <p>
788 * Note that when used to request a network, this specifies the minimum acceptable.
789 * When received as the state of an existing network this specifies the typical
790 * first hop bandwidth expected. This is never measured, but rather is inferred
791 * from technology type and other link parameters. It could be used to differentiate
792 * between very slow 1xRTT cellular links and other faster networks or even between
793 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
794 * fast backhauls and slow backhauls.
795 *
796 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700797 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700798 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600799 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700800 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600801 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700802 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700803
804 /**
805 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
806 * the estimated first hop transport bandwidth.
807 *
808 * @return The estimated first hop downstream (network to device) bandwidth.
809 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700810 public int getLinkDownstreamBandwidthKbps() {
811 return mLinkDownBandwidthKbps;
812 }
813
814 private void combineLinkBandwidths(NetworkCapabilities nc) {
815 this.mLinkUpBandwidthKbps =
816 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
817 this.mLinkDownBandwidthKbps =
818 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
819 }
820 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
821 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
822 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
823 }
824 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
825 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
826 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
827 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600828 /** @hide */
829 public static int minBandwidth(int a, int b) {
830 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
831 return b;
832 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
833 return a;
834 } else {
835 return Math.min(a, b);
836 }
837 }
838 /** @hide */
839 public static int maxBandwidth(int a, int b) {
840 return Math.max(a, b);
841 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700842
Etan Cohena7434272017-04-03 12:17:51 -0700843 private NetworkSpecifier mNetworkSpecifier = null;
844
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700845 /**
846 * Sets the optional bearer specific network specifier.
847 * This has no meaning if a single transport is also not specified, so calling
848 * this without a single transport set will generate an exception, as will
849 * subsequently adding or removing transports after this is set.
850 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700851 *
Etan Cohena7434272017-04-03 12:17:51 -0700852 * @param networkSpecifier A concrete, parcelable framework class that extends
853 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900854 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700855 * @hide
856 */
Etan Cohena7434272017-04-03 12:17:51 -0700857 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
858 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700859 throw new IllegalStateException("Must have a single transport specified to use " +
860 "setNetworkSpecifier");
861 }
Etan Cohena7434272017-04-03 12:17:51 -0700862
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700863 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700864
Pierre Imaic8419a82016-03-22 17:54:54 +0900865 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700866 }
867
868 /**
869 * Gets the optional bearer specific network specifier.
870 *
Etan Cohena7434272017-04-03 12:17:51 -0700871 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
872 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700873 * @hide
874 */
Etan Cohena7434272017-04-03 12:17:51 -0700875 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700876 return mNetworkSpecifier;
877 }
878
879 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700880 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700881 throw new IllegalStateException("Can't combine two networkSpecifiers");
882 }
Etan Cohena7434272017-04-03 12:17:51 -0700883 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700884 }
Etan Cohena7434272017-04-03 12:17:51 -0700885
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700886 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700887 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
888 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700889 }
Etan Cohena7434272017-04-03 12:17:51 -0700890
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700891 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700892 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700893 }
894
Robert Greenwalt1448f052014-04-08 13:41:39 -0700895 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900896 * Magic value that indicates no signal strength provided. A request specifying this value is
897 * always satisfied.
898 *
899 * @hide
900 */
901 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
902
903 /**
904 * Signal strength. This is a signed integer, and higher values indicate better signal.
905 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
906 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700907 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900908
909 /**
910 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
911 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
912 * reported by WifiManager.
913 * <p>
914 * Note that when used to register a network callback, this specifies the minimum acceptable
915 * signal strength. When received as the state of an existing network it specifies the current
916 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
917 * effect when requesting a callback.
918 *
919 * @param signalStrength the bearer-specific signal strength.
920 * @hide
921 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600922 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900923 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600924 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900925 }
926
927 /**
928 * Returns {@code true} if this object specifies a signal strength.
929 *
930 * @hide
931 */
932 public boolean hasSignalStrength() {
933 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
934 }
935
936 /**
937 * Retrieves the signal strength.
938 *
939 * @return The bearer-specific signal strength.
940 * @hide
941 */
942 public int getSignalStrength() {
943 return mSignalStrength;
944 }
945
946 private void combineSignalStrength(NetworkCapabilities nc) {
947 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
948 }
949
950 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
951 return this.mSignalStrength <= nc.mSignalStrength;
952 }
953
954 private boolean equalsSignalStrength(NetworkCapabilities nc) {
955 return this.mSignalStrength == nc.mSignalStrength;
956 }
957
958 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900959 * List of UIDs this network applies to. No restriction if null.
960 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +0900961 * For networks, mUids represent the list of network this applies to, and null means this
962 * network applies to all UIDs.
963 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
964 * must be included in a network so that they match. As an exception to the general rule,
965 * a null mUids field for requests mean "no requirements" rather than what the general rule
966 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
967 * of this API expect in practice. A network that must match all UIDs can still be
968 * expressed with a set ranging the entire set of possible UIDs.
969 * <p>
970 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900971 * the UIDs in this list, and it is their default network. Apps in this list that wish to
972 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
973 * member is null, then the network is not restricted by app UID. If it's an empty list, then
974 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900975 * As a special exception, the app managing this network (as identified by its UID stored in
976 * mEstablishingVpnAppUid) can always see this network. This is embodied by a special check in
977 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
978 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900979 * <p>
980 * Please note that in principle a single app can be associated with multiple UIDs because
981 * each app will have a different UID when it's run as a different (macro-)user. A single
982 * macro user can only have a single active VPN app at any given time however.
983 * <p>
984 * Also please be aware this class does not try to enforce any normalization on this. Callers
985 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
986 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
987 * their own (like requiring sortedness or no overlap) they need to enforce it
988 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
989 * or overlapping ranges are present.
990 *
991 * @hide
992 */
Chalard Jean477e36c2018-01-25 09:41:51 +0900993 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900994
995 /**
Chalard Jeandda156a2018-01-10 21:19:32 +0900996 * Convenience method to set the UIDs this network applies to to a single UID.
997 * @hide
998 */
999 public NetworkCapabilities setSingleUid(int uid) {
1000 final ArraySet<UidRange> identity = new ArraySet<>(1);
1001 identity.add(new UidRange(uid, uid));
1002 setUids(identity);
1003 return this;
1004 }
1005
1006 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001007 * Set the list of UIDs this network applies to.
1008 * This makes a copy of the set so that callers can't modify it after the call.
1009 * @hide
1010 */
1011 public NetworkCapabilities setUids(Set<UidRange> uids) {
1012 if (null == uids) {
1013 mUids = null;
1014 } else {
1015 mUids = new ArraySet<>(uids);
1016 }
1017 return this;
1018 }
1019
1020 /**
1021 * Get the list of UIDs this network applies to.
1022 * This returns a copy of the set so that callers can't modify the original object.
1023 * @hide
1024 */
1025 public Set<UidRange> getUids() {
1026 return null == mUids ? null : new ArraySet<>(mUids);
1027 }
1028
1029 /**
1030 * Test whether this network applies to this UID.
1031 * @hide
1032 */
1033 public boolean appliesToUid(int uid) {
1034 if (null == mUids) return true;
1035 for (UidRange range : mUids) {
1036 if (range.contains(uid)) {
1037 return true;
1038 }
1039 }
1040 return false;
1041 }
1042
1043 /**
1044 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
1045 * <p>
1046 * This test only checks whether equal range objects are in both sets. It will
1047 * return false if the ranges are not exactly the same, even if the covered UIDs
1048 * are for an equivalent result.
1049 * <p>
1050 * Note that this method is not very optimized, which is fine as long as it's not used very
1051 * often.
1052 * <p>
1053 * nc is assumed nonnull.
1054 *
1055 * @hide
1056 */
1057 @VisibleForTesting
1058 public boolean equalsUids(NetworkCapabilities nc) {
1059 Set<UidRange> comparedUids = nc.mUids;
1060 if (null == comparedUids) return null == mUids;
1061 if (null == mUids) return false;
1062 // Make a copy so it can be mutated to check that all ranges in mUids
1063 // also are in uids.
1064 final Set<UidRange> uids = new ArraySet<>(mUids);
1065 for (UidRange range : comparedUids) {
1066 if (!uids.contains(range)) {
1067 return false;
1068 }
1069 uids.remove(range);
1070 }
1071 return uids.isEmpty();
1072 }
1073
1074 /**
1075 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1076 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001077 * This method is called on the NetworkCapabilities embedded in a request with the
1078 * capabilities of an available network. It checks whether all the UIDs from this listen
1079 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1080 * in the passed nc (representing the UIDs that this network is available to).
1081 * <p>
1082 * As a special exception, the UID that created the passed network (as represented by its
1083 * mEstablishingVpnAppUid field) always satisfies a NetworkRequest requiring it (of LISTEN
1084 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1085 * can see its own network when it listens for it.
1086 * <p>
1087 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001088 * @see #appliesToUid
1089 * @hide
1090 */
1091 public boolean satisfiedByUids(NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001092 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001093 for (UidRange requiredRange : mUids) {
Chalard Jeanf474fc32018-01-17 15:10:05 +09001094 if (requiredRange.contains(nc.mEstablishingVpnAppUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001095 if (!nc.appliesToUidRange(requiredRange)) {
1096 return false;
1097 }
1098 }
1099 return true;
1100 }
1101
1102 /**
1103 * Returns whether this network applies to the passed ranges.
1104 * This assumes that to apply, the passed range has to be entirely contained
1105 * within one of the ranges this network applies to. If the ranges are not normalized,
1106 * this method may return false even though all required UIDs are covered because no
1107 * single range contained them all.
1108 * @hide
1109 */
1110 @VisibleForTesting
1111 public boolean appliesToUidRange(UidRange requiredRange) {
1112 if (null == mUids) return true;
1113 for (UidRange uidRange : mUids) {
1114 if (uidRange.containsRange(requiredRange)) {
1115 return true;
1116 }
1117 }
1118 return false;
1119 }
1120
1121 /**
1122 * Combine the UIDs this network currently applies to with the UIDs the passed
1123 * NetworkCapabilities apply to.
1124 * nc is assumed nonnull.
1125 */
1126 private void combineUids(NetworkCapabilities nc) {
1127 if (null == nc.mUids || null == mUids) {
1128 mUids = null;
1129 return;
1130 }
1131 mUids.addAll(nc.mUids);
1132 }
1133
1134 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001135 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001136 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001137 */
1138 public void combineCapabilities(NetworkCapabilities nc) {
1139 combineNetCapabilities(nc);
1140 combineTransportTypes(nc);
1141 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001142 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001143 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001144 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001145 }
1146
1147 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001148 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1149 *
1150 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1151 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1152 * bandwidth, signal strength, or validation / captive portal status.
1153 *
1154 * @hide
1155 */
1156 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001157 return (nc != null
1158 && satisfiedByNetCapabilities(nc, onlyImmutable)
1159 && satisfiedByTransportTypes(nc)
1160 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1161 && satisfiedBySpecifier(nc)
1162 && (onlyImmutable || satisfiedBySignalStrength(nc))
1163 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001164 }
1165
1166 /**
1167 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1168 *
1169 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1170 *
1171 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001172 */
1173 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001174 return satisfiedByNetworkCapabilities(nc, false);
1175 }
1176
1177 /**
1178 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1179 *
1180 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1181 *
1182 * @hide
1183 */
1184 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1185 return satisfiedByNetworkCapabilities(nc, true);
1186 }
1187
1188 /**
1189 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001190 * {@code NetworkCapabilities} and return a String describing any difference.
1191 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001192 *
1193 * @hide
1194 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001195 public String describeImmutableDifferences(NetworkCapabilities that) {
1196 if (that == null) {
1197 return "other NetworkCapabilities was null";
1198 }
1199
1200 StringJoiner joiner = new StringJoiner(", ");
1201
Hugo Benichieae7a222017-07-25 11:40:56 +09001202 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1203 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001204 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001205 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1206 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1207 if (oldImmutableCapabilities != newImmutableCapabilities) {
1208 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1209 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1210 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1211 }
1212
1213 if (!equalsSpecifier(that)) {
1214 NetworkSpecifier before = this.getNetworkSpecifier();
1215 NetworkSpecifier after = that.getNetworkSpecifier();
1216 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1217 }
1218
1219 if (!equalsTransportTypes(that)) {
1220 String before = transportNamesOf(this.getTransportTypes());
1221 String after = transportNamesOf(that.getTransportTypes());
1222 joiner.add(String.format("transports changed: %s -> %s", before, after));
1223 }
1224
1225 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001226 }
1227
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001228 /**
1229 * Checks that our requestable capabilities are the same as those of the given
1230 * {@code NetworkCapabilities}.
1231 *
1232 * @hide
1233 */
1234 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1235 if (nc == null) return false;
1236 return (equalsNetCapabilitiesRequestable(nc) &&
1237 equalsTransportTypes(nc) &&
1238 equalsSpecifier(nc));
1239 }
1240
Robert Greenwalt1448f052014-04-08 13:41:39 -07001241 @Override
1242 public boolean equals(Object obj) {
1243 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001244 NetworkCapabilities that = (NetworkCapabilities) obj;
1245 return (equalsNetCapabilities(that)
1246 && equalsTransportTypes(that)
1247 && equalsLinkBandwidths(that)
1248 && equalsSignalStrength(that)
1249 && equalsSpecifier(that)
1250 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001251 }
1252
1253 @Override
1254 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001255 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001256 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001257 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1258 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1259 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1260 + ((int) (mTransportTypes >> 32) * 13)
1261 + (mLinkUpBandwidthKbps * 17)
1262 + (mLinkDownBandwidthKbps * 19)
1263 + Objects.hashCode(mNetworkSpecifier) * 23
1264 + (mSignalStrength * 29)
1265 + Objects.hashCode(mUids) * 31;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001266 }
1267
Wink Saville4e2dea72014-09-20 11:04:03 -07001268 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001269 public int describeContents() {
1270 return 0;
1271 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001272 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001273 public void writeToParcel(Parcel dest, int flags) {
1274 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001275 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001276 dest.writeLong(mTransportTypes);
1277 dest.writeInt(mLinkUpBandwidthKbps);
1278 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001279 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001280 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001281 dest.writeArraySet(mUids);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001282 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001283
Robert Greenwalt1448f052014-04-08 13:41:39 -07001284 public static final Creator<NetworkCapabilities> CREATOR =
1285 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001286 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001287 public NetworkCapabilities createFromParcel(Parcel in) {
1288 NetworkCapabilities netCap = new NetworkCapabilities();
1289
1290 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001291 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001292 netCap.mTransportTypes = in.readLong();
1293 netCap.mLinkUpBandwidthKbps = in.readInt();
1294 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001295 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001296 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001297 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1298 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001299 return netCap;
1300 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001301 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001302 public NetworkCapabilities[] newArray(int size) {
1303 return new NetworkCapabilities[size];
1304 }
1305 };
1306
Wink Saville4e2dea72014-09-20 11:04:03 -07001307 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001308 public String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001309 final StringBuilder sb = new StringBuilder("[");
1310 if (0 != mTransportTypes) {
1311 sb.append(" Transports: ");
1312 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1313 NetworkCapabilities::transportNameOf, "|");
1314 }
1315 if (0 != mNetworkCapabilities) {
1316 sb.append(" Capabilities: ");
1317 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1318 NetworkCapabilities::capabilityNameOf, "&");
1319 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001320 if (0 != mNetworkCapabilities) {
1321 sb.append(" Unwanted: ");
1322 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1323 NetworkCapabilities::capabilityNameOf, "&");
1324 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001325 if (mLinkUpBandwidthKbps > 0) {
1326 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1327 }
1328 if (mLinkDownBandwidthKbps > 0) {
1329 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1330 }
1331 if (mNetworkSpecifier != null) {
1332 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1333 }
1334 if (hasSignalStrength()) {
1335 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001336 }
1337
Chalard Jean07ace0f2018-02-26 19:00:45 +09001338 if (null != mUids) {
1339 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1340 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1341 } else {
1342 sb.append(" Uids: <").append(mUids).append(">");
1343 }
1344 }
1345 if (mEstablishingVpnAppUid != INVALID_UID) {
1346 sb.append(" EstablishingAppUid: ").append(mEstablishingVpnAppUid);
1347 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001348
Chalard Jean07ace0f2018-02-26 19:00:45 +09001349 sb.append("]");
1350 return sb.toString();
1351 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001352
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001353
Chalard Jean07ace0f2018-02-26 19:00:45 +09001354 private interface NameOf {
1355 String nameOf(int value);
1356 }
1357 /**
1358 * @hide
1359 */
1360 public static void appendStringRepresentationOfBitMaskToStringBuilder(StringBuilder sb,
1361 long bitMask, NameOf nameFetcher, String separator) {
1362 int bitPos = 0;
1363 boolean firstElementAdded = false;
1364 while (bitMask != 0) {
1365 if ((bitMask & 1) != 0) {
1366 if (firstElementAdded) {
1367 sb.append(separator);
1368 } else {
1369 firstElementAdded = true;
1370 }
1371 sb.append(nameFetcher.nameOf(bitPos));
1372 }
1373 bitMask >>= 1;
1374 ++bitPos;
1375 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001376 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001377
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001378 /** @hide */
1379 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1380 final long token = proto.start(fieldId);
1381
1382 for (int transport : getTransportTypes()) {
1383 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1384 }
1385
1386 for (int capability : getCapabilities()) {
1387 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1388 }
1389
1390 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1391 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1392
1393 if (mNetworkSpecifier != null) {
1394 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1395 }
1396
1397 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1398 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1399
1400 proto.end(token);
1401 }
1402
Hugo Benichi5df9d722016-04-25 17:16:35 +09001403 /**
1404 * @hide
1405 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001406 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001407 StringJoiner joiner = new StringJoiner("|");
1408 if (capabilities != null) {
1409 for (int c : capabilities) {
1410 joiner.add(capabilityNameOf(c));
1411 }
1412 }
1413 return joiner.toString();
1414 }
1415
1416 /**
1417 * @hide
1418 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001419 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001420 switch (capability) {
1421 case NET_CAPABILITY_MMS: return "MMS";
1422 case NET_CAPABILITY_SUPL: return "SUPL";
1423 case NET_CAPABILITY_DUN: return "DUN";
1424 case NET_CAPABILITY_FOTA: return "FOTA";
1425 case NET_CAPABILITY_IMS: return "IMS";
1426 case NET_CAPABILITY_CBS: return "CBS";
1427 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1428 case NET_CAPABILITY_IA: return "IA";
1429 case NET_CAPABILITY_RCS: return "RCS";
1430 case NET_CAPABILITY_XCAP: return "XCAP";
1431 case NET_CAPABILITY_EIMS: return "EIMS";
1432 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1433 case NET_CAPABILITY_INTERNET: return "INTERNET";
1434 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1435 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1436 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1437 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1438 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001439 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001440 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001441 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Chalard Jean804b8fb2018-01-30 22:41:41 +09001442 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
Pavel Maltsev43403202018-01-30 17:19:44 -08001443 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
Hugo Benichieae7a222017-07-25 11:40:56 +09001444 default: return Integer.toString(capability);
1445 }
1446 }
1447
1448 /**
1449 * @hide
1450 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001451 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001452 StringJoiner joiner = new StringJoiner("|");
1453 if (types != null) {
1454 for (int t : types) {
1455 joiner.add(transportNameOf(t));
1456 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001457 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001458 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001459 }
1460
1461 /**
1462 * @hide
1463 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001464 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001465 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001466 return "UNKNOWN";
1467 }
1468 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001469 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001470
Jeff Sharkeyde570312017-10-24 21:25:50 -06001471 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001472 Preconditions.checkArgument(
1473 isValidTransport(transport), "Invalid TransportType " + transport);
1474 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001475
1476 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1477 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1478 }
1479
1480 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1481 Preconditions.checkArgument(isValidCapability(capability),
1482 "NetworkCapability " + capability + "out of range");
1483 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001484}