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