blob: 52d6fdfbd52943b726d207ad88077b077648834f [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,
Jack Yu30be5e52020-04-02 15:34:33 -0700172 NET_CAPABILITY_TEMPORARILY_NOT_METERED,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600173 })
174 public @interface NetCapability { }
175
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700177 * Indicates this is a network that has the ability to reach the
178 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 */
180 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach the carrier's
184 * SUPL server, used to retrieve GPS information.
185 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Indicates this is a network that has the ability to reach the carrier's
190 * DUN or tethering gateway.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates this is a network that has the ability to reach the carrier's
196 * FOTA portal, used for over the air updates.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates this is a network that has the ability to reach the carrier's
202 * IMS servers, used for network registration and signaling.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates this is a network that has the ability to reach the carrier's
208 * CBS servers, used for carrier specific services.
209 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700210 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700211
212 /**
213 * Indicates this is a network that has the ability to reach a Wi-Fi direct
214 * peer.
215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700217
218 /**
219 * Indicates this is a network that has the ability to reach a carrier's
220 * Initial Attach servers.
221 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700222 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700223
224 /**
225 * Indicates this is a network that has the ability to reach a carrier's
226 * RCS servers, used for Rich Communication Services.
227 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700228 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700229
230 /**
231 * Indicates this is a network that has the ability to reach a carrier's
232 * XCAP servers, used for configuration and control.
233 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700234 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700235
236 /**
237 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700238 * Emergency IMS servers or other services, used for network signaling
239 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700240 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700241 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700242
243 /**
244 * Indicates that this network is unmetered.
245 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700246 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700247
248 /**
249 * Indicates that this network should be able to reach the internet.
250 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700251 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700252
253 /**
254 * Indicates that this network is available for general use. If this is not set
255 * applications should not attempt to communicate on this network. Note that this
256 * is simply informative and not enforcement - enforcement is handled via other means.
257 * Set by default.
258 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700259 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
260
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700261 /**
262 * Indicates that the user has indicated implicit trust of this network. This
263 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
264 * BT device or a wifi the user asked to connect to. Untrusted networks
265 * are probably limited to unknown wifi AP. Set by default.
266 */
267 public static final int NET_CAPABILITY_TRUSTED = 14;
268
Paul Jensen76b610a2015-03-18 09:33:07 -0400269 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400270 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400271 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400272 */
273 public static final int NET_CAPABILITY_NOT_VPN = 15;
274
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900275 /**
276 * Indicates that connectivity on this network was successfully validated. For example, for a
277 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
278 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900279 */
280 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700281
Paul Jensen3d194ea2015-06-16 14:27:36 -0400282 /**
283 * Indicates that this network was found to have a captive portal in place last time it was
284 * probed.
285 */
286 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
287
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900288 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600289 * Indicates that this network is not roaming.
290 */
291 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
292
293 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900294 * Indicates that this network is available for use by apps, and not a network that is being
295 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900296 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600297 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900298
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900299 /**
300 * Indicates that this network is not congested.
301 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600302 * When a network is congested, applications should defer network traffic
303 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900304 */
305 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
306
Chalard Jean804b8fb2018-01-30 22:41:41 +0900307 /**
308 * Indicates that this network is not currently suspended.
309 * <p>
310 * When a network is suspended, the network's IP addresses and any connections
311 * established on the network remain valid, but the network is temporarily unable
312 * to transfer data. This can happen, for example, if a cellular network experiences
313 * a temporary loss of signal, such as when driving through a tunnel, etc.
314 * A network with this capability is not suspended, so is expected to be able to
315 * transfer data.
316 */
317 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
318
Pavel Maltsev43403202018-01-30 17:19:44 -0800319 /**
320 * Indicates that traffic that goes through this network is paid by oem. For example,
321 * this network can be used by system apps to upload telemetry data.
322 * @hide
323 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700324 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800325 public static final int NET_CAPABILITY_OEM_PAID = 22;
326
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800327 /**
328 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
329 * servers.
330 */
331 public static final int NET_CAPABILITY_MCX = 23;
332
lucasline252a742019-03-12 13:08:03 +0800333 /**
334 * Indicates that this network was tested to only provide partial connectivity.
335 * @hide
336 */
337 @SystemApi
338 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
339
Jack Yu30be5e52020-04-02 15:34:33 -0700340 /**
341 * This capability will be set for networks that are generally metered, but are currently
342 * unmetered, e.g., because the user is in a particular area. This capability can be changed at
343 * any time. When it is removed, applications are responsible for stopping any data transfer
344 * that should not occur on a metered network.
345 */
346 public static final int NET_CAPABILITY_TEMPORARILY_NOT_METERED = 25;
347
Robert Greenwalt1448f052014-04-08 13:41:39 -0700348 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Jack Yu30be5e52020-04-02 15:34:33 -0700349 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_TEMPORARILY_NOT_METERED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700350
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700351 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900352 * Network capabilities that are expected to be mutable, i.e., can change while a particular
353 * network is connected.
354 */
355 private static final long MUTABLE_CAPABILITIES =
356 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
357 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900358 (1 << NET_CAPABILITY_TRUSTED)
359 | (1 << NET_CAPABILITY_VALIDATED)
360 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
361 | (1 << NET_CAPABILITY_NOT_ROAMING)
362 | (1 << NET_CAPABILITY_FOREGROUND)
363 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800364 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
Jack Yu30be5e52020-04-02 15:34:33 -0700365 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY
366 | (1 << NET_CAPABILITY_TEMPORARILY_NOT_METERED));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900367
368 /**
369 * Network capabilities that are not allowed in NetworkRequests. This exists because the
370 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
371 * capability's presence cannot be known in advance. If such a capability is requested, then we
372 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
373 * get immediately torn down because they do not have the requested capability.
374 */
375 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900376 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900377
378 /**
379 * Capabilities that are set by default when the object is constructed.
380 */
381 private static final long DEFAULT_CAPABILITIES =
382 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
383 (1 << NET_CAPABILITY_TRUSTED) |
384 (1 << NET_CAPABILITY_NOT_VPN);
385
386 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400387 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800388 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400389 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700390 @VisibleForTesting
391 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400392 (1 << NET_CAPABILITY_CBS) |
393 (1 << NET_CAPABILITY_DUN) |
394 (1 << NET_CAPABILITY_EIMS) |
395 (1 << NET_CAPABILITY_FOTA) |
396 (1 << NET_CAPABILITY_IA) |
397 (1 << NET_CAPABILITY_IMS) |
398 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800399 (1 << NET_CAPABILITY_XCAP) |
400 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800401
402 /**
403 * Capabilities that force network to be restricted.
404 * {@see #maybeMarkCapabilitiesRestricted}.
405 */
406 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800407 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400408
409 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700410 * Capabilities that suggest that a network is unrestricted.
411 * {@see #maybeMarkCapabilitiesRestricted}.
412 */
413 @VisibleForTesting
414 /* package */ static final long UNRESTRICTED_CAPABILITIES =
415 (1 << NET_CAPABILITY_INTERNET) |
416 (1 << NET_CAPABILITY_MMS) |
417 (1 << NET_CAPABILITY_SUPL) |
418 (1 << NET_CAPABILITY_WIFI_P2P);
419
420 /**
lucasline252a742019-03-12 13:08:03 +0800421 * Capabilities that are managed by ConnectivityService.
422 */
423 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
424 (1 << NET_CAPABILITY_VALIDATED)
425 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
426 | (1 << NET_CAPABILITY_FOREGROUND)
427 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
428
429 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000430 * Capabilities that are allowed for test networks. This list must be set so that it is safe
431 * for an unprivileged user to create a network with these capabilities via shell. As such,
432 * it must never contain capabilities that are generally useful to the system, such as
433 * INTERNET, IMS, SUPL, etc.
434 */
435 private static final long TEST_NETWORKS_ALLOWED_CAPABILITIES =
436 (1 << NET_CAPABILITY_NOT_METERED)
Jack Yu30be5e52020-04-02 15:34:33 -0700437 | (1 << NET_CAPABILITY_TEMPORARILY_NOT_METERED)
Chalard Jean09c48e42020-03-25 10:33:55 +0000438 | (1 << NET_CAPABILITY_NOT_RESTRICTED)
439 | (1 << NET_CAPABILITY_NOT_VPN)
440 | (1 << NET_CAPABILITY_NOT_ROAMING)
441 | (1 << NET_CAPABILITY_NOT_CONGESTED)
442 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
443
444 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700445 * Adds the given capability to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900446 * Note that when searching for a network to satisfy a request, all capabilities
447 * requested must be satisfied.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700448 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600449 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900450 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900451 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452 */
paulhud9736de2019-03-08 16:35:20 +0800453 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800454 // If the given capability was previously added to the list of unwanted capabilities
455 // then the capability will also be removed from the list of unwanted capabilities.
456 // TODO: Consider adding unwanted capabilities to the public API and mention this
457 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800458 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700459 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800460 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700461 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700462 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700463
464 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800465 * Adds the given capability to the list of unwanted capabilities of this
Chalard Jeane5e38502020-03-18 15:58:50 +0900466 * {@code NetworkCapability} instance. Note that when searching for a network to
467 * satisfy a request, the network must not contain any capability from unwanted capability
468 * list.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800469 * <p>
470 * If the capability was previously added to the list of required capabilities (for
471 * example, it was there by default or added using {@link #addCapability(int)} method), then
472 * it will be removed from the list of required capabilities as well.
473 *
474 * @see #addCapability(int)
475 * @hide
476 */
477 public void addUnwantedCapability(@NetCapability int capability) {
478 checkValidCapability(capability);
479 mUnwantedNetworkCapabilities |= 1 << capability;
480 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
481 }
482
483 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700484 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
485 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600486 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900487 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900488 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700489 */
paulhud9736de2019-03-08 16:35:20 +0800490 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800491 // Note that this method removes capabilities that were added via addCapability(int),
492 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800493 checkValidCapability(capability);
494 final long mask = ~(1 << capability);
495 mNetworkCapabilities &= mask;
496 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700497 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700498 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700499
500 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600501 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
502 * instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900503 * @hide
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600504 */
paulhud9736de2019-03-08 16:35:20 +0800505 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
506 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600507 if (value) {
508 addCapability(capability);
509 } else {
510 removeCapability(capability);
511 }
512 return this;
513 }
514
515 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700516 * Gets all the capabilities set on this {@code NetworkCapability} instance.
517 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600518 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700519 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700520 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000521 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600522 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600523 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900524 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700525 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700526
527 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800528 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
529 *
530 * @return an array of unwanted capability values for this instance.
531 * @hide
532 */
533 public @NetCapability int[] getUnwantedCapabilities() {
534 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
535 }
536
537
538 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600539 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700540 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600541 *
542 * @hide
543 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800544 public void setCapabilities(@NetCapability int[] capabilities,
545 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600546 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800547 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600548 }
549
550 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800551 * @deprecated use {@link #setCapabilities(int[], int[])}
552 * @hide
553 */
554 @Deprecated
555 public void setCapabilities(@NetCapability int[] capabilities) {
556 setCapabilities(capabilities, new int[] {});
557 }
558
559 /**
560 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700561 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600562 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700563 * @return {@code true} if set on this instance.
564 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600565 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800566 return isValidCapability(capability)
567 && ((mNetworkCapabilities & (1 << capability)) != 0);
568 }
569
570 /** @hide */
571 public boolean hasUnwantedCapability(@NetCapability int capability) {
572 return isValidCapability(capability)
573 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700574 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575
lucasline252a742019-03-12 13:08:03 +0800576 /**
577 * Check if this NetworkCapabilities has system managed capabilities or not.
578 * @hide
579 */
580 public boolean hasConnectivityManagedCapability() {
581 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
582 }
583
Pavel Maltseve18ef262018-03-07 11:13:04 -0800584 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800585 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700586 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800587 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700588 }
589
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900590 /**
591 * Convenience function that returns a human-readable description of the first mutable
592 * capability we find. Used to present an error message to apps that request mutable
593 * capabilities.
594 *
595 * @hide
596 */
paulhud9736de2019-03-08 16:35:20 +0800597 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800598 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
599 & NON_REQUESTABLE_CAPABILITIES;
600
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900601 if (nonRequestable != 0) {
602 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900603 }
604 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900605 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800606 if (isPrivateDnsBroken()) {
607 return "privateDnsBroken";
608 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900609 return null;
610 }
611
paulhud9736de2019-03-08 16:35:20 +0800612 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
613 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800614 long requestedCapabilities = mNetworkCapabilities;
615 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
616 long providedCapabilities = nc.mNetworkCapabilities;
617
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900618 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800619 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
620 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900621 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800622 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
623 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700624 }
625
Robert Greenwalt06314e42014-10-29 14:04:06 -0700626 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800627 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800628 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
629 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700630 }
631
paulhud9736de2019-03-08 16:35:20 +0800632 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900633 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800634 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
635 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
636 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900637 }
638
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639 /**
paulhu18354322020-01-09 17:08:11 +0800640 * Deduces that all the capabilities it provides are typically provided by restricted networks
641 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400642 *
paulhu18354322020-01-09 17:08:11 +0800643 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400644 * @hide
645 */
paulhu18354322020-01-09 17:08:11 +0800646 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800647 // Check if we have any capability that forces the network to be restricted.
648 final boolean forceRestrictedCapability =
649 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
650
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700651 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800652 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700653 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800654 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700655
656 // Must have at least some restricted capabilities.
657 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800658 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700659
paulhu18354322020-01-09 17:08:11 +0800660 return forceRestrictedCapability
661 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
662 }
663
664 /**
665 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
666 *
667 * @hide
668 */
669 public void maybeMarkCapabilitiesRestricted() {
670 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400671 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400672 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400673 }
674
675 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000676 * Test networks have strong restrictions on what capabilities they can have. Enforce these
677 * restrictions.
678 * @hide
679 */
680 public void restrictCapabilitesForTestNetwork() {
681 final long originalCapabilities = mNetworkCapabilities;
682 final NetworkSpecifier originalSpecifier = mNetworkSpecifier;
Chalard Jean3b2a81b2020-04-13 19:10:13 +0000683 final int originalSignalStrength = mSignalStrength;
Chalard Jean09c48e42020-03-25 10:33:55 +0000684 clearAll();
685 // Reset the transports to only contain TRANSPORT_TEST.
686 mTransportTypes = (1 << TRANSPORT_TEST);
687 mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES;
688 mNetworkSpecifier = originalSpecifier;
Chalard Jean3b2a81b2020-04-13 19:10:13 +0000689 mSignalStrength = originalSignalStrength;
Chalard Jean09c48e42020-03-25 10:33:55 +0000690 }
691
692 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700693 * Representing the transport type. Apps should generally not care about transport. A
694 * request for a fast internet connection could be satisfied by a number of different
695 * transports. If any are specified here it will be satisfied a Network that matches
696 * any of them. If a caller doesn't care about the transport it should not specify any.
697 */
698 private long mTransportTypes;
699
Jeff Sharkeyde570312017-10-24 21:25:50 -0600700 /** @hide */
701 @Retention(RetentionPolicy.SOURCE)
702 @IntDef(prefix = { "TRANSPORT_" }, value = {
703 TRANSPORT_CELLULAR,
704 TRANSPORT_WIFI,
705 TRANSPORT_BLUETOOTH,
706 TRANSPORT_ETHERNET,
707 TRANSPORT_VPN,
708 TRANSPORT_WIFI_AWARE,
709 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800710 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 })
712 public @interface Transport { }
713
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700715 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700716 */
717 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700718
719 /**
720 * Indicates this network uses a Wi-Fi transport.
721 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700722 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700723
724 /**
725 * Indicates this network uses a Bluetooth transport.
726 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700727 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700728
729 /**
730 * Indicates this network uses an Ethernet transport.
731 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700732 public static final int TRANSPORT_ETHERNET = 3;
733
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400734 /**
735 * Indicates this network uses a VPN transport.
736 */
737 public static final int TRANSPORT_VPN = 4;
738
Etan Cohen305ea282016-06-20 09:27:12 -0700739 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700740 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700741 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700742 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700743
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700744 /**
745 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700746 */
747 public static final int TRANSPORT_LOWPAN = 6;
748
Benedict Wong89ce5e32018-11-14 17:40:55 -0800749 /**
750 * Indicates this network uses a Test-only virtual interface as a transport.
751 *
752 * @hide
753 */
754 @TestApi
755 public static final int TRANSPORT_TEST = 7;
756
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900757 /** @hide */
758 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
759 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800760 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700761
Hugo Benichi16f0a942017-06-20 14:07:59 +0900762 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600763 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900764 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
765 }
766
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900767 private static final String[] TRANSPORT_NAMES = {
768 "CELLULAR",
769 "WIFI",
770 "BLUETOOTH",
771 "ETHERNET",
772 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700773 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800774 "LOWPAN",
775 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900776 };
777
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700778 /**
779 * Adds the given transport type to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900780 * Multiple transports may be applied. Note that when searching
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700781 * for a network to satisfy a request, any listed in the request will satisfy the request.
782 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
783 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
784 * to be selected. This is logically different than
785 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
786 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600787 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900788 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900789 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700790 */
paulhud9736de2019-03-08 16:35:20 +0800791 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900792 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700793 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700794 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700795 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700796 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700797
798 /**
799 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
800 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600801 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900802 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700803 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700804 */
paulhud9736de2019-03-08 16:35:20 +0800805 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900806 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700807 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700808 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700809 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700810 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700811
812 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600813 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
814 * instance.
815 *
816 * @hide
817 */
paulhud9736de2019-03-08 16:35:20 +0800818 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
819 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600820 if (value) {
821 addTransportType(transportType);
822 } else {
823 removeTransportType(transportType);
824 }
825 return this;
826 }
827
828 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700829 * Gets all the transports set on this {@code NetworkCapability} instance.
830 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600831 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700832 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700833 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600834 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900835 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800836 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900837 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700838 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700839
840 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600841 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700842 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600843 *
844 * @hide
845 */
846 public void setTransportTypes(@Transport int[] transportTypes) {
847 mTransportTypes = BitUtils.packBits(transportTypes);
848 }
849
850 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700851 * Tests for the presence of a transport on this instance.
852 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600853 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700854 * @return {@code true} if set on this instance.
855 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600856 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900857 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700858 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700859
860 private void combineTransportTypes(NetworkCapabilities nc) {
861 this.mTransportTypes |= nc.mTransportTypes;
862 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900863
Robert Greenwalt1448f052014-04-08 13:41:39 -0700864 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
865 return ((this.mTransportTypes == 0) ||
866 ((this.mTransportTypes & nc.mTransportTypes) != 0));
867 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900868
Robert Greenwalt06314e42014-10-29 14:04:06 -0700869 /** @hide */
870 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700871 return (nc.mTransportTypes == this.mTransportTypes);
872 }
873
874 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800875 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900876 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800877 * <p>This field keeps track of the UID of the app that created this network and is in charge of
878 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
879 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800880 *
881 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
882 * reset to Process.INVALID_UID unless all the following conditions are met:
883 *
884 * <ol>
885 * <li>The destination app is the network owner
886 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
887 * <li>The user's location toggle is on
888 * </ol>
889 *
890 * This is because the owner UID is location-sensitive. The apps that request a network could
891 * know where the device is if they can tell for sure the system has connected to the network
892 * they requested.
893 *
894 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
895 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
896 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900897 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800898 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900899
900 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800901 * Set the UID of the owner app.
Chalard Jeane5e38502020-03-18 15:58:50 +0900902 * @hide
Chalard Jeanf474fc32018-01-17 15:10:05 +0900903 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800904 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800905 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800906 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900907 }
908
Qingxi Li7cf06622020-01-17 17:54:27 -0800909 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800910 * Retrieves the UID of the app that owns this network.
911 *
912 * <p>For user privacy reasons, this field will only be populated if:
913 *
914 * <ol>
915 * <li>The calling app is the network owner
916 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
917 * <li>The user's location toggle is on
918 * </ol>
919 *
Chalard Jeane5e38502020-03-18 15:58:50 +0900920 * Instances of NetworkCapabilities sent to apps without the appropriate permissions will
921 * have this field cleared out.
Qingxi Li7cf06622020-01-17 17:54:27 -0800922 */
923 public int getOwnerUid() {
924 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000925 }
926
Chalard Jeanf474fc32018-01-17 15:10:05 +0900927 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800928 * UIDs of packages that are administrators of this network, or empty if none.
929 *
930 * <p>This field tracks the UIDs of packages that have permission to manage this network.
931 *
932 * <p>Network owners will also be listed as administrators.
933 *
934 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
935 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
936 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
Chalard Jean981dcca2020-02-06 18:31:19 +0900937 *
938 * <p>When received from an app in a NetworkRequest this is always cleared out by the system
939 * server. This field is never used for matching NetworkRequests to NetworkAgents.
Cody Kesting201fc132020-01-17 11:58:36 -0800940 */
Cody Kesting919385b2020-03-18 15:22:12 -0700941 @NonNull private int[] mAdministratorUids = new int[0];
Cody Kesting201fc132020-01-17 11:58:36 -0800942
943 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700944 * Sets the int[] of UIDs that are administrators of this network.
Cody Kesting201fc132020-01-17 11:58:36 -0800945 *
946 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
947 * Examples of UIDs that should be included in administratorUids are:
Chalard Jean981dcca2020-02-06 18:31:19 +0900948 *
Cody Kesting201fc132020-01-17 11:58:36 -0800949 * <ul>
Chalard Jean981dcca2020-02-06 18:31:19 +0900950 * <li>Carrier apps with privileges for the relevant subscription
951 * <li>Active VPN apps
952 * <li>Other application groups with a particular Network-related role
Cody Kesting201fc132020-01-17 11:58:36 -0800953 * </ul>
954 *
955 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
956 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800957 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
958 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800959 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900960 * <p>The administrator UIDs are set by network agents.
961 *
Cody Kesting201fc132020-01-17 11:58:36 -0800962 * @param administratorUids the UIDs to be set as administrators of this Network.
Cody Kesting93c1e652020-03-24 11:53:30 -0700963 * @throws IllegalArgumentException if duplicate UIDs are contained in administratorUids
Chalard Jean981dcca2020-02-06 18:31:19 +0900964 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800965 * @hide
966 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800967 @NonNull
Cody Kestingf7ac9962020-03-16 18:15:28 -0700968 public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) {
969 mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length);
Cody Kesting93c1e652020-03-24 11:53:30 -0700970 Arrays.sort(mAdministratorUids);
971 for (int i = 0; i < mAdministratorUids.length - 1; i++) {
972 if (mAdministratorUids[i] >= mAdministratorUids[i + 1]) {
973 throw new IllegalArgumentException("All administrator UIDs must be unique");
974 }
975 }
Roshan Piuse38acab2020-01-16 12:17:17 -0800976 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800977 }
978
979 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700980 * Retrieves the UIDs that are administrators of this Network.
Cody Kesting201fc132020-01-17 11:58:36 -0800981 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900982 * <p>This is only populated in NetworkCapabilities objects that come from network agents for
983 * networks that are managed by specific apps on the system, such as carrier privileged apps or
984 * wifi suggestion apps. This will include the network owner.
985 *
Cody Kestingf7ac9962020-03-16 18:15:28 -0700986 * @return the int[] of UIDs that are administrators of this Network
Chalard Jean981dcca2020-02-06 18:31:19 +0900987 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800988 * @hide
989 */
990 @NonNull
991 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +0900992 @TestApi
Cody Kestingf7ac9962020-03-16 18:15:28 -0700993 public int[] getAdministratorUids() {
994 return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
Cody Kesting201fc132020-01-17 11:58:36 -0800995 }
996
997 /**
Chalard Jean981dcca2020-02-06 18:31:19 +0900998 * Tests if the set of administrator UIDs of this network is the same as that of the passed one.
999 *
1000 * <p>The administrator UIDs must be in sorted order.
1001 *
1002 * <p>nc is assumed non-null. Else, NPE.
1003 *
1004 * @hide
1005 */
1006 @VisibleForTesting(visibility = PRIVATE)
1007 public boolean equalsAdministratorUids(@NonNull final NetworkCapabilities nc) {
1008 return Arrays.equals(mAdministratorUids, nc.mAdministratorUids);
1009 }
1010
1011 /**
1012 * Combine the administrator UIDs of the capabilities.
1013 *
1014 * <p>This is only legal if either of the administrators lists are empty, or if they are equal.
1015 * Combining administrator UIDs is only possible for combining non-overlapping sets of UIDs.
1016 *
1017 * <p>If both administrator lists are non-empty but not equal, they conflict with each other. In
1018 * this case, it would not make sense to add them together.
1019 */
1020 private void combineAdministratorUids(@NonNull final NetworkCapabilities nc) {
1021 if (nc.mAdministratorUids.length == 0) return;
1022 if (mAdministratorUids.length == 0) {
1023 mAdministratorUids = Arrays.copyOf(nc.mAdministratorUids, nc.mAdministratorUids.length);
1024 return;
1025 }
1026 if (!equalsAdministratorUids(nc)) {
1027 throw new IllegalStateException("Can't combine two different administrator UID lists");
1028 }
1029 }
1030
1031 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001032 * Value indicating that link bandwidth is unspecified.
1033 * @hide
1034 */
1035 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
1036
1037 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001038 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
1039 * for the first hop on the given transport. It is not measured, but may take into account
1040 * link parameters (Radio technology, allocated channels, etc).
1041 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001042 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
1043 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001044
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001045 /**
1046 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
1047 * the estimated first hop transport bandwidth.
1048 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001049 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001050 *
1051 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001052 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001053 */
paulhud9736de2019-03-08 16:35:20 +08001054 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001055 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001056 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001057 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001058
1059 /**
1060 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
1061 * the estimated first hop transport bandwidth.
1062 *
1063 * @return The estimated first hop upstream (device to network) bandwidth.
1064 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001065 public int getLinkUpstreamBandwidthKbps() {
1066 return mLinkUpBandwidthKbps;
1067 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001068
1069 /**
1070 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
1071 * the estimated first hop transport bandwidth.
1072 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001073 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001074 *
1075 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001076 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001077 */
paulhud9736de2019-03-08 16:35:20 +08001078 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001079 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001080 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001081 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001082
1083 /**
1084 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
1085 * the estimated first hop transport bandwidth.
1086 *
1087 * @return The estimated first hop downstream (network to device) bandwidth.
1088 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001089 public int getLinkDownstreamBandwidthKbps() {
1090 return mLinkDownBandwidthKbps;
1091 }
1092
1093 private void combineLinkBandwidths(NetworkCapabilities nc) {
1094 this.mLinkUpBandwidthKbps =
1095 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
1096 this.mLinkDownBandwidthKbps =
1097 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1098 }
1099 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1100 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1101 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1102 }
1103 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1104 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1105 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1106 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001107 /** @hide */
1108 public static int minBandwidth(int a, int b) {
1109 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1110 return b;
1111 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1112 return a;
1113 } else {
1114 return Math.min(a, b);
1115 }
1116 }
1117 /** @hide */
1118 public static int maxBandwidth(int a, int b) {
1119 return Math.max(a, b);
1120 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001121
Etan Cohena7434272017-04-03 12:17:51 -07001122 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001123 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001124
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001125 /**
1126 * Sets the optional bearer specific network specifier.
1127 * This has no meaning if a single transport is also not specified, so calling
1128 * this without a single transport set will generate an exception, as will
1129 * subsequently adding or removing transports after this is set.
1130 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001131 *
Etan Cohena7434272017-04-03 12:17:51 -07001132 * @param networkSpecifier A concrete, parcelable framework class that extends
1133 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001134 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +09001135 * @hide
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001136 */
Aaron Huange6b62392019-09-20 22:52:54 +08001137 public @NonNull NetworkCapabilities setNetworkSpecifier(
1138 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001139 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001140 throw new IllegalStateException("Must have a single transport specified to use " +
1141 "setNetworkSpecifier");
1142 }
Etan Cohena7434272017-04-03 12:17:51 -07001143
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001144 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001145
Pierre Imaic8419a82016-03-22 17:54:54 +09001146 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001147 }
1148
1149 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001150 * Sets the optional transport specific information.
1151 *
1152 * @param transportInfo A concrete, parcelable framework class that extends
1153 * {@link TransportInfo}.
1154 * @return This NetworkCapabilities instance, to facilitate chaining.
1155 * @hide
1156 */
Aaron Huange6b62392019-09-20 22:52:54 +08001157 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001158 mTransportInfo = transportInfo;
1159 return this;
1160 }
1161
1162 /**
paulhud9736de2019-03-08 16:35:20 +08001163 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001164 *
Etan Cohena7434272017-04-03 12:17:51 -07001165 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
Chalard Jeane5e38502020-03-18 15:58:50 +09001166 * specifier or {@code null}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001167 */
paulhud9736de2019-03-08 16:35:20 +08001168 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001169 return mNetworkSpecifier;
1170 }
1171
Etan Cohenca9fb562018-11-27 07:32:39 -08001172 /**
1173 * Returns a transport-specific information container. The application may cast this
1174 * container to a concrete sub-class based on its knowledge of the network request. The
1175 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001176 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001177 *
1178 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1179 * available for the network.
1180 */
1181 @Nullable public TransportInfo getTransportInfo() {
1182 return mTransportInfo;
1183 }
1184
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001185 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001186 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001187 throw new IllegalStateException("Can't combine two networkSpecifiers");
1188 }
Etan Cohena7434272017-04-03 12:17:51 -07001189 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001190 }
Etan Cohena7434272017-04-03 12:17:51 -07001191
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001192 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Chalard Jean2da4f9f2020-03-27 17:57:34 +09001193 return mNetworkSpecifier == null || mNetworkSpecifier.canBeSatisfiedBy(nc.mNetworkSpecifier)
Etan Cohena7434272017-04-03 12:17:51 -07001194 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001195 }
Etan Cohena7434272017-04-03 12:17:51 -07001196
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001197 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001198 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001199 }
1200
Etan Cohenca9fb562018-11-27 07:32:39 -08001201 private void combineTransportInfos(NetworkCapabilities nc) {
1202 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1203 throw new IllegalStateException("Can't combine two TransportInfos");
1204 }
1205 setTransportInfo(nc.mTransportInfo);
1206 }
1207
1208 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1209 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1210 }
1211
Robert Greenwalt1448f052014-04-08 13:41:39 -07001212 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001213 * Magic value that indicates no signal strength provided. A request specifying this value is
1214 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001215 */
1216 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1217
1218 /**
1219 * Signal strength. This is a signed integer, and higher values indicate better signal.
1220 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1221 */
paulhud9736de2019-03-08 16:35:20 +08001222 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001223 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001224
1225 /**
1226 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1227 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001228 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001229 * <p>
1230 * Note that when used to register a network callback, this specifies the minimum acceptable
1231 * signal strength. When received as the state of an existing network it specifies the current
1232 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1233 * effect when requesting a callback.
1234 *
1235 * @param signalStrength the bearer-specific signal strength.
Chalard Jeane5e38502020-03-18 15:58:50 +09001236 * @hide
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001237 */
paulhud9736de2019-03-08 16:35:20 +08001238 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001239 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001240 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001241 }
1242
1243 /**
1244 * Returns {@code true} if this object specifies a signal strength.
1245 *
1246 * @hide
1247 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001248 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001249 public boolean hasSignalStrength() {
1250 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1251 }
1252
1253 /**
1254 * Retrieves the signal strength.
1255 *
1256 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001257 */
1258 public int getSignalStrength() {
1259 return mSignalStrength;
1260 }
1261
1262 private void combineSignalStrength(NetworkCapabilities nc) {
1263 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1264 }
1265
1266 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1267 return this.mSignalStrength <= nc.mSignalStrength;
1268 }
1269
1270 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1271 return this.mSignalStrength == nc.mSignalStrength;
1272 }
1273
1274 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001275 * List of UIDs this network applies to. No restriction if null.
1276 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001277 * For networks, mUids represent the list of network this applies to, and null means this
1278 * network applies to all UIDs.
1279 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1280 * must be included in a network so that they match. As an exception to the general rule,
1281 * a null mUids field for requests mean "no requirements" rather than what the general rule
1282 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1283 * of this API expect in practice. A network that must match all UIDs can still be
1284 * expressed with a set ranging the entire set of possible UIDs.
1285 * <p>
1286 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001287 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1288 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1289 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1290 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001291 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001292 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001293 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1294 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001295 * <p>
1296 * Please note that in principle a single app can be associated with multiple UIDs because
1297 * each app will have a different UID when it's run as a different (macro-)user. A single
1298 * macro user can only have a single active VPN app at any given time however.
1299 * <p>
1300 * Also please be aware this class does not try to enforce any normalization on this. Callers
1301 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1302 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1303 * their own (like requiring sortedness or no overlap) they need to enforce it
1304 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1305 * or overlapping ranges are present.
1306 *
1307 * @hide
1308 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001309 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001310
1311 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001312 * Convenience method to set the UIDs this network applies to to a single UID.
1313 * @hide
1314 */
paulhud9736de2019-03-08 16:35:20 +08001315 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001316 final ArraySet<UidRange> identity = new ArraySet<>(1);
1317 identity.add(new UidRange(uid, uid));
1318 setUids(identity);
1319 return this;
1320 }
1321
1322 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001323 * Set the list of UIDs this network applies to.
1324 * This makes a copy of the set so that callers can't modify it after the call.
1325 * @hide
1326 */
paulhud9736de2019-03-08 16:35:20 +08001327 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001328 if (null == uids) {
1329 mUids = null;
1330 } else {
1331 mUids = new ArraySet<>(uids);
1332 }
1333 return this;
1334 }
1335
1336 /**
1337 * Get the list of UIDs this network applies to.
1338 * This returns a copy of the set so that callers can't modify the original object.
1339 * @hide
1340 */
paulhud9736de2019-03-08 16:35:20 +08001341 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001342 return null == mUids ? null : new ArraySet<>(mUids);
1343 }
1344
1345 /**
1346 * Test whether this network applies to this UID.
1347 * @hide
1348 */
1349 public boolean appliesToUid(int uid) {
1350 if (null == mUids) return true;
1351 for (UidRange range : mUids) {
1352 if (range.contains(uid)) {
1353 return true;
1354 }
1355 }
1356 return false;
1357 }
1358
1359 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001360 * 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 +09001361 * <p>
1362 * This test only checks whether equal range objects are in both sets. It will
1363 * return false if the ranges are not exactly the same, even if the covered UIDs
1364 * are for an equivalent result.
1365 * <p>
1366 * Note that this method is not very optimized, which is fine as long as it's not used very
1367 * often.
1368 * <p>
1369 * nc is assumed nonnull.
1370 *
1371 * @hide
1372 */
1373 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001374 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001375 Set<UidRange> comparedUids = nc.mUids;
1376 if (null == comparedUids) return null == mUids;
1377 if (null == mUids) return false;
1378 // Make a copy so it can be mutated to check that all ranges in mUids
1379 // also are in uids.
1380 final Set<UidRange> uids = new ArraySet<>(mUids);
1381 for (UidRange range : comparedUids) {
1382 if (!uids.contains(range)) {
1383 return false;
1384 }
1385 uids.remove(range);
1386 }
1387 return uids.isEmpty();
1388 }
1389
1390 /**
1391 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1392 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001393 * This method is called on the NetworkCapabilities embedded in a request with the
1394 * capabilities of an available network. It checks whether all the UIDs from this listen
1395 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1396 * in the passed nc (representing the UIDs that this network is available to).
1397 * <p>
1398 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001399 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001400 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1401 * can see its own network when it listens for it.
1402 * <p>
1403 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001404 * @see #appliesToUid
1405 * @hide
1406 */
paulhud9736de2019-03-08 16:35:20 +08001407 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001408 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001409 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001410 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001411 if (!nc.appliesToUidRange(requiredRange)) {
1412 return false;
1413 }
1414 }
1415 return true;
1416 }
1417
1418 /**
1419 * Returns whether this network applies to the passed ranges.
1420 * This assumes that to apply, the passed range has to be entirely contained
1421 * within one of the ranges this network applies to. If the ranges are not normalized,
1422 * this method may return false even though all required UIDs are covered because no
1423 * single range contained them all.
1424 * @hide
1425 */
1426 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001427 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001428 if (null == mUids) return true;
1429 for (UidRange uidRange : mUids) {
1430 if (uidRange.containsRange(requiredRange)) {
1431 return true;
1432 }
1433 }
1434 return false;
1435 }
1436
1437 /**
1438 * Combine the UIDs this network currently applies to with the UIDs the passed
1439 * NetworkCapabilities apply to.
1440 * nc is assumed nonnull.
1441 */
paulhud9736de2019-03-08 16:35:20 +08001442 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001443 if (null == nc.mUids || null == mUids) {
1444 mUids = null;
1445 return;
1446 }
1447 mUids.addAll(nc.mUids);
1448 }
1449
Chalard Jeanb03a6222018-04-11 21:09:10 +09001450
1451 /**
1452 * The SSID of the network, or null if not applicable or unknown.
1453 * <p>
1454 * This is filled in by wifi code.
1455 * @hide
1456 */
1457 private String mSSID;
1458
1459 /**
1460 * Sets the SSID of this network.
1461 * @hide
1462 */
paulhud9736de2019-03-08 16:35:20 +08001463 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001464 mSSID = ssid;
1465 return this;
1466 }
1467
1468 /**
1469 * Gets the SSID of this network, or null if none or unknown.
1470 * @hide
1471 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001472 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +09001473 @TestApi
1474 public @Nullable String getSsid() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001475 return mSSID;
1476 }
1477
1478 /**
1479 * Tests if the SSID of this network is the same as the SSID of the passed network.
1480 * @hide
1481 */
paulhud9736de2019-03-08 16:35:20 +08001482 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001483 return Objects.equals(mSSID, nc.mSSID);
1484 }
1485
1486 /**
1487 * Check if the SSID requirements of this object are matched by the passed object.
1488 * @hide
1489 */
paulhud9736de2019-03-08 16:35:20 +08001490 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001491 return mSSID == null || mSSID.equals(nc.mSSID);
1492 }
1493
1494 /**
1495 * Combine SSIDs of the capabilities.
1496 * <p>
1497 * This is only legal if either the SSID of this object is null, or both SSIDs are
1498 * equal.
1499 * @hide
1500 */
paulhud9736de2019-03-08 16:35:20 +08001501 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001502 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1503 throw new IllegalStateException("Can't combine two SSIDs");
1504 }
1505 setSSID(nc.mSSID);
1506 }
1507
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001508 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001509 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1510 * <p>
1511 * Note that this method may break an invariant of having a particular capability in either
1512 * wanted or unwanted lists but never in both. Requests that have the same capability in
1513 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001514 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001515 */
paulhud9736de2019-03-08 16:35:20 +08001516 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001517 combineNetCapabilities(nc);
1518 combineTransportTypes(nc);
1519 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001520 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001521 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001522 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001523 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001524 combineSSIDs(nc);
Roshan Piuse38acab2020-01-16 12:17:17 -08001525 combineRequestor(nc);
Chalard Jean981dcca2020-02-06 18:31:19 +09001526 combineAdministratorUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001527 }
1528
1529 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001530 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1531 *
1532 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1533 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1534 * bandwidth, signal strength, or validation / captive portal status.
1535 *
1536 * @hide
1537 */
1538 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001539 return (nc != null
1540 && satisfiedByNetCapabilities(nc, onlyImmutable)
1541 && satisfiedByTransportTypes(nc)
1542 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1543 && satisfiedBySpecifier(nc)
1544 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001545 && (onlyImmutable || satisfiedByUids(nc))
Roshan Piuse38acab2020-01-16 12:17:17 -08001546 && (onlyImmutable || satisfiedBySSID(nc)))
1547 && (onlyImmutable || satisfiedByRequestor(nc));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001548 }
1549
1550 /**
1551 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1552 *
1553 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1554 *
1555 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001556 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001557 @TestApi
1558 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001559 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001560 return satisfiedByNetworkCapabilities(nc, false);
1561 }
1562
1563 /**
1564 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1565 *
1566 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1567 *
1568 * @hide
1569 */
paulhud9736de2019-03-08 16:35:20 +08001570 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001571 return satisfiedByNetworkCapabilities(nc, true);
1572 }
1573
1574 /**
1575 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001576 * {@code NetworkCapabilities} and return a String describing any difference.
1577 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001578 *
1579 * @hide
1580 */
paulhud9736de2019-03-08 16:35:20 +08001581 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001582 if (that == null) {
1583 return "other NetworkCapabilities was null";
1584 }
1585
1586 StringJoiner joiner = new StringJoiner(", ");
1587
Hugo Benichieae7a222017-07-25 11:40:56 +09001588 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1589 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001590 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001591 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1592 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1593 if (oldImmutableCapabilities != newImmutableCapabilities) {
1594 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1595 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1596 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1597 }
1598
1599 if (!equalsSpecifier(that)) {
1600 NetworkSpecifier before = this.getNetworkSpecifier();
1601 NetworkSpecifier after = that.getNetworkSpecifier();
1602 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1603 }
1604
1605 if (!equalsTransportTypes(that)) {
1606 String before = transportNamesOf(this.getTransportTypes());
1607 String after = transportNamesOf(that.getTransportTypes());
1608 joiner.add(String.format("transports changed: %s -> %s", before, after));
1609 }
1610
1611 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001612 }
1613
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001614 /**
1615 * Checks that our requestable capabilities are the same as those of the given
1616 * {@code NetworkCapabilities}.
1617 *
1618 * @hide
1619 */
paulhud9736de2019-03-08 16:35:20 +08001620 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001621 if (nc == null) return false;
1622 return (equalsNetCapabilitiesRequestable(nc) &&
1623 equalsTransportTypes(nc) &&
1624 equalsSpecifier(nc));
1625 }
1626
Robert Greenwalt1448f052014-04-08 13:41:39 -07001627 @Override
paulhud9736de2019-03-08 16:35:20 +08001628 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001629 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001630 NetworkCapabilities that = (NetworkCapabilities) obj;
Roshan Piuse38acab2020-01-16 12:17:17 -08001631 return equalsNetCapabilities(that)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001632 && equalsTransportTypes(that)
1633 && equalsLinkBandwidths(that)
1634 && equalsSignalStrength(that)
1635 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001636 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001637 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001638 && equalsSSID(that)
Roshan Piuse38acab2020-01-16 12:17:17 -08001639 && equalsPrivateDnsBroken(that)
Chalard Jean981dcca2020-02-06 18:31:19 +09001640 && equalsRequestor(that)
1641 && equalsAdministratorUids(that);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001642 }
1643
1644 @Override
1645 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001646 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001647 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001648 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1649 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1650 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1651 + ((int) (mTransportTypes >> 32) * 13)
1652 + (mLinkUpBandwidthKbps * 17)
1653 + (mLinkDownBandwidthKbps * 19)
1654 + Objects.hashCode(mNetworkSpecifier) * 23
1655 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001656 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001657 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001658 + Objects.hashCode(mTransportInfo) * 41
Roshan Piuse38acab2020-01-16 12:17:17 -08001659 + Objects.hashCode(mPrivateDnsBroken) * 43
1660 + Objects.hashCode(mRequestorUid) * 47
Chalard Jean981dcca2020-02-06 18:31:19 +09001661 + Objects.hashCode(mRequestorPackageName) * 53
1662 + Arrays.hashCode(mAdministratorUids) * 59;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001663 }
1664
Wink Saville4e2dea72014-09-20 11:04:03 -07001665 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001666 public int describeContents() {
1667 return 0;
1668 }
Cody Kesting201fc132020-01-17 11:58:36 -08001669
Wink Saville4e2dea72014-09-20 11:04:03 -07001670 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001671 public void writeToParcel(Parcel dest, int flags) {
1672 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001673 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001674 dest.writeLong(mTransportTypes);
1675 dest.writeInt(mLinkUpBandwidthKbps);
1676 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001677 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001678 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001679 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001680 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001681 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001682 dest.writeBoolean(mPrivateDnsBroken);
Chalard Jean981dcca2020-02-06 18:31:19 +09001683 dest.writeIntArray(getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -08001684 dest.writeInt(mOwnerUid);
Roshan Piuse38acab2020-01-16 12:17:17 -08001685 dest.writeInt(mRequestorUid);
1686 dest.writeString(mRequestorPackageName);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001687 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001688
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001689 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001690 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001691 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001692 public NetworkCapabilities createFromParcel(Parcel in) {
1693 NetworkCapabilities netCap = new NetworkCapabilities();
1694
1695 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001696 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001697 netCap.mTransportTypes = in.readLong();
1698 netCap.mLinkUpBandwidthKbps = in.readInt();
1699 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001700 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001701 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001702 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001703 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1704 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001705 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001706 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kestingf7ac9962020-03-16 18:15:28 -07001707 netCap.setAdministratorUids(in.createIntArray());
Qingxi Li7cf06622020-01-17 17:54:27 -08001708 netCap.mOwnerUid = in.readInt();
Roshan Piuse38acab2020-01-16 12:17:17 -08001709 netCap.mRequestorUid = in.readInt();
1710 netCap.mRequestorPackageName = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001711 return netCap;
1712 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001713 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001714 public NetworkCapabilities[] newArray(int size) {
1715 return new NetworkCapabilities[size];
1716 }
1717 };
1718
Wink Saville4e2dea72014-09-20 11:04:03 -07001719 @Override
paulhud9736de2019-03-08 16:35:20 +08001720 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001721 final StringBuilder sb = new StringBuilder("[");
1722 if (0 != mTransportTypes) {
1723 sb.append(" Transports: ");
1724 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1725 NetworkCapabilities::transportNameOf, "|");
1726 }
1727 if (0 != mNetworkCapabilities) {
1728 sb.append(" Capabilities: ");
1729 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1730 NetworkCapabilities::capabilityNameOf, "&");
1731 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001732 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001733 sb.append(" Unwanted: ");
1734 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1735 NetworkCapabilities::capabilityNameOf, "&");
1736 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001737 if (mLinkUpBandwidthKbps > 0) {
1738 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1739 }
1740 if (mLinkDownBandwidthKbps > 0) {
1741 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1742 }
1743 if (mNetworkSpecifier != null) {
1744 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1745 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001746 if (mTransportInfo != null) {
1747 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1748 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001749 if (hasSignalStrength()) {
1750 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001751 }
1752
Chalard Jean07ace0f2018-02-26 19:00:45 +09001753 if (null != mUids) {
1754 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1755 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1756 } else {
1757 sb.append(" Uids: <").append(mUids).append(">");
1758 }
1759 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001760 if (mOwnerUid != Process.INVALID_UID) {
1761 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001762 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001763
Cody Kestingf7ac9962020-03-16 18:15:28 -07001764 if (mAdministratorUids.length == 0) {
1765 sb.append(" AdministratorUids: ").append(Arrays.toString(mAdministratorUids));
Cody Kesting201fc132020-01-17 11:58:36 -08001766 }
1767
Chalard Jeanb03a6222018-04-11 21:09:10 +09001768 if (null != mSSID) {
1769 sb.append(" SSID: ").append(mSSID);
1770 }
1771
lucaslin783f2212019-10-22 18:27:33 +08001772 if (mPrivateDnsBroken) {
1773 sb.append(" Private DNS is broken");
1774 }
1775
Roshan Piuse38acab2020-01-16 12:17:17 -08001776 sb.append(" RequestorUid: ").append(mRequestorUid);
1777 sb.append(" RequestorPackageName: ").append(mRequestorPackageName);
1778
Chalard Jean07ace0f2018-02-26 19:00:45 +09001779 sb.append("]");
1780 return sb.toString();
1781 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001782
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001783
Chalard Jean07ace0f2018-02-26 19:00:45 +09001784 private interface NameOf {
1785 String nameOf(int value);
1786 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001787
Chalard Jean07ace0f2018-02-26 19:00:45 +09001788 /**
1789 * @hide
1790 */
paulhud9736de2019-03-08 16:35:20 +08001791 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1792 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001793 int bitPos = 0;
1794 boolean firstElementAdded = false;
1795 while (bitMask != 0) {
1796 if ((bitMask & 1) != 0) {
1797 if (firstElementAdded) {
1798 sb.append(separator);
1799 } else {
1800 firstElementAdded = true;
1801 }
1802 sb.append(nameFetcher.nameOf(bitPos));
1803 }
1804 bitMask >>= 1;
1805 ++bitPos;
1806 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001807 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001808
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001809 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001810 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001811 final long token = proto.start(fieldId);
1812
1813 for (int transport : getTransportTypes()) {
1814 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1815 }
1816
1817 for (int capability : getCapabilities()) {
1818 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1819 }
1820
1821 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1822 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1823
1824 if (mNetworkSpecifier != null) {
1825 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1826 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001827 if (mTransportInfo != null) {
1828 // TODO b/120653863: write transport-specific info to proto?
1829 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001830
1831 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1832 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1833
1834 proto.end(token);
1835 }
1836
Hugo Benichi5df9d722016-04-25 17:16:35 +09001837 /**
1838 * @hide
1839 */
paulhud9736de2019-03-08 16:35:20 +08001840 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001841 StringJoiner joiner = new StringJoiner("|");
1842 if (capabilities != null) {
1843 for (int c : capabilities) {
1844 joiner.add(capabilityNameOf(c));
1845 }
1846 }
1847 return joiner.toString();
1848 }
1849
1850 /**
1851 * @hide
1852 */
paulhud9736de2019-03-08 16:35:20 +08001853 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001854 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001855 case NET_CAPABILITY_MMS: return "MMS";
1856 case NET_CAPABILITY_SUPL: return "SUPL";
1857 case NET_CAPABILITY_DUN: return "DUN";
1858 case NET_CAPABILITY_FOTA: return "FOTA";
1859 case NET_CAPABILITY_IMS: return "IMS";
1860 case NET_CAPABILITY_CBS: return "CBS";
1861 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1862 case NET_CAPABILITY_IA: return "IA";
1863 case NET_CAPABILITY_RCS: return "RCS";
1864 case NET_CAPABILITY_XCAP: return "XCAP";
1865 case NET_CAPABILITY_EIMS: return "EIMS";
1866 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1867 case NET_CAPABILITY_INTERNET: return "INTERNET";
1868 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1869 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1870 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1871 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1872 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1873 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1874 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1875 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1876 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1877 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1878 case NET_CAPABILITY_MCX: return "MCX";
1879 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
Jack Yu30be5e52020-04-02 15:34:33 -07001880 case NET_CAPABILITY_TEMPORARILY_NOT_METERED: return "TEMPORARILY_NOT_METERED";
lucasline252a742019-03-12 13:08:03 +08001881 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001882 }
1883 }
1884
1885 /**
1886 * @hide
1887 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001888 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001889 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001890 StringJoiner joiner = new StringJoiner("|");
1891 if (types != null) {
1892 for (int t : types) {
1893 joiner.add(transportNameOf(t));
1894 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001895 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001896 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001897 }
1898
1899 /**
1900 * @hide
1901 */
paulhud9736de2019-03-08 16:35:20 +08001902 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001903 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001904 return "UNKNOWN";
1905 }
1906 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001907 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001908
Jeff Sharkeyde570312017-10-24 21:25:50 -06001909 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001910 Preconditions.checkArgument(
1911 isValidTransport(transport), "Invalid TransportType " + transport);
1912 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001913
1914 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1915 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1916 }
1917
1918 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1919 Preconditions.checkArgument(isValidCapability(capability),
1920 "NetworkCapability " + capability + "out of range");
1921 }
junyulai05986c62018-08-07 19:50:45 +08001922
1923 /**
1924 * Check if this {@code NetworkCapability} instance is metered.
1925 *
1926 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1927 * @hide
1928 */
1929 public boolean isMetered() {
1930 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1931 }
lucaslin783f2212019-10-22 18:27:33 +08001932
1933 /**
1934 * Check if private dns is broken.
1935 *
1936 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1937 * @hide
1938 */
1939 public boolean isPrivateDnsBroken() {
1940 return mPrivateDnsBroken;
1941 }
1942
1943 /**
1944 * Set mPrivateDnsBroken to true when private dns is broken.
1945 *
1946 * @param broken the status of private DNS to be set.
1947 * @hide
1948 */
1949 public void setPrivateDnsBroken(boolean broken) {
1950 mPrivateDnsBroken = broken;
1951 }
1952
1953 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1954 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1955 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001956
1957 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001958 * Set the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001959 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001960 * For instances of NetworkCapabilities representing a request, sets the
1961 * UID of the app making the request. For a network created by the system,
1962 * sets the UID of the only app whose requests can match this network.
1963 * This can be set to {@link Process#INVALID_UID} if there is no such app,
1964 * or if this instance of NetworkCapabilities is about to be sent to a
1965 * party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001966 *
1967 * @param uid UID of the app.
1968 * @hide
1969 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001970 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1971 mRequestorUid = uid;
1972 return this;
1973 }
1974
1975 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001976 * Returns the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001977 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001978 * For a NetworkRequest being made by an app, contains the app's UID. For a network
1979 * created by the system, contains the UID of the only app whose requests can match
1980 * this network, or {@link Process#INVALID_UID} if none or if the
1981 * caller does not have permission to learn about this.
1982 *
1983 * @return the uid of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001984 * @hide
1985 */
1986 public int getRequestorUid() {
1987 return mRequestorUid;
1988 }
1989
1990 /**
1991 * Set the package name of the app making the request.
1992 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001993 * For instances of NetworkCapabilities representing a request, sets the
1994 * package name of the app making the request. For a network created by the system,
1995 * sets the package name of the only app whose requests can match this network.
1996 * This can be set to null if there is no such app, or if this instance of
1997 * NetworkCapabilities is about to be sent to a party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001998 *
1999 * @param packageName package name of the app.
2000 * @hide
2001 */
Roshan Piuse38acab2020-01-16 12:17:17 -08002002 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
2003 mRequestorPackageName = packageName;
2004 return this;
2005 }
2006
2007 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09002008 * Returns the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08002009 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002010 * For a NetworkRequest being made by an app, contains the app's package name. For a
2011 * network created by the system, contains the package name of the only app whose
2012 * requests can match this network, or null if none or if the caller does not have
2013 * permission to learn about this.
2014 *
2015 * @return the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08002016 * @hide
2017 */
2018 @Nullable
2019 public String getRequestorPackageName() {
2020 return mRequestorPackageName;
2021 }
2022
2023 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09002024 * Set the uid and package name of the app causing this network to exist.
Roshan Piuse38acab2020-01-16 12:17:17 -08002025 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002026 * {@see #setRequestorUid} and {@link #setRequestorPackageName}
Roshan Piuse38acab2020-01-16 12:17:17 -08002027 *
2028 * @param uid UID of the app.
2029 * @param packageName package name of the app.
2030 * @hide
2031 */
2032 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
2033 int uid, @NonNull String packageName) {
2034 return setRequestorUid(uid).setRequestorPackageName(packageName);
2035 }
2036
2037 /**
2038 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
2039 * capabilities.
2040 *
2041 * This method is called on the NetworkCapabilities embedded in a request with the
2042 * capabilities of an available network. If the available network, sets a specific
2043 * requestor (by uid and optionally package name), then this will only match a request from the
2044 * same app. If either of the capabilities have an unset uid or package name, then it matches
2045 * everything.
2046 * <p>
2047 * nc is assumed nonnull. Else, NPE.
2048 */
2049 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
2050 // No uid set, matches everything.
2051 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
2052 return true;
2053 }
2054 // uids don't match.
2055 if (mRequestorUid != nc.mRequestorUid) return false;
2056 // No package names set, matches everything
2057 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
2058 // check for package name match.
2059 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2060 }
2061
2062 /**
2063 * Combine requestor info of the capabilities.
2064 * <p>
2065 * This is only legal if either the requestor info of this object is reset, or both info are
2066 * equal.
2067 * nc is assumed nonnull.
2068 */
2069 private void combineRequestor(@NonNull NetworkCapabilities nc) {
2070 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
2071 throw new IllegalStateException("Can't combine two uids");
2072 }
2073 if (mRequestorPackageName != null
2074 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
2075 throw new IllegalStateException("Can't combine two package names");
2076 }
2077 setRequestorUid(nc.mRequestorUid);
2078 setRequestorPackageName(nc.mRequestorPackageName);
2079 }
2080
2081 private boolean equalsRequestor(NetworkCapabilities nc) {
2082 return mRequestorUid == nc.mRequestorUid
2083 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2084 }
Chalard Jeane5e38502020-03-18 15:58:50 +09002085
2086 /**
2087 * Builder class for NetworkCapabilities.
2088 *
2089 * This class is mainly for for {@link NetworkAgent} instances to use. Many fields in
2090 * the built class require holding a signature permission to use - mostly
2091 * {@link android.Manifest.permission.NETWORK_FACTORY}, but refer to the specific
2092 * description of each setter. As this class lives entirely in app space it does not
2093 * enforce these restrictions itself but the system server clears out the relevant
2094 * fields when receiving a NetworkCapabilities object from a caller without the
2095 * appropriate permission.
2096 *
2097 * Apps don't use this builder directly. Instead, they use {@link NetworkRequest} via
2098 * its builder object.
2099 *
2100 * @hide
2101 */
2102 @SystemApi
2103 @TestApi
Aaron Huangfbb485a2020-03-25 13:36:38 +08002104 public static final class Builder {
Chalard Jeane5e38502020-03-18 15:58:50 +09002105 private final NetworkCapabilities mCaps;
2106
2107 /**
2108 * Creates a new Builder to construct NetworkCapabilities objects.
2109 */
2110 public Builder() {
2111 mCaps = new NetworkCapabilities();
2112 }
2113
2114 /**
2115 * Creates a new Builder of NetworkCapabilities from an existing instance.
2116 */
2117 public Builder(@NonNull final NetworkCapabilities nc) {
2118 Objects.requireNonNull(nc);
2119 mCaps = new NetworkCapabilities(nc);
2120 }
2121
2122 /**
2123 * Adds the given transport type.
2124 *
2125 * Multiple transports may be added. Note that when searching for a network to satisfy a
2126 * request, satisfying any of the transports listed in the request will satisfy the request.
2127 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
2128 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
2129 * to be selected. This is logically different than
2130 * {@code NetworkCapabilities.NET_CAPABILITY_*}.
2131 *
2132 * @param transportType the transport type to be added or removed.
2133 * @return this builder
2134 */
2135 @NonNull
2136 public Builder addTransportType(@Transport int transportType) {
2137 checkValidTransportType(transportType);
2138 mCaps.addTransportType(transportType);
2139 return this;
2140 }
2141
2142 /**
2143 * Removes the given transport type.
2144 *
2145 * {@see #addTransportType}.
2146 *
2147 * @param transportType the transport type to be added or removed.
2148 * @return this builder
2149 */
2150 @NonNull
2151 public Builder removeTransportType(@Transport int transportType) {
2152 checkValidTransportType(transportType);
2153 mCaps.removeTransportType(transportType);
2154 return this;
2155 }
2156
2157 /**
2158 * Adds the given capability.
2159 *
2160 * @param capability the capability
2161 * @return this builder
2162 */
2163 @NonNull
2164 public Builder addCapability(@NetCapability final int capability) {
2165 mCaps.setCapability(capability, true);
2166 return this;
2167 }
2168
2169 /**
2170 * Removes the given capability.
2171 *
2172 * @param capability the capability
2173 * @return this builder
2174 */
2175 @NonNull
2176 public Builder removeCapability(@NetCapability final int capability) {
2177 mCaps.setCapability(capability, false);
2178 return this;
2179 }
2180
2181 /**
2182 * Sets the owner UID.
2183 *
2184 * The default value is {@link Process#INVALID_UID}. Pass this value to reset.
2185 *
2186 * Note: for security the system will clear out this field when received from a
2187 * non-privileged source.
2188 *
2189 * @param ownerUid the owner UID
2190 * @return this builder
2191 */
2192 @NonNull
2193 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2194 public Builder setOwnerUid(final int ownerUid) {
2195 mCaps.setOwnerUid(ownerUid);
2196 return this;
2197 }
2198
2199 /**
2200 * Sets the list of UIDs that are administrators of this network.
2201 *
2202 * <p>UIDs included in administratorUids gain administrator privileges over this
2203 * Network. Examples of UIDs that should be included in administratorUids are:
2204 * <ul>
2205 * <li>Carrier apps with privileges for the relevant subscription
2206 * <li>Active VPN apps
2207 * <li>Other application groups with a particular Network-related role
2208 * </ul>
2209 *
2210 * <p>In general, user-supplied networks (such as WiFi networks) do not have
2211 * administrators.
2212 *
2213 * <p>An app is granted owner privileges over Networks that it supplies. The owner
2214 * UID MUST always be included in administratorUids.
2215 *
2216 * The default value is the empty array. Pass an empty array to reset.
2217 *
2218 * Note: for security the system will clear out this field when received from a
2219 * non-privileged source, such as an app using reflection to call this or
2220 * mutate the member in the built object.
2221 *
2222 * @param administratorUids the UIDs to be set as administrators of this Network.
2223 * @return this builder
2224 */
2225 @NonNull
2226 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2227 public Builder setAdministratorUids(@NonNull final int[] administratorUids) {
2228 Objects.requireNonNull(administratorUids);
2229 mCaps.setAdministratorUids(administratorUids);
2230 return this;
2231 }
2232
2233 /**
2234 * Sets the upstream bandwidth of the link.
2235 *
2236 * Sets the upstream 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 upKbps the estimated first hop upstream (device to network) bandwidth.
2248 * @return this builder
2249 */
2250 @NonNull
2251 public Builder setLinkUpstreamBandwidthKbps(final int upKbps) {
2252 mCaps.setLinkUpstreamBandwidthKbps(upKbps);
2253 return this;
2254 }
2255
2256 /**
2257 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
2258 * the estimated first hop transport bandwidth.
2259 * <p>
2260 * Note that when used to request a network, this specifies the minimum acceptable.
2261 * When received as the state of an existing network this specifies the typical
2262 * first hop bandwidth expected. This is never measured, but rather is inferred
2263 * from technology type and other link parameters. It could be used to differentiate
2264 * between very slow 1xRTT cellular links and other faster networks or even between
2265 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2266 * fast backhauls and slow backhauls.
2267 *
2268 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
2269 * @return this builder
2270 */
2271 @NonNull
2272 public Builder setLinkDownstreamBandwidthKbps(final int downKbps) {
2273 mCaps.setLinkDownstreamBandwidthKbps(downKbps);
2274 return this;
2275 }
2276
2277 /**
2278 * Sets the optional bearer specific network specifier.
2279 * This has no meaning if a single transport is also not specified, so calling
2280 * this without a single transport set will generate an exception, as will
2281 * subsequently adding or removing transports after this is set.
2282 * </p>
2283 *
2284 * @param specifier a concrete, parcelable framework class that extends NetworkSpecifier,
2285 * or null to clear it.
2286 * @return this builder
2287 */
2288 @NonNull
2289 public Builder setNetworkSpecifier(@Nullable final NetworkSpecifier specifier) {
2290 mCaps.setNetworkSpecifier(specifier);
2291 return this;
2292 }
2293
2294 /**
2295 * Sets the optional transport specific information.
2296 *
2297 * @param info A concrete, parcelable framework class that extends {@link TransportInfo},
2298 * or null to clear it.
2299 * @return this builder
2300 */
2301 @NonNull
2302 public Builder setTransportInfo(@Nullable final TransportInfo info) {
2303 mCaps.setTransportInfo(info);
2304 return this;
2305 }
2306
2307 /**
2308 * Sets the signal strength. This is a signed integer, with higher values indicating a
2309 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the
2310 * same RSSI units reported by wifi code.
2311 * <p>
2312 * Note that when used to register a network callback, this specifies the minimum
2313 * acceptable signal strength. When received as the state of an existing network it
2314 * specifies the current value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means
2315 * no value when received and has no effect when requesting a callback.
2316 *
2317 * Note: for security the system will throw if it receives a NetworkRequest where
2318 * the underlying NetworkCapabilities has this member set from a source that does
2319 * not hold the {@link android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP}
2320 * permission. Apps with this permission can use this indirectly through
2321 * {@link android.net.NetworkRequest}.
2322 *
2323 * @param signalStrength the bearer-specific signal strength.
2324 * @return this builder
2325 */
2326 @NonNull
2327 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP)
2328 public Builder setSignalStrength(final int signalStrength) {
2329 mCaps.setSignalStrength(signalStrength);
2330 return this;
2331 }
2332
2333 /**
2334 * Sets the SSID of this network.
2335 *
2336 * Note: for security the system will clear out this field when received from a
2337 * non-privileged source, like an app using reflection to set this.
2338 *
2339 * @param ssid the SSID, or null to clear it.
2340 * @return this builder
2341 */
2342 @NonNull
2343 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2344 public Builder setSsid(@Nullable final String ssid) {
2345 mCaps.setSSID(ssid);
2346 return this;
2347 }
2348
2349 /**
2350 * Set the uid of the app causing this network to exist.
2351 *
2352 * Note: for security the system will clear out this field when received from a
2353 * non-privileged source.
2354 *
2355 * @param uid UID of the app.
2356 * @return this builder
2357 */
2358 @NonNull
2359 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2360 public Builder setRequestorUid(final int uid) {
2361 mCaps.setRequestorUid(uid);
2362 return this;
2363 }
2364
2365 /**
2366 * Set the package name of the app causing this network to exist.
2367 *
2368 * Note: for security the system will clear out this field when received from a
2369 * non-privileged source.
2370 *
2371 * @param packageName package name of the app, or null to clear it.
2372 * @return this builder
2373 */
2374 @NonNull
2375 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2376 public Builder setRequestorPackageName(@Nullable final String packageName) {
2377 mCaps.setRequestorPackageName(packageName);
2378 return this;
2379 }
2380
2381 /**
2382 * Builds the instance of the capabilities.
2383 *
2384 * @return the built instance of NetworkCapabilities.
2385 */
2386 @NonNull
2387 public NetworkCapabilities build() {
2388 if (mCaps.getOwnerUid() != Process.INVALID_UID) {
2389 if (!ArrayUtils.contains(mCaps.getAdministratorUids(), mCaps.getOwnerUid())) {
2390 throw new IllegalStateException("The owner UID must be included in "
2391 + " administrator UIDs.");
2392 }
2393 }
2394 return new NetworkCapabilities(mCaps);
2395 }
2396 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07002397}