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