blob: 1da0d281538d1ffda7ba9752ed718994b361146b [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
19import android.os.Parcel;
20import android.os.Parcelable;
Robert Greenwalta7e148a2017-04-10 14:32:23 -070021
22import com.android.internal.annotations.VisibleForTesting;
Hugo Benichi9910dbc2017-03-22 18:29:58 +090023import com.android.internal.util.BitUtils;
Etan Cohena7434272017-04-03 12:17:51 -070024
25import java.util.Objects;
Robert Greenwalt1448f052014-04-08 13:41:39 -070026
27/**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070028 * This class represents the capabilities of a network. This is used both to specify
29 * needs to {@link ConnectivityManager} and when inspecting a network.
30 *
31 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
32 * of network selection. Rather than indicate a need for Wi-Fi because an application
Wink Saville4e2dea72014-09-20 11:04:03 -070033 * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE),
Robert Greenwalt01d004e2014-05-18 15:24:21 -070034 * the application should specify it needs high bandwidth. Similarly if an application
35 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
36 * all cellular based connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070037 */
38public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070039 private static final String TAG = "NetworkCapabilities";
40
Robert Greenwalt7569f182014-06-08 16:42:59 -070041 /**
42 * @hide
43 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070044 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090045 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090046 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070047 }
48
49 public NetworkCapabilities(NetworkCapabilities nc) {
50 if (nc != null) {
51 mNetworkCapabilities = nc.mNetworkCapabilities;
52 mTransportTypes = nc.mTransportTypes;
53 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
54 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070055 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090056 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070057 }
58 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070059
60 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090061 * Completely clears the contents of this object, removing even the capabilities that are set
62 * by default when the object is constructed.
63 * @hide
64 */
65 public void clearAll() {
66 mNetworkCapabilities = mTransportTypes = 0;
67 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
68 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090069 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090070 }
71
72 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070073 * Represents the network's capabilities. If any are specified they will be satisfied
74 * by any Network that matches all of them.
75 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090076 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070077
78 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070079 * Indicates this is a network that has the ability to reach the
80 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070081 */
82 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070083
84 /**
85 * Indicates this is a network that has the ability to reach the carrier's
86 * SUPL server, used to retrieve GPS information.
87 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070088 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070089
90 /**
91 * Indicates this is a network that has the ability to reach the carrier's
92 * DUN or tethering gateway.
93 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070094 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070095
96 /**
97 * Indicates this is a network that has the ability to reach the carrier's
98 * FOTA portal, used for over the air updates.
99 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700100 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700101
102 /**
103 * Indicates this is a network that has the ability to reach the carrier's
104 * IMS servers, used for network registration and signaling.
105 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700106 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700107
108 /**
109 * Indicates this is a network that has the ability to reach the carrier's
110 * CBS servers, used for carrier specific services.
111 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700112 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700113
114 /**
115 * Indicates this is a network that has the ability to reach a Wi-Fi direct
116 * peer.
117 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700118 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700119
120 /**
121 * Indicates this is a network that has the ability to reach a carrier's
122 * Initial Attach servers.
123 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700124 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700125
126 /**
127 * Indicates this is a network that has the ability to reach a carrier's
128 * RCS servers, used for Rich Communication Services.
129 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700130 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700131
132 /**
133 * Indicates this is a network that has the ability to reach a carrier's
134 * XCAP servers, used for configuration and control.
135 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700136 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700137
138 /**
139 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700140 * Emergency IMS servers or other services, used for network signaling
141 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates that this network is unmetered.
147 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700148 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700149
150 /**
151 * Indicates that this network should be able to reach the internet.
152 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700153 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700154
155 /**
156 * Indicates that this network is available for general use. If this is not set
157 * applications should not attempt to communicate on this network. Note that this
158 * is simply informative and not enforcement - enforcement is handled via other means.
159 * Set by default.
160 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700161 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
162
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700163 /**
164 * Indicates that the user has indicated implicit trust of this network. This
165 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
166 * BT device or a wifi the user asked to connect to. Untrusted networks
167 * are probably limited to unknown wifi AP. Set by default.
168 */
169 public static final int NET_CAPABILITY_TRUSTED = 14;
170
Paul Jensen76b610a2015-03-18 09:33:07 -0400171 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400172 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400173 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400174 */
175 public static final int NET_CAPABILITY_NOT_VPN = 15;
176
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900177 /**
178 * Indicates that connectivity on this network was successfully validated. For example, for a
179 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
180 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900181 */
182 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700183
Paul Jensen3d194ea2015-06-16 14:27:36 -0400184 /**
185 * Indicates that this network was found to have a captive portal in place last time it was
186 * probed.
187 */
188 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
189
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900190 /**
191 * Indicates that this network is available for use by apps, and not a network that is being
192 * kept up in the background to facilitate fast network switching.
193 * @hide
194 */
195 public static final int NET_CAPABILITY_FOREGROUND = 18;
196
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900198 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700199
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700200 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900201 * Network capabilities that are expected to be mutable, i.e., can change while a particular
202 * network is connected.
203 */
204 private static final long MUTABLE_CAPABILITIES =
205 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
206 // http://b/18206275
207 (1 << NET_CAPABILITY_TRUSTED) |
208 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900209 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
210 (1 << NET_CAPABILITY_FOREGROUND);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900211
212 /**
213 * Network capabilities that are not allowed in NetworkRequests. This exists because the
214 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
215 * capability's presence cannot be known in advance. If such a capability is requested, then we
216 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
217 * get immediately torn down because they do not have the requested capability.
218 */
219 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900220 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900221
222 /**
223 * Capabilities that are set by default when the object is constructed.
224 */
225 private static final long DEFAULT_CAPABILITIES =
226 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
227 (1 << NET_CAPABILITY_TRUSTED) |
228 (1 << NET_CAPABILITY_NOT_VPN);
229
230 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400231 * Capabilities that suggest that a network is restricted.
232 * {@see #maybeMarkCapabilitiesRestricted}.
233 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700234 @VisibleForTesting
235 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400236 (1 << NET_CAPABILITY_CBS) |
237 (1 << NET_CAPABILITY_DUN) |
238 (1 << NET_CAPABILITY_EIMS) |
239 (1 << NET_CAPABILITY_FOTA) |
240 (1 << NET_CAPABILITY_IA) |
241 (1 << NET_CAPABILITY_IMS) |
242 (1 << NET_CAPABILITY_RCS) |
243 (1 << NET_CAPABILITY_XCAP);
244
245 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700246 * Capabilities that suggest that a network is unrestricted.
247 * {@see #maybeMarkCapabilitiesRestricted}.
248 */
249 @VisibleForTesting
250 /* package */ static final long UNRESTRICTED_CAPABILITIES =
251 (1 << NET_CAPABILITY_INTERNET) |
252 (1 << NET_CAPABILITY_MMS) |
253 (1 << NET_CAPABILITY_SUPL) |
254 (1 << NET_CAPABILITY_WIFI_P2P);
255
256 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700257 * Adds the given capability to this {@code NetworkCapability} instance.
258 * Multiple capabilities may be applied sequentially. Note that when searching
259 * for a network to satisfy a request, all capabilities requested must be satisfied.
260 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700261 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900262 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700263 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700264 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700265 public NetworkCapabilities addCapability(int capability) {
266 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700267 throw new IllegalArgumentException("NetworkCapability out of range");
268 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700269 mNetworkCapabilities |= 1 << capability;
270 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700271 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700272
273 /**
274 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
275 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700276 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900277 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700278 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700279 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700280 public NetworkCapabilities removeCapability(int capability) {
281 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700282 throw new IllegalArgumentException("NetworkCapability out of range");
283 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700284 mNetworkCapabilities &= ~(1 << capability);
285 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700286 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700287
288 /**
289 * Gets all the capabilities set on this {@code NetworkCapability} instance.
290 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700291 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700292 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700293 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700294 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700295 public int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900296 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700297 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700298
299 /**
300 * Tests for the presence of a capabilitity on this instance.
301 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700302 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700303 * @return {@code true} if set on this instance.
304 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700305 public boolean hasCapability(int capability) {
306 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700307 return false;
308 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700309 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700310 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700311
Robert Greenwalt1448f052014-04-08 13:41:39 -0700312 private void combineNetCapabilities(NetworkCapabilities nc) {
313 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
314 }
315
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900316 /**
317 * Convenience function that returns a human-readable description of the first mutable
318 * capability we find. Used to present an error message to apps that request mutable
319 * capabilities.
320 *
321 * @hide
322 */
323 public String describeFirstNonRequestableCapability() {
324 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
325 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900326 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900327 // This cannot happen unless the preceding checks are incomplete.
328 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
329 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
330 }
331 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900332 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900333 return null;
334 }
335
336 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
337 long networkCapabilities = this.mNetworkCapabilities;
338 if (onlyImmutable) {
339 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
340 }
341 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700342 }
343
Robert Greenwalt06314e42014-10-29 14:04:06 -0700344 /** @hide */
345 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700346 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
347 }
348
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900349 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
350 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
351 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
352 }
353
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900354 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
355 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
356 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
357 }
358
Robert Greenwalt1448f052014-04-08 13:41:39 -0700359 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400360 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
361 * typically provided by restricted networks.
362 *
363 * TODO: consider:
364 * - Renaming it to guessRestrictedCapability and make it set the
365 * restricted capability bit in addition to clearing it.
366 * @hide
367 */
368 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700369 // Verify there aren't any unrestricted capabilities. If there are we say
370 // the whole thing is unrestricted.
371 final boolean hasUnrestrictedCapabilities =
372 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
373
374 // Must have at least some restricted capabilities.
375 final boolean hasRestrictedCapabilities =
376 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
377
378 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400379 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400380 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400381 }
382
383 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700384 * Representing the transport type. Apps should generally not care about transport. A
385 * request for a fast internet connection could be satisfied by a number of different
386 * transports. If any are specified here it will be satisfied a Network that matches
387 * any of them. If a caller doesn't care about the transport it should not specify any.
388 */
389 private long mTransportTypes;
390
391 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700392 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700393 */
394 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700395
396 /**
397 * Indicates this network uses a Wi-Fi transport.
398 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700399 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700400
401 /**
402 * Indicates this network uses a Bluetooth transport.
403 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700404 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700405
406 /**
407 * Indicates this network uses an Ethernet transport.
408 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700409 public static final int TRANSPORT_ETHERNET = 3;
410
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400411 /**
412 * Indicates this network uses a VPN transport.
413 */
414 public static final int TRANSPORT_VPN = 4;
415
Etan Cohen305ea282016-06-20 09:27:12 -0700416 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700417 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700418 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700419 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700420
Robert Greenwalt1448f052014-04-08 13:41:39 -0700421 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Etan Cohen0849ded2016-10-26 11:22:06 -0700422 private static final int MAX_TRANSPORT = TRANSPORT_WIFI_AWARE;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700423
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900424 private static final String[] TRANSPORT_NAMES = {
425 "CELLULAR",
426 "WIFI",
427 "BLUETOOTH",
428 "ETHERNET",
429 "VPN",
430 "WIFI_AWARE"
431 };
432
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700433 /**
434 * Adds the given transport type to this {@code NetworkCapability} instance.
435 * Multiple transports may be applied sequentially. Note that when searching
436 * for a network to satisfy a request, any listed in the request will satisfy the request.
437 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
438 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
439 * to be selected. This is logically different than
440 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
441 *
442 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900443 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700444 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700445 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700446 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700447 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
448 throw new IllegalArgumentException("TransportType out of range");
449 }
450 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700451 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
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 /**
456 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
457 *
458 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900459 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700460 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700461 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700462 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700463 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
464 throw new IllegalArgumentException("TransportType out of range");
465 }
466 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700467 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700468 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700469 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700470
471 /**
472 * Gets all the transports set on this {@code NetworkCapability} instance.
473 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700474 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700475 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700476 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700477 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700478 public int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900479 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700481
482 /**
483 * Tests for the presence of a transport on this instance.
484 *
485 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
486 * @return {@code true} if set on this instance.
487 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700488 public boolean hasTransport(int transportType) {
489 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
490 return false;
491 }
492 return ((mTransportTypes & (1 << transportType)) != 0);
493 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700494
495 private void combineTransportTypes(NetworkCapabilities nc) {
496 this.mTransportTypes |= nc.mTransportTypes;
497 }
498 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
499 return ((this.mTransportTypes == 0) ||
500 ((this.mTransportTypes & nc.mTransportTypes) != 0));
501 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700502 /** @hide */
503 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700504 return (nc.mTransportTypes == this.mTransportTypes);
505 }
506
507 /**
508 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
509 * for the first hop on the given transport. It is not measured, but may take into account
510 * link parameters (Radio technology, allocated channels, etc).
511 */
512 private int mLinkUpBandwidthKbps;
513 private int mLinkDownBandwidthKbps;
514
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515 /**
516 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
517 * the estimated first hop transport bandwidth.
518 * <p>
519 * Note that when used to request a network, this specifies the minimum acceptable.
520 * When received as the state of an existing network this specifies the typical
521 * first hop bandwidth expected. This is never measured, but rather is inferred
522 * from technology type and other link parameters. It could be used to differentiate
523 * between very slow 1xRTT cellular links and other faster networks or even between
524 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
525 * fast backhauls and slow backhauls.
526 *
527 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700528 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700529 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700530 public void setLinkUpstreamBandwidthKbps(int upKbps) {
531 mLinkUpBandwidthKbps = upKbps;
532 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700533
534 /**
535 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
536 * the estimated first hop transport bandwidth.
537 *
538 * @return The estimated first hop upstream (device to network) bandwidth.
539 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700540 public int getLinkUpstreamBandwidthKbps() {
541 return mLinkUpBandwidthKbps;
542 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700543
544 /**
545 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
546 * the estimated first hop transport bandwidth.
547 * <p>
548 * Note that when used to request a network, this specifies the minimum acceptable.
549 * When received as the state of an existing network this specifies the typical
550 * first hop bandwidth expected. This is never measured, but rather is inferred
551 * from technology type and other link parameters. It could be used to differentiate
552 * between very slow 1xRTT cellular links and other faster networks or even between
553 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
554 * fast backhauls and slow backhauls.
555 *
556 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700557 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700558 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700559 public void setLinkDownstreamBandwidthKbps(int downKbps) {
560 mLinkDownBandwidthKbps = downKbps;
561 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700562
563 /**
564 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
565 * the estimated first hop transport bandwidth.
566 *
567 * @return The estimated first hop downstream (network to device) bandwidth.
568 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700569 public int getLinkDownstreamBandwidthKbps() {
570 return mLinkDownBandwidthKbps;
571 }
572
573 private void combineLinkBandwidths(NetworkCapabilities nc) {
574 this.mLinkUpBandwidthKbps =
575 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
576 this.mLinkDownBandwidthKbps =
577 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
578 }
579 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
580 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
581 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
582 }
583 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
584 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
585 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
586 }
587
Etan Cohena7434272017-04-03 12:17:51 -0700588 private NetworkSpecifier mNetworkSpecifier = null;
589
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700590 /**
591 * Sets the optional bearer specific network specifier.
592 * This has no meaning if a single transport is also not specified, so calling
593 * this without a single transport set will generate an exception, as will
594 * subsequently adding or removing transports after this is set.
595 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700596 *
Etan Cohena7434272017-04-03 12:17:51 -0700597 * @param networkSpecifier A concrete, parcelable framework class that extends
598 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900599 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700600 * @hide
601 */
Etan Cohena7434272017-04-03 12:17:51 -0700602 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
603 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700604 throw new IllegalStateException("Must have a single transport specified to use " +
605 "setNetworkSpecifier");
606 }
Etan Cohena7434272017-04-03 12:17:51 -0700607
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700608 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700609
Pierre Imaic8419a82016-03-22 17:54:54 +0900610 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700611 }
612
613 /**
614 * Gets the optional bearer specific network specifier.
615 *
Etan Cohena7434272017-04-03 12:17:51 -0700616 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
617 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700618 * @hide
619 */
Etan Cohena7434272017-04-03 12:17:51 -0700620 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700621 return mNetworkSpecifier;
622 }
623
624 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700625 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700626 throw new IllegalStateException("Can't combine two networkSpecifiers");
627 }
Etan Cohena7434272017-04-03 12:17:51 -0700628 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700629 }
Etan Cohena7434272017-04-03 12:17:51 -0700630
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700631 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700632 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
633 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700634 }
Etan Cohena7434272017-04-03 12:17:51 -0700635
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700636 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700637 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700638 }
639
Robert Greenwalt1448f052014-04-08 13:41:39 -0700640 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900641 * Magic value that indicates no signal strength provided. A request specifying this value is
642 * always satisfied.
643 *
644 * @hide
645 */
646 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
647
648 /**
649 * Signal strength. This is a signed integer, and higher values indicate better signal.
650 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
651 */
652 private int mSignalStrength;
653
654 /**
655 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
656 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
657 * reported by WifiManager.
658 * <p>
659 * Note that when used to register a network callback, this specifies the minimum acceptable
660 * signal strength. When received as the state of an existing network it specifies the current
661 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
662 * effect when requesting a callback.
663 *
664 * @param signalStrength the bearer-specific signal strength.
665 * @hide
666 */
667 public void setSignalStrength(int signalStrength) {
668 mSignalStrength = signalStrength;
669 }
670
671 /**
672 * Returns {@code true} if this object specifies a signal strength.
673 *
674 * @hide
675 */
676 public boolean hasSignalStrength() {
677 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
678 }
679
680 /**
681 * Retrieves the signal strength.
682 *
683 * @return The bearer-specific signal strength.
684 * @hide
685 */
686 public int getSignalStrength() {
687 return mSignalStrength;
688 }
689
690 private void combineSignalStrength(NetworkCapabilities nc) {
691 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
692 }
693
694 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
695 return this.mSignalStrength <= nc.mSignalStrength;
696 }
697
698 private boolean equalsSignalStrength(NetworkCapabilities nc) {
699 return this.mSignalStrength == nc.mSignalStrength;
700 }
701
702 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700703 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900704 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700705 */
706 public void combineCapabilities(NetworkCapabilities nc) {
707 combineNetCapabilities(nc);
708 combineTransportTypes(nc);
709 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700710 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900711 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700712 }
713
714 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900715 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
716 *
717 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
718 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
719 * bandwidth, signal strength, or validation / captive portal status.
720 *
721 * @hide
722 */
723 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
724 return (nc != null &&
725 satisfiedByNetCapabilities(nc, onlyImmutable) &&
726 satisfiedByTransportTypes(nc) &&
727 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900728 satisfiedBySpecifier(nc) &&
729 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900730 }
731
732 /**
733 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
734 *
735 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
736 *
737 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700738 */
739 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900740 return satisfiedByNetworkCapabilities(nc, false);
741 }
742
743 /**
744 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
745 *
746 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
747 *
748 * @hide
749 */
750 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
751 return satisfiedByNetworkCapabilities(nc, true);
752 }
753
754 /**
755 * Checks that our immutable capabilities are the same as those of the given
756 * {@code NetworkCapabilities}.
757 *
758 * @hide
759 */
760 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
761 if (nc == null) return false;
762 return (equalsNetCapabilitiesImmutable(nc) &&
763 equalsTransportTypes(nc) &&
764 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700765 }
766
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900767 /**
768 * Checks that our requestable capabilities are the same as those of the given
769 * {@code NetworkCapabilities}.
770 *
771 * @hide
772 */
773 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
774 if (nc == null) return false;
775 return (equalsNetCapabilitiesRequestable(nc) &&
776 equalsTransportTypes(nc) &&
777 equalsSpecifier(nc));
778 }
779
Robert Greenwalt1448f052014-04-08 13:41:39 -0700780 @Override
781 public boolean equals(Object obj) {
782 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
783 NetworkCapabilities that = (NetworkCapabilities)obj;
784 return (equalsNetCapabilities(that) &&
785 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700786 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900787 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700788 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700789 }
790
791 @Override
792 public int hashCode() {
793 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
794 ((int)(mNetworkCapabilities >> 32) * 3) +
795 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
796 ((int)(mTransportTypes >> 32) * 7) +
797 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700798 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700799 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900800 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700801 }
802
Wink Saville4e2dea72014-09-20 11:04:03 -0700803 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700804 public int describeContents() {
805 return 0;
806 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700807 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700808 public void writeToParcel(Parcel dest, int flags) {
809 dest.writeLong(mNetworkCapabilities);
810 dest.writeLong(mTransportTypes);
811 dest.writeInt(mLinkUpBandwidthKbps);
812 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700813 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900814 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700815 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900816
Robert Greenwalt1448f052014-04-08 13:41:39 -0700817 public static final Creator<NetworkCapabilities> CREATOR =
818 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700819 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700820 public NetworkCapabilities createFromParcel(Parcel in) {
821 NetworkCapabilities netCap = new NetworkCapabilities();
822
823 netCap.mNetworkCapabilities = in.readLong();
824 netCap.mTransportTypes = in.readLong();
825 netCap.mLinkUpBandwidthKbps = in.readInt();
826 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700827 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900828 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700829 return netCap;
830 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700831 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700832 public NetworkCapabilities[] newArray(int size) {
833 return new NetworkCapabilities[size];
834 }
835 };
836
Wink Saville4e2dea72014-09-20 11:04:03 -0700837 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700838 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700839 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +0900840 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700841
Robert Greenwalt7569f182014-06-08 16:42:59 -0700842 types = getCapabilities();
843 String capabilities = (types.length > 0 ? " Capabilities: " : "");
844 for (int i = 0; i < types.length; ) {
845 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700846 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
847 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
848 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
849 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
850 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
851 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
852 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
853 case NET_CAPABILITY_IA: capabilities += "IA"; break;
854 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
855 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
856 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
857 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
858 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
859 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700860 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400861 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900862 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400863 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900864 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700865 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700866 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700867 }
868
869 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
870 mLinkUpBandwidthKbps + "Kbps" : "");
871 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
872 mLinkDownBandwidthKbps + "Kbps" : "");
873
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700874 String specifier = (mNetworkSpecifier == null ?
875 "" : " Specifier: <" + mNetworkSpecifier + ">");
876
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900877 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
878
879 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700880 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900881
882 /**
883 * @hide
884 */
885 public static String transportNamesOf(int[] types) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900886 if (types == null || types.length == 0) {
887 return "";
Hugo Benichi5df9d722016-04-25 17:16:35 +0900888 }
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900889 StringBuilder transports = new StringBuilder();
890 for (int t : types) {
891 transports.append("|").append(transportNameOf(t));
892 }
893 return transports.substring(1);
894 }
895
896 /**
897 * @hide
898 */
899 public static String transportNameOf(int transport) {
900 if (transport < 0 || TRANSPORT_NAMES.length <= transport) {
901 return "UNKNOWN";
902 }
903 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +0900904 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700905}