blob: a0724091abeeca7e41e59ecf8a5dc161eb424f95 [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;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070020import android.os.Parcel;
21import android.os.Parcelable;
Etan Cohena7434272017-04-03 12:17:51 -070022import android.text.TextUtils;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080023import android.util.proto.ProtoOutputStream;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070024
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090025import java.util.Objects;
26
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070027/**
Robert Greenwalt7569f182014-06-08 16:42:59 -070028 * 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 Greenwalt6078b502014-06-11 16:05:07 -070030 * via {@link ConnectivityManager#registerNetworkCallback}.
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070031 */
32public class NetworkRequest implements Parcelable {
Robert Greenwalt7b816022014-04-18 15:25:25 -070033 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -070034 * The {@link NetworkCapabilities} that define this request.
35 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070036 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -070037 public final @NonNull NetworkCapabilities networkCapabilities;
Robert Greenwalt7b816022014-04-18 15:25:25 -070038
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 Greenwalt34524f02014-05-18 16:22:10 -070043 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070044 */
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070045 public final int requestId;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070046
Robert Greenwalt7b816022014-04-18 15:25:25 -070047 /**
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070048 * Set for legacy requests and the default. Set to TYPE_NONE for none.
Robert Greenwalt7b816022014-04-18 15:25:25 -070049 * Causes CONNECTIVITY_ACTION broadcasts to be sent.
50 * @hide
51 */
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070052 public final int legacyType;
Robert Greenwalt7b816022014-04-18 15:25:25 -070053
Robert Greenwalt7b816022014-04-18 15:25:25 -070054 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090055 * A NetworkRequest as used by the system can be one of the following types:
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090056 *
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 Colitti3d4a1062016-09-09 18:48:56 +090070 * 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 Colittib35d40d2016-07-01 13:19:21 +090084 *
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 Greenwalt7b816022014-04-18 15:25:25 -070090 * @hide
91 */
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090092 public static enum Type {
93 NONE,
94 LISTEN,
95 TRACK_DEFAULT,
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090096 REQUEST,
97 BACKGROUND_REQUEST,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090098 };
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 Greenwaltf5b74f92014-09-03 19:35:47 -0700111 if (nc == null) {
112 throw new NullPointerException();
113 }
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700114 requestId = rId;
115 networkCapabilities = nc;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700116 this.legacyType = legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900117 this.type = type;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700118 }
119
Robert Greenwalt34524f02014-05-18 16:22:10 -0700120 /**
121 * @hide
122 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700123 public NetworkRequest(NetworkRequest that) {
124 networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
125 requestId = that.requestId;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700126 this.legacyType = that.legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900127 this.type = that.type;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700128 }
129
Robert Greenwalt7569f182014-06-08 16:42:59 -0700130 /**
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 Jensen487ffe72015-07-24 15:57:11 -0400146 // 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 Colittib35d40d2016-07-01 13:19:21 +0900153 ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700154 }
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 Sharkeyde570312017-10-24 21:25:50 -0600160 * satisfied.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700161 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600162 * @param capability The capability to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700163 * @return The builder to facilitate chaining
164 * {@code builder.addCapability(...).addCapability();}.
165 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600166 public Builder addCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700167 mNetworkCapabilities.addCapability(capability);
168 return this;
169 }
170
171 /**
172 * Removes (if found) the given capability from this builder instance.
173 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600174 * @param capability The capability to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700175 * @return The builder to facilitate chaining.
176 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600177 public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700178 mNetworkCapabilities.removeCapability(capability);
179 return this;
180 }
181
182 /**
Erik Kline35bf06c2017-01-26 18:08:28 +0900183 * 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 Colitti84b83c52015-05-19 23:31:49 +0900197 * 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 Greenwalt7569f182014-06-08 16:42:59 -0700209 * 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 Sharkeyde570312017-10-24 21:25:50 -0600212 * are required, none should be specified here.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700213 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600214 * @param transportType The transport type to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700215 * @return The builder to facilitate chaining.
216 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600217 public Builder addTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700218 mNetworkCapabilities.addTransportType(transportType);
219 return this;
220 }
221
222 /**
223 * Removes (if found) the given transport from this builder instance.
224 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600225 * @param transportType The transport type to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700226 * @return The builder to facilitate chaining.
227 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600228 public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700229 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 Greenwalt5f90bcc2014-07-09 17:25:41 -0700247
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 Cohena7434272017-04-03 12:17:51 -0700263 /*
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 Cohena7434272017-04-03 12:17:51 -0700281 */
282 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
283 MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(networkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700284 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
285 return this;
286 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900287
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 Greenwalt7569f182014-06-08 16:42:59 -0700305 }
306
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700307 // implement the Parcelable interface
308 public int describeContents() {
309 return 0;
310 }
311 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700312 networkCapabilities.writeToParcel(dest, flags);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700313 dest.writeInt(legacyType);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700314 dest.writeInt(requestId);
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900315 dest.writeString(type.name());
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700316 }
317 public static final Creator<NetworkRequest> CREATOR =
318 new Creator<NetworkRequest>() {
319 public NetworkRequest createFromParcel(Parcel in) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700320 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700321 int legacyType = in.readInt();
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700322 int requestId = in.readInt();
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900323 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
324 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700325 return result;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700326 }
327 public NetworkRequest[] newArray(int size) {
328 return new NetworkRequest[size];
329 }
330 };
331
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900332 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900333 * Returns true iff. this NetworkRequest is of type LISTEN.
Lorenzo Colittif4a45f42016-07-18 18:17:08 +0900334 *
335 * @hide
336 */
337 public boolean isListen() {
338 return type == Type.LISTEN;
339 }
340
341 /**
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900342 * 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 Colitti3d4a1062016-09-09 18:48:56 +0900347 * - 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 Colittib35d40d2016-07-01 13:19:21 +0900350 *
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 Colitti3d4a1062016-09-09 18:48:56 +0900357 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 Colittib35d40d2016-07-01 13:19:21 +0900375 return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
376 }
377
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900378 /**
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 Greenwalt3c0bf5e2014-04-11 15:08:03 -0700387 public String toString() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900388 return "NetworkRequest [ " + type + " id=" + requestId +
389 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
Robert Greenwalt7b816022014-04-18 15:25:25 -0700390 ", " + networkCapabilities.toString() + " ]";
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700391 }
392
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800393 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 Greenwalt3c0bf5e2014-04-11 15:08:03 -0700422 public boolean equals(Object obj) {
423 if (obj instanceof NetworkRequest == false) return false;
424 NetworkRequest that = (NetworkRequest)obj;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700425 return (that.legacyType == this.legacyType &&
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700426 that.requestId == this.requestId &&
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900427 that.type == this.type &&
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900428 Objects.equals(that.networkCapabilities, this.networkCapabilities));
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700429 }
430
431 public int hashCode() {
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900432 return Objects.hash(requestId, legacyType, networkCapabilities, type);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700433 }
434}