blob: ab70485d38bc950cf633f75826a042855d398140 [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.
176 * @hide
177 */
178 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700179
Robert Greenwalt1448f052014-04-08 13:41:39 -0700180 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900181 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_VALIDATED;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700182
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700183 /**
184 * Adds the given capability to this {@code NetworkCapability} instance.
185 * Multiple capabilities may be applied sequentially. Note that when searching
186 * for a network to satisfy a request, all capabilities requested must be satisfied.
187 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700188 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
189 * @return This NetworkCapability to facilitate chaining.
190 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700191 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700192 public NetworkCapabilities addCapability(int capability) {
193 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700194 throw new IllegalArgumentException("NetworkCapability out of range");
195 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700196 mNetworkCapabilities |= 1 << capability;
197 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700198 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199
200 /**
201 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
202 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700203 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
204 * @return This NetworkCapability to facilitate chaining.
205 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700206 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700207 public NetworkCapabilities removeCapability(int capability) {
208 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700209 throw new IllegalArgumentException("NetworkCapability out of range");
210 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700211 mNetworkCapabilities &= ~(1 << capability);
212 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700213 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700214
215 /**
216 * Gets all the capabilities set on this {@code NetworkCapability} instance.
217 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700218 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700219 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700220 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700221 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700222 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700223 return enumerateBits(mNetworkCapabilities);
224 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700225
226 /**
227 * Tests for the presence of a capabilitity on this instance.
228 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700229 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700230 * @return {@code true} if set on this instance.
231 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700232 public boolean hasCapability(int capability) {
233 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700234 return false;
235 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700236 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700237 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700238
Robert Greenwalt7569f182014-06-08 16:42:59 -0700239 private int[] enumerateBits(long val) {
240 int size = Long.bitCount(val);
241 int[] result = new int[size];
242 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700243 int resource = 0;
244 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700245 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700246 val = val >> 1;
247 resource++;
248 }
249 return result;
250 }
251
252 private void combineNetCapabilities(NetworkCapabilities nc) {
253 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
254 }
255
256 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
257 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
258 }
259
Robert Greenwalt06314e42014-10-29 14:04:06 -0700260 /** @hide */
261 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700262 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
263 }
264
265 /**
266 * Representing the transport type. Apps should generally not care about transport. A
267 * request for a fast internet connection could be satisfied by a number of different
268 * transports. If any are specified here it will be satisfied a Network that matches
269 * any of them. If a caller doesn't care about the transport it should not specify any.
270 */
271 private long mTransportTypes;
272
273 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700274 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700275 */
276 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700277
278 /**
279 * Indicates this network uses a Wi-Fi transport.
280 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700281 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700282
283 /**
284 * Indicates this network uses a Bluetooth transport.
285 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700286 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700287
288 /**
289 * Indicates this network uses an Ethernet transport.
290 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700291 public static final int TRANSPORT_ETHERNET = 3;
292
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400293 /**
294 * Indicates this network uses a VPN transport.
295 */
296 public static final int TRANSPORT_VPN = 4;
297
Robert Greenwalt1448f052014-04-08 13:41:39 -0700298 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400299 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700300
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700301 /**
302 * Adds the given transport type to this {@code NetworkCapability} instance.
303 * Multiple transports may be applied sequentially. Note that when searching
304 * for a network to satisfy a request, any listed in the request will satisfy the request.
305 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
306 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
307 * to be selected. This is logically different than
308 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
309 *
310 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700311 * @return This NetworkCapability to facilitate chaining.
312 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700313 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700314 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700315 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
316 throw new IllegalArgumentException("TransportType out of range");
317 }
318 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700319 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700320 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700321 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700322
323 /**
324 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
325 *
326 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700327 * @return This NetworkCapability to facilitate chaining.
328 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700329 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700330 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700331 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
332 throw new IllegalArgumentException("TransportType out of range");
333 }
334 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700335 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700336 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700337 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700338
339 /**
340 * Gets all the transports set on this {@code NetworkCapability} instance.
341 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700342 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700343 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700344 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700345 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700346 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700347 return enumerateBits(mTransportTypes);
348 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700349
350 /**
351 * Tests for the presence of a transport on this instance.
352 *
353 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
354 * @return {@code true} if set on this instance.
355 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700356 public boolean hasTransport(int transportType) {
357 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
358 return false;
359 }
360 return ((mTransportTypes & (1 << transportType)) != 0);
361 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700362
363 private void combineTransportTypes(NetworkCapabilities nc) {
364 this.mTransportTypes |= nc.mTransportTypes;
365 }
366 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
367 return ((this.mTransportTypes == 0) ||
368 ((this.mTransportTypes & nc.mTransportTypes) != 0));
369 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700370 /** @hide */
371 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700372 return (nc.mTransportTypes == this.mTransportTypes);
373 }
374
375 /**
376 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
377 * for the first hop on the given transport. It is not measured, but may take into account
378 * link parameters (Radio technology, allocated channels, etc).
379 */
380 private int mLinkUpBandwidthKbps;
381 private int mLinkDownBandwidthKbps;
382
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700383 /**
384 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
385 * the estimated first hop transport bandwidth.
386 * <p>
387 * Note that when used to request a network, this specifies the minimum acceptable.
388 * When received as the state of an existing network this specifies the typical
389 * first hop bandwidth expected. This is never measured, but rather is inferred
390 * from technology type and other link parameters. It could be used to differentiate
391 * between very slow 1xRTT cellular links and other faster networks or even between
392 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
393 * fast backhauls and slow backhauls.
394 *
395 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700396 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700397 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700398 public void setLinkUpstreamBandwidthKbps(int upKbps) {
399 mLinkUpBandwidthKbps = upKbps;
400 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700401
402 /**
403 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
404 * the estimated first hop transport bandwidth.
405 *
406 * @return The estimated first hop upstream (device to network) bandwidth.
407 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700408 public int getLinkUpstreamBandwidthKbps() {
409 return mLinkUpBandwidthKbps;
410 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700411
412 /**
413 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
414 * the estimated first hop transport bandwidth.
415 * <p>
416 * Note that when used to request a network, this specifies the minimum acceptable.
417 * When received as the state of an existing network this specifies the typical
418 * first hop bandwidth expected. This is never measured, but rather is inferred
419 * from technology type and other link parameters. It could be used to differentiate
420 * between very slow 1xRTT cellular links and other faster networks or even between
421 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
422 * fast backhauls and slow backhauls.
423 *
424 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700425 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700426 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700427 public void setLinkDownstreamBandwidthKbps(int downKbps) {
428 mLinkDownBandwidthKbps = downKbps;
429 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700430
431 /**
432 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
433 * the estimated first hop transport bandwidth.
434 *
435 * @return The estimated first hop downstream (network to device) bandwidth.
436 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700437 public int getLinkDownstreamBandwidthKbps() {
438 return mLinkDownBandwidthKbps;
439 }
440
441 private void combineLinkBandwidths(NetworkCapabilities nc) {
442 this.mLinkUpBandwidthKbps =
443 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
444 this.mLinkDownBandwidthKbps =
445 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
446 }
447 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
448 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
449 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
450 }
451 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
452 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
453 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
454 }
455
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700456 private String mNetworkSpecifier;
457 /**
458 * Sets the optional bearer specific network specifier.
459 * This has no meaning if a single transport is also not specified, so calling
460 * this without a single transport set will generate an exception, as will
461 * subsequently adding or removing transports after this is set.
462 * </p>
463 * The interpretation of this {@code String} is bearer specific and bearers that use
464 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700465 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
466 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700467 *
468 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
469 * specific network specifier where the bearer has a choice of
470 * networks.
471 * @hide
472 */
473 public void setNetworkSpecifier(String networkSpecifier) {
474 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
475 throw new IllegalStateException("Must have a single transport specified to use " +
476 "setNetworkSpecifier");
477 }
478 mNetworkSpecifier = networkSpecifier;
479 }
480
481 /**
482 * Gets the optional bearer specific network specifier.
483 *
484 * @return The optional {@code String} specifying the bearer specific network specifier.
485 * See {@link #setNetworkSpecifier}.
486 * @hide
487 */
488 public String getNetworkSpecifier() {
489 return mNetworkSpecifier;
490 }
491
492 private void combineSpecifiers(NetworkCapabilities nc) {
493 String otherSpecifier = nc.getNetworkSpecifier();
494 if (TextUtils.isEmpty(otherSpecifier)) return;
495 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
496 throw new IllegalStateException("Can't combine two networkSpecifiers");
497 }
498 setNetworkSpecifier(otherSpecifier);
499 }
500 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
501 return (TextUtils.isEmpty(mNetworkSpecifier) ||
502 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
503 }
504 private boolean equalsSpecifier(NetworkCapabilities nc) {
505 if (TextUtils.isEmpty(mNetworkSpecifier)) {
506 return TextUtils.isEmpty(nc.mNetworkSpecifier);
507 } else {
508 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
509 }
510 }
511
Robert Greenwalt1448f052014-04-08 13:41:39 -0700512 /**
513 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
514 * {@hide}
515 */
516 public void combineCapabilities(NetworkCapabilities nc) {
517 combineNetCapabilities(nc);
518 combineTransportTypes(nc);
519 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700520 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700521 }
522
523 /**
524 * Check if our requirements are satisfied by the given Capabilities.
525 * {@hide}
526 */
527 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700528 return (nc != null &&
529 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700530 satisfiedByTransportTypes(nc) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700531 satisfiedByLinkBandwidths(nc) &&
532 satisfiedBySpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700533 }
534
535 @Override
536 public boolean equals(Object obj) {
537 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
538 NetworkCapabilities that = (NetworkCapabilities)obj;
539 return (equalsNetCapabilities(that) &&
540 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700541 equalsLinkBandwidths(that) &&
542 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700543 }
544
545 @Override
546 public int hashCode() {
547 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
548 ((int)(mNetworkCapabilities >> 32) * 3) +
549 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
550 ((int)(mTransportTypes >> 32) * 7) +
551 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700552 (mLinkDownBandwidthKbps * 13) +
553 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700554 }
555
Wink Saville4e2dea72014-09-20 11:04:03 -0700556 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700557 public int describeContents() {
558 return 0;
559 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700560 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700561 public void writeToParcel(Parcel dest, int flags) {
562 dest.writeLong(mNetworkCapabilities);
563 dest.writeLong(mTransportTypes);
564 dest.writeInt(mLinkUpBandwidthKbps);
565 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700566 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700567 }
568 public static final Creator<NetworkCapabilities> CREATOR =
569 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700570 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 public NetworkCapabilities createFromParcel(Parcel in) {
572 NetworkCapabilities netCap = new NetworkCapabilities();
573
574 netCap.mNetworkCapabilities = in.readLong();
575 netCap.mTransportTypes = in.readLong();
576 netCap.mLinkUpBandwidthKbps = in.readInt();
577 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700578 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700579 return netCap;
580 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700581 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700582 public NetworkCapabilities[] newArray(int size) {
583 return new NetworkCapabilities[size];
584 }
585 };
586
Wink Saville4e2dea72014-09-20 11:04:03 -0700587 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700588 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700589 int[] types = getTransportTypes();
590 String transports = (types.length > 0 ? " Transports: " : "");
591 for (int i = 0; i < types.length;) {
592 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700593 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
594 case TRANSPORT_WIFI: transports += "WIFI"; break;
595 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
596 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400597 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700598 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700599 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700600 }
601
Robert Greenwalt7569f182014-06-08 16:42:59 -0700602 types = getCapabilities();
603 String capabilities = (types.length > 0 ? " Capabilities: " : "");
604 for (int i = 0; i < types.length; ) {
605 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
607 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
608 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
609 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
610 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
611 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
612 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
613 case NET_CAPABILITY_IA: capabilities += "IA"; break;
614 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
615 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
616 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
617 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
618 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
619 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700620 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400621 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; 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}