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