blob: 9ded22fb70c652170bc4be66d9fc3a4470df7d5f [file] [log] [blame]
Robert Greenwalt1448f052014-04-08 13:41:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Chalard Jean981dcca2020-02-06 18:31:19 +090019import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
20
Jeff Sharkeyde570312017-10-24 21:25:50 -060021import android.annotation.IntDef;
paulhud9736de2019-03-08 16:35:20 +080022import android.annotation.NonNull;
Etan Cohenca9fb562018-11-27 07:32:39 -080023import android.annotation.Nullable;
Chalard Jeane5e38502020-03-18 15:58:50 +090024import android.annotation.RequiresPermission;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070025import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060026import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000027import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060028import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010029import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070030import android.os.Parcel;
31import android.os.Parcelable;
Qingxi Li7cf06622020-01-17 17:54:27 -080032import android.os.Process;
Roshan Piuse38acab2020-01-16 12:17:17 -080033import android.text.TextUtils;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090034import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080035import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070036
37import com.android.internal.annotations.VisibleForTesting;
Chalard Jeane5e38502020-03-18 15:58:50 +090038import com.android.internal.util.ArrayUtils;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090039import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090040import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070041
Jeff Sharkeyde570312017-10-24 21:25:50 -060042import java.lang.annotation.Retention;
43import java.lang.annotation.RetentionPolicy;
Cody Kestingf7ac9962020-03-16 18:15:28 -070044import java.util.Arrays;
Etan Cohena7434272017-04-03 12:17:51 -070045import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090046import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090047import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070048
49/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070050 * Representation of the capabilities of an active network. Instances are
51 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060052 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
53 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060054 * <p>
55 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
56 * network selection. Rather than indicate a need for Wi-Fi because an
57 * application needs high bandwidth and risk obsolescence when a new, fast
58 * network appears (like LTE), the application should specify it needs high
59 * bandwidth. Similarly if an application needs an unmetered network for a bulk
60 * transfer it can specify that rather than assuming all cellular based
61 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070062 */
63public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070064 private static final String TAG = "NetworkCapabilities";
65
lucaslin783f2212019-10-22 18:27:33 +080066 // Set to true when private DNS is broken.
67 private boolean mPrivateDnsBroken;
68
Roshan Piuse38acab2020-01-16 12:17:17 -080069 /**
70 * Uid of the app making the request.
71 */
72 private int mRequestorUid;
73
74 /**
75 * Package name of the app making the request.
76 */
77 private String mRequestorPackageName;
78
Robert Greenwalt01d004e2014-05-18 15:24:21 -070079 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090081 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070082 }
83
84 public NetworkCapabilities(NetworkCapabilities nc) {
85 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090086 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070087 }
88 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070089
90 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 * Completely clears the contents of this object, removing even the capabilities that are set
92 * by default when the object is constructed.
Chalard Jeane5e38502020-03-18 15:58:50 +090093 * @hide
Lorenzo Colittif7058f52015-04-27 11:31:55 +090094 */
95 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080096 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070097 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090098 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080099 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900100 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +0900101 mUids = null;
Cody Kestingf7ac9962020-03-16 18:15:28 -0700102 mAdministratorUids = new int[0];
Qingxi Li7cf06622020-01-17 17:54:27 -0800103 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +0900104 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +0800105 mPrivateDnsBroken = false;
Roshan Piuse38acab2020-01-16 12:17:17 -0800106 mRequestorUid = Process.INVALID_UID;
107 mRequestorPackageName = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900108 }
109
110 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +0900111 * Set all contents of this object to the contents of a NetworkCapabilities.
112 * @hide
113 */
paulhud9736de2019-03-08 16:35:20 +0800114 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900115 mNetworkCapabilities = nc.mNetworkCapabilities;
116 mTransportTypes = nc.mTransportTypes;
117 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
118 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
119 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800120 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900121 mSignalStrength = nc.mSignalStrength;
122 setUids(nc.mUids); // Will make the defensive copy
Chalard Jean981dcca2020-02-06 18:31:19 +0900123 setAdministratorUids(nc.getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -0800124 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900125 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
126 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800127 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Roshan Piuse38acab2020-01-16 12:17:17 -0800128 mRequestorUid = nc.mRequestorUid;
129 mRequestorPackageName = nc.mRequestorPackageName;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900130 }
131
132 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 * Represents the network's capabilities. If any are specified they will be satisfied
134 * by any Network that matches all of them.
135 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100136 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900137 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800139 /**
140 * If any capabilities specified here they must not exist in the matching Network.
141 */
142 private long mUnwantedNetworkCapabilities;
143
Jeff Sharkeyde570312017-10-24 21:25:50 -0600144 /** @hide */
145 @Retention(RetentionPolicy.SOURCE)
146 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
147 NET_CAPABILITY_MMS,
148 NET_CAPABILITY_SUPL,
149 NET_CAPABILITY_DUN,
150 NET_CAPABILITY_FOTA,
151 NET_CAPABILITY_IMS,
152 NET_CAPABILITY_CBS,
153 NET_CAPABILITY_WIFI_P2P,
154 NET_CAPABILITY_IA,
155 NET_CAPABILITY_RCS,
156 NET_CAPABILITY_XCAP,
157 NET_CAPABILITY_EIMS,
158 NET_CAPABILITY_NOT_METERED,
159 NET_CAPABILITY_INTERNET,
160 NET_CAPABILITY_NOT_RESTRICTED,
161 NET_CAPABILITY_TRUSTED,
162 NET_CAPABILITY_NOT_VPN,
163 NET_CAPABILITY_VALIDATED,
164 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600165 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600166 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900167 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900168 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800169 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800170 NET_CAPABILITY_MCX,
171 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jack Yu30be5e52020-04-02 15:34:33 -0700172 NET_CAPABILITY_TEMPORARILY_NOT_METERED,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600173 })
174 public @interface NetCapability { }
175
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700177 * Indicates this is a network that has the ability to reach the
178 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 */
180 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Indicates this is a network that has the ability to reach the carrier's
184 * SUPL server, used to retrieve GPS information.
185 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Indicates this is a network that has the ability to reach the carrier's
190 * DUN or tethering gateway.
191 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193
194 /**
195 * Indicates this is a network that has the ability to reach the carrier's
196 * FOTA portal, used for over the air updates.
197 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Indicates this is a network that has the ability to reach the carrier's
202 * IMS servers, used for network registration and signaling.
203 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Indicates this is a network that has the ability to reach the carrier's
208 * CBS servers, used for carrier specific services.
209 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700210 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700211
212 /**
213 * Indicates this is a network that has the ability to reach a Wi-Fi direct
214 * peer.
215 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700217
218 /**
219 * Indicates this is a network that has the ability to reach a carrier's
220 * Initial Attach servers.
221 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700222 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700223
224 /**
225 * Indicates this is a network that has the ability to reach a carrier's
226 * RCS servers, used for Rich Communication Services.
227 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700228 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700229
230 /**
231 * Indicates this is a network that has the ability to reach a carrier's
232 * XCAP servers, used for configuration and control.
233 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700234 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700235
236 /**
237 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700238 * Emergency IMS servers or other services, used for network signaling
239 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700240 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700241 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700242
243 /**
244 * Indicates that this network is unmetered.
245 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700246 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700247
248 /**
249 * Indicates that this network should be able to reach the internet.
250 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700251 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700252
253 /**
254 * Indicates that this network is available for general use. If this is not set
255 * applications should not attempt to communicate on this network. Note that this
256 * is simply informative and not enforcement - enforcement is handled via other means.
257 * Set by default.
258 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700259 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
260
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700261 /**
262 * Indicates that the user has indicated implicit trust of this network. This
263 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
264 * BT device or a wifi the user asked to connect to. Untrusted networks
265 * are probably limited to unknown wifi AP. Set by default.
266 */
267 public static final int NET_CAPABILITY_TRUSTED = 14;
268
Paul Jensen76b610a2015-03-18 09:33:07 -0400269 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400270 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400271 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400272 */
273 public static final int NET_CAPABILITY_NOT_VPN = 15;
274
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900275 /**
276 * Indicates that connectivity on this network was successfully validated. For example, for a
277 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
278 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900279 */
280 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700281
Paul Jensen3d194ea2015-06-16 14:27:36 -0400282 /**
283 * Indicates that this network was found to have a captive portal in place last time it was
284 * probed.
285 */
286 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
287
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900288 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600289 * Indicates that this network is not roaming.
290 */
291 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
292
293 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900294 * Indicates that this network is available for use by apps, and not a network that is being
295 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900296 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600297 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900298
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900299 /**
300 * Indicates that this network is not congested.
301 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600302 * When a network is congested, applications should defer network traffic
303 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900304 */
305 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
306
Chalard Jean804b8fb2018-01-30 22:41:41 +0900307 /**
308 * Indicates that this network is not currently suspended.
309 * <p>
310 * When a network is suspended, the network's IP addresses and any connections
311 * established on the network remain valid, but the network is temporarily unable
312 * to transfer data. This can happen, for example, if a cellular network experiences
313 * a temporary loss of signal, such as when driving through a tunnel, etc.
314 * A network with this capability is not suspended, so is expected to be able to
315 * transfer data.
316 */
317 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
318
Pavel Maltsev43403202018-01-30 17:19:44 -0800319 /**
320 * Indicates that traffic that goes through this network is paid by oem. For example,
321 * this network can be used by system apps to upload telemetry data.
322 * @hide
323 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700324 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800325 public static final int NET_CAPABILITY_OEM_PAID = 22;
326
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800327 /**
328 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
329 * servers.
330 */
331 public static final int NET_CAPABILITY_MCX = 23;
332
lucasline252a742019-03-12 13:08:03 +0800333 /**
334 * Indicates that this network was tested to only provide partial connectivity.
335 * @hide
336 */
337 @SystemApi
338 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
339
Jack Yu30be5e52020-04-02 15:34:33 -0700340 /**
341 * This capability will be set for networks that are generally metered, but are currently
342 * unmetered, e.g., because the user is in a particular area. This capability can be changed at
343 * any time. When it is removed, applications are responsible for stopping any data transfer
344 * that should not occur on a metered network.
345 */
346 public static final int NET_CAPABILITY_TEMPORARILY_NOT_METERED = 25;
347
Robert Greenwalt1448f052014-04-08 13:41:39 -0700348 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Jack Yu30be5e52020-04-02 15:34:33 -0700349 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_TEMPORARILY_NOT_METERED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700350
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700351 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900352 * Network capabilities that are expected to be mutable, i.e., can change while a particular
353 * network is connected.
354 */
355 private static final long MUTABLE_CAPABILITIES =
356 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
357 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900358 (1 << NET_CAPABILITY_TRUSTED)
359 | (1 << NET_CAPABILITY_VALIDATED)
360 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
361 | (1 << NET_CAPABILITY_NOT_ROAMING)
362 | (1 << NET_CAPABILITY_FOREGROUND)
363 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800364 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
Jack Yu30be5e52020-04-02 15:34:33 -0700365 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY
366 | (1 << NET_CAPABILITY_TEMPORARILY_NOT_METERED));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900367
368 /**
369 * Network capabilities that are not allowed in NetworkRequests. This exists because the
370 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
371 * capability's presence cannot be known in advance. If such a capability is requested, then we
372 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
373 * get immediately torn down because they do not have the requested capability.
374 */
375 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900376 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900377
378 /**
379 * Capabilities that are set by default when the object is constructed.
380 */
381 private static final long DEFAULT_CAPABILITIES =
382 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
383 (1 << NET_CAPABILITY_TRUSTED) |
384 (1 << NET_CAPABILITY_NOT_VPN);
385
386 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400387 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800388 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400389 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700390 @VisibleForTesting
391 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400392 (1 << NET_CAPABILITY_CBS) |
393 (1 << NET_CAPABILITY_DUN) |
394 (1 << NET_CAPABILITY_EIMS) |
395 (1 << NET_CAPABILITY_FOTA) |
396 (1 << NET_CAPABILITY_IA) |
397 (1 << NET_CAPABILITY_IMS) |
398 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800399 (1 << NET_CAPABILITY_XCAP) |
400 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800401
402 /**
403 * Capabilities that force network to be restricted.
404 * {@see #maybeMarkCapabilitiesRestricted}.
405 */
406 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800407 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400408
409 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700410 * Capabilities that suggest that a network is unrestricted.
411 * {@see #maybeMarkCapabilitiesRestricted}.
412 */
413 @VisibleForTesting
414 /* package */ static final long UNRESTRICTED_CAPABILITIES =
415 (1 << NET_CAPABILITY_INTERNET) |
416 (1 << NET_CAPABILITY_MMS) |
417 (1 << NET_CAPABILITY_SUPL) |
418 (1 << NET_CAPABILITY_WIFI_P2P);
419
420 /**
lucasline252a742019-03-12 13:08:03 +0800421 * Capabilities that are managed by ConnectivityService.
422 */
423 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
424 (1 << NET_CAPABILITY_VALIDATED)
425 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
426 | (1 << NET_CAPABILITY_FOREGROUND)
427 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
428
429 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000430 * Capabilities that are allowed for test networks. This list must be set so that it is safe
431 * for an unprivileged user to create a network with these capabilities via shell. As such,
432 * it must never contain capabilities that are generally useful to the system, such as
433 * INTERNET, IMS, SUPL, etc.
434 */
435 private static final long TEST_NETWORKS_ALLOWED_CAPABILITIES =
436 (1 << NET_CAPABILITY_NOT_METERED)
Jack Yu30be5e52020-04-02 15:34:33 -0700437 | (1 << NET_CAPABILITY_TEMPORARILY_NOT_METERED)
Chalard Jean09c48e42020-03-25 10:33:55 +0000438 | (1 << NET_CAPABILITY_NOT_RESTRICTED)
439 | (1 << NET_CAPABILITY_NOT_VPN)
440 | (1 << NET_CAPABILITY_NOT_ROAMING)
441 | (1 << NET_CAPABILITY_NOT_CONGESTED)
442 | (1 << NET_CAPABILITY_NOT_SUSPENDED);
443
444 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700445 * Adds the given capability to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900446 * Note that when searching for a network to satisfy a request, all capabilities
447 * requested must be satisfied.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700448 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600449 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900450 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900451 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452 */
paulhud9736de2019-03-08 16:35:20 +0800453 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800454 // If the given capability was previously added to the list of unwanted capabilities
455 // then the capability will also be removed from the list of unwanted capabilities.
456 // TODO: Consider adding unwanted capabilities to the public API and mention this
457 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800458 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700459 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800460 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700461 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700462 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700463
464 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800465 * Adds the given capability to the list of unwanted capabilities of this
Chalard Jeane5e38502020-03-18 15:58:50 +0900466 * {@code NetworkCapability} instance. Note that when searching for a network to
467 * satisfy a request, the network must not contain any capability from unwanted capability
468 * list.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800469 * <p>
470 * If the capability was previously added to the list of required capabilities (for
471 * example, it was there by default or added using {@link #addCapability(int)} method), then
472 * it will be removed from the list of required capabilities as well.
473 *
474 * @see #addCapability(int)
475 * @hide
476 */
477 public void addUnwantedCapability(@NetCapability int capability) {
478 checkValidCapability(capability);
479 mUnwantedNetworkCapabilities |= 1 << capability;
480 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
481 }
482
483 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700484 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
485 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600486 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900487 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900488 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700489 */
paulhud9736de2019-03-08 16:35:20 +0800490 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800491 // Note that this method removes capabilities that were added via addCapability(int),
492 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800493 checkValidCapability(capability);
494 final long mask = ~(1 << capability);
495 mNetworkCapabilities &= mask;
496 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700497 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700498 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700499
500 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600501 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
502 * instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900503 * @hide
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600504 */
paulhud9736de2019-03-08 16:35:20 +0800505 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
506 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600507 if (value) {
508 addCapability(capability);
509 } else {
510 removeCapability(capability);
511 }
512 return this;
513 }
514
515 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700516 * Gets all the capabilities set on this {@code NetworkCapability} instance.
517 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600518 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700519 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700520 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000521 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600522 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600523 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900524 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700525 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700526
527 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800528 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
529 *
530 * @return an array of unwanted capability values for this instance.
531 * @hide
532 */
533 public @NetCapability int[] getUnwantedCapabilities() {
534 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
535 }
536
537
538 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600539 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700540 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600541 *
542 * @hide
543 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800544 public void setCapabilities(@NetCapability int[] capabilities,
545 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600546 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800547 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600548 }
549
550 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800551 * @deprecated use {@link #setCapabilities(int[], int[])}
552 * @hide
553 */
554 @Deprecated
555 public void setCapabilities(@NetCapability int[] capabilities) {
556 setCapabilities(capabilities, new int[] {});
557 }
558
559 /**
560 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700561 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600562 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700563 * @return {@code true} if set on this instance.
564 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600565 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800566 return isValidCapability(capability)
567 && ((mNetworkCapabilities & (1 << capability)) != 0);
568 }
569
570 /** @hide */
571 public boolean hasUnwantedCapability(@NetCapability int capability) {
572 return isValidCapability(capability)
573 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700574 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575
lucasline252a742019-03-12 13:08:03 +0800576 /**
577 * Check if this NetworkCapabilities has system managed capabilities or not.
578 * @hide
579 */
580 public boolean hasConnectivityManagedCapability() {
581 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
582 }
583
Pavel Maltseve18ef262018-03-07 11:13:04 -0800584 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800585 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700586 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800587 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700588 }
589
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900590 /**
591 * Convenience function that returns a human-readable description of the first mutable
592 * capability we find. Used to present an error message to apps that request mutable
593 * capabilities.
594 *
595 * @hide
596 */
paulhud9736de2019-03-08 16:35:20 +0800597 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800598 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
599 & NON_REQUESTABLE_CAPABILITIES;
600
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900601 if (nonRequestable != 0) {
602 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900603 }
604 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900605 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800606 if (isPrivateDnsBroken()) {
607 return "privateDnsBroken";
608 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900609 return null;
610 }
611
paulhud9736de2019-03-08 16:35:20 +0800612 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
613 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800614 long requestedCapabilities = mNetworkCapabilities;
615 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
616 long providedCapabilities = nc.mNetworkCapabilities;
617
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900618 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800619 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
620 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900621 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800622 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
623 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700624 }
625
Robert Greenwalt06314e42014-10-29 14:04:06 -0700626 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800627 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800628 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
629 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700630 }
631
paulhud9736de2019-03-08 16:35:20 +0800632 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900633 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800634 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
635 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
636 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900637 }
638
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639 /**
paulhu18354322020-01-09 17:08:11 +0800640 * Deduces that all the capabilities it provides are typically provided by restricted networks
641 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400642 *
paulhu18354322020-01-09 17:08:11 +0800643 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400644 * @hide
645 */
paulhu18354322020-01-09 17:08:11 +0800646 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800647 // Check if we have any capability that forces the network to be restricted.
648 final boolean forceRestrictedCapability =
649 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
650
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700651 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800652 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700653 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800654 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700655
656 // Must have at least some restricted capabilities.
657 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800658 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700659
paulhu18354322020-01-09 17:08:11 +0800660 return forceRestrictedCapability
661 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
662 }
663
664 /**
665 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
666 *
667 * @hide
668 */
669 public void maybeMarkCapabilitiesRestricted() {
670 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400671 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400672 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400673 }
674
675 /**
Chalard Jean09c48e42020-03-25 10:33:55 +0000676 * Test networks have strong restrictions on what capabilities they can have. Enforce these
677 * restrictions.
678 * @hide
679 */
Cody Kesting0d8d6ac2020-05-12 18:47:10 +0000680 public void restrictCapabilitesForTestNetwork(int creatorUid) {
Chalard Jean09c48e42020-03-25 10:33:55 +0000681 final long originalCapabilities = mNetworkCapabilities;
682 final NetworkSpecifier originalSpecifier = mNetworkSpecifier;
Chalard Jean3b2a81b2020-04-13 19:10:13 +0000683 final int originalSignalStrength = mSignalStrength;
Cody Kesting0d8d6ac2020-05-12 18:47:10 +0000684 final int originalOwnerUid = getOwnerUid();
685 final int[] originalAdministratorUids = getAdministratorUids();
Chalard Jean09c48e42020-03-25 10:33:55 +0000686 clearAll();
687 // Reset the transports to only contain TRANSPORT_TEST.
688 mTransportTypes = (1 << TRANSPORT_TEST);
689 mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES;
690 mNetworkSpecifier = originalSpecifier;
Chalard Jean3b2a81b2020-04-13 19:10:13 +0000691 mSignalStrength = originalSignalStrength;
Cody Kesting0d8d6ac2020-05-12 18:47:10 +0000692
693 // Only retain the owner and administrator UIDs if they match the app registering the remote
694 // caller that registered the network.
695 if (originalOwnerUid == creatorUid) {
696 setOwnerUid(creatorUid);
697 }
698 if (ArrayUtils.contains(originalAdministratorUids, creatorUid)) {
699 setAdministratorUids(new int[] {creatorUid});
700 }
Chalard Jean09c48e42020-03-25 10:33:55 +0000701 }
702
703 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700704 * Representing the transport type. Apps should generally not care about transport. A
705 * request for a fast internet connection could be satisfied by a number of different
706 * transports. If any are specified here it will be satisfied a Network that matches
707 * any of them. If a caller doesn't care about the transport it should not specify any.
708 */
709 private long mTransportTypes;
710
Jeff Sharkeyde570312017-10-24 21:25:50 -0600711 /** @hide */
712 @Retention(RetentionPolicy.SOURCE)
713 @IntDef(prefix = { "TRANSPORT_" }, value = {
714 TRANSPORT_CELLULAR,
715 TRANSPORT_WIFI,
716 TRANSPORT_BLUETOOTH,
717 TRANSPORT_ETHERNET,
718 TRANSPORT_VPN,
719 TRANSPORT_WIFI_AWARE,
720 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800721 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600722 })
723 public @interface Transport { }
724
Robert Greenwalt1448f052014-04-08 13:41:39 -0700725 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700726 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700727 */
728 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700729
730 /**
731 * Indicates this network uses a Wi-Fi transport.
732 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700733 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700734
735 /**
736 * Indicates this network uses a Bluetooth transport.
737 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700738 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700739
740 /**
741 * Indicates this network uses an Ethernet transport.
742 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700743 public static final int TRANSPORT_ETHERNET = 3;
744
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400745 /**
746 * Indicates this network uses a VPN transport.
747 */
748 public static final int TRANSPORT_VPN = 4;
749
Etan Cohen305ea282016-06-20 09:27:12 -0700750 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700751 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700752 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700753 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700754
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700755 /**
756 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700757 */
758 public static final int TRANSPORT_LOWPAN = 6;
759
Benedict Wong89ce5e32018-11-14 17:40:55 -0800760 /**
761 * Indicates this network uses a Test-only virtual interface as a transport.
762 *
763 * @hide
764 */
765 @TestApi
766 public static final int TRANSPORT_TEST = 7;
767
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900768 /** @hide */
769 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
770 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800771 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772
Hugo Benichi16f0a942017-06-20 14:07:59 +0900773 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600774 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900775 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
776 }
777
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900778 private static final String[] TRANSPORT_NAMES = {
779 "CELLULAR",
780 "WIFI",
781 "BLUETOOTH",
782 "ETHERNET",
783 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700784 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800785 "LOWPAN",
786 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900787 };
788
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700789 /**
790 * Adds the given transport type to this {@code NetworkCapability} instance.
Chalard Jeane5e38502020-03-18 15:58:50 +0900791 * Multiple transports may be applied. Note that when searching
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700792 * for a network to satisfy a request, any listed in the request will satisfy the request.
793 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
794 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
795 * to be selected. This is logically different than
796 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
797 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600798 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900799 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +0900800 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700801 */
paulhud9736de2019-03-08 16:35:20 +0800802 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900803 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700804 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700805 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700806 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700807 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700808
809 /**
810 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
811 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600812 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900813 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700814 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700815 */
paulhud9736de2019-03-08 16:35:20 +0800816 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900817 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700818 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700819 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700820 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700821 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700822
823 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600824 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
825 * instance.
826 *
827 * @hide
828 */
paulhud9736de2019-03-08 16:35:20 +0800829 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
830 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600831 if (value) {
832 addTransportType(transportType);
833 } else {
834 removeTransportType(transportType);
835 }
836 return this;
837 }
838
839 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700840 * Gets all the transports set on this {@code NetworkCapability} instance.
841 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600842 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700843 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700844 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600845 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900846 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800847 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900848 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700849 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700850
851 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600852 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700853 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600854 *
855 * @hide
856 */
857 public void setTransportTypes(@Transport int[] transportTypes) {
858 mTransportTypes = BitUtils.packBits(transportTypes);
859 }
860
861 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700862 * Tests for the presence of a transport on this instance.
863 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600864 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700865 * @return {@code true} if set on this instance.
866 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600867 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900868 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700869 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700870
871 private void combineTransportTypes(NetworkCapabilities nc) {
872 this.mTransportTypes |= nc.mTransportTypes;
873 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900874
Robert Greenwalt1448f052014-04-08 13:41:39 -0700875 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
876 return ((this.mTransportTypes == 0) ||
877 ((this.mTransportTypes & nc.mTransportTypes) != 0));
878 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900879
Robert Greenwalt06314e42014-10-29 14:04:06 -0700880 /** @hide */
881 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700882 return (nc.mTransportTypes == this.mTransportTypes);
883 }
884
885 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800886 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900887 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800888 * <p>This field keeps track of the UID of the app that created this network and is in charge of
889 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
890 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800891 *
892 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
893 * reset to Process.INVALID_UID unless all the following conditions are met:
894 *
895 * <ol>
896 * <li>The destination app is the network owner
897 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
898 * <li>The user's location toggle is on
899 * </ol>
900 *
901 * This is because the owner UID is location-sensitive. The apps that request a network could
902 * know where the device is if they can tell for sure the system has connected to the network
903 * they requested.
904 *
905 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
906 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
907 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900908 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800909 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900910
911 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800912 * Set the UID of the owner app.
Chalard Jeane5e38502020-03-18 15:58:50 +0900913 * @hide
Chalard Jeanf474fc32018-01-17 15:10:05 +0900914 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800915 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800916 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800917 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900918 }
919
Qingxi Li7cf06622020-01-17 17:54:27 -0800920 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800921 * Retrieves the UID of the app that owns this network.
922 *
923 * <p>For user privacy reasons, this field will only be populated if:
924 *
925 * <ol>
926 * <li>The calling app is the network owner
927 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
928 * <li>The user's location toggle is on
929 * </ol>
930 *
Chalard Jeane5e38502020-03-18 15:58:50 +0900931 * Instances of NetworkCapabilities sent to apps without the appropriate permissions will
932 * have this field cleared out.
Qingxi Li7cf06622020-01-17 17:54:27 -0800933 */
934 public int getOwnerUid() {
935 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000936 }
937
Chalard Jeanf474fc32018-01-17 15:10:05 +0900938 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800939 * UIDs of packages that are administrators of this network, or empty if none.
940 *
941 * <p>This field tracks the UIDs of packages that have permission to manage this network.
942 *
943 * <p>Network owners will also be listed as administrators.
944 *
945 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
946 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
947 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
Chalard Jean981dcca2020-02-06 18:31:19 +0900948 *
949 * <p>When received from an app in a NetworkRequest this is always cleared out by the system
950 * server. This field is never used for matching NetworkRequests to NetworkAgents.
Cody Kesting201fc132020-01-17 11:58:36 -0800951 */
Cody Kesting919385b2020-03-18 15:22:12 -0700952 @NonNull private int[] mAdministratorUids = new int[0];
Cody Kesting201fc132020-01-17 11:58:36 -0800953
954 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700955 * Sets the int[] of UIDs that are administrators of this network.
Cody Kesting201fc132020-01-17 11:58:36 -0800956 *
957 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
958 * Examples of UIDs that should be included in administratorUids are:
Chalard Jean981dcca2020-02-06 18:31:19 +0900959 *
Cody Kesting201fc132020-01-17 11:58:36 -0800960 * <ul>
Chalard Jean981dcca2020-02-06 18:31:19 +0900961 * <li>Carrier apps with privileges for the relevant subscription
962 * <li>Active VPN apps
963 * <li>Other application groups with a particular Network-related role
Cody Kesting201fc132020-01-17 11:58:36 -0800964 * </ul>
965 *
966 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
967 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800968 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
969 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800970 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900971 * <p>The administrator UIDs are set by network agents.
972 *
Cody Kesting201fc132020-01-17 11:58:36 -0800973 * @param administratorUids the UIDs to be set as administrators of this Network.
Cody Kesting93c1e652020-03-24 11:53:30 -0700974 * @throws IllegalArgumentException if duplicate UIDs are contained in administratorUids
Chalard Jean981dcca2020-02-06 18:31:19 +0900975 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800976 * @hide
977 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800978 @NonNull
Cody Kestingf7ac9962020-03-16 18:15:28 -0700979 public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) {
980 mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length);
Cody Kesting93c1e652020-03-24 11:53:30 -0700981 Arrays.sort(mAdministratorUids);
982 for (int i = 0; i < mAdministratorUids.length - 1; i++) {
983 if (mAdministratorUids[i] >= mAdministratorUids[i + 1]) {
984 throw new IllegalArgumentException("All administrator UIDs must be unique");
985 }
986 }
Roshan Piuse38acab2020-01-16 12:17:17 -0800987 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800988 }
989
990 /**
Cody Kestingf7ac9962020-03-16 18:15:28 -0700991 * Retrieves the UIDs that are administrators of this Network.
Cody Kesting201fc132020-01-17 11:58:36 -0800992 *
Chalard Jean981dcca2020-02-06 18:31:19 +0900993 * <p>This is only populated in NetworkCapabilities objects that come from network agents for
994 * networks that are managed by specific apps on the system, such as carrier privileged apps or
995 * wifi suggestion apps. This will include the network owner.
996 *
Cody Kestingf7ac9962020-03-16 18:15:28 -0700997 * @return the int[] of UIDs that are administrators of this Network
Chalard Jean981dcca2020-02-06 18:31:19 +0900998 * @see #mAdministratorUids
Cody Kesting201fc132020-01-17 11:58:36 -0800999 * @hide
1000 */
1001 @NonNull
1002 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +09001003 @TestApi
Cody Kestingf7ac9962020-03-16 18:15:28 -07001004 public int[] getAdministratorUids() {
1005 return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length);
Cody Kesting201fc132020-01-17 11:58:36 -08001006 }
1007
1008 /**
Chalard Jean981dcca2020-02-06 18:31:19 +09001009 * Tests if the set of administrator UIDs of this network is the same as that of the passed one.
1010 *
1011 * <p>The administrator UIDs must be in sorted order.
1012 *
1013 * <p>nc is assumed non-null. Else, NPE.
1014 *
1015 * @hide
1016 */
1017 @VisibleForTesting(visibility = PRIVATE)
1018 public boolean equalsAdministratorUids(@NonNull final NetworkCapabilities nc) {
1019 return Arrays.equals(mAdministratorUids, nc.mAdministratorUids);
1020 }
1021
1022 /**
1023 * Combine the administrator UIDs of the capabilities.
1024 *
1025 * <p>This is only legal if either of the administrators lists are empty, or if they are equal.
1026 * Combining administrator UIDs is only possible for combining non-overlapping sets of UIDs.
1027 *
1028 * <p>If both administrator lists are non-empty but not equal, they conflict with each other. In
1029 * this case, it would not make sense to add them together.
1030 */
1031 private void combineAdministratorUids(@NonNull final NetworkCapabilities nc) {
1032 if (nc.mAdministratorUids.length == 0) return;
1033 if (mAdministratorUids.length == 0) {
1034 mAdministratorUids = Arrays.copyOf(nc.mAdministratorUids, nc.mAdministratorUids.length);
1035 return;
1036 }
1037 if (!equalsAdministratorUids(nc)) {
1038 throw new IllegalStateException("Can't combine two different administrator UID lists");
1039 }
1040 }
1041
1042 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001043 * Value indicating that link bandwidth is unspecified.
1044 * @hide
1045 */
1046 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
1047
1048 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -07001049 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
1050 * for the first hop on the given transport. It is not measured, but may take into account
1051 * link parameters (Radio technology, allocated channels, etc).
1052 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001053 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
1054 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001055
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001056 /**
1057 * Sets the upstream 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 upKbps the estimated first hop upstream (device to network) 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 setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001066 mLinkUpBandwidthKbps = upKbps;
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 upstream 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 upstream (device to network) bandwidth.
1075 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001076 public int getLinkUpstreamBandwidthKbps() {
1077 return mLinkUpBandwidthKbps;
1078 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001079
1080 /**
1081 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
1082 * the estimated first hop transport bandwidth.
1083 * <p>
Chalard Jeane5e38502020-03-18 15:58:50 +09001084 * {@see Builder#setLinkUpstreamBandwidthKbps}
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001085 *
1086 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Chalard Jeane5e38502020-03-18 15:58:50 +09001087 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001088 */
paulhud9736de2019-03-08 16:35:20 +08001089 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001090 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001091 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001092 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -07001093
1094 /**
1095 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
1096 * the estimated first hop transport bandwidth.
1097 *
1098 * @return The estimated first hop downstream (network to device) bandwidth.
1099 */
Robert Greenwalt1448f052014-04-08 13:41:39 -07001100 public int getLinkDownstreamBandwidthKbps() {
1101 return mLinkDownBandwidthKbps;
1102 }
1103
1104 private void combineLinkBandwidths(NetworkCapabilities nc) {
1105 this.mLinkUpBandwidthKbps =
1106 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
1107 this.mLinkDownBandwidthKbps =
1108 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1109 }
1110 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1111 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1112 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1113 }
1114 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1115 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1116 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1117 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001118 /** @hide */
1119 public static int minBandwidth(int a, int b) {
1120 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1121 return b;
1122 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1123 return a;
1124 } else {
1125 return Math.min(a, b);
1126 }
1127 }
1128 /** @hide */
1129 public static int maxBandwidth(int a, int b) {
1130 return Math.max(a, b);
1131 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001132
Etan Cohena7434272017-04-03 12:17:51 -07001133 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001134 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001135
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001136 /**
1137 * Sets the optional bearer specific network specifier.
1138 * This has no meaning if a single transport is also not specified, so calling
1139 * this without a single transport set will generate an exception, as will
1140 * subsequently adding or removing transports after this is set.
1141 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001142 *
Etan Cohena7434272017-04-03 12:17:51 -07001143 * @param networkSpecifier A concrete, parcelable framework class that extends
1144 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001145 * @return This NetworkCapabilities instance, to facilitate chaining.
Chalard Jeane5e38502020-03-18 15:58:50 +09001146 * @hide
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001147 */
Aaron Huange6b62392019-09-20 22:52:54 +08001148 public @NonNull NetworkCapabilities setNetworkSpecifier(
1149 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001150 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001151 throw new IllegalStateException("Must have a single transport specified to use " +
1152 "setNetworkSpecifier");
1153 }
Etan Cohena7434272017-04-03 12:17:51 -07001154
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001155 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001156
Pierre Imaic8419a82016-03-22 17:54:54 +09001157 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001158 }
1159
1160 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001161 * Sets the optional transport specific information.
1162 *
1163 * @param transportInfo A concrete, parcelable framework class that extends
1164 * {@link TransportInfo}.
1165 * @return This NetworkCapabilities instance, to facilitate chaining.
1166 * @hide
1167 */
Aaron Huange6b62392019-09-20 22:52:54 +08001168 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001169 mTransportInfo = transportInfo;
1170 return this;
1171 }
1172
1173 /**
paulhud9736de2019-03-08 16:35:20 +08001174 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001175 *
Etan Cohena7434272017-04-03 12:17:51 -07001176 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
Chalard Jeane5e38502020-03-18 15:58:50 +09001177 * specifier or {@code null}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001178 */
paulhud9736de2019-03-08 16:35:20 +08001179 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001180 return mNetworkSpecifier;
1181 }
1182
Etan Cohenca9fb562018-11-27 07:32:39 -08001183 /**
1184 * Returns a transport-specific information container. The application may cast this
1185 * container to a concrete sub-class based on its knowledge of the network request. The
1186 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001187 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001188 *
1189 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1190 * available for the network.
1191 */
1192 @Nullable public TransportInfo getTransportInfo() {
1193 return mTransportInfo;
1194 }
1195
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001196 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001197 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001198 throw new IllegalStateException("Can't combine two networkSpecifiers");
1199 }
Etan Cohena7434272017-04-03 12:17:51 -07001200 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001201 }
Etan Cohena7434272017-04-03 12:17:51 -07001202
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001203 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Chalard Jean2da4f9f2020-03-27 17:57:34 +09001204 return mNetworkSpecifier == null || mNetworkSpecifier.canBeSatisfiedBy(nc.mNetworkSpecifier)
Etan Cohena7434272017-04-03 12:17:51 -07001205 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001206 }
Etan Cohena7434272017-04-03 12:17:51 -07001207
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001208 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001209 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001210 }
1211
Etan Cohenca9fb562018-11-27 07:32:39 -08001212 private void combineTransportInfos(NetworkCapabilities nc) {
1213 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1214 throw new IllegalStateException("Can't combine two TransportInfos");
1215 }
1216 setTransportInfo(nc.mTransportInfo);
1217 }
1218
1219 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1220 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1221 }
1222
Robert Greenwalt1448f052014-04-08 13:41:39 -07001223 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001224 * Magic value that indicates no signal strength provided. A request specifying this value is
1225 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001226 */
1227 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1228
1229 /**
1230 * Signal strength. This is a signed integer, and higher values indicate better signal.
1231 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1232 */
paulhud9736de2019-03-08 16:35:20 +08001233 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001234 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001235
1236 /**
1237 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1238 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001239 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001240 * <p>
1241 * Note that when used to register a network callback, this specifies the minimum acceptable
1242 * signal strength. When received as the state of an existing network it specifies the current
1243 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1244 * effect when requesting a callback.
1245 *
1246 * @param signalStrength the bearer-specific signal strength.
Chalard Jeane5e38502020-03-18 15:58:50 +09001247 * @hide
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001248 */
paulhud9736de2019-03-08 16:35:20 +08001249 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001250 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001251 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001252 }
1253
1254 /**
1255 * Returns {@code true} if this object specifies a signal strength.
1256 *
1257 * @hide
1258 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001259 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001260 public boolean hasSignalStrength() {
1261 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1262 }
1263
1264 /**
1265 * Retrieves the signal strength.
1266 *
1267 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001268 */
1269 public int getSignalStrength() {
1270 return mSignalStrength;
1271 }
1272
1273 private void combineSignalStrength(NetworkCapabilities nc) {
1274 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1275 }
1276
1277 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1278 return this.mSignalStrength <= nc.mSignalStrength;
1279 }
1280
1281 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1282 return this.mSignalStrength == nc.mSignalStrength;
1283 }
1284
1285 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001286 * List of UIDs this network applies to. No restriction if null.
1287 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001288 * For networks, mUids represent the list of network this applies to, and null means this
1289 * network applies to all UIDs.
1290 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1291 * must be included in a network so that they match. As an exception to the general rule,
1292 * a null mUids field for requests mean "no requirements" rather than what the general rule
1293 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1294 * of this API expect in practice. A network that must match all UIDs can still be
1295 * expressed with a set ranging the entire set of possible UIDs.
1296 * <p>
1297 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001298 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1299 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1300 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1301 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001302 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001303 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001304 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1305 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001306 * <p>
1307 * Please note that in principle a single app can be associated with multiple UIDs because
1308 * each app will have a different UID when it's run as a different (macro-)user. A single
1309 * macro user can only have a single active VPN app at any given time however.
1310 * <p>
1311 * Also please be aware this class does not try to enforce any normalization on this. Callers
1312 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1313 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1314 * their own (like requiring sortedness or no overlap) they need to enforce it
1315 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1316 * or overlapping ranges are present.
1317 *
1318 * @hide
1319 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001320 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001321
1322 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001323 * Convenience method to set the UIDs this network applies to to a single UID.
1324 * @hide
1325 */
paulhud9736de2019-03-08 16:35:20 +08001326 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001327 final ArraySet<UidRange> identity = new ArraySet<>(1);
1328 identity.add(new UidRange(uid, uid));
1329 setUids(identity);
1330 return this;
1331 }
1332
1333 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001334 * Set the list of UIDs this network applies to.
1335 * This makes a copy of the set so that callers can't modify it after the call.
1336 * @hide
1337 */
paulhud9736de2019-03-08 16:35:20 +08001338 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001339 if (null == uids) {
1340 mUids = null;
1341 } else {
1342 mUids = new ArraySet<>(uids);
1343 }
1344 return this;
1345 }
1346
1347 /**
1348 * Get the list of UIDs this network applies to.
1349 * This returns a copy of the set so that callers can't modify the original object.
1350 * @hide
1351 */
paulhud9736de2019-03-08 16:35:20 +08001352 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001353 return null == mUids ? null : new ArraySet<>(mUids);
1354 }
1355
1356 /**
1357 * Test whether this network applies to this UID.
1358 * @hide
1359 */
1360 public boolean appliesToUid(int uid) {
1361 if (null == mUids) return true;
1362 for (UidRange range : mUids) {
1363 if (range.contains(uid)) {
1364 return true;
1365 }
1366 }
1367 return false;
1368 }
1369
1370 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001371 * 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 +09001372 * <p>
1373 * This test only checks whether equal range objects are in both sets. It will
1374 * return false if the ranges are not exactly the same, even if the covered UIDs
1375 * are for an equivalent result.
1376 * <p>
1377 * Note that this method is not very optimized, which is fine as long as it's not used very
1378 * often.
1379 * <p>
1380 * nc is assumed nonnull.
1381 *
1382 * @hide
1383 */
1384 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001385 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001386 Set<UidRange> comparedUids = nc.mUids;
1387 if (null == comparedUids) return null == mUids;
1388 if (null == mUids) return false;
1389 // Make a copy so it can be mutated to check that all ranges in mUids
1390 // also are in uids.
1391 final Set<UidRange> uids = new ArraySet<>(mUids);
1392 for (UidRange range : comparedUids) {
1393 if (!uids.contains(range)) {
1394 return false;
1395 }
1396 uids.remove(range);
1397 }
1398 return uids.isEmpty();
1399 }
1400
1401 /**
1402 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1403 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001404 * This method is called on the NetworkCapabilities embedded in a request with the
1405 * capabilities of an available network. It checks whether all the UIDs from this listen
1406 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1407 * in the passed nc (representing the UIDs that this network is available to).
1408 * <p>
1409 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001410 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001411 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1412 * can see its own network when it listens for it.
1413 * <p>
1414 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001415 * @see #appliesToUid
1416 * @hide
1417 */
paulhud9736de2019-03-08 16:35:20 +08001418 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001419 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001420 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001421 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001422 if (!nc.appliesToUidRange(requiredRange)) {
1423 return false;
1424 }
1425 }
1426 return true;
1427 }
1428
1429 /**
1430 * Returns whether this network applies to the passed ranges.
1431 * This assumes that to apply, the passed range has to be entirely contained
1432 * within one of the ranges this network applies to. If the ranges are not normalized,
1433 * this method may return false even though all required UIDs are covered because no
1434 * single range contained them all.
1435 * @hide
1436 */
1437 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001438 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001439 if (null == mUids) return true;
1440 for (UidRange uidRange : mUids) {
1441 if (uidRange.containsRange(requiredRange)) {
1442 return true;
1443 }
1444 }
1445 return false;
1446 }
1447
1448 /**
1449 * Combine the UIDs this network currently applies to with the UIDs the passed
1450 * NetworkCapabilities apply to.
1451 * nc is assumed nonnull.
1452 */
paulhud9736de2019-03-08 16:35:20 +08001453 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001454 if (null == nc.mUids || null == mUids) {
1455 mUids = null;
1456 return;
1457 }
1458 mUids.addAll(nc.mUids);
1459 }
1460
Chalard Jeanb03a6222018-04-11 21:09:10 +09001461
1462 /**
1463 * The SSID of the network, or null if not applicable or unknown.
1464 * <p>
1465 * This is filled in by wifi code.
1466 * @hide
1467 */
1468 private String mSSID;
1469
1470 /**
1471 * Sets the SSID of this network.
1472 * @hide
1473 */
paulhud9736de2019-03-08 16:35:20 +08001474 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001475 mSSID = ssid;
1476 return this;
1477 }
1478
1479 /**
1480 * Gets the SSID of this network, or null if none or unknown.
1481 * @hide
1482 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001483 @SystemApi
Chalard Jeane5e38502020-03-18 15:58:50 +09001484 @TestApi
1485 public @Nullable String getSsid() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001486 return mSSID;
1487 }
1488
1489 /**
1490 * Tests if the SSID of this network is the same as the SSID of the passed network.
1491 * @hide
1492 */
paulhud9736de2019-03-08 16:35:20 +08001493 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001494 return Objects.equals(mSSID, nc.mSSID);
1495 }
1496
1497 /**
1498 * Check if the SSID requirements of this object are matched by the passed object.
1499 * @hide
1500 */
paulhud9736de2019-03-08 16:35:20 +08001501 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001502 return mSSID == null || mSSID.equals(nc.mSSID);
1503 }
1504
1505 /**
1506 * Combine SSIDs of the capabilities.
1507 * <p>
1508 * This is only legal if either the SSID of this object is null, or both SSIDs are
1509 * equal.
1510 * @hide
1511 */
paulhud9736de2019-03-08 16:35:20 +08001512 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001513 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1514 throw new IllegalStateException("Can't combine two SSIDs");
1515 }
1516 setSSID(nc.mSSID);
1517 }
1518
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001519 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001520 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1521 * <p>
1522 * Note that this method may break an invariant of having a particular capability in either
1523 * wanted or unwanted lists but never in both. Requests that have the same capability in
1524 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001525 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001526 */
paulhud9736de2019-03-08 16:35:20 +08001527 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001528 combineNetCapabilities(nc);
1529 combineTransportTypes(nc);
1530 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001531 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001532 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001533 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001534 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001535 combineSSIDs(nc);
Roshan Piuse38acab2020-01-16 12:17:17 -08001536 combineRequestor(nc);
Chalard Jean981dcca2020-02-06 18:31:19 +09001537 combineAdministratorUids(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001538 }
1539
1540 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001541 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1542 *
1543 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1544 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1545 * bandwidth, signal strength, or validation / captive portal status.
1546 *
1547 * @hide
1548 */
1549 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001550 return (nc != null
1551 && satisfiedByNetCapabilities(nc, onlyImmutable)
1552 && satisfiedByTransportTypes(nc)
1553 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1554 && satisfiedBySpecifier(nc)
1555 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001556 && (onlyImmutable || satisfiedByUids(nc))
Roshan Piuse38acab2020-01-16 12:17:17 -08001557 && (onlyImmutable || satisfiedBySSID(nc)))
1558 && (onlyImmutable || satisfiedByRequestor(nc));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001559 }
1560
1561 /**
1562 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1563 *
1564 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1565 *
1566 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001567 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001568 @TestApi
1569 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001570 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001571 return satisfiedByNetworkCapabilities(nc, false);
1572 }
1573
1574 /**
1575 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1576 *
1577 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1578 *
1579 * @hide
1580 */
paulhud9736de2019-03-08 16:35:20 +08001581 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001582 return satisfiedByNetworkCapabilities(nc, true);
1583 }
1584
1585 /**
1586 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001587 * {@code NetworkCapabilities} and return a String describing any difference.
1588 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001589 *
1590 * @hide
1591 */
paulhud9736de2019-03-08 16:35:20 +08001592 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001593 if (that == null) {
1594 return "other NetworkCapabilities was null";
1595 }
1596
1597 StringJoiner joiner = new StringJoiner(", ");
1598
Hugo Benichieae7a222017-07-25 11:40:56 +09001599 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1600 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001601 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001602 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1603 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1604 if (oldImmutableCapabilities != newImmutableCapabilities) {
1605 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1606 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1607 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1608 }
1609
1610 if (!equalsSpecifier(that)) {
1611 NetworkSpecifier before = this.getNetworkSpecifier();
1612 NetworkSpecifier after = that.getNetworkSpecifier();
1613 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1614 }
1615
1616 if (!equalsTransportTypes(that)) {
1617 String before = transportNamesOf(this.getTransportTypes());
1618 String after = transportNamesOf(that.getTransportTypes());
1619 joiner.add(String.format("transports changed: %s -> %s", before, after));
1620 }
1621
1622 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001623 }
1624
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001625 /**
1626 * Checks that our requestable capabilities are the same as those of the given
1627 * {@code NetworkCapabilities}.
1628 *
1629 * @hide
1630 */
paulhud9736de2019-03-08 16:35:20 +08001631 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001632 if (nc == null) return false;
1633 return (equalsNetCapabilitiesRequestable(nc) &&
1634 equalsTransportTypes(nc) &&
1635 equalsSpecifier(nc));
1636 }
1637
Robert Greenwalt1448f052014-04-08 13:41:39 -07001638 @Override
paulhud9736de2019-03-08 16:35:20 +08001639 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001640 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001641 NetworkCapabilities that = (NetworkCapabilities) obj;
Roshan Piuse38acab2020-01-16 12:17:17 -08001642 return equalsNetCapabilities(that)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001643 && equalsTransportTypes(that)
1644 && equalsLinkBandwidths(that)
1645 && equalsSignalStrength(that)
1646 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001647 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001648 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001649 && equalsSSID(that)
Roshan Piuse38acab2020-01-16 12:17:17 -08001650 && equalsPrivateDnsBroken(that)
Chalard Jean981dcca2020-02-06 18:31:19 +09001651 && equalsRequestor(that)
1652 && equalsAdministratorUids(that);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001653 }
1654
1655 @Override
1656 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001657 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001658 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001659 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1660 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1661 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1662 + ((int) (mTransportTypes >> 32) * 13)
1663 + (mLinkUpBandwidthKbps * 17)
1664 + (mLinkDownBandwidthKbps * 19)
1665 + Objects.hashCode(mNetworkSpecifier) * 23
1666 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001667 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001668 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001669 + Objects.hashCode(mTransportInfo) * 41
Roshan Piuse38acab2020-01-16 12:17:17 -08001670 + Objects.hashCode(mPrivateDnsBroken) * 43
1671 + Objects.hashCode(mRequestorUid) * 47
Chalard Jean981dcca2020-02-06 18:31:19 +09001672 + Objects.hashCode(mRequestorPackageName) * 53
1673 + Arrays.hashCode(mAdministratorUids) * 59;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001674 }
1675
Wink Saville4e2dea72014-09-20 11:04:03 -07001676 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001677 public int describeContents() {
1678 return 0;
1679 }
Cody Kesting201fc132020-01-17 11:58:36 -08001680
Wink Saville4e2dea72014-09-20 11:04:03 -07001681 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001682 public void writeToParcel(Parcel dest, int flags) {
1683 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001684 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001685 dest.writeLong(mTransportTypes);
1686 dest.writeInt(mLinkUpBandwidthKbps);
1687 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001688 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001689 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001690 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001691 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001692 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001693 dest.writeBoolean(mPrivateDnsBroken);
Chalard Jean981dcca2020-02-06 18:31:19 +09001694 dest.writeIntArray(getAdministratorUids());
Qingxi Li7cf06622020-01-17 17:54:27 -08001695 dest.writeInt(mOwnerUid);
Roshan Piuse38acab2020-01-16 12:17:17 -08001696 dest.writeInt(mRequestorUid);
1697 dest.writeString(mRequestorPackageName);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001698 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001699
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001700 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001701 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001702 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001703 public NetworkCapabilities createFromParcel(Parcel in) {
1704 NetworkCapabilities netCap = new NetworkCapabilities();
1705
1706 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001707 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001708 netCap.mTransportTypes = in.readLong();
1709 netCap.mLinkUpBandwidthKbps = in.readInt();
1710 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001711 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001712 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001713 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001714 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1715 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001716 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001717 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kestingf7ac9962020-03-16 18:15:28 -07001718 netCap.setAdministratorUids(in.createIntArray());
Qingxi Li7cf06622020-01-17 17:54:27 -08001719 netCap.mOwnerUid = in.readInt();
Roshan Piuse38acab2020-01-16 12:17:17 -08001720 netCap.mRequestorUid = in.readInt();
1721 netCap.mRequestorPackageName = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001722 return netCap;
1723 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001724 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001725 public NetworkCapabilities[] newArray(int size) {
1726 return new NetworkCapabilities[size];
1727 }
1728 };
1729
Wink Saville4e2dea72014-09-20 11:04:03 -07001730 @Override
paulhud9736de2019-03-08 16:35:20 +08001731 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001732 final StringBuilder sb = new StringBuilder("[");
1733 if (0 != mTransportTypes) {
1734 sb.append(" Transports: ");
1735 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1736 NetworkCapabilities::transportNameOf, "|");
1737 }
1738 if (0 != mNetworkCapabilities) {
1739 sb.append(" Capabilities: ");
1740 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1741 NetworkCapabilities::capabilityNameOf, "&");
1742 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001743 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001744 sb.append(" Unwanted: ");
1745 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1746 NetworkCapabilities::capabilityNameOf, "&");
1747 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001748 if (mLinkUpBandwidthKbps > 0) {
1749 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1750 }
1751 if (mLinkDownBandwidthKbps > 0) {
1752 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1753 }
1754 if (mNetworkSpecifier != null) {
1755 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1756 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001757 if (mTransportInfo != null) {
1758 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1759 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001760 if (hasSignalStrength()) {
1761 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001762 }
1763
Chalard Jean07ace0f2018-02-26 19:00:45 +09001764 if (null != mUids) {
1765 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1766 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1767 } else {
1768 sb.append(" Uids: <").append(mUids).append(">");
1769 }
1770 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001771 if (mOwnerUid != Process.INVALID_UID) {
1772 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001773 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001774
Cody Kestingf7ac9962020-03-16 18:15:28 -07001775 if (mAdministratorUids.length == 0) {
1776 sb.append(" AdministratorUids: ").append(Arrays.toString(mAdministratorUids));
Cody Kesting201fc132020-01-17 11:58:36 -08001777 }
1778
Chalard Jeanb03a6222018-04-11 21:09:10 +09001779 if (null != mSSID) {
1780 sb.append(" SSID: ").append(mSSID);
1781 }
1782
lucaslin783f2212019-10-22 18:27:33 +08001783 if (mPrivateDnsBroken) {
1784 sb.append(" Private DNS is broken");
1785 }
1786
Roshan Piuse38acab2020-01-16 12:17:17 -08001787 sb.append(" RequestorUid: ").append(mRequestorUid);
1788 sb.append(" RequestorPackageName: ").append(mRequestorPackageName);
1789
Chalard Jean07ace0f2018-02-26 19:00:45 +09001790 sb.append("]");
1791 return sb.toString();
1792 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001793
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001794
Chalard Jean07ace0f2018-02-26 19:00:45 +09001795 private interface NameOf {
1796 String nameOf(int value);
1797 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001798
Chalard Jean07ace0f2018-02-26 19:00:45 +09001799 /**
1800 * @hide
1801 */
paulhud9736de2019-03-08 16:35:20 +08001802 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1803 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001804 int bitPos = 0;
1805 boolean firstElementAdded = false;
1806 while (bitMask != 0) {
1807 if ((bitMask & 1) != 0) {
1808 if (firstElementAdded) {
1809 sb.append(separator);
1810 } else {
1811 firstElementAdded = true;
1812 }
1813 sb.append(nameFetcher.nameOf(bitPos));
1814 }
1815 bitMask >>= 1;
1816 ++bitPos;
1817 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001818 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001819
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001820 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001821 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001822 final long token = proto.start(fieldId);
1823
1824 for (int transport : getTransportTypes()) {
1825 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1826 }
1827
1828 for (int capability : getCapabilities()) {
1829 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1830 }
1831
1832 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1833 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1834
1835 if (mNetworkSpecifier != null) {
1836 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1837 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001838 if (mTransportInfo != null) {
1839 // TODO b/120653863: write transport-specific info to proto?
1840 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001841
1842 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1843 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1844
1845 proto.end(token);
1846 }
1847
Hugo Benichi5df9d722016-04-25 17:16:35 +09001848 /**
1849 * @hide
1850 */
paulhud9736de2019-03-08 16:35:20 +08001851 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001852 StringJoiner joiner = new StringJoiner("|");
1853 if (capabilities != null) {
1854 for (int c : capabilities) {
1855 joiner.add(capabilityNameOf(c));
1856 }
1857 }
1858 return joiner.toString();
1859 }
1860
1861 /**
1862 * @hide
1863 */
paulhud9736de2019-03-08 16:35:20 +08001864 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001865 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001866 case NET_CAPABILITY_MMS: return "MMS";
1867 case NET_CAPABILITY_SUPL: return "SUPL";
1868 case NET_CAPABILITY_DUN: return "DUN";
1869 case NET_CAPABILITY_FOTA: return "FOTA";
1870 case NET_CAPABILITY_IMS: return "IMS";
1871 case NET_CAPABILITY_CBS: return "CBS";
1872 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1873 case NET_CAPABILITY_IA: return "IA";
1874 case NET_CAPABILITY_RCS: return "RCS";
1875 case NET_CAPABILITY_XCAP: return "XCAP";
1876 case NET_CAPABILITY_EIMS: return "EIMS";
1877 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1878 case NET_CAPABILITY_INTERNET: return "INTERNET";
1879 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1880 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1881 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1882 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1883 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1884 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1885 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1886 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1887 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1888 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1889 case NET_CAPABILITY_MCX: return "MCX";
1890 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
Jack Yu30be5e52020-04-02 15:34:33 -07001891 case NET_CAPABILITY_TEMPORARILY_NOT_METERED: return "TEMPORARILY_NOT_METERED";
lucasline252a742019-03-12 13:08:03 +08001892 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001893 }
1894 }
1895
1896 /**
1897 * @hide
1898 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001899 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001900 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001901 StringJoiner joiner = new StringJoiner("|");
1902 if (types != null) {
1903 for (int t : types) {
1904 joiner.add(transportNameOf(t));
1905 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001906 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001907 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001908 }
1909
1910 /**
1911 * @hide
1912 */
paulhud9736de2019-03-08 16:35:20 +08001913 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001914 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001915 return "UNKNOWN";
1916 }
1917 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001918 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001919
Jeff Sharkeyde570312017-10-24 21:25:50 -06001920 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001921 Preconditions.checkArgument(
1922 isValidTransport(transport), "Invalid TransportType " + transport);
1923 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001924
1925 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1926 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1927 }
1928
1929 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1930 Preconditions.checkArgument(isValidCapability(capability),
1931 "NetworkCapability " + capability + "out of range");
1932 }
junyulai05986c62018-08-07 19:50:45 +08001933
1934 /**
1935 * Check if this {@code NetworkCapability} instance is metered.
1936 *
1937 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1938 * @hide
1939 */
1940 public boolean isMetered() {
1941 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1942 }
lucaslin783f2212019-10-22 18:27:33 +08001943
1944 /**
1945 * Check if private dns is broken.
1946 *
1947 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1948 * @hide
1949 */
1950 public boolean isPrivateDnsBroken() {
1951 return mPrivateDnsBroken;
1952 }
1953
1954 /**
1955 * Set mPrivateDnsBroken to true when private dns is broken.
1956 *
1957 * @param broken the status of private DNS to be set.
1958 * @hide
1959 */
1960 public void setPrivateDnsBroken(boolean broken) {
1961 mPrivateDnsBroken = broken;
1962 }
1963
1964 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1965 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1966 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001967
1968 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001969 * Set the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001970 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001971 * For instances of NetworkCapabilities representing a request, sets the
1972 * UID of the app making the request. For a network created by the system,
1973 * sets the UID of the only app whose requests can match this network.
1974 * This can be set to {@link Process#INVALID_UID} if there is no such app,
1975 * or if this instance of NetworkCapabilities is about to be sent to a
1976 * party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08001977 *
1978 * @param uid UID of the app.
1979 * @hide
1980 */
Roshan Piuse38acab2020-01-16 12:17:17 -08001981 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1982 mRequestorUid = uid;
1983 return this;
1984 }
1985
1986 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09001987 * Returns the UID of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001988 *
Chalard Jeane5e38502020-03-18 15:58:50 +09001989 * For a NetworkRequest being made by an app, contains the app's UID. For a network
1990 * created by the system, contains the UID of the only app whose requests can match
1991 * this network, or {@link Process#INVALID_UID} if none or if the
1992 * caller does not have permission to learn about this.
1993 *
1994 * @return the uid of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08001995 * @hide
1996 */
1997 public int getRequestorUid() {
1998 return mRequestorUid;
1999 }
2000
2001 /**
2002 * Set the package name of the app making the request.
2003 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002004 * For instances of NetworkCapabilities representing a request, sets the
2005 * package name of the app making the request. For a network created by the system,
2006 * sets the package name of the only app whose requests can match this network.
2007 * This can be set to null if there is no such app, or if this instance of
2008 * NetworkCapabilities is about to be sent to a party that should not learn about this.
Roshan Piuse38acab2020-01-16 12:17:17 -08002009 *
2010 * @param packageName package name of the app.
2011 * @hide
2012 */
Roshan Piuse38acab2020-01-16 12:17:17 -08002013 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
2014 mRequestorPackageName = packageName;
2015 return this;
2016 }
2017
2018 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09002019 * Returns the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08002020 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002021 * For a NetworkRequest being made by an app, contains the app's package name. For a
2022 * network created by the system, contains the package name of the only app whose
2023 * requests can match this network, or null if none or if the caller does not have
2024 * permission to learn about this.
2025 *
2026 * @return the package name of the app making the request.
Roshan Piuse38acab2020-01-16 12:17:17 -08002027 * @hide
2028 */
2029 @Nullable
2030 public String getRequestorPackageName() {
2031 return mRequestorPackageName;
2032 }
2033
2034 /**
Chalard Jeane5e38502020-03-18 15:58:50 +09002035 * Set the uid and package name of the app causing this network to exist.
Roshan Piuse38acab2020-01-16 12:17:17 -08002036 *
Chalard Jeane5e38502020-03-18 15:58:50 +09002037 * {@see #setRequestorUid} and {@link #setRequestorPackageName}
Roshan Piuse38acab2020-01-16 12:17:17 -08002038 *
2039 * @param uid UID of the app.
2040 * @param packageName package name of the app.
2041 * @hide
2042 */
2043 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
2044 int uid, @NonNull String packageName) {
2045 return setRequestorUid(uid).setRequestorPackageName(packageName);
2046 }
2047
2048 /**
2049 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
2050 * capabilities.
2051 *
2052 * This method is called on the NetworkCapabilities embedded in a request with the
2053 * capabilities of an available network. If the available network, sets a specific
2054 * requestor (by uid and optionally package name), then this will only match a request from the
2055 * same app. If either of the capabilities have an unset uid or package name, then it matches
2056 * everything.
2057 * <p>
2058 * nc is assumed nonnull. Else, NPE.
2059 */
2060 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
2061 // No uid set, matches everything.
2062 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
2063 return true;
2064 }
2065 // uids don't match.
2066 if (mRequestorUid != nc.mRequestorUid) return false;
2067 // No package names set, matches everything
2068 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
2069 // check for package name match.
2070 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2071 }
2072
2073 /**
2074 * Combine requestor info of the capabilities.
2075 * <p>
2076 * This is only legal if either the requestor info of this object is reset, or both info are
2077 * equal.
2078 * nc is assumed nonnull.
2079 */
2080 private void combineRequestor(@NonNull NetworkCapabilities nc) {
2081 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
2082 throw new IllegalStateException("Can't combine two uids");
2083 }
2084 if (mRequestorPackageName != null
2085 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
2086 throw new IllegalStateException("Can't combine two package names");
2087 }
2088 setRequestorUid(nc.mRequestorUid);
2089 setRequestorPackageName(nc.mRequestorPackageName);
2090 }
2091
2092 private boolean equalsRequestor(NetworkCapabilities nc) {
2093 return mRequestorUid == nc.mRequestorUid
2094 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
2095 }
Chalard Jeane5e38502020-03-18 15:58:50 +09002096
2097 /**
2098 * Builder class for NetworkCapabilities.
2099 *
2100 * This class is mainly for for {@link NetworkAgent} instances to use. Many fields in
2101 * the built class require holding a signature permission to use - mostly
2102 * {@link android.Manifest.permission.NETWORK_FACTORY}, but refer to the specific
2103 * description of each setter. As this class lives entirely in app space it does not
2104 * enforce these restrictions itself but the system server clears out the relevant
2105 * fields when receiving a NetworkCapabilities object from a caller without the
2106 * appropriate permission.
2107 *
2108 * Apps don't use this builder directly. Instead, they use {@link NetworkRequest} via
2109 * its builder object.
2110 *
2111 * @hide
2112 */
2113 @SystemApi
2114 @TestApi
Aaron Huangfbb485a2020-03-25 13:36:38 +08002115 public static final class Builder {
Chalard Jeane5e38502020-03-18 15:58:50 +09002116 private final NetworkCapabilities mCaps;
2117
2118 /**
2119 * Creates a new Builder to construct NetworkCapabilities objects.
2120 */
2121 public Builder() {
2122 mCaps = new NetworkCapabilities();
2123 }
2124
2125 /**
2126 * Creates a new Builder of NetworkCapabilities from an existing instance.
2127 */
2128 public Builder(@NonNull final NetworkCapabilities nc) {
2129 Objects.requireNonNull(nc);
2130 mCaps = new NetworkCapabilities(nc);
2131 }
2132
2133 /**
2134 * Adds the given transport type.
2135 *
2136 * Multiple transports may be added. Note that when searching for a network to satisfy a
2137 * request, satisfying any of the transports listed in the request will satisfy the request.
2138 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
2139 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
2140 * to be selected. This is logically different than
2141 * {@code NetworkCapabilities.NET_CAPABILITY_*}.
2142 *
2143 * @param transportType the transport type to be added or removed.
2144 * @return this builder
2145 */
2146 @NonNull
2147 public Builder addTransportType(@Transport int transportType) {
2148 checkValidTransportType(transportType);
2149 mCaps.addTransportType(transportType);
2150 return this;
2151 }
2152
2153 /**
2154 * Removes the given transport type.
2155 *
2156 * {@see #addTransportType}.
2157 *
2158 * @param transportType the transport type to be added or removed.
2159 * @return this builder
2160 */
2161 @NonNull
2162 public Builder removeTransportType(@Transport int transportType) {
2163 checkValidTransportType(transportType);
2164 mCaps.removeTransportType(transportType);
2165 return this;
2166 }
2167
2168 /**
2169 * Adds the given capability.
2170 *
2171 * @param capability the capability
2172 * @return this builder
2173 */
2174 @NonNull
2175 public Builder addCapability(@NetCapability final int capability) {
2176 mCaps.setCapability(capability, true);
2177 return this;
2178 }
2179
2180 /**
2181 * Removes the given capability.
2182 *
2183 * @param capability the capability
2184 * @return this builder
2185 */
2186 @NonNull
2187 public Builder removeCapability(@NetCapability final int capability) {
2188 mCaps.setCapability(capability, false);
2189 return this;
2190 }
2191
2192 /**
2193 * Sets the owner UID.
2194 *
2195 * The default value is {@link Process#INVALID_UID}. Pass this value to reset.
2196 *
2197 * Note: for security the system will clear out this field when received from a
2198 * non-privileged source.
2199 *
2200 * @param ownerUid the owner UID
2201 * @return this builder
2202 */
2203 @NonNull
2204 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2205 public Builder setOwnerUid(final int ownerUid) {
2206 mCaps.setOwnerUid(ownerUid);
2207 return this;
2208 }
2209
2210 /**
2211 * Sets the list of UIDs that are administrators of this network.
2212 *
2213 * <p>UIDs included in administratorUids gain administrator privileges over this
2214 * Network. Examples of UIDs that should be included in administratorUids are:
2215 * <ul>
2216 * <li>Carrier apps with privileges for the relevant subscription
2217 * <li>Active VPN apps
2218 * <li>Other application groups with a particular Network-related role
2219 * </ul>
2220 *
2221 * <p>In general, user-supplied networks (such as WiFi networks) do not have
2222 * administrators.
2223 *
2224 * <p>An app is granted owner privileges over Networks that it supplies. The owner
2225 * UID MUST always be included in administratorUids.
2226 *
2227 * The default value is the empty array. Pass an empty array to reset.
2228 *
2229 * Note: for security the system will clear out this field when received from a
2230 * non-privileged source, such as an app using reflection to call this or
2231 * mutate the member in the built object.
2232 *
2233 * @param administratorUids the UIDs to be set as administrators of this Network.
2234 * @return this builder
2235 */
2236 @NonNull
2237 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2238 public Builder setAdministratorUids(@NonNull final int[] administratorUids) {
2239 Objects.requireNonNull(administratorUids);
2240 mCaps.setAdministratorUids(administratorUids);
2241 return this;
2242 }
2243
2244 /**
2245 * Sets the upstream bandwidth of the link.
2246 *
2247 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
2248 * the estimated first hop transport bandwidth.
2249 * <p>
2250 * Note that when used to request a network, this specifies the minimum acceptable.
2251 * When received as the state of an existing network this specifies the typical
2252 * first hop bandwidth expected. This is never measured, but rather is inferred
2253 * from technology type and other link parameters. It could be used to differentiate
2254 * between very slow 1xRTT cellular links and other faster networks or even between
2255 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2256 * fast backhauls and slow backhauls.
2257 *
2258 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
2259 * @return this builder
2260 */
2261 @NonNull
2262 public Builder setLinkUpstreamBandwidthKbps(final int upKbps) {
2263 mCaps.setLinkUpstreamBandwidthKbps(upKbps);
2264 return this;
2265 }
2266
2267 /**
2268 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
2269 * the estimated first hop transport bandwidth.
2270 * <p>
2271 * Note that when used to request a network, this specifies the minimum acceptable.
2272 * When received as the state of an existing network this specifies the typical
2273 * first hop bandwidth expected. This is never measured, but rather is inferred
2274 * from technology type and other link parameters. It could be used to differentiate
2275 * between very slow 1xRTT cellular links and other faster networks or even between
2276 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
2277 * fast backhauls and slow backhauls.
2278 *
2279 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
2280 * @return this builder
2281 */
2282 @NonNull
2283 public Builder setLinkDownstreamBandwidthKbps(final int downKbps) {
2284 mCaps.setLinkDownstreamBandwidthKbps(downKbps);
2285 return this;
2286 }
2287
2288 /**
2289 * Sets the optional bearer specific network specifier.
2290 * This has no meaning if a single transport is also not specified, so calling
2291 * this without a single transport set will generate an exception, as will
2292 * subsequently adding or removing transports after this is set.
2293 * </p>
2294 *
2295 * @param specifier a concrete, parcelable framework class that extends NetworkSpecifier,
2296 * or null to clear it.
2297 * @return this builder
2298 */
2299 @NonNull
2300 public Builder setNetworkSpecifier(@Nullable final NetworkSpecifier specifier) {
2301 mCaps.setNetworkSpecifier(specifier);
2302 return this;
2303 }
2304
2305 /**
2306 * Sets the optional transport specific information.
2307 *
2308 * @param info A concrete, parcelable framework class that extends {@link TransportInfo},
2309 * or null to clear it.
2310 * @return this builder
2311 */
2312 @NonNull
2313 public Builder setTransportInfo(@Nullable final TransportInfo info) {
2314 mCaps.setTransportInfo(info);
2315 return this;
2316 }
2317
2318 /**
2319 * Sets the signal strength. This is a signed integer, with higher values indicating a
2320 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the
2321 * same RSSI units reported by wifi code.
2322 * <p>
2323 * Note that when used to register a network callback, this specifies the minimum
2324 * acceptable signal strength. When received as the state of an existing network it
2325 * specifies the current value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means
2326 * no value when received and has no effect when requesting a callback.
2327 *
2328 * Note: for security the system will throw if it receives a NetworkRequest where
2329 * the underlying NetworkCapabilities has this member set from a source that does
2330 * not hold the {@link android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP}
2331 * permission. Apps with this permission can use this indirectly through
2332 * {@link android.net.NetworkRequest}.
2333 *
2334 * @param signalStrength the bearer-specific signal strength.
2335 * @return this builder
2336 */
2337 @NonNull
2338 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP)
2339 public Builder setSignalStrength(final int signalStrength) {
2340 mCaps.setSignalStrength(signalStrength);
2341 return this;
2342 }
2343
2344 /**
2345 * Sets the SSID of this network.
2346 *
2347 * Note: for security the system will clear out this field when received from a
2348 * non-privileged source, like an app using reflection to set this.
2349 *
2350 * @param ssid the SSID, or null to clear it.
2351 * @return this builder
2352 */
2353 @NonNull
2354 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2355 public Builder setSsid(@Nullable final String ssid) {
2356 mCaps.setSSID(ssid);
2357 return this;
2358 }
2359
2360 /**
2361 * Set the uid of the app causing this network to exist.
2362 *
2363 * Note: for security the system will clear out this field when received from a
2364 * non-privileged source.
2365 *
2366 * @param uid UID of the app.
2367 * @return this builder
2368 */
2369 @NonNull
2370 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2371 public Builder setRequestorUid(final int uid) {
2372 mCaps.setRequestorUid(uid);
2373 return this;
2374 }
2375
2376 /**
2377 * Set the package name of the app causing this network to exist.
2378 *
2379 * Note: for security the system will clear out this field when received from a
2380 * non-privileged source.
2381 *
2382 * @param packageName package name of the app, or null to clear it.
2383 * @return this builder
2384 */
2385 @NonNull
2386 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
2387 public Builder setRequestorPackageName(@Nullable final String packageName) {
2388 mCaps.setRequestorPackageName(packageName);
2389 return this;
2390 }
2391
2392 /**
2393 * Builds the instance of the capabilities.
2394 *
2395 * @return the built instance of NetworkCapabilities.
2396 */
2397 @NonNull
2398 public NetworkCapabilities build() {
2399 if (mCaps.getOwnerUid() != Process.INVALID_UID) {
2400 if (!ArrayUtils.contains(mCaps.getAdministratorUids(), mCaps.getOwnerUid())) {
2401 throw new IllegalStateException("The owner UID must be included in "
2402 + " administrator UIDs.");
2403 }
2404 }
2405 return new NetworkCapabilities(mCaps);
2406 }
2407 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07002408}