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 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 21 | import android.util.Log; |
| 22 | |
| 23 | import java.util.Objects; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 26 | * This class represents the capabilities of a network. This is used both to specify |
| 27 | * needs to {@link ConnectivityManager} and when inspecting a network. |
| 28 | * |
| 29 | * Note that this replaces the old {@link ConnectivityManager#TYPE_MOBILE} method |
| 30 | * of network selection. Rather than indicate a need for Wi-Fi because an application |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 31 | * needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE), |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 32 | * the application should specify it needs high bandwidth. Similarly if an application |
| 33 | * needs an unmetered network for a bulk transfer it can specify that rather than assuming |
| 34 | * all cellular based connections are metered and all Wi-Fi based connections are not. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 35 | */ |
| 36 | public final class NetworkCapabilities implements Parcelable { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 37 | private static final String TAG = "NetworkCapabilities"; |
| 38 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @hide |
| 41 | */ |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 42 | public NetworkCapabilities() { |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 43 | clearAll(); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 44 | mNetworkCapabilities = DEFAULT_CAPABILITIES; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public NetworkCapabilities(NetworkCapabilities nc) { |
| 48 | if (nc != null) { |
| 49 | mNetworkCapabilities = nc.mNetworkCapabilities; |
| 50 | mTransportTypes = nc.mTransportTypes; |
| 51 | mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps; |
| 52 | mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps; |
Robert Greenwalt | 94badcc | 2014-07-10 14:53:24 -0700 | [diff] [blame] | 53 | mNetworkSpecifier = nc.mNetworkSpecifier; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 54 | mSignalStrength = nc.mSignalStrength; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 59 | * Completely clears the contents of this object, removing even the capabilities that are set |
| 60 | * by default when the object is constructed. |
| 61 | * @hide |
| 62 | */ |
| 63 | public void clearAll() { |
| 64 | mNetworkCapabilities = mTransportTypes = 0; |
| 65 | mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0; |
| 66 | mNetworkSpecifier = null; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 67 | mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED; |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 71 | * Represents the network's capabilities. If any are specified they will be satisfied |
| 72 | * by any Network that matches all of them. |
| 73 | */ |
Lorenzo Colitti | f7058f5 | 2015-04-27 11:31:55 +0900 | [diff] [blame] | 74 | private long mNetworkCapabilities; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 77 | * Indicates this is a network that has the ability to reach the |
| 78 | * carrier's MMSC for sending and receiving MMS messages. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 79 | */ |
| 80 | public static final int NET_CAPABILITY_MMS = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Indicates this is a network that has the ability to reach the carrier's |
| 84 | * SUPL server, used to retrieve GPS information. |
| 85 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 86 | public static final int NET_CAPABILITY_SUPL = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Indicates this is a network that has the ability to reach the carrier's |
| 90 | * DUN or tethering gateway. |
| 91 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 92 | public static final int NET_CAPABILITY_DUN = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Indicates this is a network that has the ability to reach the carrier's |
| 96 | * FOTA portal, used for over the air updates. |
| 97 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 98 | public static final int NET_CAPABILITY_FOTA = 3; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Indicates this is a network that has the ability to reach the carrier's |
| 102 | * IMS servers, used for network registration and signaling. |
| 103 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 104 | public static final int NET_CAPABILITY_IMS = 4; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Indicates this is a network that has the ability to reach the carrier's |
| 108 | * CBS servers, used for carrier specific services. |
| 109 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 110 | public static final int NET_CAPABILITY_CBS = 5; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Indicates this is a network that has the ability to reach a Wi-Fi direct |
| 114 | * peer. |
| 115 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 116 | public static final int NET_CAPABILITY_WIFI_P2P = 6; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Indicates this is a network that has the ability to reach a carrier's |
| 120 | * Initial Attach servers. |
| 121 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 122 | public static final int NET_CAPABILITY_IA = 7; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Indicates this is a network that has the ability to reach a carrier's |
| 126 | * RCS servers, used for Rich Communication Services. |
| 127 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 128 | public static final int NET_CAPABILITY_RCS = 8; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Indicates this is a network that has the ability to reach a carrier's |
| 132 | * XCAP servers, used for configuration and control. |
| 133 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 134 | public static final int NET_CAPABILITY_XCAP = 9; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * 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] | 138 | * Emergency IMS servers or other services, used for network signaling |
| 139 | * during emergency calls. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 140 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 141 | public static final int NET_CAPABILITY_EIMS = 10; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Indicates that this network is unmetered. |
| 145 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 146 | public static final int NET_CAPABILITY_NOT_METERED = 11; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Indicates that this network should be able to reach the internet. |
| 150 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 151 | public static final int NET_CAPABILITY_INTERNET = 12; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Indicates that this network is available for general use. If this is not set |
| 155 | * applications should not attempt to communicate on this network. Note that this |
| 156 | * is simply informative and not enforcement - enforcement is handled via other means. |
| 157 | * Set by default. |
| 158 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 159 | public static final int NET_CAPABILITY_NOT_RESTRICTED = 13; |
| 160 | |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Indicates that the user has indicated implicit trust of this network. This |
| 163 | * generally means it's a sim-selected carrier, a plugged in ethernet, a paired |
| 164 | * BT device or a wifi the user asked to connect to. Untrusted networks |
| 165 | * are probably limited to unknown wifi AP. Set by default. |
| 166 | */ |
| 167 | public static final int NET_CAPABILITY_TRUSTED = 14; |
| 168 | |
Paul Jensen | 76b610a | 2015-03-18 09:33:07 -0400 | [diff] [blame] | 169 | /** |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 170 | * 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] | 171 | * explicitly cleared for VPN networks. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 172 | */ |
| 173 | public static final int NET_CAPABILITY_NOT_VPN = 15; |
| 174 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 175 | /** |
| 176 | * Indicates that connectivity on this network was successfully validated. For example, for a |
| 177 | * network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully |
| 178 | * detected. |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 179 | */ |
| 180 | public static final int NET_CAPABILITY_VALIDATED = 16; |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 181 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 182 | /** |
| 183 | * Indicates that this network was found to have a captive portal in place last time it was |
| 184 | * probed. |
| 185 | */ |
| 186 | public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17; |
| 187 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 188 | /** |
| 189 | * Indicates that this network is available for use by apps, and not a network that is being |
| 190 | * kept up in the background to facilitate fast network switching. |
| 191 | * @hide |
| 192 | */ |
| 193 | public static final int NET_CAPABILITY_FOREGROUND = 18; |
| 194 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 195 | private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 196 | private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 197 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 198 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 199 | * Network capabilities that are expected to be mutable, i.e., can change while a particular |
| 200 | * network is connected. |
| 201 | */ |
| 202 | private static final long MUTABLE_CAPABILITIES = |
| 203 | // TRUSTED can change when user explicitly connects to an untrusted network in Settings. |
| 204 | // http://b/18206275 |
| 205 | (1 << NET_CAPABILITY_TRUSTED) | |
| 206 | (1 << NET_CAPABILITY_VALIDATED) | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 207 | (1 << NET_CAPABILITY_CAPTIVE_PORTAL) | |
| 208 | (1 << NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Network capabilities that are not allowed in NetworkRequests. This exists because the |
| 212 | * NetworkFactory / NetworkAgent model does not deal well with the situation where a |
| 213 | * capability's presence cannot be known in advance. If such a capability is requested, then we |
| 214 | * can get into a cycle where the NetworkFactory endlessly churns out NetworkAgents that then |
| 215 | * get immediately torn down because they do not have the requested capability. |
| 216 | */ |
| 217 | private static final long NON_REQUESTABLE_CAPABILITIES = |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 218 | MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * Capabilities that are set by default when the object is constructed. |
| 222 | */ |
| 223 | private static final long DEFAULT_CAPABILITIES = |
| 224 | (1 << NET_CAPABILITY_NOT_RESTRICTED) | |
| 225 | (1 << NET_CAPABILITY_TRUSTED) | |
| 226 | (1 << NET_CAPABILITY_NOT_VPN); |
| 227 | |
| 228 | /** |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 229 | * Capabilities that suggest that a network is restricted. |
| 230 | * {@see #maybeMarkCapabilitiesRestricted}. |
| 231 | */ |
| 232 | private static final long RESTRICTED_CAPABILITIES = |
| 233 | (1 << NET_CAPABILITY_CBS) | |
| 234 | (1 << NET_CAPABILITY_DUN) | |
| 235 | (1 << NET_CAPABILITY_EIMS) | |
| 236 | (1 << NET_CAPABILITY_FOTA) | |
| 237 | (1 << NET_CAPABILITY_IA) | |
| 238 | (1 << NET_CAPABILITY_IMS) | |
| 239 | (1 << NET_CAPABILITY_RCS) | |
| 240 | (1 << NET_CAPABILITY_XCAP); |
| 241 | |
| 242 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 243 | * Adds the given capability to this {@code NetworkCapability} instance. |
| 244 | * Multiple capabilities may be applied sequentially. Note that when searching |
| 245 | * for a network to satisfy a request, all capabilities requested must be satisfied. |
| 246 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 247 | * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 248 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 249 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 250 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 251 | public NetworkCapabilities addCapability(int capability) { |
| 252 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 253 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 254 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 255 | mNetworkCapabilities |= 1 << capability; |
| 256 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 257 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Removes (if found) the given capability from this {@code NetworkCapability} instance. |
| 261 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 262 | * @param capability the {@code NetworkCapabilities.NET_CAPABILTIY_*} to be removed. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 263 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 264 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 265 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 266 | public NetworkCapabilities removeCapability(int capability) { |
| 267 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 268 | throw new IllegalArgumentException("NetworkCapability out of range"); |
| 269 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 270 | mNetworkCapabilities &= ~(1 << capability); |
| 271 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 272 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Gets all the capabilities set on this {@code NetworkCapability} instance. |
| 276 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 277 | * @return an array of {@code NetworkCapabilities.NET_CAPABILITY_*} values |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 278 | * for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 279 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 280 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 281 | public int[] getCapabilities() { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 282 | return enumerateBits(mNetworkCapabilities); |
| 283 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * Tests for the presence of a capabilitity on this instance. |
| 287 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 288 | * @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for. |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 289 | * @return {@code true} if set on this instance. |
| 290 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 291 | public boolean hasCapability(int capability) { |
| 292 | if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) { |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 295 | return ((mNetworkCapabilities & (1 << capability)) != 0); |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 296 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 297 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 298 | private int[] enumerateBits(long val) { |
| 299 | int size = Long.bitCount(val); |
| 300 | int[] result = new int[size]; |
| 301 | int index = 0; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 302 | int resource = 0; |
| 303 | while (val > 0) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 304 | if ((val & 1) == 1) result[index++] = resource; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 305 | val = val >> 1; |
| 306 | resource++; |
| 307 | } |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | private void combineNetCapabilities(NetworkCapabilities nc) { |
| 312 | this.mNetworkCapabilities |= nc.mNetworkCapabilities; |
| 313 | } |
| 314 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 315 | /** |
| 316 | * Convenience function that returns a human-readable description of the first mutable |
| 317 | * capability we find. Used to present an error message to apps that request mutable |
| 318 | * capabilities. |
| 319 | * |
| 320 | * @hide |
| 321 | */ |
| 322 | public String describeFirstNonRequestableCapability() { |
| 323 | if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED"; |
| 324 | if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL"; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 325 | if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND"; |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 326 | // This cannot happen unless the preceding checks are incomplete. |
| 327 | if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) { |
| 328 | return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities); |
| 329 | } |
| 330 | if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth"; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 331 | if (hasSignalStrength()) return "signalStrength"; |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 332 | return null; |
| 333 | } |
| 334 | |
| 335 | private boolean satisfiedByNetCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
| 336 | long networkCapabilities = this.mNetworkCapabilities; |
| 337 | if (onlyImmutable) { |
| 338 | networkCapabilities = networkCapabilities & ~MUTABLE_CAPABILITIES; |
| 339 | } |
| 340 | return ((nc.mNetworkCapabilities & networkCapabilities) == networkCapabilities); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 343 | /** @hide */ |
| 344 | public boolean equalsNetCapabilities(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 345 | return (nc.mNetworkCapabilities == this.mNetworkCapabilities); |
| 346 | } |
| 347 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 348 | private boolean equalsNetCapabilitiesImmutable(NetworkCapabilities that) { |
| 349 | return ((this.mNetworkCapabilities & ~MUTABLE_CAPABILITIES) == |
| 350 | (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES)); |
| 351 | } |
| 352 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 353 | private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) { |
| 354 | return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) == |
| 355 | (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES)); |
| 356 | } |
| 357 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 358 | /** |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 359 | * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are |
| 360 | * typically provided by restricted networks. |
| 361 | * |
| 362 | * TODO: consider: |
| 363 | * - Renaming it to guessRestrictedCapability and make it set the |
| 364 | * restricted capability bit in addition to clearing it. |
| 365 | * @hide |
| 366 | */ |
| 367 | public void maybeMarkCapabilitiesRestricted() { |
| 368 | // If all the capabilities are typically provided by restricted networks, conclude that this |
| 369 | // network is restricted. |
Paul Jensen | aae613d | 2015-08-19 11:06:15 -0400 | [diff] [blame] | 370 | if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0 && |
| 371 | // Must have at least some restricted capabilities, otherwise a request for an |
| 372 | // internet-less network will get marked restricted. |
| 373 | (mNetworkCapabilities & RESTRICTED_CAPABILITIES) != 0) { |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 374 | removeCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Paul Jensen | aae613d | 2015-08-19 11:06:15 -0400 | [diff] [blame] | 375 | } |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 379 | * Representing the transport type. Apps should generally not care about transport. A |
| 380 | * request for a fast internet connection could be satisfied by a number of different |
| 381 | * transports. If any are specified here it will be satisfied a Network that matches |
| 382 | * any of them. If a caller doesn't care about the transport it should not specify any. |
| 383 | */ |
| 384 | private long mTransportTypes; |
| 385 | |
| 386 | /** |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 387 | * Indicates this network uses a Cellular transport. |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 388 | */ |
| 389 | public static final int TRANSPORT_CELLULAR = 0; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Indicates this network uses a Wi-Fi transport. |
| 393 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 394 | public static final int TRANSPORT_WIFI = 1; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 395 | |
| 396 | /** |
| 397 | * Indicates this network uses a Bluetooth transport. |
| 398 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 399 | public static final int TRANSPORT_BLUETOOTH = 2; |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Indicates this network uses an Ethernet transport. |
| 403 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 404 | public static final int TRANSPORT_ETHERNET = 3; |
| 405 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 406 | /** |
| 407 | * Indicates this network uses a VPN transport. |
| 408 | */ |
| 409 | public static final int TRANSPORT_VPN = 4; |
| 410 | |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 411 | /** |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 412 | * Indicates this network uses a Wi-Fi Aware transport. |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 413 | */ |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 414 | public static final int TRANSPORT_WIFI_AWARE = 5; |
Etan Cohen | 305ea28 | 2016-06-20 09:27:12 -0700 | [diff] [blame] | 415 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 416 | private static final int MIN_TRANSPORT = TRANSPORT_CELLULAR; |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 417 | private static final int MAX_TRANSPORT = TRANSPORT_WIFI_AWARE; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 418 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Adds the given transport type to this {@code NetworkCapability} instance. |
| 421 | * Multiple transports may be applied sequentially. Note that when searching |
| 422 | * for a network to satisfy a request, any listed in the request will satisfy the request. |
| 423 | * For example {@code TRANSPORT_WIFI} and {@code TRANSPORT_ETHERNET} added to a |
| 424 | * {@code NetworkCapabilities} would cause either a Wi-Fi network or an Ethernet network |
| 425 | * to be selected. This is logically different than |
| 426 | * {@code NetworkCapabilities.NET_CAPABILITY_*} listed above. |
| 427 | * |
| 428 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be added. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 429 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 430 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 431 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 432 | public NetworkCapabilities addTransportType(int transportType) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 433 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 434 | throw new IllegalArgumentException("TransportType out of range"); |
| 435 | } |
| 436 | mTransportTypes |= 1 << transportType; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 437 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 438 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 439 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * Removes (if found) the given transport from this {@code NetworkCapability} instance. |
| 443 | * |
| 444 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be removed. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 445 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 446 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 447 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 448 | public NetworkCapabilities removeTransportType(int transportType) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 449 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 450 | throw new IllegalArgumentException("TransportType out of range"); |
| 451 | } |
| 452 | mTransportTypes &= ~(1 << transportType); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 453 | setNetworkSpecifier(mNetworkSpecifier); // used for exception checking |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 454 | return this; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 455 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * Gets all the transports set on this {@code NetworkCapability} instance. |
| 459 | * |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 460 | * @return an array of {@code NetworkCapabilities.TRANSPORT_*} values |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 461 | * for this instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 462 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 463 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 464 | public int[] getTransportTypes() { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 465 | return enumerateBits(mTransportTypes); |
| 466 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * Tests for the presence of a transport on this instance. |
| 470 | * |
| 471 | * @param transportType the {@code NetworkCapabilities.TRANSPORT_*} to be tested for. |
| 472 | * @return {@code true} if set on this instance. |
| 473 | */ |
Robert Greenwalt | 5c55e33 | 2014-05-08 00:02:04 -0700 | [diff] [blame] | 474 | public boolean hasTransport(int transportType) { |
| 475 | if (transportType < MIN_TRANSPORT || transportType > MAX_TRANSPORT) { |
| 476 | return false; |
| 477 | } |
| 478 | return ((mTransportTypes & (1 << transportType)) != 0); |
| 479 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 480 | |
| 481 | private void combineTransportTypes(NetworkCapabilities nc) { |
| 482 | this.mTransportTypes |= nc.mTransportTypes; |
| 483 | } |
| 484 | private boolean satisfiedByTransportTypes(NetworkCapabilities nc) { |
| 485 | return ((this.mTransportTypes == 0) || |
| 486 | ((this.mTransportTypes & nc.mTransportTypes) != 0)); |
| 487 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 488 | /** @hide */ |
| 489 | public boolean equalsTransportTypes(NetworkCapabilities nc) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 490 | return (nc.mTransportTypes == this.mTransportTypes); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Passive link bandwidth. This is a rough guide of the expected peak bandwidth |
| 495 | * for the first hop on the given transport. It is not measured, but may take into account |
| 496 | * link parameters (Radio technology, allocated channels, etc). |
| 497 | */ |
| 498 | private int mLinkUpBandwidthKbps; |
| 499 | private int mLinkDownBandwidthKbps; |
| 500 | |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 501 | /** |
| 502 | * Sets the upstream bandwidth for this network in Kbps. This always only refers to |
| 503 | * the estimated first hop transport bandwidth. |
| 504 | * <p> |
| 505 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 506 | * When received as the state of an existing network this specifies the typical |
| 507 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 508 | * from technology type and other link parameters. It could be used to differentiate |
| 509 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 510 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 511 | * fast backhauls and slow backhauls. |
| 512 | * |
| 513 | * @param upKbps the estimated first hop upstream (device to network) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 514 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 515 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 516 | public void setLinkUpstreamBandwidthKbps(int upKbps) { |
| 517 | mLinkUpBandwidthKbps = upKbps; |
| 518 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * Retrieves the upstream bandwidth for this network in Kbps. This always only refers to |
| 522 | * the estimated first hop transport bandwidth. |
| 523 | * |
| 524 | * @return The estimated first hop upstream (device to network) bandwidth. |
| 525 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 526 | public int getLinkUpstreamBandwidthKbps() { |
| 527 | return mLinkUpBandwidthKbps; |
| 528 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * Sets the downstream bandwidth for this network in Kbps. This always only refers to |
| 532 | * the estimated first hop transport bandwidth. |
| 533 | * <p> |
| 534 | * Note that when used to request a network, this specifies the minimum acceptable. |
| 535 | * When received as the state of an existing network this specifies the typical |
| 536 | * first hop bandwidth expected. This is never measured, but rather is inferred |
| 537 | * from technology type and other link parameters. It could be used to differentiate |
| 538 | * between very slow 1xRTT cellular links and other faster networks or even between |
| 539 | * 802.11b vs 802.11AC wifi technologies. It should not be used to differentiate between |
| 540 | * fast backhauls and slow backhauls. |
| 541 | * |
| 542 | * @param downKbps the estimated first hop downstream (network to device) bandwidth. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 543 | * @hide |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 544 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 545 | public void setLinkDownstreamBandwidthKbps(int downKbps) { |
| 546 | mLinkDownBandwidthKbps = downKbps; |
| 547 | } |
Robert Greenwalt | 01d004e | 2014-05-18 15:24:21 -0700 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * Retrieves the downstream bandwidth for this network in Kbps. This always only refers to |
| 551 | * the estimated first hop transport bandwidth. |
| 552 | * |
| 553 | * @return The estimated first hop downstream (network to device) bandwidth. |
| 554 | */ |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 555 | public int getLinkDownstreamBandwidthKbps() { |
| 556 | return mLinkDownBandwidthKbps; |
| 557 | } |
| 558 | |
| 559 | private void combineLinkBandwidths(NetworkCapabilities nc) { |
| 560 | this.mLinkUpBandwidthKbps = |
| 561 | Math.max(this.mLinkUpBandwidthKbps, nc.mLinkUpBandwidthKbps); |
| 562 | this.mLinkDownBandwidthKbps = |
| 563 | Math.max(this.mLinkDownBandwidthKbps, nc.mLinkDownBandwidthKbps); |
| 564 | } |
| 565 | private boolean satisfiedByLinkBandwidths(NetworkCapabilities nc) { |
| 566 | return !(this.mLinkUpBandwidthKbps > nc.mLinkUpBandwidthKbps || |
| 567 | this.mLinkDownBandwidthKbps > nc.mLinkDownBandwidthKbps); |
| 568 | } |
| 569 | private boolean equalsLinkBandwidths(NetworkCapabilities nc) { |
| 570 | return (this.mLinkUpBandwidthKbps == nc.mLinkUpBandwidthKbps && |
| 571 | this.mLinkDownBandwidthKbps == nc.mLinkDownBandwidthKbps); |
| 572 | } |
| 573 | |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 574 | private NetworkSpecifier mNetworkSpecifier = null; |
| 575 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 576 | /** |
| 577 | * Sets the optional bearer specific network specifier. |
| 578 | * This has no meaning if a single transport is also not specified, so calling |
| 579 | * this without a single transport set will generate an exception, as will |
| 580 | * subsequently adding or removing transports after this is set. |
| 581 | * </p> |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 582 | * |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 583 | * @param networkSpecifier A concrete, parcelable framework class that extends |
| 584 | * NetworkSpecifier. |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 585 | * @return This NetworkCapabilities instance, to facilitate chaining. |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 586 | * @hide |
| 587 | */ |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 588 | public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) { |
| 589 | if (networkSpecifier != null && Long.bitCount(mTransportTypes) != 1) { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 590 | throw new IllegalStateException("Must have a single transport specified to use " + |
| 591 | "setNetworkSpecifier"); |
| 592 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 593 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 594 | mNetworkSpecifier = networkSpecifier; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 595 | |
Pierre Imai | c8419a8 | 2016-03-22 17:54:54 +0900 | [diff] [blame] | 596 | return this; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Gets the optional bearer specific network specifier. |
| 601 | * |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 602 | * @return The optional {@link NetworkSpecifier} specifying the bearer specific network |
| 603 | * specifier. See {@link #setNetworkSpecifier}. |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 604 | * @hide |
| 605 | */ |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 606 | public NetworkSpecifier getNetworkSpecifier() { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 607 | return mNetworkSpecifier; |
| 608 | } |
| 609 | |
| 610 | private void combineSpecifiers(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 611 | if (mNetworkSpecifier != null && !mNetworkSpecifier.equals(nc.mNetworkSpecifier)) { |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 612 | throw new IllegalStateException("Can't combine two networkSpecifiers"); |
| 613 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 614 | setNetworkSpecifier(nc.mNetworkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 615 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 616 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 617 | private boolean satisfiedBySpecifier(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 618 | return mNetworkSpecifier == null || mNetworkSpecifier.satisfiedBy(nc.mNetworkSpecifier) |
| 619 | || nc.mNetworkSpecifier instanceof MatchAllNetworkSpecifier; |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 620 | } |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 621 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 622 | private boolean equalsSpecifier(NetworkCapabilities nc) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 623 | return Objects.equals(mNetworkSpecifier, nc.mNetworkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 626 | /** |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 627 | * Magic value that indicates no signal strength provided. A request specifying this value is |
| 628 | * always satisfied. |
| 629 | * |
| 630 | * @hide |
| 631 | */ |
| 632 | public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE; |
| 633 | |
| 634 | /** |
| 635 | * Signal strength. This is a signed integer, and higher values indicate better signal. |
| 636 | * The exact units are bearer-dependent. For example, Wi-Fi uses RSSI. |
| 637 | */ |
| 638 | private int mSignalStrength; |
| 639 | |
| 640 | /** |
| 641 | * Sets the signal strength. This is a signed integer, with higher values indicating a stronger |
| 642 | * signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units |
| 643 | * reported by WifiManager. |
| 644 | * <p> |
| 645 | * Note that when used to register a network callback, this specifies the minimum acceptable |
| 646 | * signal strength. When received as the state of an existing network it specifies the current |
| 647 | * value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no |
| 648 | * effect when requesting a callback. |
| 649 | * |
| 650 | * @param signalStrength the bearer-specific signal strength. |
| 651 | * @hide |
| 652 | */ |
| 653 | public void setSignalStrength(int signalStrength) { |
| 654 | mSignalStrength = signalStrength; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Returns {@code true} if this object specifies a signal strength. |
| 659 | * |
| 660 | * @hide |
| 661 | */ |
| 662 | public boolean hasSignalStrength() { |
| 663 | return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Retrieves the signal strength. |
| 668 | * |
| 669 | * @return The bearer-specific signal strength. |
| 670 | * @hide |
| 671 | */ |
| 672 | public int getSignalStrength() { |
| 673 | return mSignalStrength; |
| 674 | } |
| 675 | |
| 676 | private void combineSignalStrength(NetworkCapabilities nc) { |
| 677 | this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength); |
| 678 | } |
| 679 | |
| 680 | private boolean satisfiedBySignalStrength(NetworkCapabilities nc) { |
| 681 | return this.mSignalStrength <= nc.mSignalStrength; |
| 682 | } |
| 683 | |
| 684 | private boolean equalsSignalStrength(NetworkCapabilities nc) { |
| 685 | return this.mSignalStrength == nc.mSignalStrength; |
| 686 | } |
| 687 | |
| 688 | /** |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 689 | * Combine a set of Capabilities to this one. Useful for coming up with the complete set |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 690 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 691 | */ |
| 692 | public void combineCapabilities(NetworkCapabilities nc) { |
| 693 | combineNetCapabilities(nc); |
| 694 | combineTransportTypes(nc); |
| 695 | combineLinkBandwidths(nc); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 696 | combineSpecifiers(nc); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 697 | combineSignalStrength(nc); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /** |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 701 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 702 | * |
| 703 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 704 | * @param onlyImmutable if {@code true}, do not consider mutable requirements such as link |
| 705 | * bandwidth, signal strength, or validation / captive portal status. |
| 706 | * |
| 707 | * @hide |
| 708 | */ |
| 709 | private boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc, boolean onlyImmutable) { |
| 710 | return (nc != null && |
| 711 | satisfiedByNetCapabilities(nc, onlyImmutable) && |
| 712 | satisfiedByTransportTypes(nc) && |
| 713 | (onlyImmutable || satisfiedByLinkBandwidths(nc)) && |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 714 | satisfiedBySpecifier(nc) && |
| 715 | (onlyImmutable || satisfiedBySignalStrength(nc))); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Check if our requirements are satisfied by the given {@code NetworkCapabilities}. |
| 720 | * |
| 721 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 722 | * |
| 723 | * @hide |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 724 | */ |
| 725 | public boolean satisfiedByNetworkCapabilities(NetworkCapabilities nc) { |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 726 | return satisfiedByNetworkCapabilities(nc, false); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Check if our immutable requirements are satisfied by the given {@code NetworkCapabilities}. |
| 731 | * |
| 732 | * @param nc the {@code NetworkCapabilities} that may or may not satisfy our requirements. |
| 733 | * |
| 734 | * @hide |
| 735 | */ |
| 736 | public boolean satisfiedByImmutableNetworkCapabilities(NetworkCapabilities nc) { |
| 737 | return satisfiedByNetworkCapabilities(nc, true); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Checks that our immutable capabilities are the same as those of the given |
| 742 | * {@code NetworkCapabilities}. |
| 743 | * |
| 744 | * @hide |
| 745 | */ |
| 746 | public boolean equalImmutableCapabilities(NetworkCapabilities nc) { |
| 747 | if (nc == null) return false; |
| 748 | return (equalsNetCapabilitiesImmutable(nc) && |
| 749 | equalsTransportTypes(nc) && |
| 750 | equalsSpecifier(nc)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 753 | /** |
| 754 | * Checks that our requestable capabilities are the same as those of the given |
| 755 | * {@code NetworkCapabilities}. |
| 756 | * |
| 757 | * @hide |
| 758 | */ |
| 759 | public boolean equalRequestableCapabilities(NetworkCapabilities nc) { |
| 760 | if (nc == null) return false; |
| 761 | return (equalsNetCapabilitiesRequestable(nc) && |
| 762 | equalsTransportTypes(nc) && |
| 763 | equalsSpecifier(nc)); |
| 764 | } |
| 765 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 766 | @Override |
| 767 | public boolean equals(Object obj) { |
| 768 | if (obj == null || (obj instanceof NetworkCapabilities == false)) return false; |
| 769 | NetworkCapabilities that = (NetworkCapabilities)obj; |
| 770 | return (equalsNetCapabilities(that) && |
| 771 | equalsTransportTypes(that) && |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 772 | equalsLinkBandwidths(that) && |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 773 | equalsSignalStrength(that) && |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 774 | equalsSpecifier(that)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public int hashCode() { |
| 779 | return ((int)(mNetworkCapabilities & 0xFFFFFFFF) + |
| 780 | ((int)(mNetworkCapabilities >> 32) * 3) + |
| 781 | ((int)(mTransportTypes & 0xFFFFFFFF) * 5) + |
| 782 | ((int)(mTransportTypes >> 32) * 7) + |
| 783 | (mLinkUpBandwidthKbps * 11) + |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 784 | (mLinkDownBandwidthKbps * 13) + |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 785 | Objects.hashCode(mNetworkSpecifier) * 17 + |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 786 | (mSignalStrength * 19)); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 789 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 790 | public int describeContents() { |
| 791 | return 0; |
| 792 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 793 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 794 | public void writeToParcel(Parcel dest, int flags) { |
| 795 | dest.writeLong(mNetworkCapabilities); |
| 796 | dest.writeLong(mTransportTypes); |
| 797 | dest.writeInt(mLinkUpBandwidthKbps); |
| 798 | dest.writeInt(mLinkDownBandwidthKbps); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 799 | dest.writeParcelable((Parcelable) mNetworkSpecifier, flags); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 800 | dest.writeInt(mSignalStrength); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 801 | } |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 802 | |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 803 | public static final Creator<NetworkCapabilities> CREATOR = |
| 804 | new Creator<NetworkCapabilities>() { |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 805 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 806 | public NetworkCapabilities createFromParcel(Parcel in) { |
| 807 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 808 | |
| 809 | netCap.mNetworkCapabilities = in.readLong(); |
| 810 | netCap.mTransportTypes = in.readLong(); |
| 811 | netCap.mLinkUpBandwidthKbps = in.readInt(); |
| 812 | netCap.mLinkDownBandwidthKbps = in.readInt(); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame^] | 813 | netCap.mNetworkSpecifier = in.readParcelable(null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 814 | netCap.mSignalStrength = in.readInt(); |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 815 | return netCap; |
| 816 | } |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 817 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 818 | public NetworkCapabilities[] newArray(int size) { |
| 819 | return new NetworkCapabilities[size]; |
| 820 | } |
| 821 | }; |
| 822 | |
Wink Saville | 4e2dea7 | 2014-09-20 11:04:03 -0700 | [diff] [blame] | 823 | @Override |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 824 | public String toString() { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 825 | int[] types = getTransportTypes(); |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 826 | String transports = (types.length > 0) ? " Transports: " + transportNamesOf(types) : ""; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 827 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 828 | types = getCapabilities(); |
| 829 | String capabilities = (types.length > 0 ? " Capabilities: " : ""); |
| 830 | for (int i = 0; i < types.length; ) { |
| 831 | switch (types[i]) { |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 832 | case NET_CAPABILITY_MMS: capabilities += "MMS"; break; |
| 833 | case NET_CAPABILITY_SUPL: capabilities += "SUPL"; break; |
| 834 | case NET_CAPABILITY_DUN: capabilities += "DUN"; break; |
| 835 | case NET_CAPABILITY_FOTA: capabilities += "FOTA"; break; |
| 836 | case NET_CAPABILITY_IMS: capabilities += "IMS"; break; |
| 837 | case NET_CAPABILITY_CBS: capabilities += "CBS"; break; |
| 838 | case NET_CAPABILITY_WIFI_P2P: capabilities += "WIFI_P2P"; break; |
| 839 | case NET_CAPABILITY_IA: capabilities += "IA"; break; |
| 840 | case NET_CAPABILITY_RCS: capabilities += "RCS"; break; |
| 841 | case NET_CAPABILITY_XCAP: capabilities += "XCAP"; break; |
| 842 | case NET_CAPABILITY_EIMS: capabilities += "EIMS"; break; |
| 843 | case NET_CAPABILITY_NOT_METERED: capabilities += "NOT_METERED"; break; |
| 844 | case NET_CAPABILITY_INTERNET: capabilities += "INTERNET"; break; |
| 845 | case NET_CAPABILITY_NOT_RESTRICTED: capabilities += "NOT_RESTRICTED"; break; |
Robert Greenwalt | 16e12ab | 2014-07-08 15:31:37 -0700 | [diff] [blame] | 846 | case NET_CAPABILITY_TRUSTED: capabilities += "TRUSTED"; break; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 847 | case NET_CAPABILITY_NOT_VPN: capabilities += "NOT_VPN"; break; |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 848 | case NET_CAPABILITY_VALIDATED: capabilities += "VALIDATED"; break; |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 849 | case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 850 | case NET_CAPABILITY_FOREGROUND: capabilities += "FOREGROUND"; break; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 851 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 852 | if (++i < types.length) capabilities += "&"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | String upBand = ((mLinkUpBandwidthKbps > 0) ? " LinkUpBandwidth>=" + |
| 856 | mLinkUpBandwidthKbps + "Kbps" : ""); |
| 857 | String dnBand = ((mLinkDownBandwidthKbps > 0) ? " LinkDnBandwidth>=" + |
| 858 | mLinkDownBandwidthKbps + "Kbps" : ""); |
| 859 | |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 860 | String specifier = (mNetworkSpecifier == null ? |
| 861 | "" : " Specifier: <" + mNetworkSpecifier + ">"); |
| 862 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 863 | String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : ""); |
| 864 | |
| 865 | return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]"; |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 866 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 867 | |
| 868 | /** |
| 869 | * @hide |
| 870 | */ |
| 871 | public static String transportNamesOf(int[] types) { |
| 872 | String transports = ""; |
| 873 | for (int i = 0; i < types.length;) { |
| 874 | switch (types[i]) { |
| 875 | case TRANSPORT_CELLULAR: transports += "CELLULAR"; break; |
| 876 | case TRANSPORT_WIFI: transports += "WIFI"; break; |
| 877 | case TRANSPORT_BLUETOOTH: transports += "BLUETOOTH"; break; |
| 878 | case TRANSPORT_ETHERNET: transports += "ETHERNET"; break; |
| 879 | case TRANSPORT_VPN: transports += "VPN"; break; |
Etan Cohen | 0849ded | 2016-10-26 11:22:06 -0700 | [diff] [blame] | 880 | case TRANSPORT_WIFI_AWARE: transports += "WIFI_AWARE"; break; |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 881 | } |
| 882 | if (++i < types.length) transports += "|"; |
| 883 | } |
| 884 | return transports; |
| 885 | } |
Robert Greenwalt | 1448f05 | 2014-04-08 13:41:39 -0700 | [diff] [blame] | 886 | } |