1 /* 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 17 package android.net; 18 19 import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; 20 import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN; 21 import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND; 22 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; 23 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED; 24 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; 25 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; 26 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; 27 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED; 28 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED; 29 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; 30 import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY; 31 import static android.net.NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED; 32 import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; 33 import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; 34 import static android.net.NetworkCapabilities.TRANSPORT_TEST; 35 36 import android.annotation.NonNull; 37 import android.annotation.Nullable; 38 import android.annotation.RequiresPermission; 39 import android.annotation.SuppressLint; 40 import android.annotation.SystemApi; 41 import android.compat.annotation.UnsupportedAppUsage; 42 import android.net.NetworkCapabilities.NetCapability; 43 import android.net.NetworkCapabilities.Transport; 44 import android.os.Build; 45 import android.os.Parcel; 46 import android.os.Parcelable; 47 import android.os.Process; 48 import android.text.TextUtils; 49 import android.util.Range; 50 51 import java.util.Arrays; 52 import java.util.List; 53 import java.util.Objects; 54 import java.util.Set; 55 56 /** 57 * Defines a request for a network, made through {@link NetworkRequest.Builder} and used 58 * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes 59 * via {@link ConnectivityManager#registerNetworkCallback}. 60 */ 61 public class NetworkRequest implements Parcelable { 62 /** 63 * The first requestId value that will be allocated. 64 * @hide only used by ConnectivityService. 65 */ 66 public static final int FIRST_REQUEST_ID = 1; 67 68 /** 69 * The requestId value that represents the absence of a request. 70 * @hide only used by ConnectivityService. 71 */ 72 public static final int REQUEST_ID_NONE = -1; 73 74 /** 75 * The {@link NetworkCapabilities} that define this request. 76 * @hide 77 */ 78 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 79 public final @NonNull NetworkCapabilities networkCapabilities; 80 81 /** 82 * Identifies the request. NetworkRequests should only be constructed by 83 * the Framework and given out to applications as tokens to be used to identify 84 * the request. 85 * @hide 86 */ 87 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 88 public final int requestId; 89 90 /** 91 * Set for legacy requests and the default. Set to TYPE_NONE for none. 92 * Causes CONNECTIVITY_ACTION broadcasts to be sent. 93 * @hide 94 */ 95 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 96 public final int legacyType; 97 98 /** 99 * A NetworkRequest as used by the system can be one of the following types: 100 * 101 * - LISTEN, for which the framework will issue callbacks about any 102 * and all networks that match the specified NetworkCapabilities, 103 * 104 * - REQUEST, capable of causing a specific network to be created 105 * first (e.g. a telephony DUN request), the framework will issue 106 * callbacks about the single, highest scoring current network 107 * (if any) that matches the specified NetworkCapabilities, or 108 * 109 * - TRACK_DEFAULT, which causes the framework to issue callbacks for 110 * the single, highest scoring current network (if any) that will 111 * be chosen for an app, but which cannot cause the framework to 112 * either create or retain the existence of any specific network. 113 * 114 * - TRACK_SYSTEM_DEFAULT, which causes the framework to send callbacks 115 * for the network (if any) that satisfies the default Internet 116 * request. 117 * 118 * - TRACK_BEST, which causes the framework to send callbacks about 119 * the single, highest scoring current network (if any) that matches 120 * the specified NetworkCapabilities. 121 * 122 * - BACKGROUND_REQUEST, like REQUEST but does not cause any networks 123 * to retain the NET_CAPABILITY_FOREGROUND capability. A network with 124 * no foreground requests is in the background. A network that has 125 * one or more background requests and loses its last foreground 126 * request to a higher-scoring network will not go into the 127 * background immediately, but will linger and go into the background 128 * after the linger timeout. 129 * 130 * - The value NONE is used only by applications. When an application 131 * creates a NetworkRequest, it does not have a type; the type is set 132 * by the system depending on the method used to file the request 133 * (requestNetwork, registerNetworkCallback, etc.). 134 * 135 * @hide 136 */ 137 public static enum Type { 138 NONE, 139 LISTEN, 140 TRACK_DEFAULT, 141 REQUEST, 142 BACKGROUND_REQUEST, 143 TRACK_SYSTEM_DEFAULT, 144 LISTEN_FOR_BEST, 145 }; 146 147 /** 148 * The type of the request. This is only used by the system and is always NONE elsewhere. 149 * 150 * @hide 151 */ 152 public final Type type; 153 154 /** 155 * @hide 156 */ NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type)157 public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type) { 158 if (nc == null) { 159 throw new NullPointerException(); 160 } 161 requestId = rId; 162 networkCapabilities = nc; 163 this.legacyType = legacyType; 164 this.type = type; 165 } 166 167 /** 168 * @hide 169 */ NetworkRequest(NetworkRequest that)170 public NetworkRequest(NetworkRequest that) { 171 networkCapabilities = new NetworkCapabilities(that.networkCapabilities); 172 requestId = that.requestId; 173 this.legacyType = that.legacyType; 174 this.type = that.type; 175 } 176 177 /** 178 * Builder used to create {@link NetworkRequest} objects. Specify the Network features 179 * needed in terms of {@link NetworkCapabilities} features 180 */ 181 public static class Builder { 182 /** 183 * Capabilities that are currently compatible with VCN networks. 184 */ 185 private static final List<Integer> VCN_SUPPORTED_CAPABILITIES = Arrays.asList( 186 NET_CAPABILITY_CAPTIVE_PORTAL, 187 NET_CAPABILITY_DUN, 188 NET_CAPABILITY_FOREGROUND, 189 NET_CAPABILITY_INTERNET, 190 NET_CAPABILITY_NOT_CONGESTED, 191 NET_CAPABILITY_NOT_METERED, 192 NET_CAPABILITY_NOT_RESTRICTED, 193 NET_CAPABILITY_NOT_ROAMING, 194 NET_CAPABILITY_NOT_SUSPENDED, 195 NET_CAPABILITY_NOT_VPN, 196 NET_CAPABILITY_PARTIAL_CONNECTIVITY, 197 NET_CAPABILITY_TEMPORARILY_NOT_METERED, 198 NET_CAPABILITY_TRUSTED, 199 NET_CAPABILITY_VALIDATED); 200 201 private final NetworkCapabilities mNetworkCapabilities; 202 203 // A boolean that represents whether the NOT_VCN_MANAGED capability should be deduced when 204 // the NetworkRequest object is built. 205 private boolean mShouldDeduceNotVcnManaged = true; 206 207 /** 208 * Default constructor for Builder. 209 */ Builder()210 public Builder() { 211 // By default, restrict this request to networks available to this app. 212 // Apps can rescind this restriction, but ConnectivityService will enforce 213 // it for apps that do not have the NETWORK_SETTINGS permission. 214 mNetworkCapabilities = new NetworkCapabilities(); 215 mNetworkCapabilities.setSingleUid(Process.myUid()); 216 } 217 218 /** 219 * Creates a new Builder of NetworkRequest from an existing instance. 220 */ Builder(@onNull final NetworkRequest request)221 public Builder(@NonNull final NetworkRequest request) { 222 Objects.requireNonNull(request); 223 mNetworkCapabilities = request.networkCapabilities; 224 // If the caller constructed the builder from a request, it means the user 225 // might explicitly want the capabilities from the request. Thus, the NOT_VCN_MANAGED 226 // capabilities should not be touched later. 227 mShouldDeduceNotVcnManaged = false; 228 } 229 230 /** 231 * Build {@link NetworkRequest} give the current set of capabilities. 232 */ build()233 public NetworkRequest build() { 234 // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED 235 // when later an unrestricted capability could be added to mNetworkCapabilities, in 236 // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which 237 // maybeMarkCapabilitiesRestricted() doesn't add back. 238 final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities); 239 nc.maybeMarkCapabilitiesRestricted(); 240 deduceNotVcnManagedCapability(nc); 241 return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE, 242 ConnectivityManager.REQUEST_ID_UNSET, Type.NONE); 243 } 244 245 /** 246 * Add the given capability requirement to this builder. These represent 247 * the requested network's required capabilities. Note that when searching 248 * for a network to satisfy a request, all capabilities requested must be 249 * satisfied. 250 * 251 * @param capability The capability to add. 252 * @return The builder to facilitate chaining 253 * {@code builder.addCapability(...).addCapability();}. 254 */ addCapability(@etworkCapabilities.NetCapability int capability)255 public Builder addCapability(@NetworkCapabilities.NetCapability int capability) { 256 mNetworkCapabilities.addCapability(capability); 257 if (capability == NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED) { 258 mShouldDeduceNotVcnManaged = false; 259 } 260 return this; 261 } 262 263 /** 264 * Removes (if found) the given capability from this builder instance. 265 * 266 * @param capability The capability to remove. 267 * @return The builder to facilitate chaining. 268 */ removeCapability(@etworkCapabilities.NetCapability int capability)269 public Builder removeCapability(@NetworkCapabilities.NetCapability int capability) { 270 mNetworkCapabilities.removeCapability(capability); 271 if (capability == NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED) { 272 mShouldDeduceNotVcnManaged = false; 273 } 274 return this; 275 } 276 277 /** 278 * Set the {@code NetworkCapabilities} for this builder instance, 279 * overriding any capabilities that had been previously set. 280 * 281 * @param nc The superseding {@code NetworkCapabilities} instance. 282 * @return The builder to facilitate chaining. 283 * @hide 284 */ setCapabilities(NetworkCapabilities nc)285 public Builder setCapabilities(NetworkCapabilities nc) { 286 mNetworkCapabilities.set(nc); 287 return this; 288 } 289 290 /** 291 * Sets this request to match only networks that apply to the specified UIDs. 292 * 293 * By default, the set of UIDs is the UID of the calling app, and this request will match 294 * any network that applies to the app. Setting it to {@code null} will observe any 295 * network on the system, even if it does not apply to this app. In this case, any 296 * {@link NetworkSpecifier} set on this request will be redacted or removed to prevent the 297 * application deducing restricted information such as location. 298 * 299 * @param uids The UIDs as a set of {@code Range<Integer>}, or null for everything. 300 * @return The builder to facilitate chaining. 301 * @hide 302 */ 303 @NonNull 304 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 305 @SuppressLint("MissingGetterMatchingBuilder") setUids(@ullable Set<Range<Integer>> uids)306 public Builder setUids(@Nullable Set<Range<Integer>> uids) { 307 mNetworkCapabilities.setUids(uids); 308 return this; 309 } 310 311 /** 312 * Add a capability that must not exist in the requested network. 313 * <p> 314 * If the capability was previously added to the list of required capabilities (for 315 * example, it was there by default or added using {@link #addCapability(int)} method), then 316 * it will be removed from the list of required capabilities as well. 317 * 318 * @see #addCapability(int) 319 * 320 * @param capability The capability to add to forbidden capability list. 321 * @return The builder to facilitate chaining. 322 * 323 * @hide 324 */ 325 @NonNull 326 @SuppressLint("MissingGetterMatchingBuilder") 327 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) addForbiddenCapability(@etworkCapabilities.NetCapability int capability)328 public Builder addForbiddenCapability(@NetworkCapabilities.NetCapability int capability) { 329 mNetworkCapabilities.addForbiddenCapability(capability); 330 return this; 331 } 332 333 /** 334 * Removes (if found) the given forbidden capability from this builder instance. 335 * 336 * @param capability The forbidden capability to remove. 337 * @return The builder to facilitate chaining. 338 * 339 * @hide 340 */ 341 @NonNull 342 @SuppressLint("BuilderSetStyle") 343 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) removeForbiddenCapability( @etworkCapabilities.NetCapability int capability)344 public Builder removeForbiddenCapability( 345 @NetworkCapabilities.NetCapability int capability) { 346 mNetworkCapabilities.removeForbiddenCapability(capability); 347 return this; 348 } 349 350 /** 351 * Completely clears all the {@code NetworkCapabilities} from this builder instance, 352 * removing even the capabilities that are set by default when the object is constructed. 353 * 354 * @return The builder to facilitate chaining. 355 */ 356 @NonNull clearCapabilities()357 public Builder clearCapabilities() { 358 mNetworkCapabilities.clearAll(); 359 // If the caller explicitly clear all capabilities, the NOT_VCN_MANAGED capabilities 360 // should not be add back later. 361 mShouldDeduceNotVcnManaged = false; 362 return this; 363 } 364 365 /** 366 * Adds the given transport requirement to this builder. These represent 367 * the set of allowed transports for the request. Only networks using one 368 * of these transports will satisfy the request. If no particular transports 369 * are required, none should be specified here. 370 * 371 * @param transportType The transport type to add. 372 * @return The builder to facilitate chaining. 373 */ addTransportType(@etworkCapabilities.Transport int transportType)374 public Builder addTransportType(@NetworkCapabilities.Transport int transportType) { 375 mNetworkCapabilities.addTransportType(transportType); 376 return this; 377 } 378 379 /** 380 * Removes (if found) the given transport from this builder instance. 381 * 382 * @param transportType The transport type to remove. 383 * @return The builder to facilitate chaining. 384 */ removeTransportType(@etworkCapabilities.Transport int transportType)385 public Builder removeTransportType(@NetworkCapabilities.Transport int transportType) { 386 mNetworkCapabilities.removeTransportType(transportType); 387 return this; 388 } 389 390 /** 391 * @hide 392 */ setLinkUpstreamBandwidthKbps(int upKbps)393 public Builder setLinkUpstreamBandwidthKbps(int upKbps) { 394 mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps); 395 return this; 396 } 397 /** 398 * @hide 399 */ setLinkDownstreamBandwidthKbps(int downKbps)400 public Builder setLinkDownstreamBandwidthKbps(int downKbps) { 401 mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps); 402 return this; 403 } 404 405 /** 406 * Sets the optional bearer specific network specifier. 407 * This has no meaning if a single transport is also not specified, so calling 408 * this without a single transport set will generate an exception, as will 409 * subsequently adding or removing transports after this is set. 410 * </p> 411 * If the {@code networkSpecifier} is provided, it shall be interpreted as follows: 412 * <ul> 413 * <li>If the specifier can be parsed as an integer, it will be treated as a 414 * {@link android.net TelephonyNetworkSpecifier}, and the provided integer will be 415 * interpreted as a SubscriptionId. 416 * <li>If the value is an ethernet interface name, it will be treated as such. 417 * <li>For all other cases, the behavior is undefined. 418 * </ul> 419 * 420 * @param networkSpecifier A {@code String} of either a SubscriptionId in cellular 421 * network request or an ethernet interface name in ethernet 422 * network request. 423 * 424 * @deprecated Use {@link #setNetworkSpecifier(NetworkSpecifier)} instead. 425 */ 426 @SuppressLint("NewApi") // TODO: b/193460475 remove once fixed 427 @Deprecated setNetworkSpecifier(String networkSpecifier)428 public Builder setNetworkSpecifier(String networkSpecifier) { 429 try { 430 int subId = Integer.parseInt(networkSpecifier); 431 return setNetworkSpecifier(new TelephonyNetworkSpecifier.Builder() 432 .setSubscriptionId(subId).build()); 433 } catch (NumberFormatException nfe) { 434 // An EthernetNetworkSpecifier or TestNetworkSpecifier does not accept null or empty 435 // ("") strings. When network specifiers were strings a null string and an empty 436 // string were considered equivalent. Hence no meaning is attached to a null or 437 // empty ("") string. 438 if (TextUtils.isEmpty(networkSpecifier)) { 439 return setNetworkSpecifier((NetworkSpecifier) null); 440 } else if (mNetworkCapabilities.hasTransport(TRANSPORT_TEST)) { 441 return setNetworkSpecifier(new TestNetworkSpecifier(networkSpecifier)); 442 } else { 443 // TODO: b/193460475 remove comment once fixed 444 // @SuppressLint("NewApi") is due to EthernetNetworkSpecifier being changed 445 // from @SystemApi to public. EthernetNetworkSpecifier was introduced in Android 446 // 12 as @SystemApi(client = MODULE_LIBRARIES) and made public in Android 13. 447 // b/193460475 means in the above situation the tools will think 448 // EthernetNetworkSpecifier didn't exist in Android 12, causing the NewApi lint 449 // to fail. In this case, this is actually safe because this code was 450 // modularized in Android 12, so it can't run on SDKs before Android 12 and is 451 // therefore guaranteed to always have this class available to it. 452 return setNetworkSpecifier(new EthernetNetworkSpecifier(networkSpecifier)); 453 } 454 } 455 } 456 457 /** 458 * Sets the optional bearer specific network specifier. 459 * This has no meaning if a single transport is also not specified, so calling 460 * this without a single transport set will generate an exception, as will 461 * subsequently adding or removing transports after this is set. 462 * </p> 463 * 464 * @param networkSpecifier A concrete, parcelable framework class that extends 465 * NetworkSpecifier. 466 */ setNetworkSpecifier(NetworkSpecifier networkSpecifier)467 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) { 468 if (networkSpecifier instanceof MatchAllNetworkSpecifier) { 469 throw new IllegalArgumentException("A MatchAllNetworkSpecifier is not permitted"); 470 } 471 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier); 472 // Do not touch NOT_VCN_MANAGED if the caller needs to access to a very specific 473 // Network. 474 mShouldDeduceNotVcnManaged = false; 475 return this; 476 } 477 478 /** 479 * Sets the signal strength. This is a signed integer, with higher values indicating a 480 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same 481 * RSSI units reported by WifiManager. 482 * <p> 483 * Note that when used to register a network callback, this specifies the minimum acceptable 484 * signal strength. When received as the state of an existing network it specifies the 485 * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when 486 * received and has no effect when requesting a callback. 487 * 488 * <p>This method requires the caller to hold the 489 * {@link android.Manifest.permission#NETWORK_SIGNAL_STRENGTH_WAKEUP} permission 490 * 491 * @param signalStrength the bearer-specific signal strength. 492 * @hide 493 */ 494 @SystemApi 495 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP) setSignalStrength(int signalStrength)496 public @NonNull Builder setSignalStrength(int signalStrength) { 497 mNetworkCapabilities.setSignalStrength(signalStrength); 498 return this; 499 } 500 501 /** 502 * Deduce the NET_CAPABILITY_NOT_VCN_MANAGED capability from other capabilities 503 * and user intention, which includes: 504 * 1. For the requests that don't have anything besides 505 * {@link #VCN_SUPPORTED_CAPABILITIES}, add the NET_CAPABILITY_NOT_VCN_MANAGED to 506 * allow the callers automatically utilize VCN networks if available. 507 * 2. For the requests that explicitly add or remove NET_CAPABILITY_NOT_VCN_MANAGED, 508 * or has clear intention of tracking specific network, 509 * do not alter them to allow user fire request that suits their need. 510 * 511 * @hide 512 */ deduceNotVcnManagedCapability(final NetworkCapabilities nc)513 private void deduceNotVcnManagedCapability(final NetworkCapabilities nc) { 514 if (!mShouldDeduceNotVcnManaged) return; 515 for (final int cap : nc.getCapabilities()) { 516 if (!VCN_SUPPORTED_CAPABILITIES.contains(cap)) return; 517 } 518 nc.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED); 519 } 520 521 /** 522 * Sets the optional subscription ID set. 523 * <p> 524 * This specify the subscription IDs requirement. 525 * A network will satisfy this request only if it matches one of the subIds in this set. 526 * An empty set matches all networks, including those without a subId. 527 * 528 * <p>Registering a NetworkRequest with a non-empty set of subIds requires the 529 * NETWORK_FACTORY permission. 530 * 531 * @param subIds A {@code Set} that represents subscription IDs. 532 * @hide 533 */ 534 @NonNull 535 @SystemApi setSubscriptionIds(@onNull Set<Integer> subIds)536 public Builder setSubscriptionIds(@NonNull Set<Integer> subIds) { 537 mNetworkCapabilities.setSubscriptionIds(subIds); 538 return this; 539 } 540 541 /** 542 * Specifies whether the built request should also match networks that do not apply to the 543 * calling UID. 544 * 545 * By default, the built request will only match networks that apply to the calling UID. 546 * If this method is called with {@code true}, the built request will match any network on 547 * the system that matches the other parameters of the request. In this case, any 548 * information in the built request that is subject to redaction for security or privacy 549 * purposes, such as a {@link NetworkSpecifier}, will be redacted or removed to prevent the 550 * application deducing sensitive information. 551 * 552 * @param include Whether to match networks that do not apply to the calling UID. 553 * @return The builder to facilitate chaining. 554 */ 555 @NonNull setIncludeOtherUidNetworks(boolean include)556 public Builder setIncludeOtherUidNetworks(boolean include) { 557 if (include) { 558 mNetworkCapabilities.setUids(null); 559 } else { 560 mNetworkCapabilities.setSingleUid(Process.myUid()); 561 } 562 return this; 563 } 564 } 565 566 // implement the Parcelable interface describeContents()567 public int describeContents() { 568 return 0; 569 } writeToParcel(Parcel dest, int flags)570 public void writeToParcel(Parcel dest, int flags) { 571 networkCapabilities.writeToParcel(dest, flags); 572 dest.writeInt(legacyType); 573 dest.writeInt(requestId); 574 dest.writeString(type.name()); 575 } 576 577 public static final @android.annotation.NonNull Creator<NetworkRequest> CREATOR = 578 new Creator<NetworkRequest>() { 579 public NetworkRequest createFromParcel(Parcel in) { 580 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in); 581 int legacyType = in.readInt(); 582 int requestId = in.readInt(); 583 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid. 584 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type); 585 return result; 586 } 587 public NetworkRequest[] newArray(int size) { 588 return new NetworkRequest[size]; 589 } 590 }; 591 592 /** 593 * Returns true iff. this NetworkRequest is of type LISTEN. 594 * 595 * @hide 596 */ isListen()597 public boolean isListen() { 598 return type == Type.LISTEN; 599 } 600 601 /** 602 * Returns true iff. this NetworkRequest is of type LISTEN_FOR_BEST. 603 * 604 * @hide 605 */ isListenForBest()606 public boolean isListenForBest() { 607 return type == Type.LISTEN_FOR_BEST; 608 } 609 610 /** 611 * Returns true iff. the contained NetworkRequest is one that: 612 * 613 * - should be associated with at most one satisfying network 614 * at a time; 615 * 616 * - should cause a network to be kept up, but not necessarily in 617 * the foreground, if it is the best network which can satisfy the 618 * NetworkRequest. 619 * 620 * For full detail of how isRequest() is used for pairing Networks with 621 * NetworkRequests read rematchNetworkAndRequests(). 622 * 623 * @hide 624 */ isRequest()625 public boolean isRequest() { 626 return type == Type.REQUEST || type == Type.BACKGROUND_REQUEST; 627 } 628 629 /** 630 * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST. 631 * 632 * @hide 633 */ isBackgroundRequest()634 public boolean isBackgroundRequest() { 635 return type == Type.BACKGROUND_REQUEST; 636 } 637 638 /** 639 * @see Builder#addCapability(int) 640 */ hasCapability(@etCapability int capability)641 public boolean hasCapability(@NetCapability int capability) { 642 return networkCapabilities.hasCapability(capability); 643 } 644 645 /** 646 * @see Builder#addForbiddenCapability(int) 647 * 648 * @hide 649 */ 650 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) hasForbiddenCapability(@etCapability int capability)651 public boolean hasForbiddenCapability(@NetCapability int capability) { 652 return networkCapabilities.hasForbiddenCapability(capability); 653 } 654 655 /** 656 * Returns true if and only if the capabilities requested in this NetworkRequest are satisfied 657 * by the provided {@link NetworkCapabilities}. 658 * 659 * @param nc Capabilities that should satisfy this NetworkRequest. null capabilities do not 660 * satisfy any request. 661 */ canBeSatisfiedBy(@ullable NetworkCapabilities nc)662 public boolean canBeSatisfiedBy(@Nullable NetworkCapabilities nc) { 663 return networkCapabilities.satisfiedByNetworkCapabilities(nc); 664 } 665 666 /** 667 * @see Builder#addTransportType(int) 668 */ hasTransport(@ransport int transportType)669 public boolean hasTransport(@Transport int transportType) { 670 return networkCapabilities.hasTransport(transportType); 671 } 672 673 /** 674 * @see Builder#setNetworkSpecifier(NetworkSpecifier) 675 */ 676 @Nullable getNetworkSpecifier()677 public NetworkSpecifier getNetworkSpecifier() { 678 return networkCapabilities.getNetworkSpecifier(); 679 } 680 681 /** 682 * @return the uid of the app making the request. 683 * 684 * Note: This could return {@link Process#INVALID_UID} if the {@link NetworkRequest} object was 685 * not obtained from {@link ConnectivityManager}. 686 * @hide 687 */ 688 @SystemApi getRequestorUid()689 public int getRequestorUid() { 690 return networkCapabilities.getRequestorUid(); 691 } 692 693 /** 694 * @return the package name of the app making the request. 695 * 696 * Note: This could return {@code null} if the {@link NetworkRequest} object was not obtained 697 * from {@link ConnectivityManager}. 698 * @hide 699 */ 700 @SystemApi 701 @Nullable getRequestorPackageName()702 public String getRequestorPackageName() { 703 return networkCapabilities.getRequestorPackageName(); 704 } 705 toString()706 public String toString() { 707 return "NetworkRequest [ " + type + " id=" + requestId + 708 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") + 709 ", " + networkCapabilities.toString() + " ]"; 710 } 711 equals(@ullable Object obj)712 public boolean equals(@Nullable Object obj) { 713 if (obj instanceof NetworkRequest == false) return false; 714 NetworkRequest that = (NetworkRequest)obj; 715 return (that.legacyType == this.legacyType && 716 that.requestId == this.requestId && 717 that.type == this.type && 718 Objects.equals(that.networkCapabilities, this.networkCapabilities)); 719 } 720 hashCode()721 public int hashCode() { 722 return Objects.hash(requestId, legacyType, networkCapabilities, type); 723 } 724 725 /** 726 * Gets all the capabilities set on this {@code NetworkRequest} instance. 727 * 728 * @return an array of capability values for this instance. 729 */ 730 @NonNull getCapabilities()731 public @NetCapability int[] getCapabilities() { 732 // No need to make a defensive copy here as NC#getCapabilities() already returns 733 // a new array. 734 return networkCapabilities.getCapabilities(); 735 } 736 737 /** 738 * Get the enteprise identifiers. 739 * 740 * Get all the enterprise identifiers set on this {@code NetworkCapability} 741 * @return array of all the enterprise identifiers. 742 * @hide 743 */ 744 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) getEnterpriseIds()745 public @NonNull @NetworkCapabilities.EnterpriseId int[] getEnterpriseIds() { 746 // No need to make a defensive copy here as NC#getCapabilities() already returns 747 // a new array. 748 return networkCapabilities.getEnterpriseIds(); 749 } 750 751 /** 752 * Tests for the presence of an enterprise identifier on this instance. 753 * 754 * @param enterpriseId the enterprise capability identifier to be tested for. 755 * @return {@code true} if set on this instance. 756 * @hide 757 */ 758 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) hasEnterpriseId( @etworkCapabilities.EnterpriseId int enterpriseId)759 public boolean hasEnterpriseId( 760 @NetworkCapabilities.EnterpriseId int enterpriseId) { 761 return networkCapabilities.hasEnterpriseId(enterpriseId); 762 } 763 764 /** 765 * Gets all the forbidden capabilities set on this {@code NetworkRequest} instance. 766 * 767 * @return an array of forbidden capability values for this instance. 768 * 769 * @hide 770 */ 771 @NonNull 772 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) getForbiddenCapabilities()773 public @NetCapability int[] getForbiddenCapabilities() { 774 // No need to make a defensive copy here as NC#getForbiddenCapabilities() already returns 775 // a new array. 776 return networkCapabilities.getForbiddenCapabilities(); 777 } 778 779 /** 780 * Gets all the transports set on this {@code NetworkRequest} instance. 781 * 782 * @return an array of transport type values for this instance. 783 */ 784 @NonNull getTransportTypes()785 public @Transport int[] getTransportTypes() { 786 // No need to make a defensive copy here as NC#getTransportTypes() already returns 787 // a new array. 788 return networkCapabilities.getTransportTypes(); 789 } 790 } 791