blob: 116e343ff2505e7c73d535f907022fe90b8b155e [file] [log] [blame]
Robert Greenwalt1448f052014-04-08 13:41:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Jeff Sharkeyde570312017-10-24 21:25:50 -060019import android.annotation.IntDef;
paulhud9736de2019-03-08 16:35:20 +080020import android.annotation.NonNull;
Etan Cohenca9fb562018-11-27 07:32:39 -080021import android.annotation.Nullable;
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -070022import android.annotation.SystemApi;
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -060023import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000024import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey72f9c422017-10-27 17:22:59 -060025import android.net.ConnectivityManager.NetworkCallback;
Mathew Inwood45d2c252018-09-14 12:35:36 +010026import android.os.Build;
Robert Greenwalt1448f052014-04-08 13:41:39 -070027import android.os.Parcel;
28import android.os.Parcelable;
Qingxi Li7cf06622020-01-17 17:54:27 -080029import android.os.Process;
Roshan Piuse38acab2020-01-16 12:17:17 -080030import android.text.TextUtils;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090031import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080032import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070033
34import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090035import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090036import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070037
Jeff Sharkeyde570312017-10-24 21:25:50 -060038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Cody Kesting201fc132020-01-17 11:58:36 -080040import java.util.ArrayList;
41import java.util.Collections;
42import java.util.List;
Etan Cohena7434272017-04-03 12:17:51 -070043import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090044import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090045import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070046
47/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070048 * Representation of the capabilities of an active network. Instances are
49 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060050 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
51 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060052 * <p>
53 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
54 * network selection. Rather than indicate a need for Wi-Fi because an
55 * application needs high bandwidth and risk obsolescence when a new, fast
56 * network appears (like LTE), the application should specify it needs high
57 * bandwidth. Similarly if an application needs an unmetered network for a bulk
58 * transfer it can specify that rather than assuming all cellular based
59 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070060 */
61public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070062 private static final String TAG = "NetworkCapabilities";
63
lucaslin783f2212019-10-22 18:27:33 +080064 // Set to true when private DNS is broken.
65 private boolean mPrivateDnsBroken;
66
Roshan Piuse38acab2020-01-16 12:17:17 -080067 /**
68 * Uid of the app making the request.
69 */
70 private int mRequestorUid;
71
72 /**
73 * Package name of the app making the request.
74 */
75 private String mRequestorPackageName;
76
Robert Greenwalt01d004e2014-05-18 15:24:21 -070077 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090078 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090079 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070080 }
81
82 public NetworkCapabilities(NetworkCapabilities nc) {
83 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090084 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070085 }
86 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070087
88 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090089 * Completely clears the contents of this object, removing even the capabilities that are set
90 * by default when the object is constructed.
Lorenzo Colittif7058f52015-04-27 11:31:55 +090091 */
92 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080093 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070094 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090095 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080096 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090097 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090098 mUids = null;
Cody Kesting201fc132020-01-17 11:58:36 -080099 mAdministratorUids.clear();
Qingxi Li7cf06622020-01-17 17:54:27 -0800100 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +0900101 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +0800102 mPrivateDnsBroken = false;
Roshan Piuse38acab2020-01-16 12:17:17 -0800103 mRequestorUid = Process.INVALID_UID;
104 mRequestorPackageName = null;
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900105 }
106
107 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +0900108 * Set all contents of this object to the contents of a NetworkCapabilities.
109 * @hide
110 */
paulhud9736de2019-03-08 16:35:20 +0800111 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900112 mNetworkCapabilities = nc.mNetworkCapabilities;
113 mTransportTypes = nc.mTransportTypes;
114 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
115 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
116 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800117 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900118 mSignalStrength = nc.mSignalStrength;
119 setUids(nc.mUids); // Will make the defensive copy
Cody Kesting201fc132020-01-17 11:58:36 -0800120 setAdministratorUids(nc.mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -0800121 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900122 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
123 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800124 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Roshan Piuse38acab2020-01-16 12:17:17 -0800125 mRequestorUid = nc.mRequestorUid;
126 mRequestorPackageName = nc.mRequestorPackageName;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900127 }
128
129 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700130 * Represents the network's capabilities. If any are specified they will be satisfied
131 * by any Network that matches all of them.
132 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100133 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900134 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700135
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800136 /**
137 * If any capabilities specified here they must not exist in the matching Network.
138 */
139 private long mUnwantedNetworkCapabilities;
140
Jeff Sharkeyde570312017-10-24 21:25:50 -0600141 /** @hide */
142 @Retention(RetentionPolicy.SOURCE)
143 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
144 NET_CAPABILITY_MMS,
145 NET_CAPABILITY_SUPL,
146 NET_CAPABILITY_DUN,
147 NET_CAPABILITY_FOTA,
148 NET_CAPABILITY_IMS,
149 NET_CAPABILITY_CBS,
150 NET_CAPABILITY_WIFI_P2P,
151 NET_CAPABILITY_IA,
152 NET_CAPABILITY_RCS,
153 NET_CAPABILITY_XCAP,
154 NET_CAPABILITY_EIMS,
155 NET_CAPABILITY_NOT_METERED,
156 NET_CAPABILITY_INTERNET,
157 NET_CAPABILITY_NOT_RESTRICTED,
158 NET_CAPABILITY_TRUSTED,
159 NET_CAPABILITY_NOT_VPN,
160 NET_CAPABILITY_VALIDATED,
161 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600162 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600163 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900164 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900165 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800166 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800167 NET_CAPABILITY_MCX,
168 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600169 })
170 public @interface NetCapability { }
171
Robert Greenwalt1448f052014-04-08 13:41:39 -0700172 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700173 * Indicates this is a network that has the ability to reach the
174 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700175 */
176 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700177
178 /**
179 * Indicates this is a network that has the ability to reach the carrier's
180 * SUPL server, used to retrieve GPS information.
181 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700182 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700183
184 /**
185 * Indicates this is a network that has the ability to reach the carrier's
186 * DUN or tethering gateway.
187 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700188 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700189
190 /**
191 * Indicates this is a network that has the ability to reach the carrier's
192 * FOTA portal, used for over the air updates.
193 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195
196 /**
197 * Indicates this is a network that has the ability to reach the carrier's
198 * IMS servers, used for network registration and signaling.
199 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700200 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700201
202 /**
203 * Indicates this is a network that has the ability to reach the carrier's
204 * CBS servers, used for carrier specific services.
205 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700206 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700207
208 /**
209 * Indicates this is a network that has the ability to reach a Wi-Fi direct
210 * peer.
211 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700212 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700213
214 /**
215 * Indicates this is a network that has the ability to reach a carrier's
216 * Initial Attach servers.
217 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700218 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700219
220 /**
221 * Indicates this is a network that has the ability to reach a carrier's
222 * RCS servers, used for Rich Communication Services.
223 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700224 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700225
226 /**
227 * Indicates this is a network that has the ability to reach a carrier's
228 * XCAP servers, used for configuration and control.
229 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700230 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700231
232 /**
233 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700234 * Emergency IMS servers or other services, used for network signaling
235 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700236 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700237 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700238
239 /**
240 * Indicates that this network is unmetered.
241 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700242 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700243
244 /**
245 * Indicates that this network should be able to reach the internet.
246 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700247 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700248
249 /**
250 * Indicates that this network is available for general use. If this is not set
251 * applications should not attempt to communicate on this network. Note that this
252 * is simply informative and not enforcement - enforcement is handled via other means.
253 * Set by default.
254 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700255 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
256
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700257 /**
258 * Indicates that the user has indicated implicit trust of this network. This
259 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
260 * BT device or a wifi the user asked to connect to. Untrusted networks
261 * are probably limited to unknown wifi AP. Set by default.
262 */
263 public static final int NET_CAPABILITY_TRUSTED = 14;
264
Paul Jensen76b610a2015-03-18 09:33:07 -0400265 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400266 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400267 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400268 */
269 public static final int NET_CAPABILITY_NOT_VPN = 15;
270
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900271 /**
272 * Indicates that connectivity on this network was successfully validated. For example, for a
273 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
274 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900275 */
276 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700277
Paul Jensen3d194ea2015-06-16 14:27:36 -0400278 /**
279 * Indicates that this network was found to have a captive portal in place last time it was
280 * probed.
281 */
282 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
283
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900284 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600285 * Indicates that this network is not roaming.
286 */
287 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
288
289 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900290 * Indicates that this network is available for use by apps, and not a network that is being
291 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900292 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600293 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900294
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900295 /**
296 * Indicates that this network is not congested.
297 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600298 * When a network is congested, applications should defer network traffic
299 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900300 */
301 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
302
Chalard Jean804b8fb2018-01-30 22:41:41 +0900303 /**
304 * Indicates that this network is not currently suspended.
305 * <p>
306 * When a network is suspended, the network's IP addresses and any connections
307 * established on the network remain valid, but the network is temporarily unable
308 * to transfer data. This can happen, for example, if a cellular network experiences
309 * a temporary loss of signal, such as when driving through a tunnel, etc.
310 * A network with this capability is not suspended, so is expected to be able to
311 * transfer data.
312 */
313 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
314
Pavel Maltsev43403202018-01-30 17:19:44 -0800315 /**
316 * Indicates that traffic that goes through this network is paid by oem. For example,
317 * this network can be used by system apps to upload telemetry data.
318 * @hide
319 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700320 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800321 public static final int NET_CAPABILITY_OEM_PAID = 22;
322
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800323 /**
324 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
325 * servers.
326 */
327 public static final int NET_CAPABILITY_MCX = 23;
328
lucasline252a742019-03-12 13:08:03 +0800329 /**
330 * Indicates that this network was tested to only provide partial connectivity.
331 * @hide
332 */
333 @SystemApi
334 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
335
Robert Greenwalt1448f052014-04-08 13:41:39 -0700336 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800337 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700338
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700339 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900340 * Network capabilities that are expected to be mutable, i.e., can change while a particular
341 * network is connected.
342 */
343 private static final long MUTABLE_CAPABILITIES =
344 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
345 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900346 (1 << NET_CAPABILITY_TRUSTED)
347 | (1 << NET_CAPABILITY_VALIDATED)
348 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
349 | (1 << NET_CAPABILITY_NOT_ROAMING)
350 | (1 << NET_CAPABILITY_FOREGROUND)
351 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800352 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
353 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900354
355 /**
356 * Network capabilities that are not allowed in NetworkRequests. This exists because the
357 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
358 * capability's presence cannot be known in advance. If such a capability is requested, then we
359 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
360 * get immediately torn down because they do not have the requested capability.
361 */
362 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900363 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900364
365 /**
366 * Capabilities that are set by default when the object is constructed.
367 */
368 private static final long DEFAULT_CAPABILITIES =
369 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
370 (1 << NET_CAPABILITY_TRUSTED) |
371 (1 << NET_CAPABILITY_NOT_VPN);
372
373 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400374 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800375 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400376 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700377 @VisibleForTesting
378 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400379 (1 << NET_CAPABILITY_CBS) |
380 (1 << NET_CAPABILITY_DUN) |
381 (1 << NET_CAPABILITY_EIMS) |
382 (1 << NET_CAPABILITY_FOTA) |
383 (1 << NET_CAPABILITY_IA) |
384 (1 << NET_CAPABILITY_IMS) |
385 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800386 (1 << NET_CAPABILITY_XCAP) |
387 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800388
389 /**
390 * Capabilities that force network to be restricted.
391 * {@see #maybeMarkCapabilitiesRestricted}.
392 */
393 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800394 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400395
396 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700397 * Capabilities that suggest that a network is unrestricted.
398 * {@see #maybeMarkCapabilitiesRestricted}.
399 */
400 @VisibleForTesting
401 /* package */ static final long UNRESTRICTED_CAPABILITIES =
402 (1 << NET_CAPABILITY_INTERNET) |
403 (1 << NET_CAPABILITY_MMS) |
404 (1 << NET_CAPABILITY_SUPL) |
405 (1 << NET_CAPABILITY_WIFI_P2P);
406
407 /**
lucasline252a742019-03-12 13:08:03 +0800408 * Capabilities that are managed by ConnectivityService.
409 */
410 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
411 (1 << NET_CAPABILITY_VALIDATED)
412 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
413 | (1 << NET_CAPABILITY_FOREGROUND)
414 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
415
416 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700417 * Adds the given capability to this {@code NetworkCapability} instance.
418 * Multiple capabilities may be applied sequentially. Note that when searching
419 * for a network to satisfy a request, all capabilities requested must be satisfied.
420 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600421 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900422 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700423 */
paulhud9736de2019-03-08 16:35:20 +0800424 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800425 // If the given capability was previously added to the list of unwanted capabilities
426 // then the capability will also be removed from the list of unwanted capabilities.
427 // TODO: Consider adding unwanted capabilities to the public API and mention this
428 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800429 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700430 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800431 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700432 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700433 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700434
435 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800436 * Adds the given capability to the list of unwanted capabilities of this
437 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
438 * sequentially. Note that when searching for a network to satisfy a request, the network
439 * must not contain any capability from unwanted capability list.
440 * <p>
441 * If the capability was previously added to the list of required capabilities (for
442 * example, it was there by default or added using {@link #addCapability(int)} method), then
443 * it will be removed from the list of required capabilities as well.
444 *
445 * @see #addCapability(int)
446 * @hide
447 */
448 public void addUnwantedCapability(@NetCapability int capability) {
449 checkValidCapability(capability);
450 mUnwantedNetworkCapabilities |= 1 << capability;
451 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
452 }
453
454 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700455 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
456 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600457 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900458 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700459 */
paulhud9736de2019-03-08 16:35:20 +0800460 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800461 // Note that this method removes capabilities that were added via addCapability(int),
462 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800463 checkValidCapability(capability);
464 final long mask = ~(1 << capability);
465 mNetworkCapabilities &= mask;
466 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700467 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700468 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700469
470 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600471 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
472 * instance.
473 *
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600474 */
paulhud9736de2019-03-08 16:35:20 +0800475 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
476 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600477 if (value) {
478 addCapability(capability);
479 } else {
480 removeCapability(capability);
481 }
482 return this;
483 }
484
485 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700486 * Gets all the capabilities set on this {@code NetworkCapability} instance.
487 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600488 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700489 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700490 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000491 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600492 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600493 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900494 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700495 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700496
497 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800498 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
499 *
500 * @return an array of unwanted capability values for this instance.
501 * @hide
502 */
503 public @NetCapability int[] getUnwantedCapabilities() {
504 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
505 }
506
507
508 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600509 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700510 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600511 *
512 * @hide
513 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800514 public void setCapabilities(@NetCapability int[] capabilities,
515 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600516 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800517 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600518 }
519
520 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800521 * @deprecated use {@link #setCapabilities(int[], int[])}
522 * @hide
523 */
524 @Deprecated
525 public void setCapabilities(@NetCapability int[] capabilities) {
526 setCapabilities(capabilities, new int[] {});
527 }
528
529 /**
530 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700531 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600532 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700533 * @return {@code true} if set on this instance.
534 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600535 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800536 return isValidCapability(capability)
537 && ((mNetworkCapabilities & (1 << capability)) != 0);
538 }
539
540 /** @hide */
541 public boolean hasUnwantedCapability(@NetCapability int capability) {
542 return isValidCapability(capability)
543 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700544 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700545
lucasline252a742019-03-12 13:08:03 +0800546 /**
547 * Check if this NetworkCapabilities has system managed capabilities or not.
548 * @hide
549 */
550 public boolean hasConnectivityManagedCapability() {
551 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
552 }
553
Pavel Maltseve18ef262018-03-07 11:13:04 -0800554 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800555 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700556 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800557 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700558 }
559
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900560 /**
561 * Convenience function that returns a human-readable description of the first mutable
562 * capability we find. Used to present an error message to apps that request mutable
563 * capabilities.
564 *
565 * @hide
566 */
paulhud9736de2019-03-08 16:35:20 +0800567 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800568 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
569 & NON_REQUESTABLE_CAPABILITIES;
570
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900571 if (nonRequestable != 0) {
572 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900573 }
574 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900575 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800576 if (isPrivateDnsBroken()) {
577 return "privateDnsBroken";
578 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900579 return null;
580 }
581
paulhud9736de2019-03-08 16:35:20 +0800582 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
583 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800584 long requestedCapabilities = mNetworkCapabilities;
585 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
586 long providedCapabilities = nc.mNetworkCapabilities;
587
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900588 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800589 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
590 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900591 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800592 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
593 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700594 }
595
Robert Greenwalt06314e42014-10-29 14:04:06 -0700596 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800597 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800598 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
599 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700600 }
601
paulhud9736de2019-03-08 16:35:20 +0800602 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900603 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800604 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
605 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
606 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900607 }
608
Robert Greenwalt1448f052014-04-08 13:41:39 -0700609 /**
paulhu18354322020-01-09 17:08:11 +0800610 * Deduces that all the capabilities it provides are typically provided by restricted networks
611 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400612 *
paulhu18354322020-01-09 17:08:11 +0800613 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400614 * @hide
615 */
paulhu18354322020-01-09 17:08:11 +0800616 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800617 // Check if we have any capability that forces the network to be restricted.
618 final boolean forceRestrictedCapability =
619 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
620
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700621 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800622 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700623 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800624 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700625
626 // Must have at least some restricted capabilities.
627 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800628 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700629
paulhu18354322020-01-09 17:08:11 +0800630 return forceRestrictedCapability
631 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
632 }
633
634 /**
635 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
636 *
637 * @hide
638 */
639 public void maybeMarkCapabilitiesRestricted() {
640 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400641 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400642 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400643 }
644
645 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700646 * Representing the transport type. Apps should generally not care about transport. A
647 * request for a fast internet connection could be satisfied by a number of different
648 * transports. If any are specified here it will be satisfied a Network that matches
649 * any of them. If a caller doesn't care about the transport it should not specify any.
650 */
651 private long mTransportTypes;
652
Jeff Sharkeyde570312017-10-24 21:25:50 -0600653 /** @hide */
654 @Retention(RetentionPolicy.SOURCE)
655 @IntDef(prefix = { "TRANSPORT_" }, value = {
656 TRANSPORT_CELLULAR,
657 TRANSPORT_WIFI,
658 TRANSPORT_BLUETOOTH,
659 TRANSPORT_ETHERNET,
660 TRANSPORT_VPN,
661 TRANSPORT_WIFI_AWARE,
662 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800663 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600664 })
665 public @interface Transport { }
666
Robert Greenwalt1448f052014-04-08 13:41:39 -0700667 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700668 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700669 */
670 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700671
672 /**
673 * Indicates this network uses a Wi-Fi transport.
674 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700675 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700676
677 /**
678 * Indicates this network uses a Bluetooth transport.
679 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700680 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700681
682 /**
683 * Indicates this network uses an Ethernet transport.
684 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700685 public static final int TRANSPORT_ETHERNET = 3;
686
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400687 /**
688 * Indicates this network uses a VPN transport.
689 */
690 public static final int TRANSPORT_VPN = 4;
691
Etan Cohen305ea282016-06-20 09:27:12 -0700692 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700693 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700694 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700695 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700696
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700697 /**
698 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700699 */
700 public static final int TRANSPORT_LOWPAN = 6;
701
Benedict Wong89ce5e32018-11-14 17:40:55 -0800702 /**
703 * Indicates this network uses a Test-only virtual interface as a transport.
704 *
705 * @hide
706 */
707 @TestApi
708 public static final int TRANSPORT_TEST = 7;
709
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900710 /** @hide */
711 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
712 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800713 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700714
Hugo Benichi16f0a942017-06-20 14:07:59 +0900715 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600716 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900717 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
718 }
719
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900720 private static final String[] TRANSPORT_NAMES = {
721 "CELLULAR",
722 "WIFI",
723 "BLUETOOTH",
724 "ETHERNET",
725 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700726 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800727 "LOWPAN",
728 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900729 };
730
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700731 /**
732 * Adds the given transport type to this {@code NetworkCapability} instance.
733 * Multiple transports may be applied sequentially. Note that when searching
734 * for a network to satisfy a request, any listed in the request will satisfy the request.
735 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
736 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
737 * to be selected. This is logically different than
738 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
739 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600740 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900741 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700742 */
paulhud9736de2019-03-08 16:35:20 +0800743 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900744 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700745 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700746 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700747 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700748 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700749
750 /**
751 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
752 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600753 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900754 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700755 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700756 */
paulhud9736de2019-03-08 16:35:20 +0800757 public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900758 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700759 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700760 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700761 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700762 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700763
764 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600765 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
766 * instance.
767 *
768 * @hide
769 */
paulhud9736de2019-03-08 16:35:20 +0800770 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
771 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600772 if (value) {
773 addTransportType(transportType);
774 } else {
775 removeTransportType(transportType);
776 }
777 return this;
778 }
779
780 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700781 * Gets all the transports set on this {@code NetworkCapability} instance.
782 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600783 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700784 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700785 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600786 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900787 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800788 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900789 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700790 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700791
792 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600793 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700794 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600795 *
796 * @hide
797 */
798 public void setTransportTypes(@Transport int[] transportTypes) {
799 mTransportTypes = BitUtils.packBits(transportTypes);
800 }
801
802 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700803 * Tests for the presence of a transport on this instance.
804 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600805 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700806 * @return {@code true} if set on this instance.
807 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600808 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900809 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700810 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700811
812 private void combineTransportTypes(NetworkCapabilities nc) {
813 this.mTransportTypes |= nc.mTransportTypes;
814 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900815
Robert Greenwalt1448f052014-04-08 13:41:39 -0700816 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
817 return ((this.mTransportTypes == 0) ||
818 ((this.mTransportTypes & nc.mTransportTypes) != 0));
819 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900820
Robert Greenwalt06314e42014-10-29 14:04:06 -0700821 /** @hide */
822 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700823 return (nc.mTransportTypes == this.mTransportTypes);
824 }
825
826 /**
Roshan Piuse38acab2020-01-16 12:17:17 -0800827 * UID of the app that owns this network, or Process#INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900828 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800829 * <p>This field keeps track of the UID of the app that created this network and is in charge of
830 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
831 * VPN, or Carrier Service app managing a cellular data connection.
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800832 *
833 * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be
834 * reset to Process.INVALID_UID unless all the following conditions are met:
835 *
836 * <ol>
837 * <li>The destination app is the network owner
838 * <li>The destination app has the ACCESS_FINE_LOCATION permission granted
839 * <li>The user's location toggle is on
840 * </ol>
841 *
842 * This is because the owner UID is location-sensitive. The apps that request a network could
843 * know where the device is if they can tell for sure the system has connected to the network
844 * they requested.
845 *
846 * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by
847 * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system
848 * server.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900849 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800850 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900851
852 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800853 * Set the UID of the owner app.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900854 */
Roshan Piuse38acab2020-01-16 12:17:17 -0800855 public @NonNull NetworkCapabilities setOwnerUid(final int uid) {
Qingxi Li7cf06622020-01-17 17:54:27 -0800856 mOwnerUid = uid;
Roshan Piuse38acab2020-01-16 12:17:17 -0800857 return this;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900858 }
859
Qingxi Li7cf06622020-01-17 17:54:27 -0800860 /**
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800861 * Retrieves the UID of the app that owns this network.
862 *
863 * <p>For user privacy reasons, this field will only be populated if:
864 *
865 * <ol>
866 * <li>The calling app is the network owner
867 * <li>The calling app has the ACCESS_FINE_LOCATION permission granted
868 * <li>The user's location toggle is on
869 * </ol>
870 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800871 */
872 public int getOwnerUid() {
873 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000874 }
875
Chalard Jeanf474fc32018-01-17 15:10:05 +0900876 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800877 * UIDs of packages that are administrators of this network, or empty if none.
878 *
879 * <p>This field tracks the UIDs of packages that have permission to manage this network.
880 *
881 * <p>Network owners will also be listed as administrators.
882 *
883 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
884 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
885 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
886 */
887 private final List<Integer> mAdministratorUids = new ArrayList<>();
888
889 /**
890 * Sets the list of UIDs that are administrators of this network.
891 *
892 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
893 * Examples of UIDs that should be included in administratorUids are:
894 * <ul>
895 * <li>Carrier apps with privileges for the relevant subscription
896 * <li>Active VPN apps
897 * <li>Other application groups with a particular Network-related role
898 * </ul>
899 *
900 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
901 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800902 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
903 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800904 *
905 * @param administratorUids the UIDs to be set as administrators of this Network.
906 * @hide
907 */
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800908 @NonNull
Cody Kesting201fc132020-01-17 11:58:36 -0800909 @SystemApi
Qingxi Li9c5d8b92020-01-08 12:51:49 -0800910 public NetworkCapabilities setAdministratorUids(
Roshan Piuse38acab2020-01-16 12:17:17 -0800911 @NonNull final List<Integer> administratorUids) {
Cody Kesting201fc132020-01-17 11:58:36 -0800912 mAdministratorUids.clear();
913 mAdministratorUids.addAll(administratorUids);
Roshan Piuse38acab2020-01-16 12:17:17 -0800914 return this;
Cody Kesting201fc132020-01-17 11:58:36 -0800915 }
916
917 /**
918 * Retrieves the list of UIDs that are administrators of this Network.
919 *
920 * @return the List of UIDs that are administrators of this Network
921 * @hide
922 */
923 @NonNull
924 @SystemApi
925 public List<Integer> getAdministratorUids() {
926 return Collections.unmodifiableList(mAdministratorUids);
927 }
928
929 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600930 * Value indicating that link bandwidth is unspecified.
931 * @hide
932 */
933 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
934
935 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700936 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
937 * for the first hop on the given transport. It is not measured, but may take into account
938 * link parameters (Radio technology, allocated channels, etc).
939 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600940 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
941 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700942
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700943 /**
944 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
945 * the estimated first hop transport bandwidth.
946 * <p>
947 * Note that when used to request a network, this specifies the minimum acceptable.
948 * When received as the state of an existing network this specifies the typical
949 * first hop bandwidth expected. This is never measured, but rather is inferred
950 * from technology type and other link parameters. It could be used to differentiate
951 * between very slow 1xRTT cellular links and other faster networks or even between
952 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
953 * fast backhauls and slow backhauls.
954 *
955 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
956 */
paulhud9736de2019-03-08 16:35:20 +0800957 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700958 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600959 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700960 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700961
962 /**
963 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
964 * the estimated first hop transport bandwidth.
965 *
966 * @return The estimated first hop upstream (device to network) bandwidth.
967 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700968 public int getLinkUpstreamBandwidthKbps() {
969 return mLinkUpBandwidthKbps;
970 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700971
972 /**
973 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
974 * the estimated first hop transport bandwidth.
975 * <p>
976 * Note that when used to request a network, this specifies the minimum acceptable.
977 * When received as the state of an existing network this specifies the typical
978 * first hop bandwidth expected. This is never measured, but rather is inferred
979 * from technology type and other link parameters. It could be used to differentiate
980 * between very slow 1xRTT cellular links and other faster networks or even between
981 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
982 * fast backhauls and slow backhauls.
983 *
984 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
985 */
paulhud9736de2019-03-08 16:35:20 +0800986 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700987 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600988 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700989 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700990
991 /**
992 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
993 * the estimated first hop transport bandwidth.
994 *
995 * @return The estimated first hop downstream (network to device) bandwidth.
996 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700997 public int getLinkDownstreamBandwidthKbps() {
998 return mLinkDownBandwidthKbps;
999 }
1000
1001 private void combineLinkBandwidths(NetworkCapabilities nc) {
1002 this.mLinkUpBandwidthKbps =
1003 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
1004 this.mLinkDownBandwidthKbps =
1005 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
1006 }
1007 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
1008 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
1009 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
1010 }
1011 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
1012 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
1013 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
1014 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001015 /** @hide */
1016 public static int minBandwidth(int a, int b) {
1017 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
1018 return b;
1019 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
1020 return a;
1021 } else {
1022 return Math.min(a, b);
1023 }
1024 }
1025 /** @hide */
1026 public static int maxBandwidth(int a, int b) {
1027 return Math.max(a, b);
1028 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001029
Etan Cohena7434272017-04-03 12:17:51 -07001030 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -08001031 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -07001032
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001033 /**
1034 * Sets the optional bearer specific network specifier.
1035 * This has no meaning if a single transport is also not specified, so calling
1036 * this without a single transport set will generate an exception, as will
1037 * subsequently adding or removing transports after this is set.
1038 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001039 *
Etan Cohena7434272017-04-03 12:17:51 -07001040 * @param networkSpecifier A concrete, parcelable framework class that extends
1041 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +09001042 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001043 */
Aaron Huange6b62392019-09-20 22:52:54 +08001044 public @NonNull NetworkCapabilities setNetworkSpecifier(
1045 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001046 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001047 throw new IllegalStateException("Must have a single transport specified to use " +
1048 "setNetworkSpecifier");
1049 }
Etan Cohena7434272017-04-03 12:17:51 -07001050
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001051 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001052
Pierre Imaic8419a82016-03-22 17:54:54 +09001053 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001054 }
1055
1056 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001057 * Sets the optional transport specific information.
1058 *
1059 * @param transportInfo A concrete, parcelable framework class that extends
1060 * {@link TransportInfo}.
1061 * @return This NetworkCapabilities instance, to facilitate chaining.
1062 * @hide
1063 */
Aaron Huange6b62392019-09-20 22:52:54 +08001064 @SystemApi
1065 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001066 mTransportInfo = transportInfo;
1067 return this;
1068 }
1069
1070 /**
paulhud9736de2019-03-08 16:35:20 +08001071 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001072 *
Etan Cohena7434272017-04-03 12:17:51 -07001073 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
paulhud9736de2019-03-08 16:35:20 +08001074 * specifier or {@code null}. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001075 */
paulhud9736de2019-03-08 16:35:20 +08001076 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001077 return mNetworkSpecifier;
1078 }
1079
Etan Cohenca9fb562018-11-27 07:32:39 -08001080 /**
1081 * Returns a transport-specific information container. The application may cast this
1082 * container to a concrete sub-class based on its knowledge of the network request. The
1083 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001084 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001085 *
1086 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1087 * available for the network.
1088 */
1089 @Nullable public TransportInfo getTransportInfo() {
1090 return mTransportInfo;
1091 }
1092
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001093 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001094 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001095 throw new IllegalStateException("Can't combine two networkSpecifiers");
1096 }
Etan Cohena7434272017-04-03 12:17:51 -07001097 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001098 }
Etan Cohena7434272017-04-03 12:17:51 -07001099
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001100 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001101 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
1102 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001103 }
Etan Cohena7434272017-04-03 12:17:51 -07001104
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001105 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001106 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001107 }
1108
Etan Cohenca9fb562018-11-27 07:32:39 -08001109 private void combineTransportInfos(NetworkCapabilities nc) {
1110 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1111 throw new IllegalStateException("Can't combine two TransportInfos");
1112 }
1113 setTransportInfo(nc.mTransportInfo);
1114 }
1115
1116 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1117 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1118 }
1119
Robert Greenwalt1448f052014-04-08 13:41:39 -07001120 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001121 * Magic value that indicates no signal strength provided. A request specifying this value is
1122 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001123 */
1124 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1125
1126 /**
1127 * Signal strength. This is a signed integer, and higher values indicate better signal.
1128 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1129 */
paulhud9736de2019-03-08 16:35:20 +08001130 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001131 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001132
1133 /**
1134 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1135 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001136 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001137 * <p>
1138 * Note that when used to register a network callback, this specifies the minimum acceptable
1139 * signal strength. When received as the state of an existing network it specifies the current
1140 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1141 * effect when requesting a callback.
1142 *
1143 * @param signalStrength the bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001144 */
paulhud9736de2019-03-08 16:35:20 +08001145 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001146 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001147 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001148 }
1149
1150 /**
1151 * Returns {@code true} if this object specifies a signal strength.
1152 *
1153 * @hide
1154 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001155 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001156 public boolean hasSignalStrength() {
1157 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1158 }
1159
1160 /**
1161 * Retrieves the signal strength.
1162 *
1163 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001164 */
1165 public int getSignalStrength() {
1166 return mSignalStrength;
1167 }
1168
1169 private void combineSignalStrength(NetworkCapabilities nc) {
1170 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1171 }
1172
1173 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1174 return this.mSignalStrength <= nc.mSignalStrength;
1175 }
1176
1177 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1178 return this.mSignalStrength == nc.mSignalStrength;
1179 }
1180
1181 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001182 * List of UIDs this network applies to. No restriction if null.
1183 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001184 * For networks, mUids represent the list of network this applies to, and null means this
1185 * network applies to all UIDs.
1186 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1187 * must be included in a network so that they match. As an exception to the general rule,
1188 * a null mUids field for requests mean "no requirements" rather than what the general rule
1189 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1190 * of this API expect in practice. A network that must match all UIDs can still be
1191 * expressed with a set ranging the entire set of possible UIDs.
1192 * <p>
1193 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001194 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1195 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1196 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1197 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001198 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001199 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001200 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1201 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001202 * <p>
1203 * Please note that in principle a single app can be associated with multiple UIDs because
1204 * each app will have a different UID when it's run as a different (macro-)user. A single
1205 * macro user can only have a single active VPN app at any given time however.
1206 * <p>
1207 * Also please be aware this class does not try to enforce any normalization on this. Callers
1208 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1209 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1210 * their own (like requiring sortedness or no overlap) they need to enforce it
1211 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1212 * or overlapping ranges are present.
1213 *
1214 * @hide
1215 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001216 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001217
1218 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001219 * Convenience method to set the UIDs this network applies to to a single UID.
1220 * @hide
1221 */
paulhud9736de2019-03-08 16:35:20 +08001222 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001223 final ArraySet<UidRange> identity = new ArraySet<>(1);
1224 identity.add(new UidRange(uid, uid));
1225 setUids(identity);
1226 return this;
1227 }
1228
1229 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001230 * Set the list of UIDs this network applies to.
1231 * This makes a copy of the set so that callers can't modify it after the call.
1232 * @hide
1233 */
paulhud9736de2019-03-08 16:35:20 +08001234 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001235 if (null == uids) {
1236 mUids = null;
1237 } else {
1238 mUids = new ArraySet<>(uids);
1239 }
1240 return this;
1241 }
1242
1243 /**
1244 * Get the list of UIDs this network applies to.
1245 * This returns a copy of the set so that callers can't modify the original object.
1246 * @hide
1247 */
paulhud9736de2019-03-08 16:35:20 +08001248 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001249 return null == mUids ? null : new ArraySet<>(mUids);
1250 }
1251
1252 /**
1253 * Test whether this network applies to this UID.
1254 * @hide
1255 */
1256 public boolean appliesToUid(int uid) {
1257 if (null == mUids) return true;
1258 for (UidRange range : mUids) {
1259 if (range.contains(uid)) {
1260 return true;
1261 }
1262 }
1263 return false;
1264 }
1265
1266 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001267 * 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 +09001268 * <p>
1269 * This test only checks whether equal range objects are in both sets. It will
1270 * return false if the ranges are not exactly the same, even if the covered UIDs
1271 * are for an equivalent result.
1272 * <p>
1273 * Note that this method is not very optimized, which is fine as long as it's not used very
1274 * often.
1275 * <p>
1276 * nc is assumed nonnull.
1277 *
1278 * @hide
1279 */
1280 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001281 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001282 Set<UidRange> comparedUids = nc.mUids;
1283 if (null == comparedUids) return null == mUids;
1284 if (null == mUids) return false;
1285 // Make a copy so it can be mutated to check that all ranges in mUids
1286 // also are in uids.
1287 final Set<UidRange> uids = new ArraySet<>(mUids);
1288 for (UidRange range : comparedUids) {
1289 if (!uids.contains(range)) {
1290 return false;
1291 }
1292 uids.remove(range);
1293 }
1294 return uids.isEmpty();
1295 }
1296
1297 /**
1298 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1299 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001300 * This method is called on the NetworkCapabilities embedded in a request with the
1301 * capabilities of an available network. It checks whether all the UIDs from this listen
1302 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1303 * in the passed nc (representing the UIDs that this network is available to).
1304 * <p>
1305 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001306 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001307 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1308 * can see its own network when it listens for it.
1309 * <p>
1310 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001311 * @see #appliesToUid
1312 * @hide
1313 */
paulhud9736de2019-03-08 16:35:20 +08001314 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001315 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001316 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001317 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001318 if (!nc.appliesToUidRange(requiredRange)) {
1319 return false;
1320 }
1321 }
1322 return true;
1323 }
1324
1325 /**
1326 * Returns whether this network applies to the passed ranges.
1327 * This assumes that to apply, the passed range has to be entirely contained
1328 * within one of the ranges this network applies to. If the ranges are not normalized,
1329 * this method may return false even though all required UIDs are covered because no
1330 * single range contained them all.
1331 * @hide
1332 */
1333 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001334 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001335 if (null == mUids) return true;
1336 for (UidRange uidRange : mUids) {
1337 if (uidRange.containsRange(requiredRange)) {
1338 return true;
1339 }
1340 }
1341 return false;
1342 }
1343
1344 /**
1345 * Combine the UIDs this network currently applies to with the UIDs the passed
1346 * NetworkCapabilities apply to.
1347 * nc is assumed nonnull.
1348 */
paulhud9736de2019-03-08 16:35:20 +08001349 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001350 if (null == nc.mUids || null == mUids) {
1351 mUids = null;
1352 return;
1353 }
1354 mUids.addAll(nc.mUids);
1355 }
1356
Chalard Jeanb03a6222018-04-11 21:09:10 +09001357
1358 /**
1359 * The SSID of the network, or null if not applicable or unknown.
1360 * <p>
1361 * This is filled in by wifi code.
1362 * @hide
1363 */
1364 private String mSSID;
1365
1366 /**
1367 * Sets the SSID of this network.
1368 * @hide
1369 */
Aaron Huange6b62392019-09-20 22:52:54 +08001370 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001371 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001372 mSSID = ssid;
1373 return this;
1374 }
1375
1376 /**
1377 * Gets the SSID of this network, or null if none or unknown.
1378 * @hide
1379 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001380 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001381 public @Nullable String getSSID() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001382 return mSSID;
1383 }
1384
1385 /**
1386 * Tests if the SSID of this network is the same as the SSID of the passed network.
1387 * @hide
1388 */
paulhud9736de2019-03-08 16:35:20 +08001389 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001390 return Objects.equals(mSSID, nc.mSSID);
1391 }
1392
1393 /**
1394 * Check if the SSID requirements of this object are matched by the passed object.
1395 * @hide
1396 */
paulhud9736de2019-03-08 16:35:20 +08001397 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001398 return mSSID == null || mSSID.equals(nc.mSSID);
1399 }
1400
1401 /**
1402 * Combine SSIDs of the capabilities.
1403 * <p>
1404 * This is only legal if either the SSID of this object is null, or both SSIDs are
1405 * equal.
1406 * @hide
1407 */
paulhud9736de2019-03-08 16:35:20 +08001408 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001409 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1410 throw new IllegalStateException("Can't combine two SSIDs");
1411 }
1412 setSSID(nc.mSSID);
1413 }
1414
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001415 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001416 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1417 * <p>
1418 * Note that this method may break an invariant of having a particular capability in either
1419 * wanted or unwanted lists but never in both. Requests that have the same capability in
1420 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001421 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001422 */
paulhud9736de2019-03-08 16:35:20 +08001423 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001424 combineNetCapabilities(nc);
1425 combineTransportTypes(nc);
1426 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001427 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001428 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001429 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001430 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001431 combineSSIDs(nc);
Roshan Piuse38acab2020-01-16 12:17:17 -08001432 combineRequestor(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001433 }
1434
1435 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001436 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1437 *
1438 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1439 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1440 * bandwidth, signal strength, or validation / captive portal status.
1441 *
1442 * @hide
1443 */
1444 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001445 return (nc != null
1446 && satisfiedByNetCapabilities(nc, onlyImmutable)
1447 && satisfiedByTransportTypes(nc)
1448 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1449 && satisfiedBySpecifier(nc)
1450 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001451 && (onlyImmutable || satisfiedByUids(nc))
Roshan Piuse38acab2020-01-16 12:17:17 -08001452 && (onlyImmutable || satisfiedBySSID(nc)))
1453 && (onlyImmutable || satisfiedByRequestor(nc));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001454 }
1455
1456 /**
1457 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1458 *
1459 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1460 *
1461 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001462 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001463 @TestApi
1464 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001465 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001466 return satisfiedByNetworkCapabilities(nc, false);
1467 }
1468
1469 /**
1470 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1471 *
1472 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1473 *
1474 * @hide
1475 */
paulhud9736de2019-03-08 16:35:20 +08001476 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001477 return satisfiedByNetworkCapabilities(nc, true);
1478 }
1479
1480 /**
1481 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001482 * {@code NetworkCapabilities} and return a String describing any difference.
1483 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001484 *
1485 * @hide
1486 */
paulhud9736de2019-03-08 16:35:20 +08001487 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001488 if (that == null) {
1489 return "other NetworkCapabilities was null";
1490 }
1491
1492 StringJoiner joiner = new StringJoiner(", ");
1493
Hugo Benichieae7a222017-07-25 11:40:56 +09001494 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1495 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001496 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001497 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1498 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1499 if (oldImmutableCapabilities != newImmutableCapabilities) {
1500 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1501 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1502 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1503 }
1504
1505 if (!equalsSpecifier(that)) {
1506 NetworkSpecifier before = this.getNetworkSpecifier();
1507 NetworkSpecifier after = that.getNetworkSpecifier();
1508 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1509 }
1510
1511 if (!equalsTransportTypes(that)) {
1512 String before = transportNamesOf(this.getTransportTypes());
1513 String after = transportNamesOf(that.getTransportTypes());
1514 joiner.add(String.format("transports changed: %s -> %s", before, after));
1515 }
1516
1517 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001518 }
1519
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001520 /**
1521 * Checks that our requestable capabilities are the same as those of the given
1522 * {@code NetworkCapabilities}.
1523 *
1524 * @hide
1525 */
paulhud9736de2019-03-08 16:35:20 +08001526 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001527 if (nc == null) return false;
1528 return (equalsNetCapabilitiesRequestable(nc) &&
1529 equalsTransportTypes(nc) &&
1530 equalsSpecifier(nc));
1531 }
1532
Robert Greenwalt1448f052014-04-08 13:41:39 -07001533 @Override
paulhud9736de2019-03-08 16:35:20 +08001534 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001535 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001536 NetworkCapabilities that = (NetworkCapabilities) obj;
Roshan Piuse38acab2020-01-16 12:17:17 -08001537 return equalsNetCapabilities(that)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001538 && equalsTransportTypes(that)
1539 && equalsLinkBandwidths(that)
1540 && equalsSignalStrength(that)
1541 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001542 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001543 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001544 && equalsSSID(that)
Roshan Piuse38acab2020-01-16 12:17:17 -08001545 && equalsPrivateDnsBroken(that)
1546 && equalsRequestor(that);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001547 }
1548
1549 @Override
1550 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001551 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001552 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001553 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1554 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1555 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1556 + ((int) (mTransportTypes >> 32) * 13)
1557 + (mLinkUpBandwidthKbps * 17)
1558 + (mLinkDownBandwidthKbps * 19)
1559 + Objects.hashCode(mNetworkSpecifier) * 23
1560 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001561 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001562 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001563 + Objects.hashCode(mTransportInfo) * 41
Roshan Piuse38acab2020-01-16 12:17:17 -08001564 + Objects.hashCode(mPrivateDnsBroken) * 43
1565 + Objects.hashCode(mRequestorUid) * 47
1566 + Objects.hashCode(mRequestorPackageName) * 53;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001567 }
1568
Wink Saville4e2dea72014-09-20 11:04:03 -07001569 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001570 public int describeContents() {
1571 return 0;
1572 }
Cody Kesting201fc132020-01-17 11:58:36 -08001573
Wink Saville4e2dea72014-09-20 11:04:03 -07001574 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001575 public void writeToParcel(Parcel dest, int flags) {
1576 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001577 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001578 dest.writeLong(mTransportTypes);
1579 dest.writeInt(mLinkUpBandwidthKbps);
1580 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001581 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001582 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001583 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001584 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001585 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001586 dest.writeBoolean(mPrivateDnsBroken);
Cody Kesting201fc132020-01-17 11:58:36 -08001587 dest.writeList(mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -08001588 dest.writeInt(mOwnerUid);
Roshan Piuse38acab2020-01-16 12:17:17 -08001589 dest.writeInt(mRequestorUid);
1590 dest.writeString(mRequestorPackageName);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001591 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001592
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001593 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001594 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001595 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001596 public NetworkCapabilities createFromParcel(Parcel in) {
1597 NetworkCapabilities netCap = new NetworkCapabilities();
1598
1599 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001600 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001601 netCap.mTransportTypes = in.readLong();
1602 netCap.mLinkUpBandwidthKbps = in.readInt();
1603 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001604 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001605 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001606 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001607 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1608 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001609 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001610 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kesting201fc132020-01-17 11:58:36 -08001611 netCap.setAdministratorUids(in.readArrayList(null));
Qingxi Li7cf06622020-01-17 17:54:27 -08001612 netCap.mOwnerUid = in.readInt();
Roshan Piuse38acab2020-01-16 12:17:17 -08001613 netCap.mRequestorUid = in.readInt();
1614 netCap.mRequestorPackageName = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001615 return netCap;
1616 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001617 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001618 public NetworkCapabilities[] newArray(int size) {
1619 return new NetworkCapabilities[size];
1620 }
1621 };
1622
Wink Saville4e2dea72014-09-20 11:04:03 -07001623 @Override
paulhud9736de2019-03-08 16:35:20 +08001624 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001625 final StringBuilder sb = new StringBuilder("[");
1626 if (0 != mTransportTypes) {
1627 sb.append(" Transports: ");
1628 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1629 NetworkCapabilities::transportNameOf, "|");
1630 }
1631 if (0 != mNetworkCapabilities) {
1632 sb.append(" Capabilities: ");
1633 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1634 NetworkCapabilities::capabilityNameOf, "&");
1635 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001636 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001637 sb.append(" Unwanted: ");
1638 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1639 NetworkCapabilities::capabilityNameOf, "&");
1640 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001641 if (mLinkUpBandwidthKbps > 0) {
1642 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1643 }
1644 if (mLinkDownBandwidthKbps > 0) {
1645 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1646 }
1647 if (mNetworkSpecifier != null) {
1648 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1649 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001650 if (mTransportInfo != null) {
1651 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1652 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001653 if (hasSignalStrength()) {
1654 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001655 }
1656
Chalard Jean07ace0f2018-02-26 19:00:45 +09001657 if (null != mUids) {
1658 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1659 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1660 } else {
1661 sb.append(" Uids: <").append(mUids).append(">");
1662 }
1663 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001664 if (mOwnerUid != Process.INVALID_UID) {
1665 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001666 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001667
Cody Kesting201fc132020-01-17 11:58:36 -08001668 if (!mAdministratorUids.isEmpty()) {
1669 sb.append(" AdministratorUids: ").append(mAdministratorUids);
1670 }
1671
Chalard Jeanb03a6222018-04-11 21:09:10 +09001672 if (null != mSSID) {
1673 sb.append(" SSID: ").append(mSSID);
1674 }
1675
lucaslin783f2212019-10-22 18:27:33 +08001676 if (mPrivateDnsBroken) {
1677 sb.append(" Private DNS is broken");
1678 }
1679
Roshan Piuse38acab2020-01-16 12:17:17 -08001680 sb.append(" RequestorUid: ").append(mRequestorUid);
1681 sb.append(" RequestorPackageName: ").append(mRequestorPackageName);
1682
Chalard Jean07ace0f2018-02-26 19:00:45 +09001683 sb.append("]");
1684 return sb.toString();
1685 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001686
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001687
Chalard Jean07ace0f2018-02-26 19:00:45 +09001688 private interface NameOf {
1689 String nameOf(int value);
1690 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001691
Chalard Jean07ace0f2018-02-26 19:00:45 +09001692 /**
1693 * @hide
1694 */
paulhud9736de2019-03-08 16:35:20 +08001695 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1696 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001697 int bitPos = 0;
1698 boolean firstElementAdded = false;
1699 while (bitMask != 0) {
1700 if ((bitMask & 1) != 0) {
1701 if (firstElementAdded) {
1702 sb.append(separator);
1703 } else {
1704 firstElementAdded = true;
1705 }
1706 sb.append(nameFetcher.nameOf(bitPos));
1707 }
1708 bitMask >>= 1;
1709 ++bitPos;
1710 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001711 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001712
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001713 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001714 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001715 final long token = proto.start(fieldId);
1716
1717 for (int transport : getTransportTypes()) {
1718 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1719 }
1720
1721 for (int capability : getCapabilities()) {
1722 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1723 }
1724
1725 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1726 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1727
1728 if (mNetworkSpecifier != null) {
1729 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1730 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001731 if (mTransportInfo != null) {
1732 // TODO b/120653863: write transport-specific info to proto?
1733 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001734
1735 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1736 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1737
1738 proto.end(token);
1739 }
1740
Hugo Benichi5df9d722016-04-25 17:16:35 +09001741 /**
1742 * @hide
1743 */
paulhud9736de2019-03-08 16:35:20 +08001744 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001745 StringJoiner joiner = new StringJoiner("|");
1746 if (capabilities != null) {
1747 for (int c : capabilities) {
1748 joiner.add(capabilityNameOf(c));
1749 }
1750 }
1751 return joiner.toString();
1752 }
1753
1754 /**
1755 * @hide
1756 */
paulhud9736de2019-03-08 16:35:20 +08001757 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001758 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001759 case NET_CAPABILITY_MMS: return "MMS";
1760 case NET_CAPABILITY_SUPL: return "SUPL";
1761 case NET_CAPABILITY_DUN: return "DUN";
1762 case NET_CAPABILITY_FOTA: return "FOTA";
1763 case NET_CAPABILITY_IMS: return "IMS";
1764 case NET_CAPABILITY_CBS: return "CBS";
1765 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1766 case NET_CAPABILITY_IA: return "IA";
1767 case NET_CAPABILITY_RCS: return "RCS";
1768 case NET_CAPABILITY_XCAP: return "XCAP";
1769 case NET_CAPABILITY_EIMS: return "EIMS";
1770 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1771 case NET_CAPABILITY_INTERNET: return "INTERNET";
1772 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1773 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1774 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1775 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1776 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1777 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1778 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1779 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1780 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1781 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1782 case NET_CAPABILITY_MCX: return "MCX";
1783 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1784 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001785 }
1786 }
1787
1788 /**
1789 * @hide
1790 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001791 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001792 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001793 StringJoiner joiner = new StringJoiner("|");
1794 if (types != null) {
1795 for (int t : types) {
1796 joiner.add(transportNameOf(t));
1797 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001798 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001799 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001800 }
1801
1802 /**
1803 * @hide
1804 */
paulhud9736de2019-03-08 16:35:20 +08001805 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001806 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001807 return "UNKNOWN";
1808 }
1809 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001810 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001811
Jeff Sharkeyde570312017-10-24 21:25:50 -06001812 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001813 Preconditions.checkArgument(
1814 isValidTransport(transport), "Invalid TransportType " + transport);
1815 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001816
1817 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1818 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1819 }
1820
1821 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1822 Preconditions.checkArgument(isValidCapability(capability),
1823 "NetworkCapability " + capability + "out of range");
1824 }
junyulai05986c62018-08-07 19:50:45 +08001825
1826 /**
1827 * Check if this {@code NetworkCapability} instance is metered.
1828 *
1829 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1830 * @hide
1831 */
1832 public boolean isMetered() {
1833 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1834 }
lucaslin783f2212019-10-22 18:27:33 +08001835
1836 /**
1837 * Check if private dns is broken.
1838 *
1839 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1840 * @hide
1841 */
1842 public boolean isPrivateDnsBroken() {
1843 return mPrivateDnsBroken;
1844 }
1845
1846 /**
1847 * Set mPrivateDnsBroken to true when private dns is broken.
1848 *
1849 * @param broken the status of private DNS to be set.
1850 * @hide
1851 */
1852 public void setPrivateDnsBroken(boolean broken) {
1853 mPrivateDnsBroken = broken;
1854 }
1855
1856 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1857 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1858 }
Roshan Piuse38acab2020-01-16 12:17:17 -08001859
1860 /**
1861 * Set the uid of the app making the request.
1862 *
1863 * Note: This works only for {@link NetworkAgent} instances. Any capabilities passed in
1864 * via the public {@link ConnectivityManager} API's will have this field overwritten.
1865 *
1866 * @param uid UID of the app.
1867 * @hide
1868 */
1869 @SystemApi
1870 public @NonNull NetworkCapabilities setRequestorUid(int uid) {
1871 mRequestorUid = uid;
1872 return this;
1873 }
1874
1875 /**
1876 * @return the uid of the app making the request.
1877 *
1878 * Note: This could return {@link Process#INVALID_UID} if the {@link NetworkRequest}
1879 * object was not obtained from {@link ConnectivityManager}.
1880 * @hide
1881 */
1882 public int getRequestorUid() {
1883 return mRequestorUid;
1884 }
1885
1886 /**
1887 * Set the package name of the app making the request.
1888 *
1889 * Note: This works only for {@link NetworkAgent} instances. Any capabilities passed in
1890 * via the public {@link ConnectivityManager} API's will have this field overwritten.
1891 *
1892 * @param packageName package name of the app.
1893 * @hide
1894 */
1895 @SystemApi
1896 public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) {
1897 mRequestorPackageName = packageName;
1898 return this;
1899 }
1900
1901 /**
1902 * @return the package name of the app making the request.
1903 *
1904 * Note: This could return {@code null} if the {@link NetworkRequest} object was not obtained
1905 * from {@link ConnectivityManager}.
1906 * @hide
1907 */
1908 @Nullable
1909 public String getRequestorPackageName() {
1910 return mRequestorPackageName;
1911 }
1912
1913 /**
1914 * Set the uid and package name of the app making the request.
1915 *
1916 * Note: This is intended to be only invoked from within connectivitiy service.
1917 *
1918 * @param uid UID of the app.
1919 * @param packageName package name of the app.
1920 * @hide
1921 */
1922 public @NonNull NetworkCapabilities setRequestorUidAndPackageName(
1923 int uid, @NonNull String packageName) {
1924 return setRequestorUid(uid).setRequestorPackageName(packageName);
1925 }
1926
1927 /**
1928 * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this
1929 * capabilities.
1930 *
1931 * This method is called on the NetworkCapabilities embedded in a request with the
1932 * capabilities of an available network. If the available network, sets a specific
1933 * requestor (by uid and optionally package name), then this will only match a request from the
1934 * same app. If either of the capabilities have an unset uid or package name, then it matches
1935 * everything.
1936 * <p>
1937 * nc is assumed nonnull. Else, NPE.
1938 */
1939 private boolean satisfiedByRequestor(NetworkCapabilities nc) {
1940 // No uid set, matches everything.
1941 if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) {
1942 return true;
1943 }
1944 // uids don't match.
1945 if (mRequestorUid != nc.mRequestorUid) return false;
1946 // No package names set, matches everything
1947 if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true;
1948 // check for package name match.
1949 return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
1950 }
1951
1952 /**
1953 * Combine requestor info of the capabilities.
1954 * <p>
1955 * This is only legal if either the requestor info of this object is reset, or both info are
1956 * equal.
1957 * nc is assumed nonnull.
1958 */
1959 private void combineRequestor(@NonNull NetworkCapabilities nc) {
1960 if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) {
1961 throw new IllegalStateException("Can't combine two uids");
1962 }
1963 if (mRequestorPackageName != null
1964 && !mRequestorPackageName.equals(nc.mRequestorPackageName)) {
1965 throw new IllegalStateException("Can't combine two package names");
1966 }
1967 setRequestorUid(nc.mRequestorUid);
1968 setRequestorPackageName(nc.mRequestorPackageName);
1969 }
1970
1971 private boolean equalsRequestor(NetworkCapabilities nc) {
1972 return mRequestorUid == nc.mRequestorUid
1973 && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName);
1974 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001975}