blob: 97ded2d73b6022fc2dbaf7c37a67f13b2e2d3ebc [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;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070023
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +090024import java.util.Objects;
25
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070026/**
Robert Greenwalt7569f182014-06-08 16:42:59 -070027 * Defines a request for a network, made through {@link NetworkRequest.Builder} and used
28 * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes
Robert Greenwalt6078b502014-06-11 16:05:07 -070029 * via {@link ConnectivityManager#registerNetworkCallback}.
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070030 */
31public class NetworkRequest implements Parcelable {
Robert Greenwalt7b816022014-04-18 15:25:25 -070032 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -070033 * The {@link NetworkCapabilities} that define this request.
34 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070035 */
Jeff Sharkey49bcd602017-11-09 13:11:50 -070036 public final @NonNull NetworkCapabilities networkCapabilities;
Robert Greenwalt7b816022014-04-18 15:25:25 -070037
38 /**
39 * Identifies the request. NetworkRequests should only be constructed by
40 * the Framework and given out to applications as tokens to be used to identify
41 * the request.
Robert Greenwalt34524f02014-05-18 16:22:10 -070042 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -070043 */
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070044 public final int requestId;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -070045
Robert Greenwalt7b816022014-04-18 15:25:25 -070046 /**
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070047 * Set for legacy requests and the default. Set to TYPE_NONE for none.
Robert Greenwalt7b816022014-04-18 15:25:25 -070048 * Causes CONNECTIVITY_ACTION broadcasts to be sent.
49 * @hide
50 */
Robert Greenwalt32aa65a2014-06-02 15:32:02 -070051 public final int legacyType;
Robert Greenwalt7b816022014-04-18 15:25:25 -070052
Robert Greenwalt7b816022014-04-18 15:25:25 -070053 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090054 * A NetworkRequest as used by the system can be one of the following types:
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090055 *
56 * - LISTEN, for which the framework will issue callbacks about any
57 * and all networks that match the specified NetworkCapabilities,
58 *
59 * - REQUEST, capable of causing a specific network to be created
60 * first (e.g. a telephony DUN request), the framework will issue
61 * callbacks about the single, highest scoring current network
62 * (if any) that matches the specified NetworkCapabilities, or
63 *
64 * - TRACK_DEFAULT, a hybrid of the two designed such that the
65 * framework will issue callbacks for the single, highest scoring
66 * current network (if any) that matches the capabilities of the
67 * default Internet request (mDefaultRequest), but which cannot cause
68 * the framework to either create or retain the existence of any
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090069 * specific network. Note that from the point of view of the request
70 * matching code, TRACK_DEFAULT is identical to REQUEST: its special
71 * behaviour is not due to different semantics, but to the fact that
72 * the system will only ever create a TRACK_DEFAULT with capabilities
73 * that are identical to the default request's capabilities, thus
74 * causing it to share fate in every way with the default request.
75 *
76 * - BACKGROUND_REQUEST, like REQUEST but does not cause any networks
77 * to retain the NET_CAPABILITY_FOREGROUND capability. A network with
78 * no foreground requests is in the background. A network that has
79 * one or more background requests and loses its last foreground
80 * request to a higher-scoring network will not go into the
81 * background immediately, but will linger and go into the background
82 * after the linger timeout.
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090083 *
84 * - The value NONE is used only by applications. When an application
85 * creates a NetworkRequest, it does not have a type; the type is set
86 * by the system depending on the method used to file the request
87 * (requestNetwork, registerNetworkCallback, etc.).
88 *
Robert Greenwalt7b816022014-04-18 15:25:25 -070089 * @hide
90 */
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090091 public static enum Type {
92 NONE,
93 LISTEN,
94 TRACK_DEFAULT,
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +090095 REQUEST,
96 BACKGROUND_REQUEST,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +090097 };
98
99 /**
100 * The type of the request. This is only used by the system and is always NONE elsewhere.
101 *
102 * @hide
103 */
104 public final Type type;
105
106 /**
107 * @hide
108 */
109 public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type) {
Robert Greenwaltf5b74f92014-09-03 19:35:47 -0700110 if (nc == null) {
111 throw new NullPointerException();
112 }
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700113 requestId = rId;
114 networkCapabilities = nc;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700115 this.legacyType = legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900116 this.type = type;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700117 }
118
Robert Greenwalt34524f02014-05-18 16:22:10 -0700119 /**
120 * @hide
121 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700122 public NetworkRequest(NetworkRequest that) {
123 networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
124 requestId = that.requestId;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700125 this.legacyType = that.legacyType;
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900126 this.type = that.type;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700127 }
128
Robert Greenwalt7569f182014-06-08 16:42:59 -0700129 /**
130 * Builder used to create {@link NetworkRequest} objects. Specify the Network features
131 * needed in terms of {@link NetworkCapabilities} features
132 */
133 public static class Builder {
134 private final NetworkCapabilities mNetworkCapabilities = new NetworkCapabilities();
135
136 /**
137 * Default constructor for Builder.
138 */
139 public Builder() {}
140
141 /**
142 * Build {@link NetworkRequest} give the current set of capabilities.
143 */
144 public NetworkRequest build() {
Paul Jensen487ffe72015-07-24 15:57:11 -0400145 // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED
146 // when later an unrestricted capability could be added to mNetworkCapabilities, in
147 // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which
148 // maybeMarkCapabilitiesRestricted() doesn't add back.
149 final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities);
150 nc.maybeMarkCapabilitiesRestricted();
151 return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE,
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900152 ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700153 }
154
155 /**
156 * Add the given capability requirement to this builder. These represent
157 * the requested network's required capabilities. Note that when searching
158 * for a network to satisfy a request, all capabilities requested must be
Jeff Sharkeyde570312017-10-24 21:25:50 -0600159 * satisfied.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700160 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600161 * @param capability The capability to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700162 * @return The builder to facilitate chaining
163 * {@code builder.addCapability(...).addCapability();}.
164 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600165 public Builder addCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700166 mNetworkCapabilities.addCapability(capability);
167 return this;
168 }
169
170 /**
171 * Removes (if found) the given capability from this builder instance.
172 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600173 * @param capability The capability to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700174 * @return The builder to facilitate chaining.
175 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600176 public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700177 mNetworkCapabilities.removeCapability(capability);
178 return this;
179 }
180
181 /**
Erik Kline35bf06c2017-01-26 18:08:28 +0900182 * Set the {@code NetworkCapabilities} for this builder instance,
183 * overriding any capabilities that had been previously set.
184 *
185 * @param nc The superseding {@code NetworkCapabilities} instance.
186 * @return The builder to facilitate chaining.
187 * @hide
188 */
189 public Builder setCapabilities(NetworkCapabilities nc) {
190 mNetworkCapabilities.clearAll();
191 mNetworkCapabilities.combineCapabilities(nc);
192 return this;
193 }
194
195 /**
Lorenzo Colitti84b83c52015-05-19 23:31:49 +0900196 * Completely clears all the {@code NetworkCapabilities} from this builder instance,
197 * removing even the capabilities that are set by default when the object is constructed.
198 *
199 * @return The builder to facilitate chaining.
200 * @hide
201 */
202 public Builder clearCapabilities() {
203 mNetworkCapabilities.clearAll();
204 return this;
205 }
206
207 /**
Robert Greenwalt7569f182014-06-08 16:42:59 -0700208 * Adds the given transport requirement to this builder. These represent
209 * the set of allowed transports for the request. Only networks using one
210 * of these transports will satisfy the request. If no particular transports
Jeff Sharkeyde570312017-10-24 21:25:50 -0600211 * are required, none should be specified here.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700212 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600213 * @param transportType The transport type to add.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700214 * @return The builder to facilitate chaining.
215 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600216 public Builder addTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700217 mNetworkCapabilities.addTransportType(transportType);
218 return this;
219 }
220
221 /**
222 * Removes (if found) the given transport from this builder instance.
223 *
Jeff Sharkeyde570312017-10-24 21:25:50 -0600224 * @param transportType The transport type to remove.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700225 * @return The builder to facilitate chaining.
226 */
Jeff Sharkeyde570312017-10-24 21:25:50 -0600227 public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700228 mNetworkCapabilities.removeTransportType(transportType);
229 return this;
230 }
231
232 /**
233 * @hide
234 */
235 public Builder setLinkUpstreamBandwidthKbps(int upKbps) {
236 mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps);
237 return this;
238 }
239 /**
240 * @hide
241 */
242 public Builder setLinkDownstreamBandwidthKbps(int downKbps) {
243 mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps);
244 return this;
245 }
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700246
247 /**
248 * Sets the optional bearer specific network specifier.
249 * This has no meaning if a single transport is also not specified, so calling
250 * this without a single transport set will generate an exception, as will
251 * subsequently adding or removing transports after this is set.
252 * </p>
253 * The interpretation of this {@code String} is bearer specific and bearers that use
254 * it should document their particulars. For example, Bluetooth may use some sort of
255 * device id while WiFi could used ssid and/or bssid. Cellular may use carrier spn.
256 *
257 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
258 * specific network specifier where the bearer has a choice of
259 * networks.
260 */
261 public Builder setNetworkSpecifier(String networkSpecifier) {
Etan Cohena7434272017-04-03 12:17:51 -0700262 /*
263 * A StringNetworkSpecifier does not accept null or empty ("") strings. When network
264 * specifiers were strings a null string and an empty string were considered equivalent.
265 * Hence no meaning is attached to a null or empty ("") string.
266 */
267 return setNetworkSpecifier(TextUtils.isEmpty(networkSpecifier) ? null
268 : new StringNetworkSpecifier(networkSpecifier));
269 }
270
271 /**
272 * Sets the optional bearer specific network specifier.
273 * This has no meaning if a single transport is also not specified, so calling
274 * this without a single transport set will generate an exception, as will
275 * subsequently adding or removing transports after this is set.
276 * </p>
277 *
278 * @param networkSpecifier A concrete, parcelable framework class that extends
279 * NetworkSpecifier.
Etan Cohena7434272017-04-03 12:17:51 -0700280 */
281 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) {
282 MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(networkSpecifier);
Robert Greenwalt5f90bcc2014-07-09 17:25:41 -0700283 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
284 return this;
285 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900286
287 /**
288 * Sets the signal strength. This is a signed integer, with higher values indicating a
289 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same
290 * RSSI units reported by WifiManager.
291 * <p>
292 * Note that when used to register a network callback, this specifies the minimum acceptable
293 * signal strength. When received as the state of an existing network it specifies the
294 * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when
295 * received and has no effect when requesting a callback.
296 *
297 * @param signalStrength the bearer-specific signal strength.
298 * @hide
299 */
300 public Builder setSignalStrength(int signalStrength) {
301 mNetworkCapabilities.setSignalStrength(signalStrength);
302 return this;
303 }
Robert Greenwalt7569f182014-06-08 16:42:59 -0700304 }
305
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700306 // implement the Parcelable interface
307 public int describeContents() {
308 return 0;
309 }
310 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700311 networkCapabilities.writeToParcel(dest, flags);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700312 dest.writeInt(legacyType);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700313 dest.writeInt(requestId);
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900314 dest.writeString(type.name());
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700315 }
316 public static final Creator<NetworkRequest> CREATOR =
317 new Creator<NetworkRequest>() {
318 public NetworkRequest createFromParcel(Parcel in) {
Jeff Sharkey49bcd602017-11-09 13:11:50 -0700319 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in);
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700320 int legacyType = in.readInt();
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700321 int requestId = in.readInt();
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900322 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
323 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700324 return result;
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700325 }
326 public NetworkRequest[] newArray(int size) {
327 return new NetworkRequest[size];
328 }
329 };
330
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900331 /**
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900332 * Returns true iff. this NetworkRequest is of type LISTEN.
Lorenzo Colittif4a45f42016-07-18 18:17:08 +0900333 *
334 * @hide
335 */
336 public boolean isListen() {
337 return type == Type.LISTEN;
338 }
339
340 /**
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900341 * Returns true iff. the contained NetworkRequest is one that:
342 *
343 * - should be associated with at most one satisfying network
344 * at a time;
345 *
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900346 * - should cause a network to be kept up, but not necessarily in
347 * the foreground, if it is the best network which can satisfy the
348 * NetworkRequest.
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900349 *
350 * For full detail of how isRequest() is used for pairing Networks with
351 * NetworkRequests read rematchNetworkAndRequests().
352 *
353 * @hide
354 */
355 public boolean isRequest() {
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900356 return isForegroundRequest() || isBackgroundRequest();
357 }
358
359 /**
360 * Returns true iff. the contained NetworkRequest is one that:
361 *
362 * - should be associated with at most one satisfying network
363 * at a time;
364 *
365 * - should cause a network to be kept up and in the foreground if
366 * it is the best network which can satisfy the NetworkRequest.
367 *
368 * For full detail of how isRequest() is used for pairing Networks with
369 * NetworkRequests read rematchNetworkAndRequests().
370 *
371 * @hide
372 */
373 public boolean isForegroundRequest() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900374 return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
375 }
376
Lorenzo Colitti3d4a1062016-09-09 18:48:56 +0900377 /**
378 * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST.
379 *
380 * @hide
381 */
382 public boolean isBackgroundRequest() {
383 return type == Type.BACKGROUND_REQUEST;
384 }
385
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700386 public String toString() {
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900387 return "NetworkRequest [ " + type + " id=" + requestId +
388 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
Robert Greenwalt7b816022014-04-18 15:25:25 -0700389 ", " + networkCapabilities.toString() + " ]";
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700390 }
391
392 public boolean equals(Object obj) {
393 if (obj instanceof NetworkRequest == false) return false;
394 NetworkRequest that = (NetworkRequest)obj;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700395 return (that.legacyType == this.legacyType &&
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700396 that.requestId == this.requestId &&
Lorenzo Colittib35d40d2016-07-01 13:19:21 +0900397 that.type == this.type &&
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900398 Objects.equals(that.networkCapabilities, this.networkCapabilities));
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700399 }
400
401 public int hashCode() {
Lorenzo Colitti1ec11eb2016-07-05 01:22:13 +0900402 return Objects.hash(requestId, legacyType, networkCapabilities, type);
Robert Greenwalt3c0bf5e2014-04-11 15:08:03 -0700403 }
404}