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