blob: d0f6f9bf77568dad2e651fede4df71aa17c18e90 [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;
Chalard Jeance1a9d82018-01-23 21:25:37 +090024import 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";
52
Robert Greenwalt7569f182014-06-08 16:42:59 -070053 /**
54 * @hide
55 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070056 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090057 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090058 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070059 }
60
61 public NetworkCapabilities(NetworkCapabilities nc) {
62 if (nc != null) {
63 mNetworkCapabilities = nc.mNetworkCapabilities;
64 mTransportTypes = nc.mTransportTypes;
65 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
66 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070067 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090068 mSignalStrength = nc.mSignalStrength;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090069 mUids = nc.mUids;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070070 }
71 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070072
73 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090074 * Completely clears the contents of this object, removing even the capabilities that are set
75 * by default when the object is constructed.
76 * @hide
77 */
78 public void clearAll() {
79 mNetworkCapabilities = mTransportTypes = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070080 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090081 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090082 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090083 mUids = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090084 }
85
86 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070087 * Represents the network's capabilities. If any are specified they will be satisfied
88 * by any Network that matches all of them.
89 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090090 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070091
Jeff Sharkeyde570312017-10-24 21:25:50 -060092 /** @hide */
93 @Retention(RetentionPolicy.SOURCE)
94 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
95 NET_CAPABILITY_MMS,
96 NET_CAPABILITY_SUPL,
97 NET_CAPABILITY_DUN,
98 NET_CAPABILITY_FOTA,
99 NET_CAPABILITY_IMS,
100 NET_CAPABILITY_CBS,
101 NET_CAPABILITY_WIFI_P2P,
102 NET_CAPABILITY_IA,
103 NET_CAPABILITY_RCS,
104 NET_CAPABILITY_XCAP,
105 NET_CAPABILITY_EIMS,
106 NET_CAPABILITY_NOT_METERED,
107 NET_CAPABILITY_INTERNET,
108 NET_CAPABILITY_NOT_RESTRICTED,
109 NET_CAPABILITY_TRUSTED,
110 NET_CAPABILITY_NOT_VPN,
111 NET_CAPABILITY_VALIDATED,
112 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600113 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600114 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900115 NET_CAPABILITY_NOT_CONGESTED,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600116 })
117 public @interface NetCapability { }
118
Robert Greenwalt1448f052014-04-08 13:41:39 -0700119 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700120 * Indicates this is a network that has the ability to reach the
121 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700122 */
123 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700124
125 /**
126 * Indicates this is a network that has the ability to reach the carrier's
127 * SUPL server, used to retrieve GPS information.
128 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700129 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700130
131 /**
132 * Indicates this is a network that has the ability to reach the carrier's
133 * DUN or tethering gateway.
134 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700135 public static final int NET_CAPABILITY_DUN = 2;
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 * FOTA portal, used for over the air updates.
140 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700141 public static final int NET_CAPABILITY_FOTA = 3;
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 * IMS servers, used for network registration and signaling.
146 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700147 public static final int NET_CAPABILITY_IMS = 4;
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 * CBS servers, used for carrier specific services.
152 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700153 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700154
155 /**
156 * Indicates this is a network that has the ability to reach a Wi-Fi direct
157 * peer.
158 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700159 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700160
161 /**
162 * Indicates this is a network that has the ability to reach a carrier's
163 * Initial Attach servers.
164 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700165 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700166
167 /**
168 * Indicates this is a network that has the ability to reach a carrier's
169 * RCS servers, used for Rich Communication Services.
170 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700171 public static final int NET_CAPABILITY_RCS = 8;
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 * XCAP servers, used for configuration and control.
176 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700177 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178
179 /**
180 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700181 * Emergency IMS servers or other services, used for network signaling
182 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700183 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700184 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700185
186 /**
187 * Indicates that this network is unmetered.
188 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700189 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190
191 /**
192 * Indicates that this network should be able to reach the internet.
193 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195
196 /**
197 * Indicates that this network is available for general use. If this is not set
198 * applications should not attempt to communicate on this network. Note that this
199 * is simply informative and not enforcement - enforcement is handled via other means.
200 * Set by default.
201 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700202 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
203
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700204 /**
205 * Indicates that the user has indicated implicit trust of this network. This
206 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
207 * BT device or a wifi the user asked to connect to. Untrusted networks
208 * are probably limited to unknown wifi AP. Set by default.
209 */
210 public static final int NET_CAPABILITY_TRUSTED = 14;
211
Paul Jensen76b610a2015-03-18 09:33:07 -0400212 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400213 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400214 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400215 */
216 public static final int NET_CAPABILITY_NOT_VPN = 15;
217
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900218 /**
219 * Indicates that connectivity on this network was successfully validated. For example, for a
220 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
221 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900222 */
223 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700224
Paul Jensen3d194ea2015-06-16 14:27:36 -0400225 /**
226 * Indicates that this network was found to have a captive portal in place last time it was
227 * probed.
228 */
229 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
230
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900231 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600232 * Indicates that this network is not roaming.
233 */
234 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
235
236 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900237 * Indicates that this network is available for use by apps, and not a network that is being
238 * kept up in the background to facilitate fast network switching.
239 * @hide
240 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600241 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900242
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900243 /**
244 * Indicates that this network is not congested.
245 * <p>
246 * When a network is congested, the device should defer network traffic that
247 * can be done at a later time without breaking developer contracts.
248 * @hide
249 */
250 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
251
Robert Greenwalt1448f052014-04-08 13:41:39 -0700252 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900253 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_CONGESTED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700254
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700255 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900256 * Network capabilities that are expected to be mutable, i.e., can change while a particular
257 * network is connected.
258 */
259 private static final long MUTABLE_CAPABILITIES =
260 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
261 // http://b/18206275
262 (1 << NET_CAPABILITY_TRUSTED) |
263 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900264 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600265 (1 << NET_CAPABILITY_NOT_ROAMING) |
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900266 (1 << NET_CAPABILITY_FOREGROUND) |
267 (1 << NET_CAPABILITY_NOT_CONGESTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900268
269 /**
270 * Network capabilities that are not allowed in NetworkRequests. This exists because the
271 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
272 * capability's presence cannot be known in advance. If such a capability is requested, then we
273 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
274 * get immediately torn down because they do not have the requested capability.
275 */
276 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900277 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900278
279 /**
280 * Capabilities that are set by default when the object is constructed.
281 */
282 private static final long DEFAULT_CAPABILITIES =
283 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
284 (1 << NET_CAPABILITY_TRUSTED) |
285 (1 << NET_CAPABILITY_NOT_VPN);
286
287 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400288 * Capabilities that suggest that a network is restricted.
289 * {@see #maybeMarkCapabilitiesRestricted}.
290 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700291 @VisibleForTesting
292 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400293 (1 << NET_CAPABILITY_CBS) |
294 (1 << NET_CAPABILITY_DUN) |
295 (1 << NET_CAPABILITY_EIMS) |
296 (1 << NET_CAPABILITY_FOTA) |
297 (1 << NET_CAPABILITY_IA) |
298 (1 << NET_CAPABILITY_IMS) |
299 (1 << NET_CAPABILITY_RCS) |
300 (1 << NET_CAPABILITY_XCAP);
301
302 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700303 * Capabilities that suggest that a network is unrestricted.
304 * {@see #maybeMarkCapabilitiesRestricted}.
305 */
306 @VisibleForTesting
307 /* package */ static final long UNRESTRICTED_CAPABILITIES =
308 (1 << NET_CAPABILITY_INTERNET) |
309 (1 << NET_CAPABILITY_MMS) |
310 (1 << NET_CAPABILITY_SUPL) |
311 (1 << NET_CAPABILITY_WIFI_P2P);
312
313 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700314 * Adds the given capability to this {@code NetworkCapability} instance.
315 * Multiple capabilities may be applied sequentially. Note that when searching
316 * for a network to satisfy a request, all capabilities requested must be satisfied.
317 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600318 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900319 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700320 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700321 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600322 public NetworkCapabilities addCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700323 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700324 throw new IllegalArgumentException("NetworkCapability out of range");
325 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700326 mNetworkCapabilities |= 1 << capability;
327 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700328 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700329
330 /**
331 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
332 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600333 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900334 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700335 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700336 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600337 public NetworkCapabilities removeCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700338 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339 throw new IllegalArgumentException("NetworkCapability out of range");
340 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700341 mNetworkCapabilities &= ~(1 << capability);
342 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700343 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700344
345 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600346 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
347 * instance.
348 *
349 * @hide
350 */
351 public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) {
352 if (value) {
353 addCapability(capability);
354 } else {
355 removeCapability(capability);
356 }
357 return this;
358 }
359
360 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700361 * Gets all the capabilities set on this {@code NetworkCapability} instance.
362 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600363 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700364 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700365 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600366 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900367 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700368 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700369
370 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600371 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700372 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600373 *
374 * @hide
375 */
376 public void setCapabilities(@NetCapability int[] capabilities) {
377 mNetworkCapabilities = BitUtils.packBits(capabilities);
378 }
379
380 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700381 * Tests for the presence of a capabilitity on this instance.
382 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600383 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384 * @return {@code true} if set on this instance.
385 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600386 public boolean hasCapability(@NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700387 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700388 return false;
389 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700390 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700391 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700392
Robert Greenwalt1448f052014-04-08 13:41:39 -0700393 private void combineNetCapabilities(NetworkCapabilities nc) {
394 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
395 }
396
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900397 /**
398 * Convenience function that returns a human-readable description of the first mutable
399 * capability we find. Used to present an error message to apps that request mutable
400 * capabilities.
401 *
402 * @hide
403 */
404 public String describeFirstNonRequestableCapability() {
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900405 final long nonRequestable = (mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES);
406 if (nonRequestable != 0) {
407 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900408 }
409 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900410 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900411 return null;
412 }
413
414 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
415 long networkCapabilities = this.mNetworkCapabilities;
416 if (onlyImmutable) {
417 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
418 }
419 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700420 }
421
Robert Greenwalt06314e42014-10-29 14:04:06 -0700422 /** @hide */
423 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700424 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
425 }
426
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900427 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
428 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
429 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
430 }
431
Robert Greenwalt1448f052014-04-08 13:41:39 -0700432 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400433 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
434 * typically provided by restricted networks.
435 *
436 * TODO: consider:
437 * - Renaming it to guessRestrictedCapability and make it set the
438 * restricted capability bit in addition to clearing it.
439 * @hide
440 */
441 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700442 // Verify there aren't any unrestricted capabilities. If there are we say
443 // the whole thing is unrestricted.
444 final boolean hasUnrestrictedCapabilities =
445 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
446
447 // Must have at least some restricted capabilities.
448 final boolean hasRestrictedCapabilities =
449 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
450
451 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400452 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400453 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400454 }
455
456 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700457 * Representing the transport type. Apps should generally not care about transport. A
458 * request for a fast internet connection could be satisfied by a number of different
459 * transports. If any are specified here it will be satisfied a Network that matches
460 * any of them. If a caller doesn't care about the transport it should not specify any.
461 */
462 private long mTransportTypes;
463
Jeff Sharkeyde570312017-10-24 21:25:50 -0600464 /** @hide */
465 @Retention(RetentionPolicy.SOURCE)
466 @IntDef(prefix = { "TRANSPORT_" }, value = {
467 TRANSPORT_CELLULAR,
468 TRANSPORT_WIFI,
469 TRANSPORT_BLUETOOTH,
470 TRANSPORT_ETHERNET,
471 TRANSPORT_VPN,
472 TRANSPORT_WIFI_AWARE,
473 TRANSPORT_LOWPAN,
474 })
475 public @interface Transport { }
476
Robert Greenwalt1448f052014-04-08 13:41:39 -0700477 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700479 */
480 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700481
482 /**
483 * Indicates this network uses a Wi-Fi transport.
484 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700485 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700486
487 /**
488 * Indicates this network uses a Bluetooth transport.
489 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700491
492 /**
493 * Indicates this network uses an Ethernet transport.
494 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700495 public static final int TRANSPORT_ETHERNET = 3;
496
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400497 /**
498 * Indicates this network uses a VPN transport.
499 */
500 public static final int TRANSPORT_VPN = 4;
501
Etan Cohen305ea282016-06-20 09:27:12 -0700502 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700503 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700504 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700505 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700506
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700507 /**
508 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700509 */
510 public static final int TRANSPORT_LOWPAN = 6;
511
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900512 /** @hide */
513 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
514 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700515 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700516
Hugo Benichi16f0a942017-06-20 14:07:59 +0900517 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600518 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900519 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
520 }
521
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900522 private static final String[] TRANSPORT_NAMES = {
523 "CELLULAR",
524 "WIFI",
525 "BLUETOOTH",
526 "ETHERNET",
527 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700528 "WIFI_AWARE",
529 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900530 };
531
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700532 /**
533 * Adds the given transport type to this {@code NetworkCapability} instance.
534 * Multiple transports may be applied sequentially. Note that when searching
535 * for a network to satisfy a request, any listed in the request will satisfy the request.
536 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
537 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
538 * to be selected. This is logically different than
539 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
540 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600541 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900542 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700543 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700544 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600545 public NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900546 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700547 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700548 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700549 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700550 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700551
552 /**
553 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
554 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600555 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900556 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700557 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700558 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600559 public NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900560 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700561 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700562 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700563 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700564 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700565
566 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600567 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
568 * instance.
569 *
570 * @hide
571 */
572 public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) {
573 if (value) {
574 addTransportType(transportType);
575 } else {
576 removeTransportType(transportType);
577 }
578 return this;
579 }
580
581 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700582 * Gets all the transports set on this {@code NetworkCapability} instance.
583 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600584 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700585 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700586 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600587 public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900588 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700589 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700590
591 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600592 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700593 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600594 *
595 * @hide
596 */
597 public void setTransportTypes(@Transport int[] transportTypes) {
598 mTransportTypes = BitUtils.packBits(transportTypes);
599 }
600
601 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700602 * Tests for the presence of a transport on this instance.
603 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600604 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700605 * @return {@code true} if set on this instance.
606 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600607 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900608 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700609 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700610
611 private void combineTransportTypes(NetworkCapabilities nc) {
612 this.mTransportTypes |= nc.mTransportTypes;
613 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900614
Robert Greenwalt1448f052014-04-08 13:41:39 -0700615 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
616 return ((this.mTransportTypes == 0) ||
617 ((this.mTransportTypes & nc.mTransportTypes) != 0));
618 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900619
Robert Greenwalt06314e42014-10-29 14:04:06 -0700620 /** @hide */
621 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700622 return (nc.mTransportTypes == this.mTransportTypes);
623 }
624
625 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600626 * Value indicating that link bandwidth is unspecified.
627 * @hide
628 */
629 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
630
631 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700632 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
633 * for the first hop on the given transport. It is not measured, but may take into account
634 * link parameters (Radio technology, allocated channels, etc).
635 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600636 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
637 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700638
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700639 /**
640 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
641 * the estimated first hop transport bandwidth.
642 * <p>
643 * Note that when used to request a network, this specifies the minimum acceptable.
644 * When received as the state of an existing network this specifies the typical
645 * first hop bandwidth expected. This is never measured, but rather is inferred
646 * from technology type and other link parameters. It could be used to differentiate
647 * between very slow 1xRTT cellular links and other faster networks or even between
648 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
649 * fast backhauls and slow backhauls.
650 *
651 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700652 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700653 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600654 public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700655 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600656 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700657 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700658
659 /**
660 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
661 * the estimated first hop transport bandwidth.
662 *
663 * @return The estimated first hop upstream (device to network) bandwidth.
664 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700665 public int getLinkUpstreamBandwidthKbps() {
666 return mLinkUpBandwidthKbps;
667 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700668
669 /**
670 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
671 * the estimated first hop transport bandwidth.
672 * <p>
673 * Note that when used to request a network, this specifies the minimum acceptable.
674 * When received as the state of an existing network this specifies the typical
675 * first hop bandwidth expected. This is never measured, but rather is inferred
676 * from technology type and other link parameters. It could be used to differentiate
677 * between very slow 1xRTT cellular links and other faster networks or even between
678 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
679 * fast backhauls and slow backhauls.
680 *
681 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700682 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700683 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600684 public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700685 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600686 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700687 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700688
689 /**
690 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
691 * the estimated first hop transport bandwidth.
692 *
693 * @return The estimated first hop downstream (network to device) bandwidth.
694 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700695 public int getLinkDownstreamBandwidthKbps() {
696 return mLinkDownBandwidthKbps;
697 }
698
699 private void combineLinkBandwidths(NetworkCapabilities nc) {
700 this.mLinkUpBandwidthKbps =
701 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
702 this.mLinkDownBandwidthKbps =
703 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
704 }
705 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
706 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
707 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
708 }
709 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
710 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
711 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
712 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600713 /** @hide */
714 public static int minBandwidth(int a, int b) {
715 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
716 return b;
717 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
718 return a;
719 } else {
720 return Math.min(a, b);
721 }
722 }
723 /** @hide */
724 public static int maxBandwidth(int a, int b) {
725 return Math.max(a, b);
726 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700727
Etan Cohena7434272017-04-03 12:17:51 -0700728 private NetworkSpecifier mNetworkSpecifier = null;
729
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700730 /**
731 * Sets the optional bearer specific network specifier.
732 * This has no meaning if a single transport is also not specified, so calling
733 * this without a single transport set will generate an exception, as will
734 * subsequently adding or removing transports after this is set.
735 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700736 *
Etan Cohena7434272017-04-03 12:17:51 -0700737 * @param networkSpecifier A concrete, parcelable framework class that extends
738 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900739 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700740 * @hide
741 */
Etan Cohena7434272017-04-03 12:17:51 -0700742 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
743 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700744 throw new IllegalStateException("Must have a single transport specified to use " +
745 "setNetworkSpecifier");
746 }
Etan Cohena7434272017-04-03 12:17:51 -0700747
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700748 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700749
Pierre Imaic8419a82016-03-22 17:54:54 +0900750 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700751 }
752
753 /**
754 * Gets the optional bearer specific network specifier.
755 *
Etan Cohena7434272017-04-03 12:17:51 -0700756 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
757 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700758 * @hide
759 */
Etan Cohena7434272017-04-03 12:17:51 -0700760 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700761 return mNetworkSpecifier;
762 }
763
764 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700765 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700766 throw new IllegalStateException("Can't combine two networkSpecifiers");
767 }
Etan Cohena7434272017-04-03 12:17:51 -0700768 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700769 }
Etan Cohena7434272017-04-03 12:17:51 -0700770
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700771 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700772 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
773 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700774 }
Etan Cohena7434272017-04-03 12:17:51 -0700775
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700776 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700777 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700778 }
779
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900781 * Magic value that indicates no signal strength provided. A request specifying this value is
782 * always satisfied.
783 *
784 * @hide
785 */
786 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
787
788 /**
789 * Signal strength. This is a signed integer, and higher values indicate better signal.
790 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
791 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700792 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900793
794 /**
795 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
796 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
797 * reported by WifiManager.
798 * <p>
799 * Note that when used to register a network callback, this specifies the minimum acceptable
800 * signal strength. When received as the state of an existing network it specifies the current
801 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
802 * effect when requesting a callback.
803 *
804 * @param signalStrength the bearer-specific signal strength.
805 * @hide
806 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600807 public NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900808 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600809 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900810 }
811
812 /**
813 * Returns {@code true} if this object specifies a signal strength.
814 *
815 * @hide
816 */
817 public boolean hasSignalStrength() {
818 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
819 }
820
821 /**
822 * Retrieves the signal strength.
823 *
824 * @return The bearer-specific signal strength.
825 * @hide
826 */
827 public int getSignalStrength() {
828 return mSignalStrength;
829 }
830
831 private void combineSignalStrength(NetworkCapabilities nc) {
832 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
833 }
834
835 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
836 return this.mSignalStrength <= nc.mSignalStrength;
837 }
838
839 private boolean equalsSignalStrength(NetworkCapabilities nc) {
840 return this.mSignalStrength == nc.mSignalStrength;
841 }
842
843 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900844 * List of UIDs this network applies to. No restriction if null.
845 * <p>
846 * This is typically (and at this time, only) used by VPN. This network is only available to
847 * the UIDs in this list, and it is their default network. Apps in this list that wish to
848 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
849 * member is null, then the network is not restricted by app UID. If it's an empty list, then
850 * it means nobody can use it.
851 * <p>
852 * Please note that in principle a single app can be associated with multiple UIDs because
853 * each app will have a different UID when it's run as a different (macro-)user. A single
854 * macro user can only have a single active VPN app at any given time however.
855 * <p>
856 * Also please be aware this class does not try to enforce any normalization on this. Callers
857 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
858 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
859 * their own (like requiring sortedness or no overlap) they need to enforce it
860 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
861 * or overlapping ranges are present.
862 *
863 * @hide
864 */
865 private Set<UidRange> mUids = null;
866
867 /**
868 * Set the list of UIDs this network applies to.
869 * This makes a copy of the set so that callers can't modify it after the call.
870 * @hide
871 */
872 public NetworkCapabilities setUids(Set<UidRange> uids) {
873 if (null == uids) {
874 mUids = null;
875 } else {
876 mUids = new ArraySet<>(uids);
877 }
878 return this;
879 }
880
881 /**
882 * Get the list of UIDs this network applies to.
883 * This returns a copy of the set so that callers can't modify the original object.
884 * @hide
885 */
886 public Set<UidRange> getUids() {
887 return null == mUids ? null : new ArraySet<>(mUids);
888 }
889
890 /**
891 * Test whether this network applies to this UID.
892 * @hide
893 */
894 public boolean appliesToUid(int uid) {
895 if (null == mUids) return true;
896 for (UidRange range : mUids) {
897 if (range.contains(uid)) {
898 return true;
899 }
900 }
901 return false;
902 }
903
904 /**
905 * Tests if the set of UIDs that this network applies to is the same of the passed set of UIDs.
906 * <p>
907 * This test only checks whether equal range objects are in both sets. It will
908 * return false if the ranges are not exactly the same, even if the covered UIDs
909 * are for an equivalent result.
910 * <p>
911 * Note that this method is not very optimized, which is fine as long as it's not used very
912 * often.
913 * <p>
914 * nc is assumed nonnull.
915 *
916 * @hide
917 */
918 @VisibleForTesting
919 public boolean equalsUids(NetworkCapabilities nc) {
920 Set<UidRange> comparedUids = nc.mUids;
921 if (null == comparedUids) return null == mUids;
922 if (null == mUids) return false;
923 // Make a copy so it can be mutated to check that all ranges in mUids
924 // also are in uids.
925 final Set<UidRange> uids = new ArraySet<>(mUids);
926 for (UidRange range : comparedUids) {
927 if (!uids.contains(range)) {
928 return false;
929 }
930 uids.remove(range);
931 }
932 return uids.isEmpty();
933 }
934
935 /**
936 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
937 *
938 * This is called on the NetworkCapabilities embedded in a request with the capabilities
939 * of an available network.
940 * nc is assumed nonnull.
941 * @see #appliesToUid
942 * @hide
943 */
944 public boolean satisfiedByUids(NetworkCapabilities nc) {
945 if (null == nc.mUids) return true; // The network satisfies everything.
946 if (null == mUids) return false; // Not everything allowed but requires everything
947 for (UidRange requiredRange : mUids) {
948 if (!nc.appliesToUidRange(requiredRange)) {
949 return false;
950 }
951 }
952 return true;
953 }
954
955 /**
956 * Returns whether this network applies to the passed ranges.
957 * This assumes that to apply, the passed range has to be entirely contained
958 * within one of the ranges this network applies to. If the ranges are not normalized,
959 * this method may return false even though all required UIDs are covered because no
960 * single range contained them all.
961 * @hide
962 */
963 @VisibleForTesting
964 public boolean appliesToUidRange(UidRange requiredRange) {
965 if (null == mUids) return true;
966 for (UidRange uidRange : mUids) {
967 if (uidRange.containsRange(requiredRange)) {
968 return true;
969 }
970 }
971 return false;
972 }
973
974 /**
975 * Combine the UIDs this network currently applies to with the UIDs the passed
976 * NetworkCapabilities apply to.
977 * nc is assumed nonnull.
978 */
979 private void combineUids(NetworkCapabilities nc) {
980 if (null == nc.mUids || null == mUids) {
981 mUids = null;
982 return;
983 }
984 mUids.addAll(nc.mUids);
985 }
986
987 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700988 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900989 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700990 */
991 public void combineCapabilities(NetworkCapabilities nc) {
992 combineNetCapabilities(nc);
993 combineTransportTypes(nc);
994 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700995 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900996 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900997 combineUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700998 }
999
1000 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001001 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1002 *
1003 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1004 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1005 * bandwidth, signal strength, or validation / captive portal status.
1006 *
1007 * @hide
1008 */
1009 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001010 return (nc != null
1011 && satisfiedByNetCapabilities(nc, onlyImmutable)
1012 && satisfiedByTransportTypes(nc)
1013 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1014 && satisfiedBySpecifier(nc)
1015 && (onlyImmutable || satisfiedBySignalStrength(nc))
1016 && (onlyImmutable || satisfiedByUids(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001017 }
1018
1019 /**
1020 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1021 *
1022 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1023 *
1024 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001025 */
1026 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001027 return satisfiedByNetworkCapabilities(nc, false);
1028 }
1029
1030 /**
1031 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1032 *
1033 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1034 *
1035 * @hide
1036 */
1037 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
1038 return satisfiedByNetworkCapabilities(nc, true);
1039 }
1040
1041 /**
1042 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001043 * {@code NetworkCapabilities} and return a String describing any difference.
1044 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001045 *
1046 * @hide
1047 */
Hugo Benichieae7a222017-07-25 11:40:56 +09001048 public String describeImmutableDifferences(NetworkCapabilities that) {
1049 if (that == null) {
1050 return "other NetworkCapabilities was null";
1051 }
1052
1053 StringJoiner joiner = new StringJoiner(", ");
1054
Hugo Benichieae7a222017-07-25 11:40:56 +09001055 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1056 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichia8f39572017-09-30 22:17:07 +09001057 // Ignore DUN being added or removed. http://b/65257223.
1058 final long mask = ~MUTABLE_CAPABILITIES
1059 & ~(1 << NET_CAPABILITY_NOT_METERED) & ~(1 << NET_CAPABILITY_DUN);
Hugo Benichieae7a222017-07-25 11:40:56 +09001060 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1061 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1062 if (oldImmutableCapabilities != newImmutableCapabilities) {
1063 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1064 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1065 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1066 }
1067
1068 if (!equalsSpecifier(that)) {
1069 NetworkSpecifier before = this.getNetworkSpecifier();
1070 NetworkSpecifier after = that.getNetworkSpecifier();
1071 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1072 }
1073
1074 if (!equalsTransportTypes(that)) {
1075 String before = transportNamesOf(this.getTransportTypes());
1076 String after = transportNamesOf(that.getTransportTypes());
1077 joiner.add(String.format("transports changed: %s -> %s", before, after));
1078 }
1079
1080 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001081 }
1082
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001083 /**
1084 * Checks that our requestable capabilities are the same as those of the given
1085 * {@code NetworkCapabilities}.
1086 *
1087 * @hide
1088 */
1089 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
1090 if (nc == null) return false;
1091 return (equalsNetCapabilitiesRequestable(nc) &&
1092 equalsTransportTypes(nc) &&
1093 equalsSpecifier(nc));
1094 }
1095
Robert Greenwalt1448f052014-04-08 13:41:39 -07001096 @Override
1097 public boolean equals(Object obj) {
1098 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001099 NetworkCapabilities that = (NetworkCapabilities) obj;
1100 return (equalsNetCapabilities(that)
1101 && equalsTransportTypes(that)
1102 && equalsLinkBandwidths(that)
1103 && equalsSignalStrength(that)
1104 && equalsSpecifier(that)
1105 && equalsUids(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001106 }
1107
1108 @Override
1109 public int hashCode() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001110 return ((int) (mNetworkCapabilities & 0xFFFFFFFF)
1111 + ((int) (mNetworkCapabilities >> 32) * 3)
1112 + ((int) (mTransportTypes & 0xFFFFFFFF) * 5)
1113 + ((int) (mTransportTypes >> 32) * 7)
1114 + (mLinkUpBandwidthKbps * 11)
1115 + (mLinkDownBandwidthKbps * 13)
1116 + Objects.hashCode(mNetworkSpecifier) * 17
1117 + (mSignalStrength * 19)
1118 + Objects.hashCode(mUids) * 23);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001119 }
1120
Wink Saville4e2dea72014-09-20 11:04:03 -07001121 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001122 public int describeContents() {
1123 return 0;
1124 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001125 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001126 public void writeToParcel(Parcel dest, int flags) {
1127 dest.writeLong(mNetworkCapabilities);
1128 dest.writeLong(mTransportTypes);
1129 dest.writeInt(mLinkUpBandwidthKbps);
1130 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001131 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001132 dest.writeInt(mSignalStrength);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001133 dest.writeArraySet(new ArraySet<>(mUids));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001134 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001135
Robert Greenwalt1448f052014-04-08 13:41:39 -07001136 public static final Creator<NetworkCapabilities> CREATOR =
1137 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001138 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001139 public NetworkCapabilities createFromParcel(Parcel in) {
1140 NetworkCapabilities netCap = new NetworkCapabilities();
1141
1142 netCap.mNetworkCapabilities = in.readLong();
1143 netCap.mTransportTypes = in.readLong();
1144 netCap.mLinkUpBandwidthKbps = in.readInt();
1145 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001146 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001147 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001148 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1149 null /* ClassLoader, null for default */);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001150 return netCap;
1151 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001152 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001153 public NetworkCapabilities[] newArray(int size) {
1154 return new NetworkCapabilities[size];
1155 }
1156 };
1157
Wink Saville4e2dea72014-09-20 11:04:03 -07001158 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001159 public String toString() {
Hugo Benichieae7a222017-07-25 11:40:56 +09001160 // TODO: enumerate bits for transports and capabilities instead of creating arrays.
1161 // TODO: use a StringBuilder instead of string concatenation.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001162 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +09001163 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001164
Robert Greenwalt7569f182014-06-08 16:42:59 -07001165 types = getCapabilities();
1166 String capabilities = (types.length > 0 ? " Capabilities: " : "");
1167 for (int i = 0; i < types.length; ) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001168 capabilities += capabilityNameOf(types[i]);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001169 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001170 }
1171
1172 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
1173 mLinkUpBandwidthKbps + "Kbps" : "");
1174 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
1175 mLinkDownBandwidthKbps + "Kbps" : "");
1176
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001177 String specifier = (mNetworkSpecifier == null ?
1178 "" : " Specifier: <" + mNetworkSpecifier + ">");
1179
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001180 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
1181
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001182 String uids = (null != mUids ? " Uids: <" + mUids + ">" : "");
1183
1184 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength
1185 + uids + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -07001186 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001187
1188 /**
1189 * @hide
1190 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001191 public static String capabilityNamesOf(@NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001192 StringJoiner joiner = new StringJoiner("|");
1193 if (capabilities != null) {
1194 for (int c : capabilities) {
1195 joiner.add(capabilityNameOf(c));
1196 }
1197 }
1198 return joiner.toString();
1199 }
1200
1201 /**
1202 * @hide
1203 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001204 public static String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001205 switch (capability) {
1206 case NET_CAPABILITY_MMS: return "MMS";
1207 case NET_CAPABILITY_SUPL: return "SUPL";
1208 case NET_CAPABILITY_DUN: return "DUN";
1209 case NET_CAPABILITY_FOTA: return "FOTA";
1210 case NET_CAPABILITY_IMS: return "IMS";
1211 case NET_CAPABILITY_CBS: return "CBS";
1212 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1213 case NET_CAPABILITY_IA: return "IA";
1214 case NET_CAPABILITY_RCS: return "RCS";
1215 case NET_CAPABILITY_XCAP: return "XCAP";
1216 case NET_CAPABILITY_EIMS: return "EIMS";
1217 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1218 case NET_CAPABILITY_INTERNET: return "INTERNET";
1219 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1220 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1221 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1222 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1223 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001224 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
Hugo Benichieae7a222017-07-25 11:40:56 +09001225 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +09001226 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
Hugo Benichieae7a222017-07-25 11:40:56 +09001227 default: return Integer.toString(capability);
1228 }
1229 }
1230
1231 /**
1232 * @hide
1233 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001234 public static String transportNamesOf(@Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001235 StringJoiner joiner = new StringJoiner("|");
1236 if (types != null) {
1237 for (int t : types) {
1238 joiner.add(transportNameOf(t));
1239 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001240 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001241 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001242 }
1243
1244 /**
1245 * @hide
1246 */
Jeff Sharkeyde570312017-10-24 21:25:50 -06001247 public static String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001248 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001249 return "UNKNOWN";
1250 }
1251 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001252 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001253
Jeff Sharkeyde570312017-10-24 21:25:50 -06001254 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001255 Preconditions.checkArgument(
1256 isValidTransport(transport), "Invalid TransportType " + transport);
1257 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001258}