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