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 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 20 | import android.net.ConnectivityManager.NetworkCallback; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Robert Greenwalt | a7e148a | 2017-04-10 14:32:23 -0700 | [diff] [blame] | 23 | |
| 24 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 25 | import com.android.internal.util.BitUtils; |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 30 | import java.util.Objects; |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 31 | import java.util.StringJoiner; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 34 | * Representation of the capabilities of a network. This object serves two |
| 35 | * purposes: |
| 36 | * <ul> |
| 37 | * <li>An expression of the current capabilities of an active network, typically |
| 38 | * expressed through |
| 39 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} |
| 40 | * or {@link ConnectivityManager#getNetworkCapabilities(Network)}. |
| 41 | * <li>An expression of the future capabilities of a desired network, typically |
| 42 | * expressed through {@link NetworkRequest}. |
| 43 | * </ul> |
| 44 | * <p> |
| 45 | * This replaces the old {@link ConnectivityManager#TYPE_MOBILE} method of |
| 46 | * network selection. Rather than indicate a need for Wi-Fi because an |
| 47 | * application needs high bandwidth and risk obsolescence when a new, fast |
| 48 | * network appears (like LTE), the application should specify it needs high |
| 49 | * bandwidth. Similarly if an application needs an unmetered network for a bulk |
| 50 | * transfer it can specify that rather than assuming all cellular based |
| 51 | * connections are metered and all Wi-Fi based connections are not. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 52 | */ |
| 53 | public final class NetworkCapabilities implements Parcelable { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 54 | private static final String TAG = "NetworkCapabilities"; |
| 55 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 56 | /** |
| 57 | * @hide |
| 58 | */ |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 59 | public NetworkCapabilities() { |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 60 | clearAll(); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 61 | mNetworkCapabilities = DEFAULT_CAPABILITIES; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public NetworkCapabilities(NetworkCapabilities nc) { |
| 65 | if (nc != null) { |
| 66 | mNetworkCapabilities = nc.mNetworkCapabilities; |
| 67 | mTransportTypes = nc.mTransportTypes; |
| 68 | mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps; |
| 69 | mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps; |
Robert Greenwalt | 94badcc | 2014-07-10 14:53:24 -0700 | [diff] [blame] | 70 | mNetworkSpecifier = nc.mNetworkSpecifier; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 71 | mSignalStrength = nc.mSignalStrength; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 76 | * Completely clears the contents of this object, removing even the capabilities that are set |
| 77 | * by default when the object is constructed. |
| 78 | * @hide |
| 79 | */ |
| 80 | public void clearAll() { |
| 81 | mNetworkCapabilities = mTransportTypes = 0; |
| 82 | mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0; |
| 83 | mNetworkSpecifier = null; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 84 | mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED; |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 88 | * Represents the network's capabilities. If any are specified they will be satisfied |
| 89 | * by any Network that matches all of them. |
| 90 | */ |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 91 | private long mNetworkCapabilities; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 92 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 93 | /** @hide */ |
| 94 | @Retention(RetentionPolicy.SOURCE) |
| 95 | @IntDef(prefix = { "NET_CAPABILITY_" }, value = { |
| 96 | NET_CAPABILITY_MMS, |
| 97 | NET_CAPABILITY_SUPL, |
| 98 | NET_CAPABILITY_DUN, |
| 99 | NET_CAPABILITY_FOTA, |
| 100 | NET_CAPABILITY_IMS, |
| 101 | NET_CAPABILITY_CBS, |
| 102 | NET_CAPABILITY_WIFI_P2P, |
| 103 | NET_CAPABILITY_IA, |
| 104 | NET_CAPABILITY_RCS, |
| 105 | NET_CAPABILITY_XCAP, |
| 106 | NET_CAPABILITY_EIMS, |
| 107 | NET_CAPABILITY_NOT_METERED, |
| 108 | NET_CAPABILITY_INTERNET, |
| 109 | NET_CAPABILITY_NOT_RESTRICTED, |
| 110 | NET_CAPABILITY_TRUSTED, |
| 111 | NET_CAPABILITY_NOT_VPN, |
| 112 | NET_CAPABILITY_VALIDATED, |
| 113 | NET_CAPABILITY_CAPTIVE_PORTAL, |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 114 | NET_CAPABILITY_NOT_ROAMING, |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 115 | NET_CAPABILITY_FOREGROUND, |
| 116 | }) |
| 117 | public @interface NetCapability { } |
| 118 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 119 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 120 | * Indicates this is a network that has the ability to reach the |
| 121 | * carrier's MMSC for sending and receiving MMS messages. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 122 | */ |
| 123 | public static final int NET_CAPABILITY_MMS = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Indicates this is a network that has the ability to reach the carrier's |
| 127 | * SUPL server, used to retrieve GPS information. |
| 128 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 129 | public static final int NET_CAPABILITY_SUPL = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Indicates this is a network that has the ability to reach the carrier's |
| 133 | * DUN or tethering gateway. |
| 134 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 135 | public static final int NET_CAPABILITY_DUN = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Indicates this is a network that has the ability to reach the carrier's |
| 139 | * FOTA portal, used for over the air updates. |
| 140 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 141 | public static final int NET_CAPABILITY_FOTA = 3; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Indicates this is a network that has the ability to reach the carrier's |
| 145 | * IMS servers, used for network registration and signaling. |
| 146 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 147 | public static final int NET_CAPABILITY_IMS = 4; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Indicates this is a network that has the ability to reach the carrier's |
| 151 | * CBS servers, used for carrier specific services. |
| 152 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 153 | public static final int NET_CAPABILITY_CBS = 5; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Indicates this is a network that has the ability to reach a Wi-Fi direct |
| 157 | * peer. |
| 158 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 159 | public static final int NET_CAPABILITY_WIFI_P2P = 6; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Indicates this is a network that has the ability to reach a carrier's |
| 163 | * Initial Attach servers. |
| 164 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 165 | public static final int NET_CAPABILITY_IA = 7; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Indicates this is a network that has the ability to reach a carrier's |
| 169 | * RCS servers, used for Rich Communication Services. |
| 170 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 171 | public static final int NET_CAPABILITY_RCS = 8; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Indicates this is a network that has the ability to reach a carrier's |
| 175 | * XCAP servers, used for configuration and control. |
| 176 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 177 | public static final int NET_CAPABILITY_XCAP = 9; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * 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] | 181 | * Emergency IMS servers or other services, used for network signaling |
| 182 | * during emergency calls. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 183 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 184 | public static final int NET_CAPABILITY_EIMS = 10; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Indicates that this network is unmetered. |
| 188 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 189 | public static final int NET_CAPABILITY_NOT_METERED = 11; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Indicates that this network should be able to reach the internet. |
| 193 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 194 | public static final int NET_CAPABILITY_INTERNET = 12; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Indicates that this network is available for general use. If this is not set |
| 198 | * applications should not attempt to communicate on this network. Note that this |
| 199 | * is simply informative and not enforcement - enforcement is handled via other means. |
| 200 | * Set by default. |
| 201 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 202 | public static final int NET_CAPABILITY_NOT_RESTRICTED = 13; |
| 203 | |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Indicates that the user has indicated implicit trust of this network. This |
| 206 | * generally means it's a sim-selected carrier, a plugged in ethernet, a paired |
| 207 | * BT device or a wifi the user asked to connect to. Untrusted networks |
| 208 | * are probably limited to unknown wifi AP. Set by default. |
| 209 | */ |
| 210 | public static final int NET_CAPABILITY_TRUSTED = 14; |
| 211 | |
Paul Jensen | 76b610a | 2015-03-18 09:33:07 -0400 | [diff] [blame] | 212 | /** |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 213 | * 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] | 214 | * explicitly cleared for VPN networks. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 215 | */ |
| 216 | public static final int NET_CAPABILITY_NOT_VPN = 15; |
| 217 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 218 | /** |
| 219 | * Indicates that connectivity on this network was successfully validated. For example, for a |
| 220 | * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully |
| 221 | * detected. |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 222 | */ |
| 223 | public static final int NET_CAPABILITY_VALIDATED = 16; |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 224 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 225 | /** |
| 226 | * Indicates that this network was found to have a captive portal in place last time it was |
| 227 | * probed. |
| 228 | */ |
| 229 | public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17; |
| 230 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 231 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 232 | * Indicates that this network is not roaming. |
| 233 | */ |
| 234 | public static final int NET_CAPABILITY_NOT_ROAMING = 18; |
| 235 | |
| 236 | /** |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 237 | * Indicates that this network is available for use by apps, and not a network that is being |
| 238 | * kept up in the background to facilitate fast network switching. |
| 239 | * @hide |
| 240 | */ |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 241 | public static final int NET_CAPABILITY_FOREGROUND = 19; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 242 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 243 | private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 244 | private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 245 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 246 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 247 | * Network capabilities that are expected to be mutable, i.e., can change while a particular |
| 248 | * network is connected. |
| 249 | */ |
| 250 | private static final long MUTABLE_CAPABILITIES = |
| 251 | // TRUSTED can change when user explicitly connects to an untrusted network in Settings. |
| 252 | // http://b/18206275 |
| 253 | (1 << NET_CAPABILITY_TRUSTED) | |
| 254 | (1 << NET_CAPABILITY_VALIDATED) | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 255 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL) | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 256 | (1 << NET_CAPABILITY_NOT_ROAMING) | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 257 | (1 << NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Network capabilities that are not allowed in NetworkRequests. This exists because the |
| 261 | * NetworkFactory / NetworkAgent model does not deal well with the situation where a |
| 262 | * capability's presence cannot be known in advance. If such a capability is requested, then we |
| 263 | * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then |
| 264 | * get immediately torn down because they do not have the requested capability. |
| 265 | */ |
| 266 | private static final long NON_REQUESTABLE_CAPABILITIES = |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 267 | MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Capabilities that are set by default when the object is constructed. |
| 271 | */ |
| 272 | private static final long DEFAULT_CAPABILITIES = |
| 273 | (1 << NET_CAPABILITY_NOT_RESTRICTED) | |
| 274 | (1 << NET_CAPABILITY_TRUSTED) | |
| 275 | (1 << NET_CAPABILITY_NOT_VPN); |
| 276 | |
| 277 | /** |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 278 | * Capabilities that suggest that a network is restricted. |
| 279 | * {@see #maybeMarkCapabilitiesRestricted}. |
| 280 | */ |
Robert Greenwalt | a7e148a | 2017-04-10 14:32:23 -0700 | [diff] [blame] | 281 | @VisibleForTesting |
| 282 | /* package */ static final long RESTRICTED_CAPABILITIES = |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 283 | (1 << NET_CAPABILITY_CBS) | |
| 284 | (1 << NET_CAPABILITY_DUN) | |
| 285 | (1 << NET_CAPABILITY_EIMS) | |
| 286 | (1 << NET_CAPABILITY_FOTA) | |
| 287 | (1 << NET_CAPABILITY_IA) | |
| 288 | (1 << NET_CAPABILITY_IMS) | |
| 289 | (1 << NET_CAPABILITY_RCS) | |
| 290 | (1 << NET_CAPABILITY_XCAP); |
| 291 | |
| 292 | /** |
Robert Greenwalt | a7e148a | 2017-04-10 14:32:23 -0700 | [diff] [blame] | 293 | * Capabilities that suggest that a network is unrestricted. |
| 294 | * {@see #maybeMarkCapabilitiesRestricted}. |
| 295 | */ |
| 296 | @VisibleForTesting |
| 297 | /* package */ static final long UNRESTRICTED_CAPABILITIES = |
| 298 | (1 << NET_CAPABILITY_INTERNET) | |
| 299 | (1 << NET_CAPABILITY_MMS) | |
| 300 | (1 << NET_CAPABILITY_SUPL) | |
| 301 | (1 << NET_CAPABILITY_WIFI_P2P); |
| 302 | |
| 303 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 304 | * Adds the given capability to this {@code NetworkCapability} instance. |
| 305 | * Multiple capabilities may be applied sequentially. Note that when searching |
| 306 | * for a network to satisfy a request, all capabilities requested must be satisfied. |
| 307 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 308 | * @param capability the capability to be added. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 309 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 310 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 311 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 312 | public NetworkCapabilities addCapability(@NetCapability int capability) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 313 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 314 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 315 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 316 | mNetworkCapabilities |= 1 << capability; |
| 317 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 318 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 319 | |
| 320 | /** |
| 321 | * Removes (if found) the given capability from this {@code NetworkCapability} instance. |
| 322 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 323 | * @param capability the capability to be removed. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 324 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 325 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 326 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 327 | public NetworkCapabilities removeCapability(@NetCapability int capability) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 328 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 329 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 330 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 331 | mNetworkCapabilities &= ~(1 << capability); |
| 332 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 333 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 334 | |
| 335 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 336 | * Sets (or clears) the given capability on this {@link NetworkCapabilities} |
| 337 | * instance. |
| 338 | * |
| 339 | * @hide |
| 340 | */ |
| 341 | public NetworkCapabilities setCapability(@NetCapability int capability, boolean value) { |
| 342 | if (value) { |
| 343 | addCapability(capability); |
| 344 | } else { |
| 345 | removeCapability(capability); |
| 346 | } |
| 347 | return this; |
| 348 | } |
| 349 | |
| 350 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 351 | * Gets all the capabilities set on this {@code NetworkCapability} instance. |
| 352 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 353 | * @return an array of capability values for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 354 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 355 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 356 | public @NetCapability int[] getCapabilities() { |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 357 | return BitUtils.unpackBits(mNetworkCapabilities); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 358 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 359 | |
| 360 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 361 | * Sets all the capabilities set on this {@code NetworkCapability} instance. |
| 362 | * |
| 363 | * @hide |
| 364 | */ |
| 365 | public void setCapabilities(@NetCapability int[] capabilities) { |
| 366 | mNetworkCapabilities = BitUtils.packBits(capabilities); |
| 367 | } |
| 368 | |
| 369 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 370 | * Tests for the presence of a capabilitity on this instance. |
| 371 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 372 | * @param capability the capabilities to be tested for. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 373 | * @return {@code true} if set on this instance. |
| 374 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 375 | public boolean hasCapability(@NetCapability int capability) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 376 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 377 | return false; |
| 378 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 379 | return ((mNetworkCapabilities & (1 << capability)) != 0); |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 380 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 381 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 382 | private void combineNetCapabilities(NetworkCapabilities nc) { |
| 383 | this.mNetworkCapabilities |= nc.mNetworkCapabilities; |
| 384 | } |
| 385 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 386 | /** |
| 387 | * Convenience function that returns a human-readable description of the first mutable |
| 388 | * capability we find. Used to present an error message to apps that request mutable |
| 389 | * capabilities. |
| 390 | * |
| 391 | * @hide |
| 392 | */ |
| 393 | public String describeFirstNonRequestableCapability() { |
| 394 | if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED"; |
| 395 | if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL"; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 396 | if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND"; |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 397 | // This cannot happen unless the preceding checks are incomplete. |
| 398 | if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) { |
| 399 | return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities); |
| 400 | } |
| 401 | if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth"; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 402 | if (hasSignalStrength()) return "signalStrength"; |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 403 | return null; |
| 404 | } |
| 405 | |
| 406 | private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
| 407 | long networkCapabilities = this.mNetworkCapabilities; |
| 408 | if (onlyImmutable) { |
| 409 | networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES; |
| 410 | } |
| 411 | return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 414 | /** @hide */ |
| 415 | public boolean equalsNetCapabilities(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 416 | return (nc.mNetworkCapabilities == this.mNetworkCapabilities); |
| 417 | } |
| 418 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 419 | private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) { |
| 420 | return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) == |
| 421 | (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES)); |
| 422 | } |
| 423 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 424 | /** |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 425 | * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are |
| 426 | * typically provided by restricted networks. |
| 427 | * |
| 428 | * TODO: consider: |
| 429 | * - Renaming it to guessRestrictedCapability and make it set the |
| 430 | * restricted capability bit in addition to clearing it. |
| 431 | * @hide |
| 432 | */ |
| 433 | public void maybeMarkCapabilitiesRestricted() { |
Robert Greenwalt | a7e148a | 2017-04-10 14:32:23 -0700 | [diff] [blame] | 434 | // Verify there aren't any unrestricted capabilities. If there are we say |
| 435 | // the whole thing is unrestricted. |
| 436 | final boolean hasUnrestrictedCapabilities = |
| 437 | ((mNetworkCapabilities & UNRESTRICTED_CAPABILITIES) != 0); |
| 438 | |
| 439 | // Must have at least some restricted capabilities. |
| 440 | final boolean hasRestrictedCapabilities = |
| 441 | ((mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0); |
| 442 | |
| 443 | if (hasRestrictedCapabilities && !hasUnrestrictedCapabilities) { |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 444 | removeCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Paul Jensen | aae613d | 2015-08-19 11:06:15 -0400 | [diff] [blame] | 445 | } |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 449 | * Representing the transport type. Apps should generally not care about transport. A |
| 450 | * request for a fast internet connection could be satisfied by a number of different |
| 451 | * transports. If any are specified here it will be satisfied a Network that matches |
| 452 | * any of them. If a caller doesn't care about the transport it should not specify any. |
| 453 | */ |
| 454 | private long mTransportTypes; |
| 455 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 456 | /** @hide */ |
| 457 | @Retention(RetentionPolicy.SOURCE) |
| 458 | @IntDef(prefix = { "TRANSPORT_" }, value = { |
| 459 | TRANSPORT_CELLULAR, |
| 460 | TRANSPORT_WIFI, |
| 461 | TRANSPORT_BLUETOOTH, |
| 462 | TRANSPORT_ETHERNET, |
| 463 | TRANSPORT_VPN, |
| 464 | TRANSPORT_WIFI_AWARE, |
| 465 | TRANSPORT_LOWPAN, |
| 466 | }) |
| 467 | public @interface Transport { } |
| 468 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 469 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 470 | * Indicates this network uses a Cellular transport. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 471 | */ |
| 472 | public static final int TRANSPORT_CELLULAR = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Indicates this network uses a Wi-Fi transport. |
| 476 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 477 | public static final int TRANSPORT_WIFI = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 478 | |
| 479 | /** |
| 480 | * Indicates this network uses a Bluetooth transport. |
| 481 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 482 | public static final int TRANSPORT_BLUETOOTH = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * Indicates this network uses an Ethernet transport. |
| 486 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 487 | public static final int TRANSPORT_ETHERNET = 3; |
| 488 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 489 | /** |
| 490 | * Indicates this network uses a VPN transport. |
| 491 | */ |
| 492 | public static final int TRANSPORT_VPN = 4; |
| 493 | |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 494 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 495 | * Indicates this network uses a Wi-Fi Aware transport. |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 496 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 497 | public static final int TRANSPORT_WIFI_AWARE = 5; |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 498 | |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 499 | /** |
| 500 | * Indicates this network uses a LoWPAN transport. |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 501 | */ |
| 502 | public static final int TRANSPORT_LOWPAN = 6; |
| 503 | |
Hugo Benichi | 6a9bb8e | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 504 | /** @hide */ |
| 505 | public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR; |
| 506 | /** @hide */ |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 507 | public static final int MAX_TRANSPORT = TRANSPORT_LOWPAN; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 508 | |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 509 | /** @hide */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 510 | public static boolean isValidTransport(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 511 | return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT); |
| 512 | } |
| 513 | |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 514 | private static final String[] TRANSPORT_NAMES = { |
| 515 | "CELLULAR", |
| 516 | "WIFI", |
| 517 | "BLUETOOTH", |
| 518 | "ETHERNET", |
| 519 | "VPN", |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 520 | "WIFI_AWARE", |
| 521 | "LOWPAN" |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 522 | }; |
| 523 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 524 | /** |
| 525 | * Adds the given transport type to this {@code NetworkCapability} instance. |
| 526 | * Multiple transports may be applied sequentially. Note that when searching |
| 527 | * for a network to satisfy a request, any listed in the request will satisfy the request. |
| 528 | * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a |
| 529 | * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network |
| 530 | * to be selected. This is logically different than |
| 531 | * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. |
| 532 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 533 | * @param transportType the transport type to be added. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 534 | * @return This NetworkCapabilities instance, to facilitate chaining. |
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 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 537 | public NetworkCapabilities addTransportType(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 538 | checkValidTransportType(transportType); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 539 | mTransportTypes |= 1 << transportType; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 540 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 541 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 542 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 543 | |
| 544 | /** |
| 545 | * Removes (if found) the given transport from this {@code NetworkCapability} instance. |
| 546 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 547 | * @param transportType the transport type to be removed. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 548 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 549 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 550 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 551 | public NetworkCapabilities removeTransportType(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 552 | checkValidTransportType(transportType); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 553 | mTransportTypes &= ~(1 << transportType); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 554 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 555 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 556 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 557 | |
| 558 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 559 | * Sets (or clears) the given transport on this {@link NetworkCapabilities} |
| 560 | * instance. |
| 561 | * |
| 562 | * @hide |
| 563 | */ |
| 564 | public NetworkCapabilities setTransportType(@Transport int transportType, boolean value) { |
| 565 | if (value) { |
| 566 | addTransportType(transportType); |
| 567 | } else { |
| 568 | removeTransportType(transportType); |
| 569 | } |
| 570 | return this; |
| 571 | } |
| 572 | |
| 573 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 574 | * Gets all the transports set on this {@code NetworkCapability} instance. |
| 575 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 576 | * @return an array of transport type values for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 577 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 578 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 579 | public @Transport int[] getTransportTypes() { |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 580 | return BitUtils.unpackBits(mTransportTypes); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 581 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 582 | |
| 583 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 584 | * Sets all the transports set on this {@code NetworkCapability} instance. |
| 585 | * |
| 586 | * @hide |
| 587 | */ |
| 588 | public void setTransportTypes(@Transport int[] transportTypes) { |
| 589 | mTransportTypes = BitUtils.packBits(transportTypes); |
| 590 | } |
| 591 | |
| 592 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 593 | * Tests for the presence of a transport on this instance. |
| 594 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 595 | * @param transportType the transport type to be tested for. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 596 | * @return {@code true} if set on this instance. |
| 597 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 598 | public boolean hasTransport(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 599 | return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0); |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 600 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 601 | |
| 602 | private void combineTransportTypes(NetworkCapabilities nc) { |
| 603 | this.mTransportTypes |= nc.mTransportTypes; |
| 604 | } |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 605 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 606 | private boolean satisfiedByTransportTypes(NetworkCapabilities nc) { |
| 607 | return ((this.mTransportTypes == 0) || |
| 608 | ((this.mTransportTypes & nc.mTransportTypes) != 0)); |
| 609 | } |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 610 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 611 | /** @hide */ |
| 612 | public boolean equalsTransportTypes(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 613 | return (nc.mTransportTypes == this.mTransportTypes); |
| 614 | } |
| 615 | |
| 616 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 617 | * Value indicating that link bandwidth is unspecified. |
| 618 | * @hide |
| 619 | */ |
| 620 | public static final int LINK_BANDWIDTH_UNSPECIFIED = 0; |
| 621 | |
| 622 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 623 | * Passive link bandwidth. This is a rough guide of the expected peak bandwidth |
| 624 | * for the first hop on the given transport. It is not measured, but may take into account |
| 625 | * link parameters (Radio technology, allocated channels, etc). |
| 626 | */ |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 627 | private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED; |
| 628 | private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 629 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 630 | /** |
| 631 | * Sets the upstream bandwidth for this network in Kbps. This always only refers to |
| 632 | * the estimated first hop transport bandwidth. |
| 633 | * <p> |
| 634 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 635 | * When received as the state of an existing network this specifies the typical |
| 636 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 637 | * from technology type and other link parameters. It could be used to differentiate |
| 638 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 639 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 640 | * fast backhauls and slow backhauls. |
| 641 | * |
| 642 | * @param upKbps the estimated first hop upstream (device to network) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 643 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 644 | */ |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 645 | public NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 646 | mLinkUpBandwidthKbps = upKbps; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 647 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 648 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 649 | |
| 650 | /** |
| 651 | * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to |
| 652 | * the estimated first hop transport bandwidth. |
| 653 | * |
| 654 | * @return The estimated first hop upstream (device to network) bandwidth. |
| 655 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 656 | public int getLinkUpstreamBandwidthKbps() { |
| 657 | return mLinkUpBandwidthKbps; |
| 658 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 659 | |
| 660 | /** |
| 661 | * Sets the downstream bandwidth for this network in Kbps. This always only refers to |
| 662 | * the estimated first hop transport bandwidth. |
| 663 | * <p> |
| 664 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 665 | * When received as the state of an existing network this specifies the typical |
| 666 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 667 | * from technology type and other link parameters. It could be used to differentiate |
| 668 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 669 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 670 | * fast backhauls and slow backhauls. |
| 671 | * |
| 672 | * @param downKbps the estimated first hop downstream (network to device) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 673 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 674 | */ |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 675 | public NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 676 | mLinkDownBandwidthKbps = downKbps; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 677 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 678 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 679 | |
| 680 | /** |
| 681 | * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to |
| 682 | * the estimated first hop transport bandwidth. |
| 683 | * |
| 684 | * @return The estimated first hop downstream (network to device) bandwidth. |
| 685 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 686 | public int getLinkDownstreamBandwidthKbps() { |
| 687 | return mLinkDownBandwidthKbps; |
| 688 | } |
| 689 | |
| 690 | private void combineLinkBandwidths(NetworkCapabilities nc) { |
| 691 | this.mLinkUpBandwidthKbps = |
| 692 | Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps); |
| 693 | this.mLinkDownBandwidthKbps = |
| 694 | Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps); |
| 695 | } |
| 696 | private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) { |
| 697 | return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps || |
| 698 | this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps); |
| 699 | } |
| 700 | private boolean equalsLinkBandwidths(NetworkCapabilities nc) { |
| 701 | return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && |
| 702 | this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); |
| 703 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 704 | /** @hide */ |
| 705 | public static int minBandwidth(int a, int b) { |
| 706 | if (a == LINK_BANDWIDTH_UNSPECIFIED) { |
| 707 | return b; |
| 708 | } else if (b == LINK_BANDWIDTH_UNSPECIFIED) { |
| 709 | return a; |
| 710 | } else { |
| 711 | return Math.min(a, b); |
| 712 | } |
| 713 | } |
| 714 | /** @hide */ |
| 715 | public static int maxBandwidth(int a, int b) { |
| 716 | return Math.max(a, b); |
| 717 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 718 | |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 719 | private NetworkSpecifier mNetworkSpecifier = null; |
| 720 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 721 | /** |
| 722 | * Sets the optional bearer specific network specifier. |
| 723 | * This has no meaning if a single transport is also not specified, so calling |
| 724 | * this without a single transport set will generate an exception, as will |
| 725 | * subsequently adding or removing transports after this is set. |
| 726 | * </p> |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 727 | * |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 728 | * @param networkSpecifier A concrete, parcelable framework class that extends |
| 729 | * NetworkSpecifier. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 730 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 731 | * @hide |
| 732 | */ |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 733 | public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) { |
| 734 | if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 735 | throw new IllegalStateException("Must have a single transport specified to use " + |
| 736 | "setNetworkSpecifier"); |
| 737 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 738 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 739 | mNetworkSpecifier = networkSpecifier; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 740 | |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 741 | return this; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** |
| 745 | * Gets the optional bearer specific network specifier. |
| 746 | * |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 747 | * @return The optional {@link NetworkSpecifier} specifying the bearer specific network |
| 748 | * specifier. See {@link #setNetworkSpecifier}. |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 749 | * @hide |
| 750 | */ |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 751 | public NetworkSpecifier getNetworkSpecifier() { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 752 | return mNetworkSpecifier; |
| 753 | } |
| 754 | |
| 755 | private void combineSpecifiers(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 756 | if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 757 | throw new IllegalStateException("Can't combine two networkSpecifiers"); |
| 758 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 759 | setNetworkSpecifier(nc.mNetworkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 760 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 761 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 762 | private boolean satisfiedBySpecifier(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 763 | return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier) |
| 764 | || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 765 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 766 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 767 | private boolean equalsSpecifier(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 768 | return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 771 | /** |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 772 | * Magic value that indicates no signal strength provided. A request specifying this value is |
| 773 | * always satisfied. |
| 774 | * |
| 775 | * @hide |
| 776 | */ |
| 777 | public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE; |
| 778 | |
| 779 | /** |
| 780 | * Signal strength. This is a signed integer, and higher values indicate better signal. |
| 781 | * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI. |
| 782 | */ |
| 783 | private int mSignalStrength; |
| 784 | |
| 785 | /** |
| 786 | * Sets the signal strength. This is a signed integer, with higher values indicating a stronger |
| 787 | * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units |
| 788 | * reported by WifiManager. |
| 789 | * <p> |
| 790 | * Note that when used to register a network callback, this specifies the minimum acceptable |
| 791 | * signal strength. When received as the state of an existing network it specifies the current |
| 792 | * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no |
| 793 | * effect when requesting a callback. |
| 794 | * |
| 795 | * @param signalStrength the bearer-specific signal strength. |
| 796 | * @hide |
| 797 | */ |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 798 | public NetworkCapabilities setSignalStrength(int signalStrength) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 799 | mSignalStrength = signalStrength; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 800 | return this; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Returns {@code true} if this object specifies a signal strength. |
| 805 | * |
| 806 | * @hide |
| 807 | */ |
| 808 | public boolean hasSignalStrength() { |
| 809 | return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Retrieves the signal strength. |
| 814 | * |
| 815 | * @return The bearer-specific signal strength. |
| 816 | * @hide |
| 817 | */ |
| 818 | public int getSignalStrength() { |
| 819 | return mSignalStrength; |
| 820 | } |
| 821 | |
| 822 | private void combineSignalStrength(NetworkCapabilities nc) { |
| 823 | this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength); |
| 824 | } |
| 825 | |
| 826 | private boolean satisfiedBySignalStrength(NetworkCapabilities nc) { |
| 827 | return this.mSignalStrength <= nc.mSignalStrength; |
| 828 | } |
| 829 | |
| 830 | private boolean equalsSignalStrength(NetworkCapabilities nc) { |
| 831 | return this.mSignalStrength == nc.mSignalStrength; |
| 832 | } |
| 833 | |
| 834 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 835 | * 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] | 836 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 837 | */ |
| 838 | public void combineCapabilities(NetworkCapabilities nc) { |
| 839 | combineNetCapabilities(nc); |
| 840 | combineTransportTypes(nc); |
| 841 | combineLinkBandwidths(nc); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 842 | combineSpecifiers(nc); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 843 | combineSignalStrength(nc); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 847 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 848 | * |
| 849 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 850 | * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link |
| 851 | * bandwidth, signal strength, or validation / captive portal status. |
| 852 | * |
| 853 | * @hide |
| 854 | */ |
| 855 | private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
| 856 | return (nc != null && |
| 857 | satisfiedByNetCapabilities(nc, onlyImmutable) && |
| 858 | satisfiedByTransportTypes(nc) && |
| 859 | (onlyImmutable || satisfiedByLinkBandwidths(nc)) && |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 860 | satisfiedBySpecifier(nc) && |
| 861 | (onlyImmutable || satisfiedBySignalStrength(nc))); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 866 | * |
| 867 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 868 | * |
| 869 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 870 | */ |
| 871 | public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) { |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 872 | return satisfiedByNetworkCapabilities(nc, false); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}. |
| 877 | * |
| 878 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 879 | * |
| 880 | * @hide |
| 881 | */ |
| 882 | public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) { |
| 883 | return satisfiedByNetworkCapabilities(nc, true); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Checks that our immutable capabilities are the same as those of the given |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 888 | * {@code NetworkCapabilities} and return a String describing any difference. |
| 889 | * The returned String is empty if there is no difference. |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 890 | * |
| 891 | * @hide |
| 892 | */ |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 893 | public String describeImmutableDifferences(NetworkCapabilities that) { |
| 894 | if (that == null) { |
| 895 | return "other NetworkCapabilities was null"; |
| 896 | } |
| 897 | |
| 898 | StringJoiner joiner = new StringJoiner(", "); |
| 899 | |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 900 | // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103 |
| 901 | // TODO: properly support NOT_METERED as a mutable and requestable capability. |
Hugo Benichi | 2ecb940 | 2017-08-04 13:18:40 +0900 | [diff] [blame] | 902 | final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED); |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 903 | long oldImmutableCapabilities = this.mNetworkCapabilities & mask; |
| 904 | long newImmutableCapabilities = that.mNetworkCapabilities & mask; |
| 905 | if (oldImmutableCapabilities != newImmutableCapabilities) { |
| 906 | String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities)); |
| 907 | String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities)); |
| 908 | joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after)); |
| 909 | } |
| 910 | |
| 911 | if (!equalsSpecifier(that)) { |
| 912 | NetworkSpecifier before = this.getNetworkSpecifier(); |
| 913 | NetworkSpecifier after = that.getNetworkSpecifier(); |
| 914 | joiner.add(String.format("specifier changed: %s -> %s", before, after)); |
| 915 | } |
| 916 | |
| 917 | if (!equalsTransportTypes(that)) { |
| 918 | String before = transportNamesOf(this.getTransportTypes()); |
| 919 | String after = transportNamesOf(that.getTransportTypes()); |
| 920 | joiner.add(String.format("transports changed: %s -> %s", before, after)); |
| 921 | } |
| 922 | |
| 923 | return joiner.toString(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 926 | /** |
| 927 | * Checks that our requestable capabilities are the same as those of the given |
| 928 | * {@code NetworkCapabilities}. |
| 929 | * |
| 930 | * @hide |
| 931 | */ |
| 932 | public boolean equalRequestableCapabilities(NetworkCapabilities nc) { |
| 933 | if (nc == null) return false; |
| 934 | return (equalsNetCapabilitiesRequestable(nc) && |
| 935 | equalsTransportTypes(nc) && |
| 936 | equalsSpecifier(nc)); |
| 937 | } |
| 938 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 939 | @Override |
| 940 | public boolean equals(Object obj) { |
| 941 | if (obj == null || (obj instanceof NetworkCapabilities == false)) return false; |
| 942 | NetworkCapabilities that = (NetworkCapabilities)obj; |
| 943 | return (equalsNetCapabilities(that) && |
| 944 | equalsTransportTypes(that) && |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 945 | equalsLinkBandwidths(that) && |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 946 | equalsSignalStrength(that) && |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 947 | equalsSpecifier(that)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | @Override |
| 951 | public int hashCode() { |
| 952 | return ((int)(mNetworkCapabilities & 0xFFFFFFFF) + |
| 953 | ((int)(mNetworkCapabilities >> 32) * 3) + |
| 954 | ((int)(mTransportTypes & 0xFFFFFFFF) * 5) + |
| 955 | ((int)(mTransportTypes >> 32) * 7) + |
| 956 | (mLinkUpBandwidthKbps * 11) + |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 957 | (mLinkDownBandwidthKbps * 13) + |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 958 | Objects.hashCode(mNetworkSpecifier) * 17 + |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 959 | (mSignalStrength * 19)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 962 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 963 | public int describeContents() { |
| 964 | return 0; |
| 965 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 966 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 967 | public void writeToParcel(Parcel dest, int flags) { |
| 968 | dest.writeLong(mNetworkCapabilities); |
| 969 | dest.writeLong(mTransportTypes); |
| 970 | dest.writeInt(mLinkUpBandwidthKbps); |
| 971 | dest.writeInt(mLinkDownBandwidthKbps); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 972 | dest.writeParcelable((Parcelable) mNetworkSpecifier, flags); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 973 | dest.writeInt(mSignalStrength); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 974 | } |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 975 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 976 | public static final Creator<NetworkCapabilities> CREATOR = |
| 977 | new Creator<NetworkCapabilities>() { |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 978 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 979 | public NetworkCapabilities createFromParcel(Parcel in) { |
| 980 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 981 | |
| 982 | netCap.mNetworkCapabilities = in.readLong(); |
| 983 | netCap.mTransportTypes = in.readLong(); |
| 984 | netCap.mLinkUpBandwidthKbps = in.readInt(); |
| 985 | netCap.mLinkDownBandwidthKbps = in.readInt(); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 986 | netCap.mNetworkSpecifier = in.readParcelable(null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 987 | netCap.mSignalStrength = in.readInt(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 988 | return netCap; |
| 989 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 990 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 991 | public NetworkCapabilities[] newArray(int size) { |
| 992 | return new NetworkCapabilities[size]; |
| 993 | } |
| 994 | }; |
| 995 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 996 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 997 | public String toString() { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 998 | // TODO: enumerate bits for transports and capabilities instead of creating arrays. |
| 999 | // TODO: use a StringBuilder instead of string concatenation. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1000 | int[] types = getTransportTypes(); |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1001 | String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : ""; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1002 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1003 | types = getCapabilities(); |
| 1004 | String capabilities = (types.length > 0 ? " Capabilities: " : ""); |
| 1005 | for (int i = 0; i < types.length; ) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1006 | capabilities += capabilityNameOf(types[i]); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1007 | if (++i < types.length) capabilities += "&"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + |
| 1011 | mLinkUpBandwidthKbps + "Kbps" : ""); |
| 1012 | String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" + |
| 1013 | mLinkDownBandwidthKbps + "Kbps" : ""); |
| 1014 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1015 | String specifier = (mNetworkSpecifier == null ? |
| 1016 | "" : " Specifier: <" + mNetworkSpecifier + ">"); |
| 1017 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1018 | String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : ""); |
| 1019 | |
| 1020 | return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1021 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1022 | |
| 1023 | /** |
| 1024 | * @hide |
| 1025 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 1026 | public static String capabilityNamesOf(@NetCapability int[] capabilities) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1027 | StringJoiner joiner = new StringJoiner("|"); |
| 1028 | if (capabilities != null) { |
| 1029 | for (int c : capabilities) { |
| 1030 | joiner.add(capabilityNameOf(c)); |
| 1031 | } |
| 1032 | } |
| 1033 | return joiner.toString(); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * @hide |
| 1038 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 1039 | public static String capabilityNameOf(@NetCapability int capability) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1040 | switch (capability) { |
| 1041 | case NET_CAPABILITY_MMS: return "MMS"; |
| 1042 | case NET_CAPABILITY_SUPL: return "SUPL"; |
| 1043 | case NET_CAPABILITY_DUN: return "DUN"; |
| 1044 | case NET_CAPABILITY_FOTA: return "FOTA"; |
| 1045 | case NET_CAPABILITY_IMS: return "IMS"; |
| 1046 | case NET_CAPABILITY_CBS: return "CBS"; |
| 1047 | case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P"; |
| 1048 | case NET_CAPABILITY_IA: return "IA"; |
| 1049 | case NET_CAPABILITY_RCS: return "RCS"; |
| 1050 | case NET_CAPABILITY_XCAP: return "XCAP"; |
| 1051 | case NET_CAPABILITY_EIMS: return "EIMS"; |
| 1052 | case NET_CAPABILITY_NOT_METERED: return "NOT_METERED"; |
| 1053 | case NET_CAPABILITY_INTERNET: return "INTERNET"; |
| 1054 | case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED"; |
| 1055 | case NET_CAPABILITY_TRUSTED: return "TRUSTED"; |
| 1056 | case NET_CAPABILITY_NOT_VPN: return "NOT_VPN"; |
| 1057 | case NET_CAPABILITY_VALIDATED: return "VALIDATED"; |
| 1058 | case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL"; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame^] | 1059 | case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING"; |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1060 | case NET_CAPABILITY_FOREGROUND: return "FOREGROUND"; |
| 1061 | default: return Integer.toString(capability); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * @hide |
| 1067 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 1068 | public static String transportNamesOf(@Transport int[] types) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1069 | StringJoiner joiner = new StringJoiner("|"); |
| 1070 | if (types != null) { |
| 1071 | for (int t : types) { |
| 1072 | joiner.add(transportNameOf(t)); |
| 1073 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1074 | } |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1075 | return joiner.toString(); |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * @hide |
| 1080 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 1081 | public static String transportNameOf(@Transport int transport) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 1082 | if (!isValidTransport(transport)) { |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 1083 | return "UNKNOWN"; |
| 1084 | } |
| 1085 | return TRANSPORT_NAMES[transport]; |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1086 | } |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 1087 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 1088 | private static void checkValidTransportType(@Transport int transport) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 1089 | Preconditions.checkArgument( |
| 1090 | isValidTransport(transport), "Invalid TransportType " + transport); |
| 1091 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1092 | } |