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