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