2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.os.Parcel;
20 import android.os.Parcelable;
23 * Defines a request for a network, made through {@link NetworkRequest.Builder} and used
24 * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes
25 * via {@link ConnectivityManager#registerNetworkCallback}.
27 public class NetworkRequest implements Parcelable {
29 * The {@link NetworkCapabilities} that define this request.
32 public final NetworkCapabilities networkCapabilities;
35 * Identifies the request. NetworkRequests should only be constructed by
36 * the Framework and given out to applications as tokens to be used to identify
40 public final int requestId;
43 * Set for legacy requests and the default. Set to TYPE_NONE for none.
44 * Causes CONNECTIVITY_ACTION broadcasts to be sent.
47 public final int legacyType;
52 public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId) {
54 throw new NullPointerException();
57 networkCapabilities = nc;
58 this.legacyType = legacyType;
64 public NetworkRequest(NetworkRequest that) {
65 networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
66 requestId = that.requestId;
67 this.legacyType = that.legacyType;
71 * Builder used to create {@link NetworkRequest} objects. Specify the Network features
72 * needed in terms of {@link NetworkCapabilities} features
74 public static class Builder {
75 private final NetworkCapabilities mNetworkCapabilities = new NetworkCapabilities();
78 * Default constructor for Builder.
83 * Build {@link NetworkRequest} give the current set of capabilities.
85 public NetworkRequest build() {
86 // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED
87 // when later an unrestricted capability could be added to mNetworkCapabilities, in
88 // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which
89 // maybeMarkCapabilitiesRestricted() doesn't add back.
90 final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities);
91 nc.maybeMarkCapabilitiesRestricted();
92 return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE,
93 ConnectivityManager.REQUEST_ID_UNSET);
97 * Add the given capability requirement to this builder. These represent
98 * the requested network's required capabilities. Note that when searching
99 * for a network to satisfy a request, all capabilities requested must be
100 * satisfied. See {@link NetworkCapabilities} for {@code NET_CAPABILITIY_*}
103 * @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to add.
104 * @return The builder to facilitate chaining
105 * {@code builder.addCapability(...).addCapability();}.
107 public Builder addCapability(int capability) {
108 mNetworkCapabilities.addCapability(capability);
113 * Removes (if found) the given capability from this builder instance.
115 * @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to remove.
116 * @return The builder to facilitate chaining.
118 public Builder removeCapability(int capability) {
119 mNetworkCapabilities.removeCapability(capability);
124 * Completely clears all the {@code NetworkCapabilities} from this builder instance,
125 * removing even the capabilities that are set by default when the object is constructed.
127 * @return The builder to facilitate chaining.
130 public Builder clearCapabilities() {
131 mNetworkCapabilities.clearAll();
136 * Adds the given transport requirement to this builder. These represent
137 * the set of allowed transports for the request. Only networks using one
138 * of these transports will satisfy the request. If no particular transports
139 * are required, none should be specified here. See {@link NetworkCapabilities}
140 * for {@code TRANSPORT_*} definitions.
142 * @param transportType The {@code NetworkCapabilities.TRANSPORT_*} to add.
143 * @return The builder to facilitate chaining.
145 public Builder addTransportType(int transportType) {
146 mNetworkCapabilities.addTransportType(transportType);
151 * Removes (if found) the given transport from this builder instance.
153 * @param transportType The {@code NetworkCapabilities.TRANSPORT_*} to remove.
154 * @return The builder to facilitate chaining.
156 public Builder removeTransportType(int transportType) {
157 mNetworkCapabilities.removeTransportType(transportType);
164 public Builder setLinkUpstreamBandwidthKbps(int upKbps) {
165 mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps);
171 public Builder setLinkDownstreamBandwidthKbps(int downKbps) {
172 mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps);
177 * Sets the optional bearer specific network specifier.
178 * This has no meaning if a single transport is also not specified, so calling
179 * this without a single transport set will generate an exception, as will
180 * subsequently adding or removing transports after this is set.
182 * The interpretation of this {@code String} is bearer specific and bearers that use
183 * it should document their particulars. For example, Bluetooth may use some sort of
184 * device id while WiFi could used ssid and/or bssid. Cellular may use carrier spn.
186 * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
187 * specific network specifier where the bearer has a choice of
190 public Builder setNetworkSpecifier(String networkSpecifier) {
191 if (NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER.equals(networkSpecifier)) {
192 throw new IllegalArgumentException("Invalid network specifier - must not be '"
193 + NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER + "'");
195 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
200 * Sets the signal strength. This is a signed integer, with higher values indicating a
201 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same
202 * RSSI units reported by WifiManager.
204 * Note that when used to register a network callback, this specifies the minimum acceptable
205 * signal strength. When received as the state of an existing network it specifies the
206 * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when
207 * received and has no effect when requesting a callback.
209 * @param signalStrength the bearer-specific signal strength.
212 public Builder setSignalStrength(int signalStrength) {
213 mNetworkCapabilities.setSignalStrength(signalStrength);
218 // implement the Parcelable interface
219 public int describeContents() {
222 public void writeToParcel(Parcel dest, int flags) {
223 dest.writeParcelable(networkCapabilities, flags);
224 dest.writeInt(legacyType);
225 dest.writeInt(requestId);
227 public static final Creator<NetworkRequest> CREATOR =
228 new Creator<NetworkRequest>() {
229 public NetworkRequest createFromParcel(Parcel in) {
230 NetworkCapabilities nc = (NetworkCapabilities)in.readParcelable(null);
231 int legacyType = in.readInt();
232 int requestId = in.readInt();
233 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId);
236 public NetworkRequest[] newArray(int size) {
237 return new NetworkRequest[size];
241 public String toString() {
242 return "NetworkRequest [ id=" + requestId + ", legacyType=" + legacyType +
243 ", " + networkCapabilities.toString() + " ]";
246 public boolean equals(Object obj) {
247 if (obj instanceof NetworkRequest == false) return false;
248 NetworkRequest that = (NetworkRequest)obj;
249 return (that.legacyType == this.legacyType &&
250 that.requestId == this.requestId &&
251 ((that.networkCapabilities == null && this.networkCapabilities == null) ||
252 (that.networkCapabilities != null &&
253 that.networkCapabilities.equals(this.networkCapabilities))));
256 public int hashCode() {
257 return requestId + (legacyType * 1013) +
258 (networkCapabilities.hashCode() * 1051);