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 @Deprecated setNetworkSpecifier(String networkSpecifier)427 public Builder setNetworkSpecifier(String networkSpecifier) { 428 try { 429 int subId = Integer.parseInt(networkSpecifier); 430 return setNetworkSpecifier(new TelephonyNetworkSpecifier.Builder() 431 .setSubscriptionId(subId).build()); 432 } catch (NumberFormatException nfe) { 433 // An EthernetNetworkSpecifier or TestNetworkSpecifier does not accept null or empty 434 // ("") strings. When network specifiers were strings a null string and an empty 435 // string were considered equivalent. Hence no meaning is attached to a null or 436 // empty ("") string. 437 if (TextUtils.isEmpty(networkSpecifier)) { 438 return setNetworkSpecifier((NetworkSpecifier) null); 439 } else if (mNetworkCapabilities.hasTransport(TRANSPORT_TEST)) { 440 return setNetworkSpecifier(new TestNetworkSpecifier(networkSpecifier)); 441 } else { 442 return setNetworkSpecifier(new EthernetNetworkSpecifier(networkSpecifier)); 443 } 444 } 445 } 446 447 /** 448 * Sets the optional bearer specific network specifier. 449 * This has no meaning if a single transport is also not specified, so calling 450 * this without a single transport set will generate an exception, as will 451 * subsequently adding or removing transports after this is set. 452 * </p> 453 * 454 * @param networkSpecifier A concrete, parcelable framework class that extends 455 * NetworkSpecifier. 456 */ setNetworkSpecifier(NetworkSpecifier networkSpecifier)457 public Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier) { 458 if (networkSpecifier instanceof MatchAllNetworkSpecifier) { 459 throw new IllegalArgumentException("A MatchAllNetworkSpecifier is not permitted"); 460 } 461 mNetworkCapabilities.setNetworkSpecifier(networkSpecifier); 462 // Do not touch NOT_VCN_MANAGED if the caller needs to access to a very specific 463 // Network. 464 mShouldDeduceNotVcnManaged = false; 465 return this; 466 } 467 468 /** 469 * Sets the signal strength. This is a signed integer, with higher values indicating a 470 * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same 471 * RSSI units reported by WifiManager. 472 * <p> 473 * Note that when used to register a network callback, this specifies the minimum acceptable 474 * signal strength. When received as the state of an existing network it specifies the 475 * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when 476 * received and has no effect when requesting a callback. 477 * 478 * <p>This method requires the caller to hold the 479 * {@link android.Manifest.permission#NETWORK_SIGNAL_STRENGTH_WAKEUP} permission 480 * 481 * @param signalStrength the bearer-specific signal strength. 482 * @hide 483 */ 484 @SystemApi 485 @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP) setSignalStrength(int signalStrength)486 public @NonNull Builder setSignalStrength(int signalStrength) { 487 mNetworkCapabilities.setSignalStrength(signalStrength); 488 return this; 489 } 490 491 /** 492 * Deduce the NET_CAPABILITY_NOT_VCN_MANAGED capability from other capabilities 493 * and user intention, which includes: 494 * 1. For the requests that don't have anything besides 495 * {@link #VCN_SUPPORTED_CAPABILITIES}, add the NET_CAPABILITY_NOT_VCN_MANAGED to 496 * allow the callers automatically utilize VCN networks if available. 497 * 2. For the requests that explicitly add or remove NET_CAPABILITY_NOT_VCN_MANAGED, 498 * or has clear intention of tracking specific network, 499 * do not alter them to allow user fire request that suits their need. 500 * 501 * @hide 502 */ deduceNotVcnManagedCapability(final NetworkCapabilities nc)503 private void deduceNotVcnManagedCapability(final NetworkCapabilities nc) { 504 if (!mShouldDeduceNotVcnManaged) return; 505 for (final int cap : nc.getCapabilities()) { 506 if (!VCN_SUPPORTED_CAPABILITIES.contains(cap)) return; 507 } 508 nc.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED); 509 } 510 511 /** 512 * Sets the optional subscription ID set. 513 * <p> 514 * This specify the subscription IDs requirement. 515 * A network will satisfy this request only if it matches one of the subIds in this set. 516 * An empty set matches all networks, including those without a subId. 517 * 518 * <p>Registering a NetworkRequest with a non-empty set of subIds requires the 519 * NETWORK_FACTORY permission. 520 * 521 * @param subIds A {@code Set} that represents subscription IDs. 522 * @hide 523 */ 524 @NonNull 525 @SystemApi setSubscriptionIds(@onNull Set<Integer> subIds)526 public Builder setSubscriptionIds(@NonNull Set<Integer> subIds) { 527 mNetworkCapabilities.setSubscriptionIds(subIds); 528 return this; 529 } 530 531 /** 532 * Specifies whether the built request should also match networks that do not apply to the 533 * calling UID. 534 * 535 * By default, the built request will only match networks that apply to the calling UID. 536 * If this method is called with {@code true}, the built request will match any network on 537 * the system that matches the other parameters of the request. In this case, any 538 * information in the built request that is subject to redaction for security or privacy 539 * purposes, such as a {@link NetworkSpecifier}, will be redacted or removed to prevent the 540 * application deducing sensitive information. 541 * 542 * @param include Whether to match networks that do not apply to the calling UID. 543 * @return The builder to facilitate chaining. 544 */ 545 @NonNull setIncludeOtherUidNetworks(boolean include)546 public Builder setIncludeOtherUidNetworks(boolean include) { 547 if (include) { 548 mNetworkCapabilities.setUids(null); 549 } else { 550 mNetworkCapabilities.setSingleUid(Process.myUid()); 551 } 552 return this; 553 } 554 } 555 556 // implement the Parcelable interface describeContents()557 public int describeContents() { 558 return 0; 559 } writeToParcel(Parcel dest, int flags)560 public void writeToParcel(Parcel dest, int flags) { 561 networkCapabilities.writeToParcel(dest, flags); 562 dest.writeInt(legacyType); 563 dest.writeInt(requestId); 564 dest.writeString(type.name()); 565 } 566 567 public static final @android.annotation.NonNull Creator<NetworkRequest> CREATOR = 568 new Creator<NetworkRequest>() { 569 public NetworkRequest createFromParcel(Parcel in) { 570 NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in); 571 int legacyType = in.readInt(); 572 int requestId = in.readInt(); 573 Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid. 574 NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type); 575 return result; 576 } 577 public NetworkRequest[] newArray(int size) { 578 return new NetworkRequest[size]; 579 } 580 }; 581 582 /** 583 * Returns true iff. this NetworkRequest is of type LISTEN. 584 * 585 * @hide 586 */ isListen()587 public boolean isListen() { 588 return type == Type.LISTEN; 589 } 590 591 /** 592 * Returns true iff. this NetworkRequest is of type LISTEN_FOR_BEST. 593 * 594 * @hide 595 */ isListenForBest()596 public boolean isListenForBest() { 597 return type == Type.LISTEN_FOR_BEST; 598 } 599 600 /** 601 * Returns true iff. the contained NetworkRequest is one that: 602 * 603 * - should be associated with at most one satisfying network 604 * at a time; 605 * 606 * - should cause a network to be kept up, but not necessarily in 607 * the foreground, if it is the best network which can satisfy the 608 * NetworkRequest. 609 * 610 * For full detail of how isRequest() is used for pairing Networks with 611 * NetworkRequests read rematchNetworkAndRequests(). 612 * 613 * @hide 614 */ isRequest()615 public boolean isRequest() { 616 return type == Type.REQUEST || type == Type.BACKGROUND_REQUEST; 617 } 618 619 /** 620 * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST. 621 * 622 * @hide 623 */ isBackgroundRequest()624 public boolean isBackgroundRequest() { 625 return type == Type.BACKGROUND_REQUEST; 626 } 627 628 /** 629 * @see Builder#addCapability(int) 630 */ hasCapability(@etCapability int capability)631 public boolean hasCapability(@NetCapability int capability) { 632 return networkCapabilities.hasCapability(capability); 633 } 634 635 /** 636 * @see Builder#addForbiddenCapability(int) 637 * 638 * @hide 639 */ 640 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) hasForbiddenCapability(@etCapability int capability)641 public boolean hasForbiddenCapability(@NetCapability int capability) { 642 return networkCapabilities.hasForbiddenCapability(capability); 643 } 644 645 /** 646 * Returns true if and only if the capabilities requested in this NetworkRequest are satisfied 647 * by the provided {@link NetworkCapabilities}. 648 * 649 * @param nc Capabilities that should satisfy this NetworkRequest. null capabilities do not 650 * satisfy any request. 651 */ canBeSatisfiedBy(@ullable NetworkCapabilities nc)652 public boolean canBeSatisfiedBy(@Nullable NetworkCapabilities nc) { 653 return networkCapabilities.satisfiedByNetworkCapabilities(nc); 654 } 655 656 /** 657 * @see Builder#addTransportType(int) 658 */ hasTransport(@ransport int transportType)659 public boolean hasTransport(@Transport int transportType) { 660 return networkCapabilities.hasTransport(transportType); 661 } 662 663 /** 664 * @see Builder#setNetworkSpecifier(NetworkSpecifier) 665 */ 666 @Nullable getNetworkSpecifier()667 public NetworkSpecifier getNetworkSpecifier() { 668 return networkCapabilities.getNetworkSpecifier(); 669 } 670 671 /** 672 * @return the uid of the app making the request. 673 * 674 * Note: This could return {@link Process#INVALID_UID} if the {@link NetworkRequest} object was 675 * not obtained from {@link ConnectivityManager}. 676 * @hide 677 */ 678 @SystemApi getRequestorUid()679 public int getRequestorUid() { 680 return networkCapabilities.getRequestorUid(); 681 } 682 683 /** 684 * @return the package name of the app making the request. 685 * 686 * Note: This could return {@code null} if the {@link NetworkRequest} object was not obtained 687 * from {@link ConnectivityManager}. 688 * @hide 689 */ 690 @SystemApi 691 @Nullable getRequestorPackageName()692 public String getRequestorPackageName() { 693 return networkCapabilities.getRequestorPackageName(); 694 } 695 toString()696 public String toString() { 697 return "NetworkRequest [ " + type + " id=" + requestId + 698 (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") + 699 ", " + networkCapabilities.toString() + " ]"; 700 } 701 equals(@ullable Object obj)702 public boolean equals(@Nullable Object obj) { 703 if (obj instanceof NetworkRequest == false) return false; 704 NetworkRequest that = (NetworkRequest)obj; 705 return (that.legacyType == this.legacyType && 706 that.requestId == this.requestId && 707 that.type == this.type && 708 Objects.equals(that.networkCapabilities, this.networkCapabilities)); 709 } 710 hashCode()711 public int hashCode() { 712 return Objects.hash(requestId, legacyType, networkCapabilities, type); 713 } 714 715 /** 716 * Gets all the capabilities set on this {@code NetworkRequest} instance. 717 * 718 * @return an array of capability values for this instance. 719 */ 720 @NonNull getCapabilities()721 public @NetCapability int[] getCapabilities() { 722 // No need to make a defensive copy here as NC#getCapabilities() already returns 723 // a new array. 724 return networkCapabilities.getCapabilities(); 725 } 726 727 /** 728 * Gets all the forbidden capabilities set on this {@code NetworkRequest} instance. 729 * 730 * @return an array of forbidden capability values for this instance. 731 * 732 * @hide 733 */ 734 @NonNull 735 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) getForbiddenCapabilities()736 public @NetCapability int[] getForbiddenCapabilities() { 737 // No need to make a defensive copy here as NC#getForbiddenCapabilities() already returns 738 // a new array. 739 return networkCapabilities.getForbiddenCapabilities(); 740 } 741 742 /** 743 * Gets all the transports set on this {@code NetworkRequest} instance. 744 * 745 * @return an array of transport type values for this instance. 746 */ 747 @NonNull getTransportTypes()748 public @Transport int[] getTransportTypes() { 749 // No need to make a defensive copy here as NC#getTransportTypes() already returns 750 // a new array. 751 return networkCapabilities.getTransportTypes(); 752 } 753 } 754