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