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