blob: 9508217be53f0f05b8e1b5f65d7c86c4bf06d1f5 [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;
paulhu7610bc72018-12-12 17:52:57 +080020import android.annotation.SystemApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010021import android.annotation.UnsupportedAppUsage;
Jeff Sharkeyd778da32018-03-28 14:01:55 -060022import android.net.NetworkCapabilities.NetCapability;
23import android.net.NetworkCapabilities.Transport;
Mathew Inwood31755f92018-12-20 13:53:36 +000024import android.os.Build;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070025import android.os.Parcel;
26import android.os.Parcelable;
Chalard Jeanb552c462018-02-21 18:43:54 +090027import android.os.Process;
Etan Cohena7434272017-04-03 12:17:51 -070028import android.text.TextUtils;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080029import android.util.proto.ProtoOutputStream;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070030
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090031import java.util.Objects;
Chalard Jean616ede02018-03-09 20:52:15 +090032import java.util.Set;
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090033
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070034/**
Robert Greenwalt7569f182014-06-08 16:42:59 -070035 * 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 Greenwalt6078b502014-06-11 16:05:07 -070037 * via {@link ConnectivityManager#registerNetworkCallback}.
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070038 */
39public class NetworkRequest implements Parcelable {
Robert Greenwalt7b816022014-04-18 15:25:25 -070040 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -070041 * The {@link NetworkCapabilities} that define this request.
42 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070043 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010044 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -070045 public final @NonNull NetworkCapabilities networkCapabilities;
Robert Greenwalt7b816022014-04-18 15:25:25 -070046
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 Greenwalt34524f02014-05-18 16:22:10 -070051 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070052 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010053 @UnsupportedAppUsage
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070054 public final int requestId;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070055
Robert Greenwalt7b816022014-04-18 15:25:25 -070056 /**
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070057 * Set for legacy requests and the default. Set to TYPE_NONE for none.
Robert Greenwalt7b816022014-04-18 15:25:25 -070058 * Causes CONNECTIVITY_ACTION broadcasts to be sent.
59 * @hide
60 */
Mathew Inwood31755f92018-12-20 13:53:36 +000061 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070062 public final int legacyType;
Robert Greenwalt7b816022014-04-18 15:25:25 -070063
Robert Greenwalt7b816022014-04-18 15:25:25 -070064 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090065 * A NetworkRequest as used by the system can be one of the following types:
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090066 *
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 Colitti3d4a1062016-09-09 18:48:56 +090080 * 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 Colittib35d40d2016-07-01 13:19:21 +090094 *
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 Greenwalt7b816022014-04-18 15:25:25 -0700100 * @hide
101 */
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900102 public static enum Type {
103 NONE,
104 LISTEN,
105 TRACK_DEFAULT,
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900106 REQUEST,
107 BACKGROUND_REQUEST,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900108 };
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 Greenwaltf5b74f92014-09-03 19:35:47 -0700121 if (nc == null) {
122 throw new NullPointerException();
123 }
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700124 requestId = rId;
125 networkCapabilities = nc;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700126 this.legacyType = legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900127 this.type = type;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700128 }
129
Robert Greenwalt34524f02014-05-18 16:22:10 -0700130 /**
131 * @hide
132 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700133 public NetworkRequest(NetworkRequest that) {
134 networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
135 requestId = that.requestId;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700136 this.legacyType = that.legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900137 this.type = that.type;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700138 }
139
Robert Greenwalt7569f182014-06-08 16:42:59 -0700140 /**
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 Jeanb552c462018-02-21 18:43:54 +0900145 private final NetworkCapabilities mNetworkCapabilities;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700146
147 /**
148 * Default constructor for Builder.
149 */
Chalard Jeanb552c462018-02-21 18:43:54 +0900150 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 Greenwalt7569f182014-06-08 16:42:59 -0700157
158 /**
159 * Build {@link NetworkRequest} give the current set of capabilities.
160 */
161 public NetworkRequest build() {
Paul Jensen487ffe72015-07-24 15:57:11 -0400162 // 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 Colittib35d40d2016-07-01 13:19:21 +0900169 ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700170 }
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 Sharkeyde570312017-10-24 21:25:50 -0600176 * satisfied.
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700177 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600178 * @param capability The capability to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700179 * @return The builder to facilitate chaining
180 * {@code builder.addCapability(...).addCapability();}.
181 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600182 public Builder addCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700183 mNetworkCapabilities.addCapability(capability);
184 return this;
185 }
186
187 /**
Pavel Maltsevbb5b3af2018-04-09 13:10:11 -0700188 * Removes (if found) the given capability from this builder instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700189 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600190 * @param capability The capability to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700191 * @return The builder to facilitate chaining.
192 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600193 public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700194 mNetworkCapabilities.removeCapability(capability);
195 return this;
196 }
197
198 /**
Erik Kline35bf06c2017-01-26 18:08:28 +0900199 * 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 Jean4c4bc932018-05-18 23:48:49 +0900207 mNetworkCapabilities.set(nc);
Erik Kline35bf06c2017-01-26 18:08:28 +0900208 return this;
209 }
210
211 /**
Chalard Jean616ede02018-03-09 20:52:15 +0900212 * 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 Maltsev1cd48da2018-02-01 11:16:02 -0800225 * 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 Colitti4c6f4a62018-04-06 17:35:33 +0900235 *
Lorenzo Colitti19c0cca2018-04-20 17:53:29 +0900236 * @hide
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800237 */
238 public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
239 mNetworkCapabilities.addUnwantedCapability(capability);
240 return this;
241 }
242
243 /**
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900244 * 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 Inwoodfa3a7462018-08-08 14:52:47 +0100250 @UnsupportedAppUsage
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900251 public Builder clearCapabilities() {
252 mNetworkCapabilities.clearAll();
253 return this;
254 }
255
256 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -0700257 * 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 Sharkeyde570312017-10-24 21:25:50 -0600260 * are required, none should be specified here.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700261 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600262 * @param transportType The transport type to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700263 * @return The builder to facilitate chaining.
264 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600265 public Builder addTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700266 mNetworkCapabilities.addTransportType(transportType);
267 return this;
268 }
269
270 /**
271 * Removes (if found) the given transport from this builder instance.
272 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600273 * @param transportType The transport type to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700274 * @return The builder to facilitate chaining.
275 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600276 public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700277 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 Greenwalt5f90bcc2014-07-09 17:25:41 -0700295
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 Cohena7434272017-04-03 12:17:51 -0700311 /*
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 Cohena7434272017-04-03 12:17:51 -0700329 */
330 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
331 MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(networkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700332 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
333 return this;
334 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900335
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 */
paulhu7610bc72018-12-12 17:52:57 +0800349 @SystemApi
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900350 public Builder setSignalStrength(int signalStrength) {
351 mNetworkCapabilities.setSignalStrength(signalStrength);
352 return this;
353 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700354 }
355
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700356 // implement the Parcelable interface
357 public int describeContents() {
358 return 0;
359 }
360 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700361 networkCapabilities.writeToParcel(dest, flags);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700362 dest.writeInt(legacyType);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700363 dest.writeInt(requestId);
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900364 dest.writeString(type.name());
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700365 }
366 public static final Creator<NetworkRequest> CREATOR =
367 new Creator<NetworkRequest>() {
368 public NetworkRequest createFromParcel(Parcel in) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700369 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700370 int legacyType = in.readInt();
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700371 int requestId = in.readInt();
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900372 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
373 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700374 return result;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700375 }
376 public NetworkRequest[] newArray(int size) {
377 return new NetworkRequest[size];
378 }
379 };
380
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900381 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900382 * Returns true iff. this NetworkRequest is of type LISTEN.
Lorenzo Colittif4a45f42016-07-18 18:17:08 +0900383 *
384 * @hide
385 */
386 public boolean isListen() {
387 return type == Type.LISTEN;
388 }
389
390 /**
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900391 * 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 Colitti3d4a1062016-09-09 18:48:56 +0900396 * - 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 Colittib35d40d2016-07-01 13:19:21 +0900399 *
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 Colitti3d4a1062016-09-09 18:48:56 +0900406 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 Colittib35d40d2016-07-01 13:19:21 +0900424 return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
425 }
426
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900427 /**
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 Sharkeyd778da32018-03-28 14:01:55 -0600436 /**
437 * @see Builder#addCapability(int)
438 */
439 public boolean hasCapability(@NetCapability int capability) {
440 return networkCapabilities.hasCapability(capability);
441 }
442
443 /**
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700444 * @see Builder#addUnwantedCapability(int)
Lorenzo Colitti4c6f4a62018-04-06 17:35:33 +0900445 *
Lorenzo Colitti19c0cca2018-04-20 17:53:29 +0900446 * @hide
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700447 */
448 public boolean hasUnwantedCapability(@NetCapability int capability) {
449 return networkCapabilities.hasUnwantedCapability(capability);
450 }
451
452 /**
Jeff Sharkeyd778da32018-03-28 14:01:55 -0600453 * @see Builder#addTransportType(int)
454 */
455 public boolean hasTransport(@Transport int transportType) {
456 return networkCapabilities.hasTransport(transportType);
457 }
458
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700459 public String toString() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900460 return "NetworkRequest [ " + type + " id=" + requestId +
461 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
Robert Greenwalt7b816022014-04-18 15:25:25 -0700462 ", " + networkCapabilities.toString() + " ]";
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700463 }
464
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800465 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 Greenwalt3c0bf5e2014-04-11 15:08:03 -0700494 public boolean equals(Object obj) {
495 if (obj instanceof NetworkRequest == false) return false;
496 NetworkRequest that = (NetworkRequest)obj;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700497 return (that.legacyType == this.legacyType &&
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700498 that.requestId == this.requestId &&
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900499 that.type == this.type &&
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900500 Objects.equals(that.networkCapabilities, this.networkCapabilities));
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700501 }
502
503 public int hashCode() {
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900504 return Objects.hash(requestId, legacyType, networkCapabilities, type);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700505 }
506}