blob: 431773dc2622e67d0e000e6bcf657cffb15f0630 [file] [log] [blame]
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -07001/*
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
17package android.net;
18
Jeff Sharkey49bcd602017-11-09 13:11:50 -070019import android.annotation.NonNull;
Chalard Jean7412d7a2019-12-11 17:15:31 +090020import android.annotation.Nullable;
paulhu3d67f532019-03-22 16:35:06 +080021import android.annotation.RequiresPermission;
paulhu7610bc72018-12-12 17:52:57 +080022import android.annotation.SystemApi;
Artur Satayev26958002019-12-10 17:47:52 +000023import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkeyd778da32018-03-28 14:01:55 -060024import android.net.NetworkCapabilities.NetCapability;
25import android.net.NetworkCapabilities.Transport;
Mathew Inwood31755f92018-12-20 13:53:36 +000026import android.os.Build;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070027import android.os.Parcel;
28import android.os.Parcelable;
Chalard Jeanb552c462018-02-21 18:43:54 +090029import android.os.Process;
Etan Cohena7434272017-04-03 12:17:51 -070030import android.text.TextUtils;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080031import android.util.proto.ProtoOutputStream;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070032
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090033import java.util.Objects;
Chalard Jean616ede02018-03-09 20:52:15 +090034import java.util.Set;
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090035
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070036/**
Robert Greenwalt7569f182014-06-08 16:42:59 -070037 * 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 Greenwalt6078b502014-06-11 16:05:07 -070039 * via {@link ConnectivityManager#registerNetworkCallback}.
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070040 */
41public class NetworkRequest implements Parcelable {
Robert Greenwalt7b816022014-04-18 15:25:25 -070042 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -070043 * The {@link NetworkCapabilities} that define this request.
44 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070045 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010046 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -070047 public final @NonNull NetworkCapabilities networkCapabilities;
Robert Greenwalt7b816022014-04-18 15:25:25 -070048
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 Greenwalt34524f02014-05-18 16:22:10 -070053 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070054 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010055 @UnsupportedAppUsage
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070056 public final int requestId;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070057
Robert Greenwalt7b816022014-04-18 15:25:25 -070058 /**
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070059 * Set for legacy requests and the default. Set to TYPE_NONE for none.
Robert Greenwalt7b816022014-04-18 15:25:25 -070060 * Causes CONNECTIVITY_ACTION broadcasts to be sent.
61 * @hide
62 */
Mathew Inwood31755f92018-12-20 13:53:36 +000063 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070064 public final int legacyType;
Robert Greenwalt7b816022014-04-18 15:25:25 -070065
Robert Greenwalt7b816022014-04-18 15:25:25 -070066 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090067 * A NetworkRequest as used by the system can be one of the following types:
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090068 *
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 Colitti3d4a1062016-09-09 18:48:56 +090082 * 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 Colittib35d40d2016-07-01 13:19:21 +090096 *
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 Greenwalt7b816022014-04-18 15:25:25 -0700102 * @hide
103 */
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900104 public static enum Type {
105 NONE,
106 LISTEN,
107 TRACK_DEFAULT,
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900108 REQUEST,
109 BACKGROUND_REQUEST,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900110 };
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 Greenwaltf5b74f92014-09-03 19:35:47 -0700123 if (nc == null) {
124 throw new NullPointerException();
125 }
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700126 requestId = rId;
127 networkCapabilities = nc;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700128 this.legacyType = legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900129 this.type = type;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700130 }
131
Robert Greenwalt34524f02014-05-18 16:22:10 -0700132 /**
133 * @hide
134 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700135 public NetworkRequest(NetworkRequest that) {
136 networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
137 requestId = that.requestId;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700138 this.legacyType = that.legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900139 this.type = that.type;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700140 }
141
Robert Greenwalt7569f182014-06-08 16:42:59 -0700142 /**
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 Jeanb552c462018-02-21 18:43:54 +0900147 private final NetworkCapabilities mNetworkCapabilities;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700148
149 /**
150 * Default constructor for Builder.
151 */
Chalard Jeanb552c462018-02-21 18:43:54 +0900152 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 Greenwalt7569f182014-06-08 16:42:59 -0700159
160 /**
161 * Build {@link NetworkRequest} give the current set of capabilities.
162 */
163 public NetworkRequest build() {
Paul Jensen487ffe72015-07-24 15:57:11 -0400164 // 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 Colittib35d40d2016-07-01 13:19:21 +0900171 ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700172 }
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 Sharkeyde570312017-10-24 21:25:50 -0600178 * satisfied.
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700179 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600180 * @param capability The capability to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700181 * @return The builder to facilitate chaining
182 * {@code builder.addCapability(...).addCapability();}.
183 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600184 public Builder addCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700185 mNetworkCapabilities.addCapability(capability);
186 return this;
187 }
188
189 /**
Pavel Maltsevbb5b3af2018-04-09 13:10:11 -0700190 * Removes (if found) the given capability from this builder instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700191 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600192 * @param capability The capability to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700193 * @return The builder to facilitate chaining.
194 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600195 public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700196 mNetworkCapabilities.removeCapability(capability);
197 return this;
198 }
199
200 /**
Erik Kline35bf06c2017-01-26 18:08:28 +0900201 * 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 Jean4c4bc932018-05-18 23:48:49 +0900209 mNetworkCapabilities.set(nc);
Erik Kline35bf06c2017-01-26 18:08:28 +0900210 return this;
211 }
212
213 /**
Chalard Jean616ede02018-03-09 20:52:15 +0900214 * 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 Maltsev1cd48da2018-02-01 11:16:02 -0800227 * 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 Colitti4c6f4a62018-04-06 17:35:33 +0900237 *
Lorenzo Colitti19c0cca2018-04-20 17:53:29 +0900238 * @hide
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800239 */
240 public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
241 mNetworkCapabilities.addUnwantedCapability(capability);
242 return this;
243 }
244
245 /**
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900246 * 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 Inwoodfa3a7462018-08-08 14:52:47 +0100252 @UnsupportedAppUsage
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900253 public Builder clearCapabilities() {
254 mNetworkCapabilities.clearAll();
255 return this;
256 }
257
258 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -0700259 * 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 Sharkeyde570312017-10-24 21:25:50 -0600262 * are required, none should be specified here.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700263 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600264 * @param transportType The transport type to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700265 * @return The builder to facilitate chaining.
266 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600267 public Builder addTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700268 mNetworkCapabilities.addTransportType(transportType);
269 return this;
270 }
271
272 /**
273 * Removes (if found) the given transport from this builder instance.
274 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600275 * @param transportType The transport type to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700276 * @return The builder to facilitate chaining.
277 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600278 public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700279 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 Greenwalt5f90bcc2014-07-09 17:25:41 -0700297
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 Cohena7434272017-04-03 12:17:51 -0700313 /*
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 Cohena7434272017-04-03 12:17:51 -0700331 */
332 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
333 MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(networkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700334 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
335 return this;
336 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900337
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 *
paulhu3d67f532019-03-22 16:35:06 +0800348 * <p>This method requires the caller to hold the
349 * {@link android.Manifest.permission#NETWORK_SIGNAL_STRENGTH_WAKEUP} permission
350 *
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900351 * @param signalStrength the bearer-specific signal strength.
352 * @hide
353 */
paulhu7610bc72018-12-12 17:52:57 +0800354 @SystemApi
paulhu3d67f532019-03-22 16:35:06 +0800355 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP)
paulhud9736de2019-03-08 16:35:20 +0800356 public @NonNull Builder setSignalStrength(int signalStrength) {
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900357 mNetworkCapabilities.setSignalStrength(signalStrength);
358 return this;
359 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700360 }
361
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700362 // implement the Parcelable interface
363 public int describeContents() {
364 return 0;
365 }
366 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700367 networkCapabilities.writeToParcel(dest, flags);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700368 dest.writeInt(legacyType);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700369 dest.writeInt(requestId);
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900370 dest.writeString(type.name());
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700371 }
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700372 public static final @android.annotation.NonNull Creator<NetworkRequest> CREATOR =
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700373 new Creator<NetworkRequest>() {
374 public NetworkRequest createFromParcel(Parcel in) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700375 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700376 int legacyType = in.readInt();
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700377 int requestId = in.readInt();
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900378 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
379 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700380 return result;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700381 }
382 public NetworkRequest[] newArray(int size) {
383 return new NetworkRequest[size];
384 }
385 };
386
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900387 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900388 * Returns true iff. this NetworkRequest is of type LISTEN.
Lorenzo Colittif4a45f42016-07-18 18:17:08 +0900389 *
390 * @hide
391 */
392 public boolean isListen() {
393 return type == Type.LISTEN;
394 }
395
396 /**
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900397 * 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 Colitti3d4a1062016-09-09 18:48:56 +0900402 * - 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 Colittib35d40d2016-07-01 13:19:21 +0900405 *
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 Colitti3d4a1062016-09-09 18:48:56 +0900412 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 Colittib35d40d2016-07-01 13:19:21 +0900430 return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
431 }
432
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900433 /**
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 Sharkeyd778da32018-03-28 14:01:55 -0600442 /**
443 * @see Builder#addCapability(int)
444 */
445 public boolean hasCapability(@NetCapability int capability) {
446 return networkCapabilities.hasCapability(capability);
447 }
448
449 /**
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700450 * @see Builder#addUnwantedCapability(int)
Lorenzo Colitti4c6f4a62018-04-06 17:35:33 +0900451 *
Lorenzo Colitti19c0cca2018-04-20 17:53:29 +0900452 * @hide
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700453 */
454 public boolean hasUnwantedCapability(@NetCapability int capability) {
455 return networkCapabilities.hasUnwantedCapability(capability);
456 }
457
458 /**
Jeff Sharkeyd778da32018-03-28 14:01:55 -0600459 * @see Builder#addTransportType(int)
460 */
461 public boolean hasTransport(@Transport int transportType) {
462 return networkCapabilities.hasTransport(transportType);
463 }
464
Chalard Jean7412d7a2019-12-11 17:15:31 +0900465 /**
466 * @see Builder#setNetworkSpecifier(NetworkSpecifier)
467 */
468 @Nullable
469 public NetworkSpecifier getNetworkSpecifier() {
470 return networkCapabilities.getNetworkSpecifier();
471 }
472
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700473 public String toString() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900474 return "NetworkRequest [ " + type + " id=" + requestId +
475 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
Robert Greenwalt7b816022014-04-18 15:25:25 -0700476 ", " + networkCapabilities.toString() + " ]";
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700477 }
478
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800479 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 Huangcb782852019-12-05 11:28:11 -0800497 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800498 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 Huangcb782852019-12-05 11:28:11 -0800503 networkCapabilities.dumpDebug(proto, NetworkRequestProto.NETWORK_CAPABILITIES);
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800504
505 proto.end(token);
506 }
507
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700508 public boolean equals(Object obj) {
509 if (obj instanceof NetworkRequest == false) return false;
510 NetworkRequest that = (NetworkRequest)obj;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700511 return (that.legacyType == this.legacyType &&
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700512 that.requestId == this.requestId &&
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900513 that.type == this.type &&
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900514 Objects.equals(that.networkCapabilities, this.networkCapabilities));
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700515 }
516
517 public int hashCode() {
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900518 return Objects.hash(requestId, legacyType, networkCapabilities, type);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700519 }
520}