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 | */ |
| 680 | public void restrictCapabilitesForTestNetwork() { |
| 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; |
Chalard Jean | 09c48e4 | 2020-03-25 10:33:55 +0000 | [diff] [blame] | 684 | clearAll(); |
| 685 | // Reset the transports to only contain TRANSPORT_TEST. |
| 686 | mTransportTypes = (1 << TRANSPORT_TEST); |
| 687 | mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES; |
| 688 | mNetworkSpecifier = originalSpecifier; |
Chalard Jean | 3b2a81b | 2020-04-13 19:10:13 +0000 | [diff] [blame] | 689 | mSignalStrength = originalSignalStrength; |
Chalard Jean | 09c48e4 | 2020-03-25 10:33:55 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 693 | * Representing the transport type. Apps should generally not care about transport. A |
| 694 | * request for a fast internet connection could be satisfied by a number of different |
| 695 | * transports. If any are specified here it will be satisfied a Network that matches |
| 696 | * any of them. If a caller doesn't care about the transport it should not specify any. |
| 697 | */ |
| 698 | private long mTransportTypes; |
| 699 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 700 | /** @hide */ |
| 701 | @Retention(RetentionPolicy.SOURCE) |
| 702 | @IntDef(prefix = { "TRANSPORT_" }, value = { |
| 703 | TRANSPORT_CELLULAR, |
| 704 | TRANSPORT_WIFI, |
| 705 | TRANSPORT_BLUETOOTH, |
| 706 | TRANSPORT_ETHERNET, |
| 707 | TRANSPORT_VPN, |
| 708 | TRANSPORT_WIFI_AWARE, |
| 709 | TRANSPORT_LOWPAN, |
Benedict Wong | 89ce5e3 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 710 | TRANSPORT_TEST, |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 711 | }) |
| 712 | public @interface Transport { } |
| 713 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 714 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 715 | * Indicates this network uses a Cellular transport. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 716 | */ |
| 717 | public static final int TRANSPORT_CELLULAR = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 718 | |
| 719 | /** |
| 720 | * Indicates this network uses a Wi-Fi transport. |
| 721 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 722 | public static final int TRANSPORT_WIFI = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 723 | |
| 724 | /** |
| 725 | * Indicates this network uses a Bluetooth transport. |
| 726 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 727 | public static final int TRANSPORT_BLUETOOTH = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 728 | |
| 729 | /** |
| 730 | * Indicates this network uses an Ethernet transport. |
| 731 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 732 | public static final int TRANSPORT_ETHERNET = 3; |
| 733 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 734 | /** |
| 735 | * Indicates this network uses a VPN transport. |
| 736 | */ |
| 737 | public static final int TRANSPORT_VPN = 4; |
| 738 | |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 739 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 740 | * Indicates this network uses a Wi-Fi Aware transport. |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 741 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 742 | public static final int TRANSPORT_WIFI_AWARE = 5; |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 743 | |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 744 | /** |
| 745 | * Indicates this network uses a LoWPAN transport. |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 746 | */ |
| 747 | public static final int TRANSPORT_LOWPAN = 6; |
| 748 | |
Benedict Wong | 89ce5e3 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 749 | /** |
| 750 | * Indicates this network uses a Test-only virtual interface as a transport. |
| 751 | * |
| 752 | * @hide |
| 753 | */ |
| 754 | @TestApi |
| 755 | public static final int TRANSPORT_TEST = 7; |
| 756 | |
Hugo Benichi | 6a9bb8e | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 757 | /** @hide */ |
| 758 | public static final int MIN_TRANSPORT = TRANSPORT_CELLULAR; |
| 759 | /** @hide */ |
Benedict Wong | 89ce5e3 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 760 | public static final int MAX_TRANSPORT = TRANSPORT_TEST; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 761 | |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 762 | /** @hide */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 763 | public static boolean isValidTransport(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 764 | return (MIN_TRANSPORT <= transportType) && (transportType <= MAX_TRANSPORT); |
| 765 | } |
| 766 | |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 767 | private static final String[] TRANSPORT_NAMES = { |
| 768 | "CELLULAR", |
| 769 | "WIFI", |
| 770 | "BLUETOOTH", |
| 771 | "ETHERNET", |
| 772 | "VPN", |
Robert Quattlebaum | 5f91576 | 2017-05-15 15:53:29 -0700 | [diff] [blame] | 773 | "WIFI_AWARE", |
Benedict Wong | 89ce5e3 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 774 | "LOWPAN", |
| 775 | "TEST" |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 776 | }; |
| 777 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 778 | /** |
| 779 | * Adds the given transport type to this {@code NetworkCapability} instance. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 780 | * Multiple transports may be applied. Note that when searching |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 781 | * for a network to satisfy a request, any listed in the request will satisfy the request. |
| 782 | * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a |
| 783 | * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network |
| 784 | * to be selected. This is logically different than |
| 785 | * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. |
| 786 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 787 | * @param transportType the transport type to be added. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 788 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 789 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 790 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 791 | public @NonNull NetworkCapabilities addTransportType(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 792 | checkValidTransportType(transportType); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 793 | mTransportTypes |= 1 << transportType; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 794 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 795 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 796 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 797 | |
| 798 | /** |
| 799 | * Removes (if found) the given transport from this {@code NetworkCapability} instance. |
| 800 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 801 | * @param transportType the transport type to be removed. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 802 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 803 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 804 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 805 | public @NonNull NetworkCapabilities removeTransportType(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 806 | checkValidTransportType(transportType); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 807 | mTransportTypes &= ~(1 << transportType); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 808 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 809 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 810 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 811 | |
| 812 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 813 | * Sets (or clears) the given transport on this {@link NetworkCapabilities} |
| 814 | * instance. |
| 815 | * |
| 816 | * @hide |
| 817 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 818 | public @NonNull NetworkCapabilities setTransportType(@Transport int transportType, |
| 819 | boolean value) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 820 | if (value) { |
| 821 | addTransportType(transportType); |
| 822 | } else { |
| 823 | removeTransportType(transportType); |
| 824 | } |
| 825 | return this; |
| 826 | } |
| 827 | |
| 828 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 829 | * Gets all the transports set on this {@code NetworkCapability} instance. |
| 830 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 831 | * @return an array of transport type values for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 832 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 833 | */ |
Jeff Sharkey | a5ee62f | 2018-05-14 13:49:07 -0600 | [diff] [blame] | 834 | @TestApi |
Remi NGUYEN VAN | 94a0557 | 2019-01-20 12:38:10 +0900 | [diff] [blame] | 835 | @SystemApi |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 836 | @NonNull public @Transport int[] getTransportTypes() { |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 837 | return BitUtils.unpackBits(mTransportTypes); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 838 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 839 | |
| 840 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 841 | * Sets all the transports set on this {@code NetworkCapability} instance. |
Jeff Sharkey | 49bcd60 | 2017-11-09 13:11:50 -0700 | [diff] [blame] | 842 | * This overwrites any existing transports. |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 843 | * |
| 844 | * @hide |
| 845 | */ |
| 846 | public void setTransportTypes(@Transport int[] transportTypes) { |
| 847 | mTransportTypes = BitUtils.packBits(transportTypes); |
| 848 | } |
| 849 | |
| 850 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 851 | * Tests for the presence of a transport on this instance. |
| 852 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 853 | * @param transportType the transport type to be tested for. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 854 | * @return {@code true} if set on this instance. |
| 855 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 856 | public boolean hasTransport(@Transport int transportType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 857 | return isValidTransport(transportType) && ((mTransportTypes & (1 << transportType)) != 0); |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 858 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 859 | |
| 860 | private void combineTransportTypes(NetworkCapabilities nc) { |
| 861 | this.mTransportTypes |= nc.mTransportTypes; |
| 862 | } |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 863 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 864 | private boolean satisfiedByTransportTypes(NetworkCapabilities nc) { |
| 865 | return ((this.mTransportTypes == 0) || |
| 866 | ((this.mTransportTypes & nc.mTransportTypes) != 0)); |
| 867 | } |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 868 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 869 | /** @hide */ |
| 870 | public boolean equalsTransportTypes(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 871 | return (nc.mTransportTypes == this.mTransportTypes); |
| 872 | } |
| 873 | |
| 874 | /** |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 875 | * 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] | 876 | * |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 877 | * <p>This field keeps track of the UID of the app that created this network and is in charge of |
| 878 | * its lifecycle. This could be the UID of apps such as the Wifi network suggestor, the running |
| 879 | * VPN, or Carrier Service app managing a cellular data connection. |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 880 | * |
| 881 | * <p>For NetworkCapability instances being sent from ConnectivityService, this value MUST be |
| 882 | * reset to Process.INVALID_UID unless all the following conditions are met: |
| 883 | * |
| 884 | * <ol> |
| 885 | * <li>The destination app is the network owner |
| 886 | * <li>The destination app has the ACCESS_FINE_LOCATION permission granted |
| 887 | * <li>The user's location toggle is on |
| 888 | * </ol> |
| 889 | * |
| 890 | * This is because the owner UID is location-sensitive. The apps that request a network could |
| 891 | * know where the device is if they can tell for sure the system has connected to the network |
| 892 | * they requested. |
| 893 | * |
| 894 | * <p>This is populated by the network agents and for the NetworkCapabilities instance sent by |
| 895 | * an app to the System Server, the value MUST be reset to Process.INVALID_UID by the system |
| 896 | * server. |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 897 | */ |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 898 | private int mOwnerUid = Process.INVALID_UID; |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 899 | |
| 900 | /** |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 901 | * Set the UID of the owner app. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 902 | * @hide |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 903 | */ |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 904 | public @NonNull NetworkCapabilities setOwnerUid(final int uid) { |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 905 | mOwnerUid = uid; |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 906 | return this; |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 907 | } |
| 908 | |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 909 | /** |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 910 | * Retrieves the UID of the app that owns this network. |
| 911 | * |
| 912 | * <p>For user privacy reasons, this field will only be populated if: |
| 913 | * |
| 914 | * <ol> |
| 915 | * <li>The calling app is the network owner |
| 916 | * <li>The calling app has the ACCESS_FINE_LOCATION permission granted |
| 917 | * <li>The user's location toggle is on |
| 918 | * </ol> |
| 919 | * |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 920 | * Instances of NetworkCapabilities sent to apps without the appropriate permissions will |
| 921 | * have this field cleared out. |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 922 | */ |
| 923 | public int getOwnerUid() { |
| 924 | return mOwnerUid; |
Lorenzo Colitti | 4c9f954 | 2019-04-12 10:48:06 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 927 | /** |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 928 | * UIDs of packages that are administrators of this network, or empty if none. |
| 929 | * |
| 930 | * <p>This field tracks the UIDs of packages that have permission to manage this network. |
| 931 | * |
| 932 | * <p>Network owners will also be listed as administrators. |
| 933 | * |
| 934 | * <p>For NetworkCapability instances being sent from the System Server, this value MUST be |
| 935 | * empty unless the destination is 1) the System Server, or 2) Telephony. In either case, the |
| 936 | * receiving entity must have the ACCESS_FINE_LOCATION permission and target R+. |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 937 | * |
| 938 | * <p>When received from an app in a NetworkRequest this is always cleared out by the system |
| 939 | * server. This field is never used for matching NetworkRequests to NetworkAgents. |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 940 | */ |
Cody Kesting | 919385b | 2020-03-18 15:22:12 -0700 | [diff] [blame] | 941 | @NonNull private int[] mAdministratorUids = new int[0]; |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 942 | |
| 943 | /** |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 944 | * Sets the int[] of UIDs that are administrators of this network. |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 945 | * |
| 946 | * <p>UIDs included in administratorUids gain administrator privileges over this Network. |
| 947 | * Examples of UIDs that should be included in administratorUids are: |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 948 | * |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 949 | * <ul> |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 950 | * <li>Carrier apps with privileges for the relevant subscription |
| 951 | * <li>Active VPN apps |
| 952 | * <li>Other application groups with a particular Network-related role |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 953 | * </ul> |
| 954 | * |
| 955 | * <p>In general, user-supplied networks (such as WiFi networks) do not have an administrator. |
| 956 | * |
Cody Kesting | a75e26b | 2020-01-05 14:06:39 -0800 | [diff] [blame] | 957 | * <p>An app is granted owner privileges over Networks that it supplies. The owner UID MUST |
| 958 | * always be included in administratorUids. |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 959 | * |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 960 | * <p>The administrator UIDs are set by network agents. |
| 961 | * |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 962 | * @param administratorUids the UIDs to be set as administrators of this Network. |
Cody Kesting | 93c1e65 | 2020-03-24 11:53:30 -0700 | [diff] [blame] | 963 | * @throws IllegalArgumentException if duplicate UIDs are contained in administratorUids |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 964 | * @see #mAdministratorUids |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 965 | * @hide |
| 966 | */ |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 967 | @NonNull |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 968 | public NetworkCapabilities setAdministratorUids(@NonNull final int[] administratorUids) { |
| 969 | mAdministratorUids = Arrays.copyOf(administratorUids, administratorUids.length); |
Cody Kesting | 93c1e65 | 2020-03-24 11:53:30 -0700 | [diff] [blame] | 970 | Arrays.sort(mAdministratorUids); |
| 971 | for (int i = 0; i < mAdministratorUids.length - 1; i++) { |
| 972 | if (mAdministratorUids[i] >= mAdministratorUids[i + 1]) { |
| 973 | throw new IllegalArgumentException("All administrator UIDs must be unique"); |
| 974 | } |
| 975 | } |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 976 | return this; |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /** |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 980 | * Retrieves the UIDs that are administrators of this Network. |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 981 | * |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 982 | * <p>This is only populated in NetworkCapabilities objects that come from network agents for |
| 983 | * networks that are managed by specific apps on the system, such as carrier privileged apps or |
| 984 | * wifi suggestion apps. This will include the network owner. |
| 985 | * |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 986 | * @return the int[] of UIDs that are administrators of this Network |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 987 | * @see #mAdministratorUids |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 988 | * @hide |
| 989 | */ |
| 990 | @NonNull |
| 991 | @SystemApi |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 992 | @TestApi |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 993 | public int[] getAdministratorUids() { |
| 994 | return Arrays.copyOf(mAdministratorUids, mAdministratorUids.length); |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /** |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 998 | * Tests if the set of administrator UIDs of this network is the same as that of the passed one. |
| 999 | * |
| 1000 | * <p>The administrator UIDs must be in sorted order. |
| 1001 | * |
| 1002 | * <p>nc is assumed non-null. Else, NPE. |
| 1003 | * |
| 1004 | * @hide |
| 1005 | */ |
| 1006 | @VisibleForTesting(visibility = PRIVATE) |
| 1007 | public boolean equalsAdministratorUids(@NonNull final NetworkCapabilities nc) { |
| 1008 | return Arrays.equals(mAdministratorUids, nc.mAdministratorUids); |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * Combine the administrator UIDs of the capabilities. |
| 1013 | * |
| 1014 | * <p>This is only legal if either of the administrators lists are empty, or if they are equal. |
| 1015 | * Combining administrator UIDs is only possible for combining non-overlapping sets of UIDs. |
| 1016 | * |
| 1017 | * <p>If both administrator lists are non-empty but not equal, they conflict with each other. In |
| 1018 | * this case, it would not make sense to add them together. |
| 1019 | */ |
| 1020 | private void combineAdministratorUids(@NonNull final NetworkCapabilities nc) { |
| 1021 | if (nc.mAdministratorUids.length == 0) return; |
| 1022 | if (mAdministratorUids.length == 0) { |
| 1023 | mAdministratorUids = Arrays.copyOf(nc.mAdministratorUids, nc.mAdministratorUids.length); |
| 1024 | return; |
| 1025 | } |
| 1026 | if (!equalsAdministratorUids(nc)) { |
| 1027 | throw new IllegalStateException("Can't combine two different administrator UID lists"); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /** |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 1032 | * Value indicating that link bandwidth is unspecified. |
| 1033 | * @hide |
| 1034 | */ |
| 1035 | public static final int LINK_BANDWIDTH_UNSPECIFIED = 0; |
| 1036 | |
| 1037 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1038 | * Passive link bandwidth. This is a rough guide of the expected peak bandwidth |
| 1039 | * for the first hop on the given transport. It is not measured, but may take into account |
| 1040 | * link parameters (Radio technology, allocated channels, etc). |
| 1041 | */ |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 1042 | private int mLinkUpBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED; |
| 1043 | private int mLinkDownBandwidthKbps = LINK_BANDWIDTH_UNSPECIFIED; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1044 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1045 | /** |
| 1046 | * Sets the upstream bandwidth for this network in Kbps. This always only refers to |
| 1047 | * the estimated first hop transport bandwidth. |
| 1048 | * <p> |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1049 | * {@see Builder#setLinkUpstreamBandwidthKbps} |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1050 | * |
| 1051 | * @param upKbps the estimated first hop upstream (device to network) bandwidth. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1052 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1053 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1054 | public @NonNull NetworkCapabilities setLinkUpstreamBandwidthKbps(int upKbps) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1055 | mLinkUpBandwidthKbps = upKbps; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 1056 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1057 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1058 | |
| 1059 | /** |
| 1060 | * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to |
| 1061 | * the estimated first hop transport bandwidth. |
| 1062 | * |
| 1063 | * @return The estimated first hop upstream (device to network) bandwidth. |
| 1064 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1065 | public int getLinkUpstreamBandwidthKbps() { |
| 1066 | return mLinkUpBandwidthKbps; |
| 1067 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1068 | |
| 1069 | /** |
| 1070 | * Sets the downstream bandwidth for this network in Kbps. This always only refers to |
| 1071 | * the estimated first hop transport bandwidth. |
| 1072 | * <p> |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1073 | * {@see Builder#setLinkUpstreamBandwidthKbps} |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1074 | * |
| 1075 | * @param downKbps the estimated first hop downstream (network to device) bandwidth. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1076 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1077 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1078 | public @NonNull NetworkCapabilities setLinkDownstreamBandwidthKbps(int downKbps) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1079 | mLinkDownBandwidthKbps = downKbps; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 1080 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1081 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 1082 | |
| 1083 | /** |
| 1084 | * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to |
| 1085 | * the estimated first hop transport bandwidth. |
| 1086 | * |
| 1087 | * @return The estimated first hop downstream (network to device) bandwidth. |
| 1088 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1089 | public int getLinkDownstreamBandwidthKbps() { |
| 1090 | return mLinkDownBandwidthKbps; |
| 1091 | } |
| 1092 | |
| 1093 | private void combineLinkBandwidths(NetworkCapabilities nc) { |
| 1094 | this.mLinkUpBandwidthKbps = |
| 1095 | Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps); |
| 1096 | this.mLinkDownBandwidthKbps = |
| 1097 | Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps); |
| 1098 | } |
| 1099 | private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) { |
| 1100 | return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps || |
| 1101 | this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps); |
| 1102 | } |
| 1103 | private boolean equalsLinkBandwidths(NetworkCapabilities nc) { |
| 1104 | return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && |
| 1105 | this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); |
| 1106 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 1107 | /** @hide */ |
| 1108 | public static int minBandwidth(int a, int b) { |
| 1109 | if (a == LINK_BANDWIDTH_UNSPECIFIED) { |
| 1110 | return b; |
| 1111 | } else if (b == LINK_BANDWIDTH_UNSPECIFIED) { |
| 1112 | return a; |
| 1113 | } else { |
| 1114 | return Math.min(a, b); |
| 1115 | } |
| 1116 | } |
| 1117 | /** @hide */ |
| 1118 | public static int maxBandwidth(int a, int b) { |
| 1119 | return Math.max(a, b); |
| 1120 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1121 | |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1122 | private NetworkSpecifier mNetworkSpecifier = null; |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1123 | private TransportInfo mTransportInfo = null; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1124 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1125 | /** |
| 1126 | * Sets the optional bearer specific network specifier. |
| 1127 | * This has no meaning if a single transport is also not specified, so calling |
| 1128 | * this without a single transport set will generate an exception, as will |
| 1129 | * subsequently adding or removing transports after this is set. |
| 1130 | * </p> |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1131 | * |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1132 | * @param networkSpecifier A concrete, parcelable framework class that extends |
| 1133 | * NetworkSpecifier. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 1134 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1135 | * @hide |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1136 | */ |
Aaron Huang | e6b6239 | 2019-09-20 22:52:54 +0800 | [diff] [blame] | 1137 | public @NonNull NetworkCapabilities setNetworkSpecifier( |
| 1138 | @NonNull NetworkSpecifier networkSpecifier) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1139 | if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1140 | throw new IllegalStateException("Must have a single transport specified to use " + |
| 1141 | "setNetworkSpecifier"); |
| 1142 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1143 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1144 | mNetworkSpecifier = networkSpecifier; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1145 | |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 1146 | return this; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /** |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1150 | * Sets the optional transport specific information. |
| 1151 | * |
| 1152 | * @param transportInfo A concrete, parcelable framework class that extends |
| 1153 | * {@link TransportInfo}. |
| 1154 | * @return This NetworkCapabilities instance, to facilitate chaining. |
| 1155 | * @hide |
| 1156 | */ |
Aaron Huang | e6b6239 | 2019-09-20 22:52:54 +0800 | [diff] [blame] | 1157 | public @NonNull NetworkCapabilities setTransportInfo(@NonNull TransportInfo transportInfo) { |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1158 | mTransportInfo = transportInfo; |
| 1159 | return this; |
| 1160 | } |
| 1161 | |
| 1162 | /** |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1163 | * 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] | 1164 | * |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1165 | * @return The optional {@link NetworkSpecifier} specifying the bearer specific network |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1166 | * specifier or {@code null}. |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1167 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1168 | public @Nullable NetworkSpecifier getNetworkSpecifier() { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1169 | return mNetworkSpecifier; |
| 1170 | } |
| 1171 | |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1172 | /** |
| 1173 | * Returns a transport-specific information container. The application may cast this |
| 1174 | * container to a concrete sub-class based on its knowledge of the network request. The |
| 1175 | * 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] | 1176 | * e.g. use {@code instanceof} operator to verify expected type. |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1177 | * |
| 1178 | * @return A concrete implementation of the {@link TransportInfo} class or null if not |
| 1179 | * available for the network. |
| 1180 | */ |
| 1181 | @Nullable public TransportInfo getTransportInfo() { |
| 1182 | return mTransportInfo; |
| 1183 | } |
| 1184 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1185 | private void combineSpecifiers(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1186 | if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1187 | throw new IllegalStateException("Can't combine two networkSpecifiers"); |
| 1188 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1189 | setNetworkSpecifier(nc.mNetworkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1190 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1191 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1192 | private boolean satisfiedBySpecifier(NetworkCapabilities nc) { |
Chalard Jean | 2da4f9f | 2020-03-27 17:57:34 +0900 | [diff] [blame] | 1193 | return mNetworkSpecifier == null || mNetworkSpecifier.canBeSatisfiedBy(nc.mNetworkSpecifier) |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1194 | || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1195 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1196 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1197 | private boolean equalsSpecifier(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1198 | return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1201 | private void combineTransportInfos(NetworkCapabilities nc) { |
| 1202 | if (mTransportInfo != null && !mTransportInfo.equals(nc.mTransportInfo)) { |
| 1203 | throw new IllegalStateException("Can't combine two TransportInfos"); |
| 1204 | } |
| 1205 | setTransportInfo(nc.mTransportInfo); |
| 1206 | } |
| 1207 | |
| 1208 | private boolean equalsTransportInfo(NetworkCapabilities nc) { |
| 1209 | return Objects.equals(mTransportInfo, nc.mTransportInfo); |
| 1210 | } |
| 1211 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1212 | /** |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1213 | * Magic value that indicates no signal strength provided. A request specifying this value is |
| 1214 | * always satisfied. |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1215 | */ |
| 1216 | public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE; |
| 1217 | |
| 1218 | /** |
| 1219 | * Signal strength. This is a signed integer, and higher values indicate better signal. |
| 1220 | * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI. |
| 1221 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1222 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Sharkey | 49bcd60 | 2017-11-09 13:11:50 -0700 | [diff] [blame] | 1223 | private int mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1224 | |
| 1225 | /** |
| 1226 | * Sets the signal strength. This is a signed integer, with higher values indicating a stronger |
| 1227 | * 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] | 1228 | * reported by wifi code. |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1229 | * <p> |
| 1230 | * Note that when used to register a network callback, this specifies the minimum acceptable |
| 1231 | * signal strength. When received as the state of an existing network it specifies the current |
| 1232 | * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no |
| 1233 | * effect when requesting a callback. |
| 1234 | * |
| 1235 | * @param signalStrength the bearer-specific signal strength. |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1236 | * @hide |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1237 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1238 | public @NonNull NetworkCapabilities setSignalStrength(int signalStrength) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1239 | mSignalStrength = signalStrength; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 1240 | return this; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * Returns {@code true} if this object specifies a signal strength. |
| 1245 | * |
| 1246 | * @hide |
| 1247 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1248 | @UnsupportedAppUsage |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1249 | public boolean hasSignalStrength() { |
| 1250 | return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED; |
| 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * Retrieves the signal strength. |
| 1255 | * |
| 1256 | * @return The bearer-specific signal strength. |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1257 | */ |
| 1258 | public int getSignalStrength() { |
| 1259 | return mSignalStrength; |
| 1260 | } |
| 1261 | |
| 1262 | private void combineSignalStrength(NetworkCapabilities nc) { |
| 1263 | this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength); |
| 1264 | } |
| 1265 | |
| 1266 | private boolean satisfiedBySignalStrength(NetworkCapabilities nc) { |
| 1267 | return this.mSignalStrength <= nc.mSignalStrength; |
| 1268 | } |
| 1269 | |
| 1270 | private boolean equalsSignalStrength(NetworkCapabilities nc) { |
| 1271 | return this.mSignalStrength == nc.mSignalStrength; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1275 | * List of UIDs this network applies to. No restriction if null. |
| 1276 | * <p> |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1277 | * For networks, mUids represent the list of network this applies to, and null means this |
| 1278 | * network applies to all UIDs. |
| 1279 | * For requests, mUids is the list of UIDs this network MUST apply to to match ; ALL UIDs |
| 1280 | * must be included in a network so that they match. As an exception to the general rule, |
| 1281 | * a null mUids field for requests mean "no requirements" rather than what the general rule |
| 1282 | * would suggest ("must apply to all UIDs") : this is because this has shown to be what users |
| 1283 | * of this API expect in practice. A network that must match all UIDs can still be |
| 1284 | * expressed with a set ranging the entire set of possible UIDs. |
| 1285 | * <p> |
| 1286 | * 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] | 1287 | * the UIDs in this list, and it is their default network. Apps in this list that wish to |
| 1288 | * bypass the VPN can do so iff the VPN app allows them to or if they are privileged. If this |
| 1289 | * member is null, then the network is not restricted by app UID. If it's an empty list, then |
| 1290 | * it means nobody can use it. |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 1291 | * 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] | 1292 | * 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] | 1293 | * satisfiedByUids. That still does not mean the network necessarily <strong>applies</strong> |
| 1294 | * to the app that manages it as determined by #appliesToUid. |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1295 | * <p> |
| 1296 | * Please note that in principle a single app can be associated with multiple UIDs because |
| 1297 | * each app will have a different UID when it's run as a different (macro-)user. A single |
| 1298 | * macro user can only have a single active VPN app at any given time however. |
| 1299 | * <p> |
| 1300 | * Also please be aware this class does not try to enforce any normalization on this. Callers |
| 1301 | * can only alter the UIDs by setting them wholesale : this class does not provide any utility |
| 1302 | * to add or remove individual UIDs or ranges. If callers have any normalization needs on |
| 1303 | * their own (like requiring sortedness or no overlap) they need to enforce it |
| 1304 | * themselves. Some of the internal methods also assume this is normalized as in no adjacent |
| 1305 | * or overlapping ranges are present. |
| 1306 | * |
| 1307 | * @hide |
| 1308 | */ |
Chalard Jean | 477e36c | 2018-01-25 09:41:51 +0900 | [diff] [blame] | 1309 | private ArraySet<UidRange> mUids = null; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1310 | |
| 1311 | /** |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 1312 | * Convenience method to set the UIDs this network applies to to a single UID. |
| 1313 | * @hide |
| 1314 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1315 | public @NonNull NetworkCapabilities setSingleUid(int uid) { |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 1316 | final ArraySet<UidRange> identity = new ArraySet<>(1); |
| 1317 | identity.add(new UidRange(uid, uid)); |
| 1318 | setUids(identity); |
| 1319 | return this; |
| 1320 | } |
| 1321 | |
| 1322 | /** |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1323 | * Set the list of UIDs this network applies to. |
| 1324 | * This makes a copy of the set so that callers can't modify it after the call. |
| 1325 | * @hide |
| 1326 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1327 | public @NonNull NetworkCapabilities setUids(Set<UidRange> uids) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1328 | if (null == uids) { |
| 1329 | mUids = null; |
| 1330 | } else { |
| 1331 | mUids = new ArraySet<>(uids); |
| 1332 | } |
| 1333 | return this; |
| 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * Get the list of UIDs this network applies to. |
| 1338 | * This returns a copy of the set so that callers can't modify the original object. |
| 1339 | * @hide |
| 1340 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1341 | public @Nullable Set<UidRange> getUids() { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1342 | return null == mUids ? null : new ArraySet<>(mUids); |
| 1343 | } |
| 1344 | |
| 1345 | /** |
| 1346 | * Test whether this network applies to this UID. |
| 1347 | * @hide |
| 1348 | */ |
| 1349 | public boolean appliesToUid(int uid) { |
| 1350 | if (null == mUids) return true; |
| 1351 | for (UidRange range : mUids) { |
| 1352 | if (range.contains(uid)) { |
| 1353 | return true; |
| 1354 | } |
| 1355 | } |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | /** |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1360 | * 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] | 1361 | * <p> |
| 1362 | * This test only checks whether equal range objects are in both sets. It will |
| 1363 | * return false if the ranges are not exactly the same, even if the covered UIDs |
| 1364 | * are for an equivalent result. |
| 1365 | * <p> |
| 1366 | * Note that this method is not very optimized, which is fine as long as it's not used very |
| 1367 | * often. |
| 1368 | * <p> |
| 1369 | * nc is assumed nonnull. |
| 1370 | * |
| 1371 | * @hide |
| 1372 | */ |
| 1373 | @VisibleForTesting |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1374 | public boolean equalsUids(@NonNull NetworkCapabilities nc) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1375 | Set<UidRange> comparedUids = nc.mUids; |
| 1376 | if (null == comparedUids) return null == mUids; |
| 1377 | if (null == mUids) return false; |
| 1378 | // Make a copy so it can be mutated to check that all ranges in mUids |
| 1379 | // also are in uids. |
| 1380 | final Set<UidRange> uids = new ArraySet<>(mUids); |
| 1381 | for (UidRange range : comparedUids) { |
| 1382 | if (!uids.contains(range)) { |
| 1383 | return false; |
| 1384 | } |
| 1385 | uids.remove(range); |
| 1386 | } |
| 1387 | return uids.isEmpty(); |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * Test whether the passed NetworkCapabilities satisfies the UIDs this capabilities require. |
| 1392 | * |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 1393 | * This method is called on the NetworkCapabilities embedded in a request with the |
| 1394 | * capabilities of an available network. It checks whether all the UIDs from this listen |
| 1395 | * (representing the UIDs that must have access to the network) are satisfied by the UIDs |
| 1396 | * in the passed nc (representing the UIDs that this network is available to). |
| 1397 | * <p> |
| 1398 | * 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] | 1399 | * mOwnerUid field) always satisfies a NetworkRequest requiring it (of LISTEN |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 1400 | * or REQUEST types alike), even if the network does not apply to it. That is so a VPN app |
| 1401 | * can see its own network when it listens for it. |
| 1402 | * <p> |
| 1403 | * nc is assumed nonnull. Else, NPE. |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1404 | * @see #appliesToUid |
| 1405 | * @hide |
| 1406 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1407 | public boolean satisfiedByUids(@NonNull NetworkCapabilities nc) { |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1408 | if (null == nc.mUids || null == mUids) return true; // The network satisfies everything. |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1409 | for (UidRange requiredRange : mUids) { |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 1410 | if (requiredRange.contains(nc.mOwnerUid)) return true; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1411 | if (!nc.appliesToUidRange(requiredRange)) { |
| 1412 | return false; |
| 1413 | } |
| 1414 | } |
| 1415 | return true; |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * Returns whether this network applies to the passed ranges. |
| 1420 | * This assumes that to apply, the passed range has to be entirely contained |
| 1421 | * within one of the ranges this network applies to. If the ranges are not normalized, |
| 1422 | * this method may return false even though all required UIDs are covered because no |
| 1423 | * single range contained them all. |
| 1424 | * @hide |
| 1425 | */ |
| 1426 | @VisibleForTesting |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1427 | public boolean appliesToUidRange(@Nullable UidRange requiredRange) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1428 | if (null == mUids) return true; |
| 1429 | for (UidRange uidRange : mUids) { |
| 1430 | if (uidRange.containsRange(requiredRange)) { |
| 1431 | return true; |
| 1432 | } |
| 1433 | } |
| 1434 | return false; |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * Combine the UIDs this network currently applies to with the UIDs the passed |
| 1439 | * NetworkCapabilities apply to. |
| 1440 | * nc is assumed nonnull. |
| 1441 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1442 | private void combineUids(@NonNull NetworkCapabilities nc) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1443 | if (null == nc.mUids || null == mUids) { |
| 1444 | mUids = null; |
| 1445 | return; |
| 1446 | } |
| 1447 | mUids.addAll(nc.mUids); |
| 1448 | } |
| 1449 | |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1450 | |
| 1451 | /** |
| 1452 | * The SSID of the network, or null if not applicable or unknown. |
| 1453 | * <p> |
| 1454 | * This is filled in by wifi code. |
| 1455 | * @hide |
| 1456 | */ |
| 1457 | private String mSSID; |
| 1458 | |
| 1459 | /** |
| 1460 | * Sets the SSID of this network. |
| 1461 | * @hide |
| 1462 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1463 | public @NonNull NetworkCapabilities setSSID(@Nullable String ssid) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1464 | mSSID = ssid; |
| 1465 | return this; |
| 1466 | } |
| 1467 | |
| 1468 | /** |
| 1469 | * Gets the SSID of this network, or null if none or unknown. |
| 1470 | * @hide |
| 1471 | */ |
Remi NGUYEN VAN | aa4c511 | 2020-01-22 22:52:53 +0900 | [diff] [blame] | 1472 | @SystemApi |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1473 | @TestApi |
| 1474 | public @Nullable String getSsid() { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1475 | return mSSID; |
| 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * Tests if the SSID of this network is the same as the SSID of the passed network. |
| 1480 | * @hide |
| 1481 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1482 | public boolean equalsSSID(@NonNull NetworkCapabilities nc) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1483 | return Objects.equals(mSSID, nc.mSSID); |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * Check if the SSID requirements of this object are matched by the passed object. |
| 1488 | * @hide |
| 1489 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1490 | public boolean satisfiedBySSID(@NonNull NetworkCapabilities nc) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1491 | return mSSID == null || mSSID.equals(nc.mSSID); |
| 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * Combine SSIDs of the capabilities. |
| 1496 | * <p> |
| 1497 | * This is only legal if either the SSID of this object is null, or both SSIDs are |
| 1498 | * equal. |
| 1499 | * @hide |
| 1500 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1501 | private void combineSSIDs(@NonNull NetworkCapabilities nc) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1502 | if (mSSID != null && !mSSID.equals(nc.mSSID)) { |
| 1503 | throw new IllegalStateException("Can't combine two SSIDs"); |
| 1504 | } |
| 1505 | setSSID(nc.mSSID); |
| 1506 | } |
| 1507 | |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1508 | /** |
Pavel Maltsev | e18ef26 | 2018-03-07 11:13:04 -0800 | [diff] [blame] | 1509 | * Combine a set of Capabilities to this one. Useful for coming up with the complete set. |
| 1510 | * <p> |
| 1511 | * Note that this method may break an invariant of having a particular capability in either |
| 1512 | * wanted or unwanted lists but never in both. Requests that have the same capability in |
| 1513 | * both lists will never be satisfied. |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 1514 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1515 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1516 | public void combineCapabilities(@NonNull NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1517 | combineNetCapabilities(nc); |
| 1518 | combineTransportTypes(nc); |
| 1519 | combineLinkBandwidths(nc); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1520 | combineSpecifiers(nc); |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1521 | combineTransportInfos(nc); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1522 | combineSignalStrength(nc); |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1523 | combineUids(nc); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1524 | combineSSIDs(nc); |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1525 | combineRequestor(nc); |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 1526 | combineAdministratorUids(nc); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 1530 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 1531 | * |
| 1532 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 1533 | * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link |
| 1534 | * bandwidth, signal strength, or validation / captive portal status. |
| 1535 | * |
| 1536 | * @hide |
| 1537 | */ |
| 1538 | private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1539 | return (nc != null |
| 1540 | && satisfiedByNetCapabilities(nc, onlyImmutable) |
| 1541 | && satisfiedByTransportTypes(nc) |
| 1542 | && (onlyImmutable || satisfiedByLinkBandwidths(nc)) |
| 1543 | && satisfiedBySpecifier(nc) |
| 1544 | && (onlyImmutable || satisfiedBySignalStrength(nc)) |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1545 | && (onlyImmutable || satisfiedByUids(nc)) |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1546 | && (onlyImmutable || satisfiedBySSID(nc))) |
| 1547 | && (onlyImmutable || satisfiedByRequestor(nc)); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 1552 | * |
| 1553 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 1554 | * |
| 1555 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1556 | */ |
Remi NGUYEN VAN | 94a0557 | 2019-01-20 12:38:10 +0900 | [diff] [blame] | 1557 | @TestApi |
| 1558 | @SystemApi |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1559 | public boolean satisfiedByNetworkCapabilities(@Nullable NetworkCapabilities nc) { |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 1560 | return satisfiedByNetworkCapabilities(nc, false); |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}. |
| 1565 | * |
| 1566 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 1567 | * |
| 1568 | * @hide |
| 1569 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1570 | public boolean satisfiedByImmutableNetworkCapabilities(@Nullable NetworkCapabilities nc) { |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 1571 | return satisfiedByNetworkCapabilities(nc, true); |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * 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] | 1576 | * {@code NetworkCapabilities} and return a String describing any difference. |
| 1577 | * The returned String is empty if there is no difference. |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 1578 | * |
| 1579 | * @hide |
| 1580 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1581 | public String describeImmutableDifferences(@Nullable NetworkCapabilities that) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1582 | if (that == null) { |
| 1583 | return "other NetworkCapabilities was null"; |
| 1584 | } |
| 1585 | |
| 1586 | StringJoiner joiner = new StringJoiner(", "); |
| 1587 | |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1588 | // Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103 |
| 1589 | // TODO: properly support NOT_METERED as a mutable and requestable capability. |
Hugo Benichi | 2ecb940 | 2017-08-04 13:18:40 +0900 | [diff] [blame] | 1590 | final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED); |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1591 | long oldImmutableCapabilities = this.mNetworkCapabilities & mask; |
| 1592 | long newImmutableCapabilities = that.mNetworkCapabilities & mask; |
| 1593 | if (oldImmutableCapabilities != newImmutableCapabilities) { |
| 1594 | String before = capabilityNamesOf(BitUtils.unpackBits(oldImmutableCapabilities)); |
| 1595 | String after = capabilityNamesOf(BitUtils.unpackBits(newImmutableCapabilities)); |
| 1596 | joiner.add(String.format("immutable capabilities changed: %s -> %s", before, after)); |
| 1597 | } |
| 1598 | |
| 1599 | if (!equalsSpecifier(that)) { |
| 1600 | NetworkSpecifier before = this.getNetworkSpecifier(); |
| 1601 | NetworkSpecifier after = that.getNetworkSpecifier(); |
| 1602 | joiner.add(String.format("specifier changed: %s -> %s", before, after)); |
| 1603 | } |
| 1604 | |
| 1605 | if (!equalsTransportTypes(that)) { |
| 1606 | String before = transportNamesOf(this.getTransportTypes()); |
| 1607 | String after = transportNamesOf(that.getTransportTypes()); |
| 1608 | joiner.add(String.format("transports changed: %s -> %s", before, after)); |
| 1609 | } |
| 1610 | |
| 1611 | return joiner.toString(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 1614 | /** |
| 1615 | * Checks that our requestable capabilities are the same as those of the given |
| 1616 | * {@code NetworkCapabilities}. |
| 1617 | * |
| 1618 | * @hide |
| 1619 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1620 | public boolean equalRequestableCapabilities(@Nullable NetworkCapabilities nc) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 1621 | if (nc == null) return false; |
| 1622 | return (equalsNetCapabilitiesRequestable(nc) && |
| 1623 | equalsTransportTypes(nc) && |
| 1624 | equalsSpecifier(nc)); |
| 1625 | } |
| 1626 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1627 | @Override |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1628 | public boolean equals(@Nullable Object obj) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1629 | if (obj == null || (obj instanceof NetworkCapabilities == false)) return false; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1630 | NetworkCapabilities that = (NetworkCapabilities) obj; |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1631 | return equalsNetCapabilities(that) |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1632 | && equalsTransportTypes(that) |
| 1633 | && equalsLinkBandwidths(that) |
| 1634 | && equalsSignalStrength(that) |
| 1635 | && equalsSpecifier(that) |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1636 | && equalsTransportInfo(that) |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1637 | && equalsUids(that) |
lucaslin | 783f221 | 2019-10-22 18:27:33 +0800 | [diff] [blame] | 1638 | && equalsSSID(that) |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1639 | && equalsPrivateDnsBroken(that) |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 1640 | && equalsRequestor(that) |
| 1641 | && equalsAdministratorUids(that); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | @Override |
| 1645 | public int hashCode() { |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 1646 | return (int) (mNetworkCapabilities & 0xFFFFFFFF) |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1647 | + ((int) (mNetworkCapabilities >> 32) * 3) |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 1648 | + ((int) (mUnwantedNetworkCapabilities & 0xFFFFFFFF) * 5) |
| 1649 | + ((int) (mUnwantedNetworkCapabilities >> 32) * 7) |
| 1650 | + ((int) (mTransportTypes & 0xFFFFFFFF) * 11) |
| 1651 | + ((int) (mTransportTypes >> 32) * 13) |
| 1652 | + (mLinkUpBandwidthKbps * 17) |
| 1653 | + (mLinkDownBandwidthKbps * 19) |
| 1654 | + Objects.hashCode(mNetworkSpecifier) * 23 |
| 1655 | + (mSignalStrength * 29) |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1656 | + Objects.hashCode(mUids) * 31 |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1657 | + Objects.hashCode(mSSID) * 37 |
lucaslin | 783f221 | 2019-10-22 18:27:33 +0800 | [diff] [blame] | 1658 | + Objects.hashCode(mTransportInfo) * 41 |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1659 | + Objects.hashCode(mPrivateDnsBroken) * 43 |
| 1660 | + Objects.hashCode(mRequestorUid) * 47 |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 1661 | + Objects.hashCode(mRequestorPackageName) * 53 |
| 1662 | + Arrays.hashCode(mAdministratorUids) * 59; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 1665 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1666 | public int describeContents() { |
| 1667 | return 0; |
| 1668 | } |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 1669 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 1670 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1671 | public void writeToParcel(Parcel dest, int flags) { |
| 1672 | dest.writeLong(mNetworkCapabilities); |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 1673 | dest.writeLong(mUnwantedNetworkCapabilities); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1674 | dest.writeLong(mTransportTypes); |
| 1675 | dest.writeInt(mLinkUpBandwidthKbps); |
| 1676 | dest.writeInt(mLinkDownBandwidthKbps); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1677 | dest.writeParcelable((Parcelable) mNetworkSpecifier, flags); |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1678 | dest.writeParcelable((Parcelable) mTransportInfo, flags); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1679 | dest.writeInt(mSignalStrength); |
Chalard Jean | 477e36c | 2018-01-25 09:41:51 +0900 | [diff] [blame] | 1680 | dest.writeArraySet(mUids); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1681 | dest.writeString(mSSID); |
lucaslin | 783f221 | 2019-10-22 18:27:33 +0800 | [diff] [blame] | 1682 | dest.writeBoolean(mPrivateDnsBroken); |
Chalard Jean | 981dcca | 2020-02-06 18:31:19 +0900 | [diff] [blame] | 1683 | dest.writeIntArray(getAdministratorUids()); |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 1684 | dest.writeInt(mOwnerUid); |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1685 | dest.writeInt(mRequestorUid); |
| 1686 | dest.writeString(mRequestorPackageName); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1687 | } |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1688 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 1689 | public static final @android.annotation.NonNull Creator<NetworkCapabilities> CREATOR = |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1690 | new Creator<NetworkCapabilities>() { |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 1691 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1692 | public NetworkCapabilities createFromParcel(Parcel in) { |
| 1693 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1694 | |
| 1695 | netCap.mNetworkCapabilities = in.readLong(); |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 1696 | netCap.mUnwantedNetworkCapabilities = in.readLong(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1697 | netCap.mTransportTypes = in.readLong(); |
| 1698 | netCap.mLinkUpBandwidthKbps = in.readInt(); |
| 1699 | netCap.mLinkDownBandwidthKbps = in.readInt(); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 1700 | netCap.mNetworkSpecifier = in.readParcelable(null); |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1701 | netCap.mTransportInfo = in.readParcelable(null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1702 | netCap.mSignalStrength = in.readInt(); |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1703 | netCap.mUids = (ArraySet<UidRange>) in.readArraySet( |
| 1704 | null /* ClassLoader, null for default */); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1705 | netCap.mSSID = in.readString(); |
lucaslin | 783f221 | 2019-10-22 18:27:33 +0800 | [diff] [blame] | 1706 | netCap.mPrivateDnsBroken = in.readBoolean(); |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 1707 | netCap.setAdministratorUids(in.createIntArray()); |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 1708 | netCap.mOwnerUid = in.readInt(); |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1709 | netCap.mRequestorUid = in.readInt(); |
| 1710 | netCap.mRequestorPackageName = in.readString(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1711 | return netCap; |
| 1712 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 1713 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1714 | public NetworkCapabilities[] newArray(int size) { |
| 1715 | return new NetworkCapabilities[size]; |
| 1716 | } |
| 1717 | }; |
| 1718 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 1719 | @Override |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1720 | public @NonNull String toString() { |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1721 | final StringBuilder sb = new StringBuilder("["); |
| 1722 | if (0 != mTransportTypes) { |
| 1723 | sb.append(" Transports: "); |
| 1724 | appendStringRepresentationOfBitMaskToStringBuilder(sb, mTransportTypes, |
| 1725 | NetworkCapabilities::transportNameOf, "|"); |
| 1726 | } |
| 1727 | if (0 != mNetworkCapabilities) { |
| 1728 | sb.append(" Capabilities: "); |
| 1729 | appendStringRepresentationOfBitMaskToStringBuilder(sb, mNetworkCapabilities, |
| 1730 | NetworkCapabilities::capabilityNameOf, "&"); |
| 1731 | } |
jiayanhong | e20a4fe | 2018-11-23 14:23:04 +0800 | [diff] [blame] | 1732 | if (0 != mUnwantedNetworkCapabilities) { |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 1733 | sb.append(" Unwanted: "); |
| 1734 | appendStringRepresentationOfBitMaskToStringBuilder(sb, mUnwantedNetworkCapabilities, |
| 1735 | NetworkCapabilities::capabilityNameOf, "&"); |
| 1736 | } |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1737 | if (mLinkUpBandwidthKbps > 0) { |
| 1738 | sb.append(" LinkUpBandwidth>=").append(mLinkUpBandwidthKbps).append("Kbps"); |
| 1739 | } |
| 1740 | if (mLinkDownBandwidthKbps > 0) { |
| 1741 | sb.append(" LinkDnBandwidth>=").append(mLinkDownBandwidthKbps).append("Kbps"); |
| 1742 | } |
| 1743 | if (mNetworkSpecifier != null) { |
| 1744 | sb.append(" Specifier: <").append(mNetworkSpecifier).append(">"); |
| 1745 | } |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1746 | if (mTransportInfo != null) { |
| 1747 | sb.append(" TransportInfo: <").append(mTransportInfo).append(">"); |
| 1748 | } |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1749 | if (hasSignalStrength()) { |
| 1750 | sb.append(" SignalStrength: ").append(mSignalStrength); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1753 | if (null != mUids) { |
| 1754 | if ((1 == mUids.size()) && (mUids.valueAt(0).count() == 1)) { |
| 1755 | sb.append(" Uid: ").append(mUids.valueAt(0).start); |
| 1756 | } else { |
| 1757 | sb.append(" Uids: <").append(mUids).append(">"); |
| 1758 | } |
| 1759 | } |
Qingxi Li | 7cf0662 | 2020-01-17 17:54:27 -0800 | [diff] [blame] | 1760 | if (mOwnerUid != Process.INVALID_UID) { |
| 1761 | sb.append(" OwnerUid: ").append(mOwnerUid); |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1762 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1763 | |
Cody Kesting | f7ac996 | 2020-03-16 18:15:28 -0700 | [diff] [blame] | 1764 | if (mAdministratorUids.length == 0) { |
| 1765 | sb.append(" AdministratorUids: ").append(Arrays.toString(mAdministratorUids)); |
Cody Kesting | 201fc13 | 2020-01-17 11:58:36 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1768 | if (null != mSSID) { |
| 1769 | sb.append(" SSID: ").append(mSSID); |
| 1770 | } |
| 1771 | |
lucaslin | 783f221 | 2019-10-22 18:27:33 +0800 | [diff] [blame] | 1772 | if (mPrivateDnsBroken) { |
| 1773 | sb.append(" Private DNS is broken"); |
| 1774 | } |
| 1775 | |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1776 | sb.append(" RequestorUid: ").append(mRequestorUid); |
| 1777 | sb.append(" RequestorPackageName: ").append(mRequestorPackageName); |
| 1778 | |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1779 | sb.append("]"); |
| 1780 | return sb.toString(); |
| 1781 | } |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 1782 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 1783 | |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1784 | private interface NameOf { |
| 1785 | String nameOf(int value); |
| 1786 | } |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1787 | |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1788 | /** |
| 1789 | * @hide |
| 1790 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1791 | public static void appendStringRepresentationOfBitMaskToStringBuilder(@NonNull StringBuilder sb, |
| 1792 | long bitMask, @NonNull NameOf nameFetcher, @NonNull String separator) { |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 1793 | int bitPos = 0; |
| 1794 | boolean firstElementAdded = false; |
| 1795 | while (bitMask != 0) { |
| 1796 | if ((bitMask & 1) != 0) { |
| 1797 | if (firstElementAdded) { |
| 1798 | sb.append(separator); |
| 1799 | } else { |
| 1800 | firstElementAdded = true; |
| 1801 | } |
| 1802 | sb.append(nameFetcher.nameOf(bitPos)); |
| 1803 | } |
| 1804 | bitMask >>= 1; |
| 1805 | ++bitPos; |
| 1806 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 1807 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1808 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1809 | /** @hide */ |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 1810 | public void dumpDebug(@NonNull ProtoOutputStream proto, long fieldId) { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1811 | final long token = proto.start(fieldId); |
| 1812 | |
| 1813 | for (int transport : getTransportTypes()) { |
| 1814 | proto.write(NetworkCapabilitiesProto.TRANSPORTS, transport); |
| 1815 | } |
| 1816 | |
| 1817 | for (int capability : getCapabilities()) { |
| 1818 | proto.write(NetworkCapabilitiesProto.CAPABILITIES, capability); |
| 1819 | } |
| 1820 | |
| 1821 | proto.write(NetworkCapabilitiesProto.LINK_UP_BANDWIDTH_KBPS, mLinkUpBandwidthKbps); |
| 1822 | proto.write(NetworkCapabilitiesProto.LINK_DOWN_BANDWIDTH_KBPS, mLinkDownBandwidthKbps); |
| 1823 | |
| 1824 | if (mNetworkSpecifier != null) { |
| 1825 | proto.write(NetworkCapabilitiesProto.NETWORK_SPECIFIER, mNetworkSpecifier.toString()); |
| 1826 | } |
Etan Cohen | ca9fb56 | 2018-11-27 07:32:39 -0800 | [diff] [blame] | 1827 | if (mTransportInfo != null) { |
| 1828 | // TODO b/120653863: write transport-specific info to proto? |
| 1829 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1830 | |
| 1831 | proto.write(NetworkCapabilitiesProto.CAN_REPORT_SIGNAL_STRENGTH, hasSignalStrength()); |
| 1832 | proto.write(NetworkCapabilitiesProto.SIGNAL_STRENGTH, mSignalStrength); |
| 1833 | |
| 1834 | proto.end(token); |
| 1835 | } |
| 1836 | |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1837 | /** |
| 1838 | * @hide |
| 1839 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1840 | public static @NonNull String capabilityNamesOf(@Nullable @NetCapability int[] capabilities) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1841 | StringJoiner joiner = new StringJoiner("|"); |
| 1842 | if (capabilities != null) { |
| 1843 | for (int c : capabilities) { |
| 1844 | joiner.add(capabilityNameOf(c)); |
| 1845 | } |
| 1846 | } |
| 1847 | return joiner.toString(); |
| 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * @hide |
| 1852 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1853 | public static @NonNull String capabilityNameOf(@NetCapability int capability) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1854 | switch (capability) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 1855 | case NET_CAPABILITY_MMS: return "MMS"; |
| 1856 | case NET_CAPABILITY_SUPL: return "SUPL"; |
| 1857 | case NET_CAPABILITY_DUN: return "DUN"; |
| 1858 | case NET_CAPABILITY_FOTA: return "FOTA"; |
| 1859 | case NET_CAPABILITY_IMS: return "IMS"; |
| 1860 | case NET_CAPABILITY_CBS: return "CBS"; |
| 1861 | case NET_CAPABILITY_WIFI_P2P: return "WIFI_P2P"; |
| 1862 | case NET_CAPABILITY_IA: return "IA"; |
| 1863 | case NET_CAPABILITY_RCS: return "RCS"; |
| 1864 | case NET_CAPABILITY_XCAP: return "XCAP"; |
| 1865 | case NET_CAPABILITY_EIMS: return "EIMS"; |
| 1866 | case NET_CAPABILITY_NOT_METERED: return "NOT_METERED"; |
| 1867 | case NET_CAPABILITY_INTERNET: return "INTERNET"; |
| 1868 | case NET_CAPABILITY_NOT_RESTRICTED: return "NOT_RESTRICTED"; |
| 1869 | case NET_CAPABILITY_TRUSTED: return "TRUSTED"; |
| 1870 | case NET_CAPABILITY_NOT_VPN: return "NOT_VPN"; |
| 1871 | case NET_CAPABILITY_VALIDATED: return "VALIDATED"; |
| 1872 | case NET_CAPABILITY_CAPTIVE_PORTAL: return "CAPTIVE_PORTAL"; |
| 1873 | case NET_CAPABILITY_NOT_ROAMING: return "NOT_ROAMING"; |
| 1874 | case NET_CAPABILITY_FOREGROUND: return "FOREGROUND"; |
| 1875 | case NET_CAPABILITY_NOT_CONGESTED: return "NOT_CONGESTED"; |
| 1876 | case NET_CAPABILITY_NOT_SUSPENDED: return "NOT_SUSPENDED"; |
| 1877 | case NET_CAPABILITY_OEM_PAID: return "OEM_PAID"; |
| 1878 | case NET_CAPABILITY_MCX: return "MCX"; |
| 1879 | case NET_CAPABILITY_PARTIAL_CONNECTIVITY: return "PARTIAL_CONNECTIVITY"; |
Jack Yu | 30be5e5 | 2020-04-02 15:34:33 -0700 | [diff] [blame] | 1880 | case NET_CAPABILITY_TEMPORARILY_NOT_METERED: return "TEMPORARILY_NOT_METERED"; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 1881 | default: return Integer.toString(capability); |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | /** |
| 1886 | * @hide |
| 1887 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1888 | @UnsupportedAppUsage |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1889 | public static @NonNull String transportNamesOf(@Nullable @Transport int[] types) { |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1890 | StringJoiner joiner = new StringJoiner("|"); |
| 1891 | if (types != null) { |
| 1892 | for (int t : types) { |
| 1893 | joiner.add(transportNameOf(t)); |
| 1894 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1895 | } |
Hugo Benichi | eae7a22 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 1896 | return joiner.toString(); |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | /** |
| 1900 | * @hide |
| 1901 | */ |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 1902 | public static @NonNull String transportNameOf(@Transport int transport) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 1903 | if (!isValidTransport(transport)) { |
Hugo Benichi | 9910dbc | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 1904 | return "UNKNOWN"; |
| 1905 | } |
| 1906 | return TRANSPORT_NAMES[transport]; |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 1907 | } |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 1908 | |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 1909 | private static void checkValidTransportType(@Transport int transport) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 1910 | Preconditions.checkArgument( |
| 1911 | isValidTransport(transport), "Invalid TransportType " + transport); |
| 1912 | } |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 1913 | |
| 1914 | private static boolean isValidCapability(@NetworkCapabilities.NetCapability int capability) { |
| 1915 | return capability >= MIN_NET_CAPABILITY && capability <= MAX_NET_CAPABILITY; |
| 1916 | } |
| 1917 | |
| 1918 | private static void checkValidCapability(@NetworkCapabilities.NetCapability int capability) { |
| 1919 | Preconditions.checkArgument(isValidCapability(capability), |
| 1920 | "NetworkCapability " + capability + "out of range"); |
| 1921 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1922 | |
| 1923 | /** |
| 1924 | * Check if this {@code NetworkCapability} instance is metered. |
| 1925 | * |
| 1926 | * @return {@code true} if {@code NET_CAPABILITY_NOT_METERED} is not set on this instance. |
| 1927 | * @hide |
| 1928 | */ |
| 1929 | public boolean isMetered() { |
| 1930 | return !hasCapability(NET_CAPABILITY_NOT_METERED); |
| 1931 | } |
lucaslin | 783f221 | 2019-10-22 18:27:33 +0800 | [diff] [blame] | 1932 | |
| 1933 | /** |
| 1934 | * Check if private dns is broken. |
| 1935 | * |
| 1936 | * @return {@code true} if {@code mPrivateDnsBroken} is set when private DNS is broken. |
| 1937 | * @hide |
| 1938 | */ |
| 1939 | public boolean isPrivateDnsBroken() { |
| 1940 | return mPrivateDnsBroken; |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * Set mPrivateDnsBroken to true when private dns is broken. |
| 1945 | * |
| 1946 | * @param broken the status of private DNS to be set. |
| 1947 | * @hide |
| 1948 | */ |
| 1949 | public void setPrivateDnsBroken(boolean broken) { |
| 1950 | mPrivateDnsBroken = broken; |
| 1951 | } |
| 1952 | |
| 1953 | private boolean equalsPrivateDnsBroken(NetworkCapabilities nc) { |
| 1954 | return mPrivateDnsBroken == nc.mPrivateDnsBroken; |
| 1955 | } |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1956 | |
| 1957 | /** |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1958 | * Set the UID of the app making the request. |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1959 | * |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1960 | * For instances of NetworkCapabilities representing a request, sets the |
| 1961 | * UID of the app making the request. For a network created by the system, |
| 1962 | * sets the UID of the only app whose requests can match this network. |
| 1963 | * This can be set to {@link Process#INVALID_UID} if there is no such app, |
| 1964 | * or if this instance of NetworkCapabilities is about to be sent to a |
| 1965 | * party that should not learn about this. |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1966 | * |
| 1967 | * @param uid UID of the app. |
| 1968 | * @hide |
| 1969 | */ |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1970 | public @NonNull NetworkCapabilities setRequestorUid(int uid) { |
| 1971 | mRequestorUid = uid; |
| 1972 | return this; |
| 1973 | } |
| 1974 | |
| 1975 | /** |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1976 | * Returns the UID of the app making the request. |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1977 | * |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1978 | * For a NetworkRequest being made by an app, contains the app's UID. For a network |
| 1979 | * created by the system, contains the UID of the only app whose requests can match |
| 1980 | * this network, or {@link Process#INVALID_UID} if none or if the |
| 1981 | * caller does not have permission to learn about this. |
| 1982 | * |
| 1983 | * @return the uid of the app making the request. |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 1984 | * @hide |
| 1985 | */ |
| 1986 | public int getRequestorUid() { |
| 1987 | return mRequestorUid; |
| 1988 | } |
| 1989 | |
| 1990 | /** |
| 1991 | * Set the package name of the app making the request. |
| 1992 | * |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 1993 | * For instances of NetworkCapabilities representing a request, sets the |
| 1994 | * package name of the app making the request. For a network created by the system, |
| 1995 | * sets the package name of the only app whose requests can match this network. |
| 1996 | * This can be set to null if there is no such app, or if this instance of |
| 1997 | * 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] | 1998 | * |
| 1999 | * @param packageName package name of the app. |
| 2000 | * @hide |
| 2001 | */ |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 2002 | public @NonNull NetworkCapabilities setRequestorPackageName(@NonNull String packageName) { |
| 2003 | mRequestorPackageName = packageName; |
| 2004 | return this; |
| 2005 | } |
| 2006 | |
| 2007 | /** |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 2008 | * Returns the package name of the app making the request. |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 2009 | * |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 2010 | * For a NetworkRequest being made by an app, contains the app's package name. For a |
| 2011 | * network created by the system, contains the package name of the only app whose |
| 2012 | * requests can match this network, or null if none or if the caller does not have |
| 2013 | * permission to learn about this. |
| 2014 | * |
| 2015 | * @return the package name of the app making the request. |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 2016 | * @hide |
| 2017 | */ |
| 2018 | @Nullable |
| 2019 | public String getRequestorPackageName() { |
| 2020 | return mRequestorPackageName; |
| 2021 | } |
| 2022 | |
| 2023 | /** |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 2024 | * 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] | 2025 | * |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 2026 | * {@see #setRequestorUid} and {@link #setRequestorPackageName} |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 2027 | * |
| 2028 | * @param uid UID of the app. |
| 2029 | * @param packageName package name of the app. |
| 2030 | * @hide |
| 2031 | */ |
| 2032 | public @NonNull NetworkCapabilities setRequestorUidAndPackageName( |
| 2033 | int uid, @NonNull String packageName) { |
| 2034 | return setRequestorUid(uid).setRequestorPackageName(packageName); |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * Test whether the passed NetworkCapabilities satisfies the requestor restrictions of this |
| 2039 | * capabilities. |
| 2040 | * |
| 2041 | * This method is called on the NetworkCapabilities embedded in a request with the |
| 2042 | * capabilities of an available network. If the available network, sets a specific |
| 2043 | * requestor (by uid and optionally package name), then this will only match a request from the |
| 2044 | * same app. If either of the capabilities have an unset uid or package name, then it matches |
| 2045 | * everything. |
| 2046 | * <p> |
| 2047 | * nc is assumed nonnull. Else, NPE. |
| 2048 | */ |
| 2049 | private boolean satisfiedByRequestor(NetworkCapabilities nc) { |
| 2050 | // No uid set, matches everything. |
| 2051 | if (mRequestorUid == Process.INVALID_UID || nc.mRequestorUid == Process.INVALID_UID) { |
| 2052 | return true; |
| 2053 | } |
| 2054 | // uids don't match. |
| 2055 | if (mRequestorUid != nc.mRequestorUid) return false; |
| 2056 | // No package names set, matches everything |
| 2057 | if (null == nc.mRequestorPackageName || null == mRequestorPackageName) return true; |
| 2058 | // check for package name match. |
| 2059 | return TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName); |
| 2060 | } |
| 2061 | |
| 2062 | /** |
| 2063 | * Combine requestor info of the capabilities. |
| 2064 | * <p> |
| 2065 | * This is only legal if either the requestor info of this object is reset, or both info are |
| 2066 | * equal. |
| 2067 | * nc is assumed nonnull. |
| 2068 | */ |
| 2069 | private void combineRequestor(@NonNull NetworkCapabilities nc) { |
| 2070 | if (mRequestorUid != Process.INVALID_UID && mRequestorUid != nc.mOwnerUid) { |
| 2071 | throw new IllegalStateException("Can't combine two uids"); |
| 2072 | } |
| 2073 | if (mRequestorPackageName != null |
| 2074 | && !mRequestorPackageName.equals(nc.mRequestorPackageName)) { |
| 2075 | throw new IllegalStateException("Can't combine two package names"); |
| 2076 | } |
| 2077 | setRequestorUid(nc.mRequestorUid); |
| 2078 | setRequestorPackageName(nc.mRequestorPackageName); |
| 2079 | } |
| 2080 | |
| 2081 | private boolean equalsRequestor(NetworkCapabilities nc) { |
| 2082 | return mRequestorUid == nc.mRequestorUid |
| 2083 | && TextUtils.equals(mRequestorPackageName, nc.mRequestorPackageName); |
| 2084 | } |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 2085 | |
| 2086 | /** |
| 2087 | * Builder class for NetworkCapabilities. |
| 2088 | * |
| 2089 | * This class is mainly for for {@link NetworkAgent} instances to use. Many fields in |
| 2090 | * the built class require holding a signature permission to use - mostly |
| 2091 | * {@link android.Manifest.permission.NETWORK_FACTORY}, but refer to the specific |
| 2092 | * description of each setter. As this class lives entirely in app space it does not |
| 2093 | * enforce these restrictions itself but the system server clears out the relevant |
| 2094 | * fields when receiving a NetworkCapabilities object from a caller without the |
| 2095 | * appropriate permission. |
| 2096 | * |
| 2097 | * Apps don't use this builder directly. Instead, they use {@link NetworkRequest} via |
| 2098 | * its builder object. |
| 2099 | * |
| 2100 | * @hide |
| 2101 | */ |
| 2102 | @SystemApi |
| 2103 | @TestApi |
Aaron Huang | fbb485a | 2020-03-25 13:36:38 +0800 | [diff] [blame] | 2104 | public static final class Builder { |
Chalard Jean | e5e3850 | 2020-03-18 15:58:50 +0900 | [diff] [blame] | 2105 | private final NetworkCapabilities mCaps; |
| 2106 | |
| 2107 | /** |
| 2108 | * Creates a new Builder to construct NetworkCapabilities objects. |
| 2109 | */ |
| 2110 | public Builder() { |
| 2111 | mCaps = new NetworkCapabilities(); |
| 2112 | } |
| 2113 | |
| 2114 | /** |
| 2115 | * Creates a new Builder of NetworkCapabilities from an existing instance. |
| 2116 | */ |
| 2117 | public Builder(@NonNull final NetworkCapabilities nc) { |
| 2118 | Objects.requireNonNull(nc); |
| 2119 | mCaps = new NetworkCapabilities(nc); |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * Adds the given transport type. |
| 2124 | * |
| 2125 | * Multiple transports may be added. Note that when searching for a network to satisfy a |
| 2126 | * request, satisfying any of the transports listed in the request will satisfy the request. |
| 2127 | * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a |
| 2128 | * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network |
| 2129 | * to be selected. This is logically different than |
| 2130 | * {@code NetworkCapabilities.NET_CAPABILITY_*}. |
| 2131 | * |
| 2132 | * @param transportType the transport type to be added or removed. |
| 2133 | * @return this builder |
| 2134 | */ |
| 2135 | @NonNull |
| 2136 | public Builder addTransportType(@Transport int transportType) { |
| 2137 | checkValidTransportType(transportType); |
| 2138 | mCaps.addTransportType(transportType); |
| 2139 | return this; |
| 2140 | } |
| 2141 | |
| 2142 | /** |
| 2143 | * Removes the given transport type. |
| 2144 | * |
| 2145 | * {@see #addTransportType}. |
| 2146 | * |
| 2147 | * @param transportType the transport type to be added or removed. |
| 2148 | * @return this builder |
| 2149 | */ |
| 2150 | @NonNull |
| 2151 | public Builder removeTransportType(@Transport int transportType) { |
| 2152 | checkValidTransportType(transportType); |
| 2153 | mCaps.removeTransportType(transportType); |
| 2154 | return this; |
| 2155 | } |
| 2156 | |
| 2157 | /** |
| 2158 | * Adds the given capability. |
| 2159 | * |
| 2160 | * @param capability the capability |
| 2161 | * @return this builder |
| 2162 | */ |
| 2163 | @NonNull |
| 2164 | public Builder addCapability(@NetCapability final int capability) { |
| 2165 | mCaps.setCapability(capability, true); |
| 2166 | return this; |
| 2167 | } |
| 2168 | |
| 2169 | /** |
| 2170 | * Removes the given capability. |
| 2171 | * |
| 2172 | * @param capability the capability |
| 2173 | * @return this builder |
| 2174 | */ |
| 2175 | @NonNull |
| 2176 | public Builder removeCapability(@NetCapability final int capability) { |
| 2177 | mCaps.setCapability(capability, false); |
| 2178 | return this; |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * Sets the owner UID. |
| 2183 | * |
| 2184 | * The default value is {@link Process#INVALID_UID}. Pass this value to reset. |
| 2185 | * |
| 2186 | * Note: for security the system will clear out this field when received from a |
| 2187 | * non-privileged source. |
| 2188 | * |
| 2189 | * @param ownerUid the owner UID |
| 2190 | * @return this builder |
| 2191 | */ |
| 2192 | @NonNull |
| 2193 | @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) |
| 2194 | public Builder setOwnerUid(final int ownerUid) { |
| 2195 | mCaps.setOwnerUid(ownerUid); |
| 2196 | return this; |
| 2197 | } |
| 2198 | |
| 2199 | /** |
| 2200 | * Sets the list of UIDs that are administrators of this network. |
| 2201 | * |
| 2202 | * <p>UIDs included in administratorUids gain administrator privileges over this |
| 2203 | * Network. Examples of UIDs that should be included in administratorUids are: |
| 2204 | * <ul> |
| 2205 | * <li>Carrier apps with privileges for the relevant subscription |
| 2206 | * <li>Active VPN apps |
| 2207 | * <li>Other application groups with a particular Network-related role |
| 2208 | * </ul> |
| 2209 | * |
| 2210 | * <p>In general, user-supplied networks (such as WiFi networks) do not have |
| 2211 | * administrators. |
| 2212 | * |
| 2213 | * <p>An app is granted owner privileges over Networks that it supplies. The owner |
| 2214 | * UID MUST always be included in administratorUids. |
| 2215 | * |
| 2216 | * The default value is the empty array. Pass an empty array to reset. |
| 2217 | * |
| 2218 | * Note: for security the system will clear out this field when received from a |
| 2219 | * non-privileged source, such as an app using reflection to call this or |
| 2220 | * mutate the member in the built object. |
| 2221 | * |
| 2222 | * @param administratorUids the UIDs to be set as administrators of this Network. |
| 2223 | * @return this builder |
| 2224 | */ |
| 2225 | @NonNull |
| 2226 | @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) |
| 2227 | public Builder setAdministratorUids(@NonNull final int[] administratorUids) { |
| 2228 | Objects.requireNonNull(administratorUids); |
| 2229 | mCaps.setAdministratorUids(administratorUids); |
| 2230 | return this; |
| 2231 | } |
| 2232 | |
| 2233 | /** |
| 2234 | * Sets the upstream bandwidth of the link. |
| 2235 | * |
| 2236 | * Sets the upstream bandwidth for this network in Kbps. This always only refers to |
| 2237 | * the estimated first hop transport bandwidth. |
| 2238 | * <p> |
| 2239 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 2240 | * When received as the state of an existing network this specifies the typical |
| 2241 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 2242 | * from technology type and other link parameters. It could be used to differentiate |
| 2243 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 2244 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 2245 | * fast backhauls and slow backhauls. |
| 2246 | * |
| 2247 | * @param upKbps the estimated first hop upstream (device to network) bandwidth. |
| 2248 | * @return this builder |
| 2249 | */ |
| 2250 | @NonNull |
| 2251 | public Builder setLinkUpstreamBandwidthKbps(final int upKbps) { |
| 2252 | mCaps.setLinkUpstreamBandwidthKbps(upKbps); |
| 2253 | return this; |
| 2254 | } |
| 2255 | |
| 2256 | /** |
| 2257 | * Sets the downstream bandwidth for this network in Kbps. This always only refers to |
| 2258 | * the estimated first hop transport bandwidth. |
| 2259 | * <p> |
| 2260 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 2261 | * When received as the state of an existing network this specifies the typical |
| 2262 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 2263 | * from technology type and other link parameters. It could be used to differentiate |
| 2264 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 2265 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 2266 | * fast backhauls and slow backhauls. |
| 2267 | * |
| 2268 | * @param downKbps the estimated first hop downstream (network to device) bandwidth. |
| 2269 | * @return this builder |
| 2270 | */ |
| 2271 | @NonNull |
| 2272 | public Builder setLinkDownstreamBandwidthKbps(final int downKbps) { |
| 2273 | mCaps.setLinkDownstreamBandwidthKbps(downKbps); |
| 2274 | return this; |
| 2275 | } |
| 2276 | |
| 2277 | /** |
| 2278 | * Sets the optional bearer specific network specifier. |
| 2279 | * This has no meaning if a single transport is also not specified, so calling |
| 2280 | * this without a single transport set will generate an exception, as will |
| 2281 | * subsequently adding or removing transports after this is set. |
| 2282 | * </p> |
| 2283 | * |
| 2284 | * @param specifier a concrete, parcelable framework class that extends NetworkSpecifier, |
| 2285 | * or null to clear it. |
| 2286 | * @return this builder |
| 2287 | */ |
| 2288 | @NonNull |
| 2289 | public Builder setNetworkSpecifier(@Nullable final NetworkSpecifier specifier) { |
| 2290 | mCaps.setNetworkSpecifier(specifier); |
| 2291 | return this; |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * Sets the optional transport specific information. |
| 2296 | * |
| 2297 | * @param info A concrete, parcelable framework class that extends {@link TransportInfo}, |
| 2298 | * or null to clear it. |
| 2299 | * @return this builder |
| 2300 | */ |
| 2301 | @NonNull |
| 2302 | public Builder setTransportInfo(@Nullable final TransportInfo info) { |
| 2303 | mCaps.setTransportInfo(info); |
| 2304 | return this; |
| 2305 | } |
| 2306 | |
| 2307 | /** |
| 2308 | * Sets the signal strength. This is a signed integer, with higher values indicating a |
| 2309 | * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the |
| 2310 | * same RSSI units reported by wifi code. |
| 2311 | * <p> |
| 2312 | * Note that when used to register a network callback, this specifies the minimum |
| 2313 | * acceptable signal strength. When received as the state of an existing network it |
| 2314 | * specifies the current value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means |
| 2315 | * no value when received and has no effect when requesting a callback. |
| 2316 | * |
| 2317 | * Note: for security the system will throw if it receives a NetworkRequest where |
| 2318 | * the underlying NetworkCapabilities has this member set from a source that does |
| 2319 | * not hold the {@link android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP} |
| 2320 | * permission. Apps with this permission can use this indirectly through |
| 2321 | * {@link android.net.NetworkRequest}. |
| 2322 | * |
| 2323 | * @param signalStrength the bearer-specific signal strength. |
| 2324 | * @return this builder |
| 2325 | */ |
| 2326 | @NonNull |
| 2327 | @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP) |
| 2328 | public Builder setSignalStrength(final int signalStrength) { |
| 2329 | mCaps.setSignalStrength(signalStrength); |
| 2330 | return this; |
| 2331 | } |
| 2332 | |
| 2333 | /** |
| 2334 | * Sets the SSID of this network. |
| 2335 | * |
| 2336 | * Note: for security the system will clear out this field when received from a |
| 2337 | * non-privileged source, like an app using reflection to set this. |
| 2338 | * |
| 2339 | * @param ssid the SSID, or null to clear it. |
| 2340 | * @return this builder |
| 2341 | */ |
| 2342 | @NonNull |
| 2343 | @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) |
| 2344 | public Builder setSsid(@Nullable final String ssid) { |
| 2345 | mCaps.setSSID(ssid); |
| 2346 | return this; |
| 2347 | } |
| 2348 | |
| 2349 | /** |
| 2350 | * Set the uid of the app causing this network to exist. |
| 2351 | * |
| 2352 | * Note: for security the system will clear out this field when received from a |
| 2353 | * non-privileged source. |
| 2354 | * |
| 2355 | * @param uid UID of the app. |
| 2356 | * @return this builder |
| 2357 | */ |
| 2358 | @NonNull |
| 2359 | @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) |
| 2360 | public Builder setRequestorUid(final int uid) { |
| 2361 | mCaps.setRequestorUid(uid); |
| 2362 | return this; |
| 2363 | } |
| 2364 | |
| 2365 | /** |
| 2366 | * Set the package name of the app causing this network to exist. |
| 2367 | * |
| 2368 | * Note: for security the system will clear out this field when received from a |
| 2369 | * non-privileged source. |
| 2370 | * |
| 2371 | * @param packageName package name of the app, or null to clear it. |
| 2372 | * @return this builder |
| 2373 | */ |
| 2374 | @NonNull |
| 2375 | @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) |
| 2376 | public Builder setRequestorPackageName(@Nullable final String packageName) { |
| 2377 | mCaps.setRequestorPackageName(packageName); |
| 2378 | return this; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * Builds the instance of the capabilities. |
| 2383 | * |
| 2384 | * @return the built instance of NetworkCapabilities. |
| 2385 | */ |
| 2386 | @NonNull |
| 2387 | public NetworkCapabilities build() { |
| 2388 | if (mCaps.getOwnerUid() != Process.INVALID_UID) { |
| 2389 | if (!ArrayUtils.contains(mCaps.getAdministratorUids(), mCaps.getOwnerUid())) { |
| 2390 | throw new IllegalStateException("The owner UID must be included in " |
| 2391 | + " administrator UIDs."); |
| 2392 | } |
| 2393 | } |
| 2394 | return new NetworkCapabilities(mCaps); |
| 2395 | } |
| 2396 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 2397 | } |