blob: 658051c9ebf1eed7daac8d56f88a09c5b1dfcca9 [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
Paul Jensen3d194ea2015-06-16 14:27:36 -0400179 /**
180 * Indicates that this network was found to have a captive portal in place last time it was
181 * probed.
182 */
183 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
184
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Paul Jensen3d194ea2015-06-16 14:27:36 -0400186 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700187
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700188 /**
189 * Adds the given capability to this {@code NetworkCapability} instance.
190 * Multiple capabilities may be applied sequentially. Note that when searching
191 * for a network to satisfy a request, all capabilities requested must be satisfied.
192 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700193 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
194 * @return This NetworkCapability to facilitate chaining.
195 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700196 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700197 public NetworkCapabilities addCapability(int capability) {
198 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700199 throw new IllegalArgumentException("NetworkCapability out of range");
200 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700201 mNetworkCapabilities |= 1 << capability;
202 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700203 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700204
205 /**
206 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
207 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700208 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
209 * @return This NetworkCapability to facilitate chaining.
210 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700211 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700212 public NetworkCapabilities removeCapability(int capability) {
213 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700214 throw new IllegalArgumentException("NetworkCapability out of range");
215 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700216 mNetworkCapabilities &= ~(1 << capability);
217 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700218 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700219
220 /**
221 * Gets all the capabilities set on this {@code NetworkCapability} instance.
222 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700223 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700224 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700225 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700226 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700227 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700228 return enumerateBits(mNetworkCapabilities);
229 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700230
231 /**
232 * Tests for the presence of a capabilitity on this instance.
233 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700234 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700235 * @return {@code true} if set on this instance.
236 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700237 public boolean hasCapability(int capability) {
238 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700239 return false;
240 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700241 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700242 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700243
Robert Greenwalt7569f182014-06-08 16:42:59 -0700244 private int[] enumerateBits(long val) {
245 int size = Long.bitCount(val);
246 int[] result = new int[size];
247 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700248 int resource = 0;
249 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700250 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700251 val = val >> 1;
252 resource++;
253 }
254 return result;
255 }
256
257 private void combineNetCapabilities(NetworkCapabilities nc) {
258 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
259 }
260
261 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
262 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
263 }
264
Robert Greenwalt06314e42014-10-29 14:04:06 -0700265 /** @hide */
266 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700267 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
268 }
269
270 /**
271 * Representing the transport type. Apps should generally not care about transport. A
272 * request for a fast internet connection could be satisfied by a number of different
273 * transports. If any are specified here it will be satisfied a Network that matches
274 * any of them. If a caller doesn't care about the transport it should not specify any.
275 */
276 private long mTransportTypes;
277
278 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700279 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700280 */
281 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700282
283 /**
284 * Indicates this network uses a Wi-Fi transport.
285 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700286 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700287
288 /**
289 * Indicates this network uses a Bluetooth transport.
290 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700291 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700292
293 /**
294 * Indicates this network uses an Ethernet transport.
295 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700296 public static final int TRANSPORT_ETHERNET = 3;
297
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400298 /**
299 * Indicates this network uses a VPN transport.
300 */
301 public static final int TRANSPORT_VPN = 4;
302
Robert Greenwalt1448f052014-04-08 13:41:39 -0700303 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400304 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700305
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700306 /**
307 * Adds the given transport type to this {@code NetworkCapability} instance.
308 * Multiple transports may be applied sequentially. Note that when searching
309 * for a network to satisfy a request, any listed in the request will satisfy the request.
310 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
311 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
312 * to be selected. This is logically different than
313 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
314 *
315 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700316 * @return This NetworkCapability to facilitate chaining.
317 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700318 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700319 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700320 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
321 throw new IllegalArgumentException("TransportType out of range");
322 }
323 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700324 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700325 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700326 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700327
328 /**
329 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
330 *
331 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700332 * @return This NetworkCapability to facilitate chaining.
333 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700334 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700335 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700336 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
337 throw new IllegalArgumentException("TransportType out of range");
338 }
339 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700340 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700341 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700342 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700343
344 /**
345 * Gets all the transports set on this {@code NetworkCapability} instance.
346 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700347 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700348 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700349 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700350 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700351 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700352 return enumerateBits(mTransportTypes);
353 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700354
355 /**
356 * Tests for the presence of a transport on this instance.
357 *
358 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
359 * @return {@code true} if set on this instance.
360 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700361 public boolean hasTransport(int transportType) {
362 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
363 return false;
364 }
365 return ((mTransportTypes & (1 << transportType)) != 0);
366 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700367
368 private void combineTransportTypes(NetworkCapabilities nc) {
369 this.mTransportTypes |= nc.mTransportTypes;
370 }
371 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
372 return ((this.mTransportTypes == 0) ||
373 ((this.mTransportTypes & nc.mTransportTypes) != 0));
374 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700375 /** @hide */
376 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700377 return (nc.mTransportTypes == this.mTransportTypes);
378 }
379
380 /**
381 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
382 * for the first hop on the given transport. It is not measured, but may take into account
383 * link parameters (Radio technology, allocated channels, etc).
384 */
385 private int mLinkUpBandwidthKbps;
386 private int mLinkDownBandwidthKbps;
387
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700388 /**
389 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
390 * the estimated first hop transport bandwidth.
391 * <p>
392 * Note that when used to request a network, this specifies the minimum acceptable.
393 * When received as the state of an existing network this specifies the typical
394 * first hop bandwidth expected. This is never measured, but rather is inferred
395 * from technology type and other link parameters. It could be used to differentiate
396 * between very slow 1xRTT cellular links and other faster networks or even between
397 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
398 * fast backhauls and slow backhauls.
399 *
400 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700401 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700402 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700403 public void setLinkUpstreamBandwidthKbps(int upKbps) {
404 mLinkUpBandwidthKbps = upKbps;
405 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700406
407 /**
408 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
409 * the estimated first hop transport bandwidth.
410 *
411 * @return The estimated first hop upstream (device to network) bandwidth.
412 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700413 public int getLinkUpstreamBandwidthKbps() {
414 return mLinkUpBandwidthKbps;
415 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700416
417 /**
418 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
419 * the estimated first hop transport bandwidth.
420 * <p>
421 * Note that when used to request a network, this specifies the minimum acceptable.
422 * When received as the state of an existing network this specifies the typical
423 * first hop bandwidth expected. This is never measured, but rather is inferred
424 * from technology type and other link parameters. It could be used to differentiate
425 * between very slow 1xRTT cellular links and other faster networks or even between
426 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
427 * fast backhauls and slow backhauls.
428 *
429 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700430 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700431 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700432 public void setLinkDownstreamBandwidthKbps(int downKbps) {
433 mLinkDownBandwidthKbps = downKbps;
434 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700435
436 /**
437 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
438 * the estimated first hop transport bandwidth.
439 *
440 * @return The estimated first hop downstream (network to device) bandwidth.
441 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700442 public int getLinkDownstreamBandwidthKbps() {
443 return mLinkDownBandwidthKbps;
444 }
445
446 private void combineLinkBandwidths(NetworkCapabilities nc) {
447 this.mLinkUpBandwidthKbps =
448 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
449 this.mLinkDownBandwidthKbps =
450 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
451 }
452 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
453 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
454 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
455 }
456 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
457 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
458 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
459 }
460
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700461 private String mNetworkSpecifier;
462 /**
463 * Sets the optional bearer specific network specifier.
464 * This has no meaning if a single transport is also not specified, so calling
465 * this without a single transport set will generate an exception, as will
466 * subsequently adding or removing transports after this is set.
467 * </p>
468 * The interpretation of this {@code String} is bearer specific and bearers that use
469 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700470 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
471 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700472 *
473 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
474 * specific network specifier where the bearer has a choice of
475 * networks.
476 * @hide
477 */
478 public void setNetworkSpecifier(String networkSpecifier) {
479 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
480 throw new IllegalStateException("Must have a single transport specified to use " +
481 "setNetworkSpecifier");
482 }
483 mNetworkSpecifier = networkSpecifier;
484 }
485
486 /**
487 * Gets the optional bearer specific network specifier.
488 *
489 * @return The optional {@code String} specifying the bearer specific network specifier.
490 * See {@link #setNetworkSpecifier}.
491 * @hide
492 */
493 public String getNetworkSpecifier() {
494 return mNetworkSpecifier;
495 }
496
497 private void combineSpecifiers(NetworkCapabilities nc) {
498 String otherSpecifier = nc.getNetworkSpecifier();
499 if (TextUtils.isEmpty(otherSpecifier)) return;
500 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
501 throw new IllegalStateException("Can't combine two networkSpecifiers");
502 }
503 setNetworkSpecifier(otherSpecifier);
504 }
505 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
506 return (TextUtils.isEmpty(mNetworkSpecifier) ||
507 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
508 }
509 private boolean equalsSpecifier(NetworkCapabilities nc) {
510 if (TextUtils.isEmpty(mNetworkSpecifier)) {
511 return TextUtils.isEmpty(nc.mNetworkSpecifier);
512 } else {
513 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
514 }
515 }
516
Robert Greenwalt1448f052014-04-08 13:41:39 -0700517 /**
518 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
519 * {@hide}
520 */
521 public void combineCapabilities(NetworkCapabilities nc) {
522 combineNetCapabilities(nc);
523 combineTransportTypes(nc);
524 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700525 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700526 }
527
528 /**
529 * Check if our requirements are satisfied by the given Capabilities.
530 * {@hide}
531 */
532 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700533 return (nc != null &&
534 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700535 satisfiedByTransportTypes(nc) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700536 satisfiedByLinkBandwidths(nc) &&
537 satisfiedBySpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700538 }
539
540 @Override
541 public boolean equals(Object obj) {
542 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
543 NetworkCapabilities that = (NetworkCapabilities)obj;
544 return (equalsNetCapabilities(that) &&
545 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700546 equalsLinkBandwidths(that) &&
547 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700548 }
549
550 @Override
551 public int hashCode() {
552 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
553 ((int)(mNetworkCapabilities >> 32) * 3) +
554 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
555 ((int)(mTransportTypes >> 32) * 7) +
556 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700557 (mLinkDownBandwidthKbps * 13) +
558 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700559 }
560
Wink Saville4e2dea72014-09-20 11:04:03 -0700561 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700562 public int describeContents() {
563 return 0;
564 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700565 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700566 public void writeToParcel(Parcel dest, int flags) {
567 dest.writeLong(mNetworkCapabilities);
568 dest.writeLong(mTransportTypes);
569 dest.writeInt(mLinkUpBandwidthKbps);
570 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700571 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700572 }
573 public static final Creator<NetworkCapabilities> CREATOR =
574 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700575 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700576 public NetworkCapabilities createFromParcel(Parcel in) {
577 NetworkCapabilities netCap = new NetworkCapabilities();
578
579 netCap.mNetworkCapabilities = in.readLong();
580 netCap.mTransportTypes = in.readLong();
581 netCap.mLinkUpBandwidthKbps = in.readInt();
582 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700583 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 return netCap;
585 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700586 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700587 public NetworkCapabilities[] newArray(int size) {
588 return new NetworkCapabilities[size];
589 }
590 };
591
Wink Saville4e2dea72014-09-20 11:04:03 -0700592 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700593 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700594 int[] types = getTransportTypes();
595 String transports = (types.length > 0 ? " Transports: " : "");
596 for (int i = 0; i < types.length;) {
597 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700598 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
599 case TRANSPORT_WIFI: transports += "WIFI"; break;
600 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
601 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400602 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700603 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700604 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700605 }
606
Robert Greenwalt7569f182014-06-08 16:42:59 -0700607 types = getCapabilities();
608 String capabilities = (types.length > 0 ? " Capabilities: " : "");
609 for (int i = 0; i < types.length; ) {
610 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700611 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
612 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
613 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
614 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
615 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
616 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
617 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
618 case NET_CAPABILITY_IA: capabilities += "IA"; break;
619 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
620 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
621 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
622 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
623 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
624 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700625 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400626 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900627 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700628 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700629 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700630 }
631
632 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
633 mLinkUpBandwidthKbps + "Kbps" : "");
634 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
635 mLinkDownBandwidthKbps + "Kbps" : "");
636
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700637 String specifier = (mNetworkSpecifier == null ?
638 "" : " Specifier: <" + mNetworkSpecifier + ">");
639
640 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700641 }
642}