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