blob: ce7ad65fef2d64d04dc3bad8c2f3e5e94d973683 [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() {
40 }
41
42 public NetworkCapabilities(NetworkCapabilities nc) {
43 if (nc != null) {
44 mNetworkCapabilities = nc.mNetworkCapabilities;
45 mTransportTypes = nc.mTransportTypes;
46 mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
47 mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
Robert Greenwalt94badcc2014-07-10 14:53:24 -070048 mNetworkSpecifier = nc.mNetworkSpecifier;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070049 }
50 }
Robert Greenwalt1448f052014-04-08 13:41:39 -070051
52 /**
53 * Represents the network's capabilities. If any are specified they will be satisfied
54 * by any Network that matches all of them.
55 */
Robert Greenwalt16e12ab2014-07-08 15:31:37 -070056 private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED) |
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040057 (1 << NET_CAPABILITY_TRUSTED) | (1 << NET_CAPABILITY_NOT_VPN);
Robert Greenwalt1448f052014-04-08 13:41:39 -070058
59 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -070060 * Indicates this is a network that has the ability to reach the
61 * carrier's MMSC for sending and receiving MMS messages.
Robert Greenwalt1448f052014-04-08 13:41:39 -070062 */
63 public static final int NET_CAPABILITY_MMS = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070064
65 /**
66 * Indicates this is a network that has the ability to reach the carrier's
67 * SUPL server, used to retrieve GPS information.
68 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070069 public static final int NET_CAPABILITY_SUPL = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070070
71 /**
72 * Indicates this is a network that has the ability to reach the carrier's
73 * DUN or tethering gateway.
74 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070075 public static final int NET_CAPABILITY_DUN = 2;
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 * FOTA portal, used for over the air updates.
80 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070081 public static final int NET_CAPABILITY_FOTA = 3;
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 * IMS servers, used for network registration and signaling.
86 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070087 public static final int NET_CAPABILITY_IMS = 4;
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 * CBS servers, used for carrier specific services.
92 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070093 public static final int NET_CAPABILITY_CBS = 5;
Robert Greenwalt01d004e2014-05-18 15:24:21 -070094
95 /**
96 * Indicates this is a network that has the ability to reach a Wi-Fi direct
97 * peer.
98 */
Robert Greenwalt1448f052014-04-08 13:41:39 -070099 public static final int NET_CAPABILITY_WIFI_P2P = 6;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700100
101 /**
102 * Indicates this is a network that has the ability to reach a carrier's
103 * Initial Attach servers.
104 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700105 public static final int NET_CAPABILITY_IA = 7;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700106
107 /**
108 * Indicates this is a network that has the ability to reach a carrier's
109 * RCS servers, used for Rich Communication Services.
110 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700111 public static final int NET_CAPABILITY_RCS = 8;
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 * XCAP servers, used for configuration and control.
116 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700117 public static final int NET_CAPABILITY_XCAP = 9;
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 * Emergency IMS servers, used for network signaling during emergency calls.
122 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700123 public static final int NET_CAPABILITY_EIMS = 10;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700124
125 /**
126 * Indicates that this network is unmetered.
127 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700128 public static final int NET_CAPABILITY_NOT_METERED = 11;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700129
130 /**
131 * Indicates that this network should be able to reach the internet.
132 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700133 public static final int NET_CAPABILITY_INTERNET = 12;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700134
135 /**
136 * Indicates that this network is available for general use. If this is not set
137 * applications should not attempt to communicate on this network. Note that this
138 * is simply informative and not enforcement - enforcement is handled via other means.
139 * Set by default.
140 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700141 public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
142
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700143 /**
144 * Indicates that the user has indicated implicit trust of this network. This
145 * generally means it's a sim-selected carrier, a plugged in ethernet, a paired
146 * BT device or a wifi the user asked to connect to. Untrusted networks
147 * are probably limited to unknown wifi AP. Set by default.
148 */
149 public static final int NET_CAPABILITY_TRUSTED = 14;
150
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400151 /*
152 * Indicates that this network is not a VPN. This capability is set by default and should be
153 * explicitly cleared when creating VPN networks.
154 */
155 public static final int NET_CAPABILITY_NOT_VPN = 15;
156
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700157
Robert Greenwalt1448f052014-04-08 13:41:39 -0700158 private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400159 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700160
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700161 /**
162 * Adds the given capability to this {@code NetworkCapability} instance.
163 * Multiple capabilities may be applied sequentially. Note that when searching
164 * for a network to satisfy a request, all capabilities requested must be satisfied.
165 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700166 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
167 * @return This NetworkCapability to facilitate chaining.
168 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700169 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700170 public NetworkCapabilities addCapability(int capability) {
171 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700172 throw new IllegalArgumentException("NetworkCapability out of range");
173 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700174 mNetworkCapabilities |= 1 << capability;
175 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700176 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700177
178 /**
179 * Removes (if found) the given capability from this {@code NetworkCapability} instance.
180 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700181 * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed.
182 * @return This NetworkCapability to facilitate chaining.
183 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700184 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700185 public NetworkCapabilities removeCapability(int capability) {
186 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700187 throw new IllegalArgumentException("NetworkCapability out of range");
188 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700189 mNetworkCapabilities &= ~(1 << capability);
190 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700191 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700192
193 /**
194 * Gets all the capabilities set on this {@code NetworkCapability} instance.
195 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700196 * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700197 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700198 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700199 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700200 public int[] getCapabilities() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700201 return enumerateBits(mNetworkCapabilities);
202 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700203
204 /**
205 * Tests for the presence of a capabilitity on this instance.
206 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700207 * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700208 * @return {@code true} if set on this instance.
209 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700210 public boolean hasCapability(int capability) {
211 if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700212 return false;
213 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700214 return ((mNetworkCapabilities & (1 << capability)) != 0);
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700215 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700216
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 private int[] enumerateBits(long val) {
218 int size = Long.bitCount(val);
219 int[] result = new int[size];
220 int index = 0;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700221 int resource = 0;
222 while (val > 0) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700223 if ((val & 1) == 1) result[index++] = resource;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700224 val = val >> 1;
225 resource++;
226 }
227 return result;
228 }
229
230 private void combineNetCapabilities(NetworkCapabilities nc) {
231 this.mNetworkCapabilities |= nc.mNetworkCapabilities;
232 }
233
234 private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) {
235 return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities);
236 }
237
Robert Greenwalt06314e42014-10-29 14:04:06 -0700238 /** @hide */
239 public boolean equalsNetCapabilities(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700240 return (nc.mNetworkCapabilities == this.mNetworkCapabilities);
241 }
242
243 /**
244 * Representing the transport type. Apps should generally not care about transport. A
245 * request for a fast internet connection could be satisfied by a number of different
246 * transports. If any are specified here it will be satisfied a Network that matches
247 * any of them. If a caller doesn't care about the transport it should not specify any.
248 */
249 private long mTransportTypes;
250
251 /**
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700252 * Indicates this network uses a Cellular transport.
Robert Greenwalt1448f052014-04-08 13:41:39 -0700253 */
254 public static final int TRANSPORT_CELLULAR = 0;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700255
256 /**
257 * Indicates this network uses a Wi-Fi transport.
258 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700259 public static final int TRANSPORT_WIFI = 1;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700260
261 /**
262 * Indicates this network uses a Bluetooth transport.
263 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700264 public static final int TRANSPORT_BLUETOOTH = 2;
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700265
266 /**
267 * Indicates this network uses an Ethernet transport.
268 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700269 public static final int TRANSPORT_ETHERNET = 3;
270
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400271 /**
272 * Indicates this network uses a VPN transport.
273 */
274 public static final int TRANSPORT_VPN = 4;
275
Robert Greenwalt1448f052014-04-08 13:41:39 -0700276 private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400277 private static final int MAX_TRANSPORT = TRANSPORT_VPN;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700278
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700279 /**
280 * Adds the given transport type to this {@code NetworkCapability} instance.
281 * Multiple transports may be applied sequentially. Note that when searching
282 * for a network to satisfy a request, any listed in the request will satisfy the request.
283 * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a
284 * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network
285 * to be selected. This is logically different than
286 * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above.
287 *
288 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700289 * @return This NetworkCapability to facilitate chaining.
290 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700291 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700292 public NetworkCapabilities addTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700293 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
294 throw new IllegalArgumentException("TransportType out of range");
295 }
296 mTransportTypes |= 1 << transportType;
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700297 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700298 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700299 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700300
301 /**
302 * Removes (if found) the given transport from this {@code NetworkCapability} instance.
303 *
304 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700305 * @return This NetworkCapability to facilitate chaining.
306 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700307 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700308 public NetworkCapabilities removeTransportType(int transportType) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700309 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
310 throw new IllegalArgumentException("TransportType out of range");
311 }
312 mTransportTypes &= ~(1 << transportType);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700313 setNetworkSpecifier(mNetworkSpecifier); // used for exception checking
Robert Greenwalt7569f182014-06-08 16:42:59 -0700314 return this;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700315 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700316
317 /**
318 * Gets all the transports set on this {@code NetworkCapability} instance.
319 *
Robert Greenwalt7569f182014-06-08 16:42:59 -0700320 * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700321 * for this instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700322 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700323 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700324 public int[] getTransportTypes() {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700325 return enumerateBits(mTransportTypes);
326 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700327
328 /**
329 * Tests for the presence of a transport on this instance.
330 *
331 * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for.
332 * @return {@code true} if set on this instance.
333 */
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700334 public boolean hasTransport(int transportType) {
335 if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) {
336 return false;
337 }
338 return ((mTransportTypes & (1 << transportType)) != 0);
339 }
Robert Greenwalt1448f052014-04-08 13:41:39 -0700340
341 private void combineTransportTypes(NetworkCapabilities nc) {
342 this.mTransportTypes |= nc.mTransportTypes;
343 }
344 private boolean satisfiedByTransportTypes(NetworkCapabilities nc) {
345 return ((this.mTransportTypes == 0) ||
346 ((this.mTransportTypes & nc.mTransportTypes) != 0));
347 }
Robert Greenwalt06314e42014-10-29 14:04:06 -0700348 /** @hide */
349 public boolean equalsTransportTypes(NetworkCapabilities nc) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700350 return (nc.mTransportTypes == this.mTransportTypes);
351 }
352
353 /**
354 * Passive link bandwidth. This is a rough guide of the expected peak bandwidth
355 * for the first hop on the given transport. It is not measured, but may take into account
356 * link parameters (Radio technology, allocated channels, etc).
357 */
358 private int mLinkUpBandwidthKbps;
359 private int mLinkDownBandwidthKbps;
360
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700361 /**
362 * Sets the upstream bandwidth for this network in Kbps. This always only refers to
363 * the estimated first hop transport bandwidth.
364 * <p>
365 * Note that when used to request a network, this specifies the minimum acceptable.
366 * When received as the state of an existing network this specifies the typical
367 * first hop bandwidth expected. This is never measured, but rather is inferred
368 * from technology type and other link parameters. It could be used to differentiate
369 * between very slow 1xRTT cellular links and other faster networks or even between
370 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
371 * fast backhauls and slow backhauls.
372 *
373 * @param upKbps the estimated first hop upstream (device to network) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700374 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700375 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700376 public void setLinkUpstreamBandwidthKbps(int upKbps) {
377 mLinkUpBandwidthKbps = upKbps;
378 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700379
380 /**
381 * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to
382 * the estimated first hop transport bandwidth.
383 *
384 * @return The estimated first hop upstream (device to network) bandwidth.
385 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700386 public int getLinkUpstreamBandwidthKbps() {
387 return mLinkUpBandwidthKbps;
388 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700389
390 /**
391 * Sets the downstream bandwidth for this network in Kbps. This always only refers to
392 * the estimated first hop transport bandwidth.
393 * <p>
394 * Note that when used to request a network, this specifies the minimum acceptable.
395 * When received as the state of an existing network this specifies the typical
396 * first hop bandwidth expected. This is never measured, but rather is inferred
397 * from technology type and other link parameters. It could be used to differentiate
398 * between very slow 1xRTT cellular links and other faster networks or even between
399 * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between
400 * fast backhauls and slow backhauls.
401 *
402 * @param downKbps the estimated first hop downstream (network to device) bandwidth.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700403 * @hide
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700404 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700405 public void setLinkDownstreamBandwidthKbps(int downKbps) {
406 mLinkDownBandwidthKbps = downKbps;
407 }
Robert Greenwalt01d004e2014-05-18 15:24:21 -0700408
409 /**
410 * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to
411 * the estimated first hop transport bandwidth.
412 *
413 * @return The estimated first hop downstream (network to device) bandwidth.
414 */
Robert Greenwalt1448f052014-04-08 13:41:39 -0700415 public int getLinkDownstreamBandwidthKbps() {
416 return mLinkDownBandwidthKbps;
417 }
418
419 private void combineLinkBandwidths(NetworkCapabilities nc) {
420 this.mLinkUpBandwidthKbps =
421 Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps);
422 this.mLinkDownBandwidthKbps =
423 Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps);
424 }
425 private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) {
426 return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps ||
427 this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps);
428 }
429 private boolean equalsLinkBandwidths(NetworkCapabilities nc) {
430 return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps &&
431 this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps);
432 }
433
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700434 private String mNetworkSpecifier;
435 /**
436 * Sets the optional bearer specific network specifier.
437 * This has no meaning if a single transport is also not specified, so calling
438 * this without a single transport set will generate an exception, as will
439 * subsequently adding or removing transports after this is set.
440 * </p>
441 * The interpretation of this {@code String} is bearer specific and bearers that use
442 * it should document their particulars. For example, Bluetooth may use some sort of
Robert Greenwalt94badcc2014-07-10 14:53:24 -0700443 * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name)
444 * or Subscription ID.
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700445 *
446 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
447 * specific network specifier where the bearer has a choice of
448 * networks.
449 * @hide
450 */
451 public void setNetworkSpecifier(String networkSpecifier) {
452 if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) {
453 throw new IllegalStateException("Must have a single transport specified to use " +
454 "setNetworkSpecifier");
455 }
456 mNetworkSpecifier = networkSpecifier;
457 }
458
459 /**
460 * Gets the optional bearer specific network specifier.
461 *
462 * @return The optional {@code String} specifying the bearer specific network specifier.
463 * See {@link #setNetworkSpecifier}.
464 * @hide
465 */
466 public String getNetworkSpecifier() {
467 return mNetworkSpecifier;
468 }
469
470 private void combineSpecifiers(NetworkCapabilities nc) {
471 String otherSpecifier = nc.getNetworkSpecifier();
472 if (TextUtils.isEmpty(otherSpecifier)) return;
473 if (TextUtils.isEmpty(mNetworkSpecifier) == false) {
474 throw new IllegalStateException("Can't combine two networkSpecifiers");
475 }
476 setNetworkSpecifier(otherSpecifier);
477 }
478 private boolean satisfiedBySpecifier(NetworkCapabilities nc) {
479 return (TextUtils.isEmpty(mNetworkSpecifier) ||
480 mNetworkSpecifier.equals(nc.mNetworkSpecifier));
481 }
482 private boolean equalsSpecifier(NetworkCapabilities nc) {
483 if (TextUtils.isEmpty(mNetworkSpecifier)) {
484 return TextUtils.isEmpty(nc.mNetworkSpecifier);
485 } else {
486 return mNetworkSpecifier.equals(nc.mNetworkSpecifier);
487 }
488 }
489
Robert Greenwalt1448f052014-04-08 13:41:39 -0700490 /**
491 * Combine a set of Capabilities to this one. Useful for coming up with the complete set
492 * {@hide}
493 */
494 public void combineCapabilities(NetworkCapabilities nc) {
495 combineNetCapabilities(nc);
496 combineTransportTypes(nc);
497 combineLinkBandwidths(nc);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700498 combineSpecifiers(nc);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700499 }
500
501 /**
502 * Check if our requirements are satisfied by the given Capabilities.
503 * {@hide}
504 */
505 public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) {
Robert Greenwalt5c55e332014-05-08 00:02:04 -0700506 return (nc != null &&
507 satisfiedByNetCapabilities(nc) &&
Robert Greenwalt1448f052014-04-08 13:41:39 -0700508 satisfiedByTransportTypes(nc) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700509 satisfiedByLinkBandwidths(nc) &&
510 satisfiedBySpecifier(nc));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700511 }
512
513 @Override
514 public boolean equals(Object obj) {
515 if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
516 NetworkCapabilities that = (NetworkCapabilities)obj;
517 return (equalsNetCapabilities(that) &&
518 equalsTransportTypes(that) &&
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700519 equalsLinkBandwidths(that) &&
520 equalsSpecifier(that));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700521 }
522
523 @Override
524 public int hashCode() {
525 return ((int)(mNetworkCapabilities & 0xFFFFFFFF) +
526 ((int)(mNetworkCapabilities >> 32) * 3) +
527 ((int)(mTransportTypes & 0xFFFFFFFF) * 5) +
528 ((int)(mTransportTypes >> 32) * 7) +
529 (mLinkUpBandwidthKbps * 11) +
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700530 (mLinkDownBandwidthKbps * 13) +
531 (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
Robert Greenwalt1448f052014-04-08 13:41:39 -0700532 }
533
Wink Saville4e2dea72014-09-20 11:04:03 -0700534 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700535 public int describeContents() {
536 return 0;
537 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700538 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700539 public void writeToParcel(Parcel dest, int flags) {
540 dest.writeLong(mNetworkCapabilities);
541 dest.writeLong(mTransportTypes);
542 dest.writeInt(mLinkUpBandwidthKbps);
543 dest.writeInt(mLinkDownBandwidthKbps);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700544 dest.writeString(mNetworkSpecifier);
Robert Greenwalt1448f052014-04-08 13:41:39 -0700545 }
546 public static final Creator<NetworkCapabilities> CREATOR =
547 new Creator<NetworkCapabilities>() {
Wink Saville4e2dea72014-09-20 11:04:03 -0700548 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700549 public NetworkCapabilities createFromParcel(Parcel in) {
550 NetworkCapabilities netCap = new NetworkCapabilities();
551
552 netCap.mNetworkCapabilities = in.readLong();
553 netCap.mTransportTypes = in.readLong();
554 netCap.mLinkUpBandwidthKbps = in.readInt();
555 netCap.mLinkDownBandwidthKbps = in.readInt();
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700556 netCap.mNetworkSpecifier = in.readString();
Robert Greenwalt1448f052014-04-08 13:41:39 -0700557 return netCap;
558 }
Wink Saville4e2dea72014-09-20 11:04:03 -0700559 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700560 public NetworkCapabilities[] newArray(int size) {
561 return new NetworkCapabilities[size];
562 }
563 };
564
Wink Saville4e2dea72014-09-20 11:04:03 -0700565 @Override
Robert Greenwalt1448f052014-04-08 13:41:39 -0700566 public String toString() {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700567 int[] types = getTransportTypes();
568 String transports = (types.length > 0 ? " Transports: " : "");
569 for (int i = 0; i < types.length;) {
570 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700571 case TRANSPORT_CELLULAR: transports += "CELLULAR"; break;
572 case TRANSPORT_WIFI: transports += "WIFI"; break;
573 case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break;
574 case TRANSPORT_ETHERNET: transports += "ETHERNET"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400575 case TRANSPORT_VPN: transports += "VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700576 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700577 if (++i < types.length) transports += "|";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700578 }
579
Robert Greenwalt7569f182014-06-08 16:42:59 -0700580 types = getCapabilities();
581 String capabilities = (types.length > 0 ? " Capabilities: " : "");
582 for (int i = 0; i < types.length; ) {
583 switch (types[i]) {
Robert Greenwalt1448f052014-04-08 13:41:39 -0700584 case NET_CAPABILITY_MMS: capabilities += "MMS"; break;
585 case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break;
586 case NET_CAPABILITY_DUN: capabilities += "DUN"; break;
587 case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break;
588 case NET_CAPABILITY_IMS: capabilities += "IMS"; break;
589 case NET_CAPABILITY_CBS: capabilities += "CBS"; break;
590 case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break;
591 case NET_CAPABILITY_IA: capabilities += "IA"; break;
592 case NET_CAPABILITY_RCS: capabilities += "RCS"; break;
593 case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break;
594 case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break;
595 case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break;
596 case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break;
597 case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break;
Robert Greenwalt16e12ab2014-07-08 15:31:37 -0700598 case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400599 case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break;
Robert Greenwalt1448f052014-04-08 13:41:39 -0700600 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700601 if (++i < types.length) capabilities += "&";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700602 }
603
604 String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" +
605 mLinkUpBandwidthKbps + "Kbps" : "");
606 String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" +
607 mLinkDownBandwidthKbps + "Kbps" : "");
608
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700609 String specifier = (mNetworkSpecifier == null ?
610 "" : " Specifier: <" + mNetworkSpecifier + ">");
611
612 return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
Robert Greenwalt1448f052014-04-08 13:41:39 -0700613 }
614}