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