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 | /** |
| 33 | * A class representing the capabilities of a network |
| 34 | * @hide |
| 35 | */ |
| 36 | public final class NetworkCapabilities implements Parcelable { |
| 37 | private static final String TAG = "NetworkCapabilities"; |
| 38 | private static final boolean DBG = false; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Represents the network's capabilities. If any are specified they will be satisfied |
| 43 | * by any Network that matches all of them. |
| 44 | */ |
| 45 | private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED); |
| 46 | |
| 47 | /** |
| 48 | * Values for NetworkCapabilities. Roughly matches/extends deprecated |
| 49 | * ConnectivityManager TYPE_* |
| 50 | */ |
| 51 | public static final int NET_CAPABILITY_MMS = 0; |
| 52 | public static final int NET_CAPABILITY_SUPL = 1; |
| 53 | public static final int NET_CAPABILITY_DUN = 2; |
| 54 | public static final int NET_CAPABILITY_FOTA = 3; |
| 55 | public static final int NET_CAPABILITY_IMS = 4; |
| 56 | public static final int NET_CAPABILITY_CBS = 5; |
| 57 | public static final int NET_CAPABILITY_WIFI_P2P = 6; |
| 58 | public static final int NET_CAPABILITY_IA = 7; |
| 59 | public static final int NET_CAPABILITY_RCS = 8; |
| 60 | public static final int NET_CAPABILITY_XCAP = 9; |
| 61 | public static final int NET_CAPABILITY_EIMS = 10; |
| 62 | public static final int NET_CAPABILITY_NOT_METERED = 11; |
| 63 | public static final int NET_CAPABILITY_INTERNET = 12; |
| 64 | /** Set by default */ |
| 65 | public static final int NET_CAPABILITY_NOT_RESTRICTED = 13; |
| 66 | |
| 67 | private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; |
| 68 | private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_NOT_RESTRICTED; |
| 69 | |
| 70 | public void addNetworkCapability(int networkCapability) { |
| 71 | if (networkCapability < MIN_NET_CAPABILITY || |
| 72 | networkCapability > MAX_NET_CAPABILITY) { |
| 73 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 74 | } |
| 75 | mNetworkCapabilities |= 1 << networkCapability; |
| 76 | } |
| 77 | public void removeNetworkCapability(int networkCapability) { |
| 78 | if (networkCapability < MIN_NET_CAPABILITY || |
| 79 | networkCapability > MAX_NET_CAPABILITY) { |
| 80 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 81 | } |
| 82 | mNetworkCapabilities &= ~(1 << networkCapability); |
| 83 | } |
| 84 | public Collection<Integer> getNetworkCapabilities() { |
| 85 | return enumerateBits(mNetworkCapabilities); |
| 86 | } |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 87 | public boolean hasCapability(int networkCapability) { |
| 88 | if (networkCapability < MIN_NET_CAPABILITY || |
| 89 | networkCapability > MAX_NET_CAPABILITY) { |
| 90 | return false; |
| 91 | } |
| 92 | return ((mNetworkCapabilities & (1 << networkCapability)) != 0); |
| 93 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 94 | |
| 95 | private Collection<Integer> enumerateBits(long val) { |
| 96 | ArrayList<Integer> result = new ArrayList<Integer>(); |
| 97 | int resource = 0; |
| 98 | while (val > 0) { |
| 99 | if ((val & 1) == 1) result.add(resource); |
| 100 | val = val >> 1; |
| 101 | resource++; |
| 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | private void combineNetCapabilities(NetworkCapabilities nc) { |
| 107 | this.mNetworkCapabilities |= nc.mNetworkCapabilities; |
| 108 | } |
| 109 | |
| 110 | private boolean satisfiedByNetCapabilities(NetworkCapabilities nc) { |
| 111 | return ((nc.mNetworkCapabilities & this.mNetworkCapabilities) == this.mNetworkCapabilities); |
| 112 | } |
| 113 | |
| 114 | private boolean equalsNetCapabilities(NetworkCapabilities nc) { |
| 115 | return (nc.mNetworkCapabilities == this.mNetworkCapabilities); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Representing the transport type. Apps should generally not care about transport. A |
| 120 | * request for a fast internet connection could be satisfied by a number of different |
| 121 | * transports. If any are specified here it will be satisfied a Network that matches |
| 122 | * any of them. If a caller doesn't care about the transport it should not specify any. |
| 123 | */ |
| 124 | private long mTransportTypes; |
| 125 | |
| 126 | /** |
| 127 | * Values for TransportType |
| 128 | */ |
| 129 | public static final int TRANSPORT_CELLULAR = 0; |
| 130 | public static final int TRANSPORT_WIFI = 1; |
| 131 | public static final int TRANSPORT_BLUETOOTH = 2; |
| 132 | public static final int TRANSPORT_ETHERNET = 3; |
| 133 | |
| 134 | private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR; |
| 135 | private static final int MAX_TRANSPORT = TRANSPORT_ETHERNET; |
| 136 | |
| 137 | public void addTransportType(int transportType) { |
| 138 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 139 | throw new IllegalArgumentException("TransportType out of range"); |
| 140 | } |
| 141 | mTransportTypes |= 1 << transportType; |
| 142 | } |
| 143 | public void removeTransportType(int transportType) { |
| 144 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 145 | throw new IllegalArgumentException("TransportType out of range"); |
| 146 | } |
| 147 | mTransportTypes &= ~(1 << transportType); |
| 148 | } |
| 149 | public Collection<Integer> getTransportTypes() { |
| 150 | return enumerateBits(mTransportTypes); |
| 151 | } |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 152 | public boolean hasTransport(int transportType) { |
| 153 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 154 | return false; |
| 155 | } |
| 156 | return ((mTransportTypes & (1 << transportType)) != 0); |
| 157 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 158 | |
| 159 | private void combineTransportTypes(NetworkCapabilities nc) { |
| 160 | this.mTransportTypes |= nc.mTransportTypes; |
| 161 | } |
| 162 | private boolean satisfiedByTransportTypes(NetworkCapabilities nc) { |
| 163 | return ((this.mTransportTypes == 0) || |
| 164 | ((this.mTransportTypes & nc.mTransportTypes) != 0)); |
| 165 | } |
| 166 | private boolean equalsTransportTypes(NetworkCapabilities nc) { |
| 167 | return (nc.mTransportTypes == this.mTransportTypes); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Passive link bandwidth. This is a rough guide of the expected peak bandwidth |
| 172 | * for the first hop on the given transport. It is not measured, but may take into account |
| 173 | * link parameters (Radio technology, allocated channels, etc). |
| 174 | */ |
| 175 | private int mLinkUpBandwidthKbps; |
| 176 | private int mLinkDownBandwidthKbps; |
| 177 | |
| 178 | public void setLinkUpstreamBandwidthKbps(int upKbps) { |
| 179 | mLinkUpBandwidthKbps = upKbps; |
| 180 | } |
| 181 | public int getLinkUpstreamBandwidthKbps() { |
| 182 | return mLinkUpBandwidthKbps; |
| 183 | } |
| 184 | public void setLinkDownstreamBandwidthKbps(int downKbps) { |
| 185 | mLinkDownBandwidthKbps = downKbps; |
| 186 | } |
| 187 | public int getLinkDownstreamBandwidthKbps() { |
| 188 | return mLinkDownBandwidthKbps; |
| 189 | } |
| 190 | |
| 191 | private void combineLinkBandwidths(NetworkCapabilities nc) { |
| 192 | this.mLinkUpBandwidthKbps = |
| 193 | Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps); |
| 194 | this.mLinkDownBandwidthKbps = |
| 195 | Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps); |
| 196 | } |
| 197 | private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) { |
| 198 | return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps || |
| 199 | this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps); |
| 200 | } |
| 201 | private boolean equalsLinkBandwidths(NetworkCapabilities nc) { |
| 202 | return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && |
| 203 | this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Combine a set of Capabilities to this one. Useful for coming up with the complete set |
| 208 | * {@hide} |
| 209 | */ |
| 210 | public void combineCapabilities(NetworkCapabilities nc) { |
| 211 | combineNetCapabilities(nc); |
| 212 | combineTransportTypes(nc); |
| 213 | combineLinkBandwidths(nc); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Check if our requirements are satisfied by the given Capabilities. |
| 218 | * {@hide} |
| 219 | */ |
| 220 | public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) { |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 221 | return (nc != null && |
| 222 | satisfiedByNetCapabilities(nc) && |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 223 | satisfiedByTransportTypes(nc) && |
| 224 | satisfiedByLinkBandwidths(nc)); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public boolean equals(Object obj) { |
| 229 | if (obj == null || (obj instanceof NetworkCapabilities == false)) return false; |
| 230 | NetworkCapabilities that = (NetworkCapabilities)obj; |
| 231 | return (equalsNetCapabilities(that) && |
| 232 | equalsTransportTypes(that) && |
| 233 | equalsLinkBandwidths(that)); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public int hashCode() { |
| 238 | return ((int)(mNetworkCapabilities & 0xFFFFFFFF) + |
| 239 | ((int)(mNetworkCapabilities >> 32) * 3) + |
| 240 | ((int)(mTransportTypes & 0xFFFFFFFF) * 5) + |
| 241 | ((int)(mTransportTypes >> 32) * 7) + |
| 242 | (mLinkUpBandwidthKbps * 11) + |
| 243 | (mLinkDownBandwidthKbps * 13)); |
| 244 | } |
| 245 | |
| 246 | public NetworkCapabilities() { |
| 247 | } |
| 248 | |
| 249 | public NetworkCapabilities(NetworkCapabilities nc) { |
Lorenzo Colitti | 0b38f08 | 2014-04-21 14:24:51 +0900 | [diff] [blame] | 250 | if (nc != null) { |
| 251 | mNetworkCapabilities = nc.mNetworkCapabilities; |
| 252 | mTransportTypes = nc.mTransportTypes; |
| 253 | mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps; |
| 254 | mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps; |
| 255 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Parcelable |
| 259 | public int describeContents() { |
| 260 | return 0; |
| 261 | } |
| 262 | public void writeToParcel(Parcel dest, int flags) { |
| 263 | dest.writeLong(mNetworkCapabilities); |
| 264 | dest.writeLong(mTransportTypes); |
| 265 | dest.writeInt(mLinkUpBandwidthKbps); |
| 266 | dest.writeInt(mLinkDownBandwidthKbps); |
| 267 | } |
| 268 | public static final Creator<NetworkCapabilities> CREATOR = |
| 269 | new Creator<NetworkCapabilities>() { |
| 270 | public NetworkCapabilities createFromParcel(Parcel in) { |
| 271 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 272 | |
| 273 | netCap.mNetworkCapabilities = in.readLong(); |
| 274 | netCap.mTransportTypes = in.readLong(); |
| 275 | netCap.mLinkUpBandwidthKbps = in.readInt(); |
| 276 | netCap.mLinkDownBandwidthKbps = in.readInt(); |
| 277 | return netCap; |
| 278 | } |
| 279 | public NetworkCapabilities[] newArray(int size) { |
| 280 | return new NetworkCapabilities[size]; |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | public String toString() { |
| 285 | Collection<Integer> types = getTransportTypes(); |
| 286 | String transports = (types.size() > 0 ? " Transports: " : ""); |
| 287 | Iterator<Integer> i = types.iterator(); |
| 288 | while (i.hasNext()) { |
| 289 | switch (i.next()) { |
| 290 | case TRANSPORT_CELLULAR: transports += "CELLULAR"; break; |
| 291 | case TRANSPORT_WIFI: transports += "WIFI"; break; |
| 292 | case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break; |
| 293 | case TRANSPORT_ETHERNET: transports += "ETHERNET"; break; |
| 294 | } |
| 295 | if (i.hasNext()) transports += "|"; |
| 296 | } |
| 297 | |
| 298 | types = getNetworkCapabilities(); |
| 299 | String capabilities = (types.size() > 0 ? " Capabilities: " : ""); |
| 300 | i = types.iterator(); |
| 301 | while (i.hasNext()) { |
| 302 | switch (i.next().intValue()) { |
| 303 | case NET_CAPABILITY_MMS: capabilities += "MMS"; break; |
| 304 | case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break; |
| 305 | case NET_CAPABILITY_DUN: capabilities += "DUN"; break; |
| 306 | case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break; |
| 307 | case NET_CAPABILITY_IMS: capabilities += "IMS"; break; |
| 308 | case NET_CAPABILITY_CBS: capabilities += "CBS"; break; |
| 309 | case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break; |
| 310 | case NET_CAPABILITY_IA: capabilities += "IA"; break; |
| 311 | case NET_CAPABILITY_RCS: capabilities += "RCS"; break; |
| 312 | case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break; |
| 313 | case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break; |
| 314 | case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break; |
| 315 | case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break; |
| 316 | case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break; |
| 317 | } |
| 318 | if (i.hasNext()) capabilities += "&"; |
| 319 | } |
| 320 | |
| 321 | String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + |
| 322 | mLinkUpBandwidthKbps + "Kbps" : ""); |
| 323 | String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" + |
| 324 | mLinkDownBandwidthKbps + "Kbps" : ""); |
| 325 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame^] | 326 | return "[" + transports + capabilities + upBand + dnBand + "]"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 327 | } |
| 328 | } |