blob: 18bbb07da4ba47e80e0bd8e322c2a628dc94eddc [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
Chalard Jean981dcca2020-02-06 18:31:19 +090019import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
20
Jeff Sharkeyde570312017-10-24 21:25:50 -060021import android.annotation.IntDef;
paulhud9736de2019-03-08 16:35:20 +080022import android.annotation.NonNull;
Etan Cohenca9fb562018-11-27 07:32:39 -080023import android.annotation.Nullable;
Chalard Jeane5e38502020-03-18 15:58:50 +090024import android.annotation.RequiresPermission;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070025import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060026import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000027import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060028import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010029import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070030import android.os.Parcel;
31import android.os.Parcelable;
Qingxi Li7cf06622020-01-17 17:54:27 -080032import android.os.Process;
Roshan Piuse38acab2020-01-16 12:17:17 -080033import android.text.TextUtils;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090034import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080035import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070036
37import com.android.internal.annotations.VisibleForTesting;
Chalard Jeane5e38502020-03-18 15:58:50 +090038import com.android.internal.util.ArrayUtils;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090039import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090040import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070041
Jeff Sharkeyde570312017-10-24 21:25:50 -060042import java.lang.annotation.Retention;
43import java.lang.annotation.RetentionPolicy;
Cody Kestingf7ac9962020-03-16 18:15:28 -070044import java.util.Arrays;
Etan Cohena7434272017-04-03 12:17:51 -070045import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090046import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090047import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070048
49/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070050 * Representation of the capabilities of an active network. Instances are
51 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060052 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
53 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060054 * <p>
55 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
56 * network selection. Rather than indicate a need for Wi-Fi because an
57 * application needs high bandwidth and risk obsolescence when a new, fast
58 * network appears (like LTE), the application should specify it needs high
59 * bandwidth. Similarly if an application needs an unmetered network for a bulk
60 * transfer it can specify that rather than assuming all cellular based
61 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070062 */
63public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070064 private static final String TAG = "NetworkCapabilities";
65
lucaslin783f2212019-10-22 18:27:33 +080066 // Set to true when private DNS is broken.
67 private boolean mPrivateDnsBroken;
68
Roshan Piuse38acab2020-01-16 12:17:17 -080069 /**
70 * Uid of the app making the request.
71 */
72 private int mRequestorUid;
73
74 /**
75 * Package name of the app making the request.
76 */
77 private String mRequestorPackageName;
78
Robert Greenwalt01d004e2014-05-18 15:24:21 -070079 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090081 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070082 }
83
84 public NetworkCapabilities(NetworkCapabilities nc) {
85 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090086 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070087 }
88 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070089
90 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 * Completely clears the contents of this object, removing even the capabilities that are set
92 * by default when the object is constructed.
Chalard Jeane5e38502020-03-18 15:58:50 +090093 * @hide
Lorenzo Colittif7058f52015-04-27 11:31:55 +090094 */
95 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080096 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070097 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090098 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080099 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900100 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900101 mUids = null;
Cody Kestingf7ac9962020-03-16 18:15:28 -0700102 mAdministratorUids = new int[0];
Qingxi Li7cf06622020-01-17 17:54:27 -0800103 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +0900104 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +0800105 mPrivateDnsBroken = false;
Roshan Piuse38acab2020-01-16 12:17:17 -0800106 mRequestorUid = Process.INVALID_UID;
107 mRequestorPackageName = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900108 }
109
110 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +0900111 * Set all contents of this object to the contents of a NetworkCapabilities.
112 * @hide
113 */
paulhud9736de2019-03-08 16:35:20 +0800114 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900115 mNetworkCapabilities = nc.mNetworkCapabilities;
116 mTransportTypes = nc.mTransportTypes;
117 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
118 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
119 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800120 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900121 mSignalStrength = nc.mSignalStrength;
122 setUids(nc.mUids); // Will make the defensive copy
Chalard Jean981dcca2020-02-06 18:31:19 +0900123 setAdministratorUids(nc.getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -0800124 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900125 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
126 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800127 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Roshan Piuse38acab2020-01-16 12:17:17 -0800128 mRequestorUid = nc.mRequestorUid;
129 mRequestorPackageName = nc.mRequestorPackageName;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900130 }
131
132 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 * Represents the network's capabilities. If any are specified they will be satisfied
134 * by any Network that matches all of them.
135 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100136 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900137 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800139 /**
140 * If any capabilities specified here they must not exist in the matching Network.
141 */
142 private long mUnwantedNetworkCapabilities;
143
Jeff Sharkeyde570312017-10-24 21:25:50 -0600144 /** @hide */
145 @Retention(RetentionPolicy.SOURCE)
146 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
147 NET_CAPABILITY_MMS,
148 NET_CAPABILITY_SUPL,
149 NET_CAPABILITY_DUN,
150 NET_CAPABILITY_FOTA,
151 NET_CAPABILITY_IMS,
152 NET_CAPABILITY_CBS,
153 NET_CAPABILITY_WIFI_P2P,
154 NET_CAPABILITY_IA,
155 NET_CAPABILITY_RCS,
156 NET_CAPABILITY_XCAP,
157 NET_CAPABILITY_EIMS,
158 NET_CAPABILITY_NOT_METERED,
159 NET_CAPABILITY_INTERNET,
160 NET_CAPABILITY_NOT_RESTRICTED,
161 NET_CAPABILITY_TRUSTED,
162 NET_CAPABILITY_NOT_VPN,
163 NET_CAPABILITY_VALIDATED,
164 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600165 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600166 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900167 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900168 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800169 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800170 NET_CAPABILITY_MCX,
171 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600172 })
173 public @interface NetCapability { }
174
Robert Greenwalt1448f052014-04-08 13:41:39 -0700175 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700176 * Indicates this is a network that has the ability to reach the
177 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700178 */
179 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach the carrier's
183 * SUPL server, used to retrieve GPS information.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach the carrier's
189 * DUN or tethering gateway.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach the carrier's
195 * FOTA portal, used for over the air updates.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Indicates this is a network that has the ability to reach the carrier's
201 * IMS servers, used for network registration and signaling.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates this is a network that has the ability to reach the carrier's
207 * CBS servers, used for carrier specific services.
208 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700209 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700210
211 /**
212 * Indicates this is a network that has the ability to reach a Wi-Fi direct
213 * peer.
214 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216
217 /**
218 * Indicates this is a network that has the ability to reach a carrier's
219 * Initial Attach servers.
220 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700221 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700222
223 /**
224 * Indicates this is a network that has the ability to reach a carrier's
225 * RCS servers, used for Rich Communication Services.
226 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700227 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700228
229 /**
230 * Indicates this is a network that has the ability to reach a carrier's
231 * XCAP servers, used for configuration and control.
232 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700233 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700234
235 /**
236 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700237 * Emergency IMS servers or other services, used for network signaling
238 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700239 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700240 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700241
242 /**
243 * Indicates that this network is unmetered.
244 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700245 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700246
247 /**
248 * Indicates that this network should be able to reach the internet.
249 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700250 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700251
252 /**
253 * Indicates that this network is available for general use. If this is not set
254 * applications should not attempt to communicate on this network. Note that this
255 * is simply informative and not enforcement - enforcement is handled via other means.
256 * Set by default.
257 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700258 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
259
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700260 /**
261 * Indicates that the user has indicated implicit trust of this network. This
262 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
263 * BT device or a wifi the user asked to connect to. Untrusted networks
264 * are probably limited to unknown wifi AP. Set by default.
265 */
266 public static final int NET_CAPABILITY_TRUSTED = 14;
267
Paul Jensen76b610a2015-03-18 09:33:07 -0400268 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400269 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400270 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400271 */
272 public static final int NET_CAPABILITY_NOT_VPN = 15;
273
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900274 /**
275 * Indicates that connectivity on this network was successfully validated. For example, for a
276 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
277 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900278 */
279 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700280
Paul Jensen3d194ea2015-06-16 14:27:36 -0400281 /**
282 * Indicates that this network was found to have a captive portal in place last time it was
283 * probed.
284 */
285 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
286
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900287 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600288 * Indicates that this network is not roaming.
289 */
290 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
291
292 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900293 * Indicates that this network is available for use by apps, and not a network that is being
294 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900295 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600296 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900297
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900298 /**
299 * Indicates that this network is not congested.
300 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600301 * When a network is congested, applications should defer network traffic
302 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900303 */
304 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
305
Chalard Jean804b8fb2018-01-30 22:41:41 +0900306 /**
307 * Indicates that this network is not currently suspended.
308 * <p>
309 * When a network is suspended, the network's IP addresses and any connections
310 * established on the network remain valid, but the network is temporarily unable
311 * to transfer data. This can happen, for example, if a cellular network experiences
312 * a temporary loss of signal, such as when driving through a tunnel, etc.
313 * A network with this capability is not suspended, so is expected to be able to
314 * transfer data.
315 */
316 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
317
Pavel Maltsev43403202018-01-30 17:19:44 -0800318 /**
319 * Indicates that traffic that goes through this network is paid by oem. For example,
320 * this network can be used by system apps to upload telemetry data.
321 * @hide
322 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700323 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800324 public static final int NET_CAPABILITY_OEM_PAID = 22;
325
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800326 /**
327 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
328 * servers.
329 */
330 public static final int NET_CAPABILITY_MCX = 23;
331
lucasline252a742019-03-12 13:08:03 +0800332 /**
333 * Indicates that this network was tested to only provide partial connectivity.
334 * @hide
335 */
336 @SystemApi
337 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
338
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800340 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700341
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700342 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900343 * Network capabilities that are expected to be mutable, i.e., can change while a particular
344 * network is connected.
345 */
346 private static final long MUTABLE_CAPABILITIES =
347 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
348 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900349 (1 << NET_CAPABILITY_TRUSTED)
350 | (1 << NET_CAPABILITY_VALIDATED)
351 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
352 | (1 << NET_CAPABILITY_NOT_ROAMING)
353 | (1 << NET_CAPABILITY_FOREGROUND)
354 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800355 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
356 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900357
358 /**
359 * Network capabilities that are not allowed in NetworkRequests. This exists because the
360 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
361 * capability's presence cannot be known in advance. If such a capability is requested, then we
362 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
363 * get immediately torn down because they do not have the requested capability.
364 */
365 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900366 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900367
368 /**
369 * Capabilities that are set by default when the object is constructed.
370 */
371 private static final long DEFAULT_CAPABILITIES =
372 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
373 (1 << NET_CAPABILITY_TRUSTED) |
374 (1 << NET_CAPABILITY_NOT_VPN);
375
376 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400377 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800378 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400379 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700380 @VisibleForTesting
381 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400382 (1 << NET_CAPABILITY_CBS) |
383 (1 << NET_CAPABILITY_DUN) |
384 (1 << NET_CAPABILITY_EIMS) |
385 (1 << NET_CAPABILITY_FOTA) |
386 (1 << NET_CAPABILITY_IA) |
387 (1 << NET_CAPABILITY_IMS) |
388 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800389 (1 << NET_CAPABILITY_XCAP) |
390 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800391
392 /**
393 * Capabilities that force network to be restricted.
394 * {@see #maybeMarkCapabilitiesRestricted}.
395 */
396 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800397 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400398
399 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700400 * Capabilities that suggest that a network is unrestricted.
401 * {@see #maybeMarkCapabilitiesRestricted}.
402 */
403 @VisibleForTesting
404 /* package */ static final long UNRESTRICTED_CAPABILITIES =
405 (1 << NET_CAPABILITY_INTERNET) |
406 (1 << NET_CAPABILITY_MMS) |
407 (1 << NET_CAPABILITY_SUPL) |
408 (1 << NET_CAPABILITY_WIFI_P2P);
409
410 /**
lucasline252a742019-03-12 13:08:03 +0800411 * Capabilities that are managed by ConnectivityService.
412 */
413 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
414 (1 << NET_CAPABILITY_VALIDATED)
415 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
416 | (1 << NET_CAPABILITY_FOREGROUND)
417 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
418
419 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000420 * Capabilities that are allowed for test networks. This list must be set so that it is safe
421 * for an unprivileged user to create a network with these capabilities via shell. As such,
422 * it must never contain capabilities that are generally useful to the system, such as
423 * INTERNET, IMS, SUPL, etc.
424 */
425 private static final long TEST_NETWORKS_ALLOWED_CAPABILITIES =
426 (1 << NET_CAPABILITY_NOT_METERED)
427 | (1 << NET_CAPABILITY_NOT_RESTRICTED)
428 | (1 << NET_CAPABILITY_NOT_VPN)
429 | (1 << NET_CAPABILITY_NOT_ROAMING)
430 | (1 << NET_CAPABILITY_NOT_CONGESTED)
431 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
432
433 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700434 * Adds the given capability to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900435 * Note that when searching for a network to satisfy a request, all capabilities
436 * requested must be satisfied.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700437 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600438 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900439 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900440 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700441 */
paulhud9736de2019-03-08 16:35:20 +0800442 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800443 // If the given capability was previously added to the list of unwanted capabilities
444 // then the capability will also be removed from the list of unwanted capabilities.
445 // TODO: Consider adding unwanted capabilities to the public API and mention this
446 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800447 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700448 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800449 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700450 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700451 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452
453 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800454 * Adds the given capability to the list of unwanted capabilities of this
Chalard Jeane5e38502020-03-18 15:58:50 +0900455 * {@code NetworkCapability} instance. Note that when searching for a network to
456 * satisfy a request, the network must not contain any capability from unwanted capability
457 * list.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800458 * <p>
459 * If the capability was previously added to the list of required capabilities (for
460 * example, it was there by default or added using {@link #addCapability(int)} method), then
461 * it will be removed from the list of required capabilities as well.
462 *
463 * @see #addCapability(int)
464 * @hide
465 */
466 public void addUnwantedCapability(@NetCapability int capability) {
467 checkValidCapability(capability);
468 mUnwantedNetworkCapabilities |= 1 << capability;
469 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
470 }
471
472 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
474 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600475 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900476 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900477 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478 */
paulhud9736de2019-03-08 16:35:20 +0800479 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800480 // Note that this method removes capabilities that were added via addCapability(int),
481 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800482 checkValidCapability(capability);
483 final long mask = ~(1 << capability);
484 mNetworkCapabilities &= mask;
485 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700486 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700487 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700488
489 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600490 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
491 * instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900492 * @hide
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600493 */
paulhud9736de2019-03-08 16:35:20 +0800494 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
495 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600496 if (value) {
497 addCapability(capability);
498 } else {
499 removeCapability(capability);
500 }
501 return this;
502 }
503
504 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700505 * Gets all the capabilities set on this {@code NetworkCapability} instance.
506 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600507 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700508 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700509 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000510 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600511 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600512 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900513 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700514 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515
516 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800517 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
518 *
519 * @return an array of unwanted capability values for this instance.
520 * @hide
521 */
522 public @NetCapability int[] getUnwantedCapabilities() {
523 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
524 }
525
526
527 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600528 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700529 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600530 *
531 * @hide
532 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800533 public void setCapabilities(@NetCapability int[] capabilities,
534 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600535 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800536 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600537 }
538
539 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800540 * @deprecated use {@link #setCapabilities(int[], int[])}
541 * @hide
542 */
543 @Deprecated
544 public void setCapabilities(@NetCapability int[] capabilities) {
545 setCapabilities(capabilities, new int[] {});
546 }
547
548 /**
549 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700550 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600551 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700552 * @return {@code true} if set on this instance.
553 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600554 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800555 return isValidCapability(capability)
556 && ((mNetworkCapabilities & (1 << capability)) != 0);
557 }
558
559 /** @hide */
560 public boolean hasUnwantedCapability(@NetCapability int capability) {
561 return isValidCapability(capability)
562 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700563 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700564
lucasline252a742019-03-12 13:08:03 +0800565 /**
566 * Check if this NetworkCapabilities has system managed capabilities or not.
567 * @hide
568 */
569 public boolean hasConnectivityManagedCapability() {
570 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
571 }
572
Pavel Maltseve18ef262018-03-07 11:13:04 -0800573 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800574 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800576 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 }
578
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900579 /**
580 * Convenience function that returns a human-readable description of the first mutable
581 * capability we find. Used to present an error message to apps that request mutable
582 * capabilities.
583 *
584 * @hide
585 */
paulhud9736de2019-03-08 16:35:20 +0800586 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800587 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
588 & NON_REQUESTABLE_CAPABILITIES;
589
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900590 if (nonRequestable != 0) {
591 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900592 }
593 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900594 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800595 if (isPrivateDnsBroken()) {
596 return "privateDnsBroken";
597 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900598 return null;
599 }
600
paulhud9736de2019-03-08 16:35:20 +0800601 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
602 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800603 long requestedCapabilities = mNetworkCapabilities;
604 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
605 long providedCapabilities = nc.mNetworkCapabilities;
606
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900607 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800608 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
609 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900610 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800611 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
612 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613 }
614
Robert Greenwalt06314e42014-10-29 14:04:06 -0700615 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800616 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800617 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
618 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700619 }
620
paulhud9736de2019-03-08 16:35:20 +0800621 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900622 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800623 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
624 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
625 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900626 }
627
Robert Greenwalt1448f052014-04-08 13:41:39 -0700628 /**
paulhu18354322020-01-09 17:08:11 +0800629 * Deduces that all the capabilities it provides are typically provided by restricted networks
630 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400631 *
paulhu18354322020-01-09 17:08:11 +0800632 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400633 * @hide
634 */
paulhu18354322020-01-09 17:08:11 +0800635 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800636 // Check if we have any capability that forces the network to be restricted.
637 final boolean forceRestrictedCapability =
638 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
639
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700640 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800641 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700642 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800643 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700644
645 // Must have at least some restricted capabilities.
646 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800647 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700648
paulhu18354322020-01-09 17:08:11 +0800649 return forceRestrictedCapability
650 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
651 }
652
653 /**
654 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
655 *
656 * @hide
657 */
658 public void maybeMarkCapabilitiesRestricted() {
659 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400660 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400661 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400662 }
663
664 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000665 * Test networks have strong restrictions on what capabilities they can have. Enforce these
666 * restrictions.
667 * @hide
668 */
669 public void restrictCapabilitesForTestNetwork() {
670 final long originalCapabilities = mNetworkCapabilities;
671 final NetworkSpecifier originalSpecifier = mNetworkSpecifier;
672 clearAll();
673 // Reset the transports to only contain TRANSPORT_TEST.
674 mTransportTypes = (1 << TRANSPORT_TEST);
675 mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES;
676 mNetworkSpecifier = originalSpecifier;
677 }
678
679 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700680 * Representing the transport type. Apps should generally not care about transport. A
681 * request for a fast internet connection could be satisfied by a number of different
682 * transports. If any are specified here it will be satisfied a Network that matches
683 * any of them. If a caller doesn't care about the transport it should not specify any.
684 */
685 private long mTransportTypes;
686
Jeff Sharkeyde570312017-10-24 21:25:50 -0600687 /** @hide */
688 @Retention(RetentionPolicy.SOURCE)
689 @IntDef(prefix = { "TRANSPORT_" }, value = {
690 TRANSPORT_CELLULAR,
691 TRANSPORT_WIFI,
692 TRANSPORT_BLUETOOTH,
693 TRANSPORT_ETHERNET,
694 TRANSPORT_VPN,
695 TRANSPORT_WIFI_AWARE,
696 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800697 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600698 })
699 public @interface Transport { }
700
Robert Greenwalt1448f052014-04-08 13:41:39 -0700701 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700702 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700703 */
704 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700705
706 /**
707 * Indicates this network uses a Wi-Fi transport.
708 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700709 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700710
711 /**
712 * Indicates this network uses a Bluetooth transport.
713 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700715
716 /**
717 * Indicates this network uses an Ethernet transport.
718 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700719 public static final int TRANSPORT_ETHERNET = 3;
720
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400721 /**
722 * Indicates this network uses a VPN transport.
723 */
724 public static final int TRANSPORT_VPN = 4;
725
Etan Cohen305ea282016-06-20 09:27:12 -0700726 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700727 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700728 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700729 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700730
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700731 /**
732 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700733 */
734 public static final int TRANSPORT_LOWPAN = 6;
735
Benedict Wong89ce5e32018-11-14 17:40:55 -0800736 /**
737 * Indicates this network uses a Test-only virtual interface as a transport.
738 *
739 * @hide
740 */
741 @TestApi
742 public static final int TRANSPORT_TEST = 7;
743
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900744 /** @hide */
745 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
746 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800747 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700748
Hugo Benichi16f0a942017-06-20 14:07:59 +0900749 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600750 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900751 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
752 }
753
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900754 private static final String[] TRANSPORT_NAMES = {
755 "CELLULAR",
756 "WIFI",
757 "BLUETOOTH",
758 "ETHERNET",
759 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700760 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800761 "LOWPAN",
762 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900763 };
764
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700765 /**
766 * Adds the given transport type to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900767 * Multiple transports may be applied. Note that when searching
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700768 * for a network to satisfy a request, any listed in the request will satisfy the request.
769 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
770 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
771 * to be selected. This is logically different than
772 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
773 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600774 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900775 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900776 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700777 */
paulhud9736de2019-03-08 16:35:20 +0800778 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900779 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700781 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700782 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700783 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700784
785 /**
786 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
787 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600788 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900789 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700790 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700791 */
paulhud9736de2019-03-08 16:35:20 +0800792 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900793 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700794 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700795 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700796 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700798
799 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600800 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
801 * instance.
802 *
803 * @hide
804 */
paulhud9736de2019-03-08 16:35:20 +0800805 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
806 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600807 if (value) {
808 addTransportType(transportType);
809 } else {
810 removeTransportType(transportType);
811 }
812 return this;
813 }
814
815 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700816 * Gets all the transports set on this {@code NetworkCapability} instance.
817 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600818 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700819 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700820 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600821 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900822 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800823 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900824 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700825 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700826
827 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600828 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700829 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600830 *
831 * @hide
832 */
833 public void setTransportTypes(@Transport int[] transportTypes) {
834 mTransportTypes = BitUtils.packBits(transportTypes);
835 }
836
837 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700838 * Tests for the presence of a transport on this instance.
839 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600840 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700841 * @return {@code true} if set on this instance.
842 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600843 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900844 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700845 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700846
847 private void combineTransportTypes(NetworkCapabilities nc) {
848 this.mTransportTypes |= nc.mTransportTypes;
849 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900850
Robert Greenwalt1448f052014-04-08 13:41:39 -0700851 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
852 return ((this.mTransportTypes == 0) ||
853 ((this.mTransportTypes & nc.mTransportTypes) != 0));
854 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900855
Robert Greenwalt06314e42014-10-29 14:04:06 -0700856 /** @hide */
857 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700858 return (nc.mTransportTypes == this.mTransportTypes);
859 }
860
861 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800862 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900863 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800864 * <p>This field keeps track of the UID of the app that created this network and is in charge of
865 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
866 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800867 *
868 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
869 * reset to Process.INVALID_UID unless all the following conditions are met:
870 *
871 * <ol>
872 * <li>The destination app is the network owner
873 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
874 * <li>The user's location toggle is on
875 * </ol>
876 *
877 * This is because the owner UID is location-sensitive. The apps that request a network could
878 * know where the device is if they can tell for sure the system has connected to the network
879 * they requested.
880 *
881 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
882 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
883 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900884 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800885 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900886
887 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800888 * Set the UID of the owner app.
Chalard Jeane5e38502020-03-18 15:58:50 +0900889 * @hide
Chalard Jeanf474fc32018-01-17 15:10:05 +0900890 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800891 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800892 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800893 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900894 }
895
Qingxi Li7cf06622020-01-17 17:54:27 -0800896 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800897 * Retrieves the UID of the app that owns this network.
898 *
899 * <p>For user privacy reasons, this field will only be populated if:
900 *
901 * <ol>
902 * <li>The calling app is the network owner
903 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
904 * <li>The user's location toggle is on
905 * </ol>
906 *
Chalard Jeane5e38502020-03-18 15:58:50 +0900907 * Instances of NetworkCapabilities sent to apps without the appropriate permissions will
908 * have this field cleared out.
Qingxi Li7cf06622020-01-17 17:54:27 -0800909 */
910 public int getOwnerUid() {
911 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000912 }
913
Chalard Jeanf474fc32018-01-17 15:10:05 +0900914 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800915 * UIDs of packages that are administrators of this network, or empty if none.
916 *
917 * <p>This field tracks the UIDs of packages that have permission to manage this network.
918 *
919 * <p>Network owners will also be listed as administrators.
920 *
921 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
922 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
923 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
Chalard Jean981dcca2020-02-06 18:31:19 +0900924 *
925 * <p>When received from an app in a NetworkRequest this is always cleared out by the system
926 * server. This field is never used for matching NetworkRequests to NetworkAgents.
Cody Kesting201fc132020-01-17 11:58:36 -0800927 */
Cody Kesting919385b2020-03-18 15:22:12 -0700928 @NonNull private int[] mAdministratorUids = new int[0];
Cody Kesting201fc132020-01-17 11:58:36 -0800929
930 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700931 * Sets the int[] of UIDs that are administrators of this network.
Cody Kesting201fc132020-01-17 11:58:36 -0800932 *
933 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
934 * Examples of UIDs that should be included in administratorUids are:
Chalard Jean981dcca2020-02-06 18:31:19 +0900935 *
Cody Kesting201fc132020-01-17 11:58:36 -0800936 * <ul>
Chalard Jean981dcca2020-02-06 18:31:19 +0900937 * <li>Carrier apps with privileges for the relevant subscription
938 * <li>Active VPN apps
939 * <li>Other application groups with a particular Network-related role
Cody Kesting201fc132020-01-17 11:58:36 -0800940 * </ul>
941 *
942 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
943 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800944 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
945 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800946 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900947 * <p>The administrator UIDs are set by network agents.
948 *
Cody Kesting201fc132020-01-17 11:58:36 -0800949 * @param administratorUids the UIDs to be set as administrators of this Network.
Chalard Jean981dcca2020-02-06 18:31:19 +0900950 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800951 * @hide
952 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800953 @NonNull
Cody Kestingf7ac9962020-03-16 18:15:28 -0700954 public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) {
955 mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length);
Roshan Piuse38acab2020-01-16 12:17:17 -0800956 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800957 }
958
959 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700960 * Retrieves the UIDs that are administrators of this Network.
Cody Kesting201fc132020-01-17 11:58:36 -0800961 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900962 * <p>This is only populated in NetworkCapabilities objects that come from network agents for
963 * networks that are managed by specific apps on the system, such as carrier privileged apps or
964 * wifi suggestion apps. This will include the network owner.
965 *
Cody Kestingf7ac9962020-03-16 18:15:28 -0700966 * @return the int[] of UIDs that are administrators of this Network
Chalard Jean981dcca2020-02-06 18:31:19 +0900967 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800968 * @hide
969 */
970 @NonNull
971 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +0900972 @TestApi
Cody Kestingf7ac9962020-03-16 18:15:28 -0700973 public int[] getAdministratorUids() {
974 return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
Cody Kesting201fc132020-01-17 11:58:36 -0800975 }
976
977 /**
Chalard Jean981dcca2020-02-06 18:31:19 +0900978 * Tests if the set of administrator UIDs of this network is the same as that of the passed one.
979 *
980 * <p>The administrator UIDs must be in sorted order.
981 *
982 * <p>nc is assumed non-null. Else, NPE.
983 *
984 * @hide
985 */
986 @VisibleForTesting(visibility = PRIVATE)
987 public boolean equalsAdministratorUids(@NonNull final NetworkCapabilities nc) {
988 return Arrays.equals(mAdministratorUids, nc.mAdministratorUids);
989 }
990
991 /**
992 * Combine the administrator UIDs of the capabilities.
993 *
994 * <p>This is only legal if either of the administrators lists are empty, or if they are equal.
995 * Combining administrator UIDs is only possible for combining non-overlapping sets of UIDs.
996 *
997 * <p>If both administrator lists are non-empty but not equal, they conflict with each other. In
998 * this case, it would not make sense to add them together.
999 */
1000 private void combineAdministratorUids(@NonNull final NetworkCapabilities nc) {
1001 if (nc.mAdministratorUids.length == 0) return;
1002 if (mAdministratorUids.length == 0) {
1003 mAdministratorUids = Arrays.copyOf(nc.mAdministratorUids, nc.mAdministratorUids.length);
1004 return;
1005 }
1006 if (!equalsAdministratorUids(nc)) {
1007 throw new IllegalStateException("Can't combine two different administrator UID lists");
1008 }
1009 }
1010
1011 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001012 * Value indicating that link bandwidth is unspecified.
1013 * @hide
1014 */
1015 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
1016
1017 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001018 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
1019 * for the first hop on the given transport. It is not measured, but may take into account
1020 * link parameters (Radio technology, allocated channels, etc).
1021 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001022 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
1023 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001024
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001025 /**
1026 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
1027 * the estimated first hop transport bandwidth.
1028 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001029 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001030 *
1031 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001032 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001033 */
paulhud9736de2019-03-08 16:35:20 +08001034 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001035 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001036 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001037 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001038
1039 /**
1040 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
1041 * the estimated first hop transport bandwidth.
1042 *
1043 * @return The estimated first hop upstream (device to network) bandwidth.
1044 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001045 public int getLinkUpstreamBandwidthKbps() {
1046 return mLinkUpBandwidthKbps;
1047 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001048
1049 /**
1050 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
1051 * the estimated first hop transport bandwidth.
1052 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001053 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001054 *
1055 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001056 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001057 */
paulhud9736de2019-03-08 16:35:20 +08001058 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001059 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001060 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001061 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001062
1063 /**
1064 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
1065 * the estimated first hop transport bandwidth.
1066 *
1067 * @return The estimated first hop downstream (network to device) bandwidth.
1068 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001069 public int getLinkDownstreamBandwidthKbps() {
1070 return mLinkDownBandwidthKbps;
1071 }
1072
1073 private void combineLinkBandwidths(NetworkCapabilities nc) {
1074 this.mLinkUpBandwidthKbps =
1075 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
1076 this.mLinkDownBandwidthKbps =
1077 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1078 }
1079 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1080 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1081 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1082 }
1083 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1084 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1085 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1086 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001087 /** @hide */
1088 public static int minBandwidth(int a, int b) {
1089 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1090 return b;
1091 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1092 return a;
1093 } else {
1094 return Math.min(a, b);
1095 }
1096 }
1097 /** @hide */
1098 public static int maxBandwidth(int a, int b) {
1099 return Math.max(a, b);
1100 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001101
Etan Cohena7434272017-04-03 12:17:51 -07001102 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001103 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001104
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001105 /**
1106 * Sets the optional bearer specific network specifier.
1107 * This has no meaning if a single transport is also not specified, so calling
1108 * this without a single transport set will generate an exception, as will
1109 * subsequently adding or removing transports after this is set.
1110 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001111 *
Etan Cohena7434272017-04-03 12:17:51 -07001112 * @param networkSpecifier A concrete, parcelable framework class that extends
1113 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001114 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +09001115 * @hide
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001116 */
Aaron Huange6b62392019-09-20 22:52:54 +08001117 public @NonNull NetworkCapabilities setNetworkSpecifier(
1118 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001119 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001120 throw new IllegalStateException("Must have a single transport specified to use " +
1121 "setNetworkSpecifier");
1122 }
Etan Cohena7434272017-04-03 12:17:51 -07001123
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001124 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001125
Pierre Imaic8419a82016-03-22 17:54:54 +09001126 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001127 }
1128
1129 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001130 * Sets the optional transport specific information.
1131 *
1132 * @param transportInfo A concrete, parcelable framework class that extends
1133 * {@link TransportInfo}.
1134 * @return This NetworkCapabilities instance, to facilitate chaining.
1135 * @hide
1136 */
Aaron Huange6b62392019-09-20 22:52:54 +08001137 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001138 mTransportInfo = transportInfo;
1139 return this;
1140 }
1141
1142 /**
paulhud9736de2019-03-08 16:35:20 +08001143 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001144 *
Etan Cohena7434272017-04-03 12:17:51 -07001145 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
Chalard Jeane5e38502020-03-18 15:58:50 +09001146 * specifier or {@code null}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001147 */
paulhud9736de2019-03-08 16:35:20 +08001148 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001149 return mNetworkSpecifier;
1150 }
1151
Etan Cohenca9fb562018-11-27 07:32:39 -08001152 /**
1153 * Returns a transport-specific information container. The application may cast this
1154 * container to a concrete sub-class based on its knowledge of the network request. The
1155 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001156 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001157 *
1158 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1159 * available for the network.
1160 */
1161 @Nullable public TransportInfo getTransportInfo() {
1162 return mTransportInfo;
1163 }
1164
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001165 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001166 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001167 throw new IllegalStateException("Can't combine two networkSpecifiers");
1168 }
Etan Cohena7434272017-04-03 12:17:51 -07001169 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001170 }
Etan Cohena7434272017-04-03 12:17:51 -07001171
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001172 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Chalard Jean2da4f9f2020-03-27 17:57:34 +09001173 return mNetworkSpecifier == null || mNetworkSpecifier.canBeSatisfiedBy(nc.mNetworkSpecifier)
Etan Cohena7434272017-04-03 12:17:51 -07001174 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001175 }
Etan Cohena7434272017-04-03 12:17:51 -07001176
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001177 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001178 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001179 }
1180
Etan Cohenca9fb562018-11-27 07:32:39 -08001181 private void combineTransportInfos(NetworkCapabilities nc) {
1182 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1183 throw new IllegalStateException("Can't combine two TransportInfos");
1184 }
1185 setTransportInfo(nc.mTransportInfo);
1186 }
1187
1188 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1189 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1190 }
1191
Robert Greenwalt1448f052014-04-08 13:41:39 -07001192 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001193 * Magic value that indicates no signal strength provided. A request specifying this value is
1194 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001195 */
1196 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1197
1198 /**
1199 * Signal strength. This is a signed integer, and higher values indicate better signal.
1200 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1201 */
paulhud9736de2019-03-08 16:35:20 +08001202 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001203 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001204
1205 /**
1206 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1207 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001208 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001209 * <p>
1210 * Note that when used to register a network callback, this specifies the minimum acceptable
1211 * signal strength. When received as the state of an existing network it specifies the current
1212 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1213 * effect when requesting a callback.
1214 *
1215 * @param signalStrength the bearer-specific signal strength.
Chalard Jeane5e38502020-03-18 15:58:50 +09001216 * @hide
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001217 */
paulhud9736de2019-03-08 16:35:20 +08001218 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001219 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001220 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001221 }
1222
1223 /**
1224 * Returns {@code true} if this object specifies a signal strength.
1225 *
1226 * @hide
1227 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001228 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001229 public boolean hasSignalStrength() {
1230 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1231 }
1232
1233 /**
1234 * Retrieves the signal strength.
1235 *
1236 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001237 */
1238 public int getSignalStrength() {
1239 return mSignalStrength;
1240 }
1241
1242 private void combineSignalStrength(NetworkCapabilities nc) {
1243 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1244 }
1245
1246 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1247 return this.mSignalStrength <= nc.mSignalStrength;
1248 }
1249
1250 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1251 return this.mSignalStrength == nc.mSignalStrength;
1252 }
1253
1254 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001255 * List of UIDs this network applies to. No restriction if null.
1256 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001257 * For networks, mUids represent the list of network this applies to, and null means this
1258 * network applies to all UIDs.
1259 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1260 * must be included in a network so that they match. As an exception to the general rule,
1261 * a null mUids field for requests mean "no requirements" rather than what the general rule
1262 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1263 * of this API expect in practice. A network that must match all UIDs can still be
1264 * expressed with a set ranging the entire set of possible UIDs.
1265 * <p>
1266 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001267 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1268 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1269 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1270 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001271 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001272 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001273 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1274 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001275 * <p>
1276 * Please note that in principle a single app can be associated with multiple UIDs because
1277 * each app will have a different UID when it's run as a different (macro-)user. A single
1278 * macro user can only have a single active VPN app at any given time however.
1279 * <p>
1280 * Also please be aware this class does not try to enforce any normalization on this. Callers
1281 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1282 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1283 * their own (like requiring sortedness or no overlap) they need to enforce it
1284 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1285 * or overlapping ranges are present.
1286 *
1287 * @hide
1288 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001289 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001290
1291 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001292 * Convenience method to set the UIDs this network applies to to a single UID.
1293 * @hide
1294 */
paulhud9736de2019-03-08 16:35:20 +08001295 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001296 final ArraySet<UidRange> identity = new ArraySet<>(1);
1297 identity.add(new UidRange(uid, uid));
1298 setUids(identity);
1299 return this;
1300 }
1301
1302 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001303 * Set the list of UIDs this network applies to.
1304 * This makes a copy of the set so that callers can't modify it after the call.
1305 * @hide
1306 */
paulhud9736de2019-03-08 16:35:20 +08001307 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001308 if (null == uids) {
1309 mUids = null;
1310 } else {
1311 mUids = new ArraySet<>(uids);
1312 }
1313 return this;
1314 }
1315
1316 /**
1317 * Get the list of UIDs this network applies to.
1318 * This returns a copy of the set so that callers can't modify the original object.
1319 * @hide
1320 */
paulhud9736de2019-03-08 16:35:20 +08001321 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001322 return null == mUids ? null : new ArraySet<>(mUids);
1323 }
1324
1325 /**
1326 * Test whether this network applies to this UID.
1327 * @hide
1328 */
1329 public boolean appliesToUid(int uid) {
1330 if (null == mUids) return true;
1331 for (UidRange range : mUids) {
1332 if (range.contains(uid)) {
1333 return true;
1334 }
1335 }
1336 return false;
1337 }
1338
1339 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001340 * 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 +09001341 * <p>
1342 * This test only checks whether equal range objects are in both sets. It will
1343 * return false if the ranges are not exactly the same, even if the covered UIDs
1344 * are for an equivalent result.
1345 * <p>
1346 * Note that this method is not very optimized, which is fine as long as it's not used very
1347 * often.
1348 * <p>
1349 * nc is assumed nonnull.
1350 *
1351 * @hide
1352 */
1353 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001354 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001355 Set<UidRange> comparedUids = nc.mUids;
1356 if (null == comparedUids) return null == mUids;
1357 if (null == mUids) return false;
1358 // Make a copy so it can be mutated to check that all ranges in mUids
1359 // also are in uids.
1360 final Set<UidRange> uids = new ArraySet<>(mUids);
1361 for (UidRange range : comparedUids) {
1362 if (!uids.contains(range)) {
1363 return false;
1364 }
1365 uids.remove(range);
1366 }
1367 return uids.isEmpty();
1368 }
1369
1370 /**
1371 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1372 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001373 * This method is called on the NetworkCapabilities embedded in a request with the
1374 * capabilities of an available network. It checks whether all the UIDs from this listen
1375 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1376 * in the passed nc (representing the UIDs that this network is available to).
1377 * <p>
1378 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001379 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001380 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1381 * can see its own network when it listens for it.
1382 * <p>
1383 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001384 * @see #appliesToUid
1385 * @hide
1386 */
paulhud9736de2019-03-08 16:35:20 +08001387 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001388 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001389 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001390 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001391 if (!nc.appliesToUidRange(requiredRange)) {
1392 return false;
1393 }
1394 }
1395 return true;
1396 }
1397
1398 /**
1399 * Returns whether this network applies to the passed ranges.
1400 * This assumes that to apply, the passed range has to be entirely contained
1401 * within one of the ranges this network applies to. If the ranges are not normalized,
1402 * this method may return false even though all required UIDs are covered because no
1403 * single range contained them all.
1404 * @hide
1405 */
1406 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001407 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001408 if (null == mUids) return true;
1409 for (UidRange uidRange : mUids) {
1410 if (uidRange.containsRange(requiredRange)) {
1411 return true;
1412 }
1413 }
1414 return false;
1415 }
1416
1417 /**
1418 * Combine the UIDs this network currently applies to with the UIDs the passed
1419 * NetworkCapabilities apply to.
1420 * nc is assumed nonnull.
1421 */
paulhud9736de2019-03-08 16:35:20 +08001422 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001423 if (null == nc.mUids || null == mUids) {
1424 mUids = null;
1425 return;
1426 }
1427 mUids.addAll(nc.mUids);
1428 }
1429
Chalard Jeanb03a6222018-04-11 21:09:10 +09001430
1431 /**
1432 * The SSID of the network, or null if not applicable or unknown.
1433 * <p>
1434 * This is filled in by wifi code.
1435 * @hide
1436 */
1437 private String mSSID;
1438
1439 /**
1440 * Sets the SSID of this network.
1441 * @hide
1442 */
paulhud9736de2019-03-08 16:35:20 +08001443 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001444 mSSID = ssid;
1445 return this;
1446 }
1447
1448 /**
1449 * Gets the SSID of this network, or null if none or unknown.
1450 * @hide
1451 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001452 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +09001453 @TestApi
1454 public @Nullable String getSsid() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001455 return mSSID;
1456 }
1457
1458 /**
1459 * Tests if the SSID of this network is the same as the SSID of the passed network.
1460 * @hide
1461 */
paulhud9736de2019-03-08 16:35:20 +08001462 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001463 return Objects.equals(mSSID, nc.mSSID);
1464 }
1465
1466 /**
1467 * Check if the SSID requirements of this object are matched by the passed object.
1468 * @hide
1469 */
paulhud9736de2019-03-08 16:35:20 +08001470 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001471 return mSSID == null || mSSID.equals(nc.mSSID);
1472 }
1473
1474 /**
1475 * Combine SSIDs of the capabilities.
1476 * <p>
1477 * This is only legal if either the SSID of this object is null, or both SSIDs are
1478 * equal.
1479 * @hide
1480 */
paulhud9736de2019-03-08 16:35:20 +08001481 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001482 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1483 throw new IllegalStateException("Can't combine two SSIDs");
1484 }
1485 setSSID(nc.mSSID);
1486 }
1487
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001488 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001489 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1490 * <p>
1491 * Note that this method may break an invariant of having a particular capability in either
1492 * wanted or unwanted lists but never in both. Requests that have the same capability in
1493 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001494 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001495 */
paulhud9736de2019-03-08 16:35:20 +08001496 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001497 combineNetCapabilities(nc);
1498 combineTransportTypes(nc);
1499 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001500 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001501 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001502 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001503 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001504 combineSSIDs(nc);
Roshan Piuse38acab2020-01-16 12:17:17 -08001505 combineRequestor(nc);
Chalard Jean981dcca2020-02-06 18:31:19 +09001506 combineAdministratorUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001507 }
1508
1509 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001510 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1511 *
1512 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1513 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1514 * bandwidth, signal strength, or validation / captive portal status.
1515 *
1516 * @hide
1517 */
1518 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001519 return (nc != null
1520 && satisfiedByNetCapabilities(nc, onlyImmutable)
1521 && satisfiedByTransportTypes(nc)
1522 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1523 && satisfiedBySpecifier(nc)
1524 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001525 && (onlyImmutable || satisfiedByUids(nc))
Roshan Piuse38acab2020-01-16 12:17:17 -08001526 && (onlyImmutable || satisfiedBySSID(nc)))
1527 && (onlyImmutable || satisfiedByRequestor(nc));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001528 }
1529
1530 /**
1531 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1532 *
1533 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1534 *
1535 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001536 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001537 @TestApi
1538 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001539 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001540 return satisfiedByNetworkCapabilities(nc, false);
1541 }
1542
1543 /**
1544 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1545 *
1546 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1547 *
1548 * @hide
1549 */
paulhud9736de2019-03-08 16:35:20 +08001550 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001551 return satisfiedByNetworkCapabilities(nc, true);
1552 }
1553
1554 /**
1555 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001556 * {@code NetworkCapabilities} and return a String describing any difference.
1557 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001558 *
1559 * @hide
1560 */
paulhud9736de2019-03-08 16:35:20 +08001561 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001562 if (that == null) {
1563 return "other NetworkCapabilities was null";
1564 }
1565
1566 StringJoiner joiner = new StringJoiner(", ");
1567
Hugo Benichieae7a222017-07-25 11:40:56 +09001568 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1569 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001570 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001571 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1572 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1573 if (oldImmutableCapabilities != newImmutableCapabilities) {
1574 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1575 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1576 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1577 }
1578
1579 if (!equalsSpecifier(that)) {
1580 NetworkSpecifier before = this.getNetworkSpecifier();
1581 NetworkSpecifier after = that.getNetworkSpecifier();
1582 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1583 }
1584
1585 if (!equalsTransportTypes(that)) {
1586 String before = transportNamesOf(this.getTransportTypes());
1587 String after = transportNamesOf(that.getTransportTypes());
1588 joiner.add(String.format("transports changed: %s -> %s", before, after));
1589 }
1590
1591 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001592 }
1593
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001594 /**
1595 * Checks that our requestable capabilities are the same as those of the given
1596 * {@code NetworkCapabilities}.
1597 *
1598 * @hide
1599 */
paulhud9736de2019-03-08 16:35:20 +08001600 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001601 if (nc == null) return false;
1602 return (equalsNetCapabilitiesRequestable(nc) &&
1603 equalsTransportTypes(nc) &&
1604 equalsSpecifier(nc));
1605 }
1606
Robert Greenwalt1448f052014-04-08 13:41:39 -07001607 @Override
paulhud9736de2019-03-08 16:35:20 +08001608 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001609 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001610 NetworkCapabilities that = (NetworkCapabilities) obj;
Roshan Piuse38acab2020-01-16 12:17:17 -08001611 return equalsNetCapabilities(that)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001612 && equalsTransportTypes(that)
1613 && equalsLinkBandwidths(that)
1614 && equalsSignalStrength(that)
1615 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001616 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001617 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001618 && equalsSSID(that)
Roshan Piuse38acab2020-01-16 12:17:17 -08001619 && equalsPrivateDnsBroken(that)
Chalard Jean981dcca2020-02-06 18:31:19 +09001620 && equalsRequestor(that)
1621 && equalsAdministratorUids(that);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001622 }
1623
1624 @Override
1625 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001626 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001627 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001628 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1629 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1630 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1631 + ((int) (mTransportTypes >> 32) * 13)
1632 + (mLinkUpBandwidthKbps * 17)
1633 + (mLinkDownBandwidthKbps * 19)
1634 + Objects.hashCode(mNetworkSpecifier) * 23
1635 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001636 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001637 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001638 + Objects.hashCode(mTransportInfo) * 41
Roshan Piuse38acab2020-01-16 12:17:17 -08001639 + Objects.hashCode(mPrivateDnsBroken) * 43
1640 + Objects.hashCode(mRequestorUid) * 47
Chalard Jean981dcca2020-02-06 18:31:19 +09001641 + Objects.hashCode(mRequestorPackageName) * 53
1642 + Arrays.hashCode(mAdministratorUids) * 59;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001643 }
1644
Wink Saville4e2dea72014-09-20 11:04:03 -07001645 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001646 public int describeContents() {
1647 return 0;
1648 }
Cody Kesting201fc132020-01-17 11:58:36 -08001649
Wink Saville4e2dea72014-09-20 11:04:03 -07001650 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001651 public void writeToParcel(Parcel dest, int flags) {
1652 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001653 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001654 dest.writeLong(mTransportTypes);
1655 dest.writeInt(mLinkUpBandwidthKbps);
1656 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001657 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001658 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001659 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001660 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001661 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001662 dest.writeBoolean(mPrivateDnsBroken);
Chalard Jean981dcca2020-02-06 18:31:19 +09001663 dest.writeIntArray(getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -08001664 dest.writeInt(mOwnerUid);
Roshan Piuse38acab2020-01-16 12:17:17 -08001665 dest.writeInt(mRequestorUid);
1666 dest.writeString(mRequestorPackageName);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001667 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001668
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001669 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001670 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001671 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001672 public NetworkCapabilities createFromParcel(Parcel in) {
1673 NetworkCapabilities netCap = new NetworkCapabilities();
1674
1675 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001676 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001677 netCap.mTransportTypes = in.readLong();
1678 netCap.mLinkUpBandwidthKbps = in.readInt();
1679 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001680 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001681 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001682 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001683 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1684 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001685 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001686 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kestingf7ac9962020-03-16 18:15:28 -07001687 netCap.setAdministratorUids(in.createIntArray());
Qingxi Li7cf06622020-01-17 17:54:27 -08001688 netCap.mOwnerUid = in.readInt();
Roshan Piuse38acab2020-01-16 12:17:17 -08001689 netCap.mRequestorUid = in.readInt();
1690 netCap.mRequestorPackageName = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001691 return netCap;
1692 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001693 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001694 public NetworkCapabilities[] newArray(int size) {
1695 return new NetworkCapabilities[size];
1696 }
1697 };
1698
Wink Saville4e2dea72014-09-20 11:04:03 -07001699 @Override
paulhud9736de2019-03-08 16:35:20 +08001700 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001701 final StringBuilder sb = new StringBuilder("[");
1702 if (0 != mTransportTypes) {
1703 sb.append(" Transports: ");
1704 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1705 NetworkCapabilities::transportNameOf, "|");
1706 }
1707 if (0 != mNetworkCapabilities) {
1708 sb.append(" Capabilities: ");
1709 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1710 NetworkCapabilities::capabilityNameOf, "&");
1711 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001712 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001713 sb.append(" Unwanted: ");
1714 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1715 NetworkCapabilities::capabilityNameOf, "&");
1716 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001717 if (mLinkUpBandwidthKbps > 0) {
1718 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1719 }
1720 if (mLinkDownBandwidthKbps > 0) {
1721 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1722 }
1723 if (mNetworkSpecifier != null) {
1724 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1725 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001726 if (mTransportInfo != null) {
1727 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1728 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001729 if (hasSignalStrength()) {
1730 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001731 }
1732
Chalard Jean07ace0f2018-02-26 19:00:45 +09001733 if (null != mUids) {
1734 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1735 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1736 } else {
1737 sb.append(" Uids: <").append(mUids).append(">");
1738 }
1739 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001740 if (mOwnerUid != Process.INVALID_UID) {
1741 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001742 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001743
Cody Kestingf7ac9962020-03-16 18:15:28 -07001744 if (mAdministratorUids.length == 0) {
1745 sb.append(" AdministratorUids: ").append(Arrays.toString(mAdministratorUids));
Cody Kesting201fc132020-01-17 11:58:36 -08001746 }
1747
Chalard Jeanb03a6222018-04-11 21:09:10 +09001748 if (null != mSSID) {
1749 sb.append(" SSID: ").append(mSSID);
1750 }
1751
lucaslin783f2212019-10-22 18:27:33 +08001752 if (mPrivateDnsBroken) {
1753 sb.append(" Private DNS is broken");
1754 }
1755
Roshan Piuse38acab2020-01-16 12:17:17 -08001756 sb.append(" RequestorUid: ").append(mRequestorUid);
1757 sb.append(" RequestorPackageName: ").append(mRequestorPackageName);
1758
Chalard Jean07ace0f2018-02-26 19:00:45 +09001759 sb.append("]");
1760 return sb.toString();
1761 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001762
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001763
Chalard Jean07ace0f2018-02-26 19:00:45 +09001764 private interface NameOf {
1765 String nameOf(int value);
1766 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001767
Chalard Jean07ace0f2018-02-26 19:00:45 +09001768 /**
1769 * @hide
1770 */
paulhud9736de2019-03-08 16:35:20 +08001771 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1772 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001773 int bitPos = 0;
1774 boolean firstElementAdded = false;
1775 while (bitMask != 0) {
1776 if ((bitMask & 1) != 0) {
1777 if (firstElementAdded) {
1778 sb.append(separator);
1779 } else {
1780 firstElementAdded = true;
1781 }
1782 sb.append(nameFetcher.nameOf(bitPos));
1783 }
1784 bitMask >>= 1;
1785 ++bitPos;
1786 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001787 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001788
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001789 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001790 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001791 final long token = proto.start(fieldId);
1792
1793 for (int transport : getTransportTypes()) {
1794 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1795 }
1796
1797 for (int capability : getCapabilities()) {
1798 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1799 }
1800
1801 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1802 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1803
1804 if (mNetworkSpecifier != null) {
1805 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1806 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001807 if (mTransportInfo != null) {
1808 // TODO b/120653863: write transport-specific info to proto?
1809 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001810
1811 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1812 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1813
1814 proto.end(token);
1815 }
1816
Hugo Benichi5df9d722016-04-25 17:16:35 +09001817 /**
1818 * @hide
1819 */
paulhud9736de2019-03-08 16:35:20 +08001820 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001821 StringJoiner joiner = new StringJoiner("|");
1822 if (capabilities != null) {
1823 for (int c : capabilities) {
1824 joiner.add(capabilityNameOf(c));
1825 }
1826 }
1827 return joiner.toString();
1828 }
1829
1830 /**
1831 * @hide
1832 */
paulhud9736de2019-03-08 16:35:20 +08001833 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001834 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001835 case NET_CAPABILITY_MMS: return "MMS";
1836 case NET_CAPABILITY_SUPL: return "SUPL";
1837 case NET_CAPABILITY_DUN: return "DUN";
1838 case NET_CAPABILITY_FOTA: return "FOTA";
1839 case NET_CAPABILITY_IMS: return "IMS";
1840 case NET_CAPABILITY_CBS: return "CBS";
1841 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1842 case NET_CAPABILITY_IA: return "IA";
1843 case NET_CAPABILITY_RCS: return "RCS";
1844 case NET_CAPABILITY_XCAP: return "XCAP";
1845 case NET_CAPABILITY_EIMS: return "EIMS";
1846 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1847 case NET_CAPABILITY_INTERNET: return "INTERNET";
1848 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1849 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1850 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1851 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1852 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1853 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1854 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1855 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1856 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1857 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1858 case NET_CAPABILITY_MCX: return "MCX";
1859 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1860 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001861 }
1862 }
1863
1864 /**
1865 * @hide
1866 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001867 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001868 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001869 StringJoiner joiner = new StringJoiner("|");
1870 if (types != null) {
1871 for (int t : types) {
1872 joiner.add(transportNameOf(t));
1873 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001874 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001875 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001876 }
1877
1878 /**
1879 * @hide
1880 */
paulhud9736de2019-03-08 16:35:20 +08001881 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001882 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001883 return "UNKNOWN";
1884 }
1885 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001886 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001887
Jeff Sharkeyde570312017-10-24 21:25:50 -06001888 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001889 Preconditions.checkArgument(
1890 isValidTransport(transport), "Invalid TransportType " + transport);
1891 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001892
1893 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1894 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1895 }
1896
1897 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1898 Preconditions.checkArgument(isValidCapability(capability),
1899 "NetworkCapability " + capability + "out of range");
1900 }
junyulai05986c62018-08-07 19:50:45 +08001901
1902 /**
1903 * Check if this {@code NetworkCapability} instance is metered.
1904 *
1905 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1906 * @hide
1907 */
1908 public boolean isMetered() {
1909 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1910 }
lucaslin783f2212019-10-22 18:27:33 +08001911
1912 /**
1913 * Check if private dns is broken.
1914 *
1915 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1916 * @hide
1917 */
1918 public boolean isPrivateDnsBroken() {
1919 return mPrivateDnsBroken;
1920 }
1921
1922 /**
1923 * Set mPrivateDnsBroken to true when private dns is broken.
1924 *
1925 * @param broken the status of private DNS to be set.
1926 * @hide
1927 */
1928 public void setPrivateDnsBroken(boolean broken) {
1929 mPrivateDnsBroken = broken;
1930 }
1931
1932 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1933 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1934 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001935
1936 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001937 * Set the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001938 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001939 * For instances of NetworkCapabilities representing a request, sets the
1940 * UID of the app making the request. For a network created by the system,
1941 * sets the UID of the only app whose requests can match this network.
1942 * This can be set to {@link Process#INVALID_UID} if there is no such app,
1943 * or if this instance of NetworkCapabilities is about to be sent to a
1944 * party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001945 *
1946 * @param uid UID of the app.
1947 * @hide
1948 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001949 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1950 mRequestorUid = uid;
1951 return this;
1952 }
1953
1954 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001955 * Returns the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001956 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001957 * For a NetworkRequest being made by an app, contains the app's UID. For a network
1958 * created by the system, contains the UID of the only app whose requests can match
1959 * this network, or {@link Process#INVALID_UID} if none or if the
1960 * caller does not have permission to learn about this.
1961 *
1962 * @return the uid of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001963 * @hide
1964 */
1965 public int getRequestorUid() {
1966 return mRequestorUid;
1967 }
1968
1969 /**
1970 * Set the package name of the app making the request.
1971 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001972 * For instances of NetworkCapabilities representing a request, sets the
1973 * package name of the app making the request. For a network created by the system,
1974 * sets the package name of the only app whose requests can match this network.
1975 * This can be set to null if there is no such app, or if this instance of
1976 * NetworkCapabilities is about to be sent to a party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001977 *
1978 * @param packageName package name of the app.
1979 * @hide
1980 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001981 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
1982 mRequestorPackageName = packageName;
1983 return this;
1984 }
1985
1986 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001987 * Returns the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001988 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001989 * For a NetworkRequest being made by an app, contains the app's package name. For a
1990 * network created by the system, contains the package name of the only app whose
1991 * requests can match this network, or null if none or if the caller does not have
1992 * permission to learn about this.
1993 *
1994 * @return the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001995 * @hide
1996 */
1997 @Nullable
1998 public String getRequestorPackageName() {
1999 return mRequestorPackageName;
2000 }
2001
2002 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09002003 * Set the uid and package name of the app causing this network to exist.
Roshan Piuse38acab2020-01-16 12:17:17 -08002004 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002005 * {@see #setRequestorUid} and {@link #setRequestorPackageName}
Roshan Piuse38acab2020-01-16 12:17:17 -08002006 *
2007 * @param uid UID of the app.
2008 * @param packageName package name of the app.
2009 * @hide
2010 */
2011 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
2012 int uid, @NonNull String packageName) {
2013 return setRequestorUid(uid).setRequestorPackageName(packageName);
2014 }
2015
2016 /**
2017 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
2018 * capabilities.
2019 *
2020 * This method is called on the NetworkCapabilities embedded in a request with the
2021 * capabilities of an available network. If the available network, sets a specific
2022 * requestor (by uid and optionally package name), then this will only match a request from the
2023 * same app. If either of the capabilities have an unset uid or package name, then it matches
2024 * everything.
2025 * <p>
2026 * nc is assumed nonnull. Else, NPE.
2027 */
2028 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
2029 // No uid set, matches everything.
2030 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
2031 return true;
2032 }
2033 // uids don't match.
2034 if (mRequestorUid != nc.mRequestorUid) return false;
2035 // No package names set, matches everything
2036 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
2037 // check for package name match.
2038 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2039 }
2040
2041 /**
2042 * Combine requestor info of the capabilities.
2043 * <p>
2044 * This is only legal if either the requestor info of this object is reset, or both info are
2045 * equal.
2046 * nc is assumed nonnull.
2047 */
2048 private void combineRequestor(@NonNull NetworkCapabilities nc) {
2049 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
2050 throw new IllegalStateException("Can't combine two uids");
2051 }
2052 if (mRequestorPackageName != null
2053 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
2054 throw new IllegalStateException("Can't combine two package names");
2055 }
2056 setRequestorUid(nc.mRequestorUid);
2057 setRequestorPackageName(nc.mRequestorPackageName);
2058 }
2059
2060 private boolean equalsRequestor(NetworkCapabilities nc) {
2061 return mRequestorUid == nc.mRequestorUid
2062 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2063 }
Chalard Jeane5e38502020-03-18 15:58:50 +09002064
2065 /**
2066 * Builder class for NetworkCapabilities.
2067 *
2068 * This class is mainly for for {@link NetworkAgent} instances to use. Many fields in
2069 * the built class require holding a signature permission to use - mostly
2070 * {@link android.Manifest.permission.NETWORK_FACTORY}, but refer to the specific
2071 * description of each setter. As this class lives entirely in app space it does not
2072 * enforce these restrictions itself but the system server clears out the relevant
2073 * fields when receiving a NetworkCapabilities object from a caller without the
2074 * appropriate permission.
2075 *
2076 * Apps don't use this builder directly. Instead, they use {@link NetworkRequest} via
2077 * its builder object.
2078 *
2079 * @hide
2080 */
2081 @SystemApi
2082 @TestApi
Aaron Huangfbb485a2020-03-25 13:36:38 +08002083 public static final class Builder {
Chalard Jeane5e38502020-03-18 15:58:50 +09002084 private final NetworkCapabilities mCaps;
2085
2086 /**
2087 * Creates a new Builder to construct NetworkCapabilities objects.
2088 */
2089 public Builder() {
2090 mCaps = new NetworkCapabilities();
2091 }
2092
2093 /**
2094 * Creates a new Builder of NetworkCapabilities from an existing instance.
2095 */
2096 public Builder(@NonNull final NetworkCapabilities nc) {
2097 Objects.requireNonNull(nc);
2098 mCaps = new NetworkCapabilities(nc);
2099 }
2100
2101 /**
2102 * Adds the given transport type.
2103 *
2104 * Multiple transports may be added. Note that when searching for a network to satisfy a
2105 * request, satisfying any of the transports listed in the request will satisfy the request.
2106 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
2107 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
2108 * to be selected. This is logically different than
2109 * {@code NetworkCapabilities.NET_CAPABILITY_*}.
2110 *
2111 * @param transportType the transport type to be added or removed.
2112 * @return this builder
2113 */
2114 @NonNull
2115 public Builder addTransportType(@Transport int transportType) {
2116 checkValidTransportType(transportType);
2117 mCaps.addTransportType(transportType);
2118 return this;
2119 }
2120
2121 /**
2122 * Removes the given transport type.
2123 *
2124 * {@see #addTransportType}.
2125 *
2126 * @param transportType the transport type to be added or removed.
2127 * @return this builder
2128 */
2129 @NonNull
2130 public Builder removeTransportType(@Transport int transportType) {
2131 checkValidTransportType(transportType);
2132 mCaps.removeTransportType(transportType);
2133 return this;
2134 }
2135
2136 /**
2137 * Adds the given capability.
2138 *
2139 * @param capability the capability
2140 * @return this builder
2141 */
2142 @NonNull
2143 public Builder addCapability(@NetCapability final int capability) {
2144 mCaps.setCapability(capability, true);
2145 return this;
2146 }
2147
2148 /**
2149 * Removes the given capability.
2150 *
2151 * @param capability the capability
2152 * @return this builder
2153 */
2154 @NonNull
2155 public Builder removeCapability(@NetCapability final int capability) {
2156 mCaps.setCapability(capability, false);
2157 return this;
2158 }
2159
2160 /**
2161 * Sets the owner UID.
2162 *
2163 * The default value is {@link Process#INVALID_UID}. Pass this value to reset.
2164 *
2165 * Note: for security the system will clear out this field when received from a
2166 * non-privileged source.
2167 *
2168 * @param ownerUid the owner UID
2169 * @return this builder
2170 */
2171 @NonNull
2172 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2173 public Builder setOwnerUid(final int ownerUid) {
2174 mCaps.setOwnerUid(ownerUid);
2175 return this;
2176 }
2177
2178 /**
2179 * Sets the list of UIDs that are administrators of this network.
2180 *
2181 * <p>UIDs included in administratorUids gain administrator privileges over this
2182 * Network. Examples of UIDs that should be included in administratorUids are:
2183 * <ul>
2184 * <li>Carrier apps with privileges for the relevant subscription
2185 * <li>Active VPN apps
2186 * <li>Other application groups with a particular Network-related role
2187 * </ul>
2188 *
2189 * <p>In general, user-supplied networks (such as WiFi networks) do not have
2190 * administrators.
2191 *
2192 * <p>An app is granted owner privileges over Networks that it supplies. The owner
2193 * UID MUST always be included in administratorUids.
2194 *
2195 * The default value is the empty array. Pass an empty array to reset.
2196 *
2197 * Note: for security the system will clear out this field when received from a
2198 * non-privileged source, such as an app using reflection to call this or
2199 * mutate the member in the built object.
2200 *
2201 * @param administratorUids the UIDs to be set as administrators of this Network.
2202 * @return this builder
2203 */
2204 @NonNull
2205 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2206 public Builder setAdministratorUids(@NonNull final int[] administratorUids) {
2207 Objects.requireNonNull(administratorUids);
2208 mCaps.setAdministratorUids(administratorUids);
2209 return this;
2210 }
2211
2212 /**
2213 * Sets the upstream bandwidth of the link.
2214 *
2215 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
2216 * the estimated first hop transport bandwidth.
2217 * <p>
2218 * Note that when used to request a network, this specifies the minimum acceptable.
2219 * When received as the state of an existing network this specifies the typical
2220 * first hop bandwidth expected. This is never measured, but rather is inferred
2221 * from technology type and other link parameters. It could be used to differentiate
2222 * between very slow 1xRTT cellular links and other faster networks or even between
2223 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2224 * fast backhauls and slow backhauls.
2225 *
2226 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
2227 * @return this builder
2228 */
2229 @NonNull
2230 public Builder setLinkUpstreamBandwidthKbps(final int upKbps) {
2231 mCaps.setLinkUpstreamBandwidthKbps(upKbps);
2232 return this;
2233 }
2234
2235 /**
2236 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
2237 * the estimated first hop transport bandwidth.
2238 * <p>
2239 * Note that when used to request a network, this specifies the minimum acceptable.
2240 * When received as the state of an existing network this specifies the typical
2241 * first hop bandwidth expected. This is never measured, but rather is inferred
2242 * from technology type and other link parameters. It could be used to differentiate
2243 * between very slow 1xRTT cellular links and other faster networks or even between
2244 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2245 * fast backhauls and slow backhauls.
2246 *
2247 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
2248 * @return this builder
2249 */
2250 @NonNull
2251 public Builder setLinkDownstreamBandwidthKbps(final int downKbps) {
2252 mCaps.setLinkDownstreamBandwidthKbps(downKbps);
2253 return this;
2254 }
2255
2256 /**
2257 * Sets the optional bearer specific network specifier.
2258 * This has no meaning if a single transport is also not specified, so calling
2259 * this without a single transport set will generate an exception, as will
2260 * subsequently adding or removing transports after this is set.
2261 * </p>
2262 *
2263 * @param specifier a concrete, parcelable framework class that extends NetworkSpecifier,
2264 * or null to clear it.
2265 * @return this builder
2266 */
2267 @NonNull
2268 public Builder setNetworkSpecifier(@Nullable final NetworkSpecifier specifier) {
2269 mCaps.setNetworkSpecifier(specifier);
2270 return this;
2271 }
2272
2273 /**
2274 * Sets the optional transport specific information.
2275 *
2276 * @param info A concrete, parcelable framework class that extends {@link TransportInfo},
2277 * or null to clear it.
2278 * @return this builder
2279 */
2280 @NonNull
2281 public Builder setTransportInfo(@Nullable final TransportInfo info) {
2282 mCaps.setTransportInfo(info);
2283 return this;
2284 }
2285
2286 /**
2287 * Sets the signal strength. This is a signed integer, with higher values indicating a
2288 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the
2289 * same RSSI units reported by wifi code.
2290 * <p>
2291 * Note that when used to register a network callback, this specifies the minimum
2292 * acceptable signal strength. When received as the state of an existing network it
2293 * specifies the current value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means
2294 * no value when received and has no effect when requesting a callback.
2295 *
2296 * Note: for security the system will throw if it receives a NetworkRequest where
2297 * the underlying NetworkCapabilities has this member set from a source that does
2298 * not hold the {@link android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP}
2299 * permission. Apps with this permission can use this indirectly through
2300 * {@link android.net.NetworkRequest}.
2301 *
2302 * @param signalStrength the bearer-specific signal strength.
2303 * @return this builder
2304 */
2305 @NonNull
2306 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP)
2307 public Builder setSignalStrength(final int signalStrength) {
2308 mCaps.setSignalStrength(signalStrength);
2309 return this;
2310 }
2311
2312 /**
2313 * Sets the SSID of this network.
2314 *
2315 * Note: for security the system will clear out this field when received from a
2316 * non-privileged source, like an app using reflection to set this.
2317 *
2318 * @param ssid the SSID, or null to clear it.
2319 * @return this builder
2320 */
2321 @NonNull
2322 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2323 public Builder setSsid(@Nullable final String ssid) {
2324 mCaps.setSSID(ssid);
2325 return this;
2326 }
2327
2328 /**
2329 * Set the uid of the app causing this network to exist.
2330 *
2331 * Note: for security the system will clear out this field when received from a
2332 * non-privileged source.
2333 *
2334 * @param uid UID of the app.
2335 * @return this builder
2336 */
2337 @NonNull
2338 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2339 public Builder setRequestorUid(final int uid) {
2340 mCaps.setRequestorUid(uid);
2341 return this;
2342 }
2343
2344 /**
2345 * Set the package name of the app causing this network to exist.
2346 *
2347 * Note: for security the system will clear out this field when received from a
2348 * non-privileged source.
2349 *
2350 * @param packageName package name of the app, or null to clear it.
2351 * @return this builder
2352 */
2353 @NonNull
2354 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2355 public Builder setRequestorPackageName(@Nullable final String packageName) {
2356 mCaps.setRequestorPackageName(packageName);
2357 return this;
2358 }
2359
2360 /**
2361 * Builds the instance of the capabilities.
2362 *
2363 * @return the built instance of NetworkCapabilities.
2364 */
2365 @NonNull
2366 public NetworkCapabilities build() {
2367 if (mCaps.getOwnerUid() != Process.INVALID_UID) {
2368 if (!ArrayUtils.contains(mCaps.getAdministratorUids(), mCaps.getOwnerUid())) {
2369 throw new IllegalStateException("The owner UID must be included in "
2370 + " administrator UIDs.");
2371 }
2372 }
2373 return new NetworkCapabilities(mCaps);
2374 }
2375 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07002376}