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