Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | import java.lang.IllegalArgumentException; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Collection; |
| 26 | import java.util.HashMap; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.Map; |
| 29 | import java.util.Map.Entry; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 33 | * This class represents the capabilities of a network. This is used both to specify |
| 34 | * needs to {@link ConnectivityManager} and when inspecting a network. |
| 35 | * |
| 36 | * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method |
| 37 | * of network selection. Rather than indicate a need for Wi-Fi because an application |
| 38 | * needs high bandwidth and risk obselence when a new, fast network appears (like LTE), |
| 39 | * the application should specify it needs high bandwidth. Similarly if an application |
| 40 | * needs an unmetered network for a bulk transfer it can specify that rather than assuming |
| 41 | * all cellular based connections are metered and all Wi-Fi based connections are not. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 42 | */ |
| 43 | public final class NetworkCapabilities implements Parcelable { |
| 44 | private static final String TAG = "NetworkCapabilities"; |
| 45 | private static final boolean DBG = false; |
| 46 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 47 | /** |
| 48 | * @hide |
| 49 | */ |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 50 | public NetworkCapabilities() { |
| 51 | } |
| 52 | |
| 53 | public NetworkCapabilities(NetworkCapabilities nc) { |
| 54 | if (nc != null) { |
| 55 | mNetworkCapabilities = nc.mNetworkCapabilities; |
| 56 | mTransportTypes = nc.mTransportTypes; |
| 57 | mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps; |
| 58 | mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps; |
| 59 | } |
| 60 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Represents the network's capabilities. If any are specified they will be satisfied |
| 64 | * by any Network that matches all of them. |
| 65 | */ |
| 66 | private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED); |
| 67 | |
| 68 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 69 | * Indicates this is a network that has the ability to reach the |
| 70 | * carrier's MMSC for sending and receiving MMS messages. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 71 | */ |
| 72 | public static final int NET_CAPABILITY_MMS = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Indicates this is a network that has the ability to reach the carrier's |
| 76 | * SUPL server, used to retrieve GPS information. |
| 77 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 78 | public static final int NET_CAPABILITY_SUPL = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Indicates this is a network that has the ability to reach the carrier's |
| 82 | * DUN or tethering gateway. |
| 83 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 84 | public static final int NET_CAPABILITY_DUN = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Indicates this is a network that has the ability to reach the carrier's |
| 88 | * FOTA portal, used for over the air updates. |
| 89 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 90 | public static final int NET_CAPABILITY_FOTA = 3; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Indicates this is a network that has the ability to reach the carrier's |
| 94 | * IMS servers, used for network registration and signaling. |
| 95 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 96 | public static final int NET_CAPABILITY_IMS = 4; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Indicates this is a network that has the ability to reach the carrier's |
| 100 | * CBS servers, used for carrier specific services. |
| 101 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 102 | public static final int NET_CAPABILITY_CBS = 5; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Indicates this is a network that has the ability to reach a Wi-Fi direct |
| 106 | * peer. |
| 107 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 108 | public static final int NET_CAPABILITY_WIFI_P2P = 6; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Indicates this is a network that has the ability to reach a carrier's |
| 112 | * Initial Attach servers. |
| 113 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 114 | public static final int NET_CAPABILITY_IA = 7; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Indicates this is a network that has the ability to reach a carrier's |
| 118 | * RCS servers, used for Rich Communication Services. |
| 119 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 120 | public static final int NET_CAPABILITY_RCS = 8; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Indicates this is a network that has the ability to reach a carrier's |
| 124 | * XCAP servers, used for configuration and control. |
| 125 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 126 | public static final int NET_CAPABILITY_XCAP = 9; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Indicates this is a network that has the ability to reach a carrier's |
| 130 | * Emergency IMS servers, used for network signaling during emergency calls. |
| 131 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 132 | public static final int NET_CAPABILITY_EIMS = 10; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Indicates that this network is unmetered. |
| 136 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 137 | public static final int NET_CAPABILITY_NOT_METERED = 11; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Indicates that this network should be able to reach the internet. |
| 141 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 142 | public static final int NET_CAPABILITY_INTERNET = 12; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Indicates that this network is available for general use. If this is not set |
| 146 | * applications should not attempt to communicate on this network. Note that this |
| 147 | * is simply informative and not enforcement - enforcement is handled via other means. |
| 148 | * Set by default. |
| 149 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 150 | public static final int NET_CAPABILITY_NOT_RESTRICTED = 13; |
| 151 | |
| 152 | private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; |
| 153 | private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_RESTRICTED; |
| 154 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Adds the given capability to this {@code NetworkCapability} instance. |
| 157 | * Multiple capabilities may be applied sequentially. Note that when searching |
| 158 | * for a network to satisfy a request, all capabilities requested must be satisfied. |
| 159 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 160 | * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added. |
| 161 | * @return This NetworkCapability to facilitate chaining. |
| 162 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 163 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 164 | public NetworkCapabilities addCapability(int capability) { |
| 165 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 166 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 167 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 168 | mNetworkCapabilities |= 1 << capability; |
| 169 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 170 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Removes (if found) the given capability from this {@code NetworkCapability} instance. |
| 174 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 175 | * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed. |
| 176 | * @return This NetworkCapability to facilitate chaining. |
| 177 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 178 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 179 | public NetworkCapabilities removeCapability(int capability) { |
| 180 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 181 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 182 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 183 | mNetworkCapabilities &= ~(1 << capability); |
| 184 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 185 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * Gets all the capabilities set on this {@code NetworkCapability} instance. |
| 189 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 190 | * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 191 | * for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 192 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 193 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 194 | public int[] getCapabilities() { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 195 | return enumerateBits(mNetworkCapabilities); |
| 196 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * Tests for the presence of a capabilitity on this instance. |
| 200 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 201 | * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 202 | * @return {@code true} if set on this instance. |
| 203 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 204 | public boolean hasCapability(int capability) { |
| 205 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 208 | return ((mNetworkCapabilities & (1 << capability)) != 0); |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 209 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 210 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 211 | private int[] enumerateBits(long val) { |
| 212 | int size = Long.bitCount(val); |
| 213 | int[] result = new int[size]; |
| 214 | int index = 0; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 215 | int resource = 0; |
| 216 | while (val > 0) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 217 | if ((val & 1) == 1) result[index++] = resource; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 218 | val = val >> 1; |
| 219 | resource++; |
| 220 | } |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | private void combineNetCapabilities(NetworkCapabilities nc) { |
| 225 | this.mNetworkCapabilities |= nc.mNetworkCapabilities; |
| 226 | } |
| 227 | |
| 228 | private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) { |
| 229 | return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities); |
| 230 | } |
| 231 | |
| 232 | private boolean equalsNetCapabilities(NetworkCapabilities nc) { |
| 233 | return (nc.mNetworkCapabilities == this.mNetworkCapabilities); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Representing the transport type. Apps should generally not care about transport. A |
| 238 | * request for a fast internet connection could be satisfied by a number of different |
| 239 | * transports. If any are specified here it will be satisfied a Network that matches |
| 240 | * any of them. If a caller doesn't care about the transport it should not specify any. |
| 241 | */ |
| 242 | private long mTransportTypes; |
| 243 | |
| 244 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 245 | * Indicates this network uses a Cellular transport. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 246 | */ |
| 247 | public static final int TRANSPORT_CELLULAR = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Indicates this network uses a Wi-Fi transport. |
| 251 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 252 | public static final int TRANSPORT_WIFI = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * Indicates this network uses a Bluetooth transport. |
| 256 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 257 | public static final int TRANSPORT_BLUETOOTH = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Indicates this network uses an Ethernet transport. |
| 261 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 262 | public static final int TRANSPORT_ETHERNET = 3; |
| 263 | |
| 264 | private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR; |
| 265 | private static final int MAX_TRANSPORT = TRANSPORT_ETHERNET; |
| 266 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Adds the given transport type to this {@code NetworkCapability} instance. |
| 269 | * Multiple transports may be applied sequentially. Note that when searching |
| 270 | * for a network to satisfy a request, any listed in the request will satisfy the request. |
| 271 | * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a |
| 272 | * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network |
| 273 | * to be selected. This is logically different than |
| 274 | * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. |
| 275 | * |
| 276 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 277 | * @return This NetworkCapability to facilitate chaining. |
| 278 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 279 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 280 | public NetworkCapabilities addTransportType(int transportType) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 281 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 282 | throw new IllegalArgumentException("TransportType out of range"); |
| 283 | } |
| 284 | mTransportTypes |= 1 << transportType; |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 285 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 286 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Removes (if found) the given transport from this {@code NetworkCapability} instance. |
| 290 | * |
| 291 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 292 | * @return This NetworkCapability to facilitate chaining. |
| 293 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 294 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 295 | public NetworkCapabilities removeTransportType(int transportType) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 296 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 297 | throw new IllegalArgumentException("TransportType out of range"); |
| 298 | } |
| 299 | mTransportTypes &= ~(1 << transportType); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 300 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 301 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * Gets all the transports set on this {@code NetworkCapability} instance. |
| 305 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 306 | * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 307 | * for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 308 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 309 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 310 | public int[] getTransportTypes() { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 311 | return enumerateBits(mTransportTypes); |
| 312 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * Tests for the presence of a transport on this instance. |
| 316 | * |
| 317 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for. |
| 318 | * @return {@code true} if set on this instance. |
| 319 | */ |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 320 | public boolean hasTransport(int transportType) { |
| 321 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 322 | return false; |
| 323 | } |
| 324 | return ((mTransportTypes & (1 << transportType)) != 0); |
| 325 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 326 | |
| 327 | private void combineTransportTypes(NetworkCapabilities nc) { |
| 328 | this.mTransportTypes |= nc.mTransportTypes; |
| 329 | } |
| 330 | private boolean satisfiedByTransportTypes(NetworkCapabilities nc) { |
| 331 | return ((this.mTransportTypes == 0) || |
| 332 | ((this.mTransportTypes & nc.mTransportTypes) != 0)); |
| 333 | } |
| 334 | private boolean equalsTransportTypes(NetworkCapabilities nc) { |
| 335 | return (nc.mTransportTypes == this.mTransportTypes); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Passive link bandwidth. This is a rough guide of the expected peak bandwidth |
| 340 | * for the first hop on the given transport. It is not measured, but may take into account |
| 341 | * link parameters (Radio technology, allocated channels, etc). |
| 342 | */ |
| 343 | private int mLinkUpBandwidthKbps; |
| 344 | private int mLinkDownBandwidthKbps; |
| 345 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 346 | /** |
| 347 | * Sets the upstream bandwidth for this network in Kbps. This always only refers to |
| 348 | * the estimated first hop transport bandwidth. |
| 349 | * <p> |
| 350 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 351 | * When received as the state of an existing network this specifies the typical |
| 352 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 353 | * from technology type and other link parameters. It could be used to differentiate |
| 354 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 355 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 356 | * fast backhauls and slow backhauls. |
| 357 | * |
| 358 | * @param upKbps the estimated first hop upstream (device to network) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 359 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 360 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 361 | public void setLinkUpstreamBandwidthKbps(int upKbps) { |
| 362 | mLinkUpBandwidthKbps = upKbps; |
| 363 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to |
| 367 | * the estimated first hop transport bandwidth. |
| 368 | * |
| 369 | * @return The estimated first hop upstream (device to network) bandwidth. |
| 370 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 371 | public int getLinkUpstreamBandwidthKbps() { |
| 372 | return mLinkUpBandwidthKbps; |
| 373 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * Sets the downstream bandwidth for this network in Kbps. This always only refers to |
| 377 | * the estimated first hop transport bandwidth. |
| 378 | * <p> |
| 379 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 380 | * When received as the state of an existing network this specifies the typical |
| 381 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 382 | * from technology type and other link parameters. It could be used to differentiate |
| 383 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 384 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 385 | * fast backhauls and slow backhauls. |
| 386 | * |
| 387 | * @param downKbps the estimated first hop downstream (network to device) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 388 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 389 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 390 | public void setLinkDownstreamBandwidthKbps(int downKbps) { |
| 391 | mLinkDownBandwidthKbps = downKbps; |
| 392 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 393 | |
| 394 | /** |
| 395 | * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to |
| 396 | * the estimated first hop transport bandwidth. |
| 397 | * |
| 398 | * @return The estimated first hop downstream (network to device) bandwidth. |
| 399 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 400 | public int getLinkDownstreamBandwidthKbps() { |
| 401 | return mLinkDownBandwidthKbps; |
| 402 | } |
| 403 | |
| 404 | private void combineLinkBandwidths(NetworkCapabilities nc) { |
| 405 | this.mLinkUpBandwidthKbps = |
| 406 | Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps); |
| 407 | this.mLinkDownBandwidthKbps = |
| 408 | Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps); |
| 409 | } |
| 410 | private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) { |
| 411 | return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps || |
| 412 | this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps); |
| 413 | } |
| 414 | private boolean equalsLinkBandwidths(NetworkCapabilities nc) { |
| 415 | return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && |
| 416 | this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Combine a set of Capabilities to this one. Useful for coming up with the complete set |
| 421 | * {@hide} |
| 422 | */ |
| 423 | public void combineCapabilities(NetworkCapabilities nc) { |
| 424 | combineNetCapabilities(nc); |
| 425 | combineTransportTypes(nc); |
| 426 | combineLinkBandwidths(nc); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Check if our requirements are satisfied by the given Capabilities. |
| 431 | * {@hide} |
| 432 | */ |
| 433 | public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) { |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 434 | return (nc != null && |
| 435 | satisfiedByNetCapabilities(nc) && |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 436 | satisfiedByTransportTypes(nc) && |
| 437 | satisfiedByLinkBandwidths(nc)); |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public boolean equals(Object obj) { |
| 442 | if (obj == null || (obj instanceof NetworkCapabilities == false)) return false; |
| 443 | NetworkCapabilities that = (NetworkCapabilities)obj; |
| 444 | return (equalsNetCapabilities(that) && |
| 445 | equalsTransportTypes(that) && |
| 446 | equalsLinkBandwidths(that)); |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public int hashCode() { |
| 451 | return ((int)(mNetworkCapabilities & 0xFFFFFFFF) + |
| 452 | ((int)(mNetworkCapabilities >> 32) * 3) + |
| 453 | ((int)(mTransportTypes & 0xFFFFFFFF) * 5) + |
| 454 | ((int)(mTransportTypes >> 32) * 7) + |
| 455 | (mLinkUpBandwidthKbps * 11) + |
| 456 | (mLinkDownBandwidthKbps * 13)); |
| 457 | } |
| 458 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 459 | public int describeContents() { |
| 460 | return 0; |
| 461 | } |
| 462 | public void writeToParcel(Parcel dest, int flags) { |
| 463 | dest.writeLong(mNetworkCapabilities); |
| 464 | dest.writeLong(mTransportTypes); |
| 465 | dest.writeInt(mLinkUpBandwidthKbps); |
| 466 | dest.writeInt(mLinkDownBandwidthKbps); |
| 467 | } |
| 468 | public static final Creator<NetworkCapabilities> CREATOR = |
| 469 | new Creator<NetworkCapabilities>() { |
| 470 | public NetworkCapabilities createFromParcel(Parcel in) { |
| 471 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 472 | |
| 473 | netCap.mNetworkCapabilities = in.readLong(); |
| 474 | netCap.mTransportTypes = in.readLong(); |
| 475 | netCap.mLinkUpBandwidthKbps = in.readInt(); |
| 476 | netCap.mLinkDownBandwidthKbps = in.readInt(); |
| 477 | return netCap; |
| 478 | } |
| 479 | public NetworkCapabilities[] newArray(int size) { |
| 480 | return new NetworkCapabilities[size]; |
| 481 | } |
| 482 | }; |
| 483 | |
| 484 | public String toString() { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 485 | int[] types = getTransportTypes(); |
| 486 | String transports = (types.length > 0 ? " Transports: " : ""); |
| 487 | for (int i = 0; i < types.length;) { |
| 488 | switch (types[i]) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 489 | case TRANSPORT_CELLULAR: transports += "CELLULAR"; break; |
| 490 | case TRANSPORT_WIFI: transports += "WIFI"; break; |
| 491 | case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break; |
| 492 | case TRANSPORT_ETHERNET: transports += "ETHERNET"; break; |
| 493 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 494 | if (++i < types.length) transports += "|"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 497 | types = getCapabilities(); |
| 498 | String capabilities = (types.length > 0 ? " Capabilities: " : ""); |
| 499 | for (int i = 0; i < types.length; ) { |
| 500 | switch (types[i]) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 501 | case NET_CAPABILITY_MMS: capabilities += "MMS"; break; |
| 502 | case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break; |
| 503 | case NET_CAPABILITY_DUN: capabilities += "DUN"; break; |
| 504 | case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break; |
| 505 | case NET_CAPABILITY_IMS: capabilities += "IMS"; break; |
| 506 | case NET_CAPABILITY_CBS: capabilities += "CBS"; break; |
| 507 | case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break; |
| 508 | case NET_CAPABILITY_IA: capabilities += "IA"; break; |
| 509 | case NET_CAPABILITY_RCS: capabilities += "RCS"; break; |
| 510 | case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break; |
| 511 | case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break; |
| 512 | case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break; |
| 513 | case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break; |
| 514 | case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break; |
| 515 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame^] | 516 | if (++i < types.length) capabilities += "&"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + |
| 520 | mLinkUpBandwidthKbps + "Kbps" : ""); |
| 521 | String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" + |
| 522 | mLinkDownBandwidthKbps + "Kbps" : ""); |
| 523 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 524 | return "[" + transports + capabilities + upBand + dnBand + "]"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 525 | } |
| 526 | } |