blob: 29b063a49f288bb1dfa2528709e4a1e16ffc2124 [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();
Paul Jensen487ffe72015-07-24 15:57:11 -040041 mNetworkCapabilities = DEFAULT_CAPABILITIES;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070042 }
43
44 public NetworkCapabilities(NetworkCapabilities nc) {
45 if (nc != null) {
46 mNetworkCapabilities = nc.mNetworkCapabilities;
47 mTransportTypes = nc.mTransportTypes;
48 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
49 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070050 mNetworkSpecifier = nc.mNetworkSpecifier;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070051 }
52 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070053
54 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090055 * Completely clears the contents of this object, removing even the capabilities that are set
56 * by default when the object is constructed.
57 * @hide
58 */
59 public void clearAll() {
60 mNetworkCapabilities = mTransportTypes = 0;
61 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
62 mNetworkSpecifier = null;
63 }
64
65 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070066 * Represents the network's capabilities. If any are specified they will be satisfied
67 * by any Network that matches all of them.
68 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090069 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070070
71 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070072 * Indicates this is a network that has the ability to reach the
73 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070074 */
75 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070076
77 /**
78 * Indicates this is a network that has the ability to reach the carrier's
79 * SUPL server, used to retrieve GPS information.
80 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070081 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070082
83 /**
84 * Indicates this is a network that has the ability to reach the carrier's
85 * DUN or tethering gateway.
86 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070087 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070088
89 /**
90 * Indicates this is a network that has the ability to reach the carrier's
91 * FOTA portal, used for over the air updates.
92 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070093 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070094
95 /**
96 * Indicates this is a network that has the ability to reach the carrier's
97 * IMS servers, used for network registration and signaling.
98 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070099 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700100
101 /**
102 * Indicates this is a network that has the ability to reach the carrier's
103 * CBS servers, used for carrier specific services.
104 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700105 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700106
107 /**
108 * Indicates this is a network that has the ability to reach a Wi-Fi direct
109 * peer.
110 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700111 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700112
113 /**
114 * Indicates this is a network that has the ability to reach a carrier's
115 * Initial Attach servers.
116 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700117 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700118
119 /**
120 * Indicates this is a network that has the ability to reach a carrier's
121 * RCS servers, used for Rich Communication Services.
122 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700123 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700124
125 /**
126 * Indicates this is a network that has the ability to reach a carrier's
127 * XCAP servers, used for configuration and control.
128 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700129 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700130
131 /**
132 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700133 * Emergency IMS servers or other services, used for network signaling
134 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700135 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700136 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700137
138 /**
139 * Indicates that this network is unmetered.
140 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700141 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700142
143 /**
144 * Indicates that this network should be able to reach the internet.
145 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700146 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700147
148 /**
149 * Indicates that this network is available for general use. If this is not set
150 * applications should not attempt to communicate on this network. Note that this
151 * is simply informative and not enforcement - enforcement is handled via other means.
152 * Set by default.
153 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700154 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
155
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700156 /**
157 * Indicates that the user has indicated implicit trust of this network. This
158 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
159 * BT device or a wifi the user asked to connect to. Untrusted networks
160 * are probably limited to unknown wifi AP. Set by default.
161 */
162 public static final int NET_CAPABILITY_TRUSTED = 14;
163
Paul Jensen76b610a2015-03-18 09:33:07 -0400164 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400165 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400166 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400167 */
168 public static final int NET_CAPABILITY_NOT_VPN = 15;
169
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900170 /**
171 * Indicates that connectivity on this network was successfully validated. For example, for a
172 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
173 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900174 */
175 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700176
Paul Jensen3d194ea2015-06-16 14:27:36 -0400177 /**
178 * Indicates that this network was found to have a captive portal in place last time it was
179 * probed.
180 */
181 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
182
Robert Greenwalt1448f052014-04-08 13:41:39 -0700183 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Paul Jensen3d194ea2015-06-16 14:27:36 -0400184 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700186 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400187 * Capabilities that are set by default when the object is constructed.
188 */
189 private static final long DEFAULT_CAPABILITIES =
190 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
191 (1 << NET_CAPABILITY_TRUSTED) |
192 (1 << NET_CAPABILITY_NOT_VPN);
193
194 /**
195 * Capabilities that suggest that a network is restricted.
196 * {@see #maybeMarkCapabilitiesRestricted}.
197 */
198 private static final long RESTRICTED_CAPABILITIES =
199 (1 << NET_CAPABILITY_CBS) |
200 (1 << NET_CAPABILITY_DUN) |
201 (1 << NET_CAPABILITY_EIMS) |
202 (1 << NET_CAPABILITY_FOTA) |
203 (1 << NET_CAPABILITY_IA) |
204 (1 << NET_CAPABILITY_IMS) |
205 (1 << NET_CAPABILITY_RCS) |
206 (1 << NET_CAPABILITY_XCAP);
207
208 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700209 * Adds the given capability to this {@code NetworkCapability} instance.
210 * Multiple capabilities may be applied sequentially. Note that when searching
211 * for a network to satisfy a request, all capabilities requested must be satisfied.
212 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700213 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
214 * @return This NetworkCapability to facilitate chaining.
215 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700216 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 public NetworkCapabilities addCapability(int capability) {
218 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700219 throw new IllegalArgumentException("NetworkCapability out of range");
220 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700221 mNetworkCapabilities |= 1 << capability;
222 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700223 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700224
225 /**
226 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
227 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700228 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
229 * @return This NetworkCapability to facilitate chaining.
230 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700231 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700232 public NetworkCapabilities removeCapability(int capability) {
233 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700234 throw new IllegalArgumentException("NetworkCapability out of range");
235 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700236 mNetworkCapabilities &= ~(1 << capability);
237 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700238 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700239
240 /**
241 * Gets all the capabilities set on this {@code NetworkCapability} instance.
242 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700243 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700244 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700245 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700246 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700247 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700248 return enumerateBits(mNetworkCapabilities);
249 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700250
251 /**
252 * Tests for the presence of a capabilitity on this instance.
253 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700254 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700255 * @return {@code true} if set on this instance.
256 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700257 public boolean hasCapability(int capability) {
258 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700259 return false;
260 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700261 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700262 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700263
Robert Greenwalt7569f182014-06-08 16:42:59 -0700264 private int[] enumerateBits(long val) {
265 int size = Long.bitCount(val);
266 int[] result = new int[size];
267 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700268 int resource = 0;
269 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700270 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700271 val = val >> 1;
272 resource++;
273 }
274 return result;
275 }
276
277 private void combineNetCapabilities(NetworkCapabilities nc) {
278 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
279 }
280
281 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
282 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
283 }
284
Robert Greenwalt06314e42014-10-29 14:04:06 -0700285 /** @hide */
286 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700287 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
288 }
289
290 /**
Paul Jensen487ffe72015-07-24 15:57:11 -0400291 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
292 * typically provided by restricted networks.
293 *
294 * TODO: consider:
295 * - Renaming it to guessRestrictedCapability and make it set the
296 * restricted capability bit in addition to clearing it.
297 * @hide
298 */
299 public void maybeMarkCapabilitiesRestricted() {
300 // If all the capabilities are typically provided by restricted networks, conclude that this
301 // network is restricted.
302 if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0)
303 removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
304 }
305
306 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700307 * Representing the transport type. Apps should generally not care about transport. A
308 * request for a fast internet connection could be satisfied by a number of different
309 * transports. If any are specified here it will be satisfied a Network that matches
310 * any of them. If a caller doesn't care about the transport it should not specify any.
311 */
312 private long mTransportTypes;
313
314 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700315 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700316 */
317 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700318
319 /**
320 * Indicates this network uses a Wi-Fi transport.
321 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700322 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700323
324 /**
325 * Indicates this network uses a Bluetooth transport.
326 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700327 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700328
329 /**
330 * Indicates this network uses an Ethernet transport.
331 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700332 public static final int TRANSPORT_ETHERNET = 3;
333
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400334 /**
335 * Indicates this network uses a VPN transport.
336 */
337 public static final int TRANSPORT_VPN = 4;
338
Robert Greenwalt1448f052014-04-08 13:41:39 -0700339 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400340 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700341
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700342 /**
343 * Adds the given transport type to this {@code NetworkCapability} instance.
344 * Multiple transports may be applied sequentially. Note that when searching
345 * for a network to satisfy a request, any listed in the request will satisfy the request.
346 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
347 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
348 * to be selected. This is logically different than
349 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
350 *
351 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700352 * @return This NetworkCapability to facilitate chaining.
353 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700354 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700355 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700356 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
357 throw new IllegalArgumentException("TransportType out of range");
358 }
359 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700360 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700361 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700362 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700363
364 /**
365 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
366 *
367 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700368 * @return This NetworkCapability to facilitate chaining.
369 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700370 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700371 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700372 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
373 throw new IllegalArgumentException("TransportType out of range");
374 }
375 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700376 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700377 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700378 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700379
380 /**
381 * Gets all the transports set on this {@code NetworkCapability} instance.
382 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700383 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700384 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700385 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700386 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700387 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700388 return enumerateBits(mTransportTypes);
389 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700390
391 /**
392 * Tests for the presence of a transport on this instance.
393 *
394 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
395 * @return {@code true} if set on this instance.
396 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700397 public boolean hasTransport(int transportType) {
398 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
399 return false;
400 }
401 return ((mTransportTypes & (1 << transportType)) != 0);
402 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700403
404 private void combineTransportTypes(NetworkCapabilities nc) {
405 this.mTransportTypes |= nc.mTransportTypes;
406 }
407 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
408 return ((this.mTransportTypes == 0) ||
409 ((this.mTransportTypes & nc.mTransportTypes) != 0));
410 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700411 /** @hide */
412 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700413 return (nc.mTransportTypes == this.mTransportTypes);
414 }
415
416 /**
417 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
418 * for the first hop on the given transport. It is not measured, but may take into account
419 * link parameters (Radio technology, allocated channels, etc).
420 */
421 private int mLinkUpBandwidthKbps;
422 private int mLinkDownBandwidthKbps;
423
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700424 /**
425 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
426 * the estimated first hop transport bandwidth.
427 * <p>
428 * Note that when used to request a network, this specifies the minimum acceptable.
429 * When received as the state of an existing network this specifies the typical
430 * first hop bandwidth expected. This is never measured, but rather is inferred
431 * from technology type and other link parameters. It could be used to differentiate
432 * between very slow 1xRTT cellular links and other faster networks or even between
433 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
434 * fast backhauls and slow backhauls.
435 *
436 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700437 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700438 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700439 public void setLinkUpstreamBandwidthKbps(int upKbps) {
440 mLinkUpBandwidthKbps = upKbps;
441 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700442
443 /**
444 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
445 * the estimated first hop transport bandwidth.
446 *
447 * @return The estimated first hop upstream (device to network) bandwidth.
448 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700449 public int getLinkUpstreamBandwidthKbps() {
450 return mLinkUpBandwidthKbps;
451 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700452
453 /**
454 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
455 * the estimated first hop transport bandwidth.
456 * <p>
457 * Note that when used to request a network, this specifies the minimum acceptable.
458 * When received as the state of an existing network this specifies the typical
459 * first hop bandwidth expected. This is never measured, but rather is inferred
460 * from technology type and other link parameters. It could be used to differentiate
461 * between very slow 1xRTT cellular links and other faster networks or even between
462 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
463 * fast backhauls and slow backhauls.
464 *
465 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700466 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700467 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700468 public void setLinkDownstreamBandwidthKbps(int downKbps) {
469 mLinkDownBandwidthKbps = downKbps;
470 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700471
472 /**
473 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
474 * the estimated first hop transport bandwidth.
475 *
476 * @return The estimated first hop downstream (network to device) bandwidth.
477 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700478 public int getLinkDownstreamBandwidthKbps() {
479 return mLinkDownBandwidthKbps;
480 }
481
482 private void combineLinkBandwidths(NetworkCapabilities nc) {
483 this.mLinkUpBandwidthKbps =
484 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
485 this.mLinkDownBandwidthKbps =
486 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
487 }
488 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
489 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
490 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
491 }
492 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
493 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
494 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
495 }
496
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700497 private String mNetworkSpecifier;
498 /**
499 * Sets the optional bearer specific network specifier.
500 * This has no meaning if a single transport is also not specified, so calling
501 * this without a single transport set will generate an exception, as will
502 * subsequently adding or removing transports after this is set.
503 * </p>
504 * The interpretation of this {@code String} is bearer specific and bearers that use
505 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700506 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
507 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700508 *
509 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
510 * specific network specifier where the bearer has a choice of
511 * networks.
512 * @hide
513 */
514 public void setNetworkSpecifier(String networkSpecifier) {
515 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
516 throw new IllegalStateException("Must have a single transport specified to use " +
517 "setNetworkSpecifier");
518 }
519 mNetworkSpecifier = networkSpecifier;
520 }
521
522 /**
523 * Gets the optional bearer specific network specifier.
524 *
525 * @return The optional {@code String} specifying the bearer specific network specifier.
526 * See {@link #setNetworkSpecifier}.
527 * @hide
528 */
529 public String getNetworkSpecifier() {
530 return mNetworkSpecifier;
531 }
532
533 private void combineSpecifiers(NetworkCapabilities nc) {
534 String otherSpecifier = nc.getNetworkSpecifier();
535 if (TextUtils.isEmpty(otherSpecifier)) return;
536 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
537 throw new IllegalStateException("Can't combine two networkSpecifiers");
538 }
539 setNetworkSpecifier(otherSpecifier);
540 }
541 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
542 return (TextUtils.isEmpty(mNetworkSpecifier) ||
543 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
544 }
545 private boolean equalsSpecifier(NetworkCapabilities nc) {
546 if (TextUtils.isEmpty(mNetworkSpecifier)) {
547 return TextUtils.isEmpty(nc.mNetworkSpecifier);
548 } else {
549 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
550 }
551 }
552
Robert Greenwalt1448f052014-04-08 13:41:39 -0700553 /**
554 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
555 * {@hide}
556 */
557 public void combineCapabilities(NetworkCapabilities nc) {
558 combineNetCapabilities(nc);
559 combineTransportTypes(nc);
560 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700561 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700562 }
563
564 /**
565 * Check if our requirements are satisfied by the given Capabilities.
566 * {@hide}
567 */
568 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700569 return (nc != null &&
570 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 satisfiedByTransportTypes(nc) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700572 satisfiedByLinkBandwidths(nc) &&
573 satisfiedBySpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700574 }
575
576 @Override
577 public boolean equals(Object obj) {
578 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
579 NetworkCapabilities that = (NetworkCapabilities)obj;
580 return (equalsNetCapabilities(that) &&
581 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700582 equalsLinkBandwidths(that) &&
583 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 }
585
586 @Override
587 public int hashCode() {
588 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
589 ((int)(mNetworkCapabilities >> 32) * 3) +
590 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
591 ((int)(mTransportTypes >> 32) * 7) +
592 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700593 (mLinkDownBandwidthKbps * 13) +
594 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700595 }
596
Wink Saville4e2dea72014-09-20 11:04:03 -0700597 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700598 public int describeContents() {
599 return 0;
600 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700601 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700602 public void writeToParcel(Parcel dest, int flags) {
603 dest.writeLong(mNetworkCapabilities);
604 dest.writeLong(mTransportTypes);
605 dest.writeInt(mLinkUpBandwidthKbps);
606 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700607 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700608 }
609 public static final Creator<NetworkCapabilities> CREATOR =
610 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700611 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700612 public NetworkCapabilities createFromParcel(Parcel in) {
613 NetworkCapabilities netCap = new NetworkCapabilities();
614
615 netCap.mNetworkCapabilities = in.readLong();
616 netCap.mTransportTypes = in.readLong();
617 netCap.mLinkUpBandwidthKbps = in.readInt();
618 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700619 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700620 return netCap;
621 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700622 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700623 public NetworkCapabilities[] newArray(int size) {
624 return new NetworkCapabilities[size];
625 }
626 };
627
Wink Saville4e2dea72014-09-20 11:04:03 -0700628 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700629 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700630 int[] types = getTransportTypes();
631 String transports = (types.length > 0 ? " Transports: " : "");
632 for (int i = 0; i < types.length;) {
633 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700634 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
635 case TRANSPORT_WIFI: transports += "WIFI"; break;
636 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
637 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400638 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700639 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700640 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700641 }
642
Robert Greenwalt7569f182014-06-08 16:42:59 -0700643 types = getCapabilities();
644 String capabilities = (types.length > 0 ? " Capabilities: " : "");
645 for (int i = 0; i < types.length; ) {
646 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700647 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
648 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
649 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
650 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
651 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
652 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
653 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
654 case NET_CAPABILITY_IA: capabilities += "IA"; break;
655 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
656 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
657 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
658 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
659 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
660 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700661 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400662 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900663 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Paul Jensencf4c2c62015-07-01 14:16:32 -0400664 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700665 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700666 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700667 }
668
669 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
670 mLinkUpBandwidthKbps + "Kbps" : "");
671 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
672 mLinkDownBandwidthKbps + "Kbps" : "");
673
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700674 String specifier = (mNetworkSpecifier == null ?
675 "" : " Specifier: <" + mNetworkSpecifier + ">");
676
677 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700678 }
679}