blob: c28294f9fe9d1f2ec24e2caf7b08b8bda68c1aea [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;
Etan Cohena7434272017-04-03 12:17:51 -070021import android.util.Log;
22
23import java.util.Objects;
Robert Greenwalt1448f052014-04-08 13:41:39 -070024
25/**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070026 * This class represents the capabilities of a network. This is used both to specify
27 * needs to {@link ConnectivityManager} and when inspecting a network.
28 *
29 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
30 * of network selection. Rather than indicate a need for Wi-Fi because an application
Wink Saville4e2dea72014-09-20 11:04:03 -070031 * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE),
Robert Greenwalt01d004e2014-05-18 15:24:21 -070032 * the application should specify it needs high bandwidth. Similarly if an application
33 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
34 * all cellular based connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070035 */
36public final class NetworkCapabilities implements Parcelable {
Etan Cohena7434272017-04-03 12:17:51 -070037 private static final String TAG = "NetworkCapabilities";
38
Robert Greenwalt7569f182014-06-08 16:42:59 -070039 /**
40 * @hide
41 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070042 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090043 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090044 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070045 }
46
47 public NetworkCapabilities(NetworkCapabilities nc) {
48 if (nc != null) {
49 mNetworkCapabilities = nc.mNetworkCapabilities;
50 mTransportTypes = nc.mTransportTypes;
51 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
52 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070053 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090054 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070055 }
56 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070057
58 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090059 * Completely clears the contents of this object, removing even the capabilities that are set
60 * by default when the object is constructed.
61 * @hide
62 */
63 public void clearAll() {
64 mNetworkCapabilities = mTransportTypes = 0;
65 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
66 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090067 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090068 }
69
70 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070071 * Represents the network's capabilities. If any are specified they will be satisfied
72 * by any Network that matches all of them.
73 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090074 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070075
76 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070077 * Indicates this is a network that has the ability to reach the
78 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070079 */
80 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070081
82 /**
83 * Indicates this is a network that has the ability to reach the carrier's
84 * SUPL server, used to retrieve GPS information.
85 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070086 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070087
88 /**
89 * Indicates this is a network that has the ability to reach the carrier's
90 * DUN or tethering gateway.
91 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070092 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070093
94 /**
95 * Indicates this is a network that has the ability to reach the carrier's
96 * FOTA portal, used for over the air updates.
97 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070098 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070099
100 /**
101 * Indicates this is a network that has the ability to reach the carrier's
102 * IMS servers, used for network registration and signaling.
103 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700104 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700105
106 /**
107 * Indicates this is a network that has the ability to reach the carrier's
108 * CBS servers, used for carrier specific services.
109 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700110 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700111
112 /**
113 * Indicates this is a network that has the ability to reach a Wi-Fi direct
114 * peer.
115 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700116 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700117
118 /**
119 * Indicates this is a network that has the ability to reach a carrier's
120 * Initial Attach servers.
121 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700122 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700123
124 /**
125 * Indicates this is a network that has the ability to reach a carrier's
126 * RCS servers, used for Rich Communication Services.
127 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700128 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700129
130 /**
131 * Indicates this is a network that has the ability to reach a carrier's
132 * XCAP servers, used for configuration and control.
133 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700134 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700135
136 /**
137 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700138 * Emergency IMS servers or other services, used for network signaling
139 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700140 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700141 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700142
143 /**
144 * Indicates that this network is unmetered.
145 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700146 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700147
148 /**
149 * Indicates that this network should be able to reach the internet.
150 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700151 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700152
153 /**
154 * Indicates that this network is available for general use. If this is not set
155 * applications should not attempt to communicate on this network. Note that this
156 * is simply informative and not enforcement - enforcement is handled via other means.
157 * Set by default.
158 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700159 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
160
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700161 /**
162 * Indicates that the user has indicated implicit trust of this network. This
163 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
164 * BT device or a wifi the user asked to connect to. Untrusted networks
165 * are probably limited to unknown wifi AP. Set by default.
166 */
167 public static final int NET_CAPABILITY_TRUSTED = 14;
168
Paul Jensen76b610a2015-03-18 09:33:07 -0400169 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400170 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400171 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400172 */
173 public static final int NET_CAPABILITY_NOT_VPN = 15;
174
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900175 /**
176 * Indicates that connectivity on this network was successfully validated. For example, for a
177 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
178 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900179 */
180 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700181
Paul Jensen3d194ea2015-06-16 14:27:36 -0400182 /**
183 * Indicates that this network was found to have a captive portal in place last time it was
184 * probed.
185 */
186 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
187
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900188 /**
189 * Indicates that this network is available for use by apps, and not a network that is being
190 * kept up in the background to facilitate fast network switching.
191 * @hide
192 */
193 public static final int NET_CAPABILITY_FOREGROUND = 18;
194
Robert Greenwalt1448f052014-04-08 13:41:39 -0700195 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900196 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900199 * Network capabilities that are expected to be mutable, i.e., can change while a particular
200 * network is connected.
201 */
202 private static final long MUTABLE_CAPABILITIES =
203 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
204 // http://b/18206275
205 (1 << NET_CAPABILITY_TRUSTED) |
206 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900207 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
208 (1 << NET_CAPABILITY_FOREGROUND);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900209
210 /**
211 * Network capabilities that are not allowed in NetworkRequests. This exists because the
212 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
213 * capability's presence cannot be known in advance. If such a capability is requested, then we
214 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
215 * get immediately torn down because they do not have the requested capability.
216 */
217 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900218 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900219
220 /**
221 * Capabilities that are set by default when the object is constructed.
222 */
223 private static final long DEFAULT_CAPABILITIES =
224 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
225 (1 << NET_CAPABILITY_TRUSTED) |
226 (1 << NET_CAPABILITY_NOT_VPN);
227
228 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400229 * Capabilities that suggest that a network is restricted.
230 * {@see #maybeMarkCapabilitiesRestricted}.
231 */
232 private static final long RESTRICTED_CAPABILITIES =
233 (1 << NET_CAPABILITY_CBS) |
234 (1 << NET_CAPABILITY_DUN) |
235 (1 << NET_CAPABILITY_EIMS) |
236 (1 << NET_CAPABILITY_FOTA) |
237 (1 << NET_CAPABILITY_IA) |
238 (1 << NET_CAPABILITY_IMS) |
239 (1 << NET_CAPABILITY_RCS) |
240 (1 << NET_CAPABILITY_XCAP);
241
242 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700243 * Adds the given capability to this {@code NetworkCapability} instance.
244 * Multiple capabilities may be applied sequentially. Note that when searching
245 * for a network to satisfy a request, all capabilities requested must be satisfied.
246 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700247 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900248 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700249 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700250 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700251 public NetworkCapabilities addCapability(int capability) {
252 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700253 throw new IllegalArgumentException("NetworkCapability out of range");
254 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700255 mNetworkCapabilities |= 1 << capability;
256 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700257 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700258
259 /**
260 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
261 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700262 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
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 removeCapability(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 * Gets all the capabilities set on this {@code NetworkCapability} instance.
276 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700277 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700278 * for this instance.
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 int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700282 return enumerateBits(mNetworkCapabilities);
283 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700284
285 /**
286 * Tests for the presence of a capabilitity on this instance.
287 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700288 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700289 * @return {@code true} if set on this instance.
290 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700291 public boolean hasCapability(int capability) {
292 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700293 return false;
294 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700295 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700296 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700297
Robert Greenwalt7569f182014-06-08 16:42:59 -0700298 private int[] enumerateBits(long val) {
299 int size = Long.bitCount(val);
300 int[] result = new int[size];
301 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700302 int resource = 0;
303 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700304 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700305 val = val >> 1;
306 resource++;
307 }
308 return result;
309 }
310
311 private void combineNetCapabilities(NetworkCapabilities nc) {
312 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
313 }
314
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900315 /**
316 * Convenience function that returns a human-readable description of the first mutable
317 * capability we find. Used to present an error message to apps that request mutable
318 * capabilities.
319 *
320 * @hide
321 */
322 public String describeFirstNonRequestableCapability() {
323 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
324 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900325 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900326 // This cannot happen unless the preceding checks are incomplete.
327 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
328 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
329 }
330 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900331 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900332 return null;
333 }
334
335 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
336 long networkCapabilities = this.mNetworkCapabilities;
337 if (onlyImmutable) {
338 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
339 }
340 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700341 }
342
Robert Greenwalt06314e42014-10-29 14:04:06 -0700343 /** @hide */
344 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700345 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
346 }
347
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900348 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
349 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
350 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
351 }
352
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900353 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
354 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
355 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
356 }
357
Robert Greenwalt1448f052014-04-08 13:41:39 -0700358 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400359 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
360 * typically provided by restricted networks.
361 *
362 * TODO: consider:
363 * - Renaming it to guessRestrictedCapability and make it set the
364 * restricted capability bit in addition to clearing it.
365 * @hide
366 */
367 public void maybeMarkCapabilitiesRestricted() {
368 // If all the capabilities are typically provided by restricted networks, conclude that this
369 // network is restricted.
Paul Jensenaae613d2015-08-19 11:06:15 -0400370 if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0 &&
371 // Must have at least some restricted capabilities, otherwise a request for an
372 // internet-less network will get marked restricted.
373 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400374 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400375 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400376 }
377
378 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700379 * Representing the transport type. Apps should generally not care about transport. A
380 * request for a fast internet connection could be satisfied by a number of different
381 * transports. If any are specified here it will be satisfied a Network that matches
382 * any of them. If a caller doesn't care about the transport it should not specify any.
383 */
384 private long mTransportTypes;
385
386 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700387 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700388 */
389 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700390
391 /**
392 * Indicates this network uses a Wi-Fi transport.
393 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700394 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700395
396 /**
397 * Indicates this network uses a Bluetooth transport.
398 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700399 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700400
401 /**
402 * Indicates this network uses an Ethernet transport.
403 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700404 public static final int TRANSPORT_ETHERNET = 3;
405
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400406 /**
407 * Indicates this network uses a VPN transport.
408 */
409 public static final int TRANSPORT_VPN = 4;
410
Etan Cohen305ea282016-06-20 09:27:12 -0700411 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700412 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700413 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700414 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700415
Robert Greenwalt1448f052014-04-08 13:41:39 -0700416 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Etan Cohen0849ded2016-10-26 11:22:06 -0700417 private static final int MAX_TRANSPORT = TRANSPORT_WIFI_AWARE;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700418
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700419 /**
420 * Adds the given transport type to this {@code NetworkCapability} instance.
421 * Multiple transports may be applied sequentially. Note that when searching
422 * for a network to satisfy a request, any listed in the request will satisfy the request.
423 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
424 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
425 * to be selected. This is logically different than
426 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
427 *
428 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900429 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700430 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700431 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700432 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700433 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
434 throw new IllegalArgumentException("TransportType out of range");
435 }
436 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700437 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700438 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700439 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700440
441 /**
442 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
443 *
444 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900445 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700446 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700447 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700448 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700449 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
450 throw new IllegalArgumentException("TransportType out of range");
451 }
452 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700453 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700454 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700455 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700456
457 /**
458 * Gets all the transports set on this {@code NetworkCapability} instance.
459 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700460 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700461 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700462 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700463 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700464 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700465 return enumerateBits(mTransportTypes);
466 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700467
468 /**
469 * Tests for the presence of a transport on this instance.
470 *
471 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
472 * @return {@code true} if set on this instance.
473 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700474 public boolean hasTransport(int transportType) {
475 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
476 return false;
477 }
478 return ((mTransportTypes & (1 << transportType)) != 0);
479 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480
481 private void combineTransportTypes(NetworkCapabilities nc) {
482 this.mTransportTypes |= nc.mTransportTypes;
483 }
484 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
485 return ((this.mTransportTypes == 0) ||
486 ((this.mTransportTypes & nc.mTransportTypes) != 0));
487 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700488 /** @hide */
489 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490 return (nc.mTransportTypes == this.mTransportTypes);
491 }
492
493 /**
494 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
495 * for the first hop on the given transport. It is not measured, but may take into account
496 * link parameters (Radio technology, allocated channels, etc).
497 */
498 private int mLinkUpBandwidthKbps;
499 private int mLinkDownBandwidthKbps;
500
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700501 /**
502 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
503 * the estimated first hop transport bandwidth.
504 * <p>
505 * Note that when used to request a network, this specifies the minimum acceptable.
506 * When received as the state of an existing network this specifies the typical
507 * first hop bandwidth expected. This is never measured, but rather is inferred
508 * from technology type and other link parameters. It could be used to differentiate
509 * between very slow 1xRTT cellular links and other faster networks or even between
510 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
511 * fast backhauls and slow backhauls.
512 *
513 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700514 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700515 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700516 public void setLinkUpstreamBandwidthKbps(int upKbps) {
517 mLinkUpBandwidthKbps = upKbps;
518 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700519
520 /**
521 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
522 * the estimated first hop transport bandwidth.
523 *
524 * @return The estimated first hop upstream (device to network) bandwidth.
525 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700526 public int getLinkUpstreamBandwidthKbps() {
527 return mLinkUpBandwidthKbps;
528 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700529
530 /**
531 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
532 * the estimated first hop transport bandwidth.
533 * <p>
534 * Note that when used to request a network, this specifies the minimum acceptable.
535 * When received as the state of an existing network this specifies the typical
536 * first hop bandwidth expected. This is never measured, but rather is inferred
537 * from technology type and other link parameters. It could be used to differentiate
538 * between very slow 1xRTT cellular links and other faster networks or even between
539 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
540 * fast backhauls and slow backhauls.
541 *
542 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700543 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700544 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700545 public void setLinkDownstreamBandwidthKbps(int downKbps) {
546 mLinkDownBandwidthKbps = downKbps;
547 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700548
549 /**
550 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
551 * the estimated first hop transport bandwidth.
552 *
553 * @return The estimated first hop downstream (network to device) bandwidth.
554 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700555 public int getLinkDownstreamBandwidthKbps() {
556 return mLinkDownBandwidthKbps;
557 }
558
559 private void combineLinkBandwidths(NetworkCapabilities nc) {
560 this.mLinkUpBandwidthKbps =
561 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
562 this.mLinkDownBandwidthKbps =
563 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
564 }
565 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
566 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
567 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
568 }
569 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
570 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
571 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
572 }
573
Etan Cohena7434272017-04-03 12:17:51 -0700574 private NetworkSpecifier mNetworkSpecifier = null;
575
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700576 /**
577 * Sets the optional bearer specific network specifier.
578 * This has no meaning if a single transport is also not specified, so calling
579 * this without a single transport set will generate an exception, as will
580 * subsequently adding or removing transports after this is set.
581 * </p>
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700582 *
Etan Cohena7434272017-04-03 12:17:51 -0700583 * @param networkSpecifier A concrete, parcelable framework class that extends
584 * NetworkSpecifier.
Pierre Imaic8419a82016-03-22 17:54:54 +0900585 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700586 * @hide
587 */
Etan Cohena7434272017-04-03 12:17:51 -0700588 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
589 if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700590 throw new IllegalStateException("Must have a single transport specified to use " +
591 "setNetworkSpecifier");
592 }
Etan Cohena7434272017-04-03 12:17:51 -0700593
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700594 mNetworkSpecifier = networkSpecifier;
Etan Cohena7434272017-04-03 12:17:51 -0700595
Pierre Imaic8419a82016-03-22 17:54:54 +0900596 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700597 }
598
599 /**
600 * Gets the optional bearer specific network specifier.
601 *
Etan Cohena7434272017-04-03 12:17:51 -0700602 * @return The optional {@link NetworkSpecifier} specifying the bearer specific network
603 * specifier. See {@link #setNetworkSpecifier}.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700604 * @hide
605 */
Etan Cohena7434272017-04-03 12:17:51 -0700606 public NetworkSpecifier getNetworkSpecifier() {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700607 return mNetworkSpecifier;
608 }
609
610 private void combineSpecifiers(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700611 if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700612 throw new IllegalStateException("Can't combine two networkSpecifiers");
613 }
Etan Cohena7434272017-04-03 12:17:51 -0700614 setNetworkSpecifier(nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700615 }
Etan Cohena7434272017-04-03 12:17:51 -0700616
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700617 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700618 return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier)
619 || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700620 }
Etan Cohena7434272017-04-03 12:17:51 -0700621
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700622 private boolean equalsSpecifier(NetworkCapabilities nc) {
Etan Cohena7434272017-04-03 12:17:51 -0700623 return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700624 }
625
Robert Greenwalt1448f052014-04-08 13:41:39 -0700626 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900627 * Magic value that indicates no signal strength provided. A request specifying this value is
628 * always satisfied.
629 *
630 * @hide
631 */
632 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
633
634 /**
635 * Signal strength. This is a signed integer, and higher values indicate better signal.
636 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
637 */
638 private int mSignalStrength;
639
640 /**
641 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
642 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
643 * reported by WifiManager.
644 * <p>
645 * Note that when used to register a network callback, this specifies the minimum acceptable
646 * signal strength. When received as the state of an existing network it specifies the current
647 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
648 * effect when requesting a callback.
649 *
650 * @param signalStrength the bearer-specific signal strength.
651 * @hide
652 */
653 public void setSignalStrength(int signalStrength) {
654 mSignalStrength = signalStrength;
655 }
656
657 /**
658 * Returns {@code true} if this object specifies a signal strength.
659 *
660 * @hide
661 */
662 public boolean hasSignalStrength() {
663 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
664 }
665
666 /**
667 * Retrieves the signal strength.
668 *
669 * @return The bearer-specific signal strength.
670 * @hide
671 */
672 public int getSignalStrength() {
673 return mSignalStrength;
674 }
675
676 private void combineSignalStrength(NetworkCapabilities nc) {
677 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
678 }
679
680 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
681 return this.mSignalStrength <= nc.mSignalStrength;
682 }
683
684 private boolean equalsSignalStrength(NetworkCapabilities nc) {
685 return this.mSignalStrength == nc.mSignalStrength;
686 }
687
688 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700689 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900690 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700691 */
692 public void combineCapabilities(NetworkCapabilities nc) {
693 combineNetCapabilities(nc);
694 combineTransportTypes(nc);
695 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700696 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900697 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700698 }
699
700 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900701 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
702 *
703 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
704 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
705 * bandwidth, signal strength, or validation / captive portal status.
706 *
707 * @hide
708 */
709 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
710 return (nc != null &&
711 satisfiedByNetCapabilities(nc, onlyImmutable) &&
712 satisfiedByTransportTypes(nc) &&
713 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900714 satisfiedBySpecifier(nc) &&
715 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900716 }
717
718 /**
719 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
720 *
721 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
722 *
723 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700724 */
725 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900726 return satisfiedByNetworkCapabilities(nc, false);
727 }
728
729 /**
730 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
731 *
732 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
733 *
734 * @hide
735 */
736 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
737 return satisfiedByNetworkCapabilities(nc, true);
738 }
739
740 /**
741 * Checks that our immutable capabilities are the same as those of the given
742 * {@code NetworkCapabilities}.
743 *
744 * @hide
745 */
746 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
747 if (nc == null) return false;
748 return (equalsNetCapabilitiesImmutable(nc) &&
749 equalsTransportTypes(nc) &&
750 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700751 }
752
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900753 /**
754 * Checks that our requestable capabilities are the same as those of the given
755 * {@code NetworkCapabilities}.
756 *
757 * @hide
758 */
759 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
760 if (nc == null) return false;
761 return (equalsNetCapabilitiesRequestable(nc) &&
762 equalsTransportTypes(nc) &&
763 equalsSpecifier(nc));
764 }
765
Robert Greenwalt1448f052014-04-08 13:41:39 -0700766 @Override
767 public boolean equals(Object obj) {
768 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
769 NetworkCapabilities that = (NetworkCapabilities)obj;
770 return (equalsNetCapabilities(that) &&
771 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700772 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900773 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700774 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700775 }
776
777 @Override
778 public int hashCode() {
779 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
780 ((int)(mNetworkCapabilities >> 32) * 3) +
781 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
782 ((int)(mTransportTypes >> 32) * 7) +
783 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700784 (mLinkDownBandwidthKbps * 13) +
Etan Cohena7434272017-04-03 12:17:51 -0700785 Objects.hashCode(mNetworkSpecifier) * 17 +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900786 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700787 }
788
Wink Saville4e2dea72014-09-20 11:04:03 -0700789 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700790 public int describeContents() {
791 return 0;
792 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700793 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700794 public void writeToParcel(Parcel dest, int flags) {
795 dest.writeLong(mNetworkCapabilities);
796 dest.writeLong(mTransportTypes);
797 dest.writeInt(mLinkUpBandwidthKbps);
798 dest.writeInt(mLinkDownBandwidthKbps);
Etan Cohena7434272017-04-03 12:17:51 -0700799 dest.writeParcelable((Parcelable) mNetworkSpecifier, flags);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900800 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700801 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900802
Robert Greenwalt1448f052014-04-08 13:41:39 -0700803 public static final Creator<NetworkCapabilities> CREATOR =
804 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700805 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700806 public NetworkCapabilities createFromParcel(Parcel in) {
807 NetworkCapabilities netCap = new NetworkCapabilities();
808
809 netCap.mNetworkCapabilities = in.readLong();
810 netCap.mTransportTypes = in.readLong();
811 netCap.mLinkUpBandwidthKbps = in.readInt();
812 netCap.mLinkDownBandwidthKbps = in.readInt();
Etan Cohena7434272017-04-03 12:17:51 -0700813 netCap.mNetworkSpecifier = in.readParcelable(null);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900814 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700815 return netCap;
816 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700817 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700818 public NetworkCapabilities[] newArray(int size) {
819 return new NetworkCapabilities[size];
820 }
821 };
822
Wink Saville4e2dea72014-09-20 11:04:03 -0700823 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700824 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700825 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +0900826 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700827
Robert Greenwalt7569f182014-06-08 16:42:59 -0700828 types = getCapabilities();
829 String capabilities = (types.length > 0 ? " Capabilities: " : "");
830 for (int i = 0; i < types.length; ) {
831 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700832 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
833 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
834 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
835 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
836 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
837 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
838 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
839 case NET_CAPABILITY_IA: capabilities += "IA"; break;
840 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
841 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
842 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
843 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
844 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
845 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700846 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400847 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900848 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400849 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900850 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700851 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700852 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700853 }
854
855 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
856 mLinkUpBandwidthKbps + "Kbps" : "");
857 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
858 mLinkDownBandwidthKbps + "Kbps" : "");
859
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700860 String specifier = (mNetworkSpecifier == null ?
861 "" : " Specifier: <" + mNetworkSpecifier + ">");
862
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900863 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
864
865 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700866 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900867
868 /**
869 * @hide
870 */
871 public static String transportNamesOf(int[] types) {
872 String transports = "";
873 for (int i = 0; i < types.length;) {
874 switch (types[i]) {
875 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
876 case TRANSPORT_WIFI: transports += "WIFI"; break;
877 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
878 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
879 case TRANSPORT_VPN: transports += "VPN"; break;
Etan Cohen0849ded2016-10-26 11:22:06 -0700880 case TRANSPORT_WIFI_AWARE: transports += "WIFI_AWARE"; break;
Hugo Benichi5df9d722016-04-25 17:16:35 +0900881 }
882 if (++i < types.length) transports += "|";
883 }
884 return transports;
885 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700886}