blob: 5edc7c64275c1820b35b2b6516e20734520dd7dd [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;
Etan Cohena7434272017-04-03 12:17:51 -070023
24import java.util.Objects;
Robert Greenwalt1448f052014-04-08 13:41:39 -070025
26/**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070027 * This class represents the capabilities of a network. This is used both to specify
28 * needs to {@link ConnectivityManager} and when inspecting a network.
29 *
30 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
31 * of network selection. Rather than indicate a need for Wi-Fi because an application
Wink Saville4e2dea72014-09-20 11:04:03 -070032 * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE),
Robert Greenwalt01d004e2014-05-18 15:24:21 -070033 * the application should specify it needs high bandwidth. Similarly if an application
34 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
35 * all cellular based connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070036 */
37public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070038 private static final String TAG = "NetworkCapabilities";
39
Robert Greenwalt7569f182014-06-08 16:42:59 -070040 /**
41 * @hide
42 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070043 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090044 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090045 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070046 }
47
48 public NetworkCapabilities(NetworkCapabilities nc) {
49 if (nc != null) {
50 mNetworkCapabilities = nc.mNetworkCapabilities;
51 mTransportTypes = nc.mTransportTypes;
52 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
53 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070054 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090055 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070056 }
57 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070058
59 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090060 * Completely clears the contents of this object, removing even the capabilities that are set
61 * by default when the object is constructed.
62 * @hide
63 */
64 public void clearAll() {
65 mNetworkCapabilities = mTransportTypes = 0;
66 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
67 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090068 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090069 }
70
71 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070072 * Represents the network's capabilities. If any are specified they will be satisfied
73 * by any Network that matches all of them.
74 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090075 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070076
77 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070078 * Indicates this is a network that has the ability to reach the
79 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070080 */
81 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070082
83 /**
84 * Indicates this is a network that has the ability to reach the carrier's
85 * SUPL server, used to retrieve GPS information.
86 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070087 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070088
89 /**
90 * Indicates this is a network that has the ability to reach the carrier's
91 * DUN or tethering gateway.
92 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070093 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070094
95 /**
96 * Indicates this is a network that has the ability to reach the carrier's
97 * FOTA portal, used for over the air updates.
98 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070099 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700100
101 /**
102 * Indicates this is a network that has the ability to reach the carrier's
103 * IMS servers, used for network registration and signaling.
104 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700105 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700106
107 /**
108 * Indicates this is a network that has the ability to reach the carrier's
109 * CBS servers, used for carrier specific services.
110 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700111 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700112
113 /**
114 * Indicates this is a network that has the ability to reach a Wi-Fi direct
115 * peer.
116 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700117 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700118
119 /**
120 * Indicates this is a network that has the ability to reach a carrier's
121 * Initial Attach servers.
122 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700123 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700124
125 /**
126 * Indicates this is a network that has the ability to reach a carrier's
127 * RCS servers, used for Rich Communication Services.
128 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700129 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700130
131 /**
132 * Indicates this is a network that has the ability to reach a carrier's
133 * XCAP servers, used for configuration and control.
134 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700135 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700136
137 /**
138 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700139 * Emergency IMS servers or other services, used for network signaling
140 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700141 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700142 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700143
144 /**
145 * Indicates that this network is unmetered.
146 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700147 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700148
149 /**
150 * Indicates that this network should be able to reach the internet.
151 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700152 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700153
154 /**
155 * Indicates that this network is available for general use. If this is not set
156 * applications should not attempt to communicate on this network. Note that this
157 * is simply informative and not enforcement - enforcement is handled via other means.
158 * Set by default.
159 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700160 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
161
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700162 /**
163 * Indicates that the user has indicated implicit trust of this network. This
164 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
165 * BT device or a wifi the user asked to connect to. Untrusted networks
166 * are probably limited to unknown wifi AP. Set by default.
167 */
168 public static final int NET_CAPABILITY_TRUSTED = 14;
169
Paul Jensen76b610a2015-03-18 09:33:07 -0400170 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400171 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400172 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400173 */
174 public static final int NET_CAPABILITY_NOT_VPN = 15;
175
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900176 /**
177 * Indicates that connectivity on this network was successfully validated. For example, for a
178 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
179 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900180 */
181 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700182
Paul Jensen3d194ea2015-06-16 14:27:36 -0400183 /**
184 * Indicates that this network was found to have a captive portal in place last time it was
185 * probed.
186 */
187 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
188
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900189 /**
190 * Indicates that this network is available for use by apps, and not a network that is being
191 * kept up in the background to facilitate fast network switching.
192 * @hide
193 */
194 public static final int NET_CAPABILITY_FOREGROUND = 18;
195
Robert Greenwalt1448f052014-04-08 13:41:39 -0700196 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900197 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900200 * Network capabilities that are expected to be mutable, i.e., can change while a particular
201 * network is connected.
202 */
203 private static final long MUTABLE_CAPABILITIES =
204 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
205 // http://b/18206275
206 (1 << NET_CAPABILITY_TRUSTED) |
207 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900208 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
209 (1 << NET_CAPABILITY_FOREGROUND);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900210
211 /**
212 * Network capabilities that are not allowed in NetworkRequests. This exists because the
213 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
214 * capability's presence cannot be known in advance. If such a capability is requested, then we
215 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
216 * get immediately torn down because they do not have the requested capability.
217 */
218 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900219 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900220
221 /**
222 * Capabilities that are set by default when the object is constructed.
223 */
224 private static final long DEFAULT_CAPABILITIES =
225 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
226 (1 << NET_CAPABILITY_TRUSTED) |
227 (1 << NET_CAPABILITY_NOT_VPN);
228
229 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400230 * Capabilities that suggest that a network is restricted.
231 * {@see #maybeMarkCapabilitiesRestricted}.
232 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700233 @VisibleForTesting
234 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400235 (1 << NET_CAPABILITY_CBS) |
236 (1 << NET_CAPABILITY_DUN) |
237 (1 << NET_CAPABILITY_EIMS) |
238 (1 << NET_CAPABILITY_FOTA) |
239 (1 << NET_CAPABILITY_IA) |
240 (1 << NET_CAPABILITY_IMS) |
241 (1 << NET_CAPABILITY_RCS) |
242 (1 << NET_CAPABILITY_XCAP);
243
244 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700245 * Capabilities that suggest that a network is unrestricted.
246 * {@see #maybeMarkCapabilitiesRestricted}.
247 */
248 @VisibleForTesting
249 /* package */ static final long UNRESTRICTED_CAPABILITIES =
250 (1 << NET_CAPABILITY_INTERNET) |
251 (1 << NET_CAPABILITY_MMS) |
252 (1 << NET_CAPABILITY_SUPL) |
253 (1 << NET_CAPABILITY_WIFI_P2P);
254
255 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700256 * Adds the given capability to this {@code NetworkCapability} instance.
257 * Multiple capabilities may be applied sequentially. Note that when searching
258 * for a network to satisfy a request, all capabilities requested must be satisfied.
259 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700260 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900261 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700262 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700263 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700264 public NetworkCapabilities addCapability(int capability) {
265 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700266 throw new IllegalArgumentException("NetworkCapability out of range");
267 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700268 mNetworkCapabilities |= 1 << capability;
269 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700270 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700271
272 /**
273 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
274 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700275 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900276 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700277 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700278 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700279 public NetworkCapabilities removeCapability(int capability) {
280 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700281 throw new IllegalArgumentException("NetworkCapability out of range");
282 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700283 mNetworkCapabilities &= ~(1 << capability);
284 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700285 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700286
287 /**
288 * Gets all the capabilities set on this {@code NetworkCapability} instance.
289 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700290 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700291 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700292 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700293 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700294 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700295 return enumerateBits(mNetworkCapabilities);
296 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700297
298 /**
299 * Tests for the presence of a capabilitity on this instance.
300 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700301 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700302 * @return {@code true} if set on this instance.
303 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700304 public boolean hasCapability(int capability) {
305 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700306 return false;
307 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700308 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700309 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700310
Robert Greenwalt7569f182014-06-08 16:42:59 -0700311 private int[] enumerateBits(long val) {
312 int size = Long.bitCount(val);
313 int[] result = new int[size];
314 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700315 int resource = 0;
316 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700317 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700318 val = val >> 1;
319 resource++;
320 }
321 return result;
322 }
323
324 private void combineNetCapabilities(NetworkCapabilities nc) {
325 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
326 }
327
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900328 /**
329 * Convenience function that returns a human-readable description of the first mutable
330 * capability we find. Used to present an error message to apps that request mutable
331 * capabilities.
332 *
333 * @hide
334 */
335 public String describeFirstNonRequestableCapability() {
336 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
337 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900338 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900339 // This cannot happen unless the preceding checks are incomplete.
340 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
341 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
342 }
343 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900344 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900345 return null;
346 }
347
348 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
349 long networkCapabilities = this.mNetworkCapabilities;
350 if (onlyImmutable) {
351 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
352 }
353 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700354 }
355
Robert Greenwalt06314e42014-10-29 14:04:06 -0700356 /** @hide */
357 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700358 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
359 }
360
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900361 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
362 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
363 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
364 }
365
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900366 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
367 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
368 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
369 }
370
Robert Greenwalt1448f052014-04-08 13:41:39 -0700371 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400372 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
373 * typically provided by restricted networks.
374 *
375 * TODO: consider:
376 * - Renaming it to guessRestrictedCapability and make it set the
377 * restricted capability bit in addition to clearing it.
378 * @hide
379 */
380 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700381 // Verify there aren't any unrestricted capabilities. If there are we say
382 // the whole thing is unrestricted.
383 final boolean hasUnrestrictedCapabilities =
384 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
385
386 // Must have at least some restricted capabilities.
387 final boolean hasRestrictedCapabilities =
388 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
389
390 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400391 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400392 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400393 }
394
395 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700396 * Representing the transport type. Apps should generally not care about transport. A
397 * request for a fast internet connection could be satisfied by a number of different
398 * transports. If any are specified here it will be satisfied a Network that matches
399 * any of them. If a caller doesn't care about the transport it should not specify any.
400 */
401 private long mTransportTypes;
402
403 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700404 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700405 */
406 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700407
408 /**
409 * Indicates this network uses a Wi-Fi transport.
410 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700411 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700412
413 /**
414 * Indicates this network uses a Bluetooth transport.
415 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700416 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700417
418 /**
419 * Indicates this network uses an Ethernet transport.
420 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700421 public static final int TRANSPORT_ETHERNET = 3;
422
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400423 /**
424 * Indicates this network uses a VPN transport.
425 */
426 public static final int TRANSPORT_VPN = 4;
427
Etan Cohen305ea282016-06-20 09:27:12 -0700428 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700429 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700430 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700431 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700432
Robert Greenwalt1448f052014-04-08 13:41:39 -0700433 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Etan Cohen0849ded2016-10-26 11:22:06 -0700434 private static final int MAX_TRANSPORT = TRANSPORT_WIFI_AWARE;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700435
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700436 /**
437 * Adds the given transport type to this {@code NetworkCapability} instance.
438 * Multiple transports may be applied sequentially. Note that when searching
439 * for a network to satisfy a request, any listed in the request will satisfy the request.
440 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
441 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
442 * to be selected. This is logically different than
443 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
444 *
445 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900446 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700447 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700448 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700449 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700450 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
451 throw new IllegalArgumentException("TransportType out of range");
452 }
453 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700454 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700455 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700456 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700457
458 /**
459 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
460 *
461 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900462 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700463 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700464 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700465 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700466 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
467 throw new IllegalArgumentException("TransportType out of range");
468 }
469 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700470 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700471 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700472 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473
474 /**
475 * Gets all the transports set on this {@code NetworkCapability} instance.
476 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700477 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700478 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700479 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700480 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700481 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700482 return enumerateBits(mTransportTypes);
483 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700484
485 /**
486 * Tests for the presence of a transport on this instance.
487 *
488 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
489 * @return {@code true} if set on this instance.
490 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700491 public boolean hasTransport(int transportType) {
492 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
493 return false;
494 }
495 return ((mTransportTypes & (1 << transportType)) != 0);
496 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700497
498 private void combineTransportTypes(NetworkCapabilities nc) {
499 this.mTransportTypes |= nc.mTransportTypes;
500 }
501 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
502 return ((this.mTransportTypes == 0) ||
503 ((this.mTransportTypes & nc.mTransportTypes) != 0));
504 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700505 /** @hide */
506 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700507 return (nc.mTransportTypes == this.mTransportTypes);
508 }
509
510 /**
511 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
512 * for the first hop on the given transport. It is not measured, but may take into account
513 * link parameters (Radio technology, allocated channels, etc).
514 */
515 private int mLinkUpBandwidthKbps;
516 private int mLinkDownBandwidthKbps;
517
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700518 /**
519 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
520 * the estimated first hop transport bandwidth.
521 * <p>
522 * Note that when used to request a network, this specifies the minimum acceptable.
523 * When received as the state of an existing network this specifies the typical
524 * first hop bandwidth expected. This is never measured, but rather is inferred
525 * from technology type and other link parameters. It could be used to differentiate
526 * between very slow 1xRTT cellular links and other faster networks or even between
527 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
528 * fast backhauls and slow backhauls.
529 *
530 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700531 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700532 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700533 public void setLinkUpstreamBandwidthKbps(int upKbps) {
534 mLinkUpBandwidthKbps = upKbps;
535 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700536
537 /**
538 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
539 * the estimated first hop transport bandwidth.
540 *
541 * @return The estimated first hop upstream (device to network) bandwidth.
542 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700543 public int getLinkUpstreamBandwidthKbps() {
544 return mLinkUpBandwidthKbps;
545 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700546
547 /**
548 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
549 * the estimated first hop transport bandwidth.
550 * <p>
551 * Note that when used to request a network, this specifies the minimum acceptable.
552 * When received as the state of an existing network this specifies the typical
553 * first hop bandwidth expected. This is never measured, but rather is inferred
554 * from technology type and other link parameters. It could be used to differentiate
555 * between very slow 1xRTT cellular links and other faster networks or even between
556 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
557 * fast backhauls and slow backhauls.
558 *
559 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700560 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700561 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700562 public void setLinkDownstreamBandwidthKbps(int downKbps) {
563 mLinkDownBandwidthKbps = downKbps;
564 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700565
566 /**
567 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
568 * the estimated first hop transport bandwidth.
569 *
570 * @return The estimated first hop downstream (network to device) bandwidth.
571 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700572 public int getLinkDownstreamBandwidthKbps() {
573 return mLinkDownBandwidthKbps;
574 }
575
576 private void combineLinkBandwidths(NetworkCapabilities nc) {
577 this.mLinkUpBandwidthKbps =
578 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
579 this.mLinkDownBandwidthKbps =
580 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
581 }
582 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
583 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
584 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
585 }
586 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
587 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
588 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
589 }
590
Etan Cohena7434272017-04-03 12:17:51 -0700591 private NetworkSpecifier mNetworkSpecifier = null;
592
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700593 /**
594 * Sets the optional bearer specific network specifier.
595 * This has no meaning if a single transport is also not specified, so calling
596 * this without a single transport set will generate an exception, as will
597 * subsequently adding or removing transports after this is set.
598 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700599 *
Etan Cohena7434272017-04-03 12:17:51 -0700600 * @param networkSpecifier A concrete, parcelable framework class that extends
601 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900602 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700603 * @hide
604 */
Etan Cohena7434272017-04-03 12:17:51 -0700605 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
606 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700607 throw new IllegalStateException("Must have a single transport specified to use " +
608 "setNetworkSpecifier");
609 }
Etan Cohena7434272017-04-03 12:17:51 -0700610
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700611 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700612
Pierre Imaic8419a82016-03-22 17:54:54 +0900613 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700614 }
615
616 /**
617 * Gets the optional bearer specific network specifier.
618 *
Etan Cohena7434272017-04-03 12:17:51 -0700619 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
620 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700621 * @hide
622 */
Etan Cohena7434272017-04-03 12:17:51 -0700623 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700624 return mNetworkSpecifier;
625 }
626
627 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700628 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700629 throw new IllegalStateException("Can't combine two networkSpecifiers");
630 }
Etan Cohena7434272017-04-03 12:17:51 -0700631 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700632 }
Etan Cohena7434272017-04-03 12:17:51 -0700633
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700634 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700635 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
636 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700637 }
Etan Cohena7434272017-04-03 12:17:51 -0700638
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700639 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700640 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700641 }
642
Robert Greenwalt1448f052014-04-08 13:41:39 -0700643 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900644 * Magic value that indicates no signal strength provided. A request specifying this value is
645 * always satisfied.
646 *
647 * @hide
648 */
649 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
650
651 /**
652 * Signal strength. This is a signed integer, and higher values indicate better signal.
653 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
654 */
655 private int mSignalStrength;
656
657 /**
658 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
659 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
660 * reported by WifiManager.
661 * <p>
662 * Note that when used to register a network callback, this specifies the minimum acceptable
663 * signal strength. When received as the state of an existing network it specifies the current
664 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
665 * effect when requesting a callback.
666 *
667 * @param signalStrength the bearer-specific signal strength.
668 * @hide
669 */
670 public void setSignalStrength(int signalStrength) {
671 mSignalStrength = signalStrength;
672 }
673
674 /**
675 * Returns {@code true} if this object specifies a signal strength.
676 *
677 * @hide
678 */
679 public boolean hasSignalStrength() {
680 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
681 }
682
683 /**
684 * Retrieves the signal strength.
685 *
686 * @return The bearer-specific signal strength.
687 * @hide
688 */
689 public int getSignalStrength() {
690 return mSignalStrength;
691 }
692
693 private void combineSignalStrength(NetworkCapabilities nc) {
694 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
695 }
696
697 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
698 return this.mSignalStrength <= nc.mSignalStrength;
699 }
700
701 private boolean equalsSignalStrength(NetworkCapabilities nc) {
702 return this.mSignalStrength == nc.mSignalStrength;
703 }
704
705 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700706 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900707 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700708 */
709 public void combineCapabilities(NetworkCapabilities nc) {
710 combineNetCapabilities(nc);
711 combineTransportTypes(nc);
712 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700713 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900714 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700715 }
716
717 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900718 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
719 *
720 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
721 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
722 * bandwidth, signal strength, or validation / captive portal status.
723 *
724 * @hide
725 */
726 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
727 return (nc != null &&
728 satisfiedByNetCapabilities(nc, onlyImmutable) &&
729 satisfiedByTransportTypes(nc) &&
730 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900731 satisfiedBySpecifier(nc) &&
732 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900733 }
734
735 /**
736 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
737 *
738 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
739 *
740 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700741 */
742 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900743 return satisfiedByNetworkCapabilities(nc, false);
744 }
745
746 /**
747 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
748 *
749 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
750 *
751 * @hide
752 */
753 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
754 return satisfiedByNetworkCapabilities(nc, true);
755 }
756
757 /**
758 * Checks that our immutable capabilities are the same as those of the given
759 * {@code NetworkCapabilities}.
760 *
761 * @hide
762 */
763 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
764 if (nc == null) return false;
765 return (equalsNetCapabilitiesImmutable(nc) &&
766 equalsTransportTypes(nc) &&
767 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700768 }
769
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900770 /**
771 * Checks that our requestable capabilities are the same as those of the given
772 * {@code NetworkCapabilities}.
773 *
774 * @hide
775 */
776 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
777 if (nc == null) return false;
778 return (equalsNetCapabilitiesRequestable(nc) &&
779 equalsTransportTypes(nc) &&
780 equalsSpecifier(nc));
781 }
782
Robert Greenwalt1448f052014-04-08 13:41:39 -0700783 @Override
784 public boolean equals(Object obj) {
785 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
786 NetworkCapabilities that = (NetworkCapabilities)obj;
787 return (equalsNetCapabilities(that) &&
788 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700789 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900790 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700791 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700792 }
793
794 @Override
795 public int hashCode() {
796 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
797 ((int)(mNetworkCapabilities >> 32) * 3) +
798 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
799 ((int)(mTransportTypes >> 32) * 7) +
800 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700801 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700802 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900803 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700804 }
805
Wink Saville4e2dea72014-09-20 11:04:03 -0700806 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700807 public int describeContents() {
808 return 0;
809 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700810 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700811 public void writeToParcel(Parcel dest, int flags) {
812 dest.writeLong(mNetworkCapabilities);
813 dest.writeLong(mTransportTypes);
814 dest.writeInt(mLinkUpBandwidthKbps);
815 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700816 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900817 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700818 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900819
Robert Greenwalt1448f052014-04-08 13:41:39 -0700820 public static final Creator<NetworkCapabilities> CREATOR =
821 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700822 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700823 public NetworkCapabilities createFromParcel(Parcel in) {
824 NetworkCapabilities netCap = new NetworkCapabilities();
825
826 netCap.mNetworkCapabilities = in.readLong();
827 netCap.mTransportTypes = in.readLong();
828 netCap.mLinkUpBandwidthKbps = in.readInt();
829 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700830 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900831 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700832 return netCap;
833 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700834 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700835 public NetworkCapabilities[] newArray(int size) {
836 return new NetworkCapabilities[size];
837 }
838 };
839
Wink Saville4e2dea72014-09-20 11:04:03 -0700840 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700841 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700842 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +0900843 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700844
Robert Greenwalt7569f182014-06-08 16:42:59 -0700845 types = getCapabilities();
846 String capabilities = (types.length > 0 ? " Capabilities: " : "");
847 for (int i = 0; i < types.length; ) {
848 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700849 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
850 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
851 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
852 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
853 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
854 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
855 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
856 case NET_CAPABILITY_IA: capabilities += "IA"; break;
857 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
858 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
859 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
860 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
861 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
862 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700863 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400864 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900865 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400866 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900867 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700868 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700869 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700870 }
871
872 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
873 mLinkUpBandwidthKbps + "Kbps" : "");
874 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
875 mLinkDownBandwidthKbps + "Kbps" : "");
876
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700877 String specifier = (mNetworkSpecifier == null ?
878 "" : " Specifier: <" + mNetworkSpecifier + ">");
879
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900880 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
881
882 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700883 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900884
885 /**
886 * @hide
887 */
888 public static String transportNamesOf(int[] types) {
889 String transports = "";
890 for (int i = 0; i < types.length;) {
891 switch (types[i]) {
892 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
893 case TRANSPORT_WIFI: transports += "WIFI"; break;
894 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
895 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
896 case TRANSPORT_VPN: transports += "VPN"; break;
Etan Cohen0849ded2016-10-26 11:22:06 -0700897 case TRANSPORT_WIFI_AWARE: transports += "WIFI_AWARE"; break;
Hugo Benichi5df9d722016-04-25 17:16:35 +0900898 }
899 if (++i < types.length) transports += "|";
900 }
901 return transports;
902 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700903}