blob: 4dd8ce9c8bebc435d249b0a6e095c1d06286edf8 [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 Greenwalt5f90bcc2014-07-09 17:25:41 -070021import android.text.TextUtils;
Robert Greenwalt1448f052014-04-08 13:41:39 -070022import java.lang.IllegalArgumentException;
Robert Greenwalt1448f052014-04-08 13:41:39 -070023
24/**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070025 * This class represents the capabilities of a network. This is used both to specify
26 * needs to {@link ConnectivityManager} and when inspecting a network.
27 *
28 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
29 * of network selection. Rather than indicate a need for Wi-Fi because an application
Wink Saville4e2dea72014-09-20 11:04:03 -070030 * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE),
Robert Greenwalt01d004e2014-05-18 15:24:21 -070031 * the application should specify it needs high bandwidth. Similarly if an application
32 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
33 * all cellular based connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070034 */
35public final class NetworkCapabilities implements Parcelable {
Robert Greenwalt7569f182014-06-08 16:42:59 -070036 /**
37 * @hide
38 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070039 public NetworkCapabilities() {
Lorenzo Colittif7058f52015-04-27 11:31:55 +090040 clearAll();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090041 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070042 }
43
44 public NetworkCapabilities(NetworkCapabilities nc) {
45 if (nc != null) {
46 mNetworkCapabilities = nc.mNetworkCapabilities;
47 mTransportTypes = nc.mTransportTypes;
48 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
49 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070050 mNetworkSpecifier = nc.mNetworkSpecifier;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090051 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070052 }
53 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070054
55 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090056 * Completely clears the contents of this object, removing even the capabilities that are set
57 * by default when the object is constructed.
58 * @hide
59 */
60 public void clearAll() {
61 mNetworkCapabilities = mTransportTypes = 0;
62 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
63 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090064 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090065 }
66
67 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070068 * Represents the network's capabilities. If any are specified they will be satisfied
69 * by any Network that matches all of them.
70 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090071 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070072
73 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070074 * Indicates this is a network that has the ability to reach the
75 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070076 */
77 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070078
79 /**
80 * Indicates this is a network that has the ability to reach the carrier's
81 * SUPL server, used to retrieve GPS information.
82 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070083 public static final int NET_CAPABILITY_SUPL = 1;
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 * DUN or tethering gateway.
88 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070089 public static final int NET_CAPABILITY_DUN = 2;
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 * FOTA portal, used for over the air updates.
94 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070095 public static final int NET_CAPABILITY_FOTA = 3;
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 * IMS servers, used for network registration and signaling.
100 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700101 public static final int NET_CAPABILITY_IMS = 4;
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 * CBS servers, used for carrier specific services.
106 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700107 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700108
109 /**
110 * Indicates this is a network that has the ability to reach a Wi-Fi direct
111 * peer.
112 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700113 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700114
115 /**
116 * Indicates this is a network that has the ability to reach a carrier's
117 * Initial Attach servers.
118 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700119 public static final int NET_CAPABILITY_IA = 7;
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 * RCS servers, used for Rich Communication Services.
124 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700125 public static final int NET_CAPABILITY_RCS = 8;
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 * XCAP servers, used for configuration and control.
130 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700131 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700132
133 /**
134 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700135 * Emergency IMS servers or other services, used for network signaling
136 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700137 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700139
140 /**
141 * Indicates that this network is unmetered.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates that this network should be able to reach the internet.
147 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700148 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700149
150 /**
151 * Indicates that this network is available for general use. If this is not set
152 * applications should not attempt to communicate on this network. Note that this
153 * is simply informative and not enforcement - enforcement is handled via other means.
154 * Set by default.
155 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700156 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
157
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700158 /**
159 * Indicates that the user has indicated implicit trust of this network. This
160 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
161 * BT device or a wifi the user asked to connect to. Untrusted networks
162 * are probably limited to unknown wifi AP. Set by default.
163 */
164 public static final int NET_CAPABILITY_TRUSTED = 14;
165
Paul Jensen76b610a2015-03-18 09:33:07 -0400166 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400167 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400168 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400169 */
170 public static final int NET_CAPABILITY_NOT_VPN = 15;
171
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900172 /**
173 * Indicates that connectivity on this network was successfully validated. For example, for a
174 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
175 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900176 */
177 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700178
Paul Jensen3d194ea2015-06-16 14:27:36 -0400179 /**
180 * Indicates that this network was found to have a captive portal in place last time it was
181 * probed.
182 */
183 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
184
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900185 /**
186 * Indicates that this network is available for use by apps, and not a network that is being
187 * kept up in the background to facilitate fast network switching.
188 * @hide
189 */
190 public static final int NET_CAPABILITY_FOREGROUND = 18;
191
Robert Greenwalt1448f052014-04-08 13:41:39 -0700192 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900193 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700195 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900196 * Network capabilities that are expected to be mutable, i.e., can change while a particular
197 * network is connected.
198 */
199 private static final long MUTABLE_CAPABILITIES =
200 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
201 // http://b/18206275
202 (1 << NET_CAPABILITY_TRUSTED) |
203 (1 << NET_CAPABILITY_VALIDATED) |
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900204 (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
205 (1 << NET_CAPABILITY_FOREGROUND);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900206
207 /**
Etan Cohenddb9ef02015-11-18 10:56:15 -0800208 * Network specifier for factories which want to match any network specifier
209 * (NS) in a request. Behavior:
210 * <li>Empty NS in request matches any network factory NS</li>
211 * <li>Empty NS in the network factory NS only matches a request with an
212 * empty NS</li>
213 * <li>"*" (this constant) NS in the network factory matches requests with
214 * any NS</li>
215 *
216 * @hide
217 */
218 public static final String MATCH_ALL_REQUESTS_NETWORK_SPECIFIER = "*";
219
220 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900221 * Network capabilities that are not allowed in NetworkRequests. This exists because the
222 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
223 * capability's presence cannot be known in advance. If such a capability is requested, then we
224 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
225 * get immediately torn down because they do not have the requested capability.
226 */
227 private static final long NON_REQUESTABLE_CAPABILITIES =
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900228 MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900229
230 /**
231 * Capabilities that are set by default when the object is constructed.
232 */
233 private static final long DEFAULT_CAPABILITIES =
234 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
235 (1 << NET_CAPABILITY_TRUSTED) |
236 (1 << NET_CAPABILITY_NOT_VPN);
237
238 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400239 * Capabilities that suggest that a network is restricted.
240 * {@see #maybeMarkCapabilitiesRestricted}.
241 */
242 private static final long RESTRICTED_CAPABILITIES =
243 (1 << NET_CAPABILITY_CBS) |
244 (1 << NET_CAPABILITY_DUN) |
245 (1 << NET_CAPABILITY_EIMS) |
246 (1 << NET_CAPABILITY_FOTA) |
247 (1 << NET_CAPABILITY_IA) |
248 (1 << NET_CAPABILITY_IMS) |
249 (1 << NET_CAPABILITY_RCS) |
250 (1 << NET_CAPABILITY_XCAP);
251
252 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700253 * Adds the given capability to this {@code NetworkCapability} instance.
254 * Multiple capabilities may be applied sequentially. Note that when searching
255 * for a network to satisfy a request, all capabilities requested must be satisfied.
256 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700257 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900258 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700259 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700260 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700261 public NetworkCapabilities addCapability(int capability) {
262 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700263 throw new IllegalArgumentException("NetworkCapability out of range");
264 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700265 mNetworkCapabilities |= 1 << capability;
266 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700267 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700268
269 /**
270 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
271 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700272 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900273 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700274 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700275 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700276 public NetworkCapabilities removeCapability(int capability) {
277 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700278 throw new IllegalArgumentException("NetworkCapability out of range");
279 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700280 mNetworkCapabilities &= ~(1 << capability);
281 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700282 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700283
284 /**
285 * Gets all the capabilities set on this {@code NetworkCapability} instance.
286 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700287 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700288 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700289 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700290 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700291 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700292 return enumerateBits(mNetworkCapabilities);
293 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700294
295 /**
296 * Tests for the presence of a capabilitity on this instance.
297 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700298 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700299 * @return {@code true} if set on this instance.
300 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700301 public boolean hasCapability(int capability) {
302 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700303 return false;
304 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700305 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700306 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700307
Robert Greenwalt7569f182014-06-08 16:42:59 -0700308 private int[] enumerateBits(long val) {
309 int size = Long.bitCount(val);
310 int[] result = new int[size];
311 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700312 int resource = 0;
313 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700314 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700315 val = val >> 1;
316 resource++;
317 }
318 return result;
319 }
320
321 private void combineNetCapabilities(NetworkCapabilities nc) {
322 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
323 }
324
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900325 /**
326 * Convenience function that returns a human-readable description of the first mutable
327 * capability we find. Used to present an error message to apps that request mutable
328 * capabilities.
329 *
330 * @hide
331 */
332 public String describeFirstNonRequestableCapability() {
333 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
334 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900335 if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900336 // This cannot happen unless the preceding checks are incomplete.
337 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
338 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
339 }
340 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900341 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900342 return null;
343 }
344
345 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
346 long networkCapabilities = this.mNetworkCapabilities;
347 if (onlyImmutable) {
348 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
349 }
350 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700351 }
352
Robert Greenwalt06314e42014-10-29 14:04:06 -0700353 /** @hide */
354 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700355 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
356 }
357
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900358 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
359 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
360 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
361 }
362
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900363 private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
364 return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
365 (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
366 }
367
Robert Greenwalt1448f052014-04-08 13:41:39 -0700368 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400369 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
370 * typically provided by restricted networks.
371 *
372 * TODO: consider:
373 * - Renaming it to guessRestrictedCapability and make it set the
374 * restricted capability bit in addition to clearing it.
375 * @hide
376 */
377 public void maybeMarkCapabilitiesRestricted() {
378 // If all the capabilities are typically provided by restricted networks, conclude that this
379 // network is restricted.
Paul Jensenaae613d2015-08-19 11:06:15 -0400380 if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0 &&
381 // Must have at least some restricted capabilities, otherwise a request for an
382 // internet-less network will get marked restricted.
383 (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0) {
Paul Jensen487ffe72015-07-24 15:57:11 -0400384 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
Paul Jensenaae613d2015-08-19 11:06:15 -0400385 }
Paul Jensen487ffe72015-07-24 15:57:11 -0400386 }
387
388 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700389 * Representing the transport type. Apps should generally not care about transport. A
390 * request for a fast internet connection could be satisfied by a number of different
391 * transports. If any are specified here it will be satisfied a Network that matches
392 * any of them. If a caller doesn't care about the transport it should not specify any.
393 */
394 private long mTransportTypes;
395
396 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700397 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700398 */
399 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700400
401 /**
402 * Indicates this network uses a Wi-Fi transport.
403 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700404 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700405
406 /**
407 * Indicates this network uses a Bluetooth transport.
408 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700409 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700410
411 /**
412 * Indicates this network uses an Ethernet transport.
413 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700414 public static final int TRANSPORT_ETHERNET = 3;
415
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400416 /**
417 * Indicates this network uses a VPN transport.
418 */
419 public static final int TRANSPORT_VPN = 4;
420
Etan Cohen305ea282016-06-20 09:27:12 -0700421 /**
Etan Cohen0849ded2016-10-26 11:22:06 -0700422 * Indicates this network uses a Wi-Fi Aware transport.
Etan Cohen305ea282016-06-20 09:27:12 -0700423 */
Etan Cohen0849ded2016-10-26 11:22:06 -0700424 public static final int TRANSPORT_WIFI_AWARE = 5;
Etan Cohen305ea282016-06-20 09:27:12 -0700425
Robert Greenwalt1448f052014-04-08 13:41:39 -0700426 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Etan Cohen0849ded2016-10-26 11:22:06 -0700427 private static final int MAX_TRANSPORT = TRANSPORT_WIFI_AWARE;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700428
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700429 /**
430 * Adds the given transport type to this {@code NetworkCapability} instance.
431 * Multiple transports may be applied sequentially. Note that when searching
432 * for a network to satisfy a request, any listed in the request will satisfy the request.
433 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
434 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
435 * to be selected. This is logically different than
436 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
437 *
438 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Pierre Imaic8419a82016-03-22 17:54:54 +0900439 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700440 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700441 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700442 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700443 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
444 throw new IllegalArgumentException("TransportType out of range");
445 }
446 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700447 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700448 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700449 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700450
451 /**
452 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
453 *
454 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Pierre Imaic8419a82016-03-22 17:54:54 +0900455 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700456 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700457 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700458 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700459 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
460 throw new IllegalArgumentException("TransportType out of range");
461 }
462 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 * Gets all the transports set on this {@code NetworkCapability} instance.
469 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700470 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700471 * for this instance.
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 int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700475 return enumerateBits(mTransportTypes);
476 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700477
478 /**
479 * Tests for the presence of a transport on this instance.
480 *
481 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
482 * @return {@code true} if set on this instance.
483 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700484 public boolean hasTransport(int transportType) {
485 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
486 return false;
487 }
488 return ((mTransportTypes & (1 << transportType)) != 0);
489 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490
491 private void combineTransportTypes(NetworkCapabilities nc) {
492 this.mTransportTypes |= nc.mTransportTypes;
493 }
494 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
495 return ((this.mTransportTypes == 0) ||
496 ((this.mTransportTypes & nc.mTransportTypes) != 0));
497 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700498 /** @hide */
499 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700500 return (nc.mTransportTypes == this.mTransportTypes);
501 }
502
503 /**
504 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
505 * for the first hop on the given transport. It is not measured, but may take into account
506 * link parameters (Radio technology, allocated channels, etc).
507 */
508 private int mLinkUpBandwidthKbps;
509 private int mLinkDownBandwidthKbps;
510
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700511 /**
512 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
513 * the estimated first hop transport bandwidth.
514 * <p>
515 * Note that when used to request a network, this specifies the minimum acceptable.
516 * When received as the state of an existing network this specifies the typical
517 * first hop bandwidth expected. This is never measured, but rather is inferred
518 * from technology type and other link parameters. It could be used to differentiate
519 * between very slow 1xRTT cellular links and other faster networks or even between
520 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
521 * fast backhauls and slow backhauls.
522 *
523 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700524 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700525 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700526 public void setLinkUpstreamBandwidthKbps(int upKbps) {
527 mLinkUpBandwidthKbps = upKbps;
528 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700529
530 /**
531 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
532 * the estimated first hop transport bandwidth.
533 *
534 * @return The estimated first hop upstream (device to network) bandwidth.
535 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700536 public int getLinkUpstreamBandwidthKbps() {
537 return mLinkUpBandwidthKbps;
538 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700539
540 /**
541 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
542 * the estimated first hop transport bandwidth.
543 * <p>
544 * Note that when used to request a network, this specifies the minimum acceptable.
545 * When received as the state of an existing network this specifies the typical
546 * first hop bandwidth expected. This is never measured, but rather is inferred
547 * from technology type and other link parameters. It could be used to differentiate
548 * between very slow 1xRTT cellular links and other faster networks or even between
549 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
550 * fast backhauls and slow backhauls.
551 *
552 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700553 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700554 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700555 public void setLinkDownstreamBandwidthKbps(int downKbps) {
556 mLinkDownBandwidthKbps = downKbps;
557 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700558
559 /**
560 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
561 * the estimated first hop transport bandwidth.
562 *
563 * @return The estimated first hop downstream (network to device) bandwidth.
564 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700565 public int getLinkDownstreamBandwidthKbps() {
566 return mLinkDownBandwidthKbps;
567 }
568
569 private void combineLinkBandwidths(NetworkCapabilities nc) {
570 this.mLinkUpBandwidthKbps =
571 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
572 this.mLinkDownBandwidthKbps =
573 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
574 }
575 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
576 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
577 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
578 }
579 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
580 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
581 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
582 }
583
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700584 private String mNetworkSpecifier;
585 /**
586 * Sets the optional bearer specific network specifier.
587 * This has no meaning if a single transport is also not specified, so calling
588 * this without a single transport set will generate an exception, as will
589 * subsequently adding or removing transports after this is set.
590 * </p>
591 * The interpretation of this {@code String} is bearer specific and bearers that use
592 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700593 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
594 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700595 *
596 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
597 * specific network specifier where the bearer has a choice of
598 * networks.
Pierre Imaic8419a82016-03-22 17:54:54 +0900599 * @return This NetworkCapabilities instance, to facilitate chaining.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700600 * @hide
601 */
Pierre Imaic8419a82016-03-22 17:54:54 +0900602 public NetworkCapabilities setNetworkSpecifier(String networkSpecifier) {
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700603 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
604 throw new IllegalStateException("Must have a single transport specified to use " +
605 "setNetworkSpecifier");
606 }
607 mNetworkSpecifier = networkSpecifier;
Pierre Imaic8419a82016-03-22 17:54:54 +0900608 return this;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700609 }
610
611 /**
612 * Gets the optional bearer specific network specifier.
613 *
614 * @return The optional {@code String} specifying the bearer specific network specifier.
615 * See {@link #setNetworkSpecifier}.
616 * @hide
617 */
618 public String getNetworkSpecifier() {
619 return mNetworkSpecifier;
620 }
621
622 private void combineSpecifiers(NetworkCapabilities nc) {
623 String otherSpecifier = nc.getNetworkSpecifier();
624 if (TextUtils.isEmpty(otherSpecifier)) return;
625 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
626 throw new IllegalStateException("Can't combine two networkSpecifiers");
627 }
628 setNetworkSpecifier(otherSpecifier);
629 }
630 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
631 return (TextUtils.isEmpty(mNetworkSpecifier) ||
Etan Cohenddb9ef02015-11-18 10:56:15 -0800632 mNetworkSpecifier.equals(nc.mNetworkSpecifier) ||
633 MATCH_ALL_REQUESTS_NETWORK_SPECIFIER.equals(nc.mNetworkSpecifier));
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700634 }
635 private boolean equalsSpecifier(NetworkCapabilities nc) {
636 if (TextUtils.isEmpty(mNetworkSpecifier)) {
637 return TextUtils.isEmpty(nc.mNetworkSpecifier);
638 } else {
639 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
640 }
641 }
642
Robert Greenwalt1448f052014-04-08 13:41:39 -0700643 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900644 * Magic value that indicates no signal strength provided. A request specifying this value is
645 * always satisfied.
646 *
647 * @hide
648 */
649 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
650
651 /**
652 * Signal strength. This is a signed integer, and higher values indicate better signal.
653 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
654 */
655 private int mSignalStrength;
656
657 /**
658 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
659 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
660 * reported by WifiManager.
661 * <p>
662 * Note that when used to register a network callback, this specifies the minimum acceptable
663 * signal strength. When received as the state of an existing network it specifies the current
664 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
665 * effect when requesting a callback.
666 *
667 * @param signalStrength the bearer-specific signal strength.
668 * @hide
669 */
670 public void setSignalStrength(int signalStrength) {
671 mSignalStrength = signalStrength;
672 }
673
674 /**
675 * Returns {@code true} if this object specifies a signal strength.
676 *
677 * @hide
678 */
679 public boolean hasSignalStrength() {
680 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
681 }
682
683 /**
684 * Retrieves the signal strength.
685 *
686 * @return The bearer-specific signal strength.
687 * @hide
688 */
689 public int getSignalStrength() {
690 return mSignalStrength;
691 }
692
693 private void combineSignalStrength(NetworkCapabilities nc) {
694 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
695 }
696
697 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
698 return this.mSignalStrength <= nc.mSignalStrength;
699 }
700
701 private boolean equalsSignalStrength(NetworkCapabilities nc) {
702 return this.mSignalStrength == nc.mSignalStrength;
703 }
704
705 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700706 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900707 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700708 */
709 public void combineCapabilities(NetworkCapabilities nc) {
710 combineNetCapabilities(nc);
711 combineTransportTypes(nc);
712 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700713 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900714 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700715 }
716
717 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900718 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
719 *
720 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
721 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
722 * bandwidth, signal strength, or validation / captive portal status.
723 *
724 * @hide
725 */
726 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
727 return (nc != null &&
728 satisfiedByNetCapabilities(nc, onlyImmutable) &&
729 satisfiedByTransportTypes(nc) &&
730 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900731 satisfiedBySpecifier(nc) &&
732 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900733 }
734
735 /**
736 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
737 *
738 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
739 *
740 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700741 */
742 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900743 return satisfiedByNetworkCapabilities(nc, false);
744 }
745
746 /**
747 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
748 *
749 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
750 *
751 * @hide
752 */
753 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
754 return satisfiedByNetworkCapabilities(nc, true);
755 }
756
757 /**
758 * Checks that our immutable capabilities are the same as those of the given
759 * {@code NetworkCapabilities}.
760 *
761 * @hide
762 */
763 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
764 if (nc == null) return false;
765 return (equalsNetCapabilitiesImmutable(nc) &&
766 equalsTransportTypes(nc) &&
767 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700768 }
769
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900770 /**
771 * Checks that our requestable capabilities are the same as those of the given
772 * {@code NetworkCapabilities}.
773 *
774 * @hide
775 */
776 public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
777 if (nc == null) return false;
778 return (equalsNetCapabilitiesRequestable(nc) &&
779 equalsTransportTypes(nc) &&
780 equalsSpecifier(nc));
781 }
782
Robert Greenwalt1448f052014-04-08 13:41:39 -0700783 @Override
784 public boolean equals(Object obj) {
785 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
786 NetworkCapabilities that = (NetworkCapabilities)obj;
787 return (equalsNetCapabilities(that) &&
788 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700789 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900790 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700791 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700792 }
793
794 @Override
795 public int hashCode() {
796 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
797 ((int)(mNetworkCapabilities >> 32) * 3) +
798 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
799 ((int)(mTransportTypes >> 32) * 7) +
800 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700801 (mLinkDownBandwidthKbps * 13) +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900802 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17) +
803 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700804 }
805
Wink Saville4e2dea72014-09-20 11:04:03 -0700806 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700807 public int describeContents() {
808 return 0;
809 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700810 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700811 public void writeToParcel(Parcel dest, int flags) {
812 dest.writeLong(mNetworkCapabilities);
813 dest.writeLong(mTransportTypes);
814 dest.writeInt(mLinkUpBandwidthKbps);
815 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700816 dest.writeString(mNetworkSpecifier);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900817 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700818 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900819
Robert Greenwalt1448f052014-04-08 13:41:39 -0700820 public static final Creator<NetworkCapabilities> CREATOR =
821 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700822 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700823 public NetworkCapabilities createFromParcel(Parcel in) {
824 NetworkCapabilities netCap = new NetworkCapabilities();
825
826 netCap.mNetworkCapabilities = in.readLong();
827 netCap.mTransportTypes = in.readLong();
828 netCap.mLinkUpBandwidthKbps = in.readInt();
829 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700830 netCap.mNetworkSpecifier = in.readString();
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900831 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700832 return netCap;
833 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700834 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700835 public NetworkCapabilities[] newArray(int size) {
836 return new NetworkCapabilities[size];
837 }
838 };
839
Wink Saville4e2dea72014-09-20 11:04:03 -0700840 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700841 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700842 int[] types = getTransportTypes();
Hugo Benichi5df9d722016-04-25 17:16:35 +0900843 String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : "";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700844
Robert Greenwalt7569f182014-06-08 16:42:59 -0700845 types = getCapabilities();
846 String capabilities = (types.length > 0 ? " Capabilities: " : "");
847 for (int i = 0; i < types.length; ) {
848 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700849 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
850 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
851 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
852 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
853 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
854 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
855 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
856 case NET_CAPABILITY_IA: capabilities += "IA"; break;
857 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
858 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
859 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
860 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
861 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
862 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700863 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400864 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900865 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400866 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Lorenzo Colittif0e9a332016-07-18 18:40:42 +0900867 case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700868 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700869 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700870 }
871
872 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
873 mLinkUpBandwidthKbps + "Kbps" : "");
874 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
875 mLinkDownBandwidthKbps + "Kbps" : "");
876
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700877 String specifier = (mNetworkSpecifier == null ?
878 "" : " Specifier: <" + mNetworkSpecifier + ">");
879
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900880 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
881
882 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700883 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900884
885 /**
886 * @hide
887 */
888 public static String transportNamesOf(int[] types) {
889 String transports = "";
890 for (int i = 0; i < types.length;) {
891 switch (types[i]) {
892 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
893 case TRANSPORT_WIFI: transports += "WIFI"; break;
894 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
895 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
896 case TRANSPORT_VPN: transports += "VPN"; break;
Etan Cohen0849ded2016-10-26 11:22:06 -0700897 case TRANSPORT_WIFI_AWARE: transports += "WIFI_AWARE"; break;
Hugo Benichi5df9d722016-04-25 17:16:35 +0900898 }
899 if (++i < types.length) transports += "|";
900 }
901 return transports;
902 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700903}