blob: 00200d0f42ca2be9c8909d36f9276dc1e4dd1660 [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;
21import android.util.Log;
22
23import java.lang.IllegalArgumentException;
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.HashMap;
27import java.util.Iterator;
28import java.util.Map;
29import java.util.Map.Entry;
30import java.util.Set;
31
32/**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070033 * This class represents the capabilities of a network. This is used both to specify
34 * needs to {@link ConnectivityManager} and when inspecting a network.
35 *
36 * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method
37 * of network selection. Rather than indicate a need for Wi-Fi because an application
38 * needs high bandwidth and risk obselence when a new, fast network appears (like LTE),
39 * the application should specify it needs high bandwidth. Similarly if an application
40 * needs an unmetered network for a bulk transfer it can specify that rather than assuming
41 * all cellular based connections are metered and all Wi-Fi based connections are not.
Robert Greenwalt1448f052014-04-08 13:41:39 -070042 */
43public final class NetworkCapabilities implements Parcelable {
44 private static final String TAG = "NetworkCapabilities";
45 private static final boolean DBG = false;
46
Robert Greenwalt7569f182014-06-08 16:42:59 -070047 /**
48 * @hide
49 */
Robert Greenwalt01d004e2014-05-18 15:24:21 -070050 public NetworkCapabilities() {
51 }
52
53 public NetworkCapabilities(NetworkCapabilities nc) {
54 if (nc != null) {
55 mNetworkCapabilities = nc.mNetworkCapabilities;
56 mTransportTypes = nc.mTransportTypes;
57 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
58 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
59 }
60 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070061
62 /**
63 * Represents the network's capabilities. If any are specified they will be satisfied
64 * by any Network that matches all of them.
65 */
Robert Greenwalt16e12ab2014-07-08 15:31:37 -070066 private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED) |
67 (1 << NET_CAPABILITY_TRUSTED);
Robert Greenwalt1448f052014-04-08 13:41:39 -070068
69 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070070 * Indicates this is a network that has the ability to reach the
71 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070072 */
73 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070074
75 /**
76 * Indicates this is a network that has the ability to reach the carrier's
77 * SUPL server, used to retrieve GPS information.
78 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070079 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070080
81 /**
82 * Indicates this is a network that has the ability to reach the carrier's
83 * DUN or tethering gateway.
84 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070085 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070086
87 /**
88 * Indicates this is a network that has the ability to reach the carrier's
89 * FOTA portal, used for over the air updates.
90 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070091 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070092
93 /**
94 * Indicates this is a network that has the ability to reach the carrier's
95 * IMS servers, used for network registration and signaling.
96 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070097 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070098
99 /**
100 * Indicates this is a network that has the ability to reach the carrier's
101 * CBS servers, used for carrier specific services.
102 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700103 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700104
105 /**
106 * Indicates this is a network that has the ability to reach a Wi-Fi direct
107 * peer.
108 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700109 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700110
111 /**
112 * Indicates this is a network that has the ability to reach a carrier's
113 * Initial Attach servers.
114 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700115 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700116
117 /**
118 * Indicates this is a network that has the ability to reach a carrier's
119 * RCS servers, used for Rich Communication Services.
120 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700121 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700122
123 /**
124 * Indicates this is a network that has the ability to reach a carrier's
125 * XCAP servers, used for configuration and control.
126 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700127 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700128
129 /**
130 * Indicates this is a network that has the ability to reach a carrier's
131 * Emergency IMS servers, used for network signaling during emergency calls.
132 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700134
135 /**
136 * Indicates that this network is unmetered.
137 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700139
140 /**
141 * Indicates that this network should be able to reach the internet.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates that this network is available for general use. If this is not set
147 * applications should not attempt to communicate on this network. Note that this
148 * is simply informative and not enforcement - enforcement is handled via other means.
149 * Set by default.
150 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700151 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
152
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700153 /**
154 * Indicates that the user has indicated implicit trust of this network. This
155 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
156 * BT device or a wifi the user asked to connect to. Untrusted networks
157 * are probably limited to unknown wifi AP. Set by default.
158 */
159 public static final int NET_CAPABILITY_TRUSTED = 14;
160
161
Robert Greenwalt1448f052014-04-08 13:41:39 -0700162 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700163 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_TRUSTED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700164
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700165 /**
166 * Adds the given capability to this {@code NetworkCapability} instance.
167 * Multiple capabilities may be applied sequentially. Note that when searching
168 * for a network to satisfy a request, all capabilities requested must be satisfied.
169 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700170 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
171 * @return This NetworkCapability to facilitate chaining.
172 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700173 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700174 public NetworkCapabilities addCapability(int capability) {
175 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 throw new IllegalArgumentException("NetworkCapability out of range");
177 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700178 mNetworkCapabilities |= 1 << capability;
179 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700181
182 /**
183 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
184 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700185 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
186 * @return This NetworkCapability to facilitate chaining.
187 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700188 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700189 public NetworkCapabilities removeCapability(int capability) {
190 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 throw new IllegalArgumentException("NetworkCapability out of range");
192 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700193 mNetworkCapabilities &= ~(1 << capability);
194 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700195 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700196
197 /**
198 * Gets all the capabilities set on this {@code NetworkCapability} instance.
199 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700200 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700201 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700202 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700203 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700204 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700205 return enumerateBits(mNetworkCapabilities);
206 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700207
208 /**
209 * Tests for the presence of a capabilitity on this instance.
210 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700211 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700212 * @return {@code true} if set on this instance.
213 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700214 public boolean hasCapability(int capability) {
215 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700216 return false;
217 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700218 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700219 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700220
Robert Greenwalt7569f182014-06-08 16:42:59 -0700221 private int[] enumerateBits(long val) {
222 int size = Long.bitCount(val);
223 int[] result = new int[size];
224 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700225 int resource = 0;
226 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700227 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700228 val = val >> 1;
229 resource++;
230 }
231 return result;
232 }
233
234 private void combineNetCapabilities(NetworkCapabilities nc) {
235 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
236 }
237
238 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
239 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
240 }
241
242 private boolean equalsNetCapabilities(NetworkCapabilities nc) {
243 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
244 }
245
246 /**
247 * Representing the transport type. Apps should generally not care about transport. A
248 * request for a fast internet connection could be satisfied by a number of different
249 * transports. If any are specified here it will be satisfied a Network that matches
250 * any of them. If a caller doesn't care about the transport it should not specify any.
251 */
252 private long mTransportTypes;
253
254 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700255 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700256 */
257 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700258
259 /**
260 * Indicates this network uses a Wi-Fi transport.
261 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700262 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700263
264 /**
265 * Indicates this network uses a Bluetooth transport.
266 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700267 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700268
269 /**
270 * Indicates this network uses an Ethernet transport.
271 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700272 public static final int TRANSPORT_ETHERNET = 3;
273
274 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
275 private static final int MAX_TRANSPORT = TRANSPORT_ETHERNET;
276
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700277 /**
278 * Adds the given transport type to this {@code NetworkCapability} instance.
279 * Multiple transports may be applied sequentially. Note that when searching
280 * for a network to satisfy a request, any listed in the request will satisfy the request.
281 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
282 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
283 * to be selected. This is logically different than
284 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
285 *
286 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700287 * @return This NetworkCapability to facilitate chaining.
288 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700289 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700290 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700291 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
292 throw new IllegalArgumentException("TransportType out of range");
293 }
294 mTransportTypes |= 1 << transportType;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700295 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700296 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700297
298 /**
299 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
300 *
301 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700302 * @return This NetworkCapability to facilitate chaining.
303 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700304 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700305 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700306 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
307 throw new IllegalArgumentException("TransportType out of range");
308 }
309 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700310 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700311 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700312
313 /**
314 * Gets all the transports set on this {@code NetworkCapability} instance.
315 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700316 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700317 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700318 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700319 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700320 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700321 return enumerateBits(mTransportTypes);
322 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700323
324 /**
325 * Tests for the presence of a transport on this instance.
326 *
327 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
328 * @return {@code true} if set on this instance.
329 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700330 public boolean hasTransport(int transportType) {
331 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
332 return false;
333 }
334 return ((mTransportTypes & (1 << transportType)) != 0);
335 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700336
337 private void combineTransportTypes(NetworkCapabilities nc) {
338 this.mTransportTypes |= nc.mTransportTypes;
339 }
340 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
341 return ((this.mTransportTypes == 0) ||
342 ((this.mTransportTypes & nc.mTransportTypes) != 0));
343 }
344 private boolean equalsTransportTypes(NetworkCapabilities nc) {
345 return (nc.mTransportTypes == this.mTransportTypes);
346 }
347
348 /**
349 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
350 * for the first hop on the given transport. It is not measured, but may take into account
351 * link parameters (Radio technology, allocated channels, etc).
352 */
353 private int mLinkUpBandwidthKbps;
354 private int mLinkDownBandwidthKbps;
355
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700356 /**
357 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
358 * the estimated first hop transport bandwidth.
359 * <p>
360 * Note that when used to request a network, this specifies the minimum acceptable.
361 * When received as the state of an existing network this specifies the typical
362 * first hop bandwidth expected. This is never measured, but rather is inferred
363 * from technology type and other link parameters. It could be used to differentiate
364 * between very slow 1xRTT cellular links and other faster networks or even between
365 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
366 * fast backhauls and slow backhauls.
367 *
368 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700369 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700370 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700371 public void setLinkUpstreamBandwidthKbps(int upKbps) {
372 mLinkUpBandwidthKbps = upKbps;
373 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700374
375 /**
376 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
377 * the estimated first hop transport bandwidth.
378 *
379 * @return The estimated first hop upstream (device to network) bandwidth.
380 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700381 public int getLinkUpstreamBandwidthKbps() {
382 return mLinkUpBandwidthKbps;
383 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384
385 /**
386 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
387 * the estimated first hop transport bandwidth.
388 * <p>
389 * Note that when used to request a network, this specifies the minimum acceptable.
390 * When received as the state of an existing network this specifies the typical
391 * first hop bandwidth expected. This is never measured, but rather is inferred
392 * from technology type and other link parameters. It could be used to differentiate
393 * between very slow 1xRTT cellular links and other faster networks or even between
394 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
395 * fast backhauls and slow backhauls.
396 *
397 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700398 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700399 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700400 public void setLinkDownstreamBandwidthKbps(int downKbps) {
401 mLinkDownBandwidthKbps = downKbps;
402 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700403
404 /**
405 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
406 * the estimated first hop transport bandwidth.
407 *
408 * @return The estimated first hop downstream (network to device) bandwidth.
409 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700410 public int getLinkDownstreamBandwidthKbps() {
411 return mLinkDownBandwidthKbps;
412 }
413
414 private void combineLinkBandwidths(NetworkCapabilities nc) {
415 this.mLinkUpBandwidthKbps =
416 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
417 this.mLinkDownBandwidthKbps =
418 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
419 }
420 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
421 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
422 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
423 }
424 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
425 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
426 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
427 }
428
429 /**
430 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
431 * {@hide}
432 */
433 public void combineCapabilities(NetworkCapabilities nc) {
434 combineNetCapabilities(nc);
435 combineTransportTypes(nc);
436 combineLinkBandwidths(nc);
437 }
438
439 /**
440 * Check if our requirements are satisfied by the given Capabilities.
441 * {@hide}
442 */
443 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700444 return (nc != null &&
445 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700446 satisfiedByTransportTypes(nc) &&
447 satisfiedByLinkBandwidths(nc));
448 }
449
450 @Override
451 public boolean equals(Object obj) {
452 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
453 NetworkCapabilities that = (NetworkCapabilities)obj;
454 return (equalsNetCapabilities(that) &&
455 equalsTransportTypes(that) &&
456 equalsLinkBandwidths(that));
457 }
458
459 @Override
460 public int hashCode() {
461 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
462 ((int)(mNetworkCapabilities >> 32) * 3) +
463 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
464 ((int)(mTransportTypes >> 32) * 7) +
465 (mLinkUpBandwidthKbps * 11) +
466 (mLinkDownBandwidthKbps * 13));
467 }
468
Robert Greenwalt1448f052014-04-08 13:41:39 -0700469 public int describeContents() {
470 return 0;
471 }
472 public void writeToParcel(Parcel dest, int flags) {
473 dest.writeLong(mNetworkCapabilities);
474 dest.writeLong(mTransportTypes);
475 dest.writeInt(mLinkUpBandwidthKbps);
476 dest.writeInt(mLinkDownBandwidthKbps);
477 }
478 public static final Creator<NetworkCapabilities> CREATOR =
479 new Creator<NetworkCapabilities>() {
480 public NetworkCapabilities createFromParcel(Parcel in) {
481 NetworkCapabilities netCap = new NetworkCapabilities();
482
483 netCap.mNetworkCapabilities = in.readLong();
484 netCap.mTransportTypes = in.readLong();
485 netCap.mLinkUpBandwidthKbps = in.readInt();
486 netCap.mLinkDownBandwidthKbps = in.readInt();
487 return netCap;
488 }
489 public NetworkCapabilities[] newArray(int size) {
490 return new NetworkCapabilities[size];
491 }
492 };
493
494 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700495 int[] types = getTransportTypes();
496 String transports = (types.length > 0 ? " Transports: " : "");
497 for (int i = 0; i < types.length;) {
498 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700499 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
500 case TRANSPORT_WIFI: transports += "WIFI"; break;
501 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
502 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
503 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700504 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700505 }
506
Robert Greenwalt7569f182014-06-08 16:42:59 -0700507 types = getCapabilities();
508 String capabilities = (types.length > 0 ? " Capabilities: " : "");
509 for (int i = 0; i < types.length; ) {
510 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
512 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
513 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
514 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
515 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
516 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
517 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
518 case NET_CAPABILITY_IA: capabilities += "IA"; break;
519 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
520 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
521 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
522 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
523 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
524 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700525 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700526 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700527 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700528 }
529
530 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
531 mLinkUpBandwidthKbps + "Kbps" : "");
532 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
533 mLinkDownBandwidthKbps + "Kbps" : "");
534
Robert Greenwaltc9c90c72014-05-13 15:36:27 -0700535 return "[" + transports + capabilities + upBand + dnBand + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700536 }
537}