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