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