blob: 9ff7ebee6da44d9147d20981f97f9ab059c4f6be [file] [log] [blame]
Robert Greenwalt1448f052014-04-08 13:41:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Chalard Jean981dcca2020-02-06 18:31:19 +090019import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
20
Jeff Sharkeyde570312017-10-24 21:25:50 -060021import android.annotation.IntDef;
paulhud9736de2019-03-08 16:35:20 +080022import android.annotation.NonNull;
Etan Cohenca9fb562018-11-27 07:32:39 -080023import android.annotation.Nullable;
Chalard Jeane5e38502020-03-18 15:58:50 +090024import android.annotation.RequiresPermission;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070025import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060026import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000027import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060028import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010029import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070030import android.os.Parcel;
31import android.os.Parcelable;
Qingxi Li7cf06622020-01-17 17:54:27 -080032import android.os.Process;
Roshan Piuse38acab2020-01-16 12:17:17 -080033import android.text.TextUtils;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090034import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080035import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070036
37import com.android.internal.annotations.VisibleForTesting;
Chalard Jeane5e38502020-03-18 15:58:50 +090038import com.android.internal.util.ArrayUtils;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090039import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090040import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070041
Jeff Sharkeyde570312017-10-24 21:25:50 -060042import java.lang.annotation.Retention;
43import java.lang.annotation.RetentionPolicy;
Cody Kestingf7ac9962020-03-16 18:15:28 -070044import java.util.Arrays;
Etan Cohena7434272017-04-03 12:17:51 -070045import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090046import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090047import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070048
49/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070050 * Representation of the capabilities of an active network. Instances are
51 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060052 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
53 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060054 * <p>
55 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
56 * network selection. Rather than indicate a need for Wi-Fi because an
57 * application needs high bandwidth and risk obsolescence when a new, fast
58 * network appears (like LTE), the application should specify it needs high
59 * bandwidth. Similarly if an application needs an unmetered network for a bulk
60 * transfer it can specify that rather than assuming all cellular based
61 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070062 */
63public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070064 private static final String TAG = "NetworkCapabilities";
65
lucaslin783f2212019-10-22 18:27:33 +080066 // Set to true when private DNS is broken.
67 private boolean mPrivateDnsBroken;
68
Roshan Piuse38acab2020-01-16 12:17:17 -080069 /**
70 * Uid of the app making the request.
71 */
72 private int mRequestorUid;
73
74 /**
75 * Package name of the app making the request.
76 */
77 private String mRequestorPackageName;
78
Robert Greenwalt01d004e2014-05-18 15:24:21 -070079 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090081 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070082 }
83
84 public NetworkCapabilities(NetworkCapabilities nc) {
85 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090086 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070087 }
88 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070089
90 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 * Completely clears the contents of this object, removing even the capabilities that are set
92 * by default when the object is constructed.
Chalard Jeane5e38502020-03-18 15:58:50 +090093 * @hide
Lorenzo Colittif7058f52015-04-27 11:31:55 +090094 */
95 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080096 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070097 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090098 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080099 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900100 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900101 mUids = null;
Cody Kestingf7ac9962020-03-16 18:15:28 -0700102 mAdministratorUids = new int[0];
Qingxi Li7cf06622020-01-17 17:54:27 -0800103 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +0900104 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +0800105 mPrivateDnsBroken = false;
Roshan Piuse38acab2020-01-16 12:17:17 -0800106 mRequestorUid = Process.INVALID_UID;
107 mRequestorPackageName = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900108 }
109
110 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +0900111 * Set all contents of this object to the contents of a NetworkCapabilities.
112 * @hide
113 */
paulhud9736de2019-03-08 16:35:20 +0800114 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900115 mNetworkCapabilities = nc.mNetworkCapabilities;
116 mTransportTypes = nc.mTransportTypes;
117 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
118 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
119 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800120 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900121 mSignalStrength = nc.mSignalStrength;
122 setUids(nc.mUids); // Will make the defensive copy
Chalard Jean981dcca2020-02-06 18:31:19 +0900123 setAdministratorUids(nc.getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -0800124 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900125 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
126 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800127 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Roshan Piuse38acab2020-01-16 12:17:17 -0800128 mRequestorUid = nc.mRequestorUid;
129 mRequestorPackageName = nc.mRequestorPackageName;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900130 }
131
132 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 * Represents the network's capabilities. If any are specified they will be satisfied
134 * by any Network that matches all of them.
135 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100136 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900137 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800139 /**
140 * If any capabilities specified here they must not exist in the matching Network.
141 */
142 private long mUnwantedNetworkCapabilities;
143
Jeff Sharkeyde570312017-10-24 21:25:50 -0600144 /** @hide */
145 @Retention(RetentionPolicy.SOURCE)
146 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
147 NET_CAPABILITY_MMS,
148 NET_CAPABILITY_SUPL,
149 NET_CAPABILITY_DUN,
150 NET_CAPABILITY_FOTA,
151 NET_CAPABILITY_IMS,
152 NET_CAPABILITY_CBS,
153 NET_CAPABILITY_WIFI_P2P,
154 NET_CAPABILITY_IA,
155 NET_CAPABILITY_RCS,
156 NET_CAPABILITY_XCAP,
157 NET_CAPABILITY_EIMS,
158 NET_CAPABILITY_NOT_METERED,
159 NET_CAPABILITY_INTERNET,
160 NET_CAPABILITY_NOT_RESTRICTED,
161 NET_CAPABILITY_TRUSTED,
162 NET_CAPABILITY_NOT_VPN,
163 NET_CAPABILITY_VALIDATED,
164 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600165 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600166 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900167 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900168 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800169 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800170 NET_CAPABILITY_MCX,
171 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600172 })
173 public @interface NetCapability { }
174
Robert Greenwalt1448f052014-04-08 13:41:39 -0700175 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700176 * Indicates this is a network that has the ability to reach the
177 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700178 */
179 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach the carrier's
183 * SUPL server, used to retrieve GPS information.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach the carrier's
189 * DUN or tethering gateway.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach the carrier's
195 * FOTA portal, used for over the air updates.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Indicates this is a network that has the ability to reach the carrier's
201 * IMS servers, used for network registration and signaling.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates this is a network that has the ability to reach the carrier's
207 * CBS servers, used for carrier specific services.
208 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700209 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700210
211 /**
212 * Indicates this is a network that has the ability to reach a Wi-Fi direct
213 * peer.
214 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216
217 /**
218 * Indicates this is a network that has the ability to reach a carrier's
219 * Initial Attach servers.
220 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700221 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700222
223 /**
224 * Indicates this is a network that has the ability to reach a carrier's
225 * RCS servers, used for Rich Communication Services.
226 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700227 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700228
229 /**
230 * Indicates this is a network that has the ability to reach a carrier's
231 * XCAP servers, used for configuration and control.
232 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700233 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700234
235 /**
236 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700237 * Emergency IMS servers or other services, used for network signaling
238 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700239 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700240 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700241
242 /**
243 * Indicates that this network is unmetered.
244 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700245 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700246
247 /**
248 * Indicates that this network should be able to reach the internet.
249 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700250 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700251
252 /**
253 * Indicates that this network is available for general use. If this is not set
254 * applications should not attempt to communicate on this network. Note that this
255 * is simply informative and not enforcement - enforcement is handled via other means.
256 * Set by default.
257 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700258 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
259
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700260 /**
261 * Indicates that the user has indicated implicit trust of this network. This
262 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
263 * BT device or a wifi the user asked to connect to. Untrusted networks
264 * are probably limited to unknown wifi AP. Set by default.
265 */
266 public static final int NET_CAPABILITY_TRUSTED = 14;
267
Paul Jensen76b610a2015-03-18 09:33:07 -0400268 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400269 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400270 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400271 */
272 public static final int NET_CAPABILITY_NOT_VPN = 15;
273
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900274 /**
275 * Indicates that connectivity on this network was successfully validated. For example, for a
276 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
277 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900278 */
279 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700280
Paul Jensen3d194ea2015-06-16 14:27:36 -0400281 /**
282 * Indicates that this network was found to have a captive portal in place last time it was
283 * probed.
284 */
285 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
286
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900287 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600288 * Indicates that this network is not roaming.
289 */
290 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
291
292 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900293 * Indicates that this network is available for use by apps, and not a network that is being
294 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900295 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600296 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900297
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900298 /**
299 * Indicates that this network is not congested.
300 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600301 * When a network is congested, applications should defer network traffic
302 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900303 */
304 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
305
Chalard Jean804b8fb2018-01-30 22:41:41 +0900306 /**
307 * Indicates that this network is not currently suspended.
308 * <p>
309 * When a network is suspended, the network's IP addresses and any connections
310 * established on the network remain valid, but the network is temporarily unable
311 * to transfer data. This can happen, for example, if a cellular network experiences
312 * a temporary loss of signal, such as when driving through a tunnel, etc.
313 * A network with this capability is not suspended, so is expected to be able to
314 * transfer data.
315 */
316 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
317
Pavel Maltsev43403202018-01-30 17:19:44 -0800318 /**
319 * Indicates that traffic that goes through this network is paid by oem. For example,
320 * this network can be used by system apps to upload telemetry data.
321 * @hide
322 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700323 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800324 public static final int NET_CAPABILITY_OEM_PAID = 22;
325
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800326 /**
327 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
328 * servers.
329 */
330 public static final int NET_CAPABILITY_MCX = 23;
331
lucasline252a742019-03-12 13:08:03 +0800332 /**
333 * Indicates that this network was tested to only provide partial connectivity.
334 * @hide
335 */
336 @SystemApi
337 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
338
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800340 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700341
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700342 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900343 * Network capabilities that are expected to be mutable, i.e., can change while a particular
344 * network is connected.
345 */
346 private static final long MUTABLE_CAPABILITIES =
347 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
348 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900349 (1 << NET_CAPABILITY_TRUSTED)
350 | (1 << NET_CAPABILITY_VALIDATED)
351 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
352 | (1 << NET_CAPABILITY_NOT_ROAMING)
353 | (1 << NET_CAPABILITY_FOREGROUND)
354 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800355 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
356 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900357
358 /**
359 * Network capabilities that are not allowed in NetworkRequests. This exists because the
360 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
361 * capability's presence cannot be known in advance. If such a capability is requested, then we
362 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
363 * get immediately torn down because they do not have the requested capability.
364 */
365 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900366 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900367
368 /**
369 * Capabilities that are set by default when the object is constructed.
370 */
371 private static final long DEFAULT_CAPABILITIES =
372 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
373 (1 << NET_CAPABILITY_TRUSTED) |
374 (1 << NET_CAPABILITY_NOT_VPN);
375
376 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400377 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800378 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400379 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700380 @VisibleForTesting
381 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400382 (1 << NET_CAPABILITY_CBS) |
383 (1 << NET_CAPABILITY_DUN) |
384 (1 << NET_CAPABILITY_EIMS) |
385 (1 << NET_CAPABILITY_FOTA) |
386 (1 << NET_CAPABILITY_IA) |
387 (1 << NET_CAPABILITY_IMS) |
388 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800389 (1 << NET_CAPABILITY_XCAP) |
390 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800391
392 /**
393 * Capabilities that force network to be restricted.
394 * {@see #maybeMarkCapabilitiesRestricted}.
395 */
396 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800397 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400398
399 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700400 * Capabilities that suggest that a network is unrestricted.
401 * {@see #maybeMarkCapabilitiesRestricted}.
402 */
403 @VisibleForTesting
404 /* package */ static final long UNRESTRICTED_CAPABILITIES =
405 (1 << NET_CAPABILITY_INTERNET) |
406 (1 << NET_CAPABILITY_MMS) |
407 (1 << NET_CAPABILITY_SUPL) |
408 (1 << NET_CAPABILITY_WIFI_P2P);
409
410 /**
lucasline252a742019-03-12 13:08:03 +0800411 * Capabilities that are managed by ConnectivityService.
412 */
413 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
414 (1 << NET_CAPABILITY_VALIDATED)
415 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
416 | (1 << NET_CAPABILITY_FOREGROUND)
417 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
418
419 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000420 * Capabilities that are allowed for test networks. This list must be set so that it is safe
421 * for an unprivileged user to create a network with these capabilities via shell. As such,
422 * it must never contain capabilities that are generally useful to the system, such as
423 * INTERNET, IMS, SUPL, etc.
424 */
425 private static final long TEST_NETWORKS_ALLOWED_CAPABILITIES =
426 (1 << NET_CAPABILITY_NOT_METERED)
427 | (1 << NET_CAPABILITY_NOT_RESTRICTED)
428 | (1 << NET_CAPABILITY_NOT_VPN)
429 | (1 << NET_CAPABILITY_NOT_ROAMING)
430 | (1 << NET_CAPABILITY_NOT_CONGESTED)
431 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
432
433 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700434 * Adds the given capability to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900435 * Note that when searching for a network to satisfy a request, all capabilities
436 * requested must be satisfied.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700437 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600438 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900439 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900440 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700441 */
paulhud9736de2019-03-08 16:35:20 +0800442 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800443 // If the given capability was previously added to the list of unwanted capabilities
444 // then the capability will also be removed from the list of unwanted capabilities.
445 // TODO: Consider adding unwanted capabilities to the public API and mention this
446 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800447 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700448 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800449 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700450 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700451 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452
453 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800454 * Adds the given capability to the list of unwanted capabilities of this
Chalard Jeane5e38502020-03-18 15:58:50 +0900455 * {@code NetworkCapability} instance. Note that when searching for a network to
456 * satisfy a request, the network must not contain any capability from unwanted capability
457 * list.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800458 * <p>
459 * If the capability was previously added to the list of required capabilities (for
460 * example, it was there by default or added using {@link #addCapability(int)} method), then
461 * it will be removed from the list of required capabilities as well.
462 *
463 * @see #addCapability(int)
464 * @hide
465 */
466 public void addUnwantedCapability(@NetCapability int capability) {
467 checkValidCapability(capability);
468 mUnwantedNetworkCapabilities |= 1 << capability;
469 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
470 }
471
472 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
474 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600475 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900476 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900477 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478 */
paulhud9736de2019-03-08 16:35:20 +0800479 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800480 // Note that this method removes capabilities that were added via addCapability(int),
481 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800482 checkValidCapability(capability);
483 final long mask = ~(1 << capability);
484 mNetworkCapabilities &= mask;
485 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700486 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700487 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700488
489 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600490 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
491 * instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900492 * @hide
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600493 */
paulhud9736de2019-03-08 16:35:20 +0800494 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
495 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600496 if (value) {
497 addCapability(capability);
498 } else {
499 removeCapability(capability);
500 }
501 return this;
502 }
503
504 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700505 * Gets all the capabilities set on this {@code NetworkCapability} instance.
506 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600507 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700508 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700509 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000510 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600511 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600512 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900513 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700514 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515
516 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800517 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
518 *
519 * @return an array of unwanted capability values for this instance.
520 * @hide
521 */
522 public @NetCapability int[] getUnwantedCapabilities() {
523 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
524 }
525
526
527 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600528 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700529 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600530 *
531 * @hide
532 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800533 public void setCapabilities(@NetCapability int[] capabilities,
534 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600535 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800536 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600537 }
538
539 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800540 * @deprecated use {@link #setCapabilities(int[], int[])}
541 * @hide
542 */
543 @Deprecated
544 public void setCapabilities(@NetCapability int[] capabilities) {
545 setCapabilities(capabilities, new int[] {});
546 }
547
548 /**
549 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700550 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600551 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700552 * @return {@code true} if set on this instance.
553 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600554 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800555 return isValidCapability(capability)
556 && ((mNetworkCapabilities & (1 << capability)) != 0);
557 }
558
559 /** @hide */
560 public boolean hasUnwantedCapability(@NetCapability int capability) {
561 return isValidCapability(capability)
562 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700563 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700564
lucasline252a742019-03-12 13:08:03 +0800565 /**
566 * Check if this NetworkCapabilities has system managed capabilities or not.
567 * @hide
568 */
569 public boolean hasConnectivityManagedCapability() {
570 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
571 }
572
Pavel Maltseve18ef262018-03-07 11:13:04 -0800573 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800574 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800576 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 }
578
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900579 /**
580 * Convenience function that returns a human-readable description of the first mutable
581 * capability we find. Used to present an error message to apps that request mutable
582 * capabilities.
583 *
584 * @hide
585 */
paulhud9736de2019-03-08 16:35:20 +0800586 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800587 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
588 & NON_REQUESTABLE_CAPABILITIES;
589
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900590 if (nonRequestable != 0) {
591 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900592 }
593 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900594 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800595 if (isPrivateDnsBroken()) {
596 return "privateDnsBroken";
597 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900598 return null;
599 }
600
paulhud9736de2019-03-08 16:35:20 +0800601 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
602 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800603 long requestedCapabilities = mNetworkCapabilities;
604 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
605 long providedCapabilities = nc.mNetworkCapabilities;
606
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900607 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800608 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
609 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900610 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800611 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
612 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613 }
614
Robert Greenwalt06314e42014-10-29 14:04:06 -0700615 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800616 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800617 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
618 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700619 }
620
paulhud9736de2019-03-08 16:35:20 +0800621 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900622 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800623 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
624 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
625 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900626 }
627
Robert Greenwalt1448f052014-04-08 13:41:39 -0700628 /**
paulhu18354322020-01-09 17:08:11 +0800629 * Deduces that all the capabilities it provides are typically provided by restricted networks
630 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400631 *
paulhu18354322020-01-09 17:08:11 +0800632 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400633 * @hide
634 */
paulhu18354322020-01-09 17:08:11 +0800635 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800636 // Check if we have any capability that forces the network to be restricted.
637 final boolean forceRestrictedCapability =
638 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
639
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700640 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800641 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700642 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800643 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700644
645 // Must have at least some restricted capabilities.
646 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800647 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700648
paulhu18354322020-01-09 17:08:11 +0800649 return forceRestrictedCapability
650 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
651 }
652
653 /**
654 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
655 *
656 * @hide
657 */
658 public void maybeMarkCapabilitiesRestricted() {
659 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400660 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400661 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400662 }
663
664 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000665 * Test networks have strong restrictions on what capabilities they can have. Enforce these
666 * restrictions.
667 * @hide
668 */
669 public void restrictCapabilitesForTestNetwork() {
670 final long originalCapabilities = mNetworkCapabilities;
671 final NetworkSpecifier originalSpecifier = mNetworkSpecifier;
672 clearAll();
673 // Reset the transports to only contain TRANSPORT_TEST.
674 mTransportTypes = (1 << TRANSPORT_TEST);
675 mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES;
676 mNetworkSpecifier = originalSpecifier;
677 }
678
679 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700680 * Representing the transport type. Apps should generally not care about transport. A
681 * request for a fast internet connection could be satisfied by a number of different
682 * transports. If any are specified here it will be satisfied a Network that matches
683 * any of them. If a caller doesn't care about the transport it should not specify any.
684 */
685 private long mTransportTypes;
686
Jeff Sharkeyde570312017-10-24 21:25:50 -0600687 /** @hide */
688 @Retention(RetentionPolicy.SOURCE)
689 @IntDef(prefix = { "TRANSPORT_" }, value = {
690 TRANSPORT_CELLULAR,
691 TRANSPORT_WIFI,
692 TRANSPORT_BLUETOOTH,
693 TRANSPORT_ETHERNET,
694 TRANSPORT_VPN,
695 TRANSPORT_WIFI_AWARE,
696 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800697 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600698 })
699 public @interface Transport { }
700
Robert Greenwalt1448f052014-04-08 13:41:39 -0700701 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700702 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700703 */
704 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700705
706 /**
707 * Indicates this network uses a Wi-Fi transport.
708 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700709 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700710
711 /**
712 * Indicates this network uses a Bluetooth transport.
713 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700715
716 /**
717 * Indicates this network uses an Ethernet transport.
718 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700719 public static final int TRANSPORT_ETHERNET = 3;
720
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400721 /**
722 * Indicates this network uses a VPN transport.
723 */
724 public static final int TRANSPORT_VPN = 4;
725
Etan Cohen305ea282016-06-20 09:27:12 -0700726 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700727 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700728 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700729 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700730
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700731 /**
732 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700733 */
734 public static final int TRANSPORT_LOWPAN = 6;
735
Benedict Wong89ce5e32018-11-14 17:40:55 -0800736 /**
737 * Indicates this network uses a Test-only virtual interface as a transport.
738 *
739 * @hide
740 */
741 @TestApi
742 public static final int TRANSPORT_TEST = 7;
743
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900744 /** @hide */
745 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
746 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800747 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700748
Hugo Benichi16f0a942017-06-20 14:07:59 +0900749 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600750 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900751 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
752 }
753
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900754 private static final String[] TRANSPORT_NAMES = {
755 "CELLULAR",
756 "WIFI",
757 "BLUETOOTH",
758 "ETHERNET",
759 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700760 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800761 "LOWPAN",
762 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900763 };
764
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700765 /**
766 * Adds the given transport type to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900767 * Multiple transports may be applied. Note that when searching
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700768 * for a network to satisfy a request, any listed in the request will satisfy the request.
769 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
770 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
771 * to be selected. This is logically different than
772 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
773 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600774 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900775 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900776 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700777 */
paulhud9736de2019-03-08 16:35:20 +0800778 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900779 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700781 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700782 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700783 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700784
785 /**
786 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
787 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600788 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900789 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700790 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700791 */
paulhud9736de2019-03-08 16:35:20 +0800792 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900793 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700794 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700795 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700796 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700798
799 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600800 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
801 * instance.
802 *
803 * @hide
804 */
paulhud9736de2019-03-08 16:35:20 +0800805 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
806 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600807 if (value) {
808 addTransportType(transportType);
809 } else {
810 removeTransportType(transportType);
811 }
812 return this;
813 }
814
815 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700816 * Gets all the transports set on this {@code NetworkCapability} instance.
817 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600818 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700819 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700820 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600821 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900822 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800823 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900824 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700825 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700826
827 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600828 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700829 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600830 *
831 * @hide
832 */
833 public void setTransportTypes(@Transport int[] transportTypes) {
834 mTransportTypes = BitUtils.packBits(transportTypes);
835 }
836
837 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700838 * Tests for the presence of a transport on this instance.
839 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600840 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700841 * @return {@code true} if set on this instance.
842 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600843 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900844 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700845 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700846
847 private void combineTransportTypes(NetworkCapabilities nc) {
848 this.mTransportTypes |= nc.mTransportTypes;
849 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900850
Robert Greenwalt1448f052014-04-08 13:41:39 -0700851 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
852 return ((this.mTransportTypes == 0) ||
853 ((this.mTransportTypes & nc.mTransportTypes) != 0));
854 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900855
Robert Greenwalt06314e42014-10-29 14:04:06 -0700856 /** @hide */
857 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700858 return (nc.mTransportTypes == this.mTransportTypes);
859 }
860
861 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800862 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900863 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800864 * <p>This field keeps track of the UID of the app that created this network and is in charge of
865 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
866 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800867 *
868 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
869 * reset to Process.INVALID_UID unless all the following conditions are met:
870 *
871 * <ol>
872 * <li>The destination app is the network owner
873 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
874 * <li>The user's location toggle is on
875 * </ol>
876 *
877 * This is because the owner UID is location-sensitive. The apps that request a network could
878 * know where the device is if they can tell for sure the system has connected to the network
879 * they requested.
880 *
881 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
882 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
883 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900884 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800885 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900886
887 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800888 * Set the UID of the owner app.
Chalard Jeane5e38502020-03-18 15:58:50 +0900889 * @hide
Chalard Jeanf474fc32018-01-17 15:10:05 +0900890 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800891 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800892 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800893 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900894 }
895
Qingxi Li7cf06622020-01-17 17:54:27 -0800896 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800897 * Retrieves the UID of the app that owns this network.
898 *
899 * <p>For user privacy reasons, this field will only be populated if:
900 *
901 * <ol>
902 * <li>The calling app is the network owner
903 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
904 * <li>The user's location toggle is on
905 * </ol>
906 *
Chalard Jeane5e38502020-03-18 15:58:50 +0900907 * Instances of NetworkCapabilities sent to apps without the appropriate permissions will
908 * have this field cleared out.
Qingxi Li7cf06622020-01-17 17:54:27 -0800909 */
910 public int getOwnerUid() {
911 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000912 }
913
Chalard Jeanf474fc32018-01-17 15:10:05 +0900914 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800915 * UIDs of packages that are administrators of this network, or empty if none.
916 *
917 * <p>This field tracks the UIDs of packages that have permission to manage this network.
918 *
919 * <p>Network owners will also be listed as administrators.
920 *
921 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
922 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
923 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
Chalard Jean981dcca2020-02-06 18:31:19 +0900924 *
925 * <p>When received from an app in a NetworkRequest this is always cleared out by the system
926 * server. This field is never used for matching NetworkRequests to NetworkAgents.
Cody Kesting201fc132020-01-17 11:58:36 -0800927 */
Cody Kesting919385b2020-03-18 15:22:12 -0700928 @NonNull private int[] mAdministratorUids = new int[0];
Cody Kesting201fc132020-01-17 11:58:36 -0800929
930 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700931 * Sets the int[] of UIDs that are administrators of this network.
Cody Kesting201fc132020-01-17 11:58:36 -0800932 *
933 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
934 * Examples of UIDs that should be included in administratorUids are:
Chalard Jean981dcca2020-02-06 18:31:19 +0900935 *
Cody Kesting201fc132020-01-17 11:58:36 -0800936 * <ul>
Chalard Jean981dcca2020-02-06 18:31:19 +0900937 * <li>Carrier apps with privileges for the relevant subscription
938 * <li>Active VPN apps
939 * <li>Other application groups with a particular Network-related role
Cody Kesting201fc132020-01-17 11:58:36 -0800940 * </ul>
941 *
942 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
943 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800944 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
945 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800946 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900947 * <p>The administrator UIDs are set by network agents.
948 *
Cody Kesting201fc132020-01-17 11:58:36 -0800949 * @param administratorUids the UIDs to be set as administrators of this Network.
Cody Kesting93c1e652020-03-24 11:53:30 -0700950 * @throws IllegalArgumentException if duplicate UIDs are contained in administratorUids
Chalard Jean981dcca2020-02-06 18:31:19 +0900951 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800952 * @hide
953 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800954 @NonNull
Cody Kestingf7ac9962020-03-16 18:15:28 -0700955 public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) {
956 mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length);
Cody Kesting93c1e652020-03-24 11:53:30 -0700957 Arrays.sort(mAdministratorUids);
958 for (int i = 0; i < mAdministratorUids.length - 1; i++) {
959 if (mAdministratorUids[i] >= mAdministratorUids[i + 1]) {
960 throw new IllegalArgumentException("All administrator UIDs must be unique");
961 }
962 }
Roshan Piuse38acab2020-01-16 12:17:17 -0800963 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800964 }
965
966 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700967 * Retrieves the UIDs that are administrators of this Network.
Cody Kesting201fc132020-01-17 11:58:36 -0800968 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900969 * <p>This is only populated in NetworkCapabilities objects that come from network agents for
970 * networks that are managed by specific apps on the system, such as carrier privileged apps or
971 * wifi suggestion apps. This will include the network owner.
972 *
Cody Kestingf7ac9962020-03-16 18:15:28 -0700973 * @return the int[] of UIDs that are administrators of this Network
Chalard Jean981dcca2020-02-06 18:31:19 +0900974 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800975 * @hide
976 */
977 @NonNull
978 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +0900979 @TestApi
Cody Kestingf7ac9962020-03-16 18:15:28 -0700980 public int[] getAdministratorUids() {
981 return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
Cody Kesting201fc132020-01-17 11:58:36 -0800982 }
983
984 /**
Chalard Jean981dcca2020-02-06 18:31:19 +0900985 * Tests if the set of administrator UIDs of this network is the same as that of the passed one.
986 *
987 * <p>The administrator UIDs must be in sorted order.
988 *
989 * <p>nc is assumed non-null. Else, NPE.
990 *
991 * @hide
992 */
993 @VisibleForTesting(visibility = PRIVATE)
994 public boolean equalsAdministratorUids(@NonNull final NetworkCapabilities nc) {
995 return Arrays.equals(mAdministratorUids, nc.mAdministratorUids);
996 }
997
998 /**
999 * Combine the administrator UIDs of the capabilities.
1000 *
1001 * <p>This is only legal if either of the administrators lists are empty, or if they are equal.
1002 * Combining administrator UIDs is only possible for combining non-overlapping sets of UIDs.
1003 *
1004 * <p>If both administrator lists are non-empty but not equal, they conflict with each other. In
1005 * this case, it would not make sense to add them together.
1006 */
1007 private void combineAdministratorUids(@NonNull final NetworkCapabilities nc) {
1008 if (nc.mAdministratorUids.length == 0) return;
1009 if (mAdministratorUids.length == 0) {
1010 mAdministratorUids = Arrays.copyOf(nc.mAdministratorUids, nc.mAdministratorUids.length);
1011 return;
1012 }
1013 if (!equalsAdministratorUids(nc)) {
1014 throw new IllegalStateException("Can't combine two different administrator UID lists");
1015 }
1016 }
1017
1018 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001019 * Value indicating that link bandwidth is unspecified.
1020 * @hide
1021 */
1022 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
1023
1024 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001025 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
1026 * for the first hop on the given transport. It is not measured, but may take into account
1027 * link parameters (Radio technology, allocated channels, etc).
1028 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001029 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
1030 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001031
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001032 /**
1033 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
1034 * the estimated first hop transport bandwidth.
1035 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001036 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001037 *
1038 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001039 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001040 */
paulhud9736de2019-03-08 16:35:20 +08001041 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001042 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001043 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001044 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001045
1046 /**
1047 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
1048 * the estimated first hop transport bandwidth.
1049 *
1050 * @return The estimated first hop upstream (device to network) bandwidth.
1051 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001052 public int getLinkUpstreamBandwidthKbps() {
1053 return mLinkUpBandwidthKbps;
1054 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001055
1056 /**
1057 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
1058 * the estimated first hop transport bandwidth.
1059 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001060 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001061 *
1062 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001063 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001064 */
paulhud9736de2019-03-08 16:35:20 +08001065 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001066 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001067 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001068 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001069
1070 /**
1071 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
1072 * the estimated first hop transport bandwidth.
1073 *
1074 * @return The estimated first hop downstream (network to device) bandwidth.
1075 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001076 public int getLinkDownstreamBandwidthKbps() {
1077 return mLinkDownBandwidthKbps;
1078 }
1079
1080 private void combineLinkBandwidths(NetworkCapabilities nc) {
1081 this.mLinkUpBandwidthKbps =
1082 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
1083 this.mLinkDownBandwidthKbps =
1084 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1085 }
1086 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1087 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1088 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1089 }
1090 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1091 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1092 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1093 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001094 /** @hide */
1095 public static int minBandwidth(int a, int b) {
1096 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1097 return b;
1098 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1099 return a;
1100 } else {
1101 return Math.min(a, b);
1102 }
1103 }
1104 /** @hide */
1105 public static int maxBandwidth(int a, int b) {
1106 return Math.max(a, b);
1107 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001108
Etan Cohena7434272017-04-03 12:17:51 -07001109 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001110 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001111
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001112 /**
1113 * Sets the optional bearer specific network specifier.
1114 * This has no meaning if a single transport is also not specified, so calling
1115 * this without a single transport set will generate an exception, as will
1116 * subsequently adding or removing transports after this is set.
1117 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001118 *
Etan Cohena7434272017-04-03 12:17:51 -07001119 * @param networkSpecifier A concrete, parcelable framework class that extends
1120 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001121 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +09001122 * @hide
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001123 */
Aaron Huange6b62392019-09-20 22:52:54 +08001124 public @NonNull NetworkCapabilities setNetworkSpecifier(
1125 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001126 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001127 throw new IllegalStateException("Must have a single transport specified to use " +
1128 "setNetworkSpecifier");
1129 }
Etan Cohena7434272017-04-03 12:17:51 -07001130
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001131 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001132
Pierre Imaic8419a82016-03-22 17:54:54 +09001133 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001134 }
1135
1136 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001137 * Sets the optional transport specific information.
1138 *
1139 * @param transportInfo A concrete, parcelable framework class that extends
1140 * {@link TransportInfo}.
1141 * @return This NetworkCapabilities instance, to facilitate chaining.
1142 * @hide
1143 */
Aaron Huange6b62392019-09-20 22:52:54 +08001144 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001145 mTransportInfo = transportInfo;
1146 return this;
1147 }
1148
1149 /**
paulhud9736de2019-03-08 16:35:20 +08001150 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001151 *
Etan Cohena7434272017-04-03 12:17:51 -07001152 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
Chalard Jeane5e38502020-03-18 15:58:50 +09001153 * specifier or {@code null}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001154 */
paulhud9736de2019-03-08 16:35:20 +08001155 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001156 return mNetworkSpecifier;
1157 }
1158
Etan Cohenca9fb562018-11-27 07:32:39 -08001159 /**
1160 * Returns a transport-specific information container. The application may cast this
1161 * container to a concrete sub-class based on its knowledge of the network request. The
1162 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001163 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001164 *
1165 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1166 * available for the network.
1167 */
1168 @Nullable public TransportInfo getTransportInfo() {
1169 return mTransportInfo;
1170 }
1171
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001172 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001173 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001174 throw new IllegalStateException("Can't combine two networkSpecifiers");
1175 }
Etan Cohena7434272017-04-03 12:17:51 -07001176 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001177 }
Etan Cohena7434272017-04-03 12:17:51 -07001178
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001179 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Chalard Jean2da4f9f2020-03-27 17:57:34 +09001180 return mNetworkSpecifier == null || mNetworkSpecifier.canBeSatisfiedBy(nc.mNetworkSpecifier)
Etan Cohena7434272017-04-03 12:17:51 -07001181 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001182 }
Etan Cohena7434272017-04-03 12:17:51 -07001183
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001184 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001185 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001186 }
1187
Etan Cohenca9fb562018-11-27 07:32:39 -08001188 private void combineTransportInfos(NetworkCapabilities nc) {
1189 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1190 throw new IllegalStateException("Can't combine two TransportInfos");
1191 }
1192 setTransportInfo(nc.mTransportInfo);
1193 }
1194
1195 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1196 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1197 }
1198
Robert Greenwalt1448f052014-04-08 13:41:39 -07001199 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001200 * Magic value that indicates no signal strength provided. A request specifying this value is
1201 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001202 */
1203 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1204
1205 /**
1206 * Signal strength. This is a signed integer, and higher values indicate better signal.
1207 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1208 */
paulhud9736de2019-03-08 16:35:20 +08001209 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001210 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001211
1212 /**
1213 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1214 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001215 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001216 * <p>
1217 * Note that when used to register a network callback, this specifies the minimum acceptable
1218 * signal strength. When received as the state of an existing network it specifies the current
1219 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1220 * effect when requesting a callback.
1221 *
1222 * @param signalStrength the bearer-specific signal strength.
Chalard Jeane5e38502020-03-18 15:58:50 +09001223 * @hide
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001224 */
paulhud9736de2019-03-08 16:35:20 +08001225 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001226 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001227 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001228 }
1229
1230 /**
1231 * Returns {@code true} if this object specifies a signal strength.
1232 *
1233 * @hide
1234 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001235 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001236 public boolean hasSignalStrength() {
1237 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1238 }
1239
1240 /**
1241 * Retrieves the signal strength.
1242 *
1243 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001244 */
1245 public int getSignalStrength() {
1246 return mSignalStrength;
1247 }
1248
1249 private void combineSignalStrength(NetworkCapabilities nc) {
1250 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1251 }
1252
1253 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1254 return this.mSignalStrength <= nc.mSignalStrength;
1255 }
1256
1257 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1258 return this.mSignalStrength == nc.mSignalStrength;
1259 }
1260
1261 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001262 * List of UIDs this network applies to. No restriction if null.
1263 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001264 * For networks, mUids represent the list of network this applies to, and null means this
1265 * network applies to all UIDs.
1266 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1267 * must be included in a network so that they match. As an exception to the general rule,
1268 * a null mUids field for requests mean "no requirements" rather than what the general rule
1269 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1270 * of this API expect in practice. A network that must match all UIDs can still be
1271 * expressed with a set ranging the entire set of possible UIDs.
1272 * <p>
1273 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001274 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1275 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1276 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1277 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001278 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001279 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001280 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1281 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001282 * <p>
1283 * Please note that in principle a single app can be associated with multiple UIDs because
1284 * each app will have a different UID when it's run as a different (macro-)user. A single
1285 * macro user can only have a single active VPN app at any given time however.
1286 * <p>
1287 * Also please be aware this class does not try to enforce any normalization on this. Callers
1288 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1289 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1290 * their own (like requiring sortedness or no overlap) they need to enforce it
1291 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1292 * or overlapping ranges are present.
1293 *
1294 * @hide
1295 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001296 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001297
1298 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001299 * Convenience method to set the UIDs this network applies to to a single UID.
1300 * @hide
1301 */
paulhud9736de2019-03-08 16:35:20 +08001302 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001303 final ArraySet<UidRange> identity = new ArraySet<>(1);
1304 identity.add(new UidRange(uid, uid));
1305 setUids(identity);
1306 return this;
1307 }
1308
1309 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001310 * Set the list of UIDs this network applies to.
1311 * This makes a copy of the set so that callers can't modify it after the call.
1312 * @hide
1313 */
paulhud9736de2019-03-08 16:35:20 +08001314 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001315 if (null == uids) {
1316 mUids = null;
1317 } else {
1318 mUids = new ArraySet<>(uids);
1319 }
1320 return this;
1321 }
1322
1323 /**
1324 * Get the list of UIDs this network applies to.
1325 * This returns a copy of the set so that callers can't modify the original object.
1326 * @hide
1327 */
paulhud9736de2019-03-08 16:35:20 +08001328 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001329 return null == mUids ? null : new ArraySet<>(mUids);
1330 }
1331
1332 /**
1333 * Test whether this network applies to this UID.
1334 * @hide
1335 */
1336 public boolean appliesToUid(int uid) {
1337 if (null == mUids) return true;
1338 for (UidRange range : mUids) {
1339 if (range.contains(uid)) {
1340 return true;
1341 }
1342 }
1343 return false;
1344 }
1345
1346 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001347 * 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 +09001348 * <p>
1349 * This test only checks whether equal range objects are in both sets. It will
1350 * return false if the ranges are not exactly the same, even if the covered UIDs
1351 * are for an equivalent result.
1352 * <p>
1353 * Note that this method is not very optimized, which is fine as long as it's not used very
1354 * often.
1355 * <p>
1356 * nc is assumed nonnull.
1357 *
1358 * @hide
1359 */
1360 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001361 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001362 Set<UidRange> comparedUids = nc.mUids;
1363 if (null == comparedUids) return null == mUids;
1364 if (null == mUids) return false;
1365 // Make a copy so it can be mutated to check that all ranges in mUids
1366 // also are in uids.
1367 final Set<UidRange> uids = new ArraySet<>(mUids);
1368 for (UidRange range : comparedUids) {
1369 if (!uids.contains(range)) {
1370 return false;
1371 }
1372 uids.remove(range);
1373 }
1374 return uids.isEmpty();
1375 }
1376
1377 /**
1378 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1379 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001380 * This method is called on the NetworkCapabilities embedded in a request with the
1381 * capabilities of an available network. It checks whether all the UIDs from this listen
1382 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1383 * in the passed nc (representing the UIDs that this network is available to).
1384 * <p>
1385 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001386 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001387 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1388 * can see its own network when it listens for it.
1389 * <p>
1390 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001391 * @see #appliesToUid
1392 * @hide
1393 */
paulhud9736de2019-03-08 16:35:20 +08001394 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001395 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001396 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001397 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001398 if (!nc.appliesToUidRange(requiredRange)) {
1399 return false;
1400 }
1401 }
1402 return true;
1403 }
1404
1405 /**
1406 * Returns whether this network applies to the passed ranges.
1407 * This assumes that to apply, the passed range has to be entirely contained
1408 * within one of the ranges this network applies to. If the ranges are not normalized,
1409 * this method may return false even though all required UIDs are covered because no
1410 * single range contained them all.
1411 * @hide
1412 */
1413 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001414 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001415 if (null == mUids) return true;
1416 for (UidRange uidRange : mUids) {
1417 if (uidRange.containsRange(requiredRange)) {
1418 return true;
1419 }
1420 }
1421 return false;
1422 }
1423
1424 /**
1425 * Combine the UIDs this network currently applies to with the UIDs the passed
1426 * NetworkCapabilities apply to.
1427 * nc is assumed nonnull.
1428 */
paulhud9736de2019-03-08 16:35:20 +08001429 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001430 if (null == nc.mUids || null == mUids) {
1431 mUids = null;
1432 return;
1433 }
1434 mUids.addAll(nc.mUids);
1435 }
1436
Chalard Jeanb03a6222018-04-11 21:09:10 +09001437
1438 /**
1439 * The SSID of the network, or null if not applicable or unknown.
1440 * <p>
1441 * This is filled in by wifi code.
1442 * @hide
1443 */
1444 private String mSSID;
1445
1446 /**
1447 * Sets the SSID of this network.
1448 * @hide
1449 */
paulhud9736de2019-03-08 16:35:20 +08001450 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001451 mSSID = ssid;
1452 return this;
1453 }
1454
1455 /**
1456 * Gets the SSID of this network, or null if none or unknown.
1457 * @hide
1458 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001459 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +09001460 @TestApi
1461 public @Nullable String getSsid() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001462 return mSSID;
1463 }
1464
1465 /**
1466 * Tests if the SSID of this network is the same as the SSID of the passed network.
1467 * @hide
1468 */
paulhud9736de2019-03-08 16:35:20 +08001469 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001470 return Objects.equals(mSSID, nc.mSSID);
1471 }
1472
1473 /**
1474 * Check if the SSID requirements of this object are matched by the passed object.
1475 * @hide
1476 */
paulhud9736de2019-03-08 16:35:20 +08001477 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001478 return mSSID == null || mSSID.equals(nc.mSSID);
1479 }
1480
1481 /**
1482 * Combine SSIDs of the capabilities.
1483 * <p>
1484 * This is only legal if either the SSID of this object is null, or both SSIDs are
1485 * equal.
1486 * @hide
1487 */
paulhud9736de2019-03-08 16:35:20 +08001488 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001489 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1490 throw new IllegalStateException("Can't combine two SSIDs");
1491 }
1492 setSSID(nc.mSSID);
1493 }
1494
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001495 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001496 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1497 * <p>
1498 * Note that this method may break an invariant of having a particular capability in either
1499 * wanted or unwanted lists but never in both. Requests that have the same capability in
1500 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001501 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001502 */
paulhud9736de2019-03-08 16:35:20 +08001503 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001504 combineNetCapabilities(nc);
1505 combineTransportTypes(nc);
1506 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001507 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001508 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001509 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001510 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001511 combineSSIDs(nc);
Roshan Piuse38acab2020-01-16 12:17:17 -08001512 combineRequestor(nc);
Chalard Jean981dcca2020-02-06 18:31:19 +09001513 combineAdministratorUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001514 }
1515
1516 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001517 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1518 *
1519 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1520 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1521 * bandwidth, signal strength, or validation / captive portal status.
1522 *
1523 * @hide
1524 */
1525 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001526 return (nc != null
1527 && satisfiedByNetCapabilities(nc, onlyImmutable)
1528 && satisfiedByTransportTypes(nc)
1529 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1530 && satisfiedBySpecifier(nc)
1531 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001532 && (onlyImmutable || satisfiedByUids(nc))
Roshan Piuse38acab2020-01-16 12:17:17 -08001533 && (onlyImmutable || satisfiedBySSID(nc)))
1534 && (onlyImmutable || satisfiedByRequestor(nc));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001535 }
1536
1537 /**
1538 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1539 *
1540 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1541 *
1542 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001543 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001544 @TestApi
1545 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001546 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001547 return satisfiedByNetworkCapabilities(nc, false);
1548 }
1549
1550 /**
1551 * Check if our immutable 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
1556 */
paulhud9736de2019-03-08 16:35:20 +08001557 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001558 return satisfiedByNetworkCapabilities(nc, true);
1559 }
1560
1561 /**
1562 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001563 * {@code NetworkCapabilities} and return a String describing any difference.
1564 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001565 *
1566 * @hide
1567 */
paulhud9736de2019-03-08 16:35:20 +08001568 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001569 if (that == null) {
1570 return "other NetworkCapabilities was null";
1571 }
1572
1573 StringJoiner joiner = new StringJoiner(", ");
1574
Hugo Benichieae7a222017-07-25 11:40:56 +09001575 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1576 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001577 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001578 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1579 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1580 if (oldImmutableCapabilities != newImmutableCapabilities) {
1581 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1582 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1583 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1584 }
1585
1586 if (!equalsSpecifier(that)) {
1587 NetworkSpecifier before = this.getNetworkSpecifier();
1588 NetworkSpecifier after = that.getNetworkSpecifier();
1589 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1590 }
1591
1592 if (!equalsTransportTypes(that)) {
1593 String before = transportNamesOf(this.getTransportTypes());
1594 String after = transportNamesOf(that.getTransportTypes());
1595 joiner.add(String.format("transports changed: %s -> %s", before, after));
1596 }
1597
1598 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001599 }
1600
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001601 /**
1602 * Checks that our requestable capabilities are the same as those of the given
1603 * {@code NetworkCapabilities}.
1604 *
1605 * @hide
1606 */
paulhud9736de2019-03-08 16:35:20 +08001607 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001608 if (nc == null) return false;
1609 return (equalsNetCapabilitiesRequestable(nc) &&
1610 equalsTransportTypes(nc) &&
1611 equalsSpecifier(nc));
1612 }
1613
Robert Greenwalt1448f052014-04-08 13:41:39 -07001614 @Override
paulhud9736de2019-03-08 16:35:20 +08001615 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001616 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001617 NetworkCapabilities that = (NetworkCapabilities) obj;
Roshan Piuse38acab2020-01-16 12:17:17 -08001618 return equalsNetCapabilities(that)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001619 && equalsTransportTypes(that)
1620 && equalsLinkBandwidths(that)
1621 && equalsSignalStrength(that)
1622 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001623 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001624 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001625 && equalsSSID(that)
Roshan Piuse38acab2020-01-16 12:17:17 -08001626 && equalsPrivateDnsBroken(that)
Chalard Jean981dcca2020-02-06 18:31:19 +09001627 && equalsRequestor(that)
1628 && equalsAdministratorUids(that);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001629 }
1630
1631 @Override
1632 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001633 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001634 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001635 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1636 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1637 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1638 + ((int) (mTransportTypes >> 32) * 13)
1639 + (mLinkUpBandwidthKbps * 17)
1640 + (mLinkDownBandwidthKbps * 19)
1641 + Objects.hashCode(mNetworkSpecifier) * 23
1642 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001643 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001644 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001645 + Objects.hashCode(mTransportInfo) * 41
Roshan Piuse38acab2020-01-16 12:17:17 -08001646 + Objects.hashCode(mPrivateDnsBroken) * 43
1647 + Objects.hashCode(mRequestorUid) * 47
Chalard Jean981dcca2020-02-06 18:31:19 +09001648 + Objects.hashCode(mRequestorPackageName) * 53
1649 + Arrays.hashCode(mAdministratorUids) * 59;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001650 }
1651
Wink Saville4e2dea72014-09-20 11:04:03 -07001652 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001653 public int describeContents() {
1654 return 0;
1655 }
Cody Kesting201fc132020-01-17 11:58:36 -08001656
Wink Saville4e2dea72014-09-20 11:04:03 -07001657 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001658 public void writeToParcel(Parcel dest, int flags) {
1659 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001660 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001661 dest.writeLong(mTransportTypes);
1662 dest.writeInt(mLinkUpBandwidthKbps);
1663 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001664 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001665 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001666 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001667 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001668 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001669 dest.writeBoolean(mPrivateDnsBroken);
Chalard Jean981dcca2020-02-06 18:31:19 +09001670 dest.writeIntArray(getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -08001671 dest.writeInt(mOwnerUid);
Roshan Piuse38acab2020-01-16 12:17:17 -08001672 dest.writeInt(mRequestorUid);
1673 dest.writeString(mRequestorPackageName);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001674 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001675
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001676 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001677 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001678 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001679 public NetworkCapabilities createFromParcel(Parcel in) {
1680 NetworkCapabilities netCap = new NetworkCapabilities();
1681
1682 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001683 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001684 netCap.mTransportTypes = in.readLong();
1685 netCap.mLinkUpBandwidthKbps = in.readInt();
1686 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001687 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001688 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001689 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001690 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1691 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001692 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001693 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kestingf7ac9962020-03-16 18:15:28 -07001694 netCap.setAdministratorUids(in.createIntArray());
Qingxi Li7cf06622020-01-17 17:54:27 -08001695 netCap.mOwnerUid = in.readInt();
Roshan Piuse38acab2020-01-16 12:17:17 -08001696 netCap.mRequestorUid = in.readInt();
1697 netCap.mRequestorPackageName = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001698 return netCap;
1699 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001700 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001701 public NetworkCapabilities[] newArray(int size) {
1702 return new NetworkCapabilities[size];
1703 }
1704 };
1705
Wink Saville4e2dea72014-09-20 11:04:03 -07001706 @Override
paulhud9736de2019-03-08 16:35:20 +08001707 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001708 final StringBuilder sb = new StringBuilder("[");
1709 if (0 != mTransportTypes) {
1710 sb.append(" Transports: ");
1711 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1712 NetworkCapabilities::transportNameOf, "|");
1713 }
1714 if (0 != mNetworkCapabilities) {
1715 sb.append(" Capabilities: ");
1716 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1717 NetworkCapabilities::capabilityNameOf, "&");
1718 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001719 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001720 sb.append(" Unwanted: ");
1721 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1722 NetworkCapabilities::capabilityNameOf, "&");
1723 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001724 if (mLinkUpBandwidthKbps > 0) {
1725 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1726 }
1727 if (mLinkDownBandwidthKbps > 0) {
1728 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1729 }
1730 if (mNetworkSpecifier != null) {
1731 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1732 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001733 if (mTransportInfo != null) {
1734 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1735 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001736 if (hasSignalStrength()) {
1737 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001738 }
1739
Chalard Jean07ace0f2018-02-26 19:00:45 +09001740 if (null != mUids) {
1741 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1742 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1743 } else {
1744 sb.append(" Uids: <").append(mUids).append(">");
1745 }
1746 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001747 if (mOwnerUid != Process.INVALID_UID) {
1748 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001749 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001750
Cody Kestingf7ac9962020-03-16 18:15:28 -07001751 if (mAdministratorUids.length == 0) {
1752 sb.append(" AdministratorUids: ").append(Arrays.toString(mAdministratorUids));
Cody Kesting201fc132020-01-17 11:58:36 -08001753 }
1754
Chalard Jeanb03a6222018-04-11 21:09:10 +09001755 if (null != mSSID) {
1756 sb.append(" SSID: ").append(mSSID);
1757 }
1758
lucaslin783f2212019-10-22 18:27:33 +08001759 if (mPrivateDnsBroken) {
1760 sb.append(" Private DNS is broken");
1761 }
1762
Roshan Piuse38acab2020-01-16 12:17:17 -08001763 sb.append(" RequestorUid: ").append(mRequestorUid);
1764 sb.append(" RequestorPackageName: ").append(mRequestorPackageName);
1765
Chalard Jean07ace0f2018-02-26 19:00:45 +09001766 sb.append("]");
1767 return sb.toString();
1768 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001769
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001770
Chalard Jean07ace0f2018-02-26 19:00:45 +09001771 private interface NameOf {
1772 String nameOf(int value);
1773 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001774
Chalard Jean07ace0f2018-02-26 19:00:45 +09001775 /**
1776 * @hide
1777 */
paulhud9736de2019-03-08 16:35:20 +08001778 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1779 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001780 int bitPos = 0;
1781 boolean firstElementAdded = false;
1782 while (bitMask != 0) {
1783 if ((bitMask & 1) != 0) {
1784 if (firstElementAdded) {
1785 sb.append(separator);
1786 } else {
1787 firstElementAdded = true;
1788 }
1789 sb.append(nameFetcher.nameOf(bitPos));
1790 }
1791 bitMask >>= 1;
1792 ++bitPos;
1793 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001794 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001795
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001796 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001797 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001798 final long token = proto.start(fieldId);
1799
1800 for (int transport : getTransportTypes()) {
1801 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1802 }
1803
1804 for (int capability : getCapabilities()) {
1805 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1806 }
1807
1808 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1809 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1810
1811 if (mNetworkSpecifier != null) {
1812 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1813 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001814 if (mTransportInfo != null) {
1815 // TODO b/120653863: write transport-specific info to proto?
1816 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001817
1818 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1819 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1820
1821 proto.end(token);
1822 }
1823
Hugo Benichi5df9d722016-04-25 17:16:35 +09001824 /**
1825 * @hide
1826 */
paulhud9736de2019-03-08 16:35:20 +08001827 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001828 StringJoiner joiner = new StringJoiner("|");
1829 if (capabilities != null) {
1830 for (int c : capabilities) {
1831 joiner.add(capabilityNameOf(c));
1832 }
1833 }
1834 return joiner.toString();
1835 }
1836
1837 /**
1838 * @hide
1839 */
paulhud9736de2019-03-08 16:35:20 +08001840 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001841 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001842 case NET_CAPABILITY_MMS: return "MMS";
1843 case NET_CAPABILITY_SUPL: return "SUPL";
1844 case NET_CAPABILITY_DUN: return "DUN";
1845 case NET_CAPABILITY_FOTA: return "FOTA";
1846 case NET_CAPABILITY_IMS: return "IMS";
1847 case NET_CAPABILITY_CBS: return "CBS";
1848 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1849 case NET_CAPABILITY_IA: return "IA";
1850 case NET_CAPABILITY_RCS: return "RCS";
1851 case NET_CAPABILITY_XCAP: return "XCAP";
1852 case NET_CAPABILITY_EIMS: return "EIMS";
1853 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1854 case NET_CAPABILITY_INTERNET: return "INTERNET";
1855 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1856 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1857 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1858 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1859 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1860 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1861 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1862 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1863 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1864 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1865 case NET_CAPABILITY_MCX: return "MCX";
1866 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1867 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001868 }
1869 }
1870
1871 /**
1872 * @hide
1873 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001874 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001875 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001876 StringJoiner joiner = new StringJoiner("|");
1877 if (types != null) {
1878 for (int t : types) {
1879 joiner.add(transportNameOf(t));
1880 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001881 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001882 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001883 }
1884
1885 /**
1886 * @hide
1887 */
paulhud9736de2019-03-08 16:35:20 +08001888 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001889 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001890 return "UNKNOWN";
1891 }
1892 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001893 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001894
Jeff Sharkeyde570312017-10-24 21:25:50 -06001895 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001896 Preconditions.checkArgument(
1897 isValidTransport(transport), "Invalid TransportType " + transport);
1898 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001899
1900 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1901 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1902 }
1903
1904 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1905 Preconditions.checkArgument(isValidCapability(capability),
1906 "NetworkCapability " + capability + "out of range");
1907 }
junyulai05986c62018-08-07 19:50:45 +08001908
1909 /**
1910 * Check if this {@code NetworkCapability} instance is metered.
1911 *
1912 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1913 * @hide
1914 */
1915 public boolean isMetered() {
1916 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1917 }
lucaslin783f2212019-10-22 18:27:33 +08001918
1919 /**
1920 * Check if private dns is broken.
1921 *
1922 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1923 * @hide
1924 */
1925 public boolean isPrivateDnsBroken() {
1926 return mPrivateDnsBroken;
1927 }
1928
1929 /**
1930 * Set mPrivateDnsBroken to true when private dns is broken.
1931 *
1932 * @param broken the status of private DNS to be set.
1933 * @hide
1934 */
1935 public void setPrivateDnsBroken(boolean broken) {
1936 mPrivateDnsBroken = broken;
1937 }
1938
1939 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1940 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1941 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001942
1943 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001944 * Set the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001945 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001946 * For instances of NetworkCapabilities representing a request, sets the
1947 * UID of the app making the request. For a network created by the system,
1948 * sets the UID of the only app whose requests can match this network.
1949 * This can be set to {@link Process#INVALID_UID} if there is no such app,
1950 * or if this instance of NetworkCapabilities is about to be sent to a
1951 * party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001952 *
1953 * @param uid UID of the app.
1954 * @hide
1955 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001956 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1957 mRequestorUid = uid;
1958 return this;
1959 }
1960
1961 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001962 * Returns the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001963 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001964 * For a NetworkRequest being made by an app, contains the app's UID. For a network
1965 * created by the system, contains the UID of the only app whose requests can match
1966 * this network, or {@link Process#INVALID_UID} if none or if the
1967 * caller does not have permission to learn about this.
1968 *
1969 * @return the uid of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001970 * @hide
1971 */
1972 public int getRequestorUid() {
1973 return mRequestorUid;
1974 }
1975
1976 /**
1977 * Set the package name of the app making the request.
1978 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001979 * For instances of NetworkCapabilities representing a request, sets the
1980 * package name of the app making the request. For a network created by the system,
1981 * sets the package name of the only app whose requests can match this network.
1982 * This can be set to null if there is no such app, or if this instance of
1983 * NetworkCapabilities is about to be sent to a party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001984 *
1985 * @param packageName package name of the app.
1986 * @hide
1987 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001988 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
1989 mRequestorPackageName = packageName;
1990 return this;
1991 }
1992
1993 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001994 * Returns the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001995 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001996 * For a NetworkRequest being made by an app, contains the app's package name. For a
1997 * network created by the system, contains the package name of the only app whose
1998 * requests can match this network, or null if none or if the caller does not have
1999 * permission to learn about this.
2000 *
2001 * @return the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08002002 * @hide
2003 */
2004 @Nullable
2005 public String getRequestorPackageName() {
2006 return mRequestorPackageName;
2007 }
2008
2009 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09002010 * Set the uid and package name of the app causing this network to exist.
Roshan Piuse38acab2020-01-16 12:17:17 -08002011 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002012 * {@see #setRequestorUid} and {@link #setRequestorPackageName}
Roshan Piuse38acab2020-01-16 12:17:17 -08002013 *
2014 * @param uid UID of the app.
2015 * @param packageName package name of the app.
2016 * @hide
2017 */
2018 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
2019 int uid, @NonNull String packageName) {
2020 return setRequestorUid(uid).setRequestorPackageName(packageName);
2021 }
2022
2023 /**
2024 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
2025 * capabilities.
2026 *
2027 * This method is called on the NetworkCapabilities embedded in a request with the
2028 * capabilities of an available network. If the available network, sets a specific
2029 * requestor (by uid and optionally package name), then this will only match a request from the
2030 * same app. If either of the capabilities have an unset uid or package name, then it matches
2031 * everything.
2032 * <p>
2033 * nc is assumed nonnull. Else, NPE.
2034 */
2035 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
2036 // No uid set, matches everything.
2037 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
2038 return true;
2039 }
2040 // uids don't match.
2041 if (mRequestorUid != nc.mRequestorUid) return false;
2042 // No package names set, matches everything
2043 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
2044 // check for package name match.
2045 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2046 }
2047
2048 /**
2049 * Combine requestor info of the capabilities.
2050 * <p>
2051 * This is only legal if either the requestor info of this object is reset, or both info are
2052 * equal.
2053 * nc is assumed nonnull.
2054 */
2055 private void combineRequestor(@NonNull NetworkCapabilities nc) {
2056 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
2057 throw new IllegalStateException("Can't combine two uids");
2058 }
2059 if (mRequestorPackageName != null
2060 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
2061 throw new IllegalStateException("Can't combine two package names");
2062 }
2063 setRequestorUid(nc.mRequestorUid);
2064 setRequestorPackageName(nc.mRequestorPackageName);
2065 }
2066
2067 private boolean equalsRequestor(NetworkCapabilities nc) {
2068 return mRequestorUid == nc.mRequestorUid
2069 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2070 }
Chalard Jeane5e38502020-03-18 15:58:50 +09002071
2072 /**
2073 * Builder class for NetworkCapabilities.
2074 *
2075 * This class is mainly for for {@link NetworkAgent} instances to use. Many fields in
2076 * the built class require holding a signature permission to use - mostly
2077 * {@link android.Manifest.permission.NETWORK_FACTORY}, but refer to the specific
2078 * description of each setter. As this class lives entirely in app space it does not
2079 * enforce these restrictions itself but the system server clears out the relevant
2080 * fields when receiving a NetworkCapabilities object from a caller without the
2081 * appropriate permission.
2082 *
2083 * Apps don't use this builder directly. Instead, they use {@link NetworkRequest} via
2084 * its builder object.
2085 *
2086 * @hide
2087 */
2088 @SystemApi
2089 @TestApi
Aaron Huangfbb485a2020-03-25 13:36:38 +08002090 public static final class Builder {
Chalard Jeane5e38502020-03-18 15:58:50 +09002091 private final NetworkCapabilities mCaps;
2092
2093 /**
2094 * Creates a new Builder to construct NetworkCapabilities objects.
2095 */
2096 public Builder() {
2097 mCaps = new NetworkCapabilities();
2098 }
2099
2100 /**
2101 * Creates a new Builder of NetworkCapabilities from an existing instance.
2102 */
2103 public Builder(@NonNull final NetworkCapabilities nc) {
2104 Objects.requireNonNull(nc);
2105 mCaps = new NetworkCapabilities(nc);
2106 }
2107
2108 /**
2109 * Adds the given transport type.
2110 *
2111 * Multiple transports may be added. Note that when searching for a network to satisfy a
2112 * request, satisfying any of the transports listed in the request will satisfy the request.
2113 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
2114 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
2115 * to be selected. This is logically different than
2116 * {@code NetworkCapabilities.NET_CAPABILITY_*}.
2117 *
2118 * @param transportType the transport type to be added or removed.
2119 * @return this builder
2120 */
2121 @NonNull
2122 public Builder addTransportType(@Transport int transportType) {
2123 checkValidTransportType(transportType);
2124 mCaps.addTransportType(transportType);
2125 return this;
2126 }
2127
2128 /**
2129 * Removes the given transport type.
2130 *
2131 * {@see #addTransportType}.
2132 *
2133 * @param transportType the transport type to be added or removed.
2134 * @return this builder
2135 */
2136 @NonNull
2137 public Builder removeTransportType(@Transport int transportType) {
2138 checkValidTransportType(transportType);
2139 mCaps.removeTransportType(transportType);
2140 return this;
2141 }
2142
2143 /**
2144 * Adds the given capability.
2145 *
2146 * @param capability the capability
2147 * @return this builder
2148 */
2149 @NonNull
2150 public Builder addCapability(@NetCapability final int capability) {
2151 mCaps.setCapability(capability, true);
2152 return this;
2153 }
2154
2155 /**
2156 * Removes the given capability.
2157 *
2158 * @param capability the capability
2159 * @return this builder
2160 */
2161 @NonNull
2162 public Builder removeCapability(@NetCapability final int capability) {
2163 mCaps.setCapability(capability, false);
2164 return this;
2165 }
2166
2167 /**
2168 * Sets the owner UID.
2169 *
2170 * The default value is {@link Process#INVALID_UID}. Pass this value to reset.
2171 *
2172 * Note: for security the system will clear out this field when received from a
2173 * non-privileged source.
2174 *
2175 * @param ownerUid the owner UID
2176 * @return this builder
2177 */
2178 @NonNull
2179 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2180 public Builder setOwnerUid(final int ownerUid) {
2181 mCaps.setOwnerUid(ownerUid);
2182 return this;
2183 }
2184
2185 /**
2186 * Sets the list of UIDs that are administrators of this network.
2187 *
2188 * <p>UIDs included in administratorUids gain administrator privileges over this
2189 * Network. Examples of UIDs that should be included in administratorUids are:
2190 * <ul>
2191 * <li>Carrier apps with privileges for the relevant subscription
2192 * <li>Active VPN apps
2193 * <li>Other application groups with a particular Network-related role
2194 * </ul>
2195 *
2196 * <p>In general, user-supplied networks (such as WiFi networks) do not have
2197 * administrators.
2198 *
2199 * <p>An app is granted owner privileges over Networks that it supplies. The owner
2200 * UID MUST always be included in administratorUids.
2201 *
2202 * The default value is the empty array. Pass an empty array to reset.
2203 *
2204 * Note: for security the system will clear out this field when received from a
2205 * non-privileged source, such as an app using reflection to call this or
2206 * mutate the member in the built object.
2207 *
2208 * @param administratorUids the UIDs to be set as administrators of this Network.
2209 * @return this builder
2210 */
2211 @NonNull
2212 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2213 public Builder setAdministratorUids(@NonNull final int[] administratorUids) {
2214 Objects.requireNonNull(administratorUids);
2215 mCaps.setAdministratorUids(administratorUids);
2216 return this;
2217 }
2218
2219 /**
2220 * Sets the upstream bandwidth of the link.
2221 *
2222 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
2223 * the estimated first hop transport bandwidth.
2224 * <p>
2225 * Note that when used to request a network, this specifies the minimum acceptable.
2226 * When received as the state of an existing network this specifies the typical
2227 * first hop bandwidth expected. This is never measured, but rather is inferred
2228 * from technology type and other link parameters. It could be used to differentiate
2229 * between very slow 1xRTT cellular links and other faster networks or even between
2230 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2231 * fast backhauls and slow backhauls.
2232 *
2233 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
2234 * @return this builder
2235 */
2236 @NonNull
2237 public Builder setLinkUpstreamBandwidthKbps(final int upKbps) {
2238 mCaps.setLinkUpstreamBandwidthKbps(upKbps);
2239 return this;
2240 }
2241
2242 /**
2243 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
2244 * the estimated first hop transport bandwidth.
2245 * <p>
2246 * Note that when used to request a network, this specifies the minimum acceptable.
2247 * When received as the state of an existing network this specifies the typical
2248 * first hop bandwidth expected. This is never measured, but rather is inferred
2249 * from technology type and other link parameters. It could be used to differentiate
2250 * between very slow 1xRTT cellular links and other faster networks or even between
2251 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2252 * fast backhauls and slow backhauls.
2253 *
2254 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
2255 * @return this builder
2256 */
2257 @NonNull
2258 public Builder setLinkDownstreamBandwidthKbps(final int downKbps) {
2259 mCaps.setLinkDownstreamBandwidthKbps(downKbps);
2260 return this;
2261 }
2262
2263 /**
2264 * Sets the optional bearer specific network specifier.
2265 * This has no meaning if a single transport is also not specified, so calling
2266 * this without a single transport set will generate an exception, as will
2267 * subsequently adding or removing transports after this is set.
2268 * </p>
2269 *
2270 * @param specifier a concrete, parcelable framework class that extends NetworkSpecifier,
2271 * or null to clear it.
2272 * @return this builder
2273 */
2274 @NonNull
2275 public Builder setNetworkSpecifier(@Nullable final NetworkSpecifier specifier) {
2276 mCaps.setNetworkSpecifier(specifier);
2277 return this;
2278 }
2279
2280 /**
2281 * Sets the optional transport specific information.
2282 *
2283 * @param info A concrete, parcelable framework class that extends {@link TransportInfo},
2284 * or null to clear it.
2285 * @return this builder
2286 */
2287 @NonNull
2288 public Builder setTransportInfo(@Nullable final TransportInfo info) {
2289 mCaps.setTransportInfo(info);
2290 return this;
2291 }
2292
2293 /**
2294 * Sets the signal strength. This is a signed integer, with higher values indicating a
2295 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the
2296 * same RSSI units reported by wifi code.
2297 * <p>
2298 * Note that when used to register a network callback, this specifies the minimum
2299 * acceptable signal strength. When received as the state of an existing network it
2300 * specifies the current value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means
2301 * no value when received and has no effect when requesting a callback.
2302 *
2303 * Note: for security the system will throw if it receives a NetworkRequest where
2304 * the underlying NetworkCapabilities has this member set from a source that does
2305 * not hold the {@link android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP}
2306 * permission. Apps with this permission can use this indirectly through
2307 * {@link android.net.NetworkRequest}.
2308 *
2309 * @param signalStrength the bearer-specific signal strength.
2310 * @return this builder
2311 */
2312 @NonNull
2313 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP)
2314 public Builder setSignalStrength(final int signalStrength) {
2315 mCaps.setSignalStrength(signalStrength);
2316 return this;
2317 }
2318
2319 /**
2320 * Sets the SSID of this network.
2321 *
2322 * Note: for security the system will clear out this field when received from a
2323 * non-privileged source, like an app using reflection to set this.
2324 *
2325 * @param ssid the SSID, or null to clear it.
2326 * @return this builder
2327 */
2328 @NonNull
2329 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2330 public Builder setSsid(@Nullable final String ssid) {
2331 mCaps.setSSID(ssid);
2332 return this;
2333 }
2334
2335 /**
2336 * Set the uid of the app causing this network to exist.
2337 *
2338 * Note: for security the system will clear out this field when received from a
2339 * non-privileged source.
2340 *
2341 * @param uid UID of the app.
2342 * @return this builder
2343 */
2344 @NonNull
2345 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2346 public Builder setRequestorUid(final int uid) {
2347 mCaps.setRequestorUid(uid);
2348 return this;
2349 }
2350
2351 /**
2352 * Set the package name of the app causing this network to exist.
2353 *
2354 * Note: for security the system will clear out this field when received from a
2355 * non-privileged source.
2356 *
2357 * @param packageName package name of the app, or null to clear it.
2358 * @return this builder
2359 */
2360 @NonNull
2361 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2362 public Builder setRequestorPackageName(@Nullable final String packageName) {
2363 mCaps.setRequestorPackageName(packageName);
2364 return this;
2365 }
2366
2367 /**
2368 * Builds the instance of the capabilities.
2369 *
2370 * @return the built instance of NetworkCapabilities.
2371 */
2372 @NonNull
2373 public NetworkCapabilities build() {
2374 if (mCaps.getOwnerUid() != Process.INVALID_UID) {
2375 if (!ArrayUtils.contains(mCaps.getAdministratorUids(), mCaps.getOwnerUid())) {
2376 throw new IllegalStateException("The owner UID must be included in "
2377 + " administrator UIDs.");
2378 }
2379 }
2380 return new NetworkCapabilities(mCaps);
2381 }
2382 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07002383}