blob: cf5f2259af44627c3ff18ac47e88459c890f2795 [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;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090030import android.util.ArraySet;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080031import android.util.proto.ProtoOutputStream;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070032
33import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090034import com.android.internal.util.BitUtils;
Hugo Benichi16f0a942017-06-20 14:07:59 +090035import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070036
Jeff Sharkeyde570312017-10-24 21:25:50 -060037import java.lang.annotation.Retention;
38import java.lang.annotation.RetentionPolicy;
Cody Kesting201fc132020-01-17 11:58:36 -080039import java.util.ArrayList;
40import java.util.Collections;
41import java.util.List;
Etan Cohena7434272017-04-03 12:17:51 -070042import java.util.Objects;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090043import java.util.Set;
Hugo Benichieae7a222017-07-25 11:40:56 +090044import java.util.StringJoiner;
Robert Greenwalt1448f052014-04-08 13:41:39 -070045
46/**
Jeff Sharkey49bcd602017-11-09 13:11:50 -070047 * Representation of the capabilities of an active network. Instances are
48 * typically obtained through
Jeff Sharkey72f9c422017-10-27 17:22:59 -060049 * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)}
50 * or {@link ConnectivityManager#getNetworkCapabilities(Network)}.
Jeff Sharkey72f9c422017-10-27 17:22:59 -060051 * <p>
52 * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of
53 * network selection. Rather than indicate a need for Wi-Fi because an
54 * application needs high bandwidth and risk obsolescence when a new, fast
55 * network appears (like LTE), the application should specify it needs high
56 * bandwidth. Similarly if an application needs an unmetered network for a bulk
57 * transfer it can specify that rather than assuming all cellular based
58 * connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070059 */
60public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070061 private static final String TAG = "NetworkCapabilities";
62
lucaslin783f2212019-10-22 18:27:33 +080063 // Set to true when private DNS is broken.
64 private boolean mPrivateDnsBroken;
65
Robert Greenwalt01d004e2014-05-18 15:24:21 -070066 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090067 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090068 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070069 }
70
71 public NetworkCapabilities(NetworkCapabilities nc) {
72 if (nc != null) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090073 set(nc);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070074 }
75 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070076
77 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090078 * Completely clears the contents of this object, removing even the capabilities that are set
79 * by default when the object is constructed.
Lorenzo Colittif7058f52015-04-27 11:31:55 +090080 */
81 public void clearAll() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -080082 mNetworkCapabilities = mTransportTypes = mUnwantedNetworkCapabilities = 0;
Jeff Sharkey49bcd602017-11-09 13:11:50 -070083 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090084 mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -080085 mTransportInfo = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090086 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Chalard Jeanecacd5e2017-12-27 14:23:31 +090087 mUids = null;
Cody Kesting201fc132020-01-17 11:58:36 -080088 mAdministratorUids.clear();
Qingxi Li7cf06622020-01-17 17:54:27 -080089 mOwnerUid = Process.INVALID_UID;
Chalard Jeanb03a6222018-04-11 21:09:10 +090090 mSSID = null;
lucaslin783f2212019-10-22 18:27:33 +080091 mPrivateDnsBroken = false;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090092 }
93
94 /**
Chalard Jean4c4bc932018-05-18 23:48:49 +090095 * Set all contents of this object to the contents of a NetworkCapabilities.
96 * @hide
97 */
paulhud9736de2019-03-08 16:35:20 +080098 public void set(@NonNull NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +090099 mNetworkCapabilities = nc.mNetworkCapabilities;
100 mTransportTypes = nc.mTransportTypes;
101 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
102 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
103 mNetworkSpecifier = nc.mNetworkSpecifier;
Etan Cohenca9fb562018-11-27 07:32:39 -0800104 mTransportInfo = nc.mTransportInfo;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900105 mSignalStrength = nc.mSignalStrength;
106 setUids(nc.mUids); // Will make the defensive copy
Cody Kesting201fc132020-01-17 11:58:36 -0800107 setAdministratorUids(nc.mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -0800108 mOwnerUid = nc.mOwnerUid;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900109 mUnwantedNetworkCapabilities = nc.mUnwantedNetworkCapabilities;
110 mSSID = nc.mSSID;
lucaslin783f2212019-10-22 18:27:33 +0800111 mPrivateDnsBroken = nc.mPrivateDnsBroken;
Chalard Jean4c4bc932018-05-18 23:48:49 +0900112 }
113
114 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700115 * Represents the network's capabilities. If any are specified they will be satisfied
116 * by any Network that matches all of them.
117 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100118 @UnsupportedAppUsage
Lorenzo Colittif7058f52015-04-27 11:31:55 +0900119 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700120
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800121 /**
122 * If any capabilities specified here they must not exist in the matching Network.
123 */
124 private long mUnwantedNetworkCapabilities;
125
Jeff Sharkeyde570312017-10-24 21:25:50 -0600126 /** @hide */
127 @Retention(RetentionPolicy.SOURCE)
128 @IntDef(prefix = { "NET_CAPABILITY_" }, value = {
129 NET_CAPABILITY_MMS,
130 NET_CAPABILITY_SUPL,
131 NET_CAPABILITY_DUN,
132 NET_CAPABILITY_FOTA,
133 NET_CAPABILITY_IMS,
134 NET_CAPABILITY_CBS,
135 NET_CAPABILITY_WIFI_P2P,
136 NET_CAPABILITY_IA,
137 NET_CAPABILITY_RCS,
138 NET_CAPABILITY_XCAP,
139 NET_CAPABILITY_EIMS,
140 NET_CAPABILITY_NOT_METERED,
141 NET_CAPABILITY_INTERNET,
142 NET_CAPABILITY_NOT_RESTRICTED,
143 NET_CAPABILITY_TRUSTED,
144 NET_CAPABILITY_NOT_VPN,
145 NET_CAPABILITY_VALIDATED,
146 NET_CAPABILITY_CAPTIVE_PORTAL,
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600147 NET_CAPABILITY_NOT_ROAMING,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600148 NET_CAPABILITY_FOREGROUND,
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900149 NET_CAPABILITY_NOT_CONGESTED,
Chalard Jean804b8fb2018-01-30 22:41:41 +0900150 NET_CAPABILITY_NOT_SUSPENDED,
Pavel Maltsev43403202018-01-30 17:19:44 -0800151 NET_CAPABILITY_OEM_PAID,
lucasline252a742019-03-12 13:08:03 +0800152 NET_CAPABILITY_MCX,
153 NET_CAPABILITY_PARTIAL_CONNECTIVITY,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600154 })
155 public @interface NetCapability { }
156
Robert Greenwalt1448f052014-04-08 13:41:39 -0700157 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700158 * Indicates this is a network that has the ability to reach the
159 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700160 */
161 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700162
163 /**
164 * Indicates this is a network that has the ability to reach the carrier's
165 * SUPL server, used to retrieve GPS information.
166 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700167 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700168
169 /**
170 * Indicates this is a network that has the ability to reach the carrier's
171 * DUN or tethering gateway.
172 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700173 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700174
175 /**
176 * Indicates this is a network that has the ability to reach the carrier's
177 * FOTA portal, used for over the air updates.
178 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700180
181 /**
182 * Indicates this is a network that has the ability to reach the carrier's
183 * IMS servers, used for network registration and signaling.
184 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Indicates this is a network that has the ability to reach the carrier's
189 * CBS servers, used for carrier specific services.
190 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Indicates this is a network that has the ability to reach a Wi-Fi direct
195 * peer.
196 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Indicates this is a network that has the ability to reach a carrier's
201 * Initial Attach servers.
202 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Indicates this is a network that has the ability to reach a carrier's
207 * RCS servers, used for Rich Communication Services.
208 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700209 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700210
211 /**
212 * Indicates this is a network that has the ability to reach a carrier's
213 * XCAP servers, used for configuration and control.
214 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216
217 /**
218 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700219 * Emergency IMS servers or other services, used for network signaling
220 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700221 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700222 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700223
224 /**
225 * Indicates that this network is unmetered.
226 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700227 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700228
229 /**
230 * Indicates that this network should be able to reach the internet.
231 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700232 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700233
234 /**
235 * Indicates that this network is available for general use. If this is not set
236 * applications should not attempt to communicate on this network. Note that this
237 * is simply informative and not enforcement - enforcement is handled via other means.
238 * Set by default.
239 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700240 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
241
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700242 /**
243 * Indicates that the user has indicated implicit trust of this network. This
244 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
245 * BT device or a wifi the user asked to connect to. Untrusted networks
246 * are probably limited to unknown wifi AP. Set by default.
247 */
248 public static final int NET_CAPABILITY_TRUSTED = 14;
249
Paul Jensen76b610a2015-03-18 09:33:07 -0400250 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400251 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400252 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400253 */
254 public static final int NET_CAPABILITY_NOT_VPN = 15;
255
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900256 /**
257 * Indicates that connectivity on this network was successfully validated. For example, for a
258 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
259 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900260 */
261 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700262
Paul Jensen3d194ea2015-06-16 14:27:36 -0400263 /**
264 * Indicates that this network was found to have a captive portal in place last time it was
265 * probed.
266 */
267 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
268
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900269 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600270 * Indicates that this network is not roaming.
271 */
272 public static final int NET_CAPABILITY_NOT_ROAMING = 18;
273
274 /**
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900275 * Indicates that this network is available for use by apps, and not a network that is being
276 * kept up in the background to facilitate fast network switching.
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900277 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600278 public static final int NET_CAPABILITY_FOREGROUND = 19;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900279
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900280 /**
281 * Indicates that this network is not congested.
282 * <p>
Jeff Sharkey0a5570d2018-04-10 12:38:29 -0600283 * When a network is congested, applications should defer network traffic
284 * that can be done at a later time, such as uploading analytics.
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900285 */
286 public static final int NET_CAPABILITY_NOT_CONGESTED = 20;
287
Chalard Jean804b8fb2018-01-30 22:41:41 +0900288 /**
289 * Indicates that this network is not currently suspended.
290 * <p>
291 * When a network is suspended, the network's IP addresses and any connections
292 * established on the network remain valid, but the network is temporarily unable
293 * to transfer data. This can happen, for example, if a cellular network experiences
294 * a temporary loss of signal, such as when driving through a tunnel, etc.
295 * A network with this capability is not suspended, so is expected to be able to
296 * transfer data.
297 */
298 public static final int NET_CAPABILITY_NOT_SUSPENDED = 21;
299
Pavel Maltsev43403202018-01-30 17:19:44 -0800300 /**
301 * Indicates that traffic that goes through this network is paid by oem. For example,
302 * this network can be used by system apps to upload telemetry data.
303 * @hide
304 */
Pavel Maltsevd9c9fff2018-03-22 11:41:32 -0700305 @SystemApi
Pavel Maltsev43403202018-01-30 17:19:44 -0800306 public static final int NET_CAPABILITY_OEM_PAID = 22;
307
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800308 /**
309 * Indicates this is a network that has the ability to reach a carrier's Mission Critical
310 * servers.
311 */
312 public static final int NET_CAPABILITY_MCX = 23;
313
lucasline252a742019-03-12 13:08:03 +0800314 /**
315 * Indicates that this network was tested to only provide partial connectivity.
316 * @hide
317 */
318 @SystemApi
319 public static final int NET_CAPABILITY_PARTIAL_CONNECTIVITY = 24;
320
Robert Greenwalt1448f052014-04-08 13:41:39 -0700321 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
lucasline252a742019-03-12 13:08:03 +0800322 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_PARTIAL_CONNECTIVITY;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700323
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700324 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900325 * Network capabilities that are expected to be mutable, i.e., can change while a particular
326 * network is connected.
327 */
328 private static final long MUTABLE_CAPABILITIES =
329 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
330 // http://b/18206275
Chalard Jean804b8fb2018-01-30 22:41:41 +0900331 (1 << NET_CAPABILITY_TRUSTED)
332 | (1 << NET_CAPABILITY_VALIDATED)
333 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
334 | (1 << NET_CAPABILITY_NOT_ROAMING)
335 | (1 << NET_CAPABILITY_FOREGROUND)
336 | (1 << NET_CAPABILITY_NOT_CONGESTED)
lucasline252a742019-03-12 13:08:03 +0800337 | (1 << NET_CAPABILITY_NOT_SUSPENDED)
338 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900339
340 /**
341 * Network capabilities that are not allowed in NetworkRequests. This exists because the
342 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
343 * capability's presence cannot be known in advance. If such a capability is requested, then we
344 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
345 * get immediately torn down because they do not have the requested capability.
346 */
347 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900348 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900349
350 /**
351 * Capabilities that are set by default when the object is constructed.
352 */
353 private static final long DEFAULT_CAPABILITIES =
354 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
355 (1 << NET_CAPABILITY_TRUSTED) |
356 (1 << NET_CAPABILITY_NOT_VPN);
357
358 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400359 * Capabilities that suggest that a network is restricted.
Pavel Maltsev4af91072018-03-07 14:33:22 -0800360 * {@see #maybeMarkCapabilitiesRestricted}, {@see #FORCE_RESTRICTED_CAPABILITIES}
Paul Jensen487ffe72015-07-24 15:57:11 -0400361 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700362 @VisibleForTesting
363 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400364 (1 << NET_CAPABILITY_CBS) |
365 (1 << NET_CAPABILITY_DUN) |
366 (1 << NET_CAPABILITY_EIMS) |
367 (1 << NET_CAPABILITY_FOTA) |
368 (1 << NET_CAPABILITY_IA) |
369 (1 << NET_CAPABILITY_IMS) |
370 (1 << NET_CAPABILITY_RCS) |
Amit Mahajanfd3ee572019-02-20 15:04:30 -0800371 (1 << NET_CAPABILITY_XCAP) |
372 (1 << NET_CAPABILITY_MCX);
Pavel Maltsev4af91072018-03-07 14:33:22 -0800373
374 /**
375 * Capabilities that force network to be restricted.
376 * {@see #maybeMarkCapabilitiesRestricted}.
377 */
378 private static final long FORCE_RESTRICTED_CAPABILITIES =
Pavel Maltsev43403202018-01-30 17:19:44 -0800379 (1 << NET_CAPABILITY_OEM_PAID);
Paul Jensen487ffe72015-07-24 15:57:11 -0400380
381 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700382 * Capabilities that suggest that a network is unrestricted.
383 * {@see #maybeMarkCapabilitiesRestricted}.
384 */
385 @VisibleForTesting
386 /* package */ static final long UNRESTRICTED_CAPABILITIES =
387 (1 << NET_CAPABILITY_INTERNET) |
388 (1 << NET_CAPABILITY_MMS) |
389 (1 << NET_CAPABILITY_SUPL) |
390 (1 << NET_CAPABILITY_WIFI_P2P);
391
392 /**
lucasline252a742019-03-12 13:08:03 +0800393 * Capabilities that are managed by ConnectivityService.
394 */
395 private static final long CONNECTIVITY_MANAGED_CAPABILITIES =
396 (1 << NET_CAPABILITY_VALIDATED)
397 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL)
398 | (1 << NET_CAPABILITY_FOREGROUND)
399 | (1 << NET_CAPABILITY_PARTIAL_CONNECTIVITY);
400
401 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700402 * Adds the given capability to this {@code NetworkCapability} instance.
403 * Multiple capabilities may be applied sequentially. Note that when searching
404 * for a network to satisfy a request, all capabilities requested must be satisfied.
405 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600406 * @param capability the capability to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900407 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700408 */
paulhud9736de2019-03-08 16:35:20 +0800409 public @NonNull NetworkCapabilities addCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800410 // If the given capability was previously added to the list of unwanted capabilities
411 // then the capability will also be removed from the list of unwanted capabilities.
412 // TODO: Consider adding unwanted capabilities to the public API and mention this
413 // in the documentation.
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800414 checkValidCapability(capability);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700415 mNetworkCapabilities |= 1 << capability;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800416 mUnwantedNetworkCapabilities &= ~(1 << capability); // remove from unwanted capability list
Robert Greenwalt7569f182014-06-08 16:42:59 -0700417 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700418 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700419
420 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800421 * Adds the given capability to the list of unwanted capabilities of this
422 * {@code NetworkCapability} instance. Multiple unwanted capabilities may be applied
423 * sequentially. Note that when searching for a network to satisfy a request, the network
424 * must not contain any capability from unwanted capability list.
425 * <p>
426 * If the capability was previously added to the list of required capabilities (for
427 * example, it was there by default or added using {@link #addCapability(int)} method), then
428 * it will be removed from the list of required capabilities as well.
429 *
430 * @see #addCapability(int)
431 * @hide
432 */
433 public void addUnwantedCapability(@NetCapability int capability) {
434 checkValidCapability(capability);
435 mUnwantedNetworkCapabilities |= 1 << capability;
436 mNetworkCapabilities &= ~(1 << capability); // remove from requested capabilities
437 }
438
439 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700440 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
441 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600442 * @param capability the capability to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900443 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700444 */
paulhud9736de2019-03-08 16:35:20 +0800445 public @NonNull NetworkCapabilities removeCapability(@NetCapability int capability) {
Aaron Huange6b62392019-09-20 22:52:54 +0800446 // Note that this method removes capabilities that were added via addCapability(int),
447 // addUnwantedCapability(int) or setCapabilities(int[], int[]).
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800448 checkValidCapability(capability);
449 final long mask = ~(1 << capability);
450 mNetworkCapabilities &= mask;
451 mUnwantedNetworkCapabilities &= mask;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700452 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700453 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700454
455 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600456 * Sets (or clears) the given capability on this {@link NetworkCapabilities}
457 * instance.
458 *
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600459 */
paulhud9736de2019-03-08 16:35:20 +0800460 public @NonNull NetworkCapabilities setCapability(@NetCapability int capability,
461 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600462 if (value) {
463 addCapability(capability);
464 } else {
465 removeCapability(capability);
466 }
467 return this;
468 }
469
470 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700471 * Gets all the capabilities set on this {@code NetworkCapability} instance.
472 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600473 * @return an array of capability values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700474 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700475 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000476 @UnsupportedAppUsage
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600477 @TestApi
Jeff Sharkeyde570312017-10-24 21:25:50 -0600478 public @NetCapability int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900479 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700481
482 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800483 * Gets all the unwanted capabilities set on this {@code NetworkCapability} instance.
484 *
485 * @return an array of unwanted capability values for this instance.
486 * @hide
487 */
488 public @NetCapability int[] getUnwantedCapabilities() {
489 return BitUtils.unpackBits(mUnwantedNetworkCapabilities);
490 }
491
492
493 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600494 * Sets all the capabilities set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700495 * This overwrites any existing capabilities.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600496 *
497 * @hide
498 */
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800499 public void setCapabilities(@NetCapability int[] capabilities,
500 @NetCapability int[] unwantedCapabilities) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600501 mNetworkCapabilities = BitUtils.packBits(capabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800502 mUnwantedNetworkCapabilities = BitUtils.packBits(unwantedCapabilities);
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600503 }
504
505 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800506 * @deprecated use {@link #setCapabilities(int[], int[])}
507 * @hide
508 */
509 @Deprecated
510 public void setCapabilities(@NetCapability int[] capabilities) {
511 setCapabilities(capabilities, new int[] {});
512 }
513
514 /**
515 * Tests for the presence of a capability on this instance.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700516 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600517 * @param capability the capabilities to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700518 * @return {@code true} if set on this instance.
519 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600520 public boolean hasCapability(@NetCapability int capability) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800521 return isValidCapability(capability)
522 && ((mNetworkCapabilities & (1 << capability)) != 0);
523 }
524
525 /** @hide */
526 public boolean hasUnwantedCapability(@NetCapability int capability) {
527 return isValidCapability(capability)
528 && ((mUnwantedNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700529 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700530
lucasline252a742019-03-12 13:08:03 +0800531 /**
532 * Check if this NetworkCapabilities has system managed capabilities or not.
533 * @hide
534 */
535 public boolean hasConnectivityManagedCapability() {
536 return ((mNetworkCapabilities & CONNECTIVITY_MANAGED_CAPABILITIES) != 0);
537 }
538
Pavel Maltseve18ef262018-03-07 11:13:04 -0800539 /** Note this method may result in having the same capability in wanted and unwanted lists. */
paulhud9736de2019-03-08 16:35:20 +0800540 private void combineNetCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700541 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800542 this.mUnwantedNetworkCapabilities |= nc.mUnwantedNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700543 }
544
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900545 /**
546 * Convenience function that returns a human-readable description of the first mutable
547 * capability we find. Used to present an error message to apps that request mutable
548 * capabilities.
549 *
550 * @hide
551 */
paulhud9736de2019-03-08 16:35:20 +0800552 public @Nullable String describeFirstNonRequestableCapability() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800553 final long nonRequestable = (mNetworkCapabilities | mUnwantedNetworkCapabilities)
554 & NON_REQUESTABLE_CAPABILITIES;
555
Jeff Sharkey9b2a10f2018-01-17 13:27:03 +0900556 if (nonRequestable != 0) {
557 return capabilityNameOf(BitUtils.unpackBits(nonRequestable)[0]);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900558 }
559 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900560 if (hasSignalStrength()) return "signalStrength";
lucaslin783f2212019-10-22 18:27:33 +0800561 if (isPrivateDnsBroken()) {
562 return "privateDnsBroken";
563 }
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900564 return null;
565 }
566
paulhud9736de2019-03-08 16:35:20 +0800567 private boolean satisfiedByNetCapabilities(@NonNull NetworkCapabilities nc,
568 boolean onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800569 long requestedCapabilities = mNetworkCapabilities;
570 long requestedUnwantedCapabilities = mUnwantedNetworkCapabilities;
571 long providedCapabilities = nc.mNetworkCapabilities;
572
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900573 if (onlyImmutable) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800574 requestedCapabilities &= ~MUTABLE_CAPABILITIES;
575 requestedUnwantedCapabilities &= ~MUTABLE_CAPABILITIES;
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900576 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800577 return ((providedCapabilities & requestedCapabilities) == requestedCapabilities)
578 && ((requestedUnwantedCapabilities & providedCapabilities) == 0);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700579 }
580
Robert Greenwalt06314e42014-10-29 14:04:06 -0700581 /** @hide */
paulhud9736de2019-03-08 16:35:20 +0800582 public boolean equalsNetCapabilities(@NonNull NetworkCapabilities nc) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800583 return (nc.mNetworkCapabilities == this.mNetworkCapabilities)
584 && (nc.mUnwantedNetworkCapabilities == this.mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700585 }
586
paulhud9736de2019-03-08 16:35:20 +0800587 private boolean equalsNetCapabilitiesRequestable(@NonNull NetworkCapabilities that) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900588 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800589 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES))
590 && ((this.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
591 (that.mUnwantedNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900592 }
593
Robert Greenwalt1448f052014-04-08 13:41:39 -0700594 /**
paulhu18354322020-01-09 17:08:11 +0800595 * Deduces that all the capabilities it provides are typically provided by restricted networks
596 * or not.
Paul Jensen487ffe72015-07-24 15:57:11 -0400597 *
paulhu18354322020-01-09 17:08:11 +0800598 * @return {@code true} if the network should be restricted.
Paul Jensen487ffe72015-07-24 15:57:11 -0400599 * @hide
600 */
paulhu18354322020-01-09 17:08:11 +0800601 @SystemApi
602 public boolean deduceRestrictedCapability() {
Pavel Maltsev4af91072018-03-07 14:33:22 -0800603 // Check if we have any capability that forces the network to be restricted.
604 final boolean forceRestrictedCapability =
605 (mNetworkCapabilities & FORCE_RESTRICTED_CAPABILITIES) != 0;
606
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700607 // Verify there aren't any unrestricted capabilities. If there are we say
Pavel Maltsev4af91072018-03-07 14:33:22 -0800608 // the whole thing is unrestricted unless it is forced to be restricted.
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700609 final boolean hasUnrestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800610 (mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700611
612 // Must have at least some restricted capabilities.
613 final boolean hasRestrictedCapabilities =
Pavel Maltsev4af91072018-03-07 14:33:22 -0800614 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0;
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700615
paulhu18354322020-01-09 17:08:11 +0800616 return forceRestrictedCapability
617 || (hasRestrictedCapabilities && !hasUnrestrictedCapabilities);
618 }
619
620 /**
621 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if deducing the network is restricted.
622 *
623 * @hide
624 */
625 public void maybeMarkCapabilitiesRestricted() {
626 if (deduceRestrictedCapability()) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400627 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400628 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400629 }
630
631 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700632 * Representing the transport type. Apps should generally not care about transport. A
633 * request for a fast internet connection could be satisfied by a number of different
634 * transports. If any are specified here it will be satisfied a Network that matches
635 * any of them. If a caller doesn't care about the transport it should not specify any.
636 */
637 private long mTransportTypes;
638
Jeff Sharkeyde570312017-10-24 21:25:50 -0600639 /** @hide */
640 @Retention(RetentionPolicy.SOURCE)
641 @IntDef(prefix = { "TRANSPORT_" }, value = {
642 TRANSPORT_CELLULAR,
643 TRANSPORT_WIFI,
644 TRANSPORT_BLUETOOTH,
645 TRANSPORT_ETHERNET,
646 TRANSPORT_VPN,
647 TRANSPORT_WIFI_AWARE,
648 TRANSPORT_LOWPAN,
Benedict Wong89ce5e32018-11-14 17:40:55 -0800649 TRANSPORT_TEST,
Jeff Sharkeyde570312017-10-24 21:25:50 -0600650 })
651 public @interface Transport { }
652
Robert Greenwalt1448f052014-04-08 13:41:39 -0700653 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700654 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700655 */
656 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700657
658 /**
659 * Indicates this network uses a Wi-Fi transport.
660 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700661 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700662
663 /**
664 * Indicates this network uses a Bluetooth transport.
665 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700666 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700667
668 /**
669 * Indicates this network uses an Ethernet transport.
670 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700671 public static final int TRANSPORT_ETHERNET = 3;
672
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400673 /**
674 * Indicates this network uses a VPN transport.
675 */
676 public static final int TRANSPORT_VPN = 4;
677
Etan Cohen305ea282016-06-20 09:27:12 -0700678 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700679 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700680 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700681 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700682
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700683 /**
684 * Indicates this network uses a LoWPAN transport.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700685 */
686 public static final int TRANSPORT_LOWPAN = 6;
687
Benedict Wong89ce5e32018-11-14 17:40:55 -0800688 /**
689 * Indicates this network uses a Test-only virtual interface as a transport.
690 *
691 * @hide
692 */
693 @TestApi
694 public static final int TRANSPORT_TEST = 7;
695
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900696 /** @hide */
697 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
698 /** @hide */
Benedict Wong89ce5e32018-11-14 17:40:55 -0800699 public static final int MAX_TRANSPORT = TRANSPORT_TEST;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700700
Hugo Benichi16f0a942017-06-20 14:07:59 +0900701 /** @hide */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600702 public static boolean isValidTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900703 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
704 }
705
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900706 private static final String[] TRANSPORT_NAMES = {
707 "CELLULAR",
708 "WIFI",
709 "BLUETOOTH",
710 "ETHERNET",
711 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700712 "WIFI_AWARE",
Benedict Wong89ce5e32018-11-14 17:40:55 -0800713 "LOWPAN",
714 "TEST"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900715 };
716
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700717 /**
718 * Adds the given transport type to this {@code NetworkCapability} instance.
719 * Multiple transports may be applied sequentially. Note that when searching
720 * for a network to satisfy a request, any listed in the request will satisfy the request.
721 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
722 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
723 * to be selected. This is logically different than
724 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
725 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600726 * @param transportType the transport type to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900727 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700728 */
paulhud9736de2019-03-08 16:35:20 +0800729 public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900730 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700731 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700732 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700733 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700734 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700735
736 /**
737 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
738 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600739 * @param transportType the transport type to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900740 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700741 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700742 */
paulhud9736de2019-03-08 16:35:20 +0800743 public @NonNull NetworkCapabilities removeTransportType(@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 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600751 * Sets (or clears) the given transport on this {@link NetworkCapabilities}
752 * instance.
753 *
754 * @hide
755 */
paulhud9736de2019-03-08 16:35:20 +0800756 public @NonNull NetworkCapabilities setTransportType(@Transport int transportType,
757 boolean value) {
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600758 if (value) {
759 addTransportType(transportType);
760 } else {
761 removeTransportType(transportType);
762 }
763 return this;
764 }
765
766 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700767 * Gets all the transports set on this {@code NetworkCapability} instance.
768 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600769 * @return an array of transport type values for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700770 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700771 */
Jeff Sharkeya5ee62f2018-05-14 13:49:07 -0600772 @TestApi
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900773 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800774 @NonNull public @Transport int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900775 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700776 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700777
778 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600779 * Sets all the transports set on this {@code NetworkCapability} instance.
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700780 * This overwrites any existing transports.
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600781 *
782 * @hide
783 */
784 public void setTransportTypes(@Transport int[] transportTypes) {
785 mTransportTypes = BitUtils.packBits(transportTypes);
786 }
787
788 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700789 * Tests for the presence of a transport on this instance.
790 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600791 * @param transportType the transport type to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700792 * @return {@code true} if set on this instance.
793 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600794 public boolean hasTransport(@Transport int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900795 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700796 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797
798 private void combineTransportTypes(NetworkCapabilities nc) {
799 this.mTransportTypes |= nc.mTransportTypes;
800 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900801
Robert Greenwalt1448f052014-04-08 13:41:39 -0700802 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
803 return ((this.mTransportTypes == 0) ||
804 ((this.mTransportTypes & nc.mTransportTypes) != 0));
805 }
Hugo Benichieae7a222017-07-25 11:40:56 +0900806
Robert Greenwalt06314e42014-10-29 14:04:06 -0700807 /** @hide */
808 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700809 return (nc.mTransportTypes == this.mTransportTypes);
810 }
811
812 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800813 * UID of the app that owns this network, or INVALID_UID if none/unknown.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900814 *
Qingxi Li7cf06622020-01-17 17:54:27 -0800815 * <p>This field keeps track of the UID of the app that created this network and is in charge of
816 * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running
817 * VPN, or Carrier Service app managing a cellular data connection.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900818 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800819 private int mOwnerUid = Process.INVALID_UID;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900820
821 /**
Qingxi Li7cf06622020-01-17 17:54:27 -0800822 * Set the UID of the owner app.
Chalard Jeanf474fc32018-01-17 15:10:05 +0900823 */
Qingxi Li7cf06622020-01-17 17:54:27 -0800824 public void setOwnerUid(final int uid) {
825 mOwnerUid = uid;
Chalard Jeanf474fc32018-01-17 15:10:05 +0900826 }
827
Qingxi Li7cf06622020-01-17 17:54:27 -0800828 /**
829 * Retrieves the UID of the owner app.
830 */
831 public int getOwnerUid() {
832 return mOwnerUid;
Lorenzo Colitti4c9f9542019-04-12 10:48:06 +0000833 }
834
Chalard Jeanf474fc32018-01-17 15:10:05 +0900835 /**
Cody Kesting201fc132020-01-17 11:58:36 -0800836 * UIDs of packages that are administrators of this network, or empty if none.
837 *
838 * <p>This field tracks the UIDs of packages that have permission to manage this network.
839 *
840 * <p>Network owners will also be listed as administrators.
841 *
842 * <p>For NetworkCapability instances being sent from the System Server, this value MUST be
843 * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the
844 * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+.
845 */
846 private final List<Integer> mAdministratorUids = new ArrayList<>();
847
848 /**
849 * Sets the list of UIDs that are administrators of this network.
850 *
851 * <p>UIDs included in administratorUids gain administrator privileges over this Network.
852 * Examples of UIDs that should be included in administratorUids are:
853 * <ul>
854 * <li>Carrier apps with privileges for the relevant subscription
855 * <li>Active VPN apps
856 * <li>Other application groups with a particular Network-related role
857 * </ul>
858 *
859 * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator.
860 *
Cody Kestinga75e26b2020-01-05 14:06:39 -0800861 * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST
862 * always be included in administratorUids.
Cody Kesting201fc132020-01-17 11:58:36 -0800863 *
864 * @param administratorUids the UIDs to be set as administrators of this Network.
865 * @hide
866 */
867 @SystemApi
868 public void setAdministratorUids(@NonNull final List<Integer> administratorUids) {
869 mAdministratorUids.clear();
870 mAdministratorUids.addAll(administratorUids);
871 }
872
873 /**
874 * Retrieves the list of UIDs that are administrators of this Network.
875 *
876 * @return the List of UIDs that are administrators of this Network
877 * @hide
878 */
879 @NonNull
880 @SystemApi
881 public List<Integer> getAdministratorUids() {
882 return Collections.unmodifiableList(mAdministratorUids);
883 }
884
885 /**
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600886 * Value indicating that link bandwidth is unspecified.
887 * @hide
888 */
889 public static final int LINK_BANDWIDTH_UNSPECIFIED = 0;
890
891 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700892 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
893 * for the first hop on the given transport. It is not measured, but may take into account
894 * link parameters (Radio technology, allocated channels, etc).
895 */
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600896 private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
897 private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700898
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700899 /**
900 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
901 * the estimated first hop transport bandwidth.
902 * <p>
903 * Note that when used to request a network, this specifies the minimum acceptable.
904 * When received as the state of an existing network this specifies the typical
905 * first hop bandwidth expected. This is never measured, but rather is inferred
906 * from technology type and other link parameters. It could be used to differentiate
907 * between very slow 1xRTT cellular links and other faster networks or even between
908 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
909 * fast backhauls and slow backhauls.
910 *
911 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
912 */
paulhud9736de2019-03-08 16:35:20 +0800913 public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700914 mLinkUpBandwidthKbps = upKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600915 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700916 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700917
918 /**
919 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
920 * the estimated first hop transport bandwidth.
921 *
922 * @return The estimated first hop upstream (device to network) bandwidth.
923 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700924 public int getLinkUpstreamBandwidthKbps() {
925 return mLinkUpBandwidthKbps;
926 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700927
928 /**
929 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
930 * the estimated first hop transport bandwidth.
931 * <p>
932 * Note that when used to request a network, this specifies the minimum acceptable.
933 * When received as the state of an existing network this specifies the typical
934 * first hop bandwidth expected. This is never measured, but rather is inferred
935 * from technology type and other link parameters. It could be used to differentiate
936 * between very slow 1xRTT cellular links and other faster networks or even between
937 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
938 * fast backhauls and slow backhauls.
939 *
940 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
941 */
paulhud9736de2019-03-08 16:35:20 +0800942 public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700943 mLinkDownBandwidthKbps = downKbps;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600944 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700945 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700946
947 /**
948 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
949 * the estimated first hop transport bandwidth.
950 *
951 * @return The estimated first hop downstream (network to device) bandwidth.
952 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700953 public int getLinkDownstreamBandwidthKbps() {
954 return mLinkDownBandwidthKbps;
955 }
956
957 private void combineLinkBandwidths(NetworkCapabilities nc) {
958 this.mLinkUpBandwidthKbps =
959 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
960 this.mLinkDownBandwidthKbps =
961 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
962 }
963 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
964 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
965 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
966 }
967 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
968 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
969 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
970 }
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600971 /** @hide */
972 public static int minBandwidth(int a, int b) {
973 if (a == LINK_BANDWIDTH_UNSPECIFIED) {
974 return b;
975 } else if (b == LINK_BANDWIDTH_UNSPECIFIED) {
976 return a;
977 } else {
978 return Math.min(a, b);
979 }
980 }
981 /** @hide */
982 public static int maxBandwidth(int a, int b) {
983 return Math.max(a, b);
984 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700985
Etan Cohena7434272017-04-03 12:17:51 -0700986 private NetworkSpecifier mNetworkSpecifier = null;
Etan Cohenca9fb562018-11-27 07:32:39 -0800987 private TransportInfo mTransportInfo = null;
Etan Cohena7434272017-04-03 12:17:51 -0700988
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700989 /**
990 * Sets the optional bearer specific network specifier.
991 * This has no meaning if a single transport is also not specified, so calling
992 * this without a single transport set will generate an exception, as will
993 * subsequently adding or removing transports after this is set.
994 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700995 *
Etan Cohena7434272017-04-03 12:17:51 -0700996 * @param networkSpecifier A concrete, parcelable framework class that extends
997 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900998 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700999 */
Aaron Huange6b62392019-09-20 22:52:54 +08001000 public @NonNull NetworkCapabilities setNetworkSpecifier(
1001 @NonNull NetworkSpecifier networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -07001002 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001003 throw new IllegalStateException("Must have a single transport specified to use " +
1004 "setNetworkSpecifier");
1005 }
Etan Cohena7434272017-04-03 12:17:51 -07001006
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001007 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -07001008
Pierre Imaic8419a82016-03-22 17:54:54 +09001009 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001010 }
1011
1012 /**
Etan Cohenca9fb562018-11-27 07:32:39 -08001013 * Sets the optional transport specific information.
1014 *
1015 * @param transportInfo A concrete, parcelable framework class that extends
1016 * {@link TransportInfo}.
1017 * @return This NetworkCapabilities instance, to facilitate chaining.
1018 * @hide
1019 */
Aaron Huange6b62392019-09-20 22:52:54 +08001020 @SystemApi
1021 public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) {
Etan Cohenca9fb562018-11-27 07:32:39 -08001022 mTransportInfo = transportInfo;
1023 return this;
1024 }
1025
1026 /**
paulhud9736de2019-03-08 16:35:20 +08001027 * Gets the optional bearer specific network specifier. May be {@code null} if not set.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001028 *
Etan Cohena7434272017-04-03 12:17:51 -07001029 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
paulhud9736de2019-03-08 16:35:20 +08001030 * specifier or {@code null}. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001031 */
paulhud9736de2019-03-08 16:35:20 +08001032 public @Nullable NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001033 return mNetworkSpecifier;
1034 }
1035
Etan Cohenca9fb562018-11-27 07:32:39 -08001036 /**
1037 * Returns a transport-specific information container. The application may cast this
1038 * container to a concrete sub-class based on its knowledge of the network request. The
1039 * application should be able to deal with a {@code null} return value or an invalid case,
Etan Cohenbd648ce2018-12-10 14:07:15 -08001040 * e.g. use {@code instanceof} operator to verify expected type.
Etan Cohenca9fb562018-11-27 07:32:39 -08001041 *
1042 * @return A concrete implementation of the {@link TransportInfo} class or null if not
1043 * available for the network.
1044 */
1045 @Nullable public TransportInfo getTransportInfo() {
1046 return mTransportInfo;
1047 }
1048
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001049 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001050 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001051 throw new IllegalStateException("Can't combine two networkSpecifiers");
1052 }
Etan Cohena7434272017-04-03 12:17:51 -07001053 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001054 }
Etan Cohena7434272017-04-03 12:17:51 -07001055
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001056 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001057 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
1058 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001059 }
Etan Cohena7434272017-04-03 12:17:51 -07001060
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001061 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -07001062 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001063 }
1064
Etan Cohenca9fb562018-11-27 07:32:39 -08001065 private void combineTransportInfos(NetworkCapabilities nc) {
1066 if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) {
1067 throw new IllegalStateException("Can't combine two TransportInfos");
1068 }
1069 setTransportInfo(nc.mTransportInfo);
1070 }
1071
1072 private boolean equalsTransportInfo(NetworkCapabilities nc) {
1073 return Objects.equals(mTransportInfo, nc.mTransportInfo);
1074 }
1075
Robert Greenwalt1448f052014-04-08 13:41:39 -07001076 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001077 * Magic value that indicates no signal strength provided. A request specifying this value is
1078 * always satisfied.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001079 */
1080 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
1081
1082 /**
1083 * Signal strength. This is a signed integer, and higher values indicate better signal.
1084 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
1085 */
paulhud9736de2019-03-08 16:35:20 +08001086 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Sharkey49bcd602017-11-09 13:11:50 -07001087 private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001088
1089 /**
1090 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
1091 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
Chalard Jeanb03a6222018-04-11 21:09:10 +09001092 * reported by wifi code.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001093 * <p>
1094 * Note that when used to register a network callback, this specifies the minimum acceptable
1095 * signal strength. When received as the state of an existing network it specifies the current
1096 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
1097 * effect when requesting a callback.
1098 *
1099 * @param signalStrength the bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001100 */
paulhud9736de2019-03-08 16:35:20 +08001101 public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001102 mSignalStrength = signalStrength;
Jeff Sharkey72f9c422017-10-27 17:22:59 -06001103 return this;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001104 }
1105
1106 /**
1107 * Returns {@code true} if this object specifies a signal strength.
1108 *
1109 * @hide
1110 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001111 @UnsupportedAppUsage
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001112 public boolean hasSignalStrength() {
1113 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
1114 }
1115
1116 /**
1117 * Retrieves the signal strength.
1118 *
1119 * @return The bearer-specific signal strength.
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001120 */
1121 public int getSignalStrength() {
1122 return mSignalStrength;
1123 }
1124
1125 private void combineSignalStrength(NetworkCapabilities nc) {
1126 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
1127 }
1128
1129 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
1130 return this.mSignalStrength <= nc.mSignalStrength;
1131 }
1132
1133 private boolean equalsSignalStrength(NetworkCapabilities nc) {
1134 return this.mSignalStrength == nc.mSignalStrength;
1135 }
1136
1137 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001138 * List of UIDs this network applies to. No restriction if null.
1139 * <p>
Chalard Jeanb552c462018-02-21 18:43:54 +09001140 * For networks, mUids represent the list of network this applies to, and null means this
1141 * network applies to all UIDs.
1142 * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs
1143 * must be included in a network so that they match. As an exception to the general rule,
1144 * a null mUids field for requests mean "no requirements" rather than what the general rule
1145 * would suggest ("must apply to all UIDs") : this is because this has shown to be what users
1146 * of this API expect in practice. A network that must match all UIDs can still be
1147 * expressed with a set ranging the entire set of possible UIDs.
1148 * <p>
1149 * mUids is typically (and at this time, only) used by VPN. This network is only available to
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001150 * the UIDs in this list, and it is their default network. Apps in this list that wish to
1151 * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this
1152 * member is null, then the network is not restricted by app UID. If it's an empty list, then
1153 * it means nobody can use it.
Chalard Jeanf474fc32018-01-17 15:10:05 +09001154 * As a special exception, the app managing this network (as identified by its UID stored in
Qingxi Li7cf06622020-01-17 17:54:27 -08001155 * mOwnerUid) can always see this network. This is embodied by a special check in
Chalard Jeanf474fc32018-01-17 15:10:05 +09001156 * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong>
1157 * to the app that manages it as determined by #appliesToUid.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001158 * <p>
1159 * Please note that in principle a single app can be associated with multiple UIDs because
1160 * each app will have a different UID when it's run as a different (macro-)user. A single
1161 * macro user can only have a single active VPN app at any given time however.
1162 * <p>
1163 * Also please be aware this class does not try to enforce any normalization on this. Callers
1164 * can only alter the UIDs by setting them wholesale : this class does not provide any utility
1165 * to add or remove individual UIDs or ranges. If callers have any normalization needs on
1166 * their own (like requiring sortedness or no overlap) they need to enforce it
1167 * themselves. Some of the internal methods also assume this is normalized as in no adjacent
1168 * or overlapping ranges are present.
1169 *
1170 * @hide
1171 */
Chalard Jean477e36c2018-01-25 09:41:51 +09001172 private ArraySet<UidRange> mUids = null;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001173
1174 /**
Chalard Jeandda156a2018-01-10 21:19:32 +09001175 * Convenience method to set the UIDs this network applies to to a single UID.
1176 * @hide
1177 */
paulhud9736de2019-03-08 16:35:20 +08001178 public @NonNull NetworkCapabilities setSingleUid(int uid) {
Chalard Jeandda156a2018-01-10 21:19:32 +09001179 final ArraySet<UidRange> identity = new ArraySet<>(1);
1180 identity.add(new UidRange(uid, uid));
1181 setUids(identity);
1182 return this;
1183 }
1184
1185 /**
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001186 * Set the list of UIDs this network applies to.
1187 * This makes a copy of the set so that callers can't modify it after the call.
1188 * @hide
1189 */
paulhud9736de2019-03-08 16:35:20 +08001190 public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001191 if (null == uids) {
1192 mUids = null;
1193 } else {
1194 mUids = new ArraySet<>(uids);
1195 }
1196 return this;
1197 }
1198
1199 /**
1200 * Get the list of UIDs this network applies to.
1201 * This returns a copy of the set so that callers can't modify the original object.
1202 * @hide
1203 */
paulhud9736de2019-03-08 16:35:20 +08001204 public @Nullable Set<UidRange> getUids() {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001205 return null == mUids ? null : new ArraySet<>(mUids);
1206 }
1207
1208 /**
1209 * Test whether this network applies to this UID.
1210 * @hide
1211 */
1212 public boolean appliesToUid(int uid) {
1213 if (null == mUids) return true;
1214 for (UidRange range : mUids) {
1215 if (range.contains(uid)) {
1216 return true;
1217 }
1218 }
1219 return false;
1220 }
1221
1222 /**
Chalard Jeanb03a6222018-04-11 21:09:10 +09001223 * 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 +09001224 * <p>
1225 * This test only checks whether equal range objects are in both sets. It will
1226 * return false if the ranges are not exactly the same, even if the covered UIDs
1227 * are for an equivalent result.
1228 * <p>
1229 * Note that this method is not very optimized, which is fine as long as it's not used very
1230 * often.
1231 * <p>
1232 * nc is assumed nonnull.
1233 *
1234 * @hide
1235 */
1236 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001237 public boolean equalsUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001238 Set<UidRange> comparedUids = nc.mUids;
1239 if (null == comparedUids) return null == mUids;
1240 if (null == mUids) return false;
1241 // Make a copy so it can be mutated to check that all ranges in mUids
1242 // also are in uids.
1243 final Set<UidRange> uids = new ArraySet<>(mUids);
1244 for (UidRange range : comparedUids) {
1245 if (!uids.contains(range)) {
1246 return false;
1247 }
1248 uids.remove(range);
1249 }
1250 return uids.isEmpty();
1251 }
1252
1253 /**
1254 * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require.
1255 *
Chalard Jeanf474fc32018-01-17 15:10:05 +09001256 * This method is called on the NetworkCapabilities embedded in a request with the
1257 * capabilities of an available network. It checks whether all the UIDs from this listen
1258 * (representing the UIDs that must have access to the network) are satisfied by the UIDs
1259 * in the passed nc (representing the UIDs that this network is available to).
1260 * <p>
1261 * As a special exception, the UID that created the passed network (as represented by its
Qingxi Li7cf06622020-01-17 17:54:27 -08001262 * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN
Chalard Jeanf474fc32018-01-17 15:10:05 +09001263 * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app
1264 * can see its own network when it listens for it.
1265 * <p>
1266 * nc is assumed nonnull. Else, NPE.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001267 * @see #appliesToUid
1268 * @hide
1269 */
paulhud9736de2019-03-08 16:35:20 +08001270 public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanb552c462018-02-21 18:43:54 +09001271 if (null == nc.mUids || null == mUids) return true; // The network satisfies everything.
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001272 for (UidRange requiredRange : mUids) {
Qingxi Li7cf06622020-01-17 17:54:27 -08001273 if (requiredRange.contains(nc.mOwnerUid)) return true;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001274 if (!nc.appliesToUidRange(requiredRange)) {
1275 return false;
1276 }
1277 }
1278 return true;
1279 }
1280
1281 /**
1282 * Returns whether this network applies to the passed ranges.
1283 * This assumes that to apply, the passed range has to be entirely contained
1284 * within one of the ranges this network applies to. If the ranges are not normalized,
1285 * this method may return false even though all required UIDs are covered because no
1286 * single range contained them all.
1287 * @hide
1288 */
1289 @VisibleForTesting
paulhud9736de2019-03-08 16:35:20 +08001290 public boolean appliesToUidRange(@Nullable UidRange requiredRange) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001291 if (null == mUids) return true;
1292 for (UidRange uidRange : mUids) {
1293 if (uidRange.containsRange(requiredRange)) {
1294 return true;
1295 }
1296 }
1297 return false;
1298 }
1299
1300 /**
1301 * Combine the UIDs this network currently applies to with the UIDs the passed
1302 * NetworkCapabilities apply to.
1303 * nc is assumed nonnull.
1304 */
paulhud9736de2019-03-08 16:35:20 +08001305 private void combineUids(@NonNull NetworkCapabilities nc) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001306 if (null == nc.mUids || null == mUids) {
1307 mUids = null;
1308 return;
1309 }
1310 mUids.addAll(nc.mUids);
1311 }
1312
Chalard Jeanb03a6222018-04-11 21:09:10 +09001313
1314 /**
1315 * The SSID of the network, or null if not applicable or unknown.
1316 * <p>
1317 * This is filled in by wifi code.
1318 * @hide
1319 */
1320 private String mSSID;
1321
1322 /**
1323 * Sets the SSID of this network.
1324 * @hide
1325 */
Aaron Huange6b62392019-09-20 22:52:54 +08001326 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001327 public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001328 mSSID = ssid;
1329 return this;
1330 }
1331
1332 /**
1333 * Gets the SSID of this network, or null if none or unknown.
1334 * @hide
1335 */
Remi NGUYEN VANaa4c5112020-01-22 22:52:53 +09001336 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001337 public @Nullable String getSSID() {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001338 return mSSID;
1339 }
1340
1341 /**
1342 * Tests if the SSID of this network is the same as the SSID of the passed network.
1343 * @hide
1344 */
paulhud9736de2019-03-08 16:35:20 +08001345 public boolean equalsSSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001346 return Objects.equals(mSSID, nc.mSSID);
1347 }
1348
1349 /**
1350 * Check if the SSID requirements of this object are matched by the passed object.
1351 * @hide
1352 */
paulhud9736de2019-03-08 16:35:20 +08001353 public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001354 return mSSID == null || mSSID.equals(nc.mSSID);
1355 }
1356
1357 /**
1358 * Combine SSIDs of the capabilities.
1359 * <p>
1360 * This is only legal if either the SSID of this object is null, or both SSIDs are
1361 * equal.
1362 * @hide
1363 */
paulhud9736de2019-03-08 16:35:20 +08001364 private void combineSSIDs(@NonNull NetworkCapabilities nc) {
Chalard Jeanb03a6222018-04-11 21:09:10 +09001365 if (mSSID != null && !mSSID.equals(nc.mSSID)) {
1366 throw new IllegalStateException("Can't combine two SSIDs");
1367 }
1368 setSSID(nc.mSSID);
1369 }
1370
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001371 /**
Pavel Maltseve18ef262018-03-07 11:13:04 -08001372 * Combine a set of Capabilities to this one. Useful for coming up with the complete set.
1373 * <p>
1374 * Note that this method may break an invariant of having a particular capability in either
1375 * wanted or unwanted lists but never in both. Requests that have the same capability in
1376 * both lists will never be satisfied.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001377 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001378 */
paulhud9736de2019-03-08 16:35:20 +08001379 public void combineCapabilities(@NonNull NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001380 combineNetCapabilities(nc);
1381 combineTransportTypes(nc);
1382 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001383 combineSpecifiers(nc);
Etan Cohenca9fb562018-11-27 07:32:39 -08001384 combineTransportInfos(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001385 combineSignalStrength(nc);
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001386 combineUids(nc);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001387 combineSSIDs(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001388 }
1389
1390 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001391 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1392 *
1393 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1394 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
1395 * bandwidth, signal strength, or validation / captive portal status.
1396 *
1397 * @hide
1398 */
1399 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001400 return (nc != null
1401 && satisfiedByNetCapabilities(nc, onlyImmutable)
1402 && satisfiedByTransportTypes(nc)
1403 && (onlyImmutable || satisfiedByLinkBandwidths(nc))
1404 && satisfiedBySpecifier(nc)
1405 && (onlyImmutable || satisfiedBySignalStrength(nc))
Chalard Jeanb03a6222018-04-11 21:09:10 +09001406 && (onlyImmutable || satisfiedByUids(nc))
1407 && (onlyImmutable || satisfiedBySSID(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001408 }
1409
1410 /**
1411 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
1412 *
1413 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1414 *
1415 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -07001416 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001417 @TestApi
1418 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001419 public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001420 return satisfiedByNetworkCapabilities(nc, false);
1421 }
1422
1423 /**
1424 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
1425 *
1426 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
1427 *
1428 * @hide
1429 */
paulhud9736de2019-03-08 16:35:20 +08001430 public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001431 return satisfiedByNetworkCapabilities(nc, true);
1432 }
1433
1434 /**
1435 * Checks that our immutable capabilities are the same as those of the given
Hugo Benichieae7a222017-07-25 11:40:56 +09001436 * {@code NetworkCapabilities} and return a String describing any difference.
1437 * The returned String is empty if there is no difference.
Lorenzo Colitti260a36d2015-07-08 12:49:04 +09001438 *
1439 * @hide
1440 */
paulhud9736de2019-03-08 16:35:20 +08001441 public String describeImmutableDifferences(@Nullable NetworkCapabilities that) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001442 if (that == null) {
1443 return "other NetworkCapabilities was null";
1444 }
1445
1446 StringJoiner joiner = new StringJoiner(", ");
1447
Hugo Benichieae7a222017-07-25 11:40:56 +09001448 // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
1449 // TODO: properly support NOT_METERED as a mutable and requestable capability.
Hugo Benichi2ecb9402017-08-04 13:18:40 +09001450 final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
Hugo Benichieae7a222017-07-25 11:40:56 +09001451 long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
1452 long newImmutableCapabilities = that.mNetworkCapabilities & mask;
1453 if (oldImmutableCapabilities != newImmutableCapabilities) {
1454 String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities));
1455 String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities));
1456 joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after));
1457 }
1458
1459 if (!equalsSpecifier(that)) {
1460 NetworkSpecifier before = this.getNetworkSpecifier();
1461 NetworkSpecifier after = that.getNetworkSpecifier();
1462 joiner.add(String.format("specifier changed: %s -> %s", before, after));
1463 }
1464
1465 if (!equalsTransportTypes(that)) {
1466 String before = transportNamesOf(this.getTransportTypes());
1467 String after = transportNamesOf(that.getTransportTypes());
1468 joiner.add(String.format("transports changed: %s -> %s", before, after));
1469 }
1470
1471 return joiner.toString();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001472 }
1473
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001474 /**
1475 * Checks that our requestable capabilities are the same as those of the given
1476 * {@code NetworkCapabilities}.
1477 *
1478 * @hide
1479 */
paulhud9736de2019-03-08 16:35:20 +08001480 public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) {
Lorenzo Colittif0e9a332016-07-18 18:40:42 +09001481 if (nc == null) return false;
1482 return (equalsNetCapabilitiesRequestable(nc) &&
1483 equalsTransportTypes(nc) &&
1484 equalsSpecifier(nc));
1485 }
1486
Robert Greenwalt1448f052014-04-08 13:41:39 -07001487 @Override
paulhud9736de2019-03-08 16:35:20 +08001488 public boolean equals(@Nullable Object obj) {
Robert Greenwalt1448f052014-04-08 13:41:39 -07001489 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001490 NetworkCapabilities that = (NetworkCapabilities) obj;
1491 return (equalsNetCapabilities(that)
1492 && equalsTransportTypes(that)
1493 && equalsLinkBandwidths(that)
1494 && equalsSignalStrength(that)
1495 && equalsSpecifier(that)
Etan Cohenca9fb562018-11-27 07:32:39 -08001496 && equalsTransportInfo(that)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001497 && equalsUids(that)
lucaslin783f2212019-10-22 18:27:33 +08001498 && equalsSSID(that)
1499 && equalsPrivateDnsBroken(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -07001500 }
1501
1502 @Override
1503 public int hashCode() {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001504 return (int) (mNetworkCapabilities & 0xFFFFFFFF)
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001505 + ((int) (mNetworkCapabilities >> 32) * 3)
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001506 + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5)
1507 + ((int) (mUnwantedNetworkCapabilities >> 32) * 7)
1508 + ((int) (mTransportTypes & 0xFFFFFFFF) * 11)
1509 + ((int) (mTransportTypes >> 32) * 13)
1510 + (mLinkUpBandwidthKbps * 17)
1511 + (mLinkDownBandwidthKbps * 19)
1512 + Objects.hashCode(mNetworkSpecifier) * 23
1513 + (mSignalStrength * 29)
Chalard Jeanb03a6222018-04-11 21:09:10 +09001514 + Objects.hashCode(mUids) * 31
Etan Cohenca9fb562018-11-27 07:32:39 -08001515 + Objects.hashCode(mSSID) * 37
lucaslin783f2212019-10-22 18:27:33 +08001516 + Objects.hashCode(mTransportInfo) * 41
1517 + Objects.hashCode(mPrivateDnsBroken) * 43;
Robert Greenwalt1448f052014-04-08 13:41:39 -07001518 }
1519
Wink Saville4e2dea72014-09-20 11:04:03 -07001520 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001521 public int describeContents() {
1522 return 0;
1523 }
Cody Kesting201fc132020-01-17 11:58:36 -08001524
Wink Saville4e2dea72014-09-20 11:04:03 -07001525 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001526 public void writeToParcel(Parcel dest, int flags) {
1527 dest.writeLong(mNetworkCapabilities);
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001528 dest.writeLong(mUnwantedNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001529 dest.writeLong(mTransportTypes);
1530 dest.writeInt(mLinkUpBandwidthKbps);
1531 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -07001532 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Etan Cohenca9fb562018-11-27 07:32:39 -08001533 dest.writeParcelable((Parcelable) mTransportInfo, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001534 dest.writeInt(mSignalStrength);
Chalard Jean477e36c2018-01-25 09:41:51 +09001535 dest.writeArraySet(mUids);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001536 dest.writeString(mSSID);
lucaslin783f2212019-10-22 18:27:33 +08001537 dest.writeBoolean(mPrivateDnsBroken);
Cody Kesting201fc132020-01-17 11:58:36 -08001538 dest.writeList(mAdministratorUids);
Qingxi Li7cf06622020-01-17 17:54:27 -08001539 dest.writeInt(mOwnerUid);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001540 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001541
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001542 public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR =
Robert Greenwalt1448f052014-04-08 13:41:39 -07001543 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -07001544 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001545 public NetworkCapabilities createFromParcel(Parcel in) {
1546 NetworkCapabilities netCap = new NetworkCapabilities();
1547
1548 netCap.mNetworkCapabilities = in.readLong();
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001549 netCap.mUnwantedNetworkCapabilities = in.readLong();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001550 netCap.mTransportTypes = in.readLong();
1551 netCap.mLinkUpBandwidthKbps = in.readInt();
1552 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -07001553 netCap.mNetworkSpecifier = in.readParcelable(null);
Etan Cohenca9fb562018-11-27 07:32:39 -08001554 netCap.mTransportInfo = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001555 netCap.mSignalStrength = in.readInt();
Chalard Jeanecacd5e2017-12-27 14:23:31 +09001556 netCap.mUids = (ArraySet<UidRange>) in.readArraySet(
1557 null /* ClassLoader, null for default */);
Chalard Jeanb03a6222018-04-11 21:09:10 +09001558 netCap.mSSID = in.readString();
lucaslin783f2212019-10-22 18:27:33 +08001559 netCap.mPrivateDnsBroken = in.readBoolean();
Cody Kesting201fc132020-01-17 11:58:36 -08001560 netCap.setAdministratorUids(in.readArrayList(null));
Qingxi Li7cf06622020-01-17 17:54:27 -08001561 netCap.mOwnerUid = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -07001562 return netCap;
1563 }
Wink Saville4e2dea72014-09-20 11:04:03 -07001564 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -07001565 public NetworkCapabilities[] newArray(int size) {
1566 return new NetworkCapabilities[size];
1567 }
1568 };
1569
Wink Saville4e2dea72014-09-20 11:04:03 -07001570 @Override
paulhud9736de2019-03-08 16:35:20 +08001571 public @NonNull String toString() {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001572 final StringBuilder sb = new StringBuilder("[");
1573 if (0 != mTransportTypes) {
1574 sb.append(" Transports: ");
1575 appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes,
1576 NetworkCapabilities::transportNameOf, "|");
1577 }
1578 if (0 != mNetworkCapabilities) {
1579 sb.append(" Capabilities: ");
1580 appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities,
1581 NetworkCapabilities::capabilityNameOf, "&");
1582 }
jiayanhonge20a4fe2018-11-23 14:23:04 +08001583 if (0 != mUnwantedNetworkCapabilities) {
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001584 sb.append(" Unwanted: ");
1585 appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities,
1586 NetworkCapabilities::capabilityNameOf, "&");
1587 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001588 if (mLinkUpBandwidthKbps > 0) {
1589 sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps");
1590 }
1591 if (mLinkDownBandwidthKbps > 0) {
1592 sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps");
1593 }
1594 if (mNetworkSpecifier != null) {
1595 sb.append(" Specifier: <").append(mNetworkSpecifier).append(">");
1596 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001597 if (mTransportInfo != null) {
1598 sb.append(" TransportInfo: <").append(mTransportInfo).append(">");
1599 }
Chalard Jean07ace0f2018-02-26 19:00:45 +09001600 if (hasSignalStrength()) {
1601 sb.append(" SignalStrength: ").append(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -07001602 }
1603
Chalard Jean07ace0f2018-02-26 19:00:45 +09001604 if (null != mUids) {
1605 if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) {
1606 sb.append(" Uid: ").append(mUids.valueAt(0).start);
1607 } else {
1608 sb.append(" Uids: <").append(mUids).append(">");
1609 }
1610 }
Qingxi Li7cf06622020-01-17 17:54:27 -08001611 if (mOwnerUid != Process.INVALID_UID) {
1612 sb.append(" OwnerUid: ").append(mOwnerUid);
Chalard Jean07ace0f2018-02-26 19:00:45 +09001613 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001614
Cody Kesting201fc132020-01-17 11:58:36 -08001615 if (!mAdministratorUids.isEmpty()) {
1616 sb.append(" AdministratorUids: ").append(mAdministratorUids);
1617 }
1618
Chalard Jeanb03a6222018-04-11 21:09:10 +09001619 if (null != mSSID) {
1620 sb.append(" SSID: ").append(mSSID);
1621 }
1622
lucaslin783f2212019-10-22 18:27:33 +08001623 if (mPrivateDnsBroken) {
1624 sb.append(" Private DNS is broken");
1625 }
1626
Chalard Jean07ace0f2018-02-26 19:00:45 +09001627 sb.append("]");
1628 return sb.toString();
1629 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -07001630
Lorenzo Colittic3f21f32015-07-06 23:50:27 +09001631
Chalard Jean07ace0f2018-02-26 19:00:45 +09001632 private interface NameOf {
1633 String nameOf(int value);
1634 }
1635 /**
1636 * @hide
1637 */
paulhud9736de2019-03-08 16:35:20 +08001638 public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb,
1639 long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) {
Chalard Jean07ace0f2018-02-26 19:00:45 +09001640 int bitPos = 0;
1641 boolean firstElementAdded = false;
1642 while (bitMask != 0) {
1643 if ((bitMask & 1) != 0) {
1644 if (firstElementAdded) {
1645 sb.append(separator);
1646 } else {
1647 firstElementAdded = true;
1648 }
1649 sb.append(nameFetcher.nameOf(bitPos));
1650 }
1651 bitMask >>= 1;
1652 ++bitPos;
1653 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001654 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001655
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001656 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001657 public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001658 final long token = proto.start(fieldId);
1659
1660 for (int transport : getTransportTypes()) {
1661 proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport);
1662 }
1663
1664 for (int capability : getCapabilities()) {
1665 proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability);
1666 }
1667
1668 proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps);
1669 proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps);
1670
1671 if (mNetworkSpecifier != null) {
1672 proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString());
1673 }
Etan Cohenca9fb562018-11-27 07:32:39 -08001674 if (mTransportInfo != null) {
1675 // TODO b/120653863: write transport-specific info to proto?
1676 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001677
1678 proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength());
1679 proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength);
1680
1681 proto.end(token);
1682 }
1683
Hugo Benichi5df9d722016-04-25 17:16:35 +09001684 /**
1685 * @hide
1686 */
paulhud9736de2019-03-08 16:35:20 +08001687 public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001688 StringJoiner joiner = new StringJoiner("|");
1689 if (capabilities != null) {
1690 for (int c : capabilities) {
1691 joiner.add(capabilityNameOf(c));
1692 }
1693 }
1694 return joiner.toString();
1695 }
1696
1697 /**
1698 * @hide
1699 */
paulhud9736de2019-03-08 16:35:20 +08001700 public static @NonNull String capabilityNameOf(@NetCapability int capability) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001701 switch (capability) {
lucasline252a742019-03-12 13:08:03 +08001702 case NET_CAPABILITY_MMS: return "MMS";
1703 case NET_CAPABILITY_SUPL: return "SUPL";
1704 case NET_CAPABILITY_DUN: return "DUN";
1705 case NET_CAPABILITY_FOTA: return "FOTA";
1706 case NET_CAPABILITY_IMS: return "IMS";
1707 case NET_CAPABILITY_CBS: return "CBS";
1708 case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P";
1709 case NET_CAPABILITY_IA: return "IA";
1710 case NET_CAPABILITY_RCS: return "RCS";
1711 case NET_CAPABILITY_XCAP: return "XCAP";
1712 case NET_CAPABILITY_EIMS: return "EIMS";
1713 case NET_CAPABILITY_NOT_METERED: return "NOT_METERED";
1714 case NET_CAPABILITY_INTERNET: return "INTERNET";
1715 case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED";
1716 case NET_CAPABILITY_TRUSTED: return "TRUSTED";
1717 case NET_CAPABILITY_NOT_VPN: return "NOT_VPN";
1718 case NET_CAPABILITY_VALIDATED: return "VALIDATED";
1719 case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL";
1720 case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING";
1721 case NET_CAPABILITY_FOREGROUND: return "FOREGROUND";
1722 case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED";
1723 case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED";
1724 case NET_CAPABILITY_OEM_PAID: return "OEM_PAID";
1725 case NET_CAPABILITY_MCX: return "MCX";
1726 case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY";
1727 default: return Integer.toString(capability);
Hugo Benichieae7a222017-07-25 11:40:56 +09001728 }
1729 }
1730
1731 /**
1732 * @hide
1733 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001734 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001735 public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) {
Hugo Benichieae7a222017-07-25 11:40:56 +09001736 StringJoiner joiner = new StringJoiner("|");
1737 if (types != null) {
1738 for (int t : types) {
1739 joiner.add(transportNameOf(t));
1740 }
Hugo Benichi5df9d722016-04-25 17:16:35 +09001741 }
Hugo Benichieae7a222017-07-25 11:40:56 +09001742 return joiner.toString();
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001743 }
1744
1745 /**
1746 * @hide
1747 */
paulhud9736de2019-03-08 16:35:20 +08001748 public static @NonNull String transportNameOf(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001749 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +09001750 return "UNKNOWN";
1751 }
1752 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +09001753 }
Hugo Benichi16f0a942017-06-20 14:07:59 +09001754
Jeff Sharkeyde570312017-10-24 21:25:50 -06001755 private static void checkValidTransportType(@Transport int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +09001756 Preconditions.checkArgument(
1757 isValidTransport(transport), "Invalid TransportType " + transport);
1758 }
Pavel Maltsev1cd48da2018-02-01 11:16:02 -08001759
1760 private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) {
1761 return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY;
1762 }
1763
1764 private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) {
1765 Preconditions.checkArgument(isValidCapability(capability),
1766 "NetworkCapability " + capability + "out of range");
1767 }
junyulai05986c62018-08-07 19:50:45 +08001768
1769 /**
1770 * Check if this {@code NetworkCapability} instance is metered.
1771 *
1772 * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance.
1773 * @hide
1774 */
1775 public boolean isMetered() {
1776 return !hasCapability(NET_CAPABILITY_NOT_METERED);
1777 }
lucaslin783f2212019-10-22 18:27:33 +08001778
1779 /**
1780 * Check if private dns is broken.
1781 *
1782 * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken.
1783 * @hide
1784 */
1785 public boolean isPrivateDnsBroken() {
1786 return mPrivateDnsBroken;
1787 }
1788
1789 /**
1790 * Set mPrivateDnsBroken to true when private dns is broken.
1791 *
1792 * @param broken the status of private DNS to be set.
1793 * @hide
1794 */
1795 public void setPrivateDnsBroken(boolean broken) {
1796 mPrivateDnsBroken = broken;
1797 }
1798
1799 private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) {
1800 return mPrivateDnsBroken == nc.mPrivateDnsBroken;
1801 }
Robert Greenwalt1448f052014-04-08 13:41:39 -07001802}