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