blob: 239db8666a32cd453ee2b3debe11d02b7f155dd9 [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) |
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040067 (1 << NET_CAPABILITY_TRUSTED) | (1 << NET_CAPABILITY_NOT_VPN);
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
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400161 /*
162 * Indicates that this network is not a VPN. This capability is set by default and should be
163 * explicitly cleared when creating VPN networks.
164 */
165 public static final int NET_CAPABILITY_NOT_VPN = 15;
166
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700167
Robert Greenwalt1448f052014-04-08 13:41:39 -0700168 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400169 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700170
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700171 /**
172 * Adds the given capability to this {@code NetworkCapability} instance.
173 * Multiple capabilities may be applied sequentially. Note that when searching
174 * for a network to satisfy a request, all capabilities requested must be satisfied.
175 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700176 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
177 * @return This NetworkCapability to facilitate chaining.
178 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700179 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700180 public NetworkCapabilities addCapability(int capability) {
181 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700182 throw new IllegalArgumentException("NetworkCapability out of range");
183 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700184 mNetworkCapabilities |= 1 << capability;
185 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700187
188 /**
189 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
190 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700191 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
192 * @return This NetworkCapability to facilitate chaining.
193 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700194 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700195 public NetworkCapabilities removeCapability(int capability) {
196 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 throw new IllegalArgumentException("NetworkCapability out of range");
198 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700199 mNetworkCapabilities &= ~(1 << capability);
200 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700201 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700202
203 /**
204 * Gets all the capabilities set on this {@code NetworkCapability} instance.
205 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700206 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700207 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700208 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700209 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700210 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700211 return enumerateBits(mNetworkCapabilities);
212 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700213
214 /**
215 * Tests for the presence of a capabilitity on this instance.
216 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700218 * @return {@code true} if set on this instance.
219 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700220 public boolean hasCapability(int capability) {
221 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700222 return false;
223 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700224 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700225 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700226
Robert Greenwalt7569f182014-06-08 16:42:59 -0700227 private int[] enumerateBits(long val) {
228 int size = Long.bitCount(val);
229 int[] result = new int[size];
230 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700231 int resource = 0;
232 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700233 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700234 val = val >> 1;
235 resource++;
236 }
237 return result;
238 }
239
240 private void combineNetCapabilities(NetworkCapabilities nc) {
241 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
242 }
243
244 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
245 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
246 }
247
248 private boolean equalsNetCapabilities(NetworkCapabilities nc) {
249 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
250 }
251
252 /**
253 * Representing the transport type. Apps should generally not care about transport. A
254 * request for a fast internet connection could be satisfied by a number of different
255 * transports. If any are specified here it will be satisfied a Network that matches
256 * any of them. If a caller doesn't care about the transport it should not specify any.
257 */
258 private long mTransportTypes;
259
260 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700261 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700262 */
263 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700264
265 /**
266 * Indicates this network uses a Wi-Fi transport.
267 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700268 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700269
270 /**
271 * Indicates this network uses a Bluetooth transport.
272 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700273 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700274
275 /**
276 * Indicates this network uses an Ethernet transport.
277 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700278 public static final int TRANSPORT_ETHERNET = 3;
279
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400280 /**
281 * Indicates this network uses a VPN transport.
282 */
283 public static final int TRANSPORT_VPN = 4;
284
Robert Greenwalt1448f052014-04-08 13:41:39 -0700285 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400286 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700287
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700288 /**
289 * Adds the given transport type to this {@code NetworkCapability} instance.
290 * Multiple transports may be applied sequentially. Note that when searching
291 * for a network to satisfy a request, any listed in the request will satisfy the request.
292 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
293 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
294 * to be selected. This is logically different than
295 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
296 *
297 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700298 * @return This NetworkCapability to facilitate chaining.
299 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700300 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700301 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700302 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
303 throw new IllegalArgumentException("TransportType out of range");
304 }
305 mTransportTypes |= 1 << transportType;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700306 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700307 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700308
309 /**
310 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
311 *
312 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700313 * @return This NetworkCapability to facilitate chaining.
314 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700315 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700316 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700317 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
318 throw new IllegalArgumentException("TransportType out of range");
319 }
320 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700321 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700322 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700323
324 /**
325 * Gets all the transports set on this {@code NetworkCapability} instance.
326 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700327 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700328 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700329 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700330 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700331 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700332 return enumerateBits(mTransportTypes);
333 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700334
335 /**
336 * Tests for the presence of a transport on this instance.
337 *
338 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
339 * @return {@code true} if set on this instance.
340 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700341 public boolean hasTransport(int transportType) {
342 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
343 return false;
344 }
345 return ((mTransportTypes & (1 << transportType)) != 0);
346 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700347
348 private void combineTransportTypes(NetworkCapabilities nc) {
349 this.mTransportTypes |= nc.mTransportTypes;
350 }
351 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
352 return ((this.mTransportTypes == 0) ||
353 ((this.mTransportTypes & nc.mTransportTypes) != 0));
354 }
355 private boolean equalsTransportTypes(NetworkCapabilities nc) {
356 return (nc.mTransportTypes == this.mTransportTypes);
357 }
358
359 /**
360 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
361 * for the first hop on the given transport. It is not measured, but may take into account
362 * link parameters (Radio technology, allocated channels, etc).
363 */
364 private int mLinkUpBandwidthKbps;
365 private int mLinkDownBandwidthKbps;
366
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700367 /**
368 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
369 * the estimated first hop transport bandwidth.
370 * <p>
371 * Note that when used to request a network, this specifies the minimum acceptable.
372 * When received as the state of an existing network this specifies the typical
373 * first hop bandwidth expected. This is never measured, but rather is inferred
374 * from technology type and other link parameters. It could be used to differentiate
375 * between very slow 1xRTT cellular links and other faster networks or even between
376 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
377 * fast backhauls and slow backhauls.
378 *
379 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700380 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700381 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700382 public void setLinkUpstreamBandwidthKbps(int upKbps) {
383 mLinkUpBandwidthKbps = upKbps;
384 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700385
386 /**
387 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
388 * the estimated first hop transport bandwidth.
389 *
390 * @return The estimated first hop upstream (device to network) bandwidth.
391 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700392 public int getLinkUpstreamBandwidthKbps() {
393 return mLinkUpBandwidthKbps;
394 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700395
396 /**
397 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
398 * the estimated first hop transport bandwidth.
399 * <p>
400 * Note that when used to request a network, this specifies the minimum acceptable.
401 * When received as the state of an existing network this specifies the typical
402 * first hop bandwidth expected. This is never measured, but rather is inferred
403 * from technology type and other link parameters. It could be used to differentiate
404 * between very slow 1xRTT cellular links and other faster networks or even between
405 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
406 * fast backhauls and slow backhauls.
407 *
408 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700409 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700410 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700411 public void setLinkDownstreamBandwidthKbps(int downKbps) {
412 mLinkDownBandwidthKbps = downKbps;
413 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700414
415 /**
416 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
417 * the estimated first hop transport bandwidth.
418 *
419 * @return The estimated first hop downstream (network to device) bandwidth.
420 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700421 public int getLinkDownstreamBandwidthKbps() {
422 return mLinkDownBandwidthKbps;
423 }
424
425 private void combineLinkBandwidths(NetworkCapabilities nc) {
426 this.mLinkUpBandwidthKbps =
427 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
428 this.mLinkDownBandwidthKbps =
429 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
430 }
431 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
432 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
433 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
434 }
435 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
436 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
437 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
438 }
439
440 /**
441 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
442 * {@hide}
443 */
444 public void combineCapabilities(NetworkCapabilities nc) {
445 combineNetCapabilities(nc);
446 combineTransportTypes(nc);
447 combineLinkBandwidths(nc);
448 }
449
450 /**
451 * Check if our requirements are satisfied by the given Capabilities.
452 * {@hide}
453 */
454 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700455 return (nc != null &&
456 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700457 satisfiedByTransportTypes(nc) &&
458 satisfiedByLinkBandwidths(nc));
459 }
460
461 @Override
462 public boolean equals(Object obj) {
463 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
464 NetworkCapabilities that = (NetworkCapabilities)obj;
465 return (equalsNetCapabilities(that) &&
466 equalsTransportTypes(that) &&
467 equalsLinkBandwidths(that));
468 }
469
470 @Override
471 public int hashCode() {
472 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
473 ((int)(mNetworkCapabilities >> 32) * 3) +
474 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
475 ((int)(mTransportTypes >> 32) * 7) +
476 (mLinkUpBandwidthKbps * 11) +
477 (mLinkDownBandwidthKbps * 13));
478 }
479
Robert Greenwalt1448f052014-04-08 13:41:39 -0700480 public int describeContents() {
481 return 0;
482 }
483 public void writeToParcel(Parcel dest, int flags) {
484 dest.writeLong(mNetworkCapabilities);
485 dest.writeLong(mTransportTypes);
486 dest.writeInt(mLinkUpBandwidthKbps);
487 dest.writeInt(mLinkDownBandwidthKbps);
488 }
489 public static final Creator<NetworkCapabilities> CREATOR =
490 new Creator<NetworkCapabilities>() {
491 public NetworkCapabilities createFromParcel(Parcel in) {
492 NetworkCapabilities netCap = new NetworkCapabilities();
493
494 netCap.mNetworkCapabilities = in.readLong();
495 netCap.mTransportTypes = in.readLong();
496 netCap.mLinkUpBandwidthKbps = in.readInt();
497 netCap.mLinkDownBandwidthKbps = in.readInt();
498 return netCap;
499 }
500 public NetworkCapabilities[] newArray(int size) {
501 return new NetworkCapabilities[size];
502 }
503 };
504
505 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700506 int[] types = getTransportTypes();
507 String transports = (types.length > 0 ? " Transports: " : "");
508 for (int i = 0; i < types.length;) {
509 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700510 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
511 case TRANSPORT_WIFI: transports += "WIFI"; break;
512 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
513 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400514 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700515 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700516 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700517 }
518
Robert Greenwalt7569f182014-06-08 16:42:59 -0700519 types = getCapabilities();
520 String capabilities = (types.length > 0 ? " Capabilities: " : "");
521 for (int i = 0; i < types.length; ) {
522 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700523 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
524 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
525 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
526 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
527 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
528 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
529 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
530 case NET_CAPABILITY_IA: capabilities += "IA"; break;
531 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
532 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
533 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
534 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
535 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
536 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700537 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400538 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700539 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700540 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700541 }
542
543 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
544 mLinkUpBandwidthKbps + "Kbps" : "");
545 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
546 mLinkDownBandwidthKbps + "Kbps" : "");
547
Robert Greenwaltc9c90c72014-05-13 15:36:27 -0700548 return "[" + transports + capabilities + upBand + dnBand + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700549 }
550}