blob: 305cf7695e5e64282b62285d1e276e79ced75c80 [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.
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700424 */
425 public static final int TRANSPORT_LOWPAN = 6;
426
Hugo Benichi6a9bb8e2017-03-15 23:05:01 +0900427 /** @hide */
428 public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
429 /** @hide */
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700430 public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700431
Hugo Benichi16f0a942017-06-20 14:07:59 +0900432 /** @hide */
433 public static boolean isValidTransport(int transportType) {
434 return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT);
435 }
436
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900437 private static final String[] TRANSPORT_NAMES = {
438 "CELLULAR",
439 "WIFI",
440 "BLUETOOTH",
441 "ETHERNET",
442 "VPN",
Robert Quattlebaum5f915762017-05-15 15:53:29 -0700443 "WIFI_AWARE",
444 "LOWPAN"
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900445 };
446
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700447 /**
448 * Adds the given transport type to this {@code NetworkCapability} instance.
449 * Multiple transports may be applied sequentially. Note that when searching
450 * for a network to satisfy a request, any listed in the request will satisfy the request.
451 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
452 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
453 * to be selected. This is logically different than
454 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
455 *
456 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900457 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700458 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700459 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700460 public NetworkCapabilities addTransportType(int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900461 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700462 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700463 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700464 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700465 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700466
467 /**
468 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
469 *
470 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900471 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700472 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700473 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700474 public NetworkCapabilities removeTransportType(int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900475 checkValidTransportType(transportType);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700476 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700477 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700478 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700479 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700480
481 /**
482 * Gets all the transports set on this {@code NetworkCapability} instance.
483 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700484 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700485 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700486 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700487 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700488 public int[] getTransportTypes() {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900489 return BitUtils.unpackBits(mTransportTypes);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700491
492 /**
493 * Tests for the presence of a transport on this instance.
494 *
495 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
496 * @return {@code true} if set on this instance.
497 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700498 public boolean hasTransport(int transportType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900499 return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700500 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700501
502 private void combineTransportTypes(NetworkCapabilities nc) {
503 this.mTransportTypes |= nc.mTransportTypes;
504 }
505 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
506 return ((this.mTransportTypes == 0) ||
507 ((this.mTransportTypes & nc.mTransportTypes) != 0));
508 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700509 /** @hide */
510 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 return (nc.mTransportTypes == this.mTransportTypes);
512 }
513
514 /**
515 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
516 * for the first hop on the given transport. It is not measured, but may take into account
517 * link parameters (Radio technology, allocated channels, etc).
518 */
519 private int mLinkUpBandwidthKbps;
520 private int mLinkDownBandwidthKbps;
521
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700522 /**
523 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
524 * the estimated first hop transport bandwidth.
525 * <p>
526 * Note that when used to request a network, this specifies the minimum acceptable.
527 * When received as the state of an existing network this specifies the typical
528 * first hop bandwidth expected. This is never measured, but rather is inferred
529 * from technology type and other link parameters. It could be used to differentiate
530 * between very slow 1xRTT cellular links and other faster networks or even between
531 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
532 * fast backhauls and slow backhauls.
533 *
534 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700535 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700536 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700537 public void setLinkUpstreamBandwidthKbps(int upKbps) {
538 mLinkUpBandwidthKbps = upKbps;
539 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700540
541 /**
542 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
543 * the estimated first hop transport bandwidth.
544 *
545 * @return The estimated first hop upstream (device to network) bandwidth.
546 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700547 public int getLinkUpstreamBandwidthKbps() {
548 return mLinkUpBandwidthKbps;
549 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700550
551 /**
552 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
553 * the estimated first hop transport bandwidth.
554 * <p>
555 * Note that when used to request a network, this specifies the minimum acceptable.
556 * When received as the state of an existing network this specifies the typical
557 * first hop bandwidth expected. This is never measured, but rather is inferred
558 * from technology type and other link parameters. It could be used to differentiate
559 * between very slow 1xRTT cellular links and other faster networks or even between
560 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
561 * fast backhauls and slow backhauls.
562 *
563 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700564 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700565 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700566 public void setLinkDownstreamBandwidthKbps(int downKbps) {
567 mLinkDownBandwidthKbps = downKbps;
568 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700569
570 /**
571 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
572 * the estimated first hop transport bandwidth.
573 *
574 * @return The estimated first hop downstream (network to device) bandwidth.
575 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700576 public int getLinkDownstreamBandwidthKbps() {
577 return mLinkDownBandwidthKbps;
578 }
579
580 private void combineLinkBandwidths(NetworkCapabilities nc) {
581 this.mLinkUpBandwidthKbps =
582 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
583 this.mLinkDownBandwidthKbps =
584 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
585 }
586 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
587 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
588 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
589 }
590 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
591 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
592 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
593 }
594
Etan Cohena7434272017-04-03 12:17:51 -0700595 private NetworkSpecifier mNetworkSpecifier = null;
596
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700597 /**
598 * Sets the optional bearer specific network specifier.
599 * This has no meaning if a single transport is also not specified, so calling
600 * this without a single transport set will generate an exception, as will
601 * subsequently adding or removing transports after this is set.
602 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700603 *
Etan Cohena7434272017-04-03 12:17:51 -0700604 * @param networkSpecifier A concrete, parcelable framework class that extends
605 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900606 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700607 * @hide
608 */
Etan Cohena7434272017-04-03 12:17:51 -0700609 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
610 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700611 throw new IllegalStateException("Must have a single transport specified to use " +
612 "setNetworkSpecifier");
613 }
Etan Cohena7434272017-04-03 12:17:51 -0700614
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700615 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700616
Pierre Imaic8419a82016-03-22 17:54:54 +0900617 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700618 }
619
620 /**
621 * Gets the optional bearer specific network specifier.
622 *
Etan Cohena7434272017-04-03 12:17:51 -0700623 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
624 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700625 * @hide
626 */
Etan Cohena7434272017-04-03 12:17:51 -0700627 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700628 return mNetworkSpecifier;
629 }
630
631 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700632 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700633 throw new IllegalStateException("Can't combine two networkSpecifiers");
634 }
Etan Cohena7434272017-04-03 12:17:51 -0700635 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700636 }
Etan Cohena7434272017-04-03 12:17:51 -0700637
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700638 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700639 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
640 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700641 }
Etan Cohena7434272017-04-03 12:17:51 -0700642
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700643 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700644 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700645 }
646
Robert Greenwalt1448f052014-04-08 13:41:39 -0700647 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900648 * Magic value that indicates no signal strength provided. A request specifying this value is
649 * always satisfied.
650 *
651 * @hide
652 */
653 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
654
655 /**
656 * Signal strength. This is a signed integer, and higher values indicate better signal.
657 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
658 */
659 private int mSignalStrength;
660
661 /**
662 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
663 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
664 * reported by WifiManager.
665 * <p>
666 * Note that when used to register a network callback, this specifies the minimum acceptable
667 * signal strength. When received as the state of an existing network it specifies the current
668 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
669 * effect when requesting a callback.
670 *
671 * @param signalStrength the bearer-specific signal strength.
672 * @hide
673 */
674 public void setSignalStrength(int signalStrength) {
675 mSignalStrength = signalStrength;
676 }
677
678 /**
679 * Returns {@code true} if this object specifies a signal strength.
680 *
681 * @hide
682 */
683 public boolean hasSignalStrength() {
684 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
685 }
686
687 /**
688 * Retrieves the signal strength.
689 *
690 * @return The bearer-specific signal strength.
691 * @hide
692 */
693 public int getSignalStrength() {
694 return mSignalStrength;
695 }
696
697 private void combineSignalStrength(NetworkCapabilities nc) {
698 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
699 }
700
701 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
702 return this.mSignalStrength <= nc.mSignalStrength;
703 }
704
705 private boolean equalsSignalStrength(NetworkCapabilities nc) {
706 return this.mSignalStrength == nc.mSignalStrength;
707 }
708
709 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700710 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900711 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700712 */
713 public void combineCapabilities(NetworkCapabilities nc) {
714 combineNetCapabilities(nc);
715 combineTransportTypes(nc);
716 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700717 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900718 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700719 }
720
721 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900722 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
723 *
724 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
725 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
726 * bandwidth, signal strength, or validation / captive portal status.
727 *
728 * @hide
729 */
730 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
731 return (nc != null &&
732 satisfiedByNetCapabilities(nc, onlyImmutable) &&
733 satisfiedByTransportTypes(nc) &&
734 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900735 satisfiedBySpecifier(nc) &&
736 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900737 }
738
739 /**
740 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
741 *
742 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
743 *
744 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700745 */
746 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900747 return satisfiedByNetworkCapabilities(nc, false);
748 }
749
750 /**
751 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
752 *
753 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
754 *
755 * @hide
756 */
757 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
758 return satisfiedByNetworkCapabilities(nc, true);
759 }
760
761 /**
762 * Checks that our immutable capabilities are the same as those of the given
763 * {@code NetworkCapabilities}.
764 *
765 * @hide
766 */
767 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
768 if (nc == null) return false;
769 return (equalsNetCapabilitiesImmutable(nc) &&
770 equalsTransportTypes(nc) &&
771 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772 }
773
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900774 /**
775 * Checks that our requestable capabilities are the same as those of the given
776 * {@code NetworkCapabilities}.
777 *
778 * @hide
779 */
780 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
781 if (nc == null) return false;
782 return (equalsNetCapabilitiesRequestable(nc) &&
783 equalsTransportTypes(nc) &&
784 equalsSpecifier(nc));
785 }
786
Robert Greenwalt1448f052014-04-08 13:41:39 -0700787 @Override
788 public boolean equals(Object obj) {
789 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
790 NetworkCapabilities that = (NetworkCapabilities)obj;
791 return (equalsNetCapabilities(that) &&
792 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700793 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900794 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700795 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700796 }
797
798 @Override
799 public int hashCode() {
800 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
801 ((int)(mNetworkCapabilities >> 32) * 3) +
802 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
803 ((int)(mTransportTypes >> 32) * 7) +
804 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700805 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700806 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900807 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700808 }
809
Wink Saville4e2dea72014-09-20 11:04:03 -0700810 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700811 public int describeContents() {
812 return 0;
813 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700814 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700815 public void writeToParcel(Parcel dest, int flags) {
816 dest.writeLong(mNetworkCapabilities);
817 dest.writeLong(mTransportTypes);
818 dest.writeInt(mLinkUpBandwidthKbps);
819 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700820 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900821 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700822 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900823
Robert Greenwalt1448f052014-04-08 13:41:39 -0700824 public static final Creator<NetworkCapabilities> CREATOR =
825 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700826 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827 public NetworkCapabilities createFromParcel(Parcel in) {
828 NetworkCapabilities netCap = new NetworkCapabilities();
829
830 netCap.mNetworkCapabilities = in.readLong();
831 netCap.mTransportTypes = in.readLong();
832 netCap.mLinkUpBandwidthKbps = in.readInt();
833 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700834 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900835 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700836 return netCap;
837 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700838 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700839 public NetworkCapabilities[] newArray(int size) {
840 return new NetworkCapabilities[size];
841 }
842 };
843
Wink Saville4e2dea72014-09-20 11:04:03 -0700844 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700845 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700846 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +0900847 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700848
Robert Greenwalt7569f182014-06-08 16:42:59 -0700849 types = getCapabilities();
850 String capabilities = (types.length > 0 ? " Capabilities: " : "");
851 for (int i = 0; i < types.length; ) {
852 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700853 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
854 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
855 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
856 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
857 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
858 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
859 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
860 case NET_CAPABILITY_IA: capabilities += "IA"; break;
861 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
862 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
863 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
864 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
865 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
866 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700867 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400868 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900869 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400870 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900871 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700872 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700873 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700874 }
875
876 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
877 mLinkUpBandwidthKbps + "Kbps" : "");
878 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
879 mLinkDownBandwidthKbps + "Kbps" : "");
880
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700881 String specifier = (mNetworkSpecifier == null ?
882 "" : " Specifier: <" + mNetworkSpecifier + ">");
883
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900884 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
885
886 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700887 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900888
889 /**
890 * @hide
891 */
892 public static String transportNamesOf(int[] types) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900893 if (types == null || types.length == 0) {
894 return "";
Hugo Benichi5df9d722016-04-25 17:16:35 +0900895 }
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900896 StringBuilder transports = new StringBuilder();
897 for (int t : types) {
898 transports.append("|").append(transportNameOf(t));
899 }
900 return transports.substring(1);
901 }
902
903 /**
904 * @hide
905 */
906 public static String transportNameOf(int transport) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900907 if (!isValidTransport(transport)) {
Hugo Benichi9910dbc2017-03-22 18:29:58 +0900908 return "UNKNOWN";
909 }
910 return TRANSPORT_NAMES[transport];
Hugo Benichi5df9d722016-04-25 17:16:35 +0900911 }
Hugo Benichi16f0a942017-06-20 14:07:59 +0900912
913 private static void checkValidTransportType(int transport) {
914 Preconditions.checkArgument(
915 isValidTransport(transport), "Invalid TransportType " + transport);
916 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700917}