Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -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 | 49bcd60 | 2017-11-09 13:11:50 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Chalard Jean | 7412d7a | 2019-12-11 17:15:31 +0900 | [diff] [blame] | 20 | import android.annotation.Nullable; |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
paulhu | 7610bc7 | 2018-12-12 17:52:57 +0800 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | d778da3 | 2018-03-28 14:01:55 -0600 | [diff] [blame] | 24 | import android.net.NetworkCapabilities.NetCapability; |
| 25 | import android.net.NetworkCapabilities.Transport; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 26 | import android.os.Build; |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 29 | import android.os.Process; |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 31 | import android.util.proto.ProtoOutputStream; |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 32 | |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 33 | import java.util.Objects; |
Chalard Jean | 616ede0 | 2018-03-09 20:52:15 +0900 | [diff] [blame] | 34 | import java.util.Set; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 35 | |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 36 | /** |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 37 | * Defines a request for a network, made through {@link NetworkRequest.Builder} and used |
| 38 | * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 39 | * via {@link ConnectivityManager#registerNetworkCallback}. |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 40 | */ |
| 41 | public class NetworkRequest implements Parcelable { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 42 | /** |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 43 | * The {@link NetworkCapabilities} that define this request. |
| 44 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 45 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 46 | @UnsupportedAppUsage |
Jeff Sharkey | 49bcd60 | 2017-11-09 13:11:50 -0700 | [diff] [blame] | 47 | public final @NonNull NetworkCapabilities networkCapabilities; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Identifies the request. NetworkRequests should only be constructed by |
| 51 | * the Framework and given out to applications as tokens to be used to identify |
| 52 | * the request. |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 53 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 54 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 55 | @UnsupportedAppUsage |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 56 | public final int requestId; |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 57 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 58 | /** |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 59 | * Set for legacy requests and the default. Set to TYPE_NONE for none. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 60 | * Causes CONNECTIVITY_ACTION broadcasts to be sent. |
| 61 | * @hide |
| 62 | */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 63 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 64 | public final int legacyType; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 65 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 66 | /** |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 67 | * A NetworkRequest as used by the system can be one of the following types: |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 68 | * |
| 69 | * - LISTEN, for which the framework will issue callbacks about any |
| 70 | * and all networks that match the specified NetworkCapabilities, |
| 71 | * |
| 72 | * - REQUEST, capable of causing a specific network to be created |
| 73 | * first (e.g. a telephony DUN request), the framework will issue |
| 74 | * callbacks about the single, highest scoring current network |
| 75 | * (if any) that matches the specified NetworkCapabilities, or |
| 76 | * |
| 77 | * - TRACK_DEFAULT, a hybrid of the two designed such that the |
| 78 | * framework will issue callbacks for the single, highest scoring |
| 79 | * current network (if any) that matches the capabilities of the |
| 80 | * default Internet request (mDefaultRequest), but which cannot cause |
| 81 | * the framework to either create or retain the existence of any |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 82 | * specific network. Note that from the point of view of the request |
| 83 | * matching code, TRACK_DEFAULT is identical to REQUEST: its special |
| 84 | * behaviour is not due to different semantics, but to the fact that |
| 85 | * the system will only ever create a TRACK_DEFAULT with capabilities |
| 86 | * that are identical to the default request's capabilities, thus |
| 87 | * causing it to share fate in every way with the default request. |
| 88 | * |
| 89 | * - BACKGROUND_REQUEST, like REQUEST but does not cause any networks |
| 90 | * to retain the NET_CAPABILITY_FOREGROUND capability. A network with |
| 91 | * no foreground requests is in the background. A network that has |
| 92 | * one or more background requests and loses its last foreground |
| 93 | * request to a higher-scoring network will not go into the |
| 94 | * background immediately, but will linger and go into the background |
| 95 | * after the linger timeout. |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 96 | * |
| 97 | * - The value NONE is used only by applications. When an application |
| 98 | * creates a NetworkRequest, it does not have a type; the type is set |
| 99 | * by the system depending on the method used to file the request |
| 100 | * (requestNetwork, registerNetworkCallback, etc.). |
| 101 | * |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 102 | * @hide |
| 103 | */ |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 104 | public static enum Type { |
| 105 | NONE, |
| 106 | LISTEN, |
| 107 | TRACK_DEFAULT, |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 108 | REQUEST, |
| 109 | BACKGROUND_REQUEST, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * The type of the request. This is only used by the system and is always NONE elsewhere. |
| 114 | * |
| 115 | * @hide |
| 116 | */ |
| 117 | public final Type type; |
| 118 | |
| 119 | /** |
| 120 | * @hide |
| 121 | */ |
| 122 | public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type) { |
Robert Greenwalt | f5b74f9 | 2014-09-03 19:35:47 -0700 | [diff] [blame] | 123 | if (nc == null) { |
| 124 | throw new NullPointerException(); |
| 125 | } |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 126 | requestId = rId; |
| 127 | networkCapabilities = nc; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 128 | this.legacyType = legacyType; |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 129 | this.type = type; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 132 | /** |
| 133 | * @hide |
| 134 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 135 | public NetworkRequest(NetworkRequest that) { |
| 136 | networkCapabilities = new NetworkCapabilities(that.networkCapabilities); |
| 137 | requestId = that.requestId; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 138 | this.legacyType = that.legacyType; |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 139 | this.type = that.type; |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Builder used to create {@link NetworkRequest} objects. Specify the Network features |
| 144 | * needed in terms of {@link NetworkCapabilities} features |
| 145 | */ |
| 146 | public static class Builder { |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 147 | private final NetworkCapabilities mNetworkCapabilities; |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Default constructor for Builder. |
| 151 | */ |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 152 | public Builder() { |
| 153 | // By default, restrict this request to networks available to this app. |
| 154 | // Apps can rescind this restriction, but ConnectivityService will enforce |
| 155 | // it for apps that do not have the NETWORK_SETTINGS permission. |
| 156 | mNetworkCapabilities = new NetworkCapabilities(); |
| 157 | mNetworkCapabilities.setSingleUid(Process.myUid()); |
| 158 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Build {@link NetworkRequest} give the current set of capabilities. |
| 162 | */ |
| 163 | public NetworkRequest build() { |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 164 | // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED |
| 165 | // when later an unrestricted capability could be added to mNetworkCapabilities, in |
| 166 | // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which |
| 167 | // maybeMarkCapabilitiesRestricted() doesn't add back. |
| 168 | final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities); |
| 169 | nc.maybeMarkCapabilitiesRestricted(); |
| 170 | return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 171 | ConnectivityManager.REQUEST_ID_UNSET, Type.NONE); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Add the given capability requirement to this builder. These represent |
| 176 | * the requested network's required capabilities. Note that when searching |
| 177 | * for a network to satisfy a request, all capabilities requested must be |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 178 | * satisfied. |
Pavel Maltsev | 67d3dfd | 2018-04-02 11:10:13 -0700 | [diff] [blame] | 179 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 180 | * @param capability The capability to add. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 181 | * @return The builder to facilitate chaining |
| 182 | * {@code builder.addCapability(...).addCapability();}. |
| 183 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 184 | public Builder addCapability(@NetworkCapabilities.NetCapability int capability) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 185 | mNetworkCapabilities.addCapability(capability); |
| 186 | return this; |
| 187 | } |
| 188 | |
| 189 | /** |
Pavel Maltsev | bb5b3af | 2018-04-09 13:10:11 -0700 | [diff] [blame] | 190 | * Removes (if found) the given capability from this builder instance. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 191 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 192 | * @param capability The capability to remove. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 193 | * @return The builder to facilitate chaining. |
| 194 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 195 | public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 196 | mNetworkCapabilities.removeCapability(capability); |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 201 | * Set the {@code NetworkCapabilities} for this builder instance, |
| 202 | * overriding any capabilities that had been previously set. |
| 203 | * |
| 204 | * @param nc The superseding {@code NetworkCapabilities} instance. |
| 205 | * @return The builder to facilitate chaining. |
| 206 | * @hide |
| 207 | */ |
| 208 | public Builder setCapabilities(NetworkCapabilities nc) { |
Chalard Jean | 4c4bc93 | 2018-05-18 23:48:49 +0900 | [diff] [blame] | 209 | mNetworkCapabilities.set(nc); |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 210 | return this; |
| 211 | } |
| 212 | |
| 213 | /** |
Chalard Jean | 616ede0 | 2018-03-09 20:52:15 +0900 | [diff] [blame] | 214 | * Set the watched UIDs for this request. This will be reset and wiped out unless |
| 215 | * the calling app holds the CHANGE_NETWORK_STATE permission. |
| 216 | * |
| 217 | * @param uids The watched UIDs as a set of UidRanges, or null for everything. |
| 218 | * @return The builder to facilitate chaining. |
| 219 | * @hide |
| 220 | */ |
| 221 | public Builder setUids(Set<UidRange> uids) { |
| 222 | mNetworkCapabilities.setUids(uids); |
| 223 | return this; |
| 224 | } |
| 225 | |
| 226 | /** |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 227 | * Add a capability that must not exist in the requested network. |
| 228 | * <p> |
| 229 | * If the capability was previously added to the list of required capabilities (for |
| 230 | * example, it was there by default or added using {@link #addCapability(int)} method), then |
| 231 | * it will be removed from the list of required capabilities as well. |
| 232 | * |
| 233 | * @see #addCapability(int) |
| 234 | * |
| 235 | * @param capability The capability to add to unwanted capability list. |
| 236 | * @return The builder to facilitate chaining. |
Lorenzo Colitti | 4c6f4a6 | 2018-04-06 17:35:33 +0900 | [diff] [blame] | 237 | * |
Lorenzo Colitti | 19c0cca | 2018-04-20 17:53:29 +0900 | [diff] [blame] | 238 | * @hide |
Pavel Maltsev | 1cd48da | 2018-02-01 11:16:02 -0800 | [diff] [blame] | 239 | */ |
| 240 | public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) { |
| 241 | mNetworkCapabilities.addUnwantedCapability(capability); |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | /** |
Lorenzo Colitti | 84b83c5 | 2015-05-19 23:31:49 +0900 | [diff] [blame] | 246 | * Completely clears all the {@code NetworkCapabilities} from this builder instance, |
| 247 | * removing even the capabilities that are set by default when the object is constructed. |
| 248 | * |
| 249 | * @return The builder to facilitate chaining. |
| 250 | * @hide |
| 251 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 252 | @UnsupportedAppUsage |
Lorenzo Colitti | 84b83c5 | 2015-05-19 23:31:49 +0900 | [diff] [blame] | 253 | public Builder clearCapabilities() { |
| 254 | mNetworkCapabilities.clearAll(); |
| 255 | return this; |
| 256 | } |
| 257 | |
| 258 | /** |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 259 | * Adds the given transport requirement to this builder. These represent |
| 260 | * the set of allowed transports for the request. Only networks using one |
| 261 | * of these transports will satisfy the request. If no particular transports |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 262 | * are required, none should be specified here. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 263 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 264 | * @param transportType The transport type to add. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 265 | * @return The builder to facilitate chaining. |
| 266 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 267 | public Builder addTransportType(@NetworkCapabilities.Transport int transportType) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 268 | mNetworkCapabilities.addTransportType(transportType); |
| 269 | return this; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Removes (if found) the given transport from this builder instance. |
| 274 | * |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 275 | * @param transportType The transport type to remove. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 276 | * @return The builder to facilitate chaining. |
| 277 | */ |
Jeff Sharkey | de57031 | 2017-10-24 21:25:50 -0600 | [diff] [blame] | 278 | public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 279 | mNetworkCapabilities.removeTransportType(transportType); |
| 280 | return this; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @hide |
| 285 | */ |
| 286 | public Builder setLinkUpstreamBandwidthKbps(int upKbps) { |
| 287 | mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps); |
| 288 | return this; |
| 289 | } |
| 290 | /** |
| 291 | * @hide |
| 292 | */ |
| 293 | public Builder setLinkDownstreamBandwidthKbps(int downKbps) { |
| 294 | mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps); |
| 295 | return this; |
| 296 | } |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * Sets the optional bearer specific network specifier. |
| 300 | * This has no meaning if a single transport is also not specified, so calling |
| 301 | * this without a single transport set will generate an exception, as will |
| 302 | * subsequently adding or removing transports after this is set. |
| 303 | * </p> |
| 304 | * The interpretation of this {@code String} is bearer specific and bearers that use |
| 305 | * it should document their particulars. For example, Bluetooth may use some sort of |
| 306 | * device id while WiFi could used ssid and/or bssid. Cellular may use carrier spn. |
| 307 | * |
| 308 | * @param networkSpecifier An {@code String} of opaque format used to specify the bearer |
| 309 | * specific network specifier where the bearer has a choice of |
| 310 | * networks. |
| 311 | */ |
| 312 | public Builder setNetworkSpecifier(String networkSpecifier) { |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 313 | /* |
| 314 | * A StringNetworkSpecifier does not accept null or empty ("") strings. When network |
| 315 | * specifiers were strings a null string and an empty string were considered equivalent. |
| 316 | * Hence no meaning is attached to a null or empty ("") string. |
| 317 | */ |
| 318 | return setNetworkSpecifier(TextUtils.isEmpty(networkSpecifier) ? null |
| 319 | : new StringNetworkSpecifier(networkSpecifier)); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Sets the optional bearer specific network specifier. |
| 324 | * This has no meaning if a single transport is also not specified, so calling |
| 325 | * this without a single transport set will generate an exception, as will |
| 326 | * subsequently adding or removing transports after this is set. |
| 327 | * </p> |
| 328 | * |
| 329 | * @param networkSpecifier A concrete, parcelable framework class that extends |
| 330 | * NetworkSpecifier. |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 331 | */ |
| 332 | public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) { |
| 333 | MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(networkSpecifier); |
Robert Greenwalt | 5f90bcc | 2014-07-09 17:25:41 -0700 | [diff] [blame] | 334 | mNetworkCapabilities.setNetworkSpecifier(networkSpecifier); |
| 335 | return this; |
| 336 | } |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 337 | |
| 338 | /** |
| 339 | * Sets the signal strength. This is a signed integer, with higher values indicating a |
| 340 | * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same |
| 341 | * RSSI units reported by WifiManager. |
| 342 | * <p> |
| 343 | * Note that when used to register a network callback, this specifies the minimum acceptable |
| 344 | * signal strength. When received as the state of an existing network it specifies the |
| 345 | * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when |
| 346 | * received and has no effect when requesting a callback. |
| 347 | * |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 348 | * <p>This method requires the caller to hold the |
| 349 | * {@link android.Manifest.permission#NETWORK_SIGNAL_STRENGTH_WAKEUP} permission |
| 350 | * |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 351 | * @param signalStrength the bearer-specific signal strength. |
| 352 | * @hide |
| 353 | */ |
paulhu | 7610bc7 | 2018-12-12 17:52:57 +0800 | [diff] [blame] | 354 | @SystemApi |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 355 | @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP) |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 356 | public @NonNull Builder setSignalStrength(int signalStrength) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 357 | mNetworkCapabilities.setSignalStrength(signalStrength); |
| 358 | return this; |
| 359 | } |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 362 | // implement the Parcelable interface |
| 363 | public int describeContents() { |
| 364 | return 0; |
| 365 | } |
| 366 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | 49bcd60 | 2017-11-09 13:11:50 -0700 | [diff] [blame] | 367 | networkCapabilities.writeToParcel(dest, flags); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 368 | dest.writeInt(legacyType); |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 369 | dest.writeInt(requestId); |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 370 | dest.writeString(type.name()); |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 371 | } |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 372 | public static final @android.annotation.NonNull Creator<NetworkRequest> CREATOR = |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 373 | new Creator<NetworkRequest>() { |
| 374 | public NetworkRequest createFromParcel(Parcel in) { |
Jeff Sharkey | 49bcd60 | 2017-11-09 13:11:50 -0700 | [diff] [blame] | 375 | NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 376 | int legacyType = in.readInt(); |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 377 | int requestId = in.readInt(); |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 378 | Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid. |
| 379 | NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 380 | return result; |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 381 | } |
| 382 | public NetworkRequest[] newArray(int size) { |
| 383 | return new NetworkRequest[size]; |
| 384 | } |
| 385 | }; |
| 386 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 387 | /** |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 388 | * Returns true iff. this NetworkRequest is of type LISTEN. |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 389 | * |
| 390 | * @hide |
| 391 | */ |
| 392 | public boolean isListen() { |
| 393 | return type == Type.LISTEN; |
| 394 | } |
| 395 | |
| 396 | /** |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 397 | * Returns true iff. the contained NetworkRequest is one that: |
| 398 | * |
| 399 | * - should be associated with at most one satisfying network |
| 400 | * at a time; |
| 401 | * |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 402 | * - should cause a network to be kept up, but not necessarily in |
| 403 | * the foreground, if it is the best network which can satisfy the |
| 404 | * NetworkRequest. |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 405 | * |
| 406 | * For full detail of how isRequest() is used for pairing Networks with |
| 407 | * NetworkRequests read rematchNetworkAndRequests(). |
| 408 | * |
| 409 | * @hide |
| 410 | */ |
| 411 | public boolean isRequest() { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 412 | return isForegroundRequest() || isBackgroundRequest(); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Returns true iff. the contained NetworkRequest is one that: |
| 417 | * |
| 418 | * - should be associated with at most one satisfying network |
| 419 | * at a time; |
| 420 | * |
| 421 | * - should cause a network to be kept up and in the foreground if |
| 422 | * it is the best network which can satisfy the NetworkRequest. |
| 423 | * |
| 424 | * For full detail of how isRequest() is used for pairing Networks with |
| 425 | * NetworkRequests read rematchNetworkAndRequests(). |
| 426 | * |
| 427 | * @hide |
| 428 | */ |
| 429 | public boolean isForegroundRequest() { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 430 | return type == Type.TRACK_DEFAULT || type == Type.REQUEST; |
| 431 | } |
| 432 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 433 | /** |
| 434 | * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST. |
| 435 | * |
| 436 | * @hide |
| 437 | */ |
| 438 | public boolean isBackgroundRequest() { |
| 439 | return type == Type.BACKGROUND_REQUEST; |
| 440 | } |
| 441 | |
Jeff Sharkey | d778da3 | 2018-03-28 14:01:55 -0600 | [diff] [blame] | 442 | /** |
| 443 | * @see Builder#addCapability(int) |
| 444 | */ |
| 445 | public boolean hasCapability(@NetCapability int capability) { |
| 446 | return networkCapabilities.hasCapability(capability); |
| 447 | } |
| 448 | |
| 449 | /** |
Pavel Maltsev | 67d3dfd | 2018-04-02 11:10:13 -0700 | [diff] [blame] | 450 | * @see Builder#addUnwantedCapability(int) |
Lorenzo Colitti | 4c6f4a6 | 2018-04-06 17:35:33 +0900 | [diff] [blame] | 451 | * |
Lorenzo Colitti | 19c0cca | 2018-04-20 17:53:29 +0900 | [diff] [blame] | 452 | * @hide |
Pavel Maltsev | 67d3dfd | 2018-04-02 11:10:13 -0700 | [diff] [blame] | 453 | */ |
| 454 | public boolean hasUnwantedCapability(@NetCapability int capability) { |
| 455 | return networkCapabilities.hasUnwantedCapability(capability); |
| 456 | } |
| 457 | |
| 458 | /** |
Jeff Sharkey | d778da3 | 2018-03-28 14:01:55 -0600 | [diff] [blame] | 459 | * @see Builder#addTransportType(int) |
| 460 | */ |
| 461 | public boolean hasTransport(@Transport int transportType) { |
| 462 | return networkCapabilities.hasTransport(transportType); |
| 463 | } |
| 464 | |
Chalard Jean | 7412d7a | 2019-12-11 17:15:31 +0900 | [diff] [blame] | 465 | /** |
| 466 | * @see Builder#setNetworkSpecifier(NetworkSpecifier) |
| 467 | */ |
| 468 | @Nullable |
| 469 | public NetworkSpecifier getNetworkSpecifier() { |
| 470 | return networkCapabilities.getNetworkSpecifier(); |
| 471 | } |
| 472 | |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 473 | public String toString() { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 474 | return "NetworkRequest [ " + type + " id=" + requestId + |
| 475 | (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") + |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 476 | ", " + networkCapabilities.toString() + " ]"; |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 479 | private int typeToProtoEnum(Type t) { |
| 480 | switch (t) { |
| 481 | case NONE: |
| 482 | return NetworkRequestProto.TYPE_NONE; |
| 483 | case LISTEN: |
| 484 | return NetworkRequestProto.TYPE_LISTEN; |
| 485 | case TRACK_DEFAULT: |
| 486 | return NetworkRequestProto.TYPE_TRACK_DEFAULT; |
| 487 | case REQUEST: |
| 488 | return NetworkRequestProto.TYPE_REQUEST; |
| 489 | case BACKGROUND_REQUEST: |
| 490 | return NetworkRequestProto.TYPE_BACKGROUND_REQUEST; |
| 491 | default: |
| 492 | return NetworkRequestProto.TYPE_UNKNOWN; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /** @hide */ |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 497 | public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 498 | final long token = proto.start(fieldId); |
| 499 | |
| 500 | proto.write(NetworkRequestProto.TYPE, typeToProtoEnum(type)); |
| 501 | proto.write(NetworkRequestProto.REQUEST_ID, requestId); |
| 502 | proto.write(NetworkRequestProto.LEGACY_TYPE, legacyType); |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 503 | networkCapabilities.dumpDebug(proto, NetworkRequestProto.NETWORK_CAPABILITIES); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 504 | |
| 505 | proto.end(token); |
| 506 | } |
| 507 | |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 508 | public boolean equals(Object obj) { |
| 509 | if (obj instanceof NetworkRequest == false) return false; |
| 510 | NetworkRequest that = (NetworkRequest)obj; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 511 | return (that.legacyType == this.legacyType && |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 512 | that.requestId == this.requestId && |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 513 | that.type == this.type && |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 514 | Objects.equals(that.networkCapabilities, this.networkCapabilities)); |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | public int hashCode() { |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 518 | return Objects.hash(requestId, legacyType, networkCapabilities, type); |
Robert Greenwalt | 3c0bf5e | 2014-04-11 15:08:03 -0700 | [diff] [blame] | 519 | } |
| 520 | } |