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