blob: 76646b8939c45beae1f10b90561094b3520b2b29 [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;
Hugo Benichi16f0a942017-06-20 14:07:59 +090024import com.android.internal.util.Preconditions;
Etan Cohena7434272017-04-03 12:17:51 -070025
26import java.util.Objects;
Robert Greenwalt1448f052014-04-08 13:41:39 -070027
28/**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070029 * This class represents the capabilities of a network. This is used both to specify
30 * needs to {@link ConnectivityManager} and when inspecting a network.
31 *
32 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
33 * of network selection. Rather than indicate a need for Wi-Fi because an application
Wink Saville4e2dea72014-09-20 11:04:03 -070034 * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE),
Robert Greenwalt01d004e2014-05-18 15:24:21 -070035 * the application should specify it needs high bandwidth. Similarly if an application
36 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
37 * all cellular based connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070038 */
39public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070040 private static final String TAG = "NetworkCapabilities";
41
Robert Greenwalt7569f182014-06-08 16:42:59 -070042 /**
43 * @hide
44 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070045 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090046 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090047 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070048 }
49
50 public NetworkCapabilities(NetworkCapabilities nc) {
51 if (nc != null) {
52 mNetworkCapabilities = nc.mNetworkCapabilities;
53 mTransportTypes = nc.mTransportTypes;
54 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
55 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070056 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090057 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070058 }
59 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070060
61 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090062 * Completely clears the contents of this object, removing even the capabilities that are set
63 * by default when the object is constructed.
64 * @hide
65 */
66 public void clearAll() {
67 mNetworkCapabilities = mTransportTypes = 0;
68 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
69 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090070 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090071 }
72
73 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070074 * Represents the network's capabilities. If any are specified they will be satisfied
75 * by any Network that matches all of them.
76 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090077 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070078
79 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070080 * Indicates this is a network that has the ability to reach the
81 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070082 */
83 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070084
85 /**
86 * Indicates this is a network that has the ability to reach the carrier's
87 * SUPL server, used to retrieve GPS information.
88 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070089 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070090
91 /**
92 * Indicates this is a network that has the ability to reach the carrier's
93 * DUN or tethering gateway.
94 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070095 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070096
97 /**
98 * Indicates this is a network that has the ability to reach the carrier's
99 * FOTA portal, used for over the air updates.
100 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700101 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700102
103 /**
104 * Indicates this is a network that has the ability to reach the carrier's
105 * IMS servers, used for network registration and signaling.
106 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700107 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700108
109 /**
110 * Indicates this is a network that has the ability to reach the carrier's
111 * CBS servers, used for carrier specific services.
112 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700113 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700114
115 /**
116 * Indicates this is a network that has the ability to reach a Wi-Fi direct
117 * peer.
118 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700119 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700120
121 /**
122 * Indicates this is a network that has the ability to reach a carrier's
123 * Initial Attach servers.
124 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700125 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700126
127 /**
128 * Indicates this is a network that has the ability to reach a carrier's
129 * RCS servers, used for Rich Communication Services.
130 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700131 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700132
133 /**
134 * Indicates this is a network that has the ability to reach a carrier's
135 * XCAP servers, used for configuration and control.
136 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700137 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700138
139 /**
140 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700141 * Emergency IMS servers or other services, used for network signaling
142 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700143 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700144 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700145
146 /**
147 * Indicates that this network is unmetered.
148 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150
151 /**
152 * Indicates that this network should be able to reach the internet.
153 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700154 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700155
156 /**
157 * Indicates that this network is available for general use. If this is not set
158 * applications should not attempt to communicate on this network. Note that this
159 * is simply informative and not enforcement - enforcement is handled via other means.
160 * Set by default.
161 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700162 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
163
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700164 /**
165 * Indicates that the user has indicated implicit trust of this network. This
166 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
167 * BT device or a wifi the user asked to connect to. Untrusted networks
168 * are probably limited to unknown wifi AP. Set by default.
169 */
170 public static final int NET_CAPABILITY_TRUSTED = 14;
171
Paul Jensen76b610a2015-03-18 09:33:07 -0400172 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400173 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400174 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400175 */
176 public static final int NET_CAPABILITY_NOT_VPN = 15;
177
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900178 /**
179 * Indicates that connectivity on this network was successfully validated. For example, for a
180 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
181 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900182 */
183 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700184
Paul Jensen3d194ea2015-06-16 14:27:36 -0400185 /**
186 * Indicates that this network was found to have a captive portal in place last time it was
187 * probed.
188 */
189 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
190
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900191 /**
192 * Indicates that this network is available for use by apps, and not a network that is being
193 * kept up in the background to facilitate fast network switching.
194 * @hide
195 */
196 public static final int NET_CAPABILITY_FOREGROUND = 18;
197
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900199 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700200
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700201 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900202 * Network capabilities that are expected to be mutable, i.e., can change while a particular
203 * network is connected.
204 */
205 private static final long MUTABLE_CAPABILITIES =
206 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
207 // http://b/18206275
208 (1 << NET_CAPABILITY_TRUSTED) |
209 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900210 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
211 (1 << NET_CAPABILITY_FOREGROUND);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900212
213 /**
214 * Network capabilities that are not allowed in NetworkRequests. This exists because the
215 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
216 * capability's presence cannot be known in advance. If such a capability is requested, then we
217 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
218 * get immediately torn down because they do not have the requested capability.
219 */
220 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900221 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900222
223 /**
224 * Capabilities that are set by default when the object is constructed.
225 */
226 private static final long DEFAULT_CAPABILITIES =
227 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
228 (1 << NET_CAPABILITY_TRUSTED) |
229 (1 << NET_CAPABILITY_NOT_VPN);
230
231 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400232 * Capabilities that suggest that a network is restricted.
233 * {@see #maybeMarkCapabilitiesRestricted}.
234 */
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700235 @VisibleForTesting
236 /* package */ static final long RESTRICTED_CAPABILITIES =
Paul Jensen487ffe72015-07-24 15:57:11 -0400237 (1 << NET_CAPABILITY_CBS) |
238 (1 << NET_CAPABILITY_DUN) |
239 (1 << NET_CAPABILITY_EIMS) |
240 (1 << NET_CAPABILITY_FOTA) |
241 (1 << NET_CAPABILITY_IA) |
242 (1 << NET_CAPABILITY_IMS) |
243 (1 << NET_CAPABILITY_RCS) |
244 (1 << NET_CAPABILITY_XCAP);
245
246 /**
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700247 * Capabilities that suggest that a network is unrestricted.
248 * {@see #maybeMarkCapabilitiesRestricted}.
249 */
250 @VisibleForTesting
251 /* package */ static final long UNRESTRICTED_CAPABILITIES =
252 (1 << NET_CAPABILITY_INTERNET) |
253 (1 << NET_CAPABILITY_MMS) |
254 (1 << NET_CAPABILITY_SUPL) |
255 (1 << NET_CAPABILITY_WIFI_P2P);
256
257 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700258 * Adds the given capability to this {@code NetworkCapability} instance.
259 * Multiple capabilities may be applied sequentially. Note that when searching
260 * for a network to satisfy a request, all capabilities requested must be satisfied.
261 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700262 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900263 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700264 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700265 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700266 public NetworkCapabilities addCapability(int capability) {
267 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700268 throw new IllegalArgumentException("NetworkCapability out of range");
269 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700270 mNetworkCapabilities |= 1 << capability;
271 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700272 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700273
274 /**
275 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
276 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700277 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900278 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700279 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700280 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700281 public NetworkCapabilities removeCapability(int capability) {
282 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700283 throw new IllegalArgumentException("NetworkCapability out of range");
284 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700285 mNetworkCapabilities &= ~(1 << capability);
286 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700287 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700288
289 /**
290 * Gets all the capabilities set on this {@code NetworkCapability} instance.
291 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700292 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700293 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700294 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700295 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700296 public int[] getCapabilities() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900297 return BitUtils.unpackBits(mNetworkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700298 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700299
300 /**
301 * Tests for the presence of a capabilitity on this instance.
302 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700303 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700304 * @return {@code true} if set on this instance.
305 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700306 public boolean hasCapability(int capability) {
307 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700308 return false;
309 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700310 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700311 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700312
Robert Greenwalt1448f052014-04-08 13:41:39 -0700313 private void combineNetCapabilities(NetworkCapabilities nc) {
314 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
315 }
316
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900317 /**
318 * Convenience function that returns a human-readable description of the first mutable
319 * capability we find. Used to present an error message to apps that request mutable
320 * capabilities.
321 *
322 * @hide
323 */
324 public String describeFirstNonRequestableCapability() {
325 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
326 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900327 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900328 // This cannot happen unless the preceding checks are incomplete.
329 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
330 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
331 }
332 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900333 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900334 return null;
335 }
336
337 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
338 long networkCapabilities = this.mNetworkCapabilities;
339 if (onlyImmutable) {
340 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
341 }
342 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700343 }
344
Robert Greenwalt06314e42014-10-29 14:04:06 -0700345 /** @hide */
346 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700347 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
348 }
349
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900350 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
351 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
352 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
353 }
354
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900355 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
356 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
357 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
358 }
359
Robert Greenwalt1448f052014-04-08 13:41:39 -0700360 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400361 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
362 * typically provided by restricted networks.
363 *
364 * TODO: consider:
365 * - Renaming it to guessRestrictedCapability and make it set the
366 * restricted capability bit in addition to clearing it.
367 * @hide
368 */
369 public void maybeMarkCapabilitiesRestricted() {
Robert Greenwalta7e148a2017-04-10 14:32:23 -0700370 // Verify there aren't any unrestricted capabilities. If there are we say
371 // the whole thing is unrestricted.
372 final boolean hasUnrestrictedCapabilities =
373 ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0);
374
375 // Must have at least some restricted capabilities.
376 final boolean hasRestrictedCapabilities =
377 ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0);
378
379 if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400380 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400381 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400382 }
383
384 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700385 * Representing the transport type. Apps should generally not care about transport. A
386 * request for a fast internet connection could be satisfied by a number of different
387 * transports. If any are specified here it will be satisfied a Network that matches
388 * any of them. If a caller doesn't care about the transport it should not specify any.
389 */
390 private long mTransportTypes;
391
392 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700393 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700394 */
395 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700396
397 /**
398 * Indicates this network uses a Wi-Fi transport.
399 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700400 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700401
402 /**
403 * Indicates this network uses a Bluetooth transport.
404 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700405 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700406
407 /**
408 * Indicates this network uses an Ethernet transport.
409 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700410 public static final int TRANSPORT_ETHERNET = 3;
411
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400412 /**
413 * Indicates this network uses a VPN transport.
414 */
415 public static final int TRANSPORT_VPN = 4;
416
Etan Cohen305ea282016-06-20 09:27:12 -0700417 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700418 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700419 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700420 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700421
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700422 /**
423 * Indicates this network uses a LoWPAN transport.
424 * @hide
425 */
426 public static final int TRANSPORT_LOWPAN = 6;
427
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900428 /** @hide */
429 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
430 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700431 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700432
Hugo Benichi16f0a942017-06-20 14:07:59 +0900433 /** @hide */
434 public static boolean isValidTransport(int transportType) {
435 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
436 }
437
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900438 private static final String[] TRANSPORT_NAMES = {
439 "CELLULAR",
440 "WIFI",
441 "BLUETOOTH",
442 "ETHERNET",
443 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700444 "WIFI_AWARE",
445 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900446 };
447
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700448 /**
449 * Adds the given transport type to this {@code NetworkCapability} instance.
450 * Multiple transports may be applied sequentially. Note that when searching
451 * for a network to satisfy a request, any listed in the request will satisfy the request.
452 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
453 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
454 * to be selected. This is logically different than
455 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
456 *
457 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900458 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700459 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700460 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700461 public NetworkCapabilities addTransportType(int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900462 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700463 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700464 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700465 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700466 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700467
468 /**
469 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
470 *
471 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900472 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700473 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700474 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700475 public NetworkCapabilities removeTransportType(int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900476 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700477 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700478 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700479 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700481
482 /**
483 * Gets all the transports set on this {@code NetworkCapability} instance.
484 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700485 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700486 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700487 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700488 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700489 public int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900490 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700491 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700492
493 /**
494 * Tests for the presence of a transport on this instance.
495 *
496 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
497 * @return {@code true} if set on this instance.
498 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700499 public boolean hasTransport(int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900500 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700501 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700502
503 private void combineTransportTypes(NetworkCapabilities nc) {
504 this.mTransportTypes |= nc.mTransportTypes;
505 }
506 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
507 return ((this.mTransportTypes == 0) ||
508 ((this.mTransportTypes & nc.mTransportTypes) != 0));
509 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700510 /** @hide */
511 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700512 return (nc.mTransportTypes == this.mTransportTypes);
513 }
514
515 /**
516 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
517 * for the first hop on the given transport. It is not measured, but may take into account
518 * link parameters (Radio technology, allocated channels, etc).
519 */
520 private int mLinkUpBandwidthKbps;
521 private int mLinkDownBandwidthKbps;
522
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700523 /**
524 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
525 * the estimated first hop transport bandwidth.
526 * <p>
527 * Note that when used to request a network, this specifies the minimum acceptable.
528 * When received as the state of an existing network this specifies the typical
529 * first hop bandwidth expected. This is never measured, but rather is inferred
530 * from technology type and other link parameters. It could be used to differentiate
531 * between very slow 1xRTT cellular links and other faster networks or even between
532 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
533 * fast backhauls and slow backhauls.
534 *
535 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700536 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700537 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700538 public void setLinkUpstreamBandwidthKbps(int upKbps) {
539 mLinkUpBandwidthKbps = upKbps;
540 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700541
542 /**
543 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
544 * the estimated first hop transport bandwidth.
545 *
546 * @return The estimated first hop upstream (device to network) bandwidth.
547 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700548 public int getLinkUpstreamBandwidthKbps() {
549 return mLinkUpBandwidthKbps;
550 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700551
552 /**
553 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
554 * the estimated first hop transport bandwidth.
555 * <p>
556 * Note that when used to request a network, this specifies the minimum acceptable.
557 * When received as the state of an existing network this specifies the typical
558 * first hop bandwidth expected. This is never measured, but rather is inferred
559 * from technology type and other link parameters. It could be used to differentiate
560 * between very slow 1xRTT cellular links and other faster networks or even between
561 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
562 * fast backhauls and slow backhauls.
563 *
564 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700565 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700566 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700567 public void setLinkDownstreamBandwidthKbps(int downKbps) {
568 mLinkDownBandwidthKbps = downKbps;
569 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700570
571 /**
572 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
573 * the estimated first hop transport bandwidth.
574 *
575 * @return The estimated first hop downstream (network to device) bandwidth.
576 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 public int getLinkDownstreamBandwidthKbps() {
578 return mLinkDownBandwidthKbps;
579 }
580
581 private void combineLinkBandwidths(NetworkCapabilities nc) {
582 this.mLinkUpBandwidthKbps =
583 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
584 this.mLinkDownBandwidthKbps =
585 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
586 }
587 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
588 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
589 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
590 }
591 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
592 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
593 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
594 }
595
Etan Cohena7434272017-04-03 12:17:51 -0700596 private NetworkSpecifier mNetworkSpecifier = null;
597
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700598 /**
599 * Sets the optional bearer specific network specifier.
600 * This has no meaning if a single transport is also not specified, so calling
601 * this without a single transport set will generate an exception, as will
602 * subsequently adding or removing transports after this is set.
603 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700604 *
Etan Cohena7434272017-04-03 12:17:51 -0700605 * @param networkSpecifier A concrete, parcelable framework class that extends
606 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900607 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700608 * @hide
609 */
Etan Cohena7434272017-04-03 12:17:51 -0700610 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
611 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700612 throw new IllegalStateException("Must have a single transport specified to use " +
613 "setNetworkSpecifier");
614 }
Etan Cohena7434272017-04-03 12:17:51 -0700615
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700616 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700617
Pierre Imaic8419a82016-03-22 17:54:54 +0900618 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700619 }
620
621 /**
622 * Gets the optional bearer specific network specifier.
623 *
Etan Cohena7434272017-04-03 12:17:51 -0700624 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
625 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700626 * @hide
627 */
Etan Cohena7434272017-04-03 12:17:51 -0700628 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700629 return mNetworkSpecifier;
630 }
631
632 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700633 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700634 throw new IllegalStateException("Can't combine two networkSpecifiers");
635 }
Etan Cohena7434272017-04-03 12:17:51 -0700636 setNetworkSpecifier(nc.mNetworkSpecifier);
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 satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700640 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
641 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700642 }
Etan Cohena7434272017-04-03 12:17:51 -0700643
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700644 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700645 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700646 }
647
Robert Greenwalt1448f052014-04-08 13:41:39 -0700648 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900649 * Magic value that indicates no signal strength provided. A request specifying this value is
650 * always satisfied.
651 *
652 * @hide
653 */
654 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
655
656 /**
657 * Signal strength. This is a signed integer, and higher values indicate better signal.
658 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
659 */
660 private int mSignalStrength;
661
662 /**
663 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
664 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
665 * reported by WifiManager.
666 * <p>
667 * Note that when used to register a network callback, this specifies the minimum acceptable
668 * signal strength. When received as the state of an existing network it specifies the current
669 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
670 * effect when requesting a callback.
671 *
672 * @param signalStrength the bearer-specific signal strength.
673 * @hide
674 */
675 public void setSignalStrength(int signalStrength) {
676 mSignalStrength = signalStrength;
677 }
678
679 /**
680 * Returns {@code true} if this object specifies a signal strength.
681 *
682 * @hide
683 */
684 public boolean hasSignalStrength() {
685 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
686 }
687
688 /**
689 * Retrieves the signal strength.
690 *
691 * @return The bearer-specific signal strength.
692 * @hide
693 */
694 public int getSignalStrength() {
695 return mSignalStrength;
696 }
697
698 private void combineSignalStrength(NetworkCapabilities nc) {
699 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
700 }
701
702 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
703 return this.mSignalStrength <= nc.mSignalStrength;
704 }
705
706 private boolean equalsSignalStrength(NetworkCapabilities nc) {
707 return this.mSignalStrength == nc.mSignalStrength;
708 }
709
710 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700711 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900712 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700713 */
714 public void combineCapabilities(NetworkCapabilities nc) {
715 combineNetCapabilities(nc);
716 combineTransportTypes(nc);
717 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700718 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900719 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700720 }
721
722 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900723 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
724 *
725 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
726 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
727 * bandwidth, signal strength, or validation / captive portal status.
728 *
729 * @hide
730 */
731 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
732 return (nc != null &&
733 satisfiedByNetCapabilities(nc, onlyImmutable) &&
734 satisfiedByTransportTypes(nc) &&
735 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900736 satisfiedBySpecifier(nc) &&
737 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900738 }
739
740 /**
741 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
742 *
743 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
744 *
745 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700746 */
747 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900748 return satisfiedByNetworkCapabilities(nc, false);
749 }
750
751 /**
752 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
753 *
754 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
755 *
756 * @hide
757 */
758 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
759 return satisfiedByNetworkCapabilities(nc, true);
760 }
761
762 /**
763 * Checks that our immutable capabilities are the same as those of the given
764 * {@code NetworkCapabilities}.
765 *
766 * @hide
767 */
768 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
769 if (nc == null) return false;
770 return (equalsNetCapabilitiesImmutable(nc) &&
771 equalsTransportTypes(nc) &&
772 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700773 }
774
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900775 /**
776 * Checks that our requestable capabilities are the same as those of the given
777 * {@code NetworkCapabilities}.
778 *
779 * @hide
780 */
781 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
782 if (nc == null) return false;
783 return (equalsNetCapabilitiesRequestable(nc) &&
784 equalsTransportTypes(nc) &&
785 equalsSpecifier(nc));
786 }
787
Robert Greenwalt1448f052014-04-08 13:41:39 -0700788 @Override
789 public boolean equals(Object obj) {
790 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
791 NetworkCapabilities that = (NetworkCapabilities)obj;
792 return (equalsNetCapabilities(that) &&
793 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700794 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900795 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700796 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797 }
798
799 @Override
800 public int hashCode() {
801 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
802 ((int)(mNetworkCapabilities >> 32) * 3) +
803 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
804 ((int)(mTransportTypes >> 32) * 7) +
805 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700806 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700807 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900808 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700809 }
810
Wink Saville4e2dea72014-09-20 11:04:03 -0700811 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700812 public int describeContents() {
813 return 0;
814 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700815 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700816 public void writeToParcel(Parcel dest, int flags) {
817 dest.writeLong(mNetworkCapabilities);
818 dest.writeLong(mTransportTypes);
819 dest.writeInt(mLinkUpBandwidthKbps);
820 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700821 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900822 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700823 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900824
Robert Greenwalt1448f052014-04-08 13:41:39 -0700825 public static final Creator<NetworkCapabilities> CREATOR =
826 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700827 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700828 public NetworkCapabilities createFromParcel(Parcel in) {
829 NetworkCapabilities netCap = new NetworkCapabilities();
830
831 netCap.mNetworkCapabilities = in.readLong();
832 netCap.mTransportTypes = in.readLong();
833 netCap.mLinkUpBandwidthKbps = in.readInt();
834 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700835 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900836 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700837 return netCap;
838 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700839 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700840 public NetworkCapabilities[] newArray(int size) {
841 return new NetworkCapabilities[size];
842 }
843 };
844
Wink Saville4e2dea72014-09-20 11:04:03 -0700845 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700846 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700847 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +0900848 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700849
Robert Greenwalt7569f182014-06-08 16:42:59 -0700850 types = getCapabilities();
851 String capabilities = (types.length > 0 ? " Capabilities: " : "");
852 for (int i = 0; i < types.length; ) {
853 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700854 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
855 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
856 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
857 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
858 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
859 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
860 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
861 case NET_CAPABILITY_IA: capabilities += "IA"; break;
862 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
863 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
864 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
865 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
866 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
867 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700868 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400869 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900870 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400871 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900872 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700873 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700874 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700875 }
876
877 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
878 mLinkUpBandwidthKbps + "Kbps" : "");
879 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
880 mLinkDownBandwidthKbps + "Kbps" : "");
881
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700882 String specifier = (mNetworkSpecifier == null ?
883 "" : " Specifier: <" + mNetworkSpecifier + ">");
884
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900885 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
886
887 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700888 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900889
890 /**
891 * @hide
892 */
893 public static String transportNamesOf(int[] types) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900894 if (types == null || types.length == 0) {
895 return "";
Hugo Benichi5df9d722016-04-25 17:16:35 +0900896 }
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900897 StringBuilder transports = new StringBuilder();
898 for (int t : types) {
899 transports.append("|").append(transportNameOf(t));
900 }
901 return transports.substring(1);
902 }
903
904 /**
905 * @hide
906 */
907 public static String transportNameOf(int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900908 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900909 return "UNKNOWN";
910 }
911 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +0900912 }
Hugo Benichi16f0a942017-06-20 14:07:59 +0900913
914 private static void checkValidTransportType(int transport) {
915 Preconditions.checkArgument(
916 isValidTransport(transport), "Invalid TransportType " + transport);
917 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700918}