blob: bf94b25924f19c16e31bb3007414cca0dce27ef0 [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
136 * Emergency IMS servers, used for network signaling during emergency calls.
137 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700139
140 /**
141 * Indicates that this network is unmetered.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates that this network should be able to reach the internet.
147 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700148 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700149
150 /**
151 * Indicates that this network is available for general use. If this is not set
152 * applications should not attempt to communicate on this network. Note that this
153 * is simply informative and not enforcement - enforcement is handled via other means.
154 * Set by default.
155 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700156 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
157
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700158 /**
159 * Indicates that the user has indicated implicit trust of this network. This
160 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
161 * BT device or a wifi the user asked to connect to. Untrusted networks
162 * are probably limited to unknown wifi AP. Set by default.
163 */
164 public static final int NET_CAPABILITY_TRUSTED = 14;
165
Paul Jensen76b610a2015-03-18 09:33:07 -0400166 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400167 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400168 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400169 */
170 public static final int NET_CAPABILITY_NOT_VPN = 15;
171
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900172 /**
173 * Indicates that connectivity on this network was successfully validated. For example, for a
174 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
175 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900176 */
177 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700178
Robert Greenwalt1448f052014-04-08 13:41:39 -0700179 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900180 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_VALIDATED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700181
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700182 /**
183 * Adds the given capability to this {@code NetworkCapability} instance.
184 * Multiple capabilities may be applied sequentially. Note that when searching
185 * for a network to satisfy a request, all capabilities requested must be satisfied.
186 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700187 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
188 * @return This NetworkCapability to facilitate chaining.
189 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700190 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700191 public NetworkCapabilities addCapability(int capability) {
192 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700193 throw new IllegalArgumentException("NetworkCapability out of range");
194 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700195 mNetworkCapabilities |= 1 << capability;
196 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700197 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700198
199 /**
200 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
201 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700202 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
203 * @return This NetworkCapability to facilitate chaining.
204 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700205 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700206 public NetworkCapabilities removeCapability(int capability) {
207 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700208 throw new IllegalArgumentException("NetworkCapability out of range");
209 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700210 mNetworkCapabilities &= ~(1 << capability);
211 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700212 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700213
214 /**
215 * Gets all the capabilities set on this {@code NetworkCapability} instance.
216 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700218 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700219 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700220 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700221 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700222 return enumerateBits(mNetworkCapabilities);
223 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700224
225 /**
226 * Tests for the presence of a capabilitity on this instance.
227 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700228 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700229 * @return {@code true} if set on this instance.
230 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700231 public boolean hasCapability(int capability) {
232 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700233 return false;
234 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700235 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700236 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700237
Robert Greenwalt7569f182014-06-08 16:42:59 -0700238 private int[] enumerateBits(long val) {
239 int size = Long.bitCount(val);
240 int[] result = new int[size];
241 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700242 int resource = 0;
243 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700244 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700245 val = val >> 1;
246 resource++;
247 }
248 return result;
249 }
250
251 private void combineNetCapabilities(NetworkCapabilities nc) {
252 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
253 }
254
255 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
256 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
257 }
258
Robert Greenwalt06314e42014-10-29 14:04:06 -0700259 /** @hide */
260 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700261 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
262 }
263
264 /**
265 * Representing the transport type. Apps should generally not care about transport. A
266 * request for a fast internet connection could be satisfied by a number of different
267 * transports. If any are specified here it will be satisfied a Network that matches
268 * any of them. If a caller doesn't care about the transport it should not specify any.
269 */
270 private long mTransportTypes;
271
272 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700273 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700274 */
275 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700276
277 /**
278 * Indicates this network uses a Wi-Fi transport.
279 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700280 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700281
282 /**
283 * Indicates this network uses a Bluetooth transport.
284 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700285 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700286
287 /**
288 * Indicates this network uses an Ethernet transport.
289 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700290 public static final int TRANSPORT_ETHERNET = 3;
291
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400292 /**
293 * Indicates this network uses a VPN transport.
294 */
295 public static final int TRANSPORT_VPN = 4;
296
Robert Greenwalt1448f052014-04-08 13:41:39 -0700297 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400298 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700299
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700300 /**
301 * Adds the given transport type to this {@code NetworkCapability} instance.
302 * Multiple transports may be applied sequentially. Note that when searching
303 * for a network to satisfy a request, any listed in the request will satisfy the request.
304 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
305 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
306 * to be selected. This is logically different than
307 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
308 *
309 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700310 * @return This NetworkCapability to facilitate chaining.
311 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700312 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700313 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700314 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
315 throw new IllegalArgumentException("TransportType out of range");
316 }
317 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700318 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700319 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700320 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700321
322 /**
323 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
324 *
325 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700326 * @return This NetworkCapability to facilitate chaining.
327 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700328 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700329 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700330 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
331 throw new IllegalArgumentException("TransportType out of range");
332 }
333 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700334 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700335 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700336 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700337
338 /**
339 * Gets all the transports set on this {@code NetworkCapability} instance.
340 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700341 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700342 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700343 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700344 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700345 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700346 return enumerateBits(mTransportTypes);
347 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700348
349 /**
350 * Tests for the presence of a transport on this instance.
351 *
352 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
353 * @return {@code true} if set on this instance.
354 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700355 public boolean hasTransport(int transportType) {
356 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
357 return false;
358 }
359 return ((mTransportTypes & (1 << transportType)) != 0);
360 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700361
362 private void combineTransportTypes(NetworkCapabilities nc) {
363 this.mTransportTypes |= nc.mTransportTypes;
364 }
365 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
366 return ((this.mTransportTypes == 0) ||
367 ((this.mTransportTypes & nc.mTransportTypes) != 0));
368 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700369 /** @hide */
370 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700371 return (nc.mTransportTypes == this.mTransportTypes);
372 }
373
374 /**
375 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
376 * for the first hop on the given transport. It is not measured, but may take into account
377 * link parameters (Radio technology, allocated channels, etc).
378 */
379 private int mLinkUpBandwidthKbps;
380 private int mLinkDownBandwidthKbps;
381
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700382 /**
383 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
384 * the estimated first hop transport bandwidth.
385 * <p>
386 * Note that when used to request a network, this specifies the minimum acceptable.
387 * When received as the state of an existing network this specifies the typical
388 * first hop bandwidth expected. This is never measured, but rather is inferred
389 * from technology type and other link parameters. It could be used to differentiate
390 * between very slow 1xRTT cellular links and other faster networks or even between
391 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
392 * fast backhauls and slow backhauls.
393 *
394 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700395 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700396 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700397 public void setLinkUpstreamBandwidthKbps(int upKbps) {
398 mLinkUpBandwidthKbps = upKbps;
399 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700400
401 /**
402 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
403 * the estimated first hop transport bandwidth.
404 *
405 * @return The estimated first hop upstream (device to network) bandwidth.
406 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700407 public int getLinkUpstreamBandwidthKbps() {
408 return mLinkUpBandwidthKbps;
409 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700410
411 /**
412 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
413 * the estimated first hop transport bandwidth.
414 * <p>
415 * Note that when used to request a network, this specifies the minimum acceptable.
416 * When received as the state of an existing network this specifies the typical
417 * first hop bandwidth expected. This is never measured, but rather is inferred
418 * from technology type and other link parameters. It could be used to differentiate
419 * between very slow 1xRTT cellular links and other faster networks or even between
420 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
421 * fast backhauls and slow backhauls.
422 *
423 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700424 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700425 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700426 public void setLinkDownstreamBandwidthKbps(int downKbps) {
427 mLinkDownBandwidthKbps = downKbps;
428 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700429
430 /**
431 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
432 * the estimated first hop transport bandwidth.
433 *
434 * @return The estimated first hop downstream (network to device) bandwidth.
435 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700436 public int getLinkDownstreamBandwidthKbps() {
437 return mLinkDownBandwidthKbps;
438 }
439
440 private void combineLinkBandwidths(NetworkCapabilities nc) {
441 this.mLinkUpBandwidthKbps =
442 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
443 this.mLinkDownBandwidthKbps =
444 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
445 }
446 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
447 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
448 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
449 }
450 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
451 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
452 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
453 }
454
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700455 private String mNetworkSpecifier;
456 /**
457 * Sets the optional bearer specific network specifier.
458 * This has no meaning if a single transport is also not specified, so calling
459 * this without a single transport set will generate an exception, as will
460 * subsequently adding or removing transports after this is set.
461 * </p>
462 * The interpretation of this {@code String} is bearer specific and bearers that use
463 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700464 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
465 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700466 *
467 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
468 * specific network specifier where the bearer has a choice of
469 * networks.
470 * @hide
471 */
472 public void setNetworkSpecifier(String networkSpecifier) {
473 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
474 throw new IllegalStateException("Must have a single transport specified to use " +
475 "setNetworkSpecifier");
476 }
477 mNetworkSpecifier = networkSpecifier;
478 }
479
480 /**
481 * Gets the optional bearer specific network specifier.
482 *
483 * @return The optional {@code String} specifying the bearer specific network specifier.
484 * See {@link #setNetworkSpecifier}.
485 * @hide
486 */
487 public String getNetworkSpecifier() {
488 return mNetworkSpecifier;
489 }
490
491 private void combineSpecifiers(NetworkCapabilities nc) {
492 String otherSpecifier = nc.getNetworkSpecifier();
493 if (TextUtils.isEmpty(otherSpecifier)) return;
494 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
495 throw new IllegalStateException("Can't combine two networkSpecifiers");
496 }
497 setNetworkSpecifier(otherSpecifier);
498 }
499 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
500 return (TextUtils.isEmpty(mNetworkSpecifier) ||
501 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
502 }
503 private boolean equalsSpecifier(NetworkCapabilities nc) {
504 if (TextUtils.isEmpty(mNetworkSpecifier)) {
505 return TextUtils.isEmpty(nc.mNetworkSpecifier);
506 } else {
507 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
508 }
509 }
510
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 /**
512 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
513 * {@hide}
514 */
515 public void combineCapabilities(NetworkCapabilities nc) {
516 combineNetCapabilities(nc);
517 combineTransportTypes(nc);
518 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700519 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700520 }
521
522 /**
523 * Check if our requirements are satisfied by the given Capabilities.
524 * {@hide}
525 */
526 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700527 return (nc != null &&
528 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700529 satisfiedByTransportTypes(nc) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700530 satisfiedByLinkBandwidths(nc) &&
531 satisfiedBySpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700532 }
533
534 @Override
535 public boolean equals(Object obj) {
536 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
537 NetworkCapabilities that = (NetworkCapabilities)obj;
538 return (equalsNetCapabilities(that) &&
539 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700540 equalsLinkBandwidths(that) &&
541 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700542 }
543
544 @Override
545 public int hashCode() {
546 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
547 ((int)(mNetworkCapabilities >> 32) * 3) +
548 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
549 ((int)(mTransportTypes >> 32) * 7) +
550 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700551 (mLinkDownBandwidthKbps * 13) +
552 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700553 }
554
Wink Saville4e2dea72014-09-20 11:04:03 -0700555 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700556 public int describeContents() {
557 return 0;
558 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700559 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700560 public void writeToParcel(Parcel dest, int flags) {
561 dest.writeLong(mNetworkCapabilities);
562 dest.writeLong(mTransportTypes);
563 dest.writeInt(mLinkUpBandwidthKbps);
564 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700565 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700566 }
567 public static final Creator<NetworkCapabilities> CREATOR =
568 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700569 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700570 public NetworkCapabilities createFromParcel(Parcel in) {
571 NetworkCapabilities netCap = new NetworkCapabilities();
572
573 netCap.mNetworkCapabilities = in.readLong();
574 netCap.mTransportTypes = in.readLong();
575 netCap.mLinkUpBandwidthKbps = in.readInt();
576 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700577 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700578 return netCap;
579 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700580 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700581 public NetworkCapabilities[] newArray(int size) {
582 return new NetworkCapabilities[size];
583 }
584 };
585
Wink Saville4e2dea72014-09-20 11:04:03 -0700586 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700587 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700588 int[] types = getTransportTypes();
589 String transports = (types.length > 0 ? " Transports: " : "");
590 for (int i = 0; i < types.length;) {
591 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700592 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
593 case TRANSPORT_WIFI: transports += "WIFI"; break;
594 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
595 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400596 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700597 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700598 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700599 }
600
Robert Greenwalt7569f182014-06-08 16:42:59 -0700601 types = getCapabilities();
602 String capabilities = (types.length > 0 ? " Capabilities: " : "");
603 for (int i = 0; i < types.length; ) {
604 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700605 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
606 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
607 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
608 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
609 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
610 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
611 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
612 case NET_CAPABILITY_IA: capabilities += "IA"; break;
613 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
614 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
615 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
616 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
617 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
618 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700619 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400620 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900621 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700622 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700623 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700624 }
625
626 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
627 mLinkUpBandwidthKbps + "Kbps" : "");
628 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
629 mLinkDownBandwidthKbps + "Kbps" : "");
630
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700631 String specifier = (mNetworkSpecifier == null ?
632 "" : " Specifier: <" + mNetworkSpecifier + ">");
633
634 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700635 }
636}