blob: 514d24abc52f62ee1f055d6eccd11acbf8a5e936 [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();
41 mNetworkCapabilities =
42 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
43 (1 << NET_CAPABILITY_TRUSTED) |
44 (1 << NET_CAPABILITY_NOT_VPN);
Robert Greenwalt01d004e2014-05-18 15:24:21 -070045 }
46
47 public NetworkCapabilities(NetworkCapabilities nc) {
48 if (nc != null) {
49 mNetworkCapabilities = nc.mNetworkCapabilities;
50 mTransportTypes = nc.mTransportTypes;
51 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
52 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070053 mNetworkSpecifier = nc.mNetworkSpecifier;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070054 }
55 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070056
57 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090058 * Completely clears the contents of this object, removing even the capabilities that are set
59 * by default when the object is constructed.
60 * @hide
61 */
62 public void clearAll() {
63 mNetworkCapabilities = mTransportTypes = 0;
64 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
65 mNetworkSpecifier = null;
66 }
67
68 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070069 * Represents the network's capabilities. If any are specified they will be satisfied
70 * by any Network that matches all of them.
71 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090072 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070073
74 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070075 * Indicates this is a network that has the ability to reach the
76 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070077 */
78 public static final int NET_CAPABILITY_MMS = 0;
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 * SUPL server, used to retrieve GPS information.
83 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070084 public static final int NET_CAPABILITY_SUPL = 1;
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 * DUN or tethering gateway.
89 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070090 public static final int NET_CAPABILITY_DUN = 2;
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 * FOTA portal, used for over the air updates.
95 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070096 public static final int NET_CAPABILITY_FOTA = 3;
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 * IMS servers, used for network registration and signaling.
101 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700102 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700103
104 /**
105 * Indicates this is a network that has the ability to reach the carrier's
106 * CBS servers, used for carrier specific services.
107 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700108 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700109
110 /**
111 * Indicates this is a network that has the ability to reach a Wi-Fi direct
112 * peer.
113 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700114 public static final int NET_CAPABILITY_WIFI_P2P = 6;
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 * Initial Attach servers.
119 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700120 public static final int NET_CAPABILITY_IA = 7;
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 * RCS servers, used for Rich Communication Services.
125 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700126 public static final int NET_CAPABILITY_RCS = 8;
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 * XCAP servers, used for configuration and control.
131 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700132 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700133
134 /**
135 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700136 * Emergency IMS servers or other services, used for network signaling
137 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700138 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700139 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700140
141 /**
142 * Indicates that this network is unmetered.
143 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700144 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700145
146 /**
147 * Indicates that this network should be able to reach the internet.
148 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700149 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700150
151 /**
152 * Indicates that this network is available for general use. If this is not set
153 * applications should not attempt to communicate on this network. Note that this
154 * is simply informative and not enforcement - enforcement is handled via other means.
155 * Set by default.
156 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700157 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
158
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700159 /**
160 * Indicates that the user has indicated implicit trust of this network. This
161 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
162 * BT device or a wifi the user asked to connect to. Untrusted networks
163 * are probably limited to unknown wifi AP. Set by default.
164 */
165 public static final int NET_CAPABILITY_TRUSTED = 14;
166
Paul Jensen76b610a2015-03-18 09:33:07 -0400167 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400168 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400169 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400170 */
171 public static final int NET_CAPABILITY_NOT_VPN = 15;
172
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900173 /**
174 * Indicates that connectivity on this network was successfully validated. For example, for a
175 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
176 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900177 */
178 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700179
Paul Jensen3d194ea2015-06-16 14:27:36 -0400180 /**
181 * Indicates that this network was found to have a captive portal in place last time it was
182 * probed.
183 */
184 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
185
Robert Greenwalt1448f052014-04-08 13:41:39 -0700186 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Paul Jensen3d194ea2015-06-16 14:27:36 -0400187 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700188
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700189 /**
190 * Adds the given capability to this {@code NetworkCapability} instance.
191 * Multiple capabilities may be applied sequentially. Note that when searching
192 * for a network to satisfy a request, all capabilities requested must be satisfied.
193 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700194 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
195 * @return This NetworkCapability to facilitate chaining.
196 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700198 public NetworkCapabilities addCapability(int capability) {
199 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700200 throw new IllegalArgumentException("NetworkCapability out of range");
201 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700202 mNetworkCapabilities |= 1 << capability;
203 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700204 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205
206 /**
207 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
208 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700209 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
210 * @return This NetworkCapability to facilitate chaining.
211 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700212 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700213 public NetworkCapabilities removeCapability(int capability) {
214 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700215 throw new IllegalArgumentException("NetworkCapability out of range");
216 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 mNetworkCapabilities &= ~(1 << capability);
218 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700219 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700220
221 /**
222 * Gets all the capabilities set on this {@code NetworkCapability} instance.
223 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700224 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700225 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700226 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700227 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700228 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700229 return enumerateBits(mNetworkCapabilities);
230 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700231
232 /**
233 * Tests for the presence of a capabilitity on this instance.
234 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700235 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700236 * @return {@code true} if set on this instance.
237 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700238 public boolean hasCapability(int capability) {
239 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700240 return false;
241 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700242 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700243 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700244
Robert Greenwalt7569f182014-06-08 16:42:59 -0700245 private int[] enumerateBits(long val) {
246 int size = Long.bitCount(val);
247 int[] result = new int[size];
248 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700249 int resource = 0;
250 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700251 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700252 val = val >> 1;
253 resource++;
254 }
255 return result;
256 }
257
258 private void combineNetCapabilities(NetworkCapabilities nc) {
259 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
260 }
261
262 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
263 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
264 }
265
Robert Greenwalt06314e42014-10-29 14:04:06 -0700266 /** @hide */
267 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700268 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
269 }
270
271 /**
272 * Representing the transport type. Apps should generally not care about transport. A
273 * request for a fast internet connection could be satisfied by a number of different
274 * transports. If any are specified here it will be satisfied a Network that matches
275 * any of them. If a caller doesn't care about the transport it should not specify any.
276 */
277 private long mTransportTypes;
278
279 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700280 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700281 */
282 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700283
284 /**
285 * Indicates this network uses a Wi-Fi transport.
286 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700287 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700288
289 /**
290 * Indicates this network uses a Bluetooth transport.
291 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700292 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700293
294 /**
295 * Indicates this network uses an Ethernet transport.
296 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700297 public static final int TRANSPORT_ETHERNET = 3;
298
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400299 /**
300 * Indicates this network uses a VPN transport.
301 */
302 public static final int TRANSPORT_VPN = 4;
303
Robert Greenwalt1448f052014-04-08 13:41:39 -0700304 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400305 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700306
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700307 /**
308 * Adds the given transport type to this {@code NetworkCapability} instance.
309 * Multiple transports may be applied sequentially. Note that when searching
310 * for a network to satisfy a request, any listed in the request will satisfy the request.
311 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
312 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
313 * to be selected. This is logically different than
314 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
315 *
316 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700317 * @return This NetworkCapability to facilitate chaining.
318 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700319 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700320 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700321 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
322 throw new IllegalArgumentException("TransportType out of range");
323 }
324 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700325 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700326 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700327 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700328
329 /**
330 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
331 *
332 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700333 * @return This NetworkCapability to facilitate chaining.
334 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700335 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700336 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700337 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
338 throw new IllegalArgumentException("TransportType out of range");
339 }
340 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700341 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700342 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700343 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700344
345 /**
346 * Gets all the transports set on this {@code NetworkCapability} instance.
347 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700348 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700349 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700350 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700351 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700352 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700353 return enumerateBits(mTransportTypes);
354 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700355
356 /**
357 * Tests for the presence of a transport on this instance.
358 *
359 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
360 * @return {@code true} if set on this instance.
361 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700362 public boolean hasTransport(int transportType) {
363 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
364 return false;
365 }
366 return ((mTransportTypes & (1 << transportType)) != 0);
367 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700368
369 private void combineTransportTypes(NetworkCapabilities nc) {
370 this.mTransportTypes |= nc.mTransportTypes;
371 }
372 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
373 return ((this.mTransportTypes == 0) ||
374 ((this.mTransportTypes & nc.mTransportTypes) != 0));
375 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700376 /** @hide */
377 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700378 return (nc.mTransportTypes == this.mTransportTypes);
379 }
380
381 /**
382 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
383 * for the first hop on the given transport. It is not measured, but may take into account
384 * link parameters (Radio technology, allocated channels, etc).
385 */
386 private int mLinkUpBandwidthKbps;
387 private int mLinkDownBandwidthKbps;
388
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700389 /**
390 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
391 * the estimated first hop transport bandwidth.
392 * <p>
393 * Note that when used to request a network, this specifies the minimum acceptable.
394 * When received as the state of an existing network this specifies the typical
395 * first hop bandwidth expected. This is never measured, but rather is inferred
396 * from technology type and other link parameters. It could be used to differentiate
397 * between very slow 1xRTT cellular links and other faster networks or even between
398 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
399 * fast backhauls and slow backhauls.
400 *
401 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700402 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700403 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700404 public void setLinkUpstreamBandwidthKbps(int upKbps) {
405 mLinkUpBandwidthKbps = upKbps;
406 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700407
408 /**
409 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
410 * the estimated first hop transport bandwidth.
411 *
412 * @return The estimated first hop upstream (device to network) bandwidth.
413 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700414 public int getLinkUpstreamBandwidthKbps() {
415 return mLinkUpBandwidthKbps;
416 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700417
418 /**
419 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
420 * the estimated first hop transport bandwidth.
421 * <p>
422 * Note that when used to request a network, this specifies the minimum acceptable.
423 * When received as the state of an existing network this specifies the typical
424 * first hop bandwidth expected. This is never measured, but rather is inferred
425 * from technology type and other link parameters. It could be used to differentiate
426 * between very slow 1xRTT cellular links and other faster networks or even between
427 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
428 * fast backhauls and slow backhauls.
429 *
430 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700431 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700432 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700433 public void setLinkDownstreamBandwidthKbps(int downKbps) {
434 mLinkDownBandwidthKbps = downKbps;
435 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700436
437 /**
438 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
439 * the estimated first hop transport bandwidth.
440 *
441 * @return The estimated first hop downstream (network to device) bandwidth.
442 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700443 public int getLinkDownstreamBandwidthKbps() {
444 return mLinkDownBandwidthKbps;
445 }
446
447 private void combineLinkBandwidths(NetworkCapabilities nc) {
448 this.mLinkUpBandwidthKbps =
449 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
450 this.mLinkDownBandwidthKbps =
451 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
452 }
453 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
454 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
455 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
456 }
457 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
458 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
459 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
460 }
461
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700462 private String mNetworkSpecifier;
463 /**
464 * Sets the optional bearer specific network specifier.
465 * This has no meaning if a single transport is also not specified, so calling
466 * this without a single transport set will generate an exception, as will
467 * subsequently adding or removing transports after this is set.
468 * </p>
469 * The interpretation of this {@code String} is bearer specific and bearers that use
470 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700471 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
472 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700473 *
474 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
475 * specific network specifier where the bearer has a choice of
476 * networks.
477 * @hide
478 */
479 public void setNetworkSpecifier(String networkSpecifier) {
480 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
481 throw new IllegalStateException("Must have a single transport specified to use " +
482 "setNetworkSpecifier");
483 }
484 mNetworkSpecifier = networkSpecifier;
485 }
486
487 /**
488 * Gets the optional bearer specific network specifier.
489 *
490 * @return The optional {@code String} specifying the bearer specific network specifier.
491 * See {@link #setNetworkSpecifier}.
492 * @hide
493 */
494 public String getNetworkSpecifier() {
495 return mNetworkSpecifier;
496 }
497
498 private void combineSpecifiers(NetworkCapabilities nc) {
499 String otherSpecifier = nc.getNetworkSpecifier();
500 if (TextUtils.isEmpty(otherSpecifier)) return;
501 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
502 throw new IllegalStateException("Can't combine two networkSpecifiers");
503 }
504 setNetworkSpecifier(otherSpecifier);
505 }
506 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
507 return (TextUtils.isEmpty(mNetworkSpecifier) ||
508 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
509 }
510 private boolean equalsSpecifier(NetworkCapabilities nc) {
511 if (TextUtils.isEmpty(mNetworkSpecifier)) {
512 return TextUtils.isEmpty(nc.mNetworkSpecifier);
513 } else {
514 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
515 }
516 }
517
Robert Greenwalt1448f052014-04-08 13:41:39 -0700518 /**
519 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
520 * {@hide}
521 */
522 public void combineCapabilities(NetworkCapabilities nc) {
523 combineNetCapabilities(nc);
524 combineTransportTypes(nc);
525 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700526 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700527 }
528
529 /**
530 * Check if our requirements are satisfied by the given Capabilities.
531 * {@hide}
532 */
533 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700534 return (nc != null &&
535 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700536 satisfiedByTransportTypes(nc) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700537 satisfiedByLinkBandwidths(nc) &&
538 satisfiedBySpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700539 }
540
541 @Override
542 public boolean equals(Object obj) {
543 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
544 NetworkCapabilities that = (NetworkCapabilities)obj;
545 return (equalsNetCapabilities(that) &&
546 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700547 equalsLinkBandwidths(that) &&
548 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700549 }
550
551 @Override
552 public int hashCode() {
553 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
554 ((int)(mNetworkCapabilities >> 32) * 3) +
555 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
556 ((int)(mTransportTypes >> 32) * 7) +
557 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700558 (mLinkDownBandwidthKbps * 13) +
559 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700560 }
561
Wink Saville4e2dea72014-09-20 11:04:03 -0700562 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700563 public int describeContents() {
564 return 0;
565 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700566 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700567 public void writeToParcel(Parcel dest, int flags) {
568 dest.writeLong(mNetworkCapabilities);
569 dest.writeLong(mTransportTypes);
570 dest.writeInt(mLinkUpBandwidthKbps);
571 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700572 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700573 }
574 public static final Creator<NetworkCapabilities> CREATOR =
575 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700576 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700577 public NetworkCapabilities createFromParcel(Parcel in) {
578 NetworkCapabilities netCap = new NetworkCapabilities();
579
580 netCap.mNetworkCapabilities = in.readLong();
581 netCap.mTransportTypes = in.readLong();
582 netCap.mLinkUpBandwidthKbps = in.readInt();
583 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700584 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700585 return netCap;
586 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700587 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700588 public NetworkCapabilities[] newArray(int size) {
589 return new NetworkCapabilities[size];
590 }
591 };
592
Wink Saville4e2dea72014-09-20 11:04:03 -0700593 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700594 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700595 int[] types = getTransportTypes();
596 String transports = (types.length > 0 ? " Transports: " : "");
597 for (int i = 0; i < types.length;) {
598 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700599 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
600 case TRANSPORT_WIFI: transports += "WIFI"; break;
601 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
602 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400603 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700604 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700605 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606 }
607
Robert Greenwalt7569f182014-06-08 16:42:59 -0700608 types = getCapabilities();
609 String capabilities = (types.length > 0 ? " Capabilities: " : "");
610 for (int i = 0; i < types.length; ) {
611 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
613 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
614 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
615 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
616 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
617 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
618 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
619 case NET_CAPABILITY_IA: capabilities += "IA"; break;
620 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
621 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
622 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
623 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
624 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
625 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700626 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400627 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900628 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700629 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700630 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700631 }
632
633 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
634 mLinkUpBandwidthKbps + "Kbps" : "");
635 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
636 mLinkDownBandwidthKbps + "Kbps" : "");
637
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700638 String specifier = (mNetworkSpecifier == null ?
639 "" : " Specifier: <" + mNetworkSpecifier + ">");
640
641 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700642 }
643}