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