blob: f988e0bae0f541cf2402708f79fe926096c8696e [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;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090051 mSignalStrength = nc.mSignalStrength;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070052 }
53 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070054
55 /**
Lorenzo Colittif7058f52015-04-27 11:31:55 +090056 * Completely clears the contents of this object, removing even the capabilities that are set
57 * by default when the object is constructed.
58 * @hide
59 */
60 public void clearAll() {
61 mNetworkCapabilities = mTransportTypes = 0;
62 mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
63 mNetworkSpecifier = null;
Lorenzo Colittic3f21f32015-07-06 23:50:27 +090064 mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
Lorenzo Colittif7058f52015-04-27 11:31:55 +090065 }
66
67 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -070068 * Represents the network's capabilities. If any are specified they will be satisfied
69 * by any Network that matches all of them.
70 */
Lorenzo Colittif7058f52015-04-27 11:31:55 +090071 private long mNetworkCapabilities;
Robert Greenwalt1448f052014-04-08 13:41:39 -070072
73 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070074 * Indicates this is a network that has the ability to reach the
75 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070076 */
77 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070078
79 /**
80 * Indicates this is a network that has the ability to reach the carrier's
81 * SUPL server, used to retrieve GPS information.
82 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070083 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070084
85 /**
86 * Indicates this is a network that has the ability to reach the carrier's
87 * DUN or tethering gateway.
88 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070089 public static final int NET_CAPABILITY_DUN = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070090
91 /**
92 * Indicates this is a network that has the ability to reach the carrier's
93 * FOTA portal, used for over the air updates.
94 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070095 public static final int NET_CAPABILITY_FOTA = 3;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070096
97 /**
98 * Indicates this is a network that has the ability to reach the carrier's
99 * IMS servers, used for network registration and signaling.
100 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700101 public static final int NET_CAPABILITY_IMS = 4;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700102
103 /**
104 * Indicates this is a network that has the ability to reach the carrier's
105 * CBS servers, used for carrier specific services.
106 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700107 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700108
109 /**
110 * Indicates this is a network that has the ability to reach a Wi-Fi direct
111 * peer.
112 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700113 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700114
115 /**
116 * Indicates this is a network that has the ability to reach a carrier's
117 * Initial Attach servers.
118 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700119 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700120
121 /**
122 * Indicates this is a network that has the ability to reach a carrier's
123 * RCS servers, used for Rich Communication Services.
124 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700125 public static final int NET_CAPABILITY_RCS = 8;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700126
127 /**
128 * Indicates this is a network that has the ability to reach a carrier's
129 * XCAP servers, used for configuration and control.
130 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700131 public static final int NET_CAPABILITY_XCAP = 9;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700132
133 /**
134 * Indicates this is a network that has the ability to reach a carrier's
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700135 * Emergency IMS servers or other services, used for network signaling
136 * during emergency calls.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700137 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700138 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700139
140 /**
141 * Indicates that this network is unmetered.
142 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700143 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700144
145 /**
146 * Indicates that this network should be able to reach the internet.
147 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700148 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700149
150 /**
151 * Indicates that this network is available for general use. If this is not set
152 * applications should not attempt to communicate on this network. Note that this
153 * is simply informative and not enforcement - enforcement is handled via other means.
154 * Set by default.
155 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700156 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
157
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700158 /**
159 * Indicates that the user has indicated implicit trust of this network. This
160 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
161 * BT device or a wifi the user asked to connect to. Untrusted networks
162 * are probably limited to unknown wifi AP. Set by default.
163 */
164 public static final int NET_CAPABILITY_TRUSTED = 14;
165
Paul Jensen76b610a2015-03-18 09:33:07 -0400166 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400167 * Indicates that this network is not a VPN. This capability is set by default and should be
Paul Jensen76b610a2015-03-18 09:33:07 -0400168 * explicitly cleared for VPN networks.
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400169 */
170 public static final int NET_CAPABILITY_NOT_VPN = 15;
171
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900172 /**
173 * Indicates that connectivity on this network was successfully validated. For example, for a
174 * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully
175 * detected.
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900176 */
177 public static final int NET_CAPABILITY_VALIDATED = 16;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700178
Paul Jensen3d194ea2015-06-16 14:27:36 -0400179 /**
180 * Indicates that this network was found to have a captive portal in place last time it was
181 * probed.
182 */
183 public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
184
Robert Greenwalt1448f052014-04-08 13:41:39 -0700185 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Paul Jensen3d194ea2015-06-16 14:27:36 -0400186 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700187
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700188 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900189 * Network capabilities that are expected to be mutable, i.e., can change while a particular
190 * network is connected.
191 */
192 private static final long MUTABLE_CAPABILITIES =
193 // TRUSTED can change when user explicitly connects to an untrusted network in Settings.
194 // http://b/18206275
195 (1 << NET_CAPABILITY_TRUSTED) |
196 (1 << NET_CAPABILITY_VALIDATED) |
197 (1 << NET_CAPABILITY_CAPTIVE_PORTAL);
198
199 /**
200 * Network capabilities that are not allowed in NetworkRequests. This exists because the
201 * NetworkFactory / NetworkAgent model does not deal well with the situation where a
202 * capability's presence cannot be known in advance. If such a capability is requested, then we
203 * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then
204 * get immediately torn down because they do not have the requested capability.
205 */
206 private static final long NON_REQUESTABLE_CAPABILITIES =
207 (1 << NET_CAPABILITY_VALIDATED) |
208 (1 << NET_CAPABILITY_CAPTIVE_PORTAL);
209
210 /**
211 * Capabilities that are set by default when the object is constructed.
212 */
213 private static final long DEFAULT_CAPABILITIES =
214 (1 << NET_CAPABILITY_NOT_RESTRICTED) |
215 (1 << NET_CAPABILITY_TRUSTED) |
216 (1 << NET_CAPABILITY_NOT_VPN);
217
218 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700219 * Adds the given capability to this {@code NetworkCapability} instance.
220 * Multiple capabilities may be applied sequentially. Note that when searching
221 * for a network to satisfy a request, all capabilities requested must be satisfied.
222 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700223 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
224 * @return This NetworkCapability to facilitate chaining.
225 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700226 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700227 public NetworkCapabilities addCapability(int capability) {
228 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700229 throw new IllegalArgumentException("NetworkCapability out of range");
230 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700231 mNetworkCapabilities |= 1 << capability;
232 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700233 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700234
235 /**
236 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
237 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700238 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
239 * @return This NetworkCapability to facilitate chaining.
240 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700241 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700242 public NetworkCapabilities removeCapability(int capability) {
243 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700244 throw new IllegalArgumentException("NetworkCapability out of range");
245 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700246 mNetworkCapabilities &= ~(1 << capability);
247 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700248 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700249
250 /**
251 * Gets all the capabilities set on this {@code NetworkCapability} instance.
252 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700253 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700254 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700255 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700256 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700257 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700258 return enumerateBits(mNetworkCapabilities);
259 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700260
261 /**
262 * Tests for the presence of a capabilitity on this instance.
263 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700264 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700265 * @return {@code true} if set on this instance.
266 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700267 public boolean hasCapability(int capability) {
268 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700269 return false;
270 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700271 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700272 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700273
Robert Greenwalt7569f182014-06-08 16:42:59 -0700274 private int[] enumerateBits(long val) {
275 int size = Long.bitCount(val);
276 int[] result = new int[size];
277 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700278 int resource = 0;
279 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700280 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700281 val = val >> 1;
282 resource++;
283 }
284 return result;
285 }
286
287 private void combineNetCapabilities(NetworkCapabilities nc) {
288 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
289 }
290
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900291 /**
292 * Convenience function that returns a human-readable description of the first mutable
293 * capability we find. Used to present an error message to apps that request mutable
294 * capabilities.
295 *
296 * @hide
297 */
298 public String describeFirstNonRequestableCapability() {
299 if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
300 if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
301 // This cannot happen unless the preceding checks are incomplete.
302 if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
303 return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
304 }
305 if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900306 if (hasSignalStrength()) return "signalStrength";
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900307 return null;
308 }
309
310 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
311 long networkCapabilities = this.mNetworkCapabilities;
312 if (onlyImmutable) {
313 networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES;
314 }
315 return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700316 }
317
Robert Greenwalt06314e42014-10-29 14:04:06 -0700318 /** @hide */
319 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700320 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
321 }
322
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900323 private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) {
324 return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) ==
325 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
326 }
327
Robert Greenwalt1448f052014-04-08 13:41:39 -0700328 /**
329 * Representing the transport type. Apps should generally not care about transport. A
330 * request for a fast internet connection could be satisfied by a number of different
331 * transports. If any are specified here it will be satisfied a Network that matches
332 * any of them. If a caller doesn't care about the transport it should not specify any.
333 */
334 private long mTransportTypes;
335
336 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700337 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700338 */
339 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700340
341 /**
342 * Indicates this network uses a Wi-Fi transport.
343 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700344 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700345
346 /**
347 * Indicates this network uses a Bluetooth transport.
348 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700349 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700350
351 /**
352 * Indicates this network uses an Ethernet transport.
353 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700354 public static final int TRANSPORT_ETHERNET = 3;
355
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400356 /**
357 * Indicates this network uses a VPN transport.
358 */
359 public static final int TRANSPORT_VPN = 4;
360
Robert Greenwalt1448f052014-04-08 13:41:39 -0700361 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400362 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700363
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700364 /**
365 * Adds the given transport type to this {@code NetworkCapability} instance.
366 * Multiple transports may be applied sequentially. Note that when searching
367 * for a network to satisfy a request, any listed in the request will satisfy the request.
368 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
369 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
370 * to be selected. This is logically different than
371 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
372 *
373 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700374 * @return This NetworkCapability to facilitate chaining.
375 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700376 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700377 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700378 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
379 throw new IllegalArgumentException("TransportType out of range");
380 }
381 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700382 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700383 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700384 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700385
386 /**
387 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
388 *
389 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700390 * @return This NetworkCapability to facilitate chaining.
391 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700392 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700393 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700394 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
395 throw new IllegalArgumentException("TransportType out of range");
396 }
397 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700398 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700399 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700400 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700401
402 /**
403 * Gets all the transports set on this {@code NetworkCapability} instance.
404 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700405 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700406 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700407 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700408 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700409 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700410 return enumerateBits(mTransportTypes);
411 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700412
413 /**
414 * Tests for the presence of a transport on this instance.
415 *
416 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
417 * @return {@code true} if set on this instance.
418 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700419 public boolean hasTransport(int transportType) {
420 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
421 return false;
422 }
423 return ((mTransportTypes & (1 << transportType)) != 0);
424 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700425
426 private void combineTransportTypes(NetworkCapabilities nc) {
427 this.mTransportTypes |= nc.mTransportTypes;
428 }
429 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
430 return ((this.mTransportTypes == 0) ||
431 ((this.mTransportTypes & nc.mTransportTypes) != 0));
432 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700433 /** @hide */
434 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700435 return (nc.mTransportTypes == this.mTransportTypes);
436 }
437
438 /**
439 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
440 * for the first hop on the given transport. It is not measured, but may take into account
441 * link parameters (Radio technology, allocated channels, etc).
442 */
443 private int mLinkUpBandwidthKbps;
444 private int mLinkDownBandwidthKbps;
445
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700446 /**
447 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
448 * the estimated first hop transport bandwidth.
449 * <p>
450 * Note that when used to request a network, this specifies the minimum acceptable.
451 * When received as the state of an existing network this specifies the typical
452 * first hop bandwidth expected. This is never measured, but rather is inferred
453 * from technology type and other link parameters. It could be used to differentiate
454 * between very slow 1xRTT cellular links and other faster networks or even between
455 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
456 * fast backhauls and slow backhauls.
457 *
458 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700459 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700460 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700461 public void setLinkUpstreamBandwidthKbps(int upKbps) {
462 mLinkUpBandwidthKbps = upKbps;
463 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700464
465 /**
466 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
467 * the estimated first hop transport bandwidth.
468 *
469 * @return The estimated first hop upstream (device to network) bandwidth.
470 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700471 public int getLinkUpstreamBandwidthKbps() {
472 return mLinkUpBandwidthKbps;
473 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700474
475 /**
476 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
477 * the estimated first hop transport bandwidth.
478 * <p>
479 * Note that when used to request a network, this specifies the minimum acceptable.
480 * When received as the state of an existing network this specifies the typical
481 * first hop bandwidth expected. This is never measured, but rather is inferred
482 * from technology type and other link parameters. It could be used to differentiate
483 * between very slow 1xRTT cellular links and other faster networks or even between
484 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
485 * fast backhauls and slow backhauls.
486 *
487 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700488 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700489 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490 public void setLinkDownstreamBandwidthKbps(int downKbps) {
491 mLinkDownBandwidthKbps = downKbps;
492 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700493
494 /**
495 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
496 * the estimated first hop transport bandwidth.
497 *
498 * @return The estimated first hop downstream (network to device) bandwidth.
499 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700500 public int getLinkDownstreamBandwidthKbps() {
501 return mLinkDownBandwidthKbps;
502 }
503
504 private void combineLinkBandwidths(NetworkCapabilities nc) {
505 this.mLinkUpBandwidthKbps =
506 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
507 this.mLinkDownBandwidthKbps =
508 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
509 }
510 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
511 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
512 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
513 }
514 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
515 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
516 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
517 }
518
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700519 private String mNetworkSpecifier;
520 /**
521 * Sets the optional bearer specific network specifier.
522 * This has no meaning if a single transport is also not specified, so calling
523 * this without a single transport set will generate an exception, as will
524 * subsequently adding or removing transports after this is set.
525 * </p>
526 * The interpretation of this {@code String} is bearer specific and bearers that use
527 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700528 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
529 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700530 *
531 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
532 * specific network specifier where the bearer has a choice of
533 * networks.
534 * @hide
535 */
536 public void setNetworkSpecifier(String networkSpecifier) {
537 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
538 throw new IllegalStateException("Must have a single transport specified to use " +
539 "setNetworkSpecifier");
540 }
541 mNetworkSpecifier = networkSpecifier;
542 }
543
544 /**
545 * Gets the optional bearer specific network specifier.
546 *
547 * @return The optional {@code String} specifying the bearer specific network specifier.
548 * See {@link #setNetworkSpecifier}.
549 * @hide
550 */
551 public String getNetworkSpecifier() {
552 return mNetworkSpecifier;
553 }
554
555 private void combineSpecifiers(NetworkCapabilities nc) {
556 String otherSpecifier = nc.getNetworkSpecifier();
557 if (TextUtils.isEmpty(otherSpecifier)) return;
558 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
559 throw new IllegalStateException("Can't combine two networkSpecifiers");
560 }
561 setNetworkSpecifier(otherSpecifier);
562 }
563 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
564 return (TextUtils.isEmpty(mNetworkSpecifier) ||
565 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
566 }
567 private boolean equalsSpecifier(NetworkCapabilities nc) {
568 if (TextUtils.isEmpty(mNetworkSpecifier)) {
569 return TextUtils.isEmpty(nc.mNetworkSpecifier);
570 } else {
571 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
572 }
573 }
574
Robert Greenwalt1448f052014-04-08 13:41:39 -0700575 /**
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900576 * Magic value that indicates no signal strength provided. A request specifying this value is
577 * always satisfied.
578 *
579 * @hide
580 */
581 public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
582
583 /**
584 * Signal strength. This is a signed integer, and higher values indicate better signal.
585 * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
586 */
587 private int mSignalStrength;
588
589 /**
590 * Sets the signal strength. This is a signed integer, with higher values indicating a stronger
591 * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
592 * reported by WifiManager.
593 * <p>
594 * Note that when used to register a network callback, this specifies the minimum acceptable
595 * signal strength. When received as the state of an existing network it specifies the current
596 * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
597 * effect when requesting a callback.
598 *
599 * @param signalStrength the bearer-specific signal strength.
600 * @hide
601 */
602 public void setSignalStrength(int signalStrength) {
603 mSignalStrength = signalStrength;
604 }
605
606 /**
607 * Returns {@code true} if this object specifies a signal strength.
608 *
609 * @hide
610 */
611 public boolean hasSignalStrength() {
612 return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
613 }
614
615 /**
616 * Retrieves the signal strength.
617 *
618 * @return The bearer-specific signal strength.
619 * @hide
620 */
621 public int getSignalStrength() {
622 return mSignalStrength;
623 }
624
625 private void combineSignalStrength(NetworkCapabilities nc) {
626 this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
627 }
628
629 private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
630 return this.mSignalStrength <= nc.mSignalStrength;
631 }
632
633 private boolean equalsSignalStrength(NetworkCapabilities nc) {
634 return this.mSignalStrength == nc.mSignalStrength;
635 }
636
637 /**
Robert Greenwalt1448f052014-04-08 13:41:39 -0700638 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900639 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700640 */
641 public void combineCapabilities(NetworkCapabilities nc) {
642 combineNetCapabilities(nc);
643 combineTransportTypes(nc);
644 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700645 combineSpecifiers(nc);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900646 combineSignalStrength(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700647 }
648
649 /**
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900650 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
651 *
652 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
653 * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link
654 * bandwidth, signal strength, or validation / captive portal status.
655 *
656 * @hide
657 */
658 private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) {
659 return (nc != null &&
660 satisfiedByNetCapabilities(nc, onlyImmutable) &&
661 satisfiedByTransportTypes(nc) &&
662 (onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900663 satisfiedBySpecifier(nc) &&
664 (onlyImmutable || satisfiedBySignalStrength(nc)));
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900665 }
666
667 /**
668 * Check if our requirements are satisfied by the given {@code NetworkCapabilities}.
669 *
670 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
671 *
672 * @hide
Robert Greenwalt1448f052014-04-08 13:41:39 -0700673 */
674 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Lorenzo Colitti260a36d2015-07-08 12:49:04 +0900675 return satisfiedByNetworkCapabilities(nc, false);
676 }
677
678 /**
679 * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}.
680 *
681 * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements.
682 *
683 * @hide
684 */
685 public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) {
686 return satisfiedByNetworkCapabilities(nc, true);
687 }
688
689 /**
690 * Checks that our immutable capabilities are the same as those of the given
691 * {@code NetworkCapabilities}.
692 *
693 * @hide
694 */
695 public boolean equalImmutableCapabilities(NetworkCapabilities nc) {
696 if (nc == null) return false;
697 return (equalsNetCapabilitiesImmutable(nc) &&
698 equalsTransportTypes(nc) &&
699 equalsSpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700700 }
701
702 @Override
703 public boolean equals(Object obj) {
704 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
705 NetworkCapabilities that = (NetworkCapabilities)obj;
706 return (equalsNetCapabilities(that) &&
707 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700708 equalsLinkBandwidths(that) &&
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900709 equalsSignalStrength(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700710 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700711 }
712
713 @Override
714 public int hashCode() {
715 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
716 ((int)(mNetworkCapabilities >> 32) * 3) +
717 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
718 ((int)(mTransportTypes >> 32) * 7) +
719 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700720 (mLinkDownBandwidthKbps * 13) +
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900721 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17) +
722 (mSignalStrength * 19));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700723 }
724
Wink Saville4e2dea72014-09-20 11:04:03 -0700725 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700726 public int describeContents() {
727 return 0;
728 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700729 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700730 public void writeToParcel(Parcel dest, int flags) {
731 dest.writeLong(mNetworkCapabilities);
732 dest.writeLong(mTransportTypes);
733 dest.writeInt(mLinkUpBandwidthKbps);
734 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700735 dest.writeString(mNetworkSpecifier);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900736 dest.writeInt(mSignalStrength);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700737 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900738
Robert Greenwalt1448f052014-04-08 13:41:39 -0700739 public static final Creator<NetworkCapabilities> CREATOR =
740 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700741 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700742 public NetworkCapabilities createFromParcel(Parcel in) {
743 NetworkCapabilities netCap = new NetworkCapabilities();
744
745 netCap.mNetworkCapabilities = in.readLong();
746 netCap.mTransportTypes = in.readLong();
747 netCap.mLinkUpBandwidthKbps = in.readInt();
748 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700749 netCap.mNetworkSpecifier = in.readString();
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900750 netCap.mSignalStrength = in.readInt();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700751 return netCap;
752 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700753 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700754 public NetworkCapabilities[] newArray(int size) {
755 return new NetworkCapabilities[size];
756 }
757 };
758
Wink Saville4e2dea72014-09-20 11:04:03 -0700759 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700760 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700761 int[] types = getTransportTypes();
762 String transports = (types.length > 0 ? " Transports: " : "");
763 for (int i = 0; i < types.length;) {
764 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700765 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
766 case TRANSPORT_WIFI: transports += "WIFI"; break;
767 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
768 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400769 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700770 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700771 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700772 }
773
Robert Greenwalt7569f182014-06-08 16:42:59 -0700774 types = getCapabilities();
775 String capabilities = (types.length > 0 ? " Capabilities: " : "");
776 for (int i = 0; i < types.length; ) {
777 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700778 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
779 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
780 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
781 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
782 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
783 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
784 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
785 case NET_CAPABILITY_IA: capabilities += "IA"; break;
786 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
787 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
788 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
789 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
790 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
791 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700792 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400793 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Lorenzo Colitti76f67792015-05-14 17:28:27 +0900794 case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700795 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700796 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700797 }
798
799 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
800 mLinkUpBandwidthKbps + "Kbps" : "");
801 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
802 mLinkDownBandwidthKbps + "Kbps" : "");
803
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700804 String specifier = (mNetworkSpecifier == null ?
805 "" : " Specifier: <" + mNetworkSpecifier + ">");
806
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900807 String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
808
809 return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700810 }
811}