blob: 7d8df6a8ffb993f9a8eb909835a8863c2048233e [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;
paulhud9736de2019-03-08 16:35:20 +080020import android.annotation.NonNull;
Etan Cohenca9fb562018-11-27 07:32:39 -080021import android.annotation.Nullable;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070022import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060023import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000024import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060025import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010026import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070027import android.os.Parcel;
28import android.os.Parcelable;
Qingxi Li7cf06622020-01-17 17:54:27 -080029import android.os.Process;
Roshan Piuse38acab2020-01-16 12:17:17 -080030import android.text.TextUtils;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090031import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080032import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070033
34import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090035import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090036import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070037
Jeff Sharkeyde570312017-10-24 21:25:50 -060038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Cody Kestingf7ac9962020-03-16 18:15:28 -070040import java.util.Arrays;
Etan Cohena7434272017-04-03 12:17:51 -070041import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090042import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090043import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070044
45/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070046 * Representation of the capabilities of an active network. Instances are
47 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060048 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
49 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060050 * <p>
51 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
52 * network selection. Rather than indicate a need for Wi-Fi because an
53 * application needs high bandwidth and risk obsolescence when a new, fast
54 * network appears (like LTE), the application should specify it needs high
55 * bandwidth. Similarly if an application needs an unmetered network for a bulk
56 * transfer it can specify that rather than assuming all cellular based
57 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070058 */
59public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070060 private static final String TAG = "NetworkCapabilities";
61
lucaslin783f2212019-10-22 18:27:33 +080062 // Set to true when private DNS is broken.
63 private boolean mPrivateDnsBroken;
64
Roshan Piuse38acab2020-01-16 12:17:17 -080065 /**
66 * Uid of the app making the request.
67 */
68 private int mRequestorUid;
69
70 /**
71 * Package name of the app making the request.
72 */
73 private String mRequestorPackageName;
74
Robert Greenwalt01d004e2014-05-18 15:24:21 -070075 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090076 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090077 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070078 }
79
80 public NetworkCapabilities(NetworkCapabilities nc) {
81 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090082 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070083 }
84 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070085
86 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090087 * Completely clears the contents of this object, removing even the capabilities that are set
88 * by default when the object is constructed.
Lorenzo Colittif7058f52015-04-27 11:31:55 +090089 */
90 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080091 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070092 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090093 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080094 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090095 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090096 mUids = null;
Cody Kestingf7ac9962020-03-16 18:15:28 -070097 mAdministratorUids = new int[0];
Qingxi Li7cf06622020-01-17 17:54:27 -080098 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090099 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +0800100 mPrivateDnsBroken = false;
Roshan Piuse38acab2020-01-16 12:17:17 -0800101 mRequestorUid = Process.INVALID_UID;
102 mRequestorPackageName = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900103 }
104
105 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +0900106 * Set all contents of this object to the contents of a NetworkCapabilities.
107 * @hide
108 */
paulhud9736de2019-03-08 16:35:20 +0800109 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900110 mNetworkCapabilities = nc.mNetworkCapabilities;
111 mTransportTypes = nc.mTransportTypes;
112 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
113 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
114 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800115 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900116 mSignalStrength = nc.mSignalStrength;
117 setUids(nc.mUids); // Will make the defensive copy
Cody Kesting201fc132020-01-17 11:58:36 -0800118 setAdministratorUids(nc.mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -0800119 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900120 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
121 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800122 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Roshan Piuse38acab2020-01-16 12:17:17 -0800123 mRequestorUid = nc.mRequestorUid;
124 mRequestorPackageName = nc.mRequestorPackageName;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900125 }
126
127 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700128 * Represents the network's capabilities. If any are specified they will be satisfied
129 * by any Network that matches all of them.
130 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100131 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900132 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800134 /**
135 * If any capabilities specified here they must not exist in the matching Network.
136 */
137 private long mUnwantedNetworkCapabilities;
138
Jeff Sharkeyde570312017-10-24 21:25:50 -0600139 /** @hide */
140 @Retention(RetentionPolicy.SOURCE)
141 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
142 NET_CAPABILITY_MMS,
143 NET_CAPABILITY_SUPL,
144 NET_CAPABILITY_DUN,
145 NET_CAPABILITY_FOTA,
146 NET_CAPABILITY_IMS,
147 NET_CAPABILITY_CBS,
148 NET_CAPABILITY_WIFI_P2P,
149 NET_CAPABILITY_IA,
150 NET_CAPABILITY_RCS,
151 NET_CAPABILITY_XCAP,
152 NET_CAPABILITY_EIMS,
153 NET_CAPABILITY_NOT_METERED,
154 NET_CAPABILITY_INTERNET,
155 NET_CAPABILITY_NOT_RESTRICTED,
156 NET_CAPABILITY_TRUSTED,
157 NET_CAPABILITY_NOT_VPN,
158 NET_CAPABILITY_VALIDATED,
159 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600160 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600161 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900162 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900163 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800164 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800165 NET_CAPABILITY_MCX,
166 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600167 })
168 public @interface NetCapability { }
169
Robert Greenwalt1448f052014-04-08 13:41:39 -0700170 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700171 * Indicates this is a network that has the ability to reach the
172 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 */
174 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700175
176 /**
177 * Indicates this is a network that has the ability to reach the carrier's
178 * SUPL server, used to retrieve GPS information.
179 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach the carrier's
184 * DUN or tethering gateway.
185 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Indicates this is a network that has the ability to reach the carrier's
190 * FOTA portal, used for over the air updates.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates this is a network that has the ability to reach the carrier's
196 * IMS servers, used for network registration and signaling.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates this is a network that has the ability to reach the carrier's
202 * CBS servers, used for carrier specific services.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates this is a network that has the ability to reach a Wi-Fi direct
208 * peer.
209 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700210 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700211
212 /**
213 * Indicates this is a network that has the ability to reach a carrier's
214 * Initial Attach servers.
215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700217
218 /**
219 * Indicates this is a network that has the ability to reach a carrier's
220 * RCS servers, used for Rich Communication Services.
221 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700222 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700223
224 /**
225 * Indicates this is a network that has the ability to reach a carrier's
226 * XCAP servers, used for configuration and control.
227 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700228 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700229
230 /**
231 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700232 * Emergency IMS servers or other services, used for network signaling
233 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700234 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700235 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700236
237 /**
238 * Indicates that this network is unmetered.
239 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700240 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700241
242 /**
243 * Indicates that this network should be able to reach the internet.
244 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700245 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700246
247 /**
248 * Indicates that this network is available for general use. If this is not set
249 * applications should not attempt to communicate on this network. Note that this
250 * is simply informative and not enforcement - enforcement is handled via other means.
251 * Set by default.
252 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700253 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
254
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700255 /**
256 * Indicates that the user has indicated implicit trust of this network. This
257 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
258 * BT device or a wifi the user asked to connect to. Untrusted networks
259 * are probably limited to unknown wifi AP. Set by default.
260 */
261 public static final int NET_CAPABILITY_TRUSTED = 14;
262
Paul Jensen76b610a2015-03-18 09:33:07 -0400263 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400264 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400265 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400266 */
267 public static final int NET_CAPABILITY_NOT_VPN = 15;
268
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900269 /**
270 * Indicates that connectivity on this network was successfully validated. For example, for a
271 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
272 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900273 */
274 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700275
Paul Jensen3d194ea2015-06-16 14:27:36 -0400276 /**
277 * Indicates that this network was found to have a captive portal in place last time it was
278 * probed.
279 */
280 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
281
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900282 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600283 * Indicates that this network is not roaming.
284 */
285 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
286
287 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900288 * Indicates that this network is available for use by apps, and not a network that is being
289 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900290 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600291 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900292
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900293 /**
294 * Indicates that this network is not congested.
295 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600296 * When a network is congested, applications should defer network traffic
297 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900298 */
299 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
300
Chalard Jean804b8fb2018-01-30 22:41:41 +0900301 /**
302 * Indicates that this network is not currently suspended.
303 * <p>
304 * When a network is suspended, the network's IP addresses and any connections
305 * established on the network remain valid, but the network is temporarily unable
306 * to transfer data. This can happen, for example, if a cellular network experiences
307 * a temporary loss of signal, such as when driving through a tunnel, etc.
308 * A network with this capability is not suspended, so is expected to be able to
309 * transfer data.
310 */
311 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
312
Pavel Maltsev43403202018-01-30 17:19:44 -0800313 /**
314 * Indicates that traffic that goes through this network is paid by oem. For example,
315 * this network can be used by system apps to upload telemetry data.
316 * @hide
317 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700318 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800319 public static final int NET_CAPABILITY_OEM_PAID = 22;
320
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800321 /**
322 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
323 * servers.
324 */
325 public static final int NET_CAPABILITY_MCX = 23;
326
lucasline252a742019-03-12 13:08:03 +0800327 /**
328 * Indicates that this network was tested to only provide partial connectivity.
329 * @hide
330 */
331 @SystemApi
332 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
333
Robert Greenwalt1448f052014-04-08 13:41:39 -0700334 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800335 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700336
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700337 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900338 * Network capabilities that are expected to be mutable, i.e., can change while a particular
339 * network is connected.
340 */
341 private static final long MUTABLE_CAPABILITIES =
342 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
343 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900344 (1 << NET_CAPABILITY_TRUSTED)
345 | (1 << NET_CAPABILITY_VALIDATED)
346 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
347 | (1 << NET_CAPABILITY_NOT_ROAMING)
348 | (1 << NET_CAPABILITY_FOREGROUND)
349 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800350 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
351 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900352
353 /**
354 * Network capabilities that are not allowed in NetworkRequests. This exists because the
355 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
356 * capability's presence cannot be known in advance. If such a capability is requested, then we
357 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
358 * get immediately torn down because they do not have the requested capability.
359 */
360 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900361 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900362
363 /**
364 * Capabilities that are set by default when the object is constructed.
365 */
366 private static final long DEFAULT_CAPABILITIES =
367 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
368 (1 << NET_CAPABILITY_TRUSTED) |
369 (1 << NET_CAPABILITY_NOT_VPN);
370
371 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400372 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800373 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400374 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700375 @VisibleForTesting
376 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400377 (1 << NET_CAPABILITY_CBS) |
378 (1 << NET_CAPABILITY_DUN) |
379 (1 << NET_CAPABILITY_EIMS) |
380 (1 << NET_CAPABILITY_FOTA) |
381 (1 << NET_CAPABILITY_IA) |
382 (1 << NET_CAPABILITY_IMS) |
383 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800384 (1 << NET_CAPABILITY_XCAP) |
385 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800386
387 /**
388 * Capabilities that force network to be restricted.
389 * {@see #maybeMarkCapabilitiesRestricted}.
390 */
391 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800392 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400393
394 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700395 * Capabilities that suggest that a network is unrestricted.
396 * {@see #maybeMarkCapabilitiesRestricted}.
397 */
398 @VisibleForTesting
399 /* package */ static final long UNRESTRICTED_CAPABILITIES =
400 (1 << NET_CAPABILITY_INTERNET) |
401 (1 << NET_CAPABILITY_MMS) |
402 (1 << NET_CAPABILITY_SUPL) |
403 (1 << NET_CAPABILITY_WIFI_P2P);
404
405 /**
lucasline252a742019-03-12 13:08:03 +0800406 * Capabilities that are managed by ConnectivityService.
407 */
408 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
409 (1 << NET_CAPABILITY_VALIDATED)
410 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
411 | (1 << NET_CAPABILITY_FOREGROUND)
412 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
413
414 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700415 * Adds the given capability to this {@code NetworkCapability} instance.
416 * Multiple capabilities may be applied sequentially. Note that when searching
417 * for a network to satisfy a request, all capabilities requested must be satisfied.
418 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600419 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900420 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700421 */
paulhud9736de2019-03-08 16:35:20 +0800422 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800423 // If the given capability was previously added to the list of unwanted capabilities
424 // then the capability will also be removed from the list of unwanted capabilities.
425 // TODO: Consider adding unwanted capabilities to the public API and mention this
426 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800427 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700428 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800429 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700430 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700431 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700432
433 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800434 * Adds the given capability to the list of unwanted capabilities of this
435 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
436 * sequentially. Note that when searching for a network to satisfy a request, the network
437 * must not contain any capability from unwanted capability list.
438 * <p>
439 * If the capability was previously added to the list of required capabilities (for
440 * example, it was there by default or added using {@link #addCapability(int)} method), then
441 * it will be removed from the list of required capabilities as well.
442 *
443 * @see #addCapability(int)
444 * @hide
445 */
446 public void addUnwantedCapability(@NetCapability int capability) {
447 checkValidCapability(capability);
448 mUnwantedNetworkCapabilities |= 1 << capability;
449 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
450 }
451
452 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700453 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
454 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600455 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900456 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700457 */
paulhud9736de2019-03-08 16:35:20 +0800458 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800459 // Note that this method removes capabilities that were added via addCapability(int),
460 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800461 checkValidCapability(capability);
462 final long mask = ~(1 << capability);
463 mNetworkCapabilities &= mask;
464 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700465 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700466 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700467
468 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600469 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
470 * instance.
471 *
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600472 */
paulhud9736de2019-03-08 16:35:20 +0800473 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
474 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600475 if (value) {
476 addCapability(capability);
477 } else {
478 removeCapability(capability);
479 }
480 return this;
481 }
482
483 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700484 * Gets all the capabilities set on this {@code NetworkCapability} instance.
485 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600486 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700487 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700488 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000489 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600490 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600491 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900492 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700493 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700494
495 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800496 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
497 *
498 * @return an array of unwanted capability values for this instance.
499 * @hide
500 */
501 public @NetCapability int[] getUnwantedCapabilities() {
502 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
503 }
504
505
506 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600507 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700508 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600509 *
510 * @hide
511 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800512 public void setCapabilities(@NetCapability int[] capabilities,
513 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600514 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800515 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600516 }
517
518 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800519 * @deprecated use {@link #setCapabilities(int[], int[])}
520 * @hide
521 */
522 @Deprecated
523 public void setCapabilities(@NetCapability int[] capabilities) {
524 setCapabilities(capabilities, new int[] {});
525 }
526
527 /**
528 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700529 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600530 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700531 * @return {@code true} if set on this instance.
532 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600533 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800534 return isValidCapability(capability)
535 && ((mNetworkCapabilities & (1 << capability)) != 0);
536 }
537
538 /** @hide */
539 public boolean hasUnwantedCapability(@NetCapability int capability) {
540 return isValidCapability(capability)
541 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700542 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700543
lucasline252a742019-03-12 13:08:03 +0800544 /**
545 * Check if this NetworkCapabilities has system managed capabilities or not.
546 * @hide
547 */
548 public boolean hasConnectivityManagedCapability() {
549 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
550 }
551
Pavel Maltseve18ef262018-03-07 11:13:04 -0800552 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800553 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700554 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800555 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700556 }
557
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900558 /**
559 * Convenience function that returns a human-readable description of the first mutable
560 * capability we find. Used to present an error message to apps that request mutable
561 * capabilities.
562 *
563 * @hide
564 */
paulhud9736de2019-03-08 16:35:20 +0800565 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800566 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
567 & NON_REQUESTABLE_CAPABILITIES;
568
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900569 if (nonRequestable != 0) {
570 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900571 }
572 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900573 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800574 if (isPrivateDnsBroken()) {
575 return "privateDnsBroken";
576 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900577 return null;
578 }
579
paulhud9736de2019-03-08 16:35:20 +0800580 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
581 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800582 long requestedCapabilities = mNetworkCapabilities;
583 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
584 long providedCapabilities = nc.mNetworkCapabilities;
585
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900586 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800587 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
588 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900589 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800590 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
591 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700592 }
593
Robert Greenwalt06314e42014-10-29 14:04:06 -0700594 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800595 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800596 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
597 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700598 }
599
paulhud9736de2019-03-08 16:35:20 +0800600 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900601 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800602 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
603 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
604 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900605 }
606
Robert Greenwalt1448f052014-04-08 13:41:39 -0700607 /**
paulhu18354322020-01-09 17:08:11 +0800608 * Deduces that all the capabilities it provides are typically provided by restricted networks
609 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400610 *
paulhu18354322020-01-09 17:08:11 +0800611 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400612 * @hide
613 */
paulhu18354322020-01-09 17:08:11 +0800614 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800615 // Check if we have any capability that forces the network to be restricted.
616 final boolean forceRestrictedCapability =
617 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
618
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700619 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800620 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700621 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800622 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700623
624 // Must have at least some restricted capabilities.
625 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800626 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700627
paulhu18354322020-01-09 17:08:11 +0800628 return forceRestrictedCapability
629 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
630 }
631
632 /**
633 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
634 *
635 * @hide
636 */
637 public void maybeMarkCapabilitiesRestricted() {
638 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400639 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400640 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400641 }
642
643 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700644 * Representing the transport type. Apps should generally not care about transport. A
645 * request for a fast internet connection could be satisfied by a number of different
646 * transports. If any are specified here it will be satisfied a Network that matches
647 * any of them. If a caller doesn't care about the transport it should not specify any.
648 */
649 private long mTransportTypes;
650
Jeff Sharkeyde570312017-10-24 21:25:50 -0600651 /** @hide */
652 @Retention(RetentionPolicy.SOURCE)
653 @IntDef(prefix = { "TRANSPORT_" }, value = {
654 TRANSPORT_CELLULAR,
655 TRANSPORT_WIFI,
656 TRANSPORT_BLUETOOTH,
657 TRANSPORT_ETHERNET,
658 TRANSPORT_VPN,
659 TRANSPORT_WIFI_AWARE,
660 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800661 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600662 })
663 public @interface Transport { }
664
Robert Greenwalt1448f052014-04-08 13:41:39 -0700665 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700666 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700667 */
668 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700669
670 /**
671 * Indicates this network uses a Wi-Fi transport.
672 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700673 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674
675 /**
676 * Indicates this network uses a Bluetooth transport.
677 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700678 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700679
680 /**
681 * Indicates this network uses an Ethernet transport.
682 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700683 public static final int TRANSPORT_ETHERNET = 3;
684
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400685 /**
686 * Indicates this network uses a VPN transport.
687 */
688 public static final int TRANSPORT_VPN = 4;
689
Etan Cohen305ea282016-06-20 09:27:12 -0700690 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700691 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700692 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700693 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700694
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700695 /**
696 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700697 */
698 public static final int TRANSPORT_LOWPAN = 6;
699
Benedict Wong89ce5e32018-11-14 17:40:55 -0800700 /**
701 * Indicates this network uses a Test-only virtual interface as a transport.
702 *
703 * @hide
704 */
705 @TestApi
706 public static final int TRANSPORT_TEST = 7;
707
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900708 /** @hide */
709 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
710 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800711 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700712
Hugo Benichi16f0a942017-06-20 14:07:59 +0900713 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600714 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900715 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
716 }
717
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900718 private static final String[] TRANSPORT_NAMES = {
719 "CELLULAR",
720 "WIFI",
721 "BLUETOOTH",
722 "ETHERNET",
723 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700724 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800725 "LOWPAN",
726 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900727 };
728
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700729 /**
730 * Adds the given transport type to this {@code NetworkCapability} instance.
731 * Multiple transports may be applied sequentially. Note that when searching
732 * for a network to satisfy a request, any listed in the request will satisfy the request.
733 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
734 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
735 * to be selected. This is logically different than
736 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
737 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600738 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900739 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700740 */
paulhud9736de2019-03-08 16:35:20 +0800741 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900742 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700743 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700744 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700745 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700746 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700747
748 /**
749 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
750 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600751 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900752 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700753 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700754 */
paulhud9736de2019-03-08 16:35:20 +0800755 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900756 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700757 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700758 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700759 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700760 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700761
762 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600763 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
764 * instance.
765 *
766 * @hide
767 */
paulhud9736de2019-03-08 16:35:20 +0800768 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
769 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600770 if (value) {
771 addTransportType(transportType);
772 } else {
773 removeTransportType(transportType);
774 }
775 return this;
776 }
777
778 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700779 * Gets all the transports set on this {@code NetworkCapability} instance.
780 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600781 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700782 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700783 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600784 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900785 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800786 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900787 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700788 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700789
790 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600791 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700792 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600793 *
794 * @hide
795 */
796 public void setTransportTypes(@Transport int[] transportTypes) {
797 mTransportTypes = BitUtils.packBits(transportTypes);
798 }
799
800 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700801 * Tests for the presence of a transport on this instance.
802 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600803 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700804 * @return {@code true} if set on this instance.
805 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600806 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900807 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700808 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700809
810 private void combineTransportTypes(NetworkCapabilities nc) {
811 this.mTransportTypes |= nc.mTransportTypes;
812 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900813
Robert Greenwalt1448f052014-04-08 13:41:39 -0700814 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
815 return ((this.mTransportTypes == 0) ||
816 ((this.mTransportTypes & nc.mTransportTypes) != 0));
817 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900818
Robert Greenwalt06314e42014-10-29 14:04:06 -0700819 /** @hide */
820 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700821 return (nc.mTransportTypes == this.mTransportTypes);
822 }
823
824 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800825 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900826 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800827 * <p>This field keeps track of the UID of the app that created this network and is in charge of
828 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
829 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800830 *
831 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
832 * reset to Process.INVALID_UID unless all the following conditions are met:
833 *
834 * <ol>
835 * <li>The destination app is the network owner
836 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
837 * <li>The user's location toggle is on
838 * </ol>
839 *
840 * This is because the owner UID is location-sensitive. The apps that request a network could
841 * know where the device is if they can tell for sure the system has connected to the network
842 * they requested.
843 *
844 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
845 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
846 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900847 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800848 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900849
850 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800851 * Set the UID of the owner app.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900852 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800853 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800854 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800855 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900856 }
857
Qingxi Li7cf06622020-01-17 17:54:27 -0800858 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800859 * Retrieves the UID of the app that owns this network.
860 *
861 * <p>For user privacy reasons, this field will only be populated if:
862 *
863 * <ol>
864 * <li>The calling app is the network owner
865 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
866 * <li>The user's location toggle is on
867 * </ol>
868 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800869 */
870 public int getOwnerUid() {
871 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000872 }
873
Chalard Jeanf474fc32018-01-17 15:10:05 +0900874 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800875 * UIDs of packages that are administrators of this network, or empty if none.
876 *
877 * <p>This field tracks the UIDs of packages that have permission to manage this network.
878 *
879 * <p>Network owners will also be listed as administrators.
880 *
881 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
882 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
883 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
884 */
Cody Kestingf7ac9962020-03-16 18:15:28 -0700885 private int[] mAdministratorUids = new int[0];
Cody Kesting201fc132020-01-17 11:58:36 -0800886
887 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700888 * Sets the int[] of UIDs that are administrators of this network.
Cody Kesting201fc132020-01-17 11:58:36 -0800889 *
890 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
891 * Examples of UIDs that should be included in administratorUids are:
892 * <ul>
893 * <li>Carrier apps with privileges for the relevant subscription
894 * <li>Active VPN apps
895 * <li>Other application groups with a particular Network-related role
896 * </ul>
897 *
898 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
899 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800900 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
901 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800902 *
903 * @param administratorUids the UIDs to be set as administrators of this Network.
904 * @hide
905 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800906 @NonNull
Cody Kesting201fc132020-01-17 11:58:36 -0800907 @SystemApi
Cody Kestingf7ac9962020-03-16 18:15:28 -0700908 public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) {
909 mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length);
Roshan Piuse38acab2020-01-16 12:17:17 -0800910 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800911 }
912
913 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700914 * Retrieves the UIDs that are administrators of this Network.
Cody Kesting201fc132020-01-17 11:58:36 -0800915 *
Cody Kestingf7ac9962020-03-16 18:15:28 -0700916 * @return the int[] of UIDs that are administrators of this Network
Cody Kesting201fc132020-01-17 11:58:36 -0800917 * @hide
918 */
919 @NonNull
920 @SystemApi
Cody Kestingf7ac9962020-03-16 18:15:28 -0700921 public int[] getAdministratorUids() {
922 return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
Cody Kesting201fc132020-01-17 11:58:36 -0800923 }
924
925 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600926 * Value indicating that link bandwidth is unspecified.
927 * @hide
928 */
929 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
930
931 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700932 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
933 * for the first hop on the given transport. It is not measured, but may take into account
934 * link parameters (Radio technology, allocated channels, etc).
935 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600936 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
937 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700938
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700939 /**
940 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
941 * the estimated first hop transport bandwidth.
942 * <p>
943 * Note that when used to request a network, this specifies the minimum acceptable.
944 * When received as the state of an existing network this specifies the typical
945 * first hop bandwidth expected. This is never measured, but rather is inferred
946 * from technology type and other link parameters. It could be used to differentiate
947 * between very slow 1xRTT cellular links and other faster networks or even between
948 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
949 * fast backhauls and slow backhauls.
950 *
951 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
952 */
paulhud9736de2019-03-08 16:35:20 +0800953 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700954 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600955 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700956 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700957
958 /**
959 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
960 * the estimated first hop transport bandwidth.
961 *
962 * @return The estimated first hop upstream (device to network) bandwidth.
963 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700964 public int getLinkUpstreamBandwidthKbps() {
965 return mLinkUpBandwidthKbps;
966 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700967
968 /**
969 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
970 * the estimated first hop transport bandwidth.
971 * <p>
972 * Note that when used to request a network, this specifies the minimum acceptable.
973 * When received as the state of an existing network this specifies the typical
974 * first hop bandwidth expected. This is never measured, but rather is inferred
975 * from technology type and other link parameters. It could be used to differentiate
976 * between very slow 1xRTT cellular links and other faster networks or even between
977 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
978 * fast backhauls and slow backhauls.
979 *
980 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
981 */
paulhud9736de2019-03-08 16:35:20 +0800982 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700983 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600984 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700985 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700986
987 /**
988 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
989 * the estimated first hop transport bandwidth.
990 *
991 * @return The estimated first hop downstream (network to device) bandwidth.
992 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700993 public int getLinkDownstreamBandwidthKbps() {
994 return mLinkDownBandwidthKbps;
995 }
996
997 private void combineLinkBandwidths(NetworkCapabilities nc) {
998 this.mLinkUpBandwidthKbps =
999 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
1000 this.mLinkDownBandwidthKbps =
1001 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1002 }
1003 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1004 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1005 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1006 }
1007 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1008 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1009 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1010 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001011 /** @hide */
1012 public static int minBandwidth(int a, int b) {
1013 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1014 return b;
1015 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1016 return a;
1017 } else {
1018 return Math.min(a, b);
1019 }
1020 }
1021 /** @hide */
1022 public static int maxBandwidth(int a, int b) {
1023 return Math.max(a, b);
1024 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001025
Etan Cohena7434272017-04-03 12:17:51 -07001026 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001027 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001028
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001029 /**
1030 * Sets the optional bearer specific network specifier.
1031 * This has no meaning if a single transport is also not specified, so calling
1032 * this without a single transport set will generate an exception, as will
1033 * subsequently adding or removing transports after this is set.
1034 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001035 *
Etan Cohena7434272017-04-03 12:17:51 -07001036 * @param networkSpecifier A concrete, parcelable framework class that extends
1037 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001038 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001039 */
Aaron Huange6b62392019-09-20 22:52:54 +08001040 public @NonNull NetworkCapabilities setNetworkSpecifier(
1041 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001042 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001043 throw new IllegalStateException("Must have a single transport specified to use " +
1044 "setNetworkSpecifier");
1045 }
Etan Cohena7434272017-04-03 12:17:51 -07001046
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001047 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001048
Pierre Imaic8419a82016-03-22 17:54:54 +09001049 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001050 }
1051
1052 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001053 * Sets the optional transport specific information.
1054 *
1055 * @param transportInfo A concrete, parcelable framework class that extends
1056 * {@link TransportInfo}.
1057 * @return This NetworkCapabilities instance, to facilitate chaining.
1058 * @hide
1059 */
Aaron Huange6b62392019-09-20 22:52:54 +08001060 @SystemApi
1061 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001062 mTransportInfo = transportInfo;
1063 return this;
1064 }
1065
1066 /**
paulhud9736de2019-03-08 16:35:20 +08001067 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001068 *
Etan Cohena7434272017-04-03 12:17:51 -07001069 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
paulhud9736de2019-03-08 16:35:20 +08001070 * specifier or {@code null}. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001071 */
paulhud9736de2019-03-08 16:35:20 +08001072 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001073 return mNetworkSpecifier;
1074 }
1075
Etan Cohenca9fb562018-11-27 07:32:39 -08001076 /**
1077 * Returns a transport-specific information container. The application may cast this
1078 * container to a concrete sub-class based on its knowledge of the network request. The
1079 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001080 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001081 *
1082 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1083 * available for the network.
1084 */
1085 @Nullable public TransportInfo getTransportInfo() {
1086 return mTransportInfo;
1087 }
1088
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001089 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001090 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001091 throw new IllegalStateException("Can't combine two networkSpecifiers");
1092 }
Etan Cohena7434272017-04-03 12:17:51 -07001093 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001094 }
Etan Cohena7434272017-04-03 12:17:51 -07001095
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001096 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001097 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
1098 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001099 }
Etan Cohena7434272017-04-03 12:17:51 -07001100
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001101 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001102 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001103 }
1104
Etan Cohenca9fb562018-11-27 07:32:39 -08001105 private void combineTransportInfos(NetworkCapabilities nc) {
1106 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1107 throw new IllegalStateException("Can't combine two TransportInfos");
1108 }
1109 setTransportInfo(nc.mTransportInfo);
1110 }
1111
1112 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1113 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1114 }
1115
Robert Greenwalt1448f052014-04-08 13:41:39 -07001116 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001117 * Magic value that indicates no signal strength provided. A request specifying this value is
1118 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001119 */
1120 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1121
1122 /**
1123 * Signal strength. This is a signed integer, and higher values indicate better signal.
1124 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1125 */
paulhud9736de2019-03-08 16:35:20 +08001126 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001127 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001128
1129 /**
1130 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1131 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001132 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001133 * <p>
1134 * Note that when used to register a network callback, this specifies the minimum acceptable
1135 * signal strength. When received as the state of an existing network it specifies the current
1136 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1137 * effect when requesting a callback.
1138 *
1139 * @param signalStrength the bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001140 */
paulhud9736de2019-03-08 16:35:20 +08001141 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001142 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001143 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001144 }
1145
1146 /**
1147 * Returns {@code true} if this object specifies a signal strength.
1148 *
1149 * @hide
1150 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001151 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001152 public boolean hasSignalStrength() {
1153 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1154 }
1155
1156 /**
1157 * Retrieves the signal strength.
1158 *
1159 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001160 */
1161 public int getSignalStrength() {
1162 return mSignalStrength;
1163 }
1164
1165 private void combineSignalStrength(NetworkCapabilities nc) {
1166 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1167 }
1168
1169 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1170 return this.mSignalStrength <= nc.mSignalStrength;
1171 }
1172
1173 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1174 return this.mSignalStrength == nc.mSignalStrength;
1175 }
1176
1177 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001178 * List of UIDs this network applies to. No restriction if null.
1179 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001180 * For networks, mUids represent the list of network this applies to, and null means this
1181 * network applies to all UIDs.
1182 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1183 * must be included in a network so that they match. As an exception to the general rule,
1184 * a null mUids field for requests mean "no requirements" rather than what the general rule
1185 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1186 * of this API expect in practice. A network that must match all UIDs can still be
1187 * expressed with a set ranging the entire set of possible UIDs.
1188 * <p>
1189 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001190 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1191 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1192 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1193 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001194 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001195 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001196 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1197 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001198 * <p>
1199 * Please note that in principle a single app can be associated with multiple UIDs because
1200 * each app will have a different UID when it's run as a different (macro-)user. A single
1201 * macro user can only have a single active VPN app at any given time however.
1202 * <p>
1203 * Also please be aware this class does not try to enforce any normalization on this. Callers
1204 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1205 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1206 * their own (like requiring sortedness or no overlap) they need to enforce it
1207 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1208 * or overlapping ranges are present.
1209 *
1210 * @hide
1211 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001212 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001213
1214 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001215 * Convenience method to set the UIDs this network applies to to a single UID.
1216 * @hide
1217 */
paulhud9736de2019-03-08 16:35:20 +08001218 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001219 final ArraySet<UidRange> identity = new ArraySet<>(1);
1220 identity.add(new UidRange(uid, uid));
1221 setUids(identity);
1222 return this;
1223 }
1224
1225 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001226 * Set the list of UIDs this network applies to.
1227 * This makes a copy of the set so that callers can't modify it after the call.
1228 * @hide
1229 */
paulhud9736de2019-03-08 16:35:20 +08001230 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001231 if (null == uids) {
1232 mUids = null;
1233 } else {
1234 mUids = new ArraySet<>(uids);
1235 }
1236 return this;
1237 }
1238
1239 /**
1240 * Get the list of UIDs this network applies to.
1241 * This returns a copy of the set so that callers can't modify the original object.
1242 * @hide
1243 */
paulhud9736de2019-03-08 16:35:20 +08001244 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001245 return null == mUids ? null : new ArraySet<>(mUids);
1246 }
1247
1248 /**
1249 * Test whether this network applies to this UID.
1250 * @hide
1251 */
1252 public boolean appliesToUid(int uid) {
1253 if (null == mUids) return true;
1254 for (UidRange range : mUids) {
1255 if (range.contains(uid)) {
1256 return true;
1257 }
1258 }
1259 return false;
1260 }
1261
1262 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001263 * Tests if the set of UIDs that this network applies to is the same as the passed network.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001264 * <p>
1265 * This test only checks whether equal range objects are in both sets. It will
1266 * return false if the ranges are not exactly the same, even if the covered UIDs
1267 * are for an equivalent result.
1268 * <p>
1269 * Note that this method is not very optimized, which is fine as long as it's not used very
1270 * often.
1271 * <p>
1272 * nc is assumed nonnull.
1273 *
1274 * @hide
1275 */
1276 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001277 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001278 Set<UidRange> comparedUids = nc.mUids;
1279 if (null == comparedUids) return null == mUids;
1280 if (null == mUids) return false;
1281 // Make a copy so it can be mutated to check that all ranges in mUids
1282 // also are in uids.
1283 final Set<UidRange> uids = new ArraySet<>(mUids);
1284 for (UidRange range : comparedUids) {
1285 if (!uids.contains(range)) {
1286 return false;
1287 }
1288 uids.remove(range);
1289 }
1290 return uids.isEmpty();
1291 }
1292
1293 /**
1294 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1295 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001296 * This method is called on the NetworkCapabilities embedded in a request with the
1297 * capabilities of an available network. It checks whether all the UIDs from this listen
1298 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1299 * in the passed nc (representing the UIDs that this network is available to).
1300 * <p>
1301 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001302 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001303 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1304 * can see its own network when it listens for it.
1305 * <p>
1306 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001307 * @see #appliesToUid
1308 * @hide
1309 */
paulhud9736de2019-03-08 16:35:20 +08001310 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001311 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001312 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001313 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001314 if (!nc.appliesToUidRange(requiredRange)) {
1315 return false;
1316 }
1317 }
1318 return true;
1319 }
1320
1321 /**
1322 * Returns whether this network applies to the passed ranges.
1323 * This assumes that to apply, the passed range has to be entirely contained
1324 * within one of the ranges this network applies to. If the ranges are not normalized,
1325 * this method may return false even though all required UIDs are covered because no
1326 * single range contained them all.
1327 * @hide
1328 */
1329 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001330 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001331 if (null == mUids) return true;
1332 for (UidRange uidRange : mUids) {
1333 if (uidRange.containsRange(requiredRange)) {
1334 return true;
1335 }
1336 }
1337 return false;
1338 }
1339
1340 /**
1341 * Combine the UIDs this network currently applies to with the UIDs the passed
1342 * NetworkCapabilities apply to.
1343 * nc is assumed nonnull.
1344 */
paulhud9736de2019-03-08 16:35:20 +08001345 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001346 if (null == nc.mUids || null == mUids) {
1347 mUids = null;
1348 return;
1349 }
1350 mUids.addAll(nc.mUids);
1351 }
1352
Chalard Jeanb03a6222018-04-11 21:09:10 +09001353
1354 /**
1355 * The SSID of the network, or null if not applicable or unknown.
1356 * <p>
1357 * This is filled in by wifi code.
1358 * @hide
1359 */
1360 private String mSSID;
1361
1362 /**
1363 * Sets the SSID of this network.
1364 * @hide
1365 */
Aaron Huange6b62392019-09-20 22:52:54 +08001366 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001367 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001368 mSSID = ssid;
1369 return this;
1370 }
1371
1372 /**
1373 * Gets the SSID of this network, or null if none or unknown.
1374 * @hide
1375 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001376 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001377 public @Nullable String getSSID() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001378 return mSSID;
1379 }
1380
1381 /**
1382 * Tests if the SSID of this network is the same as the SSID of the passed network.
1383 * @hide
1384 */
paulhud9736de2019-03-08 16:35:20 +08001385 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001386 return Objects.equals(mSSID, nc.mSSID);
1387 }
1388
1389 /**
1390 * Check if the SSID requirements of this object are matched by the passed object.
1391 * @hide
1392 */
paulhud9736de2019-03-08 16:35:20 +08001393 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001394 return mSSID == null || mSSID.equals(nc.mSSID);
1395 }
1396
1397 /**
1398 * Combine SSIDs of the capabilities.
1399 * <p>
1400 * This is only legal if either the SSID of this object is null, or both SSIDs are
1401 * equal.
1402 * @hide
1403 */
paulhud9736de2019-03-08 16:35:20 +08001404 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001405 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1406 throw new IllegalStateException("Can't combine two SSIDs");
1407 }
1408 setSSID(nc.mSSID);
1409 }
1410
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001411 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001412 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1413 * <p>
1414 * Note that this method may break an invariant of having a particular capability in either
1415 * wanted or unwanted lists but never in both. Requests that have the same capability in
1416 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001417 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001418 */
paulhud9736de2019-03-08 16:35:20 +08001419 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001420 combineNetCapabilities(nc);
1421 combineTransportTypes(nc);
1422 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001423 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001424 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001425 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001426 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001427 combineSSIDs(nc);
Roshan Piuse38acab2020-01-16 12:17:17 -08001428 combineRequestor(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001429 }
1430
1431 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001432 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1433 *
1434 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1435 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1436 * bandwidth, signal strength, or validation / captive portal status.
1437 *
1438 * @hide
1439 */
1440 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001441 return (nc != null
1442 && satisfiedByNetCapabilities(nc, onlyImmutable)
1443 && satisfiedByTransportTypes(nc)
1444 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1445 && satisfiedBySpecifier(nc)
1446 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001447 && (onlyImmutable || satisfiedByUids(nc))
Roshan Piuse38acab2020-01-16 12:17:17 -08001448 && (onlyImmutable || satisfiedBySSID(nc)))
1449 && (onlyImmutable || satisfiedByRequestor(nc));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001450 }
1451
1452 /**
1453 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1454 *
1455 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1456 *
1457 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001458 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001459 @TestApi
1460 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001461 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001462 return satisfiedByNetworkCapabilities(nc, false);
1463 }
1464
1465 /**
1466 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1467 *
1468 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1469 *
1470 * @hide
1471 */
paulhud9736de2019-03-08 16:35:20 +08001472 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001473 return satisfiedByNetworkCapabilities(nc, true);
1474 }
1475
1476 /**
1477 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001478 * {@code NetworkCapabilities} and return a String describing any difference.
1479 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001480 *
1481 * @hide
1482 */
paulhud9736de2019-03-08 16:35:20 +08001483 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001484 if (that == null) {
1485 return "other NetworkCapabilities was null";
1486 }
1487
1488 StringJoiner joiner = new StringJoiner(", ");
1489
Hugo Benichieae7a222017-07-25 11:40:56 +09001490 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1491 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001492 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001493 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1494 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1495 if (oldImmutableCapabilities != newImmutableCapabilities) {
1496 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1497 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1498 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1499 }
1500
1501 if (!equalsSpecifier(that)) {
1502 NetworkSpecifier before = this.getNetworkSpecifier();
1503 NetworkSpecifier after = that.getNetworkSpecifier();
1504 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1505 }
1506
1507 if (!equalsTransportTypes(that)) {
1508 String before = transportNamesOf(this.getTransportTypes());
1509 String after = transportNamesOf(that.getTransportTypes());
1510 joiner.add(String.format("transports changed: %s -> %s", before, after));
1511 }
1512
1513 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001514 }
1515
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001516 /**
1517 * Checks that our requestable capabilities are the same as those of the given
1518 * {@code NetworkCapabilities}.
1519 *
1520 * @hide
1521 */
paulhud9736de2019-03-08 16:35:20 +08001522 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001523 if (nc == null) return false;
1524 return (equalsNetCapabilitiesRequestable(nc) &&
1525 equalsTransportTypes(nc) &&
1526 equalsSpecifier(nc));
1527 }
1528
Robert Greenwalt1448f052014-04-08 13:41:39 -07001529 @Override
paulhud9736de2019-03-08 16:35:20 +08001530 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001531 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001532 NetworkCapabilities that = (NetworkCapabilities) obj;
Roshan Piuse38acab2020-01-16 12:17:17 -08001533 return equalsNetCapabilities(that)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001534 && equalsTransportTypes(that)
1535 && equalsLinkBandwidths(that)
1536 && equalsSignalStrength(that)
1537 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001538 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001539 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001540 && equalsSSID(that)
Roshan Piuse38acab2020-01-16 12:17:17 -08001541 && equalsPrivateDnsBroken(that)
1542 && equalsRequestor(that);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001543 }
1544
1545 @Override
1546 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001547 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001548 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001549 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1550 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1551 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1552 + ((int) (mTransportTypes >> 32) * 13)
1553 + (mLinkUpBandwidthKbps * 17)
1554 + (mLinkDownBandwidthKbps * 19)
1555 + Objects.hashCode(mNetworkSpecifier) * 23
1556 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001557 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001558 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001559 + Objects.hashCode(mTransportInfo) * 41
Roshan Piuse38acab2020-01-16 12:17:17 -08001560 + Objects.hashCode(mPrivateDnsBroken) * 43
1561 + Objects.hashCode(mRequestorUid) * 47
1562 + Objects.hashCode(mRequestorPackageName) * 53;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001563 }
1564
Wink Saville4e2dea72014-09-20 11:04:03 -07001565 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001566 public int describeContents() {
1567 return 0;
1568 }
Cody Kesting201fc132020-01-17 11:58:36 -08001569
Wink Saville4e2dea72014-09-20 11:04:03 -07001570 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001571 public void writeToParcel(Parcel dest, int flags) {
1572 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001573 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001574 dest.writeLong(mTransportTypes);
1575 dest.writeInt(mLinkUpBandwidthKbps);
1576 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001577 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001578 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001579 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001580 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001581 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001582 dest.writeBoolean(mPrivateDnsBroken);
Cody Kestingf7ac9962020-03-16 18:15:28 -07001583 dest.writeIntArray(mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -08001584 dest.writeInt(mOwnerUid);
Roshan Piuse38acab2020-01-16 12:17:17 -08001585 dest.writeInt(mRequestorUid);
1586 dest.writeString(mRequestorPackageName);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001587 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001588
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001589 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001590 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001591 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001592 public NetworkCapabilities createFromParcel(Parcel in) {
1593 NetworkCapabilities netCap = new NetworkCapabilities();
1594
1595 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001596 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001597 netCap.mTransportTypes = in.readLong();
1598 netCap.mLinkUpBandwidthKbps = in.readInt();
1599 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001600 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001601 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001602 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001603 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1604 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001605 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001606 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kestingf7ac9962020-03-16 18:15:28 -07001607 netCap.setAdministratorUids(in.createIntArray());
Qingxi Li7cf06622020-01-17 17:54:27 -08001608 netCap.mOwnerUid = in.readInt();
Roshan Piuse38acab2020-01-16 12:17:17 -08001609 netCap.mRequestorUid = in.readInt();
1610 netCap.mRequestorPackageName = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001611 return netCap;
1612 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001613 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001614 public NetworkCapabilities[] newArray(int size) {
1615 return new NetworkCapabilities[size];
1616 }
1617 };
1618
Wink Saville4e2dea72014-09-20 11:04:03 -07001619 @Override
paulhud9736de2019-03-08 16:35:20 +08001620 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001621 final StringBuilder sb = new StringBuilder("[");
1622 if (0 != mTransportTypes) {
1623 sb.append(" Transports: ");
1624 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1625 NetworkCapabilities::transportNameOf, "|");
1626 }
1627 if (0 != mNetworkCapabilities) {
1628 sb.append(" Capabilities: ");
1629 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1630 NetworkCapabilities::capabilityNameOf, "&");
1631 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001632 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001633 sb.append(" Unwanted: ");
1634 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1635 NetworkCapabilities::capabilityNameOf, "&");
1636 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001637 if (mLinkUpBandwidthKbps > 0) {
1638 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1639 }
1640 if (mLinkDownBandwidthKbps > 0) {
1641 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1642 }
1643 if (mNetworkSpecifier != null) {
1644 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1645 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001646 if (mTransportInfo != null) {
1647 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1648 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001649 if (hasSignalStrength()) {
1650 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001651 }
1652
Chalard Jean07ace0f2018-02-26 19:00:45 +09001653 if (null != mUids) {
1654 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1655 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1656 } else {
1657 sb.append(" Uids: <").append(mUids).append(">");
1658 }
1659 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001660 if (mOwnerUid != Process.INVALID_UID) {
1661 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001662 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001663
Cody Kestingf7ac9962020-03-16 18:15:28 -07001664 if (mAdministratorUids.length == 0) {
1665 sb.append(" AdministratorUids: ").append(Arrays.toString(mAdministratorUids));
Cody Kesting201fc132020-01-17 11:58:36 -08001666 }
1667
Chalard Jeanb03a6222018-04-11 21:09:10 +09001668 if (null != mSSID) {
1669 sb.append(" SSID: ").append(mSSID);
1670 }
1671
lucaslin783f2212019-10-22 18:27:33 +08001672 if (mPrivateDnsBroken) {
1673 sb.append(" Private DNS is broken");
1674 }
1675
Roshan Piuse38acab2020-01-16 12:17:17 -08001676 sb.append(" RequestorUid: ").append(mRequestorUid);
1677 sb.append(" RequestorPackageName: ").append(mRequestorPackageName);
1678
Chalard Jean07ace0f2018-02-26 19:00:45 +09001679 sb.append("]");
1680 return sb.toString();
1681 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001682
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001683
Chalard Jean07ace0f2018-02-26 19:00:45 +09001684 private interface NameOf {
1685 String nameOf(int value);
1686 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001687
Chalard Jean07ace0f2018-02-26 19:00:45 +09001688 /**
1689 * @hide
1690 */
paulhud9736de2019-03-08 16:35:20 +08001691 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1692 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001693 int bitPos = 0;
1694 boolean firstElementAdded = false;
1695 while (bitMask != 0) {
1696 if ((bitMask & 1) != 0) {
1697 if (firstElementAdded) {
1698 sb.append(separator);
1699 } else {
1700 firstElementAdded = true;
1701 }
1702 sb.append(nameFetcher.nameOf(bitPos));
1703 }
1704 bitMask >>= 1;
1705 ++bitPos;
1706 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001707 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001708
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001709 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001710 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001711 final long token = proto.start(fieldId);
1712
1713 for (int transport : getTransportTypes()) {
1714 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1715 }
1716
1717 for (int capability : getCapabilities()) {
1718 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1719 }
1720
1721 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1722 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1723
1724 if (mNetworkSpecifier != null) {
1725 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1726 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001727 if (mTransportInfo != null) {
1728 // TODO b/120653863: write transport-specific info to proto?
1729 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001730
1731 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1732 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1733
1734 proto.end(token);
1735 }
1736
Hugo Benichi5df9d722016-04-25 17:16:35 +09001737 /**
1738 * @hide
1739 */
paulhud9736de2019-03-08 16:35:20 +08001740 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001741 StringJoiner joiner = new StringJoiner("|");
1742 if (capabilities != null) {
1743 for (int c : capabilities) {
1744 joiner.add(capabilityNameOf(c));
1745 }
1746 }
1747 return joiner.toString();
1748 }
1749
1750 /**
1751 * @hide
1752 */
paulhud9736de2019-03-08 16:35:20 +08001753 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001754 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001755 case NET_CAPABILITY_MMS: return "MMS";
1756 case NET_CAPABILITY_SUPL: return "SUPL";
1757 case NET_CAPABILITY_DUN: return "DUN";
1758 case NET_CAPABILITY_FOTA: return "FOTA";
1759 case NET_CAPABILITY_IMS: return "IMS";
1760 case NET_CAPABILITY_CBS: return "CBS";
1761 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1762 case NET_CAPABILITY_IA: return "IA";
1763 case NET_CAPABILITY_RCS: return "RCS";
1764 case NET_CAPABILITY_XCAP: return "XCAP";
1765 case NET_CAPABILITY_EIMS: return "EIMS";
1766 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1767 case NET_CAPABILITY_INTERNET: return "INTERNET";
1768 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1769 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1770 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1771 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1772 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1773 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1774 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1775 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1776 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1777 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1778 case NET_CAPABILITY_MCX: return "MCX";
1779 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1780 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001781 }
1782 }
1783
1784 /**
1785 * @hide
1786 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001787 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001788 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001789 StringJoiner joiner = new StringJoiner("|");
1790 if (types != null) {
1791 for (int t : types) {
1792 joiner.add(transportNameOf(t));
1793 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001794 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001795 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001796 }
1797
1798 /**
1799 * @hide
1800 */
paulhud9736de2019-03-08 16:35:20 +08001801 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001802 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001803 return "UNKNOWN";
1804 }
1805 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001806 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001807
Jeff Sharkeyde570312017-10-24 21:25:50 -06001808 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001809 Preconditions.checkArgument(
1810 isValidTransport(transport), "Invalid TransportType " + transport);
1811 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001812
1813 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1814 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1815 }
1816
1817 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1818 Preconditions.checkArgument(isValidCapability(capability),
1819 "NetworkCapability " + capability + "out of range");
1820 }
junyulai05986c62018-08-07 19:50:45 +08001821
1822 /**
1823 * Check if this {@code NetworkCapability} instance is metered.
1824 *
1825 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1826 * @hide
1827 */
1828 public boolean isMetered() {
1829 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1830 }
lucaslin783f2212019-10-22 18:27:33 +08001831
1832 /**
1833 * Check if private dns is broken.
1834 *
1835 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1836 * @hide
1837 */
1838 public boolean isPrivateDnsBroken() {
1839 return mPrivateDnsBroken;
1840 }
1841
1842 /**
1843 * Set mPrivateDnsBroken to true when private dns is broken.
1844 *
1845 * @param broken the status of private DNS to be set.
1846 * @hide
1847 */
1848 public void setPrivateDnsBroken(boolean broken) {
1849 mPrivateDnsBroken = broken;
1850 }
1851
1852 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1853 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1854 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001855
1856 /**
1857 * Set the uid of the app making the request.
1858 *
1859 * Note: This works only for {@link NetworkAgent} instances. Any capabilities passed in
1860 * via the public {@link ConnectivityManager} API's will have this field overwritten.
1861 *
1862 * @param uid UID of the app.
1863 * @hide
1864 */
1865 @SystemApi
1866 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1867 mRequestorUid = uid;
1868 return this;
1869 }
1870
1871 /**
1872 * @return the uid of the app making the request.
1873 *
1874 * Note: This could return {@link Process#INVALID_UID} if the {@link NetworkRequest}
1875 * object was not obtained from {@link ConnectivityManager}.
1876 * @hide
1877 */
1878 public int getRequestorUid() {
1879 return mRequestorUid;
1880 }
1881
1882 /**
1883 * Set the package name of the app making the request.
1884 *
1885 * Note: This works only for {@link NetworkAgent} instances. Any capabilities passed in
1886 * via the public {@link ConnectivityManager} API's will have this field overwritten.
1887 *
1888 * @param packageName package name of the app.
1889 * @hide
1890 */
1891 @SystemApi
1892 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
1893 mRequestorPackageName = packageName;
1894 return this;
1895 }
1896
1897 /**
1898 * @return the package name of the app making the request.
1899 *
1900 * Note: This could return {@code null} if the {@link NetworkRequest} object was not obtained
1901 * from {@link ConnectivityManager}.
1902 * @hide
1903 */
1904 @Nullable
1905 public String getRequestorPackageName() {
1906 return mRequestorPackageName;
1907 }
1908
1909 /**
1910 * Set the uid and package name of the app making the request.
1911 *
1912 * Note: This is intended to be only invoked from within connectivitiy service.
1913 *
1914 * @param uid UID of the app.
1915 * @param packageName package name of the app.
1916 * @hide
1917 */
1918 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
1919 int uid, @NonNull String packageName) {
1920 return setRequestorUid(uid).setRequestorPackageName(packageName);
1921 }
1922
1923 /**
1924 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
1925 * capabilities.
1926 *
1927 * This method is called on the NetworkCapabilities embedded in a request with the
1928 * capabilities of an available network. If the available network, sets a specific
1929 * requestor (by uid and optionally package name), then this will only match a request from the
1930 * same app. If either of the capabilities have an unset uid or package name, then it matches
1931 * everything.
1932 * <p>
1933 * nc is assumed nonnull. Else, NPE.
1934 */
1935 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
1936 // No uid set, matches everything.
1937 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
1938 return true;
1939 }
1940 // uids don't match.
1941 if (mRequestorUid != nc.mRequestorUid) return false;
1942 // No package names set, matches everything
1943 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
1944 // check for package name match.
1945 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
1946 }
1947
1948 /**
1949 * Combine requestor info of the capabilities.
1950 * <p>
1951 * This is only legal if either the requestor info of this object is reset, or both info are
1952 * equal.
1953 * nc is assumed nonnull.
1954 */
1955 private void combineRequestor(@NonNull NetworkCapabilities nc) {
1956 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
1957 throw new IllegalStateException("Can't combine two uids");
1958 }
1959 if (mRequestorPackageName != null
1960 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
1961 throw new IllegalStateException("Can't combine two package names");
1962 }
1963 setRequestorUid(nc.mRequestorUid);
1964 setRequestorPackageName(nc.mRequestorPackageName);
1965 }
1966
1967 private boolean equalsRequestor(NetworkCapabilities nc) {
1968 return mRequestorUid == nc.mRequestorUid
1969 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
1970 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001971}