blob: 84f1d103ccf328c62b4397f02f94377c48aeb99e [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();
Lorenzo Colitti260a36d2015-07-08 12:49:04 +090041 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 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900187 * Network capabilities that are expected to be mutable, i.e., can change while a particular
188 * network is connected.
189 */
190 private static final long MUTABLE_CAPABILITIES =
191 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
192 // http://b/18206275
193 (1 << NET_CAPABILITY_TRUSTED) |
194 (1 << NET_CAPABILITY_VALIDATED) |
195 (1 << NET_CAPABILITY_CAPTIVE_PORTAL);
196
197 /**
198 * Network capabilities that are not allowed in NetworkRequests. This exists because the
199 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
200 * capability's presence cannot be known in advance. If such a capability is requested, then we
201 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
202 * get immediately torn down because they do not have the requested capability.
203 */
204 private static final long NON_REQUESTABLE_CAPABILITIES =
205 (1 << NET_CAPABILITY_VALIDATED) |
206 (1 << NET_CAPABILITY_CAPTIVE_PORTAL);
207
208 /**
209 * Capabilities that are set by default when the object is constructed.
210 */
211 private static final long DEFAULT_CAPABILITIES =
212 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
213 (1 << NET_CAPABILITY_TRUSTED) |
214 (1 << NET_CAPABILITY_NOT_VPN);
215
216 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700217 * Adds the given capability to this {@code NetworkCapability} instance.
218 * Multiple capabilities may be applied sequentially. Note that when searching
219 * for a network to satisfy a request, all capabilities requested must be satisfied.
220 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700221 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
222 * @return This NetworkCapability to facilitate chaining.
223 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700224 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700225 public NetworkCapabilities addCapability(int capability) {
226 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700227 throw new IllegalArgumentException("NetworkCapability out of range");
228 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700229 mNetworkCapabilities |= 1 << capability;
230 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700231 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700232
233 /**
234 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
235 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700236 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
237 * @return This NetworkCapability to facilitate chaining.
238 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700239 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700240 public NetworkCapabilities removeCapability(int capability) {
241 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700242 throw new IllegalArgumentException("NetworkCapability out of range");
243 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700244 mNetworkCapabilities &= ~(1 << capability);
245 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700246 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700247
248 /**
249 * Gets all the capabilities set on this {@code NetworkCapability} instance.
250 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700251 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700252 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700253 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700254 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700255 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700256 return enumerateBits(mNetworkCapabilities);
257 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700258
259 /**
260 * Tests for the presence of a capabilitity on this instance.
261 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700262 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700263 * @return {@code true} if set on this instance.
264 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700265 public boolean hasCapability(int capability) {
266 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700267 return false;
268 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700269 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700270 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700271
Robert Greenwalt7569f182014-06-08 16:42:59 -0700272 private int[] enumerateBits(long val) {
273 int size = Long.bitCount(val);
274 int[] result = new int[size];
275 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700276 int resource = 0;
277 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700278 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700279 val = val >> 1;
280 resource++;
281 }
282 return result;
283 }
284
285 private void combineNetCapabilities(NetworkCapabilities nc) {
286 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
287 }
288
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900289 /**
290 * Convenience function that returns a human-readable description of the first mutable
291 * capability we find. Used to present an error message to apps that request mutable
292 * capabilities.
293 *
294 * @hide
295 */
296 public String describeFirstNonRequestableCapability() {
297 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
298 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
299 // This cannot happen unless the preceding checks are incomplete.
300 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
301 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
302 }
303 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
304 return null;
305 }
306
307 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
308 long networkCapabilities = this.mNetworkCapabilities;
309 if (onlyImmutable) {
310 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
311 }
312 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700313 }
314
Robert Greenwalt06314e42014-10-29 14:04:06 -0700315 /** @hide */
316 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700317 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
318 }
319
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900320 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
321 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
322 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
323 }
324
Robert Greenwalt1448f052014-04-08 13:41:39 -0700325 /**
326 * Representing the transport type. Apps should generally not care about transport. A
327 * request for a fast internet connection could be satisfied by a number of different
328 * transports. If any are specified here it will be satisfied a Network that matches
329 * any of them. If a caller doesn't care about the transport it should not specify any.
330 */
331 private long mTransportTypes;
332
333 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700334 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700335 */
336 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700337
338 /**
339 * Indicates this network uses a Wi-Fi transport.
340 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700341 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700342
343 /**
344 * Indicates this network uses a Bluetooth transport.
345 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700346 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700347
348 /**
349 * Indicates this network uses an Ethernet transport.
350 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700351 public static final int TRANSPORT_ETHERNET = 3;
352
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400353 /**
354 * Indicates this network uses a VPN transport.
355 */
356 public static final int TRANSPORT_VPN = 4;
357
Robert Greenwalt1448f052014-04-08 13:41:39 -0700358 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400359 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700360
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700361 /**
362 * Adds the given transport type to this {@code NetworkCapability} instance.
363 * Multiple transports may be applied sequentially. Note that when searching
364 * for a network to satisfy a request, any listed in the request will satisfy the request.
365 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
366 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
367 * to be selected. This is logically different than
368 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
369 *
370 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700371 * @return This NetworkCapability to facilitate chaining.
372 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700373 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700374 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700375 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
376 throw new IllegalArgumentException("TransportType out of range");
377 }
378 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700379 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700380 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700381 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700382
383 /**
384 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
385 *
386 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700387 * @return This NetworkCapability to facilitate chaining.
388 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700389 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700390 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700391 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
392 throw new IllegalArgumentException("TransportType out of range");
393 }
394 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700395 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700396 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700397 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700398
399 /**
400 * Gets all the transports set on this {@code NetworkCapability} instance.
401 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700402 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700403 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700404 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700405 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700406 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700407 return enumerateBits(mTransportTypes);
408 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700409
410 /**
411 * Tests for the presence of a transport on this instance.
412 *
413 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
414 * @return {@code true} if set on this instance.
415 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700416 public boolean hasTransport(int transportType) {
417 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
418 return false;
419 }
420 return ((mTransportTypes & (1 << transportType)) != 0);
421 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700422
423 private void combineTransportTypes(NetworkCapabilities nc) {
424 this.mTransportTypes |= nc.mTransportTypes;
425 }
426 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
427 return ((this.mTransportTypes == 0) ||
428 ((this.mTransportTypes & nc.mTransportTypes) != 0));
429 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700430 /** @hide */
431 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700432 return (nc.mTransportTypes == this.mTransportTypes);
433 }
434
435 /**
436 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
437 * for the first hop on the given transport. It is not measured, but may take into account
438 * link parameters (Radio technology, allocated channels, etc).
439 */
440 private int mLinkUpBandwidthKbps;
441 private int mLinkDownBandwidthKbps;
442
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700443 /**
444 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
445 * the estimated first hop transport bandwidth.
446 * <p>
447 * Note that when used to request a network, this specifies the minimum acceptable.
448 * When received as the state of an existing network this specifies the typical
449 * first hop bandwidth expected. This is never measured, but rather is inferred
450 * from technology type and other link parameters. It could be used to differentiate
451 * between very slow 1xRTT cellular links and other faster networks or even between
452 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
453 * fast backhauls and slow backhauls.
454 *
455 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700456 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700457 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700458 public void setLinkUpstreamBandwidthKbps(int upKbps) {
459 mLinkUpBandwidthKbps = upKbps;
460 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700461
462 /**
463 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
464 * the estimated first hop transport bandwidth.
465 *
466 * @return The estimated first hop upstream (device to network) bandwidth.
467 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700468 public int getLinkUpstreamBandwidthKbps() {
469 return mLinkUpBandwidthKbps;
470 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700471
472 /**
473 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
474 * the estimated first hop transport bandwidth.
475 * <p>
476 * Note that when used to request a network, this specifies the minimum acceptable.
477 * When received as the state of an existing network this specifies the typical
478 * first hop bandwidth expected. This is never measured, but rather is inferred
479 * from technology type and other link parameters. It could be used to differentiate
480 * between very slow 1xRTT cellular links and other faster networks or even between
481 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
482 * fast backhauls and slow backhauls.
483 *
484 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700485 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700486 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700487 public void setLinkDownstreamBandwidthKbps(int downKbps) {
488 mLinkDownBandwidthKbps = downKbps;
489 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700490
491 /**
492 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
493 * the estimated first hop transport bandwidth.
494 *
495 * @return The estimated first hop downstream (network to device) bandwidth.
496 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700497 public int getLinkDownstreamBandwidthKbps() {
498 return mLinkDownBandwidthKbps;
499 }
500
501 private void combineLinkBandwidths(NetworkCapabilities nc) {
502 this.mLinkUpBandwidthKbps =
503 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
504 this.mLinkDownBandwidthKbps =
505 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
506 }
507 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
508 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
509 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
510 }
511 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
512 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
513 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
514 }
515
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700516 private String mNetworkSpecifier;
517 /**
518 * Sets the optional bearer specific network specifier.
519 * This has no meaning if a single transport is also not specified, so calling
520 * this without a single transport set will generate an exception, as will
521 * subsequently adding or removing transports after this is set.
522 * </p>
523 * The interpretation of this {@code String} is bearer specific and bearers that use
524 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700525 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
526 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700527 *
528 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
529 * specific network specifier where the bearer has a choice of
530 * networks.
531 * @hide
532 */
533 public void setNetworkSpecifier(String networkSpecifier) {
534 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
535 throw new IllegalStateException("Must have a single transport specified to use " +
536 "setNetworkSpecifier");
537 }
538 mNetworkSpecifier = networkSpecifier;
539 }
540
541 /**
542 * Gets the optional bearer specific network specifier.
543 *
544 * @return The optional {@code String} specifying the bearer specific network specifier.
545 * See {@link #setNetworkSpecifier}.
546 * @hide
547 */
548 public String getNetworkSpecifier() {
549 return mNetworkSpecifier;
550 }
551
552 private void combineSpecifiers(NetworkCapabilities nc) {
553 String otherSpecifier = nc.getNetworkSpecifier();
554 if (TextUtils.isEmpty(otherSpecifier)) return;
555 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
556 throw new IllegalStateException("Can't combine two networkSpecifiers");
557 }
558 setNetworkSpecifier(otherSpecifier);
559 }
560 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
561 return (TextUtils.isEmpty(mNetworkSpecifier) ||
562 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
563 }
564 private boolean equalsSpecifier(NetworkCapabilities nc) {
565 if (TextUtils.isEmpty(mNetworkSpecifier)) {
566 return TextUtils.isEmpty(nc.mNetworkSpecifier);
567 } else {
568 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
569 }
570 }
571
Robert Greenwalt1448f052014-04-08 13:41:39 -0700572 /**
573 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900574 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575 */
576 public void combineCapabilities(NetworkCapabilities nc) {
577 combineNetCapabilities(nc);
578 combineTransportTypes(nc);
579 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700580 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700581 }
582
583 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900584 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
585 *
586 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
587 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
588 * bandwidth, signal strength, or validation / captive portal status.
589 *
590 * @hide
591 */
592 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
593 return (nc != null &&
594 satisfiedByNetCapabilities(nc, onlyImmutable) &&
595 satisfiedByTransportTypes(nc) &&
596 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
597 satisfiedBySpecifier(nc));
598 }
599
600 /**
601 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
602 *
603 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
604 *
605 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700606 */
607 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900608 return satisfiedByNetworkCapabilities(nc, false);
609 }
610
611 /**
612 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
613 *
614 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
615 *
616 * @hide
617 */
618 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
619 return satisfiedByNetworkCapabilities(nc, true);
620 }
621
622 /**
623 * Checks that our immutable capabilities are the same as those of the given
624 * {@code NetworkCapabilities}.
625 *
626 * @hide
627 */
628 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
629 if (nc == null) return false;
630 return (equalsNetCapabilitiesImmutable(nc) &&
631 equalsTransportTypes(nc) &&
632 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700633 }
634
635 @Override
636 public boolean equals(Object obj) {
637 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
638 NetworkCapabilities that = (NetworkCapabilities)obj;
639 return (equalsNetCapabilities(that) &&
640 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700641 equalsLinkBandwidths(that) &&
642 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700643 }
644
645 @Override
646 public int hashCode() {
647 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
648 ((int)(mNetworkCapabilities >> 32) * 3) +
649 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
650 ((int)(mTransportTypes >> 32) * 7) +
651 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700652 (mLinkDownBandwidthKbps * 13) +
653 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700654 }
655
Wink Saville4e2dea72014-09-20 11:04:03 -0700656 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700657 public int describeContents() {
658 return 0;
659 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700660 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700661 public void writeToParcel(Parcel dest, int flags) {
662 dest.writeLong(mNetworkCapabilities);
663 dest.writeLong(mTransportTypes);
664 dest.writeInt(mLinkUpBandwidthKbps);
665 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700666 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700667 }
668 public static final Creator<NetworkCapabilities> CREATOR =
669 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700670 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700671 public NetworkCapabilities createFromParcel(Parcel in) {
672 NetworkCapabilities netCap = new NetworkCapabilities();
673
674 netCap.mNetworkCapabilities = in.readLong();
675 netCap.mTransportTypes = in.readLong();
676 netCap.mLinkUpBandwidthKbps = in.readInt();
677 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700678 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700679 return netCap;
680 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700681 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700682 public NetworkCapabilities[] newArray(int size) {
683 return new NetworkCapabilities[size];
684 }
685 };
686
Wink Saville4e2dea72014-09-20 11:04:03 -0700687 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700688 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700689 int[] types = getTransportTypes();
690 String transports = (types.length > 0 ? " Transports: " : "");
691 for (int i = 0; i < types.length;) {
692 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700693 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
694 case TRANSPORT_WIFI: transports += "WIFI"; break;
695 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
696 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400697 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700698 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700699 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700700 }
701
Robert Greenwalt7569f182014-06-08 16:42:59 -0700702 types = getCapabilities();
703 String capabilities = (types.length > 0 ? " Capabilities: " : "");
704 for (int i = 0; i < types.length; ) {
705 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700706 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
707 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
708 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
709 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
710 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
711 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
712 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
713 case NET_CAPABILITY_IA: capabilities += "IA"; break;
714 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
715 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
716 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
717 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
718 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
719 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700720 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400721 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900722 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700723 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700724 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700725 }
726
727 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
728 mLinkUpBandwidthKbps + "Kbps" : "");
729 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
730 mLinkDownBandwidthKbps + "Kbps" : "");
731
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700732 String specifier = (mNetworkSpecifier == null ?
733 "" : " Specifier: <" + mNetworkSpecifier + ">");
734
735 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700736 }
737}