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