blob: 3b01b03edd4e1059eec60547e2d320748aede591 [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;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070024import android.os.Parcel;
25import android.os.Parcelable;
Chalard Jeanb552c462018-02-21 18:43:54 +090026import android.os.Process;
Etan Cohena7434272017-04-03 12:17:51 -070027import android.text.TextUtils;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080028import android.util.proto.ProtoOutputStream;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070029
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090030import java.util.Objects;
Chalard Jean616ede02018-03-09 20:52:15 +090031import java.util.Set;
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090032
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070033/**
Robert Greenwalt7569f182014-06-08 16:42:59 -070034 * Defines a request for a network, made through {@link NetworkRequest.Builder} and used
35 * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes
Robert Greenwalt6078b502014-06-11 16:05:07 -070036 * via {@link ConnectivityManager#registerNetworkCallback}.
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070037 */
38public class NetworkRequest implements Parcelable {
Robert Greenwalt7b816022014-04-18 15:25:25 -070039 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -070040 * The {@link NetworkCapabilities} that define this request.
41 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070042 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010043 @UnsupportedAppUsage
Jeff Sharkey49bcd602017-11-09 13:11:50 -070044 public final @NonNull NetworkCapabilities networkCapabilities;
Robert Greenwalt7b816022014-04-18 15:25:25 -070045
46 /**
47 * Identifies the request. NetworkRequests should only be constructed by
48 * the Framework and given out to applications as tokens to be used to identify
49 * the request.
Robert Greenwalt34524f02014-05-18 16:22:10 -070050 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070051 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010052 @UnsupportedAppUsage
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070053 public final int requestId;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070054
Robert Greenwalt7b816022014-04-18 15:25:25 -070055 /**
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070056 * Set for legacy requests and the default. Set to TYPE_NONE for none.
Robert Greenwalt7b816022014-04-18 15:25:25 -070057 * Causes CONNECTIVITY_ACTION broadcasts to be sent.
58 * @hide
59 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010060 @UnsupportedAppUsage
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070061 public final int legacyType;
Robert Greenwalt7b816022014-04-18 15:25:25 -070062
Robert Greenwalt7b816022014-04-18 15:25:25 -070063 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090064 * A NetworkRequest as used by the system can be one of the following types:
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090065 *
66 * - LISTEN, for which the framework will issue callbacks about any
67 * and all networks that match the specified NetworkCapabilities,
68 *
69 * - REQUEST, capable of causing a specific network to be created
70 * first (e.g. a telephony DUN request), the framework will issue
71 * callbacks about the single, highest scoring current network
72 * (if any) that matches the specified NetworkCapabilities, or
73 *
74 * - TRACK_DEFAULT, a hybrid of the two designed such that the
75 * framework will issue callbacks for the single, highest scoring
76 * current network (if any) that matches the capabilities of the
77 * default Internet request (mDefaultRequest), but which cannot cause
78 * the framework to either create or retain the existence of any
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090079 * specific network. Note that from the point of view of the request
80 * matching code, TRACK_DEFAULT is identical to REQUEST: its special
81 * behaviour is not due to different semantics, but to the fact that
82 * the system will only ever create a TRACK_DEFAULT with capabilities
83 * that are identical to the default request's capabilities, thus
84 * causing it to share fate in every way with the default request.
85 *
86 * - BACKGROUND_REQUEST, like REQUEST but does not cause any networks
87 * to retain the NET_CAPABILITY_FOREGROUND capability. A network with
88 * no foreground requests is in the background. A network that has
89 * one or more background requests and loses its last foreground
90 * request to a higher-scoring network will not go into the
91 * background immediately, but will linger and go into the background
92 * after the linger timeout.
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090093 *
94 * - The value NONE is used only by applications. When an application
95 * creates a NetworkRequest, it does not have a type; the type is set
96 * by the system depending on the method used to file the request
97 * (requestNetwork, registerNetworkCallback, etc.).
98 *
Robert Greenwalt7b816022014-04-18 15:25:25 -070099 * @hide
100 */
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900101 public static enum Type {
102 NONE,
103 LISTEN,
104 TRACK_DEFAULT,
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900105 REQUEST,
106 BACKGROUND_REQUEST,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900107 };
108
109 /**
110 * The type of the request. This is only used by the system and is always NONE elsewhere.
111 *
112 * @hide
113 */
114 public final Type type;
115
116 /**
117 * @hide
118 */
119 public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type) {
Robert Greenwaltf5b74f92014-09-03 19:35:47 -0700120 if (nc == null) {
121 throw new NullPointerException();
122 }
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700123 requestId = rId;
124 networkCapabilities = nc;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700125 this.legacyType = legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900126 this.type = type;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700127 }
128
Robert Greenwalt34524f02014-05-18 16:22:10 -0700129 /**
130 * @hide
131 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700132 public NetworkRequest(NetworkRequest that) {
133 networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
134 requestId = that.requestId;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700135 this.legacyType = that.legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900136 this.type = that.type;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700137 }
138
Robert Greenwalt7569f182014-06-08 16:42:59 -0700139 /**
140 * Builder used to create {@link NetworkRequest} objects. Specify the Network features
141 * needed in terms of {@link NetworkCapabilities} features
142 */
143 public static class Builder {
Chalard Jeanb552c462018-02-21 18:43:54 +0900144 private final NetworkCapabilities mNetworkCapabilities;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700145
146 /**
147 * Default constructor for Builder.
148 */
Chalard Jeanb552c462018-02-21 18:43:54 +0900149 public Builder() {
150 // By default, restrict this request to networks available to this app.
151 // Apps can rescind this restriction, but ConnectivityService will enforce
152 // it for apps that do not have the NETWORK_SETTINGS permission.
153 mNetworkCapabilities = new NetworkCapabilities();
154 mNetworkCapabilities.setSingleUid(Process.myUid());
155 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700156
157 /**
158 * Build {@link NetworkRequest} give the current set of capabilities.
159 */
160 public NetworkRequest build() {
Paul Jensen487ffe72015-07-24 15:57:11 -0400161 // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED
162 // when later an unrestricted capability could be added to mNetworkCapabilities, in
163 // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which
164 // maybeMarkCapabilitiesRestricted() doesn't add back.
165 final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities);
166 nc.maybeMarkCapabilitiesRestricted();
167 return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900168 ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700169 }
170
171 /**
172 * Add the given capability requirement to this builder. These represent
173 * the requested network's required capabilities. Note that when searching
174 * for a network to satisfy a request, all capabilities requested must be
Jeff Sharkeyde570312017-10-24 21:25:50 -0600175 * satisfied.
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700176 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600177 * @param capability The capability to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700178 * @return The builder to facilitate chaining
179 * {@code builder.addCapability(...).addCapability();}.
180 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600181 public Builder addCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700182 mNetworkCapabilities.addCapability(capability);
183 return this;
184 }
185
186 /**
Pavel Maltsevbb5b3af2018-04-09 13:10:11 -0700187 * Removes (if found) the given capability from this builder instance.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700188 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600189 * @param capability The capability to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700190 * @return The builder to facilitate chaining.
191 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600192 public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700193 mNetworkCapabilities.removeCapability(capability);
194 return this;
195 }
196
197 /**
Erik Kline35bf06c2017-01-26 18:08:28 +0900198 * Set the {@code NetworkCapabilities} for this builder instance,
199 * overriding any capabilities that had been previously set.
200 *
201 * @param nc The superseding {@code NetworkCapabilities} instance.
202 * @return The builder to facilitate chaining.
203 * @hide
204 */
205 public Builder setCapabilities(NetworkCapabilities nc) {
Chalard Jean4c4bc932018-05-18 23:48:49 +0900206 mNetworkCapabilities.set(nc);
Erik Kline35bf06c2017-01-26 18:08:28 +0900207 return this;
208 }
209
210 /**
Chalard Jean616ede02018-03-09 20:52:15 +0900211 * Set the watched UIDs for this request. This will be reset and wiped out unless
212 * the calling app holds the CHANGE_NETWORK_STATE permission.
213 *
214 * @param uids The watched UIDs as a set of UidRanges, or null for everything.
215 * @return The builder to facilitate chaining.
216 * @hide
217 */
218 public Builder setUids(Set<UidRange> uids) {
219 mNetworkCapabilities.setUids(uids);
220 return this;
221 }
222
223 /**
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800224 * Add a capability that must not exist in the requested network.
225 * <p>
226 * If the capability was previously added to the list of required capabilities (for
227 * example, it was there by default or added using {@link #addCapability(int)} method), then
228 * it will be removed from the list of required capabilities as well.
229 *
230 * @see #addCapability(int)
231 *
232 * @param capability The capability to add to unwanted capability list.
233 * @return The builder to facilitate chaining.
Lorenzo Colitti4c6f4a62018-04-06 17:35:33 +0900234 *
Lorenzo Colitti19c0cca2018-04-20 17:53:29 +0900235 * @hide
Pavel Maltsev1cd48da2018-02-01 11:16:02 -0800236 */
237 public Builder addUnwantedCapability(@NetworkCapabilities.NetCapability int capability) {
238 mNetworkCapabilities.addUnwantedCapability(capability);
239 return this;
240 }
241
242 /**
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900243 * Completely clears all the {@code NetworkCapabilities} from this builder instance,
244 * removing even the capabilities that are set by default when the object is constructed.
245 *
246 * @return The builder to facilitate chaining.
247 * @hide
248 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100249 @UnsupportedAppUsage
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900250 public Builder clearCapabilities() {
251 mNetworkCapabilities.clearAll();
252 return this;
253 }
254
255 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -0700256 * Adds the given transport requirement to this builder. These represent
257 * the set of allowed transports for the request. Only networks using one
258 * of these transports will satisfy the request. If no particular transports
Jeff Sharkeyde570312017-10-24 21:25:50 -0600259 * are required, none should be specified here.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700260 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600261 * @param transportType The transport type to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700262 * @return The builder to facilitate chaining.
263 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600264 public Builder addTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700265 mNetworkCapabilities.addTransportType(transportType);
266 return this;
267 }
268
269 /**
270 * Removes (if found) the given transport from this builder instance.
271 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600272 * @param transportType The transport type to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700273 * @return The builder to facilitate chaining.
274 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600275 public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700276 mNetworkCapabilities.removeTransportType(transportType);
277 return this;
278 }
279
280 /**
281 * @hide
282 */
283 public Builder setLinkUpstreamBandwidthKbps(int upKbps) {
284 mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps);
285 return this;
286 }
287 /**
288 * @hide
289 */
290 public Builder setLinkDownstreamBandwidthKbps(int downKbps) {
291 mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps);
292 return this;
293 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700294
295 /**
296 * Sets the optional bearer specific network specifier.
297 * This has no meaning if a single transport is also not specified, so calling
298 * this without a single transport set will generate an exception, as will
299 * subsequently adding or removing transports after this is set.
300 * </p>
301 * The interpretation of this {@code String} is bearer specific and bearers that use
302 * it should document their particulars. For example, Bluetooth may use some sort of
303 * device id while WiFi could used ssid and/or bssid. Cellular may use carrier spn.
304 *
305 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
306 * specific network specifier where the bearer has a choice of
307 * networks.
308 */
309 public Builder setNetworkSpecifier(String networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -0700310 /*
311 * A StringNetworkSpecifier does not accept null or empty ("") strings. When network
312 * specifiers were strings a null string and an empty string were considered equivalent.
313 * Hence no meaning is attached to a null or empty ("") string.
314 */
315 return setNetworkSpecifier(TextUtils.isEmpty(networkSpecifier) ? null
316 : new StringNetworkSpecifier(networkSpecifier));
317 }
318
319 /**
320 * Sets the optional bearer specific network specifier.
321 * This has no meaning if a single transport is also not specified, so calling
322 * this without a single transport set will generate an exception, as will
323 * subsequently adding or removing transports after this is set.
324 * </p>
325 *
326 * @param networkSpecifier A concrete, parcelable framework class that extends
327 * NetworkSpecifier.
Etan Cohena7434272017-04-03 12:17:51 -0700328 */
329 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
330 MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(networkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700331 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
332 return this;
333 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900334
335 /**
336 * Sets the signal strength. This is a signed integer, with higher values indicating a
337 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same
338 * RSSI units reported by WifiManager.
339 * <p>
340 * Note that when used to register a network callback, this specifies the minimum acceptable
341 * signal strength. When received as the state of an existing network it specifies the
342 * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when
343 * received and has no effect when requesting a callback.
344 *
345 * @param signalStrength the bearer-specific signal strength.
346 * @hide
347 */
paulhu7610bc72018-12-12 17:52:57 +0800348 @SystemApi
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900349 public Builder setSignalStrength(int signalStrength) {
350 mNetworkCapabilities.setSignalStrength(signalStrength);
351 return this;
352 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700353 }
354
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700355 // implement the Parcelable interface
356 public int describeContents() {
357 return 0;
358 }
359 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700360 networkCapabilities.writeToParcel(dest, flags);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700361 dest.writeInt(legacyType);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700362 dest.writeInt(requestId);
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900363 dest.writeString(type.name());
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700364 }
365 public static final Creator<NetworkRequest> CREATOR =
366 new Creator<NetworkRequest>() {
367 public NetworkRequest createFromParcel(Parcel in) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700368 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700369 int legacyType = in.readInt();
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700370 int requestId = in.readInt();
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900371 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
372 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700373 return result;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700374 }
375 public NetworkRequest[] newArray(int size) {
376 return new NetworkRequest[size];
377 }
378 };
379
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900380 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900381 * Returns true iff. this NetworkRequest is of type LISTEN.
Lorenzo Colittif4a45f42016-07-18 18:17:08 +0900382 *
383 * @hide
384 */
385 public boolean isListen() {
386 return type == Type.LISTEN;
387 }
388
389 /**
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900390 * Returns true iff. the contained NetworkRequest is one that:
391 *
392 * - should be associated with at most one satisfying network
393 * at a time;
394 *
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900395 * - should cause a network to be kept up, but not necessarily in
396 * the foreground, if it is the best network which can satisfy the
397 * NetworkRequest.
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900398 *
399 * For full detail of how isRequest() is used for pairing Networks with
400 * NetworkRequests read rematchNetworkAndRequests().
401 *
402 * @hide
403 */
404 public boolean isRequest() {
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900405 return isForegroundRequest() || isBackgroundRequest();
406 }
407
408 /**
409 * Returns true iff. the contained NetworkRequest is one that:
410 *
411 * - should be associated with at most one satisfying network
412 * at a time;
413 *
414 * - should cause a network to be kept up and in the foreground if
415 * it is the best network which can satisfy the NetworkRequest.
416 *
417 * For full detail of how isRequest() is used for pairing Networks with
418 * NetworkRequests read rematchNetworkAndRequests().
419 *
420 * @hide
421 */
422 public boolean isForegroundRequest() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900423 return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
424 }
425
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900426 /**
427 * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST.
428 *
429 * @hide
430 */
431 public boolean isBackgroundRequest() {
432 return type == Type.BACKGROUND_REQUEST;
433 }
434
Jeff Sharkeyd778da32018-03-28 14:01:55 -0600435 /**
436 * @see Builder#addCapability(int)
437 */
438 public boolean hasCapability(@NetCapability int capability) {
439 return networkCapabilities.hasCapability(capability);
440 }
441
442 /**
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700443 * @see Builder#addUnwantedCapability(int)
Lorenzo Colitti4c6f4a62018-04-06 17:35:33 +0900444 *
Lorenzo Colitti19c0cca2018-04-20 17:53:29 +0900445 * @hide
Pavel Maltsev67d3dfd2018-04-02 11:10:13 -0700446 */
447 public boolean hasUnwantedCapability(@NetCapability int capability) {
448 return networkCapabilities.hasUnwantedCapability(capability);
449 }
450
451 /**
Jeff Sharkeyd778da32018-03-28 14:01:55 -0600452 * @see Builder#addTransportType(int)
453 */
454 public boolean hasTransport(@Transport int transportType) {
455 return networkCapabilities.hasTransport(transportType);
456 }
457
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700458 public String toString() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900459 return "NetworkRequest [ " + type + " id=" + requestId +
460 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
Robert Greenwalt7b816022014-04-18 15:25:25 -0700461 ", " + networkCapabilities.toString() + " ]";
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700462 }
463
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800464 private int typeToProtoEnum(Type t) {
465 switch (t) {
466 case NONE:
467 return NetworkRequestProto.TYPE_NONE;
468 case LISTEN:
469 return NetworkRequestProto.TYPE_LISTEN;
470 case TRACK_DEFAULT:
471 return NetworkRequestProto.TYPE_TRACK_DEFAULT;
472 case REQUEST:
473 return NetworkRequestProto.TYPE_REQUEST;
474 case BACKGROUND_REQUEST:
475 return NetworkRequestProto.TYPE_BACKGROUND_REQUEST;
476 default:
477 return NetworkRequestProto.TYPE_UNKNOWN;
478 }
479 }
480
481 /** @hide */
482 public void writeToProto(ProtoOutputStream proto, long fieldId) {
483 final long token = proto.start(fieldId);
484
485 proto.write(NetworkRequestProto.TYPE, typeToProtoEnum(type));
486 proto.write(NetworkRequestProto.REQUEST_ID, requestId);
487 proto.write(NetworkRequestProto.LEGACY_TYPE, legacyType);
488 networkCapabilities.writeToProto(proto, NetworkRequestProto.NETWORK_CAPABILITIES);
489
490 proto.end(token);
491 }
492
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700493 public boolean equals(Object obj) {
494 if (obj instanceof NetworkRequest == false) return false;
495 NetworkRequest that = (NetworkRequest)obj;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700496 return (that.legacyType == this.legacyType &&
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700497 that.requestId == this.requestId &&
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900498 that.type == this.type &&
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900499 Objects.equals(that.networkCapabilities, this.networkCapabilities));
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700500 }
501
502 public int hashCode() {
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900503 return Objects.hash(requestId, legacyType, networkCapabilities, type);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700504 }
505}