blob: 05d7860ff891a6d465d4f7910fe3865d6bc27ebe [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;
Chalard Jeane5e38502020-03-18 15:58:50 +090022import android.annotation.RequiresPermission;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070023import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060024import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000025import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060026import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010027import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070028import android.os.Parcel;
29import android.os.Parcelable;
Qingxi Li7cf06622020-01-17 17:54:27 -080030import android.os.Process;
Roshan Piuse38acab2020-01-16 12:17:17 -080031import android.text.TextUtils;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090032import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080033import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070034
35import com.android.internal.annotations.VisibleForTesting;
Chalard Jeane5e38502020-03-18 15:58:50 +090036import com.android.internal.util.ArrayUtils;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090037import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090038import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070039
Jeff Sharkeyde570312017-10-24 21:25:50 -060040import java.lang.annotation.Retention;
41import java.lang.annotation.RetentionPolicy;
Cody Kestingf7ac9962020-03-16 18:15:28 -070042import java.util.Arrays;
Etan Cohena7434272017-04-03 12:17:51 -070043import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090044import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090045import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070046
47/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070048 * Representation of the capabilities of an active network. Instances are
49 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060050 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
51 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060052 * <p>
53 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
54 * network selection. Rather than indicate a need for Wi-Fi because an
55 * application needs high bandwidth and risk obsolescence when a new, fast
56 * network appears (like LTE), the application should specify it needs high
57 * bandwidth. Similarly if an application needs an unmetered network for a bulk
58 * transfer it can specify that rather than assuming all cellular based
59 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070060 */
61public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070062 private static final String TAG = "NetworkCapabilities";
63
lucaslin783f2212019-10-22 18:27:33 +080064 // Set to true when private DNS is broken.
65 private boolean mPrivateDnsBroken;
66
Roshan Piuse38acab2020-01-16 12:17:17 -080067 /**
68 * Uid of the app making the request.
69 */
70 private int mRequestorUid;
71
72 /**
73 * Package name of the app making the request.
74 */
75 private String mRequestorPackageName;
76
Robert Greenwalt01d004e2014-05-18 15:24:21 -070077 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090078 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090079 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070080 }
81
82 public NetworkCapabilities(NetworkCapabilities nc) {
83 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090084 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070085 }
86 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070087
88 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090089 * Completely clears the contents of this object, removing even the capabilities that are set
90 * by default when the object is constructed.
Chalard Jeane5e38502020-03-18 15:58:50 +090091 * @hide
Lorenzo Colittif7058f52015-04-27 11:31:55 +090092 */
93 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080094 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070095 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090096 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080097 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090098 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090099 mUids = null;
Cody Kestingf7ac9962020-03-16 18:15:28 -0700100 mAdministratorUids = new int[0];
Qingxi Li7cf06622020-01-17 17:54:27 -0800101 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +0900102 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +0800103 mPrivateDnsBroken = false;
Roshan Piuse38acab2020-01-16 12:17:17 -0800104 mRequestorUid = Process.INVALID_UID;
105 mRequestorPackageName = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900106 }
107
108 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +0900109 * Set all contents of this object to the contents of a NetworkCapabilities.
110 * @hide
111 */
paulhud9736de2019-03-08 16:35:20 +0800112 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900113 mNetworkCapabilities = nc.mNetworkCapabilities;
114 mTransportTypes = nc.mTransportTypes;
115 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
116 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
117 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800118 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900119 mSignalStrength = nc.mSignalStrength;
120 setUids(nc.mUids); // Will make the defensive copy
Cody Kesting201fc132020-01-17 11:58:36 -0800121 setAdministratorUids(nc.mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -0800122 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900123 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
124 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800125 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Roshan Piuse38acab2020-01-16 12:17:17 -0800126 mRequestorUid = nc.mRequestorUid;
127 mRequestorPackageName = nc.mRequestorPackageName;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900128 }
129
130 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700131 * Represents the network's capabilities. If any are specified they will be satisfied
132 * by any Network that matches all of them.
133 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100134 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900135 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700136
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800137 /**
138 * If any capabilities specified here they must not exist in the matching Network.
139 */
140 private long mUnwantedNetworkCapabilities;
141
Jeff Sharkeyde570312017-10-24 21:25:50 -0600142 /** @hide */
143 @Retention(RetentionPolicy.SOURCE)
144 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
145 NET_CAPABILITY_MMS,
146 NET_CAPABILITY_SUPL,
147 NET_CAPABILITY_DUN,
148 NET_CAPABILITY_FOTA,
149 NET_CAPABILITY_IMS,
150 NET_CAPABILITY_CBS,
151 NET_CAPABILITY_WIFI_P2P,
152 NET_CAPABILITY_IA,
153 NET_CAPABILITY_RCS,
154 NET_CAPABILITY_XCAP,
155 NET_CAPABILITY_EIMS,
156 NET_CAPABILITY_NOT_METERED,
157 NET_CAPABILITY_INTERNET,
158 NET_CAPABILITY_NOT_RESTRICTED,
159 NET_CAPABILITY_TRUSTED,
160 NET_CAPABILITY_NOT_VPN,
161 NET_CAPABILITY_VALIDATED,
162 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600163 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600164 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900165 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900166 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800167 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800168 NET_CAPABILITY_MCX,
169 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600170 })
171 public @interface NetCapability { }
172
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174 * Indicates this is a network that has the ability to reach the
175 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 */
177 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178
179 /**
180 * Indicates this is a network that has the ability to reach the carrier's
181 * SUPL server, used to retrieve GPS information.
182 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700183 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700184
185 /**
186 * Indicates this is a network that has the ability to reach the carrier's
187 * DUN or tethering gateway.
188 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700189 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190
191 /**
192 * Indicates this is a network that has the ability to reach the carrier's
193 * FOTA portal, used for over the air updates.
194 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700195 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700196
197 /**
198 * Indicates this is a network that has the ability to reach the carrier's
199 * IMS servers, used for network registration and signaling.
200 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700201 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700202
203 /**
204 * Indicates this is a network that has the ability to reach the carrier's
205 * CBS servers, used for carrier specific services.
206 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700207 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700208
209 /**
210 * Indicates this is a network that has the ability to reach a Wi-Fi direct
211 * peer.
212 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700213 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700214
215 /**
216 * Indicates this is a network that has the ability to reach a carrier's
217 * Initial Attach servers.
218 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700219 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700220
221 /**
222 * Indicates this is a network that has the ability to reach a carrier's
223 * RCS servers, used for Rich Communication Services.
224 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700225 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700226
227 /**
228 * Indicates this is a network that has the ability to reach a carrier's
229 * XCAP servers, used for configuration and control.
230 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700231 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700232
233 /**
234 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700235 * Emergency IMS servers or other services, used for network signaling
236 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700237 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700238 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700239
240 /**
241 * Indicates that this network is unmetered.
242 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700243 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700244
245 /**
246 * Indicates that this network should be able to reach the internet.
247 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700248 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700249
250 /**
251 * Indicates that this network is available for general use. If this is not set
252 * applications should not attempt to communicate on this network. Note that this
253 * is simply informative and not enforcement - enforcement is handled via other means.
254 * Set by default.
255 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700256 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
257
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700258 /**
259 * Indicates that the user has indicated implicit trust of this network. This
260 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
261 * BT device or a wifi the user asked to connect to. Untrusted networks
262 * are probably limited to unknown wifi AP. Set by default.
263 */
264 public static final int NET_CAPABILITY_TRUSTED = 14;
265
Paul Jensen76b610a2015-03-18 09:33:07 -0400266 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400267 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400268 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400269 */
270 public static final int NET_CAPABILITY_NOT_VPN = 15;
271
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900272 /**
273 * Indicates that connectivity on this network was successfully validated. For example, for a
274 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
275 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900276 */
277 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700278
Paul Jensen3d194ea2015-06-16 14:27:36 -0400279 /**
280 * Indicates that this network was found to have a captive portal in place last time it was
281 * probed.
282 */
283 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
284
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900285 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600286 * Indicates that this network is not roaming.
287 */
288 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
289
290 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900291 * Indicates that this network is available for use by apps, and not a network that is being
292 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900293 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600294 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900295
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900296 /**
297 * Indicates that this network is not congested.
298 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600299 * When a network is congested, applications should defer network traffic
300 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900301 */
302 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
303
Chalard Jean804b8fb2018-01-30 22:41:41 +0900304 /**
305 * Indicates that this network is not currently suspended.
306 * <p>
307 * When a network is suspended, the network's IP addresses and any connections
308 * established on the network remain valid, but the network is temporarily unable
309 * to transfer data. This can happen, for example, if a cellular network experiences
310 * a temporary loss of signal, such as when driving through a tunnel, etc.
311 * A network with this capability is not suspended, so is expected to be able to
312 * transfer data.
313 */
314 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
315
Pavel Maltsev43403202018-01-30 17:19:44 -0800316 /**
317 * Indicates that traffic that goes through this network is paid by oem. For example,
318 * this network can be used by system apps to upload telemetry data.
319 * @hide
320 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700321 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800322 public static final int NET_CAPABILITY_OEM_PAID = 22;
323
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800324 /**
325 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
326 * servers.
327 */
328 public static final int NET_CAPABILITY_MCX = 23;
329
lucasline252a742019-03-12 13:08:03 +0800330 /**
331 * Indicates that this network was tested to only provide partial connectivity.
332 * @hide
333 */
334 @SystemApi
335 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
336
Robert Greenwalt1448f052014-04-08 13:41:39 -0700337 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800338 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700340 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900341 * Network capabilities that are expected to be mutable, i.e., can change while a particular
342 * network is connected.
343 */
344 private static final long MUTABLE_CAPABILITIES =
345 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
346 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900347 (1 << NET_CAPABILITY_TRUSTED)
348 | (1 << NET_CAPABILITY_VALIDATED)
349 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
350 | (1 << NET_CAPABILITY_NOT_ROAMING)
351 | (1 << NET_CAPABILITY_FOREGROUND)
352 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800353 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
354 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900355
356 /**
357 * Network capabilities that are not allowed in NetworkRequests. This exists because the
358 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
359 * capability's presence cannot be known in advance. If such a capability is requested, then we
360 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
361 * get immediately torn down because they do not have the requested capability.
362 */
363 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900364 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900365
366 /**
367 * Capabilities that are set by default when the object is constructed.
368 */
369 private static final long DEFAULT_CAPABILITIES =
370 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
371 (1 << NET_CAPABILITY_TRUSTED) |
372 (1 << NET_CAPABILITY_NOT_VPN);
373
374 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400375 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800376 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400377 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700378 @VisibleForTesting
379 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400380 (1 << NET_CAPABILITY_CBS) |
381 (1 << NET_CAPABILITY_DUN) |
382 (1 << NET_CAPABILITY_EIMS) |
383 (1 << NET_CAPABILITY_FOTA) |
384 (1 << NET_CAPABILITY_IA) |
385 (1 << NET_CAPABILITY_IMS) |
386 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800387 (1 << NET_CAPABILITY_XCAP) |
388 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800389
390 /**
391 * Capabilities that force network to be restricted.
392 * {@see #maybeMarkCapabilitiesRestricted}.
393 */
394 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800395 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400396
397 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700398 * Capabilities that suggest that a network is unrestricted.
399 * {@see #maybeMarkCapabilitiesRestricted}.
400 */
401 @VisibleForTesting
402 /* package */ static final long UNRESTRICTED_CAPABILITIES =
403 (1 << NET_CAPABILITY_INTERNET) |
404 (1 << NET_CAPABILITY_MMS) |
405 (1 << NET_CAPABILITY_SUPL) |
406 (1 << NET_CAPABILITY_WIFI_P2P);
407
408 /**
lucasline252a742019-03-12 13:08:03 +0800409 * Capabilities that are managed by ConnectivityService.
410 */
411 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
412 (1 << NET_CAPABILITY_VALIDATED)
413 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
414 | (1 << NET_CAPABILITY_FOREGROUND)
415 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
416
417 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700418 * Adds the given capability to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900419 * Note that when searching for a network to satisfy a request, all capabilities
420 * requested must be satisfied.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700421 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600422 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900423 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900424 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700425 */
paulhud9736de2019-03-08 16:35:20 +0800426 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800427 // If the given capability was previously added to the list of unwanted capabilities
428 // then the capability will also be removed from the list of unwanted capabilities.
429 // TODO: Consider adding unwanted capabilities to the public API and mention this
430 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800431 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700432 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800433 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700434 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700435 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700436
437 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800438 * Adds the given capability to the list of unwanted capabilities of this
Chalard Jeane5e38502020-03-18 15:58:50 +0900439 * {@code NetworkCapability} instance. Note that when searching for a network to
440 * satisfy a request, the network must not contain any capability from unwanted capability
441 * list.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800442 * <p>
443 * If the capability was previously added to the list of required capabilities (for
444 * example, it was there by default or added using {@link #addCapability(int)} method), then
445 * it will be removed from the list of required capabilities as well.
446 *
447 * @see #addCapability(int)
448 * @hide
449 */
450 public void addUnwantedCapability(@NetCapability int capability) {
451 checkValidCapability(capability);
452 mUnwantedNetworkCapabilities |= 1 << capability;
453 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
454 }
455
456 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700457 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
458 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600459 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900460 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900461 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700462 */
paulhud9736de2019-03-08 16:35:20 +0800463 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800464 // Note that this method removes capabilities that were added via addCapability(int),
465 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800466 checkValidCapability(capability);
467 final long mask = ~(1 << capability);
468 mNetworkCapabilities &= mask;
469 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700470 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700471 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700472
473 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600474 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
475 * instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900476 * @hide
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600477 */
paulhud9736de2019-03-08 16:35:20 +0800478 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
479 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600480 if (value) {
481 addCapability(capability);
482 } else {
483 removeCapability(capability);
484 }
485 return this;
486 }
487
488 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700489 * Gets all the capabilities set on this {@code NetworkCapability} instance.
490 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600491 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700492 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700493 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000494 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600495 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600496 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900497 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700498 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700499
500 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800501 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
502 *
503 * @return an array of unwanted capability values for this instance.
504 * @hide
505 */
506 public @NetCapability int[] getUnwantedCapabilities() {
507 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
508 }
509
510
511 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600512 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700513 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600514 *
515 * @hide
516 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800517 public void setCapabilities(@NetCapability int[] capabilities,
518 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600519 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800520 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600521 }
522
523 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800524 * @deprecated use {@link #setCapabilities(int[], int[])}
525 * @hide
526 */
527 @Deprecated
528 public void setCapabilities(@NetCapability int[] capabilities) {
529 setCapabilities(capabilities, new int[] {});
530 }
531
532 /**
533 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700534 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600535 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700536 * @return {@code true} if set on this instance.
537 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600538 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800539 return isValidCapability(capability)
540 && ((mNetworkCapabilities & (1 << capability)) != 0);
541 }
542
543 /** @hide */
544 public boolean hasUnwantedCapability(@NetCapability int capability) {
545 return isValidCapability(capability)
546 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700547 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700548
lucasline252a742019-03-12 13:08:03 +0800549 /**
550 * Check if this NetworkCapabilities has system managed capabilities or not.
551 * @hide
552 */
553 public boolean hasConnectivityManagedCapability() {
554 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
555 }
556
Pavel Maltseve18ef262018-03-07 11:13:04 -0800557 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800558 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700559 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800560 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700561 }
562
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900563 /**
564 * Convenience function that returns a human-readable description of the first mutable
565 * capability we find. Used to present an error message to apps that request mutable
566 * capabilities.
567 *
568 * @hide
569 */
paulhud9736de2019-03-08 16:35:20 +0800570 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800571 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
572 & NON_REQUESTABLE_CAPABILITIES;
573
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900574 if (nonRequestable != 0) {
575 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900576 }
577 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900578 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800579 if (isPrivateDnsBroken()) {
580 return "privateDnsBroken";
581 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900582 return null;
583 }
584
paulhud9736de2019-03-08 16:35:20 +0800585 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
586 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800587 long requestedCapabilities = mNetworkCapabilities;
588 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
589 long providedCapabilities = nc.mNetworkCapabilities;
590
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900591 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800592 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
593 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900594 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800595 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
596 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700597 }
598
Robert Greenwalt06314e42014-10-29 14:04:06 -0700599 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800600 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800601 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
602 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700603 }
604
paulhud9736de2019-03-08 16:35:20 +0800605 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900606 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800607 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
608 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
609 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900610 }
611
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 /**
paulhu18354322020-01-09 17:08:11 +0800613 * Deduces that all the capabilities it provides are typically provided by restricted networks
614 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400615 *
paulhu18354322020-01-09 17:08:11 +0800616 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400617 * @hide
618 */
paulhu18354322020-01-09 17:08:11 +0800619 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800620 // Check if we have any capability that forces the network to be restricted.
621 final boolean forceRestrictedCapability =
622 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
623
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700624 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800625 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700626 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800627 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700628
629 // Must have at least some restricted capabilities.
630 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800631 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700632
paulhu18354322020-01-09 17:08:11 +0800633 return forceRestrictedCapability
634 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
635 }
636
637 /**
638 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
639 *
640 * @hide
641 */
642 public void maybeMarkCapabilitiesRestricted() {
643 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400644 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400645 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400646 }
647
648 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700649 * Representing the transport type. Apps should generally not care about transport. A
650 * request for a fast internet connection could be satisfied by a number of different
651 * transports. If any are specified here it will be satisfied a Network that matches
652 * any of them. If a caller doesn't care about the transport it should not specify any.
653 */
654 private long mTransportTypes;
655
Jeff Sharkeyde570312017-10-24 21:25:50 -0600656 /** @hide */
657 @Retention(RetentionPolicy.SOURCE)
658 @IntDef(prefix = { "TRANSPORT_" }, value = {
659 TRANSPORT_CELLULAR,
660 TRANSPORT_WIFI,
661 TRANSPORT_BLUETOOTH,
662 TRANSPORT_ETHERNET,
663 TRANSPORT_VPN,
664 TRANSPORT_WIFI_AWARE,
665 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800666 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600667 })
668 public @interface Transport { }
669
Robert Greenwalt1448f052014-04-08 13:41:39 -0700670 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700671 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700672 */
673 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700674
675 /**
676 * Indicates this network uses a Wi-Fi transport.
677 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700678 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700679
680 /**
681 * Indicates this network uses a Bluetooth transport.
682 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700683 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700684
685 /**
686 * Indicates this network uses an Ethernet transport.
687 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700688 public static final int TRANSPORT_ETHERNET = 3;
689
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400690 /**
691 * Indicates this network uses a VPN transport.
692 */
693 public static final int TRANSPORT_VPN = 4;
694
Etan Cohen305ea282016-06-20 09:27:12 -0700695 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700696 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700697 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700698 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700699
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700700 /**
701 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700702 */
703 public static final int TRANSPORT_LOWPAN = 6;
704
Benedict Wong89ce5e32018-11-14 17:40:55 -0800705 /**
706 * Indicates this network uses a Test-only virtual interface as a transport.
707 *
708 * @hide
709 */
710 @TestApi
711 public static final int TRANSPORT_TEST = 7;
712
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900713 /** @hide */
714 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
715 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800716 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700717
Hugo Benichi16f0a942017-06-20 14:07:59 +0900718 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600719 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900720 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
721 }
722
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900723 private static final String[] TRANSPORT_NAMES = {
724 "CELLULAR",
725 "WIFI",
726 "BLUETOOTH",
727 "ETHERNET",
728 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700729 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800730 "LOWPAN",
731 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900732 };
733
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700734 /**
735 * Adds the given transport type to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900736 * Multiple transports may be applied. Note that when searching
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700737 * for a network to satisfy a request, any listed in the request will satisfy the request.
738 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
739 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
740 * to be selected. This is logically different than
741 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
742 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600743 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900744 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900745 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700746 */
paulhud9736de2019-03-08 16:35:20 +0800747 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900748 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700749 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700750 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700751 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700752 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700753
754 /**
755 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
756 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600757 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900758 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700759 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700760 */
paulhud9736de2019-03-08 16:35:20 +0800761 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900762 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700763 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700764 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700765 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700766 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700767
768 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600769 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
770 * instance.
771 *
772 * @hide
773 */
paulhud9736de2019-03-08 16:35:20 +0800774 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
775 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600776 if (value) {
777 addTransportType(transportType);
778 } else {
779 removeTransportType(transportType);
780 }
781 return this;
782 }
783
784 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700785 * Gets all the transports set on this {@code NetworkCapability} instance.
786 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600787 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700788 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700789 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600790 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900791 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800792 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900793 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700794 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700795
796 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600797 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700798 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600799 *
800 * @hide
801 */
802 public void setTransportTypes(@Transport int[] transportTypes) {
803 mTransportTypes = BitUtils.packBits(transportTypes);
804 }
805
806 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700807 * Tests for the presence of a transport on this instance.
808 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600809 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700810 * @return {@code true} if set on this instance.
811 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600812 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900813 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700814 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700815
816 private void combineTransportTypes(NetworkCapabilities nc) {
817 this.mTransportTypes |= nc.mTransportTypes;
818 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900819
Robert Greenwalt1448f052014-04-08 13:41:39 -0700820 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
821 return ((this.mTransportTypes == 0) ||
822 ((this.mTransportTypes & nc.mTransportTypes) != 0));
823 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900824
Robert Greenwalt06314e42014-10-29 14:04:06 -0700825 /** @hide */
826 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 return (nc.mTransportTypes == this.mTransportTypes);
828 }
829
830 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800831 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900832 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800833 * <p>This field keeps track of the UID of the app that created this network and is in charge of
834 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
835 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800836 *
837 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
838 * reset to Process.INVALID_UID unless all the following conditions are met:
839 *
840 * <ol>
841 * <li>The destination app is the network owner
842 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
843 * <li>The user's location toggle is on
844 * </ol>
845 *
846 * This is because the owner UID is location-sensitive. The apps that request a network could
847 * know where the device is if they can tell for sure the system has connected to the network
848 * they requested.
849 *
850 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
851 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
852 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900853 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800854 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900855
856 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800857 * Set the UID of the owner app.
Chalard Jeane5e38502020-03-18 15:58:50 +0900858 * @hide
Chalard Jeanf474fc32018-01-17 15:10:05 +0900859 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800860 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800861 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800862 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900863 }
864
Qingxi Li7cf06622020-01-17 17:54:27 -0800865 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800866 * Retrieves the UID of the app that owns this network.
867 *
868 * <p>For user privacy reasons, this field will only be populated if:
869 *
870 * <ol>
871 * <li>The calling app is the network owner
872 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
873 * <li>The user's location toggle is on
874 * </ol>
875 *
Chalard Jeane5e38502020-03-18 15:58:50 +0900876 * Instances of NetworkCapabilities sent to apps without the appropriate permissions will
877 * have this field cleared out.
Qingxi Li7cf06622020-01-17 17:54:27 -0800878 */
879 public int getOwnerUid() {
880 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000881 }
882
Chalard Jeanf474fc32018-01-17 15:10:05 +0900883 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800884 * UIDs of packages that are administrators of this network, or empty if none.
885 *
886 * <p>This field tracks the UIDs of packages that have permission to manage this network.
887 *
888 * <p>Network owners will also be listed as administrators.
889 *
890 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
891 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
892 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
893 */
Cody Kestingf7ac9962020-03-16 18:15:28 -0700894 private int[] mAdministratorUids = new int[0];
Cody Kesting201fc132020-01-17 11:58:36 -0800895
896 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700897 * Sets the int[] of UIDs that are administrators of this network.
Cody Kesting201fc132020-01-17 11:58:36 -0800898 *
899 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
900 * Examples of UIDs that should be included in administratorUids are:
901 * <ul>
902 * <li>Carrier apps with privileges for the relevant subscription
903 * <li>Active VPN apps
904 * <li>Other application groups with a particular Network-related role
905 * </ul>
906 *
907 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
908 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800909 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
910 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800911 *
912 * @param administratorUids the UIDs to be set as administrators of this Network.
913 * @hide
914 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800915 @NonNull
Cody Kestingf7ac9962020-03-16 18:15:28 -0700916 public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) {
917 mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length);
Roshan Piuse38acab2020-01-16 12:17:17 -0800918 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800919 }
920
921 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700922 * Retrieves the UIDs that are administrators of this Network.
Cody Kesting201fc132020-01-17 11:58:36 -0800923 *
Cody Kestingf7ac9962020-03-16 18:15:28 -0700924 * @return the int[] of UIDs that are administrators of this Network
Cody Kesting201fc132020-01-17 11:58:36 -0800925 * @hide
926 */
927 @NonNull
928 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +0900929 @TestApi
Cody Kestingf7ac9962020-03-16 18:15:28 -0700930 public int[] getAdministratorUids() {
931 return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
Cody Kesting201fc132020-01-17 11:58:36 -0800932 }
933
934 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600935 * Value indicating that link bandwidth is unspecified.
936 * @hide
937 */
938 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
939
940 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700941 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
942 * for the first hop on the given transport. It is not measured, but may take into account
943 * link parameters (Radio technology, allocated channels, etc).
944 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600945 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
946 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700947
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700948 /**
949 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
950 * the estimated first hop transport bandwidth.
951 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +0900952 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700953 *
954 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +0900955 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700956 */
paulhud9736de2019-03-08 16:35:20 +0800957 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700958 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600959 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700960 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700961
962 /**
963 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
964 * the estimated first hop transport bandwidth.
965 *
966 * @return The estimated first hop upstream (device to network) bandwidth.
967 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700968 public int getLinkUpstreamBandwidthKbps() {
969 return mLinkUpBandwidthKbps;
970 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700971
972 /**
973 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
974 * the estimated first hop transport bandwidth.
975 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +0900976 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700977 *
978 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +0900979 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700980 */
paulhud9736de2019-03-08 16:35:20 +0800981 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700982 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600983 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700984 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700985
986 /**
987 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
988 * the estimated first hop transport bandwidth.
989 *
990 * @return The estimated first hop downstream (network to device) bandwidth.
991 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700992 public int getLinkDownstreamBandwidthKbps() {
993 return mLinkDownBandwidthKbps;
994 }
995
996 private void combineLinkBandwidths(NetworkCapabilities nc) {
997 this.mLinkUpBandwidthKbps =
998 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
999 this.mLinkDownBandwidthKbps =
1000 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1001 }
1002 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1003 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1004 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1005 }
1006 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1007 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1008 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1009 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001010 /** @hide */
1011 public static int minBandwidth(int a, int b) {
1012 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1013 return b;
1014 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1015 return a;
1016 } else {
1017 return Math.min(a, b);
1018 }
1019 }
1020 /** @hide */
1021 public static int maxBandwidth(int a, int b) {
1022 return Math.max(a, b);
1023 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001024
Etan Cohena7434272017-04-03 12:17:51 -07001025 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001026 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001027
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001028 /**
1029 * Sets the optional bearer specific network specifier.
1030 * This has no meaning if a single transport is also not specified, so calling
1031 * this without a single transport set will generate an exception, as will
1032 * subsequently adding or removing transports after this is set.
1033 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001034 *
Etan Cohena7434272017-04-03 12:17:51 -07001035 * @param networkSpecifier A concrete, parcelable framework class that extends
1036 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001037 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +09001038 * @hide
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 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001061 mTransportInfo = transportInfo;
1062 return this;
1063 }
1064
1065 /**
paulhud9736de2019-03-08 16:35:20 +08001066 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001067 *
Etan Cohena7434272017-04-03 12:17:51 -07001068 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
Chalard Jeane5e38502020-03-18 15:58:50 +09001069 * specifier or {@code null}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001070 */
paulhud9736de2019-03-08 16:35:20 +08001071 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001072 return mNetworkSpecifier;
1073 }
1074
Etan Cohenca9fb562018-11-27 07:32:39 -08001075 /**
1076 * Returns a transport-specific information container. The application may cast this
1077 * container to a concrete sub-class based on its knowledge of the network request. The
1078 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001079 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001080 *
1081 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1082 * available for the network.
1083 */
1084 @Nullable public TransportInfo getTransportInfo() {
1085 return mTransportInfo;
1086 }
1087
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001088 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001089 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001090 throw new IllegalStateException("Can't combine two networkSpecifiers");
1091 }
Etan Cohena7434272017-04-03 12:17:51 -07001092 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001093 }
Etan Cohena7434272017-04-03 12:17:51 -07001094
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001095 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001096 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
1097 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001098 }
Etan Cohena7434272017-04-03 12:17:51 -07001099
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001100 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001101 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001102 }
1103
Etan Cohenca9fb562018-11-27 07:32:39 -08001104 private void combineTransportInfos(NetworkCapabilities nc) {
1105 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1106 throw new IllegalStateException("Can't combine two TransportInfos");
1107 }
1108 setTransportInfo(nc.mTransportInfo);
1109 }
1110
1111 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1112 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1113 }
1114
Robert Greenwalt1448f052014-04-08 13:41:39 -07001115 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001116 * Magic value that indicates no signal strength provided. A request specifying this value is
1117 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001118 */
1119 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1120
1121 /**
1122 * Signal strength. This is a signed integer, and higher values indicate better signal.
1123 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1124 */
paulhud9736de2019-03-08 16:35:20 +08001125 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001126 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001127
1128 /**
1129 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1130 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001131 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001132 * <p>
1133 * Note that when used to register a network callback, this specifies the minimum acceptable
1134 * signal strength. When received as the state of an existing network it specifies the current
1135 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1136 * effect when requesting a callback.
1137 *
1138 * @param signalStrength the bearer-specific signal strength.
Chalard Jeane5e38502020-03-18 15:58:50 +09001139 * @hide
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 */
paulhud9736de2019-03-08 16:35:20 +08001366 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001367 mSSID = ssid;
1368 return this;
1369 }
1370
1371 /**
1372 * Gets the SSID of this network, or null if none or unknown.
1373 * @hide
1374 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001375 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +09001376 @TestApi
1377 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 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001857 * Set the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001858 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001859 * For instances of NetworkCapabilities representing a request, sets the
1860 * UID of the app making the request. For a network created by the system,
1861 * sets the UID of the only app whose requests can match this network.
1862 * This can be set to {@link Process#INVALID_UID} if there is no such app,
1863 * or if this instance of NetworkCapabilities is about to be sent to a
1864 * party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001865 *
1866 * @param uid UID of the app.
1867 * @hide
1868 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001869 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1870 mRequestorUid = uid;
1871 return this;
1872 }
1873
1874 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001875 * Returns the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001876 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001877 * For a NetworkRequest being made by an app, contains the app's UID. For a network
1878 * created by the system, contains the UID of the only app whose requests can match
1879 * this network, or {@link Process#INVALID_UID} if none or if the
1880 * caller does not have permission to learn about this.
1881 *
1882 * @return the uid of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001883 * @hide
1884 */
1885 public int getRequestorUid() {
1886 return mRequestorUid;
1887 }
1888
1889 /**
1890 * Set the package name of the app making the request.
1891 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001892 * For instances of NetworkCapabilities representing a request, sets the
1893 * package name of the app making the request. For a network created by the system,
1894 * sets the package name of the only app whose requests can match this network.
1895 * This can be set to null if there is no such app, or if this instance of
1896 * NetworkCapabilities is about to be sent to a party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001897 *
1898 * @param packageName package name of the app.
1899 * @hide
1900 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001901 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
1902 mRequestorPackageName = packageName;
1903 return this;
1904 }
1905
1906 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001907 * Returns the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001908 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001909 * For a NetworkRequest being made by an app, contains the app's package name. For a
1910 * network created by the system, contains the package name of the only app whose
1911 * requests can match this network, or null if none or if the caller does not have
1912 * permission to learn about this.
1913 *
1914 * @return the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001915 * @hide
1916 */
1917 @Nullable
1918 public String getRequestorPackageName() {
1919 return mRequestorPackageName;
1920 }
1921
1922 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001923 * Set the uid and package name of the app causing this network to exist.
Roshan Piuse38acab2020-01-16 12:17:17 -08001924 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001925 * {@see #setRequestorUid} and {@link #setRequestorPackageName}
Roshan Piuse38acab2020-01-16 12:17:17 -08001926 *
1927 * @param uid UID of the app.
1928 * @param packageName package name of the app.
1929 * @hide
1930 */
1931 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
1932 int uid, @NonNull String packageName) {
1933 return setRequestorUid(uid).setRequestorPackageName(packageName);
1934 }
1935
1936 /**
1937 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
1938 * capabilities.
1939 *
1940 * This method is called on the NetworkCapabilities embedded in a request with the
1941 * capabilities of an available network. If the available network, sets a specific
1942 * requestor (by uid and optionally package name), then this will only match a request from the
1943 * same app. If either of the capabilities have an unset uid or package name, then it matches
1944 * everything.
1945 * <p>
1946 * nc is assumed nonnull. Else, NPE.
1947 */
1948 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
1949 // No uid set, matches everything.
1950 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
1951 return true;
1952 }
1953 // uids don't match.
1954 if (mRequestorUid != nc.mRequestorUid) return false;
1955 // No package names set, matches everything
1956 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
1957 // check for package name match.
1958 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
1959 }
1960
1961 /**
1962 * Combine requestor info of the capabilities.
1963 * <p>
1964 * This is only legal if either the requestor info of this object is reset, or both info are
1965 * equal.
1966 * nc is assumed nonnull.
1967 */
1968 private void combineRequestor(@NonNull NetworkCapabilities nc) {
1969 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
1970 throw new IllegalStateException("Can't combine two uids");
1971 }
1972 if (mRequestorPackageName != null
1973 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
1974 throw new IllegalStateException("Can't combine two package names");
1975 }
1976 setRequestorUid(nc.mRequestorUid);
1977 setRequestorPackageName(nc.mRequestorPackageName);
1978 }
1979
1980 private boolean equalsRequestor(NetworkCapabilities nc) {
1981 return mRequestorUid == nc.mRequestorUid
1982 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
1983 }
Chalard Jeane5e38502020-03-18 15:58:50 +09001984
1985 /**
1986 * Builder class for NetworkCapabilities.
1987 *
1988 * This class is mainly for for {@link NetworkAgent} instances to use. Many fields in
1989 * the built class require holding a signature permission to use - mostly
1990 * {@link android.Manifest.permission.NETWORK_FACTORY}, but refer to the specific
1991 * description of each setter. As this class lives entirely in app space it does not
1992 * enforce these restrictions itself but the system server clears out the relevant
1993 * fields when receiving a NetworkCapabilities object from a caller without the
1994 * appropriate permission.
1995 *
1996 * Apps don't use this builder directly. Instead, they use {@link NetworkRequest} via
1997 * its builder object.
1998 *
1999 * @hide
2000 */
2001 @SystemApi
2002 @TestApi
Aaron Huangfbb485a2020-03-25 13:36:38 +08002003 public static final class Builder {
Chalard Jeane5e38502020-03-18 15:58:50 +09002004 private final NetworkCapabilities mCaps;
2005
2006 /**
2007 * Creates a new Builder to construct NetworkCapabilities objects.
2008 */
2009 public Builder() {
2010 mCaps = new NetworkCapabilities();
2011 }
2012
2013 /**
2014 * Creates a new Builder of NetworkCapabilities from an existing instance.
2015 */
2016 public Builder(@NonNull final NetworkCapabilities nc) {
2017 Objects.requireNonNull(nc);
2018 mCaps = new NetworkCapabilities(nc);
2019 }
2020
2021 /**
2022 * Adds the given transport type.
2023 *
2024 * Multiple transports may be added. Note that when searching for a network to satisfy a
2025 * request, satisfying any of the transports listed in the request will satisfy the request.
2026 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
2027 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
2028 * to be selected. This is logically different than
2029 * {@code NetworkCapabilities.NET_CAPABILITY_*}.
2030 *
2031 * @param transportType the transport type to be added or removed.
2032 * @return this builder
2033 */
2034 @NonNull
2035 public Builder addTransportType(@Transport int transportType) {
2036 checkValidTransportType(transportType);
2037 mCaps.addTransportType(transportType);
2038 return this;
2039 }
2040
2041 /**
2042 * Removes the given transport type.
2043 *
2044 * {@see #addTransportType}.
2045 *
2046 * @param transportType the transport type to be added or removed.
2047 * @return this builder
2048 */
2049 @NonNull
2050 public Builder removeTransportType(@Transport int transportType) {
2051 checkValidTransportType(transportType);
2052 mCaps.removeTransportType(transportType);
2053 return this;
2054 }
2055
2056 /**
2057 * Adds the given capability.
2058 *
2059 * @param capability the capability
2060 * @return this builder
2061 */
2062 @NonNull
2063 public Builder addCapability(@NetCapability final int capability) {
2064 mCaps.setCapability(capability, true);
2065 return this;
2066 }
2067
2068 /**
2069 * Removes the given capability.
2070 *
2071 * @param capability the capability
2072 * @return this builder
2073 */
2074 @NonNull
2075 public Builder removeCapability(@NetCapability final int capability) {
2076 mCaps.setCapability(capability, false);
2077 return this;
2078 }
2079
2080 /**
2081 * Sets the owner UID.
2082 *
2083 * The default value is {@link Process#INVALID_UID}. Pass this value to reset.
2084 *
2085 * Note: for security the system will clear out this field when received from a
2086 * non-privileged source.
2087 *
2088 * @param ownerUid the owner UID
2089 * @return this builder
2090 */
2091 @NonNull
2092 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2093 public Builder setOwnerUid(final int ownerUid) {
2094 mCaps.setOwnerUid(ownerUid);
2095 return this;
2096 }
2097
2098 /**
2099 * Sets the list of UIDs that are administrators of this network.
2100 *
2101 * <p>UIDs included in administratorUids gain administrator privileges over this
2102 * Network. Examples of UIDs that should be included in administratorUids are:
2103 * <ul>
2104 * <li>Carrier apps with privileges for the relevant subscription
2105 * <li>Active VPN apps
2106 * <li>Other application groups with a particular Network-related role
2107 * </ul>
2108 *
2109 * <p>In general, user-supplied networks (such as WiFi networks) do not have
2110 * administrators.
2111 *
2112 * <p>An app is granted owner privileges over Networks that it supplies. The owner
2113 * UID MUST always be included in administratorUids.
2114 *
2115 * The default value is the empty array. Pass an empty array to reset.
2116 *
2117 * Note: for security the system will clear out this field when received from a
2118 * non-privileged source, such as an app using reflection to call this or
2119 * mutate the member in the built object.
2120 *
2121 * @param administratorUids the UIDs to be set as administrators of this Network.
2122 * @return this builder
2123 */
2124 @NonNull
2125 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2126 public Builder setAdministratorUids(@NonNull final int[] administratorUids) {
2127 Objects.requireNonNull(administratorUids);
2128 mCaps.setAdministratorUids(administratorUids);
2129 return this;
2130 }
2131
2132 /**
2133 * Sets the upstream bandwidth of the link.
2134 *
2135 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
2136 * the estimated first hop transport bandwidth.
2137 * <p>
2138 * Note that when used to request a network, this specifies the minimum acceptable.
2139 * When received as the state of an existing network this specifies the typical
2140 * first hop bandwidth expected. This is never measured, but rather is inferred
2141 * from technology type and other link parameters. It could be used to differentiate
2142 * between very slow 1xRTT cellular links and other faster networks or even between
2143 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2144 * fast backhauls and slow backhauls.
2145 *
2146 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
2147 * @return this builder
2148 */
2149 @NonNull
2150 public Builder setLinkUpstreamBandwidthKbps(final int upKbps) {
2151 mCaps.setLinkUpstreamBandwidthKbps(upKbps);
2152 return this;
2153 }
2154
2155 /**
2156 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
2157 * the estimated first hop transport bandwidth.
2158 * <p>
2159 * Note that when used to request a network, this specifies the minimum acceptable.
2160 * When received as the state of an existing network this specifies the typical
2161 * first hop bandwidth expected. This is never measured, but rather is inferred
2162 * from technology type and other link parameters. It could be used to differentiate
2163 * between very slow 1xRTT cellular links and other faster networks or even between
2164 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2165 * fast backhauls and slow backhauls.
2166 *
2167 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
2168 * @return this builder
2169 */
2170 @NonNull
2171 public Builder setLinkDownstreamBandwidthKbps(final int downKbps) {
2172 mCaps.setLinkDownstreamBandwidthKbps(downKbps);
2173 return this;
2174 }
2175
2176 /**
2177 * Sets the optional bearer specific network specifier.
2178 * This has no meaning if a single transport is also not specified, so calling
2179 * this without a single transport set will generate an exception, as will
2180 * subsequently adding or removing transports after this is set.
2181 * </p>
2182 *
2183 * @param specifier a concrete, parcelable framework class that extends NetworkSpecifier,
2184 * or null to clear it.
2185 * @return this builder
2186 */
2187 @NonNull
2188 public Builder setNetworkSpecifier(@Nullable final NetworkSpecifier specifier) {
2189 mCaps.setNetworkSpecifier(specifier);
2190 return this;
2191 }
2192
2193 /**
2194 * Sets the optional transport specific information.
2195 *
2196 * @param info A concrete, parcelable framework class that extends {@link TransportInfo},
2197 * or null to clear it.
2198 * @return this builder
2199 */
2200 @NonNull
2201 public Builder setTransportInfo(@Nullable final TransportInfo info) {
2202 mCaps.setTransportInfo(info);
2203 return this;
2204 }
2205
2206 /**
2207 * Sets the signal strength. This is a signed integer, with higher values indicating a
2208 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the
2209 * same RSSI units reported by wifi code.
2210 * <p>
2211 * Note that when used to register a network callback, this specifies the minimum
2212 * acceptable signal strength. When received as the state of an existing network it
2213 * specifies the current value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means
2214 * no value when received and has no effect when requesting a callback.
2215 *
2216 * Note: for security the system will throw if it receives a NetworkRequest where
2217 * the underlying NetworkCapabilities has this member set from a source that does
2218 * not hold the {@link android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP}
2219 * permission. Apps with this permission can use this indirectly through
2220 * {@link android.net.NetworkRequest}.
2221 *
2222 * @param signalStrength the bearer-specific signal strength.
2223 * @return this builder
2224 */
2225 @NonNull
2226 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP)
2227 public Builder setSignalStrength(final int signalStrength) {
2228 mCaps.setSignalStrength(signalStrength);
2229 return this;
2230 }
2231
2232 /**
2233 * Sets the SSID of this network.
2234 *
2235 * Note: for security the system will clear out this field when received from a
2236 * non-privileged source, like an app using reflection to set this.
2237 *
2238 * @param ssid the SSID, or null to clear it.
2239 * @return this builder
2240 */
2241 @NonNull
2242 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2243 public Builder setSsid(@Nullable final String ssid) {
2244 mCaps.setSSID(ssid);
2245 return this;
2246 }
2247
2248 /**
2249 * Set the uid of the app causing this network to exist.
2250 *
2251 * Note: for security the system will clear out this field when received from a
2252 * non-privileged source.
2253 *
2254 * @param uid UID of the app.
2255 * @return this builder
2256 */
2257 @NonNull
2258 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2259 public Builder setRequestorUid(final int uid) {
2260 mCaps.setRequestorUid(uid);
2261 return this;
2262 }
2263
2264 /**
2265 * Set the package name of the app causing this network to exist.
2266 *
2267 * Note: for security the system will clear out this field when received from a
2268 * non-privileged source.
2269 *
2270 * @param packageName package name of the app, or null to clear it.
2271 * @return this builder
2272 */
2273 @NonNull
2274 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2275 public Builder setRequestorPackageName(@Nullable final String packageName) {
2276 mCaps.setRequestorPackageName(packageName);
2277 return this;
2278 }
2279
2280 /**
2281 * Builds the instance of the capabilities.
2282 *
2283 * @return the built instance of NetworkCapabilities.
2284 */
2285 @NonNull
2286 public NetworkCapabilities build() {
2287 if (mCaps.getOwnerUid() != Process.INVALID_UID) {
2288 if (!ArrayUtils.contains(mCaps.getAdministratorUids(), mCaps.getOwnerUid())) {
2289 throw new IllegalStateException("The owner UID must be included in "
2290 + " administrator UIDs.");
2291 }
2292 }
2293 return new NetworkCapabilities(mCaps);
2294 }
2295 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07002296}