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