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