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; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 21 | import android.text.TextUtils; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 22 | import java.lang.IllegalArgumentException; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 25 | * 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 Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 30 | * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE), |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 31 | * 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 Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 34 | */ |
| 35 | public final class NetworkCapabilities implements Parcelable { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 36 | /** |
| 37 | * @hide |
| 38 | */ |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 39 | public NetworkCapabilities() { |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 40 | clearAll(); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 41 | mNetworkCapabilities = DEFAULT_CAPABILITIES; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public NetworkCapabilities(NetworkCapabilities nc) { |
| 45 | if (nc != null) { |
| 46 | mNetworkCapabilities = nc.mNetworkCapabilities; |
| 47 | mTransportTypes = nc.mTransportTypes; |
| 48 | mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps; |
| 49 | mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps; |
Robert Greenwalt | 94badcc | 2014-07-10 14:53:24 -0700 | [diff] [blame] | 50 | mNetworkSpecifier = nc.mNetworkSpecifier; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 51 | mSignalStrength = nc.mSignalStrength; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 56 | * Completely clears the contents of this object, removing even the capabilities that are set |
| 57 | * by default when the object is constructed. |
| 58 | * @hide |
| 59 | */ |
| 60 | public void clearAll() { |
| 61 | mNetworkCapabilities = mTransportTypes = 0; |
| 62 | mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0; |
| 63 | mNetworkSpecifier = null; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 64 | mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED; |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 68 | * Represents the network's capabilities. If any are specified they will be satisfied |
| 69 | * by any Network that matches all of them. |
| 70 | */ |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 71 | private long mNetworkCapabilities; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 74 | * Indicates this is a network that has the ability to reach the |
| 75 | * carrier's MMSC for sending and receiving MMS messages. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 76 | */ |
| 77 | public static final int NET_CAPABILITY_MMS = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Indicates this is a network that has the ability to reach the carrier's |
| 81 | * SUPL server, used to retrieve GPS information. |
| 82 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 83 | public static final int NET_CAPABILITY_SUPL = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Indicates this is a network that has the ability to reach the carrier's |
| 87 | * DUN or tethering gateway. |
| 88 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 89 | public static final int NET_CAPABILITY_DUN = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Indicates this is a network that has the ability to reach the carrier's |
| 93 | * FOTA portal, used for over the air updates. |
| 94 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 95 | public static final int NET_CAPABILITY_FOTA = 3; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Indicates this is a network that has the ability to reach the carrier's |
| 99 | * IMS servers, used for network registration and signaling. |
| 100 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 101 | public static final int NET_CAPABILITY_IMS = 4; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Indicates this is a network that has the ability to reach the carrier's |
| 105 | * CBS servers, used for carrier specific services. |
| 106 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 107 | public static final int NET_CAPABILITY_CBS = 5; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Indicates this is a network that has the ability to reach a Wi-Fi direct |
| 111 | * peer. |
| 112 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 113 | public static final int NET_CAPABILITY_WIFI_P2P = 6; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Indicates this is a network that has the ability to reach a carrier's |
| 117 | * Initial Attach servers. |
| 118 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 119 | public static final int NET_CAPABILITY_IA = 7; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Indicates this is a network that has the ability to reach a carrier's |
| 123 | * RCS servers, used for Rich Communication Services. |
| 124 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 125 | public static final int NET_CAPABILITY_RCS = 8; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Indicates this is a network that has the ability to reach a carrier's |
| 129 | * XCAP servers, used for configuration and control. |
| 130 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 131 | public static final int NET_CAPABILITY_XCAP = 9; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Indicates this is a network that has the ability to reach a carrier's |
Robert Greenwalt | 4bd4389 | 2015-07-09 14:49:35 -0700 | [diff] [blame] | 135 | * Emergency IMS servers or other services, used for network signaling |
| 136 | * during emergency calls. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 137 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 138 | public static final int NET_CAPABILITY_EIMS = 10; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Indicates that this network is unmetered. |
| 142 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 143 | public static final int NET_CAPABILITY_NOT_METERED = 11; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Indicates that this network should be able to reach the internet. |
| 147 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 148 | public static final int NET_CAPABILITY_INTERNET = 12; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Indicates that this network is available for general use. If this is not set |
| 152 | * applications should not attempt to communicate on this network. Note that this |
| 153 | * is simply informative and not enforcement - enforcement is handled via other means. |
| 154 | * Set by default. |
| 155 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 156 | public static final int NET_CAPABILITY_NOT_RESTRICTED = 13; |
| 157 | |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Indicates that the user has indicated implicit trust of this network. This |
| 160 | * generally means it's a sim-selected carrier, a plugged in ethernet, a paired |
| 161 | * BT device or a wifi the user asked to connect to. Untrusted networks |
| 162 | * are probably limited to unknown wifi AP. Set by default. |
| 163 | */ |
| 164 | public static final int NET_CAPABILITY_TRUSTED = 14; |
| 165 | |
Paul Jensen | 76b610a | 2015-03-18 09:33:07 -0400 | [diff] [blame] | 166 | /** |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 167 | * Indicates that this network is not a VPN. This capability is set by default and should be |
Paul Jensen | 76b610a | 2015-03-18 09:33:07 -0400 | [diff] [blame] | 168 | * explicitly cleared for VPN networks. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 169 | */ |
| 170 | public static final int NET_CAPABILITY_NOT_VPN = 15; |
| 171 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 172 | /** |
| 173 | * Indicates that connectivity on this network was successfully validated. For example, for a |
| 174 | * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully |
| 175 | * detected. |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 176 | */ |
| 177 | public static final int NET_CAPABILITY_VALIDATED = 16; |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 178 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Indicates that this network was found to have a captive portal in place last time it was |
| 181 | * probed. |
| 182 | */ |
| 183 | public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17; |
| 184 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 185 | private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 186 | private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 187 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 188 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 189 | * Network capabilities that are expected to be mutable, i.e., can change while a particular |
| 190 | * network is connected. |
| 191 | */ |
| 192 | private static final long MUTABLE_CAPABILITIES = |
| 193 | // TRUSTED can change when user explicitly connects to an untrusted network in Settings. |
| 194 | // http://b/18206275 |
| 195 | (1 << NET_CAPABILITY_TRUSTED) | |
| 196 | (1 << NET_CAPABILITY_VALIDATED) | |
| 197 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL); |
| 198 | |
| 199 | /** |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 200 | * Network specifier for factories which want to match any network specifier |
| 201 | * (NS) in a request. Behavior: |
| 202 | * <li>Empty NS in request matches any network factory NS</li> |
| 203 | * <li>Empty NS in the network factory NS only matches a request with an |
| 204 | * empty NS</li> |
| 205 | * <li>"*" (this constant) NS in the network factory matches requests with |
| 206 | * any NS</li> |
| 207 | * |
| 208 | * @hide |
| 209 | */ |
| 210 | public static final String MATCH_ALL_REQUESTS_NETWORK_SPECIFIER = "*"; |
| 211 | |
| 212 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 213 | * Network capabilities that are not allowed in NetworkRequests. This exists because the |
| 214 | * NetworkFactory / NetworkAgent model does not deal well with the situation where a |
| 215 | * capability's presence cannot be known in advance. If such a capability is requested, then we |
| 216 | * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then |
| 217 | * get immediately torn down because they do not have the requested capability. |
| 218 | */ |
| 219 | private static final long NON_REQUESTABLE_CAPABILITIES = |
| 220 | (1 << NET_CAPABILITY_VALIDATED) | |
| 221 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL); |
| 222 | |
| 223 | /** |
| 224 | * Capabilities that are set by default when the object is constructed. |
| 225 | */ |
| 226 | private static final long DEFAULT_CAPABILITIES = |
| 227 | (1 << NET_CAPABILITY_NOT_RESTRICTED) | |
| 228 | (1 << NET_CAPABILITY_TRUSTED) | |
| 229 | (1 << NET_CAPABILITY_NOT_VPN); |
| 230 | |
| 231 | /** |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 232 | * Capabilities that suggest that a network is restricted. |
| 233 | * {@see #maybeMarkCapabilitiesRestricted}. |
| 234 | */ |
| 235 | private static final long RESTRICTED_CAPABILITIES = |
| 236 | (1 << NET_CAPABILITY_CBS) | |
| 237 | (1 << NET_CAPABILITY_DUN) | |
| 238 | (1 << NET_CAPABILITY_EIMS) | |
| 239 | (1 << NET_CAPABILITY_FOTA) | |
| 240 | (1 << NET_CAPABILITY_IA) | |
| 241 | (1 << NET_CAPABILITY_IMS) | |
| 242 | (1 << NET_CAPABILITY_RCS) | |
| 243 | (1 << NET_CAPABILITY_XCAP); |
| 244 | |
| 245 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 246 | * Adds the given capability to this {@code NetworkCapability} instance. |
| 247 | * Multiple capabilities may be applied sequentially. Note that when searching |
| 248 | * for a network to satisfy a request, all capabilities requested must be satisfied. |
| 249 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 250 | * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added. |
| 251 | * @return This NetworkCapability to facilitate chaining. |
| 252 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 253 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 254 | public NetworkCapabilities addCapability(int capability) { |
| 255 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 256 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 257 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 258 | mNetworkCapabilities |= 1 << capability; |
| 259 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 260 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * Removes (if found) the given capability from this {@code NetworkCapability} instance. |
| 264 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 265 | * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed. |
| 266 | * @return This NetworkCapability to facilitate chaining. |
| 267 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 268 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 269 | public NetworkCapabilities removeCapability(int capability) { |
| 270 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 271 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 272 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 273 | mNetworkCapabilities &= ~(1 << capability); |
| 274 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 275 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Gets all the capabilities set on this {@code NetworkCapability} instance. |
| 279 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 280 | * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 281 | * for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 282 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 283 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 284 | public int[] getCapabilities() { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 285 | return enumerateBits(mNetworkCapabilities); |
| 286 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Tests for the presence of a capabilitity on this instance. |
| 290 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 291 | * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 292 | * @return {@code true} if set on this instance. |
| 293 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 294 | public boolean hasCapability(int capability) { |
| 295 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 298 | return ((mNetworkCapabilities & (1 << capability)) != 0); |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 299 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 300 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 301 | private int[] enumerateBits(long val) { |
| 302 | int size = Long.bitCount(val); |
| 303 | int[] result = new int[size]; |
| 304 | int index = 0; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 305 | int resource = 0; |
| 306 | while (val > 0) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 307 | if ((val & 1) == 1) result[index++] = resource; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 308 | val = val >> 1; |
| 309 | resource++; |
| 310 | } |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | private void combineNetCapabilities(NetworkCapabilities nc) { |
| 315 | this.mNetworkCapabilities |= nc.mNetworkCapabilities; |
| 316 | } |
| 317 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 318 | /** |
| 319 | * Convenience function that returns a human-readable description of the first mutable |
| 320 | * capability we find. Used to present an error message to apps that request mutable |
| 321 | * capabilities. |
| 322 | * |
| 323 | * @hide |
| 324 | */ |
| 325 | public String describeFirstNonRequestableCapability() { |
| 326 | if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED"; |
| 327 | if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL"; |
| 328 | // This cannot happen unless the preceding checks are incomplete. |
| 329 | if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) { |
| 330 | return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities); |
| 331 | } |
| 332 | if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth"; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 333 | if (hasSignalStrength()) return "signalStrength"; |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 334 | return null; |
| 335 | } |
| 336 | |
| 337 | private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
| 338 | long networkCapabilities = this.mNetworkCapabilities; |
| 339 | if (onlyImmutable) { |
| 340 | networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES; |
| 341 | } |
| 342 | return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 345 | /** @hide */ |
| 346 | public boolean equalsNetCapabilities(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 347 | return (nc.mNetworkCapabilities == this.mNetworkCapabilities); |
| 348 | } |
| 349 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 350 | private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) { |
| 351 | return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) == |
| 352 | (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES)); |
| 353 | } |
| 354 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 355 | /** |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 356 | * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are |
| 357 | * typically provided by restricted networks. |
| 358 | * |
| 359 | * TODO: consider: |
| 360 | * - Renaming it to guessRestrictedCapability and make it set the |
| 361 | * restricted capability bit in addition to clearing it. |
| 362 | * @hide |
| 363 | */ |
| 364 | public void maybeMarkCapabilitiesRestricted() { |
| 365 | // If all the capabilities are typically provided by restricted networks, conclude that this |
| 366 | // network is restricted. |
Paul Jensen | aae613d | 2015-08-19 11:06:15 -0400 | [diff] [blame] | 367 | if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0 && |
| 368 | // Must have at least some restricted capabilities, otherwise a request for an |
| 369 | // internet-less network will get marked restricted. |
| 370 | (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0) { |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 371 | removeCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Paul Jensen | aae613d | 2015-08-19 11:06:15 -0400 | [diff] [blame] | 372 | } |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 376 | * Representing the transport type. Apps should generally not care about transport. A |
| 377 | * request for a fast internet connection could be satisfied by a number of different |
| 378 | * transports. If any are specified here it will be satisfied a Network that matches |
| 379 | * any of them. If a caller doesn't care about the transport it should not specify any. |
| 380 | */ |
| 381 | private long mTransportTypes; |
| 382 | |
| 383 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 384 | * Indicates this network uses a Cellular transport. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 385 | */ |
| 386 | public static final int TRANSPORT_CELLULAR = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * Indicates this network uses a Wi-Fi transport. |
| 390 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 391 | public static final int TRANSPORT_WIFI = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 392 | |
| 393 | /** |
| 394 | * Indicates this network uses a Bluetooth transport. |
| 395 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 396 | public static final int TRANSPORT_BLUETOOTH = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 397 | |
| 398 | /** |
| 399 | * Indicates this network uses an Ethernet transport. |
| 400 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 401 | public static final int TRANSPORT_ETHERNET = 3; |
| 402 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 403 | /** |
| 404 | * Indicates this network uses a VPN transport. |
| 405 | */ |
| 406 | public static final int TRANSPORT_VPN = 4; |
| 407 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 408 | private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 409 | private static final int MAX_TRANSPORT = TRANSPORT_VPN; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 410 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 411 | /** |
| 412 | * Adds the given transport type to this {@code NetworkCapability} instance. |
| 413 | * Multiple transports may be applied sequentially. Note that when searching |
| 414 | * for a network to satisfy a request, any listed in the request will satisfy the request. |
| 415 | * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a |
| 416 | * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network |
| 417 | * to be selected. This is logically different than |
| 418 | * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. |
| 419 | * |
| 420 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 421 | * @return This NetworkCapability to facilitate chaining. |
| 422 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 423 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 424 | public NetworkCapabilities addTransportType(int transportType) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 425 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 426 | throw new IllegalArgumentException("TransportType out of range"); |
| 427 | } |
| 428 | mTransportTypes |= 1 << transportType; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 429 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 430 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 431 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * Removes (if found) the given transport from this {@code NetworkCapability} instance. |
| 435 | * |
| 436 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 437 | * @return This NetworkCapability to facilitate chaining. |
| 438 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 439 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 440 | public NetworkCapabilities removeTransportType(int transportType) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 441 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 442 | throw new IllegalArgumentException("TransportType out of range"); |
| 443 | } |
| 444 | mTransportTypes &= ~(1 << transportType); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 445 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 446 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 447 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * Gets all the transports set on this {@code NetworkCapability} instance. |
| 451 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 452 | * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 453 | * for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 454 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 455 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 456 | public int[] getTransportTypes() { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 457 | return enumerateBits(mTransportTypes); |
| 458 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 459 | |
| 460 | /** |
| 461 | * Tests for the presence of a transport on this instance. |
| 462 | * |
| 463 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for. |
| 464 | * @return {@code true} if set on this instance. |
| 465 | */ |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 466 | public boolean hasTransport(int transportType) { |
| 467 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 468 | return false; |
| 469 | } |
| 470 | return ((mTransportTypes & (1 << transportType)) != 0); |
| 471 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 472 | |
| 473 | private void combineTransportTypes(NetworkCapabilities nc) { |
| 474 | this.mTransportTypes |= nc.mTransportTypes; |
| 475 | } |
| 476 | private boolean satisfiedByTransportTypes(NetworkCapabilities nc) { |
| 477 | return ((this.mTransportTypes == 0) || |
| 478 | ((this.mTransportTypes & nc.mTransportTypes) != 0)); |
| 479 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 480 | /** @hide */ |
| 481 | public boolean equalsTransportTypes(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 482 | return (nc.mTransportTypes == this.mTransportTypes); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Passive link bandwidth. This is a rough guide of the expected peak bandwidth |
| 487 | * for the first hop on the given transport. It is not measured, but may take into account |
| 488 | * link parameters (Radio technology, allocated channels, etc). |
| 489 | */ |
| 490 | private int mLinkUpBandwidthKbps; |
| 491 | private int mLinkDownBandwidthKbps; |
| 492 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 493 | /** |
| 494 | * Sets the upstream bandwidth for this network in Kbps. This always only refers to |
| 495 | * the estimated first hop transport bandwidth. |
| 496 | * <p> |
| 497 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 498 | * When received as the state of an existing network this specifies the typical |
| 499 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 500 | * from technology type and other link parameters. It could be used to differentiate |
| 501 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 502 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 503 | * fast backhauls and slow backhauls. |
| 504 | * |
| 505 | * @param upKbps the estimated first hop upstream (device to network) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 506 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 507 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 508 | public void setLinkUpstreamBandwidthKbps(int upKbps) { |
| 509 | mLinkUpBandwidthKbps = upKbps; |
| 510 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 511 | |
| 512 | /** |
| 513 | * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to |
| 514 | * the estimated first hop transport bandwidth. |
| 515 | * |
| 516 | * @return The estimated first hop upstream (device to network) bandwidth. |
| 517 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 518 | public int getLinkUpstreamBandwidthKbps() { |
| 519 | return mLinkUpBandwidthKbps; |
| 520 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 521 | |
| 522 | /** |
| 523 | * Sets the downstream bandwidth for this network in Kbps. This always only refers to |
| 524 | * the estimated first hop transport bandwidth. |
| 525 | * <p> |
| 526 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 527 | * When received as the state of an existing network this specifies the typical |
| 528 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 529 | * from technology type and other link parameters. It could be used to differentiate |
| 530 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 531 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 532 | * fast backhauls and slow backhauls. |
| 533 | * |
| 534 | * @param downKbps the estimated first hop downstream (network to device) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 535 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 536 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 537 | public void setLinkDownstreamBandwidthKbps(int downKbps) { |
| 538 | mLinkDownBandwidthKbps = downKbps; |
| 539 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 540 | |
| 541 | /** |
| 542 | * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to |
| 543 | * the estimated first hop transport bandwidth. |
| 544 | * |
| 545 | * @return The estimated first hop downstream (network to device) bandwidth. |
| 546 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 547 | public int getLinkDownstreamBandwidthKbps() { |
| 548 | return mLinkDownBandwidthKbps; |
| 549 | } |
| 550 | |
| 551 | private void combineLinkBandwidths(NetworkCapabilities nc) { |
| 552 | this.mLinkUpBandwidthKbps = |
| 553 | Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps); |
| 554 | this.mLinkDownBandwidthKbps = |
| 555 | Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps); |
| 556 | } |
| 557 | private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) { |
| 558 | return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps || |
| 559 | this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps); |
| 560 | } |
| 561 | private boolean equalsLinkBandwidths(NetworkCapabilities nc) { |
| 562 | return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && |
| 563 | this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); |
| 564 | } |
| 565 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 566 | private String mNetworkSpecifier; |
| 567 | /** |
| 568 | * Sets the optional bearer specific network specifier. |
| 569 | * This has no meaning if a single transport is also not specified, so calling |
| 570 | * this without a single transport set will generate an exception, as will |
| 571 | * subsequently adding or removing transports after this is set. |
| 572 | * </p> |
| 573 | * The interpretation of this {@code String} is bearer specific and bearers that use |
| 574 | * it should document their particulars. For example, Bluetooth may use some sort of |
Robert Greenwalt | 94badcc | 2014-07-10 14:53:24 -0700 | [diff] [blame] | 575 | * device id while WiFi could used SSID and/or BSSID. Cellular may use carrier SPN (name) |
| 576 | * or Subscription ID. |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 577 | * |
| 578 | * @param networkSpecifier An {@code String} of opaque format used to specify the bearer |
| 579 | * specific network specifier where the bearer has a choice of |
| 580 | * networks. |
| 581 | * @hide |
| 582 | */ |
| 583 | public void setNetworkSpecifier(String networkSpecifier) { |
| 584 | if (TextUtils.isEmpty(networkSpecifier) == false && Long.bitCount(mTransportTypes) != 1) { |
| 585 | throw new IllegalStateException("Must have a single transport specified to use " + |
| 586 | "setNetworkSpecifier"); |
| 587 | } |
| 588 | mNetworkSpecifier = networkSpecifier; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Gets the optional bearer specific network specifier. |
| 593 | * |
| 594 | * @return The optional {@code String} specifying the bearer specific network specifier. |
| 595 | * See {@link #setNetworkSpecifier}. |
| 596 | * @hide |
| 597 | */ |
| 598 | public String getNetworkSpecifier() { |
| 599 | return mNetworkSpecifier; |
| 600 | } |
| 601 | |
| 602 | private void combineSpecifiers(NetworkCapabilities nc) { |
| 603 | String otherSpecifier = nc.getNetworkSpecifier(); |
| 604 | if (TextUtils.isEmpty(otherSpecifier)) return; |
| 605 | if (TextUtils.isEmpty(mNetworkSpecifier) == false) { |
| 606 | throw new IllegalStateException("Can't combine two networkSpecifiers"); |
| 607 | } |
| 608 | setNetworkSpecifier(otherSpecifier); |
| 609 | } |
| 610 | private boolean satisfiedBySpecifier(NetworkCapabilities nc) { |
| 611 | return (TextUtils.isEmpty(mNetworkSpecifier) || |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 612 | mNetworkSpecifier.equals(nc.mNetworkSpecifier) || |
| 613 | MATCH_ALL_REQUESTS_NETWORK_SPECIFIER.equals(nc.mNetworkSpecifier)); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 614 | } |
| 615 | private boolean equalsSpecifier(NetworkCapabilities nc) { |
| 616 | if (TextUtils.isEmpty(mNetworkSpecifier)) { |
| 617 | return TextUtils.isEmpty(nc.mNetworkSpecifier); |
| 618 | } else { |
| 619 | return mNetworkSpecifier.equals(nc.mNetworkSpecifier); |
| 620 | } |
| 621 | } |
| 622 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 623 | /** |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 624 | * Magic value that indicates no signal strength provided. A request specifying this value is |
| 625 | * always satisfied. |
| 626 | * |
| 627 | * @hide |
| 628 | */ |
| 629 | public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE; |
| 630 | |
| 631 | /** |
| 632 | * Signal strength. This is a signed integer, and higher values indicate better signal. |
| 633 | * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI. |
| 634 | */ |
| 635 | private int mSignalStrength; |
| 636 | |
| 637 | /** |
| 638 | * Sets the signal strength. This is a signed integer, with higher values indicating a stronger |
| 639 | * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units |
| 640 | * reported by WifiManager. |
| 641 | * <p> |
| 642 | * Note that when used to register a network callback, this specifies the minimum acceptable |
| 643 | * signal strength. When received as the state of an existing network it specifies the current |
| 644 | * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no |
| 645 | * effect when requesting a callback. |
| 646 | * |
| 647 | * @param signalStrength the bearer-specific signal strength. |
| 648 | * @hide |
| 649 | */ |
| 650 | public void setSignalStrength(int signalStrength) { |
| 651 | mSignalStrength = signalStrength; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Returns {@code true} if this object specifies a signal strength. |
| 656 | * |
| 657 | * @hide |
| 658 | */ |
| 659 | public boolean hasSignalStrength() { |
| 660 | return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Retrieves the signal strength. |
| 665 | * |
| 666 | * @return The bearer-specific signal strength. |
| 667 | * @hide |
| 668 | */ |
| 669 | public int getSignalStrength() { |
| 670 | return mSignalStrength; |
| 671 | } |
| 672 | |
| 673 | private void combineSignalStrength(NetworkCapabilities nc) { |
| 674 | this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength); |
| 675 | } |
| 676 | |
| 677 | private boolean satisfiedBySignalStrength(NetworkCapabilities nc) { |
| 678 | return this.mSignalStrength <= nc.mSignalStrength; |
| 679 | } |
| 680 | |
| 681 | private boolean equalsSignalStrength(NetworkCapabilities nc) { |
| 682 | return this.mSignalStrength == nc.mSignalStrength; |
| 683 | } |
| 684 | |
| 685 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 686 | * Combine a set of Capabilities to this one. Useful for coming up with the complete set |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 687 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 688 | */ |
| 689 | public void combineCapabilities(NetworkCapabilities nc) { |
| 690 | combineNetCapabilities(nc); |
| 691 | combineTransportTypes(nc); |
| 692 | combineLinkBandwidths(nc); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 693 | combineSpecifiers(nc); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 694 | combineSignalStrength(nc); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 698 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 699 | * |
| 700 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 701 | * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link |
| 702 | * bandwidth, signal strength, or validation / captive portal status. |
| 703 | * |
| 704 | * @hide |
| 705 | */ |
| 706 | private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
| 707 | return (nc != null && |
| 708 | satisfiedByNetCapabilities(nc, onlyImmutable) && |
| 709 | satisfiedByTransportTypes(nc) && |
| 710 | (onlyImmutable || satisfiedByLinkBandwidths(nc)) && |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 711 | satisfiedBySpecifier(nc) && |
| 712 | (onlyImmutable || satisfiedBySignalStrength(nc))); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 717 | * |
| 718 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 719 | * |
| 720 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 721 | */ |
| 722 | public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) { |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 723 | return satisfiedByNetworkCapabilities(nc, false); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}. |
| 728 | * |
| 729 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 730 | * |
| 731 | * @hide |
| 732 | */ |
| 733 | public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) { |
| 734 | return satisfiedByNetworkCapabilities(nc, true); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Checks that our immutable capabilities are the same as those of the given |
| 739 | * {@code NetworkCapabilities}. |
| 740 | * |
| 741 | * @hide |
| 742 | */ |
| 743 | public boolean equalImmutableCapabilities(NetworkCapabilities nc) { |
| 744 | if (nc == null) return false; |
| 745 | return (equalsNetCapabilitiesImmutable(nc) && |
| 746 | equalsTransportTypes(nc) && |
| 747 | equalsSpecifier(nc)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | @Override |
| 751 | public boolean equals(Object obj) { |
| 752 | if (obj == null || (obj instanceof NetworkCapabilities == false)) return false; |
| 753 | NetworkCapabilities that = (NetworkCapabilities)obj; |
| 754 | return (equalsNetCapabilities(that) && |
| 755 | equalsTransportTypes(that) && |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 756 | equalsLinkBandwidths(that) && |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 757 | equalsSignalStrength(that) && |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 758 | equalsSpecifier(that)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | @Override |
| 762 | public int hashCode() { |
| 763 | return ((int)(mNetworkCapabilities & 0xFFFFFFFF) + |
| 764 | ((int)(mNetworkCapabilities >> 32) * 3) + |
| 765 | ((int)(mTransportTypes & 0xFFFFFFFF) * 5) + |
| 766 | ((int)(mTransportTypes >> 32) * 7) + |
| 767 | (mLinkUpBandwidthKbps * 11) + |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 768 | (mLinkDownBandwidthKbps * 13) + |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 769 | (TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17) + |
| 770 | (mSignalStrength * 19)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 773 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 774 | public int describeContents() { |
| 775 | return 0; |
| 776 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 777 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 778 | public void writeToParcel(Parcel dest, int flags) { |
| 779 | dest.writeLong(mNetworkCapabilities); |
| 780 | dest.writeLong(mTransportTypes); |
| 781 | dest.writeInt(mLinkUpBandwidthKbps); |
| 782 | dest.writeInt(mLinkDownBandwidthKbps); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 783 | dest.writeString(mNetworkSpecifier); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 784 | dest.writeInt(mSignalStrength); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 785 | } |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 786 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 787 | public static final Creator<NetworkCapabilities> CREATOR = |
| 788 | new Creator<NetworkCapabilities>() { |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 789 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 790 | public NetworkCapabilities createFromParcel(Parcel in) { |
| 791 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 792 | |
| 793 | netCap.mNetworkCapabilities = in.readLong(); |
| 794 | netCap.mTransportTypes = in.readLong(); |
| 795 | netCap.mLinkUpBandwidthKbps = in.readInt(); |
| 796 | netCap.mLinkDownBandwidthKbps = in.readInt(); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 797 | netCap.mNetworkSpecifier = in.readString(); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 798 | netCap.mSignalStrength = in.readInt(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 799 | return netCap; |
| 800 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 801 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 802 | public NetworkCapabilities[] newArray(int size) { |
| 803 | return new NetworkCapabilities[size]; |
| 804 | } |
| 805 | }; |
| 806 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 807 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 808 | public String toString() { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 809 | int[] types = getTransportTypes(); |
| 810 | String transports = (types.length > 0 ? " Transports: " : ""); |
| 811 | for (int i = 0; i < types.length;) { |
| 812 | switch (types[i]) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 813 | case TRANSPORT_CELLULAR: transports += "CELLULAR"; break; |
| 814 | case TRANSPORT_WIFI: transports += "WIFI"; break; |
| 815 | case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break; |
| 816 | case TRANSPORT_ETHERNET: transports += "ETHERNET"; break; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 817 | case TRANSPORT_VPN: transports += "VPN"; break; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 818 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 819 | if (++i < types.length) transports += "|"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 822 | types = getCapabilities(); |
| 823 | String capabilities = (types.length > 0 ? " Capabilities: " : ""); |
| 824 | for (int i = 0; i < types.length; ) { |
| 825 | switch (types[i]) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 826 | case NET_CAPABILITY_MMS: capabilities += "MMS"; break; |
| 827 | case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break; |
| 828 | case NET_CAPABILITY_DUN: capabilities += "DUN"; break; |
| 829 | case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break; |
| 830 | case NET_CAPABILITY_IMS: capabilities += "IMS"; break; |
| 831 | case NET_CAPABILITY_CBS: capabilities += "CBS"; break; |
| 832 | case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break; |
| 833 | case NET_CAPABILITY_IA: capabilities += "IA"; break; |
| 834 | case NET_CAPABILITY_RCS: capabilities += "RCS"; break; |
| 835 | case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break; |
| 836 | case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break; |
| 837 | case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break; |
| 838 | case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break; |
| 839 | case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break; |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 840 | case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 841 | case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break; |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 842 | case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break; |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 843 | case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 844 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 845 | if (++i < types.length) capabilities += "&"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + |
| 849 | mLinkUpBandwidthKbps + "Kbps" : ""); |
| 850 | String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" + |
| 851 | mLinkDownBandwidthKbps + "Kbps" : ""); |
| 852 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 853 | String specifier = (mNetworkSpecifier == null ? |
| 854 | "" : " Specifier: <" + mNetworkSpecifier + ">"); |
| 855 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 856 | String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : ""); |
| 857 | |
| 858 | return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 859 | } |
| 860 | } |