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