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