blob: fe96287c83e654f2e475ca12a6e8ed3527ec7130 [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 */
66 private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED);
67
68 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070069 * Indicates this is a network that has the ability to reach the
70 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070071 */
72 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070073
74 /**
75 * Indicates this is a network that has the ability to reach the carrier's
76 * SUPL server, used to retrieve GPS information.
77 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070078 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070079
80 /**
81 * Indicates this is a network that has the ability to reach the carrier's
82 * DUN or tethering gateway.
83 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070084 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070085
86 /**
87 * Indicates this is a network that has the ability to reach the carrier's
88 * FOTA portal, used for over the air updates.
89 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070090 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070091
92 /**
93 * Indicates this is a network that has the ability to reach the carrier's
94 * IMS servers, used for network registration and signaling.
95 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070096 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070097
98 /**
99 * Indicates this is a network that has the ability to reach the carrier's
100 * CBS servers, used for carrier specific services.
101 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700102 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700103
104 /**
105 * Indicates this is a network that has the ability to reach a Wi-Fi direct
106 * peer.
107 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700108 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700109
110 /**
111 * Indicates this is a network that has the ability to reach a carrier's
112 * Initial Attach servers.
113 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700114 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700115
116 /**
117 * Indicates this is a network that has the ability to reach a carrier's
118 * RCS servers, used for Rich Communication Services.
119 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700120 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700121
122 /**
123 * Indicates this is a network that has the ability to reach a carrier's
124 * XCAP servers, used for configuration and control.
125 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700126 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700127
128 /**
129 * Indicates this is a network that has the ability to reach a carrier's
130 * Emergency IMS servers, used for network signaling during emergency calls.
131 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700132 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700133
134 /**
135 * Indicates that this network is unmetered.
136 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700137 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700138
139 /**
140 * Indicates that this network should be able to reach the internet.
141 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700142 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700143
144 /**
145 * Indicates that this network is available for general use. If this is not set
146 * applications should not attempt to communicate on this network. Note that this
147 * is simply informative and not enforcement - enforcement is handled via other means.
148 * Set by default.
149 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700150 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
151
152 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
153 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_RESTRICTED;
154
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700155 /**
156 * Adds the given capability to this {@code NetworkCapability} instance.
157 * Multiple capabilities may be applied sequentially. Note that when searching
158 * for a network to satisfy a request, all capabilities requested must be satisfied.
159 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700160 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
161 * @return This NetworkCapability to facilitate chaining.
162 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700163 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700164 public NetworkCapabilities addCapability(int capability) {
165 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700166 throw new IllegalArgumentException("NetworkCapability out of range");
167 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700168 mNetworkCapabilities |= 1 << capability;
169 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700170 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700171
172 /**
173 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
174 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700175 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
176 * @return This NetworkCapability to facilitate chaining.
177 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700178 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700179 public NetworkCapabilities removeCapability(int capability) {
180 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700181 throw new IllegalArgumentException("NetworkCapability out of range");
182 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700183 mNetworkCapabilities &= ~(1 << capability);
184 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186
187 /**
188 * Gets all the capabilities set on this {@code NetworkCapability} instance.
189 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700190 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700191 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700192 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700193 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700194 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700195 return enumerateBits(mNetworkCapabilities);
196 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197
198 /**
199 * Tests for the presence of a capabilitity on this instance.
200 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700201 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700202 * @return {@code true} if set on this instance.
203 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700204 public boolean hasCapability(int capability) {
205 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700206 return false;
207 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700208 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700209 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700210
Robert Greenwalt7569f182014-06-08 16:42:59 -0700211 private int[] enumerateBits(long val) {
212 int size = Long.bitCount(val);
213 int[] result = new int[size];
214 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 int resource = 0;
216 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700218 val = val >> 1;
219 resource++;
220 }
221 return result;
222 }
223
224 private void combineNetCapabilities(NetworkCapabilities nc) {
225 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
226 }
227
228 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
229 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
230 }
231
232 private boolean equalsNetCapabilities(NetworkCapabilities nc) {
233 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
234 }
235
236 /**
237 * Representing the transport type. Apps should generally not care about transport. A
238 * request for a fast internet connection could be satisfied by a number of different
239 * transports. If any are specified here it will be satisfied a Network that matches
240 * any of them. If a caller doesn't care about the transport it should not specify any.
241 */
242 private long mTransportTypes;
243
244 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700245 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700246 */
247 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700248
249 /**
250 * Indicates this network uses a Wi-Fi transport.
251 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700252 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700253
254 /**
255 * Indicates this network uses a Bluetooth transport.
256 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700257 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700258
259 /**
260 * Indicates this network uses an Ethernet transport.
261 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700262 public static final int TRANSPORT_ETHERNET = 3;
263
264 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
265 private static final int MAX_TRANSPORT = TRANSPORT_ETHERNET;
266
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700267 /**
268 * Adds the given transport type to this {@code NetworkCapability} instance.
269 * Multiple transports may be applied sequentially. Note that when searching
270 * for a network to satisfy a request, any listed in the request will satisfy the request.
271 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
272 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
273 * to be selected. This is logically different than
274 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
275 *
276 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700277 * @return This NetworkCapability to facilitate chaining.
278 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700279 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700280 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700281 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
282 throw new IllegalArgumentException("TransportType out of range");
283 }
284 mTransportTypes |= 1 << transportType;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700285 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700286 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700287
288 /**
289 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
290 *
291 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700292 * @return This NetworkCapability to facilitate chaining.
293 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700294 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700295 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700296 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
297 throw new IllegalArgumentException("TransportType out of range");
298 }
299 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700300 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700301 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700302
303 /**
304 * Gets all the transports set on this {@code NetworkCapability} instance.
305 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700306 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700307 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700308 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700309 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700310 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700311 return enumerateBits(mTransportTypes);
312 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700313
314 /**
315 * Tests for the presence of a transport on this instance.
316 *
317 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
318 * @return {@code true} if set on this instance.
319 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700320 public boolean hasTransport(int transportType) {
321 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
322 return false;
323 }
324 return ((mTransportTypes & (1 << transportType)) != 0);
325 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700326
327 private void combineTransportTypes(NetworkCapabilities nc) {
328 this.mTransportTypes |= nc.mTransportTypes;
329 }
330 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
331 return ((this.mTransportTypes == 0) ||
332 ((this.mTransportTypes & nc.mTransportTypes) != 0));
333 }
334 private boolean equalsTransportTypes(NetworkCapabilities nc) {
335 return (nc.mTransportTypes == this.mTransportTypes);
336 }
337
338 /**
339 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
340 * for the first hop on the given transport. It is not measured, but may take into account
341 * link parameters (Radio technology, allocated channels, etc).
342 */
343 private int mLinkUpBandwidthKbps;
344 private int mLinkDownBandwidthKbps;
345
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700346 /**
347 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
348 * the estimated first hop transport bandwidth.
349 * <p>
350 * Note that when used to request a network, this specifies the minimum acceptable.
351 * When received as the state of an existing network this specifies the typical
352 * first hop bandwidth expected. This is never measured, but rather is inferred
353 * from technology type and other link parameters. It could be used to differentiate
354 * between very slow 1xRTT cellular links and other faster networks or even between
355 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
356 * fast backhauls and slow backhauls.
357 *
358 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700359 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700360 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700361 public void setLinkUpstreamBandwidthKbps(int upKbps) {
362 mLinkUpBandwidthKbps = upKbps;
363 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700364
365 /**
366 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
367 * the estimated first hop transport bandwidth.
368 *
369 * @return The estimated first hop upstream (device to network) bandwidth.
370 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700371 public int getLinkUpstreamBandwidthKbps() {
372 return mLinkUpBandwidthKbps;
373 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700374
375 /**
376 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
377 * the estimated first hop transport bandwidth.
378 * <p>
379 * Note that when used to request a network, this specifies the minimum acceptable.
380 * When received as the state of an existing network this specifies the typical
381 * first hop bandwidth expected. This is never measured, but rather is inferred
382 * from technology type and other link parameters. It could be used to differentiate
383 * between very slow 1xRTT cellular links and other faster networks or even between
384 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
385 * fast backhauls and slow backhauls.
386 *
387 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700388 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700389 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700390 public void setLinkDownstreamBandwidthKbps(int downKbps) {
391 mLinkDownBandwidthKbps = downKbps;
392 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700393
394 /**
395 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
396 * the estimated first hop transport bandwidth.
397 *
398 * @return The estimated first hop downstream (network to device) bandwidth.
399 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700400 public int getLinkDownstreamBandwidthKbps() {
401 return mLinkDownBandwidthKbps;
402 }
403
404 private void combineLinkBandwidths(NetworkCapabilities nc) {
405 this.mLinkUpBandwidthKbps =
406 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
407 this.mLinkDownBandwidthKbps =
408 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
409 }
410 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
411 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
412 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
413 }
414 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
415 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
416 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
417 }
418
419 /**
420 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
421 * {@hide}
422 */
423 public void combineCapabilities(NetworkCapabilities nc) {
424 combineNetCapabilities(nc);
425 combineTransportTypes(nc);
426 combineLinkBandwidths(nc);
427 }
428
429 /**
430 * Check if our requirements are satisfied by the given Capabilities.
431 * {@hide}
432 */
433 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700434 return (nc != null &&
435 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700436 satisfiedByTransportTypes(nc) &&
437 satisfiedByLinkBandwidths(nc));
438 }
439
440 @Override
441 public boolean equals(Object obj) {
442 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
443 NetworkCapabilities that = (NetworkCapabilities)obj;
444 return (equalsNetCapabilities(that) &&
445 equalsTransportTypes(that) &&
446 equalsLinkBandwidths(that));
447 }
448
449 @Override
450 public int hashCode() {
451 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
452 ((int)(mNetworkCapabilities >> 32) * 3) +
453 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
454 ((int)(mTransportTypes >> 32) * 7) +
455 (mLinkUpBandwidthKbps * 11) +
456 (mLinkDownBandwidthKbps * 13));
457 }
458
Robert Greenwalt1448f052014-04-08 13:41:39 -0700459 public int describeContents() {
460 return 0;
461 }
462 public void writeToParcel(Parcel dest, int flags) {
463 dest.writeLong(mNetworkCapabilities);
464 dest.writeLong(mTransportTypes);
465 dest.writeInt(mLinkUpBandwidthKbps);
466 dest.writeInt(mLinkDownBandwidthKbps);
467 }
468 public static final Creator<NetworkCapabilities> CREATOR =
469 new Creator<NetworkCapabilities>() {
470 public NetworkCapabilities createFromParcel(Parcel in) {
471 NetworkCapabilities netCap = new NetworkCapabilities();
472
473 netCap.mNetworkCapabilities = in.readLong();
474 netCap.mTransportTypes = in.readLong();
475 netCap.mLinkUpBandwidthKbps = in.readInt();
476 netCap.mLinkDownBandwidthKbps = in.readInt();
477 return netCap;
478 }
479 public NetworkCapabilities[] newArray(int size) {
480 return new NetworkCapabilities[size];
481 }
482 };
483
484 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700485 int[] types = getTransportTypes();
486 String transports = (types.length > 0 ? " Transports: " : "");
487 for (int i = 0; i < types.length;) {
488 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700489 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
490 case TRANSPORT_WIFI: transports += "WIFI"; break;
491 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
492 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
493 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700494 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700495 }
496
Robert Greenwalt7569f182014-06-08 16:42:59 -0700497 types = getCapabilities();
498 String capabilities = (types.length > 0 ? " Capabilities: " : "");
499 for (int i = 0; i < types.length; ) {
500 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700501 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
502 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
503 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
504 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
505 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
506 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
507 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
508 case NET_CAPABILITY_IA: capabilities += "IA"; break;
509 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
510 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
511 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
512 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
513 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
514 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
515 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700516 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700517 }
518
519 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
520 mLinkUpBandwidthKbps + "Kbps" : "");
521 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
522 mLinkDownBandwidthKbps + "Kbps" : "");
523
Robert Greenwaltc9c90c72014-05-13 15:36:27 -0700524 return "[" + transports + capabilities + upBand + dnBand + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700525 }
526}