ConnectivityManager.java revision 5a041d1a24f8a8e2de20f909f605e8e87b766b54
1/* 2 * Copyright (C) 2008 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 */ 16package android.net; 17 18import android.annotation.IntDef; 19import android.annotation.Nullable; 20import android.annotation.RequiresPermission; 21import android.annotation.SdkConstant; 22import android.annotation.SdkConstant.SdkConstantType; 23import android.annotation.SystemApi; 24import android.annotation.SystemService; 25import android.app.PendingIntent; 26import android.content.Context; 27import android.content.Intent; 28import android.content.pm.PackageManager; 29import android.os.Binder; 30import android.os.Build.VERSION_CODES; 31import android.os.Bundle; 32import android.os.Handler; 33import android.os.HandlerThread; 34import android.os.IBinder; 35import android.os.INetworkActivityListener; 36import android.os.INetworkManagementService; 37import android.os.Looper; 38import android.os.Message; 39import android.os.Messenger; 40import android.os.Process; 41import android.os.RemoteException; 42import android.os.ResultReceiver; 43import android.os.ServiceManager; 44import android.os.ServiceSpecificException; 45import android.provider.Settings; 46import android.telephony.SubscriptionManager; 47import android.util.ArrayMap; 48import android.util.Log; 49import android.util.SparseIntArray; 50 51import com.android.internal.telephony.ITelephony; 52import com.android.internal.telephony.PhoneConstants; 53import com.android.internal.util.Preconditions; 54import com.android.internal.util.Protocol; 55 56import libcore.net.event.NetworkEventDispatcher; 57 58import java.lang.annotation.Retention; 59import java.lang.annotation.RetentionPolicy; 60import java.net.InetAddress; 61import java.util.ArrayList; 62import java.util.HashMap; 63import java.util.List; 64import java.util.Map; 65 66/** 67 * Class that answers queries about the state of network connectivity. It also 68 * notifies applications when network connectivity changes. 69 * <p> 70 * The primary responsibilities of this class are to: 71 * <ol> 72 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> 73 * <li>Send broadcast intents when network connectivity changes</li> 74 * <li>Attempt to "fail over" to another network when connectivity to a network 75 * is lost</li> 76 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained 77 * state of the available networks</li> 78 * <li>Provide an API that allows applications to request and select networks for their data 79 * traffic</li> 80 * </ol> 81 */ 82@SystemService(Context.CONNECTIVITY_SERVICE) 83public class ConnectivityManager { 84 private static final String TAG = "ConnectivityManager"; 85 86 /** 87 * A change in network connectivity has occurred. A default connection has either 88 * been established or lost. The NetworkInfo for the affected network is 89 * sent as an extra; it should be consulted to see what kind of 90 * connectivity event occurred. 91 * <p/> 92 * Apps targeting Android 7.0 (API level 24) and higher do not receive this 93 * broadcast if they declare the broadcast receiver in their manifest. Apps 94 * will still receive broadcasts if they register their 95 * {@link android.content.BroadcastReceiver} with 96 * {@link android.content.Context#registerReceiver Context.registerReceiver()} 97 * and that context is still valid. 98 * <p/> 99 * If this is a connection that was the result of failing over from a 100 * disconnected network, then the FAILOVER_CONNECTION boolean extra is 101 * set to true. 102 * <p/> 103 * For a loss of connectivity, if the connectivity manager is attempting 104 * to connect (or has already connected) to another network, the 105 * NetworkInfo for the new network is also passed as an extra. This lets 106 * any receivers of the broadcast know that they should not necessarily 107 * tell the user that no data traffic will be possible. Instead, the 108 * receiver should expect another broadcast soon, indicating either that 109 * the failover attempt succeeded (and so there is still overall data 110 * connectivity), or that the failover attempt failed, meaning that all 111 * connectivity has been lost. 112 * <p/> 113 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY 114 * is set to {@code true} if there are no connected networks at all. 115 * 116 * @deprecated apps should use the more versatile {@link #requestNetwork}, 117 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback} 118 * functions instead for faster and more detailed updates about the network 119 * changes they care about. 120 */ 121 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 122 @Deprecated 123 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; 124 125 /** 126 * A temporary hack until SUPL system can get off the legacy APIS. 127 * They do too many network requests and the long list of apps listening 128 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive. 129 * Use this bcast intent instead for SUPL requests. 130 * @hide 131 */ 132 public static final String CONNECTIVITY_ACTION_SUPL = 133 "android.net.conn.CONNECTIVITY_CHANGE_SUPL"; 134 135 /** 136 * The device has connected to a network that has presented a captive 137 * portal, which is blocking Internet connectivity. The user was presented 138 * with a notification that network sign in is required, 139 * and the user invoked the notification's action indicating they 140 * desire to sign in to the network. Apps handling this activity should 141 * facilitate signing in to the network. This action includes a 142 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents 143 * the network presenting the captive portal; all communication with the 144 * captive portal must be done using this {@code Network} object. 145 * <p/> 146 * This activity includes a {@link CaptivePortal} extra named 147 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different 148 * outcomes of the captive portal sign in to the system: 149 * <ul> 150 * <li> When the app handling this action believes the user has signed in to 151 * the network and the captive portal has been dismissed, the app should 152 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can 153 * reevaluate the network. If reevaluation finds the network no longer 154 * subject to a captive portal, the network may become the default active 155 * data network. </li> 156 * <li> When the app handling this action believes the user explicitly wants 157 * to ignore the captive portal and the network, the app should call 158 * {@link CaptivePortal#ignoreNetwork}. </li> 159 * </ul> 160 */ 161 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 162 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; 163 164 /** 165 * The lookup key for a {@link NetworkInfo} object. Retrieve with 166 * {@link android.content.Intent#getParcelableExtra(String)}. 167 * 168 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications 169 * should always obtain network information through 170 * {@link #getActiveNetworkInfo()}. 171 * @see #EXTRA_NETWORK_TYPE 172 */ 173 @Deprecated 174 public static final String EXTRA_NETWORK_INFO = "networkInfo"; 175 176 /** 177 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. 178 * 179 * @see android.content.Intent#getIntExtra(String, int) 180 */ 181 public static final String EXTRA_NETWORK_TYPE = "networkType"; 182 183 /** 184 * The lookup key for a boolean that indicates whether a connect event 185 * is for a network to which the connectivity manager was failing over 186 * following a disconnect on another network. 187 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. 188 */ 189 public static final String EXTRA_IS_FAILOVER = "isFailover"; 190 /** 191 * The lookup key for a {@link NetworkInfo} object. This is supplied when 192 * there is another network that it may be possible to connect to. Retrieve with 193 * {@link android.content.Intent#getParcelableExtra(String)}. 194 */ 195 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; 196 /** 197 * The lookup key for a boolean that indicates whether there is a 198 * complete lack of connectivity, i.e., no network is available. 199 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. 200 */ 201 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; 202 /** 203 * The lookup key for a string that indicates why an attempt to connect 204 * to a network failed. The string has no particular structure. It is 205 * intended to be used in notifications presented to users. Retrieve 206 * it with {@link android.content.Intent#getStringExtra(String)}. 207 */ 208 public static final String EXTRA_REASON = "reason"; 209 /** 210 * The lookup key for a string that provides optionally supplied 211 * extra information about the network state. The information 212 * may be passed up from the lower networking layers, and its 213 * meaning may be specific to a particular network type. Retrieve 214 * it with {@link android.content.Intent#getStringExtra(String)}. 215 */ 216 public static final String EXTRA_EXTRA_INFO = "extraInfo"; 217 /** 218 * The lookup key for an int that provides information about 219 * our connection to the internet at large. 0 indicates no connection, 220 * 100 indicates a great connection. Retrieve it with 221 * {@link android.content.Intent#getIntExtra(String, int)}. 222 * {@hide} 223 */ 224 public static final String EXTRA_INET_CONDITION = "inetCondition"; 225 /** 226 * The lookup key for a {@link CaptivePortal} object included with the 227 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} 228 * object can be used to either indicate to the system that the captive 229 * portal has been dismissed or that the user does not want to pursue 230 * signing in to captive portal. Retrieve it with 231 * {@link android.content.Intent#getParcelableExtra(String)}. 232 */ 233 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; 234 235 /** 236 * Key for passing a URL to the captive portal login activity. 237 */ 238 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; 239 240 /** 241 * Key for passing a user agent string to the captive portal login activity. 242 * {@hide} 243 */ 244 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT = 245 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT"; 246 247 /** 248 * Broadcast action to indicate the change of data activity status 249 * (idle or active) on a network in a recent period. 250 * The network becomes active when data transmission is started, or 251 * idle if there is no data transmission for a period of time. 252 * {@hide} 253 */ 254 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 255 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE"; 256 /** 257 * The lookup key for an enum that indicates the network device type on which this data activity 258 * change happens. 259 * {@hide} 260 */ 261 public static final String EXTRA_DEVICE_TYPE = "deviceType"; 262 /** 263 * The lookup key for a boolean that indicates the device is active or not. {@code true} means 264 * it is actively sending or receiving data and {@code false} means it is idle. 265 * {@hide} 266 */ 267 public static final String EXTRA_IS_ACTIVE = "isActive"; 268 /** 269 * The lookup key for a long that contains the timestamp (nanos) of the radio state change. 270 * {@hide} 271 */ 272 public static final String EXTRA_REALTIME_NS = "tsNanos"; 273 274 /** 275 * Broadcast Action: The setting for background data usage has changed 276 * values. Use {@link #getBackgroundDataSetting()} to get the current value. 277 * <p> 278 * If an application uses the network in the background, it should listen 279 * for this broadcast and stop using the background data if the value is 280 * {@code false}. 281 * <p> 282 * 283 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability 284 * of background data depends on several combined factors, and 285 * this broadcast is no longer sent. Instead, when background 286 * data is unavailable, {@link #getActiveNetworkInfo()} will now 287 * appear disconnected. During first boot after a platform 288 * upgrade, this broadcast will be sent once if 289 * {@link #getBackgroundDataSetting()} was {@code false} before 290 * the upgrade. 291 */ 292 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 293 @Deprecated 294 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = 295 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; 296 297 /** 298 * Broadcast Action: The network connection may not be good 299 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and 300 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify 301 * the network and it's condition. 302 * @hide 303 */ 304 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 305 public static final String INET_CONDITION_ACTION = 306 "android.net.conn.INET_CONDITION_ACTION"; 307 308 /** 309 * Broadcast Action: A tetherable connection has come or gone. 310 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, 311 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY}, 312 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and 313 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate 314 * the current state of tethering. Each include a list of 315 * interface names in that state (may be empty). 316 * @hide 317 */ 318 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 319 public static final String ACTION_TETHER_STATE_CHANGED = 320 "android.net.conn.TETHER_STATE_CHANGED"; 321 322 /** 323 * @hide 324 * gives a String[] listing all the interfaces configured for 325 * tethering and currently available for tethering. 326 */ 327 public static final String EXTRA_AVAILABLE_TETHER = "availableArray"; 328 329 /** 330 * @hide 331 * gives a String[] listing all the interfaces currently in local-only 332 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding) 333 */ 334 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray"; 335 336 /** 337 * @hide 338 * gives a String[] listing all the interfaces currently tethered 339 * (ie, has DHCPv4 support and packets potentially forwarded/NATed) 340 */ 341 public static final String EXTRA_ACTIVE_TETHER = "tetherArray"; 342 343 /** 344 * @hide 345 * gives a String[] listing all the interfaces we tried to tether and 346 * failed. Use {@link #getLastTetherError} to find the error code 347 * for any interfaces listed here. 348 */ 349 public static final String EXTRA_ERRORED_TETHER = "erroredArray"; 350 351 /** 352 * Broadcast Action: The captive portal tracker has finished its test. 353 * Sent only while running Setup Wizard, in lieu of showing a user 354 * notification. 355 * @hide 356 */ 357 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 358 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = 359 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; 360 /** 361 * The lookup key for a boolean that indicates whether a captive portal was detected. 362 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. 363 * @hide 364 */ 365 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; 366 367 /** 368 * Action used to display a dialog that asks the user whether to connect to a network that is 369 * not validated. This intent is used to start the dialog in settings via startActivity. 370 * 371 * @hide 372 */ 373 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; 374 375 /** 376 * Action used to display a dialog that asks the user whether to avoid a network that is no 377 * longer validated. This intent is used to start the dialog in settings via startActivity. 378 * 379 * @hide 380 */ 381 public static final String ACTION_PROMPT_LOST_VALIDATION = 382 "android.net.conn.PROMPT_LOST_VALIDATION"; 383 384 /** 385 * Invalid tethering type. 386 * @see #startTethering(int, OnStartTetheringCallback, boolean) 387 * @hide 388 */ 389 public static final int TETHERING_INVALID = -1; 390 391 /** 392 * Wifi tethering type. 393 * @see #startTethering(int, OnStartTetheringCallback, boolean) 394 * @hide 395 */ 396 @SystemApi 397 public static final int TETHERING_WIFI = 0; 398 399 /** 400 * USB tethering type. 401 * @see #startTethering(int, OnStartTetheringCallback, boolean) 402 * @hide 403 */ 404 @SystemApi 405 public static final int TETHERING_USB = 1; 406 407 /** 408 * Bluetooth tethering type. 409 * @see #startTethering(int, OnStartTetheringCallback, boolean) 410 * @hide 411 */ 412 @SystemApi 413 public static final int TETHERING_BLUETOOTH = 2; 414 415 /** 416 * Extra used for communicating with the TetherService. Includes the type of tethering to 417 * enable if any. 418 * @hide 419 */ 420 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType"; 421 422 /** 423 * Extra used for communicating with the TetherService. Includes the type of tethering for 424 * which to cancel provisioning. 425 * @hide 426 */ 427 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType"; 428 429 /** 430 * Extra used for communicating with the TetherService. True to schedule a recheck of tether 431 * provisioning. 432 * @hide 433 */ 434 public static final String EXTRA_SET_ALARM = "extraSetAlarm"; 435 436 /** 437 * Tells the TetherService to run a provision check now. 438 * @hide 439 */ 440 public static final String EXTRA_RUN_PROVISION = "extraRunProvision"; 441 442 /** 443 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver} 444 * which will receive provisioning results. Can be left empty. 445 * @hide 446 */ 447 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback"; 448 449 /** 450 * The absence of a connection type. 451 * @hide 452 */ 453 public static final int TYPE_NONE = -1; 454 455 /** 456 * A Mobile data connection. Devices may support more than one. 457 * 458 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or 459 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an 460 * appropriate network. {@see NetworkCapabilities} for supported transports. 461 */ 462 @Deprecated 463 public static final int TYPE_MOBILE = 0; 464 465 /** 466 * A WIFI data connection. Devices may support more than one. 467 * 468 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or 469 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an 470 * appropriate network. {@see NetworkCapabilities} for supported transports. 471 */ 472 @Deprecated 473 public static final int TYPE_WIFI = 1; 474 475 /** 476 * An MMS-specific Mobile data connection. This network type may use the 477 * same network interface as {@link #TYPE_MOBILE} or it may use a different 478 * one. This is used by applications needing to talk to the carrier's 479 * Multimedia Messaging Service servers. 480 * 481 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or 482 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that 483 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. 484 */ 485 @Deprecated 486 public static final int TYPE_MOBILE_MMS = 2; 487 488 /** 489 * A SUPL-specific Mobile data connection. This network type may use the 490 * same network interface as {@link #TYPE_MOBILE} or it may use a different 491 * one. This is used by applications needing to talk to the carrier's 492 * Secure User Plane Location servers for help locating the device. 493 * 494 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or 495 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that 496 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. 497 */ 498 @Deprecated 499 public static final int TYPE_MOBILE_SUPL = 3; 500 501 /** 502 * A DUN-specific Mobile data connection. This network type may use the 503 * same network interface as {@link #TYPE_MOBILE} or it may use a different 504 * one. This is sometimes by the system when setting up an upstream connection 505 * for tethering so that the carrier is aware of DUN traffic. 506 * 507 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or 508 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that 509 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability. 510 */ 511 @Deprecated 512 public static final int TYPE_MOBILE_DUN = 4; 513 514 /** 515 * A High Priority Mobile data connection. This network type uses the 516 * same network interface as {@link #TYPE_MOBILE} but the routing setup 517 * is different. 518 * 519 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or 520 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an 521 * appropriate network. {@see NetworkCapabilities} for supported transports. 522 */ 523 @Deprecated 524 public static final int TYPE_MOBILE_HIPRI = 5; 525 526 /** 527 * A WiMAX data connection. 528 * 529 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or 530 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an 531 * appropriate network. {@see NetworkCapabilities} for supported transports. 532 */ 533 @Deprecated 534 public static final int TYPE_WIMAX = 6; 535 536 /** 537 * A Bluetooth data connection. 538 * 539 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or 540 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an 541 * appropriate network. {@see NetworkCapabilities} for supported transports. 542 */ 543 @Deprecated 544 public static final int TYPE_BLUETOOTH = 7; 545 546 /** 547 * Dummy data connection. This should not be used on shipping devices. 548 * @deprecated This is not used any more. 549 */ 550 @Deprecated 551 public static final int TYPE_DUMMY = 8; 552 553 /** 554 * An Ethernet data connection. 555 * 556 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or 557 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an 558 * appropriate network. {@see NetworkCapabilities} for supported transports. 559 */ 560 @Deprecated 561 public static final int TYPE_ETHERNET = 9; 562 563 /** 564 * Over the air Administration. 565 * @deprecated Use {@link NetworkCapabilities} instead. 566 * {@hide} 567 */ 568 @Deprecated 569 public static final int TYPE_MOBILE_FOTA = 10; 570 571 /** 572 * IP Multimedia Subsystem. 573 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead. 574 * {@hide} 575 */ 576 @Deprecated 577 public static final int TYPE_MOBILE_IMS = 11; 578 579 /** 580 * Carrier Branded Services. 581 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead. 582 * {@hide} 583 */ 584 @Deprecated 585 public static final int TYPE_MOBILE_CBS = 12; 586 587 /** 588 * A Wi-Fi p2p connection. Only requesting processes will have access to 589 * the peers connected. 590 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead. 591 * {@hide} 592 */ 593 @Deprecated 594 public static final int TYPE_WIFI_P2P = 13; 595 596 /** 597 * The network to use for initially attaching to the network 598 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead. 599 * {@hide} 600 */ 601 @Deprecated 602 public static final int TYPE_MOBILE_IA = 14; 603 604 /** 605 * Emergency PDN connection for emergency services. This 606 * may include IMS and MMS in emergency situations. 607 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead. 608 * {@hide} 609 */ 610 @Deprecated 611 public static final int TYPE_MOBILE_EMERGENCY = 15; 612 613 /** 614 * The network that uses proxy to achieve connectivity. 615 * @deprecated Use {@link NetworkCapabilities} instead. 616 * {@hide} 617 */ 618 @Deprecated 619 public static final int TYPE_PROXY = 16; 620 621 /** 622 * A virtual network using one or more native bearers. 623 * It may or may not be providing security services. 624 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead. 625 */ 626 @Deprecated 627 public static final int TYPE_VPN = 17; 628 629 /** {@hide} */ 630 public static final int MAX_RADIO_TYPE = TYPE_VPN; 631 632 /** {@hide} */ 633 public static final int MAX_NETWORK_TYPE = TYPE_VPN; 634 635 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE; 636 637 /** 638 * If you want to set the default network preference,you can directly 639 * change the networkAttributes array in framework's config.xml. 640 * 641 * @deprecated Since we support so many more networks now, the single 642 * network default network preference can't really express 643 * the hierarchy. Instead, the default is defined by the 644 * networkAttributes in config.xml. You can determine 645 * the current value by calling {@link #getNetworkPreference()} 646 * from an App. 647 */ 648 @Deprecated 649 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; 650 651 /** 652 * @hide 653 */ 654 public static final int REQUEST_ID_UNSET = 0; 655 656 /** 657 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered. 658 * This allows to distinguish when unregistering NetworkCallbacks those that were never 659 * registered and those that were already unregistered. 660 * @hide 661 */ 662 private static final NetworkRequest ALREADY_UNREGISTERED = 663 new NetworkRequest.Builder().clearCapabilities().build(); 664 665 /** 666 * A NetID indicating no Network is selected. 667 * Keep in sync with bionic/libc/dns/include/resolv_netid.h 668 * @hide 669 */ 670 public static final int NETID_UNSET = 0; 671 672 /** 673 * Private DNS Mode values. 674 * 675 * The "private_dns_mode" global setting stores a String value which is 676 * expected to be one of the following. 677 */ 678 679 /** 680 * @hide 681 */ 682 public static final String PRIVATE_DNS_MODE_OFF = "off"; 683 /** 684 * @hide 685 */ 686 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic"; 687 /** 688 * @hide 689 */ 690 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname"; 691 /** 692 * The default Private DNS mode. 693 * 694 * This may change from release to release or may become dependent upon 695 * the capabilities of the underlying platform. 696 * 697 * @hide 698 */ 699 public static final String PRIVATE_DNS_DEFAULT_MODE = PRIVATE_DNS_MODE_OPPORTUNISTIC; 700 701 private final IConnectivityManager mService; 702 /** 703 * A kludge to facilitate static access where a Context pointer isn't available, like in the 704 * case of the static set/getProcessDefaultNetwork methods and from the Network class. 705 * TODO: Remove this after deprecating the static methods in favor of non-static methods or 706 * methods that take a Context argument. 707 */ 708 private static ConnectivityManager sInstance; 709 710 private final Context mContext; 711 712 private INetworkManagementService mNMService; 713 private INetworkPolicyManager mNPManager; 714 715 /** 716 * Tests if a given integer represents a valid network type. 717 * @param networkType the type to be tested 718 * @return a boolean. {@code true} if the type is valid, else {@code false} 719 * @deprecated All APIs accepting a network type are deprecated. There should be no need to 720 * validate a network type. 721 */ 722 @Deprecated 723 public static boolean isNetworkTypeValid(int networkType) { 724 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE; 725 } 726 727 /** 728 * Returns a non-localized string representing a given network type. 729 * ONLY used for debugging output. 730 * @param type the type needing naming 731 * @return a String for the given type, or a string version of the type ("87") 732 * if no name is known. 733 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. 734 * {@hide} 735 */ 736 @Deprecated 737 public static String getNetworkTypeName(int type) { 738 switch (type) { 739 case TYPE_NONE: 740 return "NONE"; 741 case TYPE_MOBILE: 742 return "MOBILE"; 743 case TYPE_WIFI: 744 return "WIFI"; 745 case TYPE_MOBILE_MMS: 746 return "MOBILE_MMS"; 747 case TYPE_MOBILE_SUPL: 748 return "MOBILE_SUPL"; 749 case TYPE_MOBILE_DUN: 750 return "MOBILE_DUN"; 751 case TYPE_MOBILE_HIPRI: 752 return "MOBILE_HIPRI"; 753 case TYPE_WIMAX: 754 return "WIMAX"; 755 case TYPE_BLUETOOTH: 756 return "BLUETOOTH"; 757 case TYPE_DUMMY: 758 return "DUMMY"; 759 case TYPE_ETHERNET: 760 return "ETHERNET"; 761 case TYPE_MOBILE_FOTA: 762 return "MOBILE_FOTA"; 763 case TYPE_MOBILE_IMS: 764 return "MOBILE_IMS"; 765 case TYPE_MOBILE_CBS: 766 return "MOBILE_CBS"; 767 case TYPE_WIFI_P2P: 768 return "WIFI_P2P"; 769 case TYPE_MOBILE_IA: 770 return "MOBILE_IA"; 771 case TYPE_MOBILE_EMERGENCY: 772 return "MOBILE_EMERGENCY"; 773 case TYPE_PROXY: 774 return "PROXY"; 775 case TYPE_VPN: 776 return "VPN"; 777 default: 778 return Integer.toString(type); 779 } 780 } 781 782 /** 783 * Checks if a given type uses the cellular data connection. 784 * This should be replaced in the future by a network property. 785 * @param networkType the type to check 786 * @return a boolean - {@code true} if uses cellular network, else {@code false} 787 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. 788 * {@hide} 789 */ 790 @Deprecated 791 public static boolean isNetworkTypeMobile(int networkType) { 792 switch (networkType) { 793 case TYPE_MOBILE: 794 case TYPE_MOBILE_MMS: 795 case TYPE_MOBILE_SUPL: 796 case TYPE_MOBILE_DUN: 797 case TYPE_MOBILE_HIPRI: 798 case TYPE_MOBILE_FOTA: 799 case TYPE_MOBILE_IMS: 800 case TYPE_MOBILE_CBS: 801 case TYPE_MOBILE_IA: 802 case TYPE_MOBILE_EMERGENCY: 803 return true; 804 default: 805 return false; 806 } 807 } 808 809 /** 810 * Checks if the given network type is backed by a Wi-Fi radio. 811 * 812 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. 813 * @hide 814 */ 815 @Deprecated 816 public static boolean isNetworkTypeWifi(int networkType) { 817 switch (networkType) { 818 case TYPE_WIFI: 819 case TYPE_WIFI_P2P: 820 return true; 821 default: 822 return false; 823 } 824 } 825 826 /** 827 * Specifies the preferred network type. When the device has more 828 * than one type available the preferred network type will be used. 829 * 830 * @param preference the network type to prefer over all others. It is 831 * unspecified what happens to the old preferred network in the 832 * overall ordering. 833 * @deprecated Functionality has been removed as it no longer makes sense, 834 * with many more than two networks - we'd need an array to express 835 * preference. Instead we use dynamic network properties of 836 * the networks to describe their precedence. 837 */ 838 @Deprecated 839 public void setNetworkPreference(int preference) { 840 } 841 842 /** 843 * Retrieves the current preferred network type. 844 * 845 * @return an integer representing the preferred network type 846 * 847 * @deprecated Functionality has been removed as it no longer makes sense, 848 * with many more than two networks - we'd need an array to express 849 * preference. Instead we use dynamic network properties of 850 * the networks to describe their precedence. 851 */ 852 @Deprecated 853 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 854 public int getNetworkPreference() { 855 return TYPE_NONE; 856 } 857 858 /** 859 * Returns details about the currently active default data network. When 860 * connected, this network is the default route for outgoing connections. 861 * You should always check {@link NetworkInfo#isConnected()} before initiating 862 * network traffic. This may return {@code null} when there is no default 863 * network. 864 * Note that if the default network is a VPN, this method will return the 865 * NetworkInfo for one of its underlying networks instead, or null if the 866 * VPN agent did not specify any. Apps interested in learning about VPNs 867 * should use {@link #getNetworkInfo(android.net.Network)} instead. 868 * 869 * @return a {@link NetworkInfo} object for the current default network 870 * or {@code null} if no default network is currently active 871 */ 872 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 873 public NetworkInfo getActiveNetworkInfo() { 874 try { 875 return mService.getActiveNetworkInfo(); 876 } catch (RemoteException e) { 877 throw e.rethrowFromSystemServer(); 878 } 879 } 880 881 /** 882 * Returns a {@link Network} object corresponding to the currently active 883 * default data network. In the event that the current active default data 884 * network disconnects, the returned {@code Network} object will no longer 885 * be usable. This will return {@code null} when there is no default 886 * network. 887 * 888 * @return a {@link Network} object for the current default network or 889 * {@code null} if no default network is currently active 890 */ 891 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 892 public Network getActiveNetwork() { 893 try { 894 return mService.getActiveNetwork(); 895 } catch (RemoteException e) { 896 throw e.rethrowFromSystemServer(); 897 } 898 } 899 900 /** 901 * Returns a {@link Network} object corresponding to the currently active 902 * default data network for a specific UID. In the event that the default data 903 * network disconnects, the returned {@code Network} object will no longer 904 * be usable. This will return {@code null} when there is no default 905 * network for the UID. 906 * 907 * @return a {@link Network} object for the current default network for the 908 * given UID or {@code null} if no default network is currently active 909 * 910 * @hide 911 */ 912 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 913 public Network getActiveNetworkForUid(int uid) { 914 return getActiveNetworkForUid(uid, false); 915 } 916 917 /** {@hide} */ 918 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { 919 try { 920 return mService.getActiveNetworkForUid(uid, ignoreBlocked); 921 } catch (RemoteException e) { 922 throw e.rethrowFromSystemServer(); 923 } 924 } 925 926 /** 927 * Checks if a VPN app supports always-on mode. 928 * 929 * In order to support the always-on feature, an app has to 930 * <ul> 931 * <li>target {@link VERSION_CODES#N API 24} or above, and 932 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON} 933 * meta-data field. 934 * </ul> 935 * 936 * @param userId The identifier of the user for whom the VPN app is installed. 937 * @param vpnPackage The canonical package name of the VPN app. 938 * @return {@code true} if and only if the VPN app exists and supports always-on mode. 939 * @hide 940 */ 941 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) { 942 try { 943 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage); 944 } catch (RemoteException e) { 945 throw e.rethrowFromSystemServer(); 946 } 947 } 948 949 /** 950 * Configures an always-on VPN connection through a specific application. 951 * This connection is automatically granted and persisted after a reboot. 952 * 953 * <p>The designated package should declare a {@link VpnService} in its 954 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE}, 955 * otherwise the call will fail. 956 * 957 * @param userId The identifier of the user to set an always-on VPN for. 958 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null} 959 * to remove an existing always-on VPN configuration. 960 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or 961 * {@code false} otherwise. 962 * @return {@code true} if the package is set as always-on VPN controller; 963 * {@code false} otherwise. 964 * @hide 965 */ 966 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage, 967 boolean lockdownEnabled) { 968 try { 969 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled); 970 } catch (RemoteException e) { 971 throw e.rethrowFromSystemServer(); 972 } 973 } 974 975 /** 976 * Returns the package name of the currently set always-on VPN application. 977 * If there is no always-on VPN set, or the VPN is provided by the system instead 978 * of by an app, {@code null} will be returned. 979 * 980 * @return Package name of VPN controller responsible for always-on VPN, 981 * or {@code null} if none is set. 982 * @hide 983 */ 984 public String getAlwaysOnVpnPackageForUser(int userId) { 985 try { 986 return mService.getAlwaysOnVpnPackage(userId); 987 } catch (RemoteException e) { 988 throw e.rethrowFromSystemServer(); 989 } 990 } 991 992 /** 993 * Returns details about the currently active default data network 994 * for a given uid. This is for internal use only to avoid spying 995 * other apps. 996 * 997 * @return a {@link NetworkInfo} object for the current default network 998 * for the given uid or {@code null} if no default network is 999 * available for the specified uid. 1000 * 1001 * {@hide} 1002 */ 1003 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 1004 public NetworkInfo getActiveNetworkInfoForUid(int uid) { 1005 return getActiveNetworkInfoForUid(uid, false); 1006 } 1007 1008 /** {@hide} */ 1009 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { 1010 try { 1011 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked); 1012 } catch (RemoteException e) { 1013 throw e.rethrowFromSystemServer(); 1014 } 1015 } 1016 1017 /** 1018 * Returns connection status information about a particular 1019 * network type. 1020 * 1021 * @param networkType integer specifying which networkType in 1022 * which you're interested. 1023 * @return a {@link NetworkInfo} object for the requested 1024 * network type or {@code null} if the type is not 1025 * supported by the device. If {@code networkType} is 1026 * TYPE_VPN and a VPN is active for the calling app, 1027 * then this method will try to return one of the 1028 * underlying networks for the VPN or null if the 1029 * VPN agent didn't specify any. 1030 * 1031 * @deprecated This method does not support multiple connected networks 1032 * of the same type. Use {@link #getAllNetworks} and 1033 * {@link #getNetworkInfo(android.net.Network)} instead. 1034 */ 1035 @Deprecated 1036 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1037 public NetworkInfo getNetworkInfo(int networkType) { 1038 try { 1039 return mService.getNetworkInfo(networkType); 1040 } catch (RemoteException e) { 1041 throw e.rethrowFromSystemServer(); 1042 } 1043 } 1044 1045 /** 1046 * Returns connection status information about a particular 1047 * Network. 1048 * 1049 * @param network {@link Network} specifying which network 1050 * in which you're interested. 1051 * @return a {@link NetworkInfo} object for the requested 1052 * network or {@code null} if the {@code Network} 1053 * is not valid. 1054 */ 1055 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1056 public NetworkInfo getNetworkInfo(Network network) { 1057 return getNetworkInfoForUid(network, Process.myUid(), false); 1058 } 1059 1060 /** {@hide} */ 1061 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { 1062 try { 1063 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked); 1064 } catch (RemoteException e) { 1065 throw e.rethrowFromSystemServer(); 1066 } 1067 } 1068 1069 /** 1070 * Returns connection status information about all network 1071 * types supported by the device. 1072 * 1073 * @return an array of {@link NetworkInfo} objects. Check each 1074 * {@link NetworkInfo#getType} for which type each applies. 1075 * 1076 * @deprecated This method does not support multiple connected networks 1077 * of the same type. Use {@link #getAllNetworks} and 1078 * {@link #getNetworkInfo(android.net.Network)} instead. 1079 */ 1080 @Deprecated 1081 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1082 public NetworkInfo[] getAllNetworkInfo() { 1083 try { 1084 return mService.getAllNetworkInfo(); 1085 } catch (RemoteException e) { 1086 throw e.rethrowFromSystemServer(); 1087 } 1088 } 1089 1090 /** 1091 * Returns the {@link Network} object currently serving a given type, or 1092 * null if the given type is not connected. 1093 * 1094 * @hide 1095 * @deprecated This method does not support multiple connected networks 1096 * of the same type. Use {@link #getAllNetworks} and 1097 * {@link #getNetworkInfo(android.net.Network)} instead. 1098 */ 1099 @Deprecated 1100 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1101 public Network getNetworkForType(int networkType) { 1102 try { 1103 return mService.getNetworkForType(networkType); 1104 } catch (RemoteException e) { 1105 throw e.rethrowFromSystemServer(); 1106 } 1107 } 1108 1109 /** 1110 * Returns an array of all {@link Network} currently tracked by the 1111 * framework. 1112 * 1113 * @return an array of {@link Network} objects. 1114 */ 1115 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1116 public Network[] getAllNetworks() { 1117 try { 1118 return mService.getAllNetworks(); 1119 } catch (RemoteException e) { 1120 throw e.rethrowFromSystemServer(); 1121 } 1122 } 1123 1124 /** 1125 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing 1126 * the Networks that applications run by the given user will use by default. 1127 * @hide 1128 */ 1129 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { 1130 try { 1131 return mService.getDefaultNetworkCapabilitiesForUser(userId); 1132 } catch (RemoteException e) { 1133 throw e.rethrowFromSystemServer(); 1134 } 1135 } 1136 1137 /** 1138 * Returns the IP information for the current default network. 1139 * 1140 * @return a {@link LinkProperties} object describing the IP info 1141 * for the current default network, or {@code null} if there 1142 * is no current default network. 1143 * 1144 * {@hide} 1145 */ 1146 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1147 public LinkProperties getActiveLinkProperties() { 1148 try { 1149 return mService.getActiveLinkProperties(); 1150 } catch (RemoteException e) { 1151 throw e.rethrowFromSystemServer(); 1152 } 1153 } 1154 1155 /** 1156 * Returns the IP information for a given network type. 1157 * 1158 * @param networkType the network type of interest. 1159 * @return a {@link LinkProperties} object describing the IP info 1160 * for the given networkType, or {@code null} if there is 1161 * no current default network. 1162 * 1163 * {@hide} 1164 * @deprecated This method does not support multiple connected networks 1165 * of the same type. Use {@link #getAllNetworks}, 1166 * {@link #getNetworkInfo(android.net.Network)}, and 1167 * {@link #getLinkProperties(android.net.Network)} instead. 1168 */ 1169 @Deprecated 1170 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1171 public LinkProperties getLinkProperties(int networkType) { 1172 try { 1173 return mService.getLinkPropertiesForType(networkType); 1174 } catch (RemoteException e) { 1175 throw e.rethrowFromSystemServer(); 1176 } 1177 } 1178 1179 /** 1180 * Get the {@link LinkProperties} for the given {@link Network}. This 1181 * will return {@code null} if the network is unknown. 1182 * 1183 * @param network The {@link Network} object identifying the network in question. 1184 * @return The {@link LinkProperties} for the network, or {@code null}. 1185 */ 1186 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1187 public LinkProperties getLinkProperties(Network network) { 1188 try { 1189 return mService.getLinkProperties(network); 1190 } catch (RemoteException e) { 1191 throw e.rethrowFromSystemServer(); 1192 } 1193 } 1194 1195 /** 1196 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This 1197 * will return {@code null} if the network is unknown. 1198 * 1199 * @param network The {@link Network} object identifying the network in question. 1200 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}. 1201 */ 1202 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 1203 public NetworkCapabilities getNetworkCapabilities(Network network) { 1204 try { 1205 return mService.getNetworkCapabilities(network); 1206 } catch (RemoteException e) { 1207 throw e.rethrowFromSystemServer(); 1208 } 1209 } 1210 1211 /** 1212 * Gets the URL that should be used for resolving whether a captive portal is present. 1213 * 1. This URL should respond with a 204 response to a GET request to indicate no captive 1214 * portal is present. 1215 * 2. This URL must be HTTP as redirect responses are used to find captive portal 1216 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects. 1217 * 1218 * @hide 1219 */ 1220 @SystemApi 1221 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS) 1222 public String getCaptivePortalServerUrl() { 1223 try { 1224 return mService.getCaptivePortalServerUrl(); 1225 } catch (RemoteException e) { 1226 throw e.rethrowFromSystemServer(); 1227 } 1228 } 1229 1230 /** 1231 * Tells the underlying networking system that the caller wants to 1232 * begin using the named feature. The interpretation of {@code feature} 1233 * is completely up to each networking implementation. 1234 * 1235 * <p>This method requires the caller to hold either the 1236 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 1237 * or the ability to modify system settings as determined by 1238 * {@link android.provider.Settings.System#canWrite}.</p> 1239 * 1240 * @param networkType specifies which network the request pertains to 1241 * @param feature the name of the feature to be used 1242 * @return an integer value representing the outcome of the request. 1243 * The interpretation of this value is specific to each networking 1244 * implementation+feature combination, except that the value {@code -1} 1245 * always indicates failure. 1246 * 1247 * @deprecated Deprecated in favor of the cleaner 1248 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. 1249 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will 1250 * throw {@code UnsupportedOperationException} if called. 1251 * @removed 1252 */ 1253 @Deprecated 1254 public int startUsingNetworkFeature(int networkType, String feature) { 1255 checkLegacyRoutingApiAccess(); 1256 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); 1257 if (netCap == null) { 1258 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + 1259 feature); 1260 return PhoneConstants.APN_REQUEST_FAILED; 1261 } 1262 1263 NetworkRequest request = null; 1264 synchronized (sLegacyRequests) { 1265 LegacyRequest l = sLegacyRequests.get(netCap); 1266 if (l != null) { 1267 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); 1268 renewRequestLocked(l); 1269 if (l.currentNetwork != null) { 1270 return PhoneConstants.APN_ALREADY_ACTIVE; 1271 } else { 1272 return PhoneConstants.APN_REQUEST_STARTED; 1273 } 1274 } 1275 1276 request = requestNetworkForFeatureLocked(netCap); 1277 } 1278 if (request != null) { 1279 Log.d(TAG, "starting startUsingNetworkFeature for request " + request); 1280 return PhoneConstants.APN_REQUEST_STARTED; 1281 } else { 1282 Log.d(TAG, " request Failed"); 1283 return PhoneConstants.APN_REQUEST_FAILED; 1284 } 1285 } 1286 1287 /** 1288 * Tells the underlying networking system that the caller is finished 1289 * using the named feature. The interpretation of {@code feature} 1290 * is completely up to each networking implementation. 1291 * 1292 * <p>This method requires the caller to hold either the 1293 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 1294 * or the ability to modify system settings as determined by 1295 * {@link android.provider.Settings.System#canWrite}.</p> 1296 * 1297 * @param networkType specifies which network the request pertains to 1298 * @param feature the name of the feature that is no longer needed 1299 * @return an integer value representing the outcome of the request. 1300 * The interpretation of this value is specific to each networking 1301 * implementation+feature combination, except that the value {@code -1} 1302 * always indicates failure. 1303 * 1304 * @deprecated Deprecated in favor of the cleaner 1305 * {@link #unregisterNetworkCallback(NetworkCallback)} API. 1306 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will 1307 * throw {@code UnsupportedOperationException} if called. 1308 * @removed 1309 */ 1310 @Deprecated 1311 public int stopUsingNetworkFeature(int networkType, String feature) { 1312 checkLegacyRoutingApiAccess(); 1313 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); 1314 if (netCap == null) { 1315 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + 1316 feature); 1317 return -1; 1318 } 1319 1320 if (removeRequestForFeature(netCap)) { 1321 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); 1322 } 1323 return 1; 1324 } 1325 1326 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { 1327 if (networkType == TYPE_MOBILE) { 1328 switch (feature) { 1329 case "enableCBS": 1330 return networkCapabilitiesForType(TYPE_MOBILE_CBS); 1331 case "enableDUN": 1332 case "enableDUNAlways": 1333 return networkCapabilitiesForType(TYPE_MOBILE_DUN); 1334 case "enableFOTA": 1335 return networkCapabilitiesForType(TYPE_MOBILE_FOTA); 1336 case "enableHIPRI": 1337 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI); 1338 case "enableIMS": 1339 return networkCapabilitiesForType(TYPE_MOBILE_IMS); 1340 case "enableMMS": 1341 return networkCapabilitiesForType(TYPE_MOBILE_MMS); 1342 case "enableSUPL": 1343 return networkCapabilitiesForType(TYPE_MOBILE_SUPL); 1344 default: 1345 return null; 1346 } 1347 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) { 1348 return networkCapabilitiesForType(TYPE_WIFI_P2P); 1349 } 1350 return null; 1351 } 1352 1353 /** 1354 * Guess what the network request was trying to say so that the resulting 1355 * network is accessible via the legacy (deprecated) API such as 1356 * requestRouteToHost. 1357 * 1358 * This means we should try to be fairly precise about transport and 1359 * capability but ignore things such as networkSpecifier. 1360 * If the request has more than one transport or capability it doesn't 1361 * match the old legacy requests (they selected only single transport/capability) 1362 * so this function cannot map the request to a single legacy type and 1363 * the resulting network will not be available to the legacy APIs. 1364 * 1365 * This code is only called from the requestNetwork API (L and above). 1366 * 1367 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive 1368 * because they wake up lots of apps - see http://b/23350688 . So we currently only 1369 * do this for SUPL requests, which are the only ones that we know need it. If 1370 * omitting these broadcasts causes unacceptable app breakage, then for backwards 1371 * compatibility we can send them: 1372 * 1373 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M 1374 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L 1375 * 1376 * TODO - This should be removed when the legacy APIs are removed. 1377 */ 1378 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { 1379 if (netCap == null) { 1380 return TYPE_NONE; 1381 } 1382 1383 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { 1384 return TYPE_NONE; 1385 } 1386 1387 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 . 1388 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { 1389 // NOTE: if this causes app breakage, we should not just comment out this early return; 1390 // instead, we should make this early return conditional on the requesting app's target 1391 // SDK version, as described in the comment above. 1392 return TYPE_NONE; 1393 } 1394 1395 String type = null; 1396 int result = TYPE_NONE; 1397 1398 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { 1399 type = "enableCBS"; 1400 result = TYPE_MOBILE_CBS; 1401 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { 1402 type = "enableIMS"; 1403 result = TYPE_MOBILE_IMS; 1404 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { 1405 type = "enableFOTA"; 1406 result = TYPE_MOBILE_FOTA; 1407 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { 1408 type = "enableDUN"; 1409 result = TYPE_MOBILE_DUN; 1410 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { 1411 type = "enableSUPL"; 1412 result = TYPE_MOBILE_SUPL; 1413 // back out this hack for mms as they no longer need this and it's causing 1414 // device slowdowns - b/23350688 (note, supl still needs this) 1415 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { 1416 // type = "enableMMS"; 1417 // result = TYPE_MOBILE_MMS; 1418 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { 1419 type = "enableHIPRI"; 1420 result = TYPE_MOBILE_HIPRI; 1421 } 1422 if (type != null) { 1423 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type); 1424 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) { 1425 return result; 1426 } 1427 } 1428 return TYPE_NONE; 1429 } 1430 1431 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { 1432 if (netCap == null) return TYPE_NONE; 1433 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { 1434 return TYPE_MOBILE_CBS; 1435 } 1436 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { 1437 return TYPE_MOBILE_IMS; 1438 } 1439 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { 1440 return TYPE_MOBILE_FOTA; 1441 } 1442 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { 1443 return TYPE_MOBILE_DUN; 1444 } 1445 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { 1446 return TYPE_MOBILE_SUPL; 1447 } 1448 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { 1449 return TYPE_MOBILE_MMS; 1450 } 1451 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { 1452 return TYPE_MOBILE_HIPRI; 1453 } 1454 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { 1455 return TYPE_WIFI_P2P; 1456 } 1457 return TYPE_NONE; 1458 } 1459 1460 private static class LegacyRequest { 1461 NetworkCapabilities networkCapabilities; 1462 NetworkRequest networkRequest; 1463 int expireSequenceNumber; 1464 Network currentNetwork; 1465 int delay = -1; 1466 1467 private void clearDnsBinding() { 1468 if (currentNetwork != null) { 1469 currentNetwork = null; 1470 setProcessDefaultNetworkForHostResolution(null); 1471 } 1472 } 1473 1474 NetworkCallback networkCallback = new NetworkCallback() { 1475 @Override 1476 public void onAvailable(Network network) { 1477 currentNetwork = network; 1478 Log.d(TAG, "startUsingNetworkFeature got Network:" + network); 1479 setProcessDefaultNetworkForHostResolution(network); 1480 } 1481 @Override 1482 public void onLost(Network network) { 1483 if (network.equals(currentNetwork)) clearDnsBinding(); 1484 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); 1485 } 1486 }; 1487 } 1488 1489 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = 1490 new HashMap<NetworkCapabilities, LegacyRequest>(); 1491 1492 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { 1493 synchronized (sLegacyRequests) { 1494 LegacyRequest l = sLegacyRequests.get(netCap); 1495 if (l != null) return l.networkRequest; 1496 } 1497 return null; 1498 } 1499 1500 private void renewRequestLocked(LegacyRequest l) { 1501 l.expireSequenceNumber++; 1502 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); 1503 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); 1504 } 1505 1506 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { 1507 int ourSeqNum = -1; 1508 synchronized (sLegacyRequests) { 1509 LegacyRequest l = sLegacyRequests.get(netCap); 1510 if (l == null) return; 1511 ourSeqNum = l.expireSequenceNumber; 1512 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); 1513 } 1514 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); 1515 } 1516 1517 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { 1518 int delay = -1; 1519 int type = legacyTypeForNetworkCapabilities(netCap); 1520 try { 1521 delay = mService.getRestoreDefaultNetworkDelay(type); 1522 } catch (RemoteException e) { 1523 throw e.rethrowFromSystemServer(); 1524 } 1525 LegacyRequest l = new LegacyRequest(); 1526 l.networkCapabilities = netCap; 1527 l.delay = delay; 1528 l.expireSequenceNumber = 0; 1529 l.networkRequest = sendRequestForNetwork( 1530 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler()); 1531 if (l.networkRequest == null) return null; 1532 sLegacyRequests.put(netCap, l); 1533 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); 1534 return l.networkRequest; 1535 } 1536 1537 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { 1538 if (delay >= 0) { 1539 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); 1540 CallbackHandler handler = getDefaultHandler(); 1541 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); 1542 handler.sendMessageDelayed(msg, delay); 1543 } 1544 } 1545 1546 private boolean removeRequestForFeature(NetworkCapabilities netCap) { 1547 final LegacyRequest l; 1548 synchronized (sLegacyRequests) { 1549 l = sLegacyRequests.remove(netCap); 1550 } 1551 if (l == null) return false; 1552 unregisterNetworkCallback(l.networkCallback); 1553 l.clearDnsBinding(); 1554 return true; 1555 } 1556 1557 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray(); 1558 static { 1559 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR); 1560 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR); 1561 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR); 1562 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR); 1563 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR); 1564 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR); 1565 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR); 1566 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR); 1567 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI); 1568 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI); 1569 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH); 1570 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET); 1571 } 1572 1573 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray(); 1574 static { 1575 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS); 1576 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN); 1577 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA); 1578 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS); 1579 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS); 1580 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL); 1581 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); 1582 } 1583 1584 /** 1585 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities 1586 * instance suitable for registering a request or callback. Throws an 1587 * IllegalArgumentException if no mapping from the legacy type to 1588 * NetworkCapabilities is known. 1589 * 1590 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest} 1591 * to find the network instead. 1592 * @hide 1593 */ 1594 public static NetworkCapabilities networkCapabilitiesForType(int type) { 1595 final NetworkCapabilities nc = new NetworkCapabilities(); 1596 1597 // Map from type to transports. 1598 final int NOT_FOUND = -1; 1599 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND); 1600 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type); 1601 nc.addTransportType(transport); 1602 1603 // Map from type to capabilities. 1604 nc.addCapability(sLegacyTypeToCapability.get( 1605 type, NetworkCapabilities.NET_CAPABILITY_INTERNET)); 1606 nc.maybeMarkCapabilitiesRestricted(); 1607 return nc; 1608 } 1609 1610 /** @hide */ 1611 public static class PacketKeepaliveCallback { 1612 /** The requested keepalive was successfully started. */ 1613 public void onStarted() {} 1614 /** The keepalive was successfully stopped. */ 1615 public void onStopped() {} 1616 /** An error occurred. */ 1617 public void onError(int error) {} 1618 } 1619 1620 /** 1621 * Allows applications to request that the system periodically send specific packets on their 1622 * behalf, using hardware offload to save battery power. 1623 * 1624 * To request that the system send keepalives, call one of the methods that return a 1625 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, 1626 * passing in a non-null callback. If the callback is successfully started, the callback's 1627 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, 1628 * specifying one of the {@code ERROR_*} constants in this class. 1629 * 1630 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if 1631 * the operation was successfull or {@code onError} if an error occurred. 1632 * 1633 * @hide 1634 */ 1635 public class PacketKeepalive { 1636 1637 private static final String TAG = "PacketKeepalive"; 1638 1639 /** @hide */ 1640 public static final int SUCCESS = 0; 1641 1642 /** @hide */ 1643 public static final int NO_KEEPALIVE = -1; 1644 1645 /** @hide */ 1646 public static final int BINDER_DIED = -10; 1647 1648 /** The specified {@code Network} is not connected. */ 1649 public static final int ERROR_INVALID_NETWORK = -20; 1650 /** The specified IP addresses are invalid. For example, the specified source IP address is 1651 * not configured on the specified {@code Network}. */ 1652 public static final int ERROR_INVALID_IP_ADDRESS = -21; 1653 /** The requested port is invalid. */ 1654 public static final int ERROR_INVALID_PORT = -22; 1655 /** The packet length is invalid (e.g., too long). */ 1656 public static final int ERROR_INVALID_LENGTH = -23; 1657 /** The packet transmission interval is invalid (e.g., too short). */ 1658 public static final int ERROR_INVALID_INTERVAL = -24; 1659 1660 /** The hardware does not support this request. */ 1661 public static final int ERROR_HARDWARE_UNSUPPORTED = -30; 1662 /** The hardware returned an error. */ 1663 public static final int ERROR_HARDWARE_ERROR = -31; 1664 1665 /** The NAT-T destination port for IPsec */ 1666 public static final int NATT_PORT = 4500; 1667 1668 /** The minimum interval in seconds between keepalive packet transmissions */ 1669 public static final int MIN_INTERVAL = 10; 1670 1671 private final Network mNetwork; 1672 private final PacketKeepaliveCallback mCallback; 1673 private final Looper mLooper; 1674 private final Messenger mMessenger; 1675 1676 private volatile Integer mSlot; 1677 1678 void stopLooper() { 1679 mLooper.quit(); 1680 } 1681 1682 public void stop() { 1683 try { 1684 mService.stopKeepalive(mNetwork, mSlot); 1685 } catch (RemoteException e) { 1686 Log.e(TAG, "Error stopping packet keepalive: ", e); 1687 stopLooper(); 1688 } 1689 } 1690 1691 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { 1692 Preconditions.checkNotNull(network, "network cannot be null"); 1693 Preconditions.checkNotNull(callback, "callback cannot be null"); 1694 mNetwork = network; 1695 mCallback = callback; 1696 HandlerThread thread = new HandlerThread(TAG); 1697 thread.start(); 1698 mLooper = thread.getLooper(); 1699 mMessenger = new Messenger(new Handler(mLooper) { 1700 @Override 1701 public void handleMessage(Message message) { 1702 switch (message.what) { 1703 case NetworkAgent.EVENT_PACKET_KEEPALIVE: 1704 int error = message.arg2; 1705 try { 1706 if (error == SUCCESS) { 1707 if (mSlot == null) { 1708 mSlot = message.arg1; 1709 mCallback.onStarted(); 1710 } else { 1711 mSlot = null; 1712 stopLooper(); 1713 mCallback.onStopped(); 1714 } 1715 } else { 1716 stopLooper(); 1717 mCallback.onError(error); 1718 } 1719 } catch (Exception e) { 1720 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e); 1721 } 1722 break; 1723 default: 1724 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what)); 1725 break; 1726 } 1727 } 1728 }); 1729 } 1730 } 1731 1732 /** 1733 * Starts an IPsec NAT-T keepalive packet with the specified parameters. 1734 * 1735 * @hide 1736 */ 1737 public PacketKeepalive startNattKeepalive( 1738 Network network, int intervalSeconds, PacketKeepaliveCallback callback, 1739 InetAddress srcAddr, int srcPort, InetAddress dstAddr) { 1740 final PacketKeepalive k = new PacketKeepalive(network, callback); 1741 try { 1742 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(), 1743 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); 1744 } catch (RemoteException e) { 1745 Log.e(TAG, "Error starting packet keepalive: ", e); 1746 k.stopLooper(); 1747 return null; 1748 } 1749 return k; 1750 } 1751 1752 /** 1753 * Ensure that a network route exists to deliver traffic to the specified 1754 * host via the specified network interface. An attempt to add a route that 1755 * already exists is ignored, but treated as successful. 1756 * 1757 * <p>This method requires the caller to hold either the 1758 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 1759 * or the ability to modify system settings as determined by 1760 * {@link android.provider.Settings.System#canWrite}.</p> 1761 * 1762 * @param networkType the type of the network over which traffic to the specified 1763 * host is to be routed 1764 * @param hostAddress the IP address of the host to which the route is desired 1765 * @return {@code true} on success, {@code false} on failure 1766 * 1767 * @deprecated Deprecated in favor of the 1768 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, 1769 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. 1770 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will 1771 * throw {@code UnsupportedOperationException} if called. 1772 * @removed 1773 */ 1774 @Deprecated 1775 public boolean requestRouteToHost(int networkType, int hostAddress) { 1776 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); 1777 } 1778 1779 /** 1780 * Ensure that a network route exists to deliver traffic to the specified 1781 * host via the specified network interface. An attempt to add a route that 1782 * already exists is ignored, but treated as successful. 1783 * 1784 * <p>This method requires the caller to hold either the 1785 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 1786 * or the ability to modify system settings as determined by 1787 * {@link android.provider.Settings.System#canWrite}.</p> 1788 * 1789 * @param networkType the type of the network over which traffic to the specified 1790 * host is to be routed 1791 * @param hostAddress the IP address of the host to which the route is desired 1792 * @return {@code true} on success, {@code false} on failure 1793 * @hide 1794 * @deprecated Deprecated in favor of the {@link #requestNetwork} and 1795 * {@link #bindProcessToNetwork} API. 1796 */ 1797 @Deprecated 1798 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { 1799 checkLegacyRoutingApiAccess(); 1800 try { 1801 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress()); 1802 } catch (RemoteException e) { 1803 throw e.rethrowFromSystemServer(); 1804 } 1805 } 1806 1807 /** 1808 * Returns the value of the setting for background data usage. If false, 1809 * applications should not use the network if the application is not in the 1810 * foreground. Developers should respect this setting, and check the value 1811 * of this before performing any background data operations. 1812 * <p> 1813 * All applications that have background services that use the network 1814 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. 1815 * <p> 1816 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of 1817 * background data depends on several combined factors, and this method will 1818 * always return {@code true}. Instead, when background data is unavailable, 1819 * {@link #getActiveNetworkInfo()} will now appear disconnected. 1820 * 1821 * @return Whether background data usage is allowed. 1822 */ 1823 @Deprecated 1824 public boolean getBackgroundDataSetting() { 1825 // assume that background data is allowed; final authority is 1826 // NetworkInfo which may be blocked. 1827 return true; 1828 } 1829 1830 /** 1831 * Sets the value of the setting for background data usage. 1832 * 1833 * @param allowBackgroundData Whether an application should use data while 1834 * it is in the background. 1835 * 1836 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING 1837 * @see #getBackgroundDataSetting() 1838 * @hide 1839 */ 1840 @Deprecated 1841 public void setBackgroundDataSetting(boolean allowBackgroundData) { 1842 // ignored 1843 } 1844 1845 /** {@hide} */ 1846 @Deprecated 1847 public NetworkQuotaInfo getActiveNetworkQuotaInfo() { 1848 try { 1849 return mService.getActiveNetworkQuotaInfo(); 1850 } catch (RemoteException e) { 1851 throw e.rethrowFromSystemServer(); 1852 } 1853 } 1854 1855 /** 1856 * @hide 1857 * @deprecated Talk to TelephonyManager directly 1858 */ 1859 @Deprecated 1860 public boolean getMobileDataEnabled() { 1861 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE); 1862 if (b != null) { 1863 try { 1864 ITelephony it = ITelephony.Stub.asInterface(b); 1865 int subId = SubscriptionManager.getDefaultDataSubscriptionId(); 1866 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); 1867 boolean retVal = it.isUserDataEnabled(subId); 1868 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId 1869 + " retVal=" + retVal); 1870 return retVal; 1871 } catch (RemoteException e) { 1872 throw e.rethrowFromSystemServer(); 1873 } 1874 } 1875 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); 1876 return false; 1877 } 1878 1879 /** 1880 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} 1881 * to find out when the system default network has gone in to a high power state. 1882 */ 1883 public interface OnNetworkActiveListener { 1884 /** 1885 * Called on the main thread of the process to report that the current data network 1886 * has become active, and it is now a good time to perform any pending network 1887 * operations. Note that this listener only tells you when the network becomes 1888 * active; if at any other time you want to know whether it is active (and thus okay 1889 * to initiate network traffic), you can retrieve its instantaneous state with 1890 * {@link ConnectivityManager#isDefaultNetworkActive}. 1891 */ 1892 public void onNetworkActive(); 1893 } 1894 1895 private INetworkManagementService getNetworkManagementService() { 1896 synchronized (this) { 1897 if (mNMService != null) { 1898 return mNMService; 1899 } 1900 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); 1901 mNMService = INetworkManagementService.Stub.asInterface(b); 1902 return mNMService; 1903 } 1904 } 1905 1906 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> 1907 mNetworkActivityListeners 1908 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>(); 1909 1910 /** 1911 * Start listening to reports when the system's default data network is active, meaning it is 1912 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} 1913 * to determine the current state of the system's default network after registering the 1914 * listener. 1915 * <p> 1916 * If the process default network has been set with 1917 * {@link ConnectivityManager#bindProcessToNetwork} this function will not 1918 * reflect the process's default, but the system default. 1919 * 1920 * @param l The listener to be told when the network is active. 1921 */ 1922 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { 1923 INetworkActivityListener rl = new INetworkActivityListener.Stub() { 1924 @Override 1925 public void onNetworkActive() throws RemoteException { 1926 l.onNetworkActive(); 1927 } 1928 }; 1929 1930 try { 1931 getNetworkManagementService().registerNetworkActivityListener(rl); 1932 mNetworkActivityListeners.put(l, rl); 1933 } catch (RemoteException e) { 1934 throw e.rethrowFromSystemServer(); 1935 } 1936 } 1937 1938 /** 1939 * Remove network active listener previously registered with 1940 * {@link #addDefaultNetworkActiveListener}. 1941 * 1942 * @param l Previously registered listener. 1943 */ 1944 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) { 1945 INetworkActivityListener rl = mNetworkActivityListeners.get(l); 1946 Preconditions.checkArgument(rl != null, "Listener was not registered."); 1947 try { 1948 getNetworkManagementService().unregisterNetworkActivityListener(rl); 1949 } catch (RemoteException e) { 1950 throw e.rethrowFromSystemServer(); 1951 } 1952 } 1953 1954 /** 1955 * Return whether the data network is currently active. An active network means that 1956 * it is currently in a high power state for performing data transmission. On some 1957 * types of networks, it may be expensive to move and stay in such a state, so it is 1958 * more power efficient to batch network traffic together when the radio is already in 1959 * this state. This method tells you whether right now is currently a good time to 1960 * initiate network traffic, as the network is already active. 1961 */ 1962 public boolean isDefaultNetworkActive() { 1963 try { 1964 return getNetworkManagementService().isNetworkActive(); 1965 } catch (RemoteException e) { 1966 throw e.rethrowFromSystemServer(); 1967 } 1968 } 1969 1970 /** 1971 * {@hide} 1972 */ 1973 public ConnectivityManager(Context context, IConnectivityManager service) { 1974 mContext = Preconditions.checkNotNull(context, "missing context"); 1975 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager"); 1976 sInstance = this; 1977 } 1978 1979 /** {@hide} */ 1980 public static ConnectivityManager from(Context context) { 1981 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 1982 } 1983 1984 /* TODO: These permissions checks don't belong in client-side code. Move them to 1985 * services.jar, possibly in com.android.server.net. */ 1986 1987 /** {@hide} */ 1988 public static final void enforceChangePermission(Context context) { 1989 int uid = Binder.getCallingUid(); 1990 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings 1991 .getPackageNameForUid(context, uid), true /* throwException */); 1992 } 1993 1994 /** {@hide} */ 1995 public static final void enforceTetherChangePermission(Context context, String callingPkg) { 1996 Preconditions.checkNotNull(context, "Context cannot be null"); 1997 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null"); 1998 1999 if (context.getResources().getStringArray( 2000 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) { 2001 // Have a provisioning app - must only let system apps (which check this app) 2002 // turn on tethering 2003 context.enforceCallingOrSelfPermission( 2004 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService"); 2005 } else { 2006 int uid = Binder.getCallingUid(); 2007 // If callingPkg's uid is not same as Binder.getCallingUid(), 2008 // AppOpsService throws SecurityException. 2009 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg, 2010 true /* throwException */); 2011 } 2012 } 2013 2014 /** 2015 * @deprecated - use getSystemService. This is a kludge to support static access in certain 2016 * situations where a Context pointer is unavailable. 2017 * @hide 2018 */ 2019 @Deprecated 2020 static ConnectivityManager getInstanceOrNull() { 2021 return sInstance; 2022 } 2023 2024 /** 2025 * @deprecated - use getSystemService. This is a kludge to support static access in certain 2026 * situations where a Context pointer is unavailable. 2027 * @hide 2028 */ 2029 @Deprecated 2030 private static ConnectivityManager getInstance() { 2031 if (getInstanceOrNull() == null) { 2032 throw new IllegalStateException("No ConnectivityManager yet constructed"); 2033 } 2034 return getInstanceOrNull(); 2035 } 2036 2037 /** 2038 * Get the set of tetherable, available interfaces. This list is limited by 2039 * device configuration and current interface existence. 2040 * 2041 * @return an array of 0 or more Strings of tetherable interface names. 2042 * 2043 * {@hide} 2044 */ 2045 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2046 public String[] getTetherableIfaces() { 2047 try { 2048 return mService.getTetherableIfaces(); 2049 } catch (RemoteException e) { 2050 throw e.rethrowFromSystemServer(); 2051 } 2052 } 2053 2054 /** 2055 * Get the set of tethered interfaces. 2056 * 2057 * @return an array of 0 or more String of currently tethered interface names. 2058 * 2059 * {@hide} 2060 */ 2061 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2062 public String[] getTetheredIfaces() { 2063 try { 2064 return mService.getTetheredIfaces(); 2065 } catch (RemoteException e) { 2066 throw e.rethrowFromSystemServer(); 2067 } 2068 } 2069 2070 /** 2071 * Get the set of interface names which attempted to tether but 2072 * failed. Re-attempting to tether may cause them to reset to the Tethered 2073 * state. Alternatively, causing the interface to be destroyed and recreated 2074 * may cause them to reset to the available state. 2075 * {@link ConnectivityManager#getLastTetherError} can be used to get more 2076 * information on the cause of the errors. 2077 * 2078 * @return an array of 0 or more String indicating the interface names 2079 * which failed to tether. 2080 * 2081 * {@hide} 2082 */ 2083 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2084 public String[] getTetheringErroredIfaces() { 2085 try { 2086 return mService.getTetheringErroredIfaces(); 2087 } catch (RemoteException e) { 2088 throw e.rethrowFromSystemServer(); 2089 } 2090 } 2091 2092 /** 2093 * Get the set of tethered dhcp ranges. 2094 * 2095 * @return an array of 0 or more {@code String} of tethered dhcp ranges. 2096 * {@hide} 2097 */ 2098 public String[] getTetheredDhcpRanges() { 2099 try { 2100 return mService.getTetheredDhcpRanges(); 2101 } catch (RemoteException e) { 2102 throw e.rethrowFromSystemServer(); 2103 } 2104 } 2105 2106 /** 2107 * Attempt to tether the named interface. This will setup a dhcp server 2108 * on the interface, forward and NAT IP packets and forward DNS requests 2109 * to the best active upstream network interface. Note that if no upstream 2110 * IP network interface is available, dhcp will still run and traffic will be 2111 * allowed between the tethered devices and this device, though upstream net 2112 * access will of course fail until an upstream network interface becomes 2113 * active. 2114 * 2115 * <p>This method requires the caller to hold either the 2116 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 2117 * or the ability to modify system settings as determined by 2118 * {@link android.provider.Settings.System#canWrite}.</p> 2119 * 2120 * <p>WARNING: New clients should not use this function. The only usages should be in PanService 2121 * and WifiStateMachine which need direct access. All other clients should use 2122 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning 2123 * logic.</p> 2124 * 2125 * @param iface the interface name to tether. 2126 * @return error a {@code TETHER_ERROR} value indicating success or failure type 2127 * 2128 * {@hide} 2129 */ 2130 public int tether(String iface) { 2131 try { 2132 String pkgName = mContext.getOpPackageName(); 2133 Log.i(TAG, "tether caller:" + pkgName); 2134 return mService.tether(iface, pkgName); 2135 } catch (RemoteException e) { 2136 throw e.rethrowFromSystemServer(); 2137 } 2138 } 2139 2140 /** 2141 * Stop tethering the named interface. 2142 * 2143 * <p>This method requires the caller to hold either the 2144 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 2145 * or the ability to modify system settings as determined by 2146 * {@link android.provider.Settings.System#canWrite}.</p> 2147 * 2148 * <p>WARNING: New clients should not use this function. The only usages should be in PanService 2149 * and WifiStateMachine which need direct access. All other clients should use 2150 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning 2151 * logic.</p> 2152 * 2153 * @param iface the interface name to untether. 2154 * @return error a {@code TETHER_ERROR} value indicating success or failure type 2155 * 2156 * {@hide} 2157 */ 2158 public int untether(String iface) { 2159 try { 2160 String pkgName = mContext.getOpPackageName(); 2161 Log.i(TAG, "untether caller:" + pkgName); 2162 return mService.untether(iface, pkgName); 2163 } catch (RemoteException e) { 2164 throw e.rethrowFromSystemServer(); 2165 } 2166 } 2167 2168 /** 2169 * Check if the device allows for tethering. It may be disabled via 2170 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or 2171 * due to device configuration. 2172 * 2173 * <p>If this app does not have permission to use this API, it will always 2174 * return false rather than throw an exception.</p> 2175 * 2176 * <p>If the device has a hotspot provisioning app, the caller is required to hold the 2177 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p> 2178 * 2179 * <p>Otherwise, this method requires the caller to hold the ability to modify system 2180 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p> 2181 * 2182 * @return a boolean - {@code true} indicating Tethering is supported. 2183 * 2184 * {@hide} 2185 */ 2186 @SystemApi 2187 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED, 2188 android.Manifest.permission.WRITE_SETTINGS}) 2189 public boolean isTetheringSupported() { 2190 String pkgName = mContext.getOpPackageName(); 2191 try { 2192 return mService.isTetheringSupported(pkgName); 2193 } catch (SecurityException e) { 2194 // This API is not available to this caller, but for backward-compatibility 2195 // this will just return false instead of throwing. 2196 return false; 2197 } catch (RemoteException e) { 2198 throw e.rethrowFromSystemServer(); 2199 } 2200 } 2201 2202 /** 2203 * Callback for use with {@link #startTethering} to find out whether tethering succeeded. 2204 * @hide 2205 */ 2206 @SystemApi 2207 public static abstract class OnStartTetheringCallback { 2208 /** 2209 * Called when tethering has been successfully started. 2210 */ 2211 public void onTetheringStarted() {}; 2212 2213 /** 2214 * Called when starting tethering failed. 2215 */ 2216 public void onTetheringFailed() {}; 2217 } 2218 2219 /** 2220 * Convenient overload for 2221 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null 2222 * handler to run on the current thread's {@link Looper}. 2223 * @hide 2224 */ 2225 @SystemApi 2226 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) 2227 public void startTethering(int type, boolean showProvisioningUi, 2228 final OnStartTetheringCallback callback) { 2229 startTethering(type, showProvisioningUi, callback, null); 2230 } 2231 2232 /** 2233 * Runs tether provisioning for the given type if needed and then starts tethering if 2234 * the check succeeds. If no carrier provisioning is required for tethering, tethering is 2235 * enabled immediately. If provisioning fails, tethering will not be enabled. It also 2236 * schedules tether provisioning re-checks if appropriate. 2237 * 2238 * @param type The type of tethering to start. Must be one of 2239 * {@link ConnectivityManager.TETHERING_WIFI}, 2240 * {@link ConnectivityManager.TETHERING_USB}, or 2241 * {@link ConnectivityManager.TETHERING_BLUETOOTH}. 2242 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there 2243 * is one. This should be true the first time this function is called and also any time 2244 * the user can see this UI. It gives users information from their carrier about the 2245 * check failing and how they can sign up for tethering if possible. 2246 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller 2247 * of the result of trying to tether. 2248 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. 2249 * @hide 2250 */ 2251 @SystemApi 2252 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) 2253 public void startTethering(int type, boolean showProvisioningUi, 2254 final OnStartTetheringCallback callback, Handler handler) { 2255 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null."); 2256 2257 ResultReceiver wrappedCallback = new ResultReceiver(handler) { 2258 @Override 2259 protected void onReceiveResult(int resultCode, Bundle resultData) { 2260 if (resultCode == TETHER_ERROR_NO_ERROR) { 2261 callback.onTetheringStarted(); 2262 } else { 2263 callback.onTetheringFailed(); 2264 } 2265 } 2266 }; 2267 2268 try { 2269 String pkgName = mContext.getOpPackageName(); 2270 Log.i(TAG, "startTethering caller:" + pkgName); 2271 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName); 2272 } catch (RemoteException e) { 2273 Log.e(TAG, "Exception trying to start tethering.", e); 2274 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null); 2275 } 2276 } 2277 2278 /** 2279 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if 2280 * applicable. 2281 * 2282 * @param type The type of tethering to stop. Must be one of 2283 * {@link ConnectivityManager.TETHERING_WIFI}, 2284 * {@link ConnectivityManager.TETHERING_USB}, or 2285 * {@link ConnectivityManager.TETHERING_BLUETOOTH}. 2286 * @hide 2287 */ 2288 @SystemApi 2289 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) 2290 public void stopTethering(int type) { 2291 try { 2292 String pkgName = mContext.getOpPackageName(); 2293 Log.i(TAG, "stopTethering caller:" + pkgName); 2294 mService.stopTethering(type, pkgName); 2295 } catch (RemoteException e) { 2296 throw e.rethrowFromSystemServer(); 2297 } 2298 } 2299 2300 /** 2301 * Get the list of regular expressions that define any tetherable 2302 * USB network interfaces. If USB tethering is not supported by the 2303 * device, this list should be empty. 2304 * 2305 * @return an array of 0 or more regular expression Strings defining 2306 * what interfaces are considered tetherable usb interfaces. 2307 * 2308 * {@hide} 2309 */ 2310 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2311 public String[] getTetherableUsbRegexs() { 2312 try { 2313 return mService.getTetherableUsbRegexs(); 2314 } catch (RemoteException e) { 2315 throw e.rethrowFromSystemServer(); 2316 } 2317 } 2318 2319 /** 2320 * Get the list of regular expressions that define any tetherable 2321 * Wifi network interfaces. If Wifi tethering is not supported by the 2322 * device, this list should be empty. 2323 * 2324 * @return an array of 0 or more regular expression Strings defining 2325 * what interfaces are considered tetherable wifi interfaces. 2326 * 2327 * {@hide} 2328 */ 2329 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2330 public String[] getTetherableWifiRegexs() { 2331 try { 2332 return mService.getTetherableWifiRegexs(); 2333 } catch (RemoteException e) { 2334 throw e.rethrowFromSystemServer(); 2335 } 2336 } 2337 2338 /** 2339 * Get the list of regular expressions that define any tetherable 2340 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the 2341 * device, this list should be empty. 2342 * 2343 * @return an array of 0 or more regular expression Strings defining 2344 * what interfaces are considered tetherable bluetooth interfaces. 2345 * 2346 * {@hide} 2347 */ 2348 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2349 public String[] getTetherableBluetoothRegexs() { 2350 try { 2351 return mService.getTetherableBluetoothRegexs(); 2352 } catch (RemoteException e) { 2353 throw e.rethrowFromSystemServer(); 2354 } 2355 } 2356 2357 /** 2358 * Attempt to both alter the mode of USB and Tethering of USB. A 2359 * utility method to deal with some of the complexity of USB - will 2360 * attempt to switch to Rndis and subsequently tether the resulting 2361 * interface on {@code true} or turn off tethering and switch off 2362 * Rndis on {@code false}. 2363 * 2364 * <p>This method requires the caller to hold either the 2365 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 2366 * or the ability to modify system settings as determined by 2367 * {@link android.provider.Settings.System#canWrite}.</p> 2368 * 2369 * @param enable a boolean - {@code true} to enable tethering 2370 * @return error a {@code TETHER_ERROR} value indicating success or failure type 2371 * 2372 * {@hide} 2373 */ 2374 public int setUsbTethering(boolean enable) { 2375 try { 2376 String pkgName = mContext.getOpPackageName(); 2377 Log.i(TAG, "setUsbTethering caller:" + pkgName); 2378 return mService.setUsbTethering(enable, pkgName); 2379 } catch (RemoteException e) { 2380 throw e.rethrowFromSystemServer(); 2381 } 2382 } 2383 2384 /** {@hide} */ 2385 public static final int TETHER_ERROR_NO_ERROR = 0; 2386 /** {@hide} */ 2387 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; 2388 /** {@hide} */ 2389 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; 2390 /** {@hide} */ 2391 public static final int TETHER_ERROR_UNSUPPORTED = 3; 2392 /** {@hide} */ 2393 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; 2394 /** {@hide} */ 2395 public static final int TETHER_ERROR_MASTER_ERROR = 5; 2396 /** {@hide} */ 2397 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; 2398 /** {@hide} */ 2399 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; 2400 /** {@hide} */ 2401 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8; 2402 /** {@hide} */ 2403 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9; 2404 /** {@hide} */ 2405 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; 2406 /** {@hide} */ 2407 public static final int TETHER_ERROR_PROVISION_FAILED = 11; 2408 2409 /** 2410 * Get a more detailed error code after a Tethering or Untethering 2411 * request asynchronously failed. 2412 * 2413 * @param iface The name of the interface of interest 2414 * @return error The error code of the last error tethering or untethering the named 2415 * interface 2416 * 2417 * {@hide} 2418 */ 2419 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2420 public int getLastTetherError(String iface) { 2421 try { 2422 return mService.getLastTetherError(iface); 2423 } catch (RemoteException e) { 2424 throw e.rethrowFromSystemServer(); 2425 } 2426 } 2427 2428 /** 2429 * Report network connectivity status. This is currently used only 2430 * to alter status bar UI. 2431 * <p>This method requires the caller to hold the permission 2432 * {@link android.Manifest.permission#STATUS_BAR}. 2433 * 2434 * @param networkType The type of network you want to report on 2435 * @param percentage The quality of the connection 0 is bad, 100 is good 2436 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead. 2437 * {@hide} 2438 */ 2439 public void reportInetCondition(int networkType, int percentage) { 2440 try { 2441 mService.reportInetCondition(networkType, percentage); 2442 } catch (RemoteException e) { 2443 throw e.rethrowFromSystemServer(); 2444 } 2445 } 2446 2447 /** 2448 * Report a problem network to the framework. This provides a hint to the system 2449 * that there might be connectivity problems on this network and may cause 2450 * the framework to re-evaluate network connectivity and/or switch to another 2451 * network. 2452 * 2453 * @param network The {@link Network} the application was attempting to use 2454 * or {@code null} to indicate the current default network. 2455 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both 2456 * working and non-working connectivity. 2457 */ 2458 @Deprecated 2459 public void reportBadNetwork(Network network) { 2460 try { 2461 // One of these will be ignored because it matches system's current state. 2462 // The other will trigger the necessary reevaluation. 2463 mService.reportNetworkConnectivity(network, true); 2464 mService.reportNetworkConnectivity(network, false); 2465 } catch (RemoteException e) { 2466 throw e.rethrowFromSystemServer(); 2467 } 2468 } 2469 2470 /** 2471 * Report to the framework whether a network has working connectivity. 2472 * This provides a hint to the system that a particular network is providing 2473 * working connectivity or not. In response the framework may re-evaluate 2474 * the network's connectivity and might take further action thereafter. 2475 * 2476 * @param network The {@link Network} the application was attempting to use 2477 * or {@code null} to indicate the current default network. 2478 * @param hasConnectivity {@code true} if the application was able to successfully access the 2479 * Internet using {@code network} or {@code false} if not. 2480 */ 2481 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { 2482 try { 2483 mService.reportNetworkConnectivity(network, hasConnectivity); 2484 } catch (RemoteException e) { 2485 throw e.rethrowFromSystemServer(); 2486 } 2487 } 2488 2489 /** 2490 * Set a network-independent global http proxy. This is not normally what you want 2491 * for typical HTTP proxies - they are general network dependent. However if you're 2492 * doing something unusual like general internal filtering this may be useful. On 2493 * a private network where the proxy is not accessible, you may break HTTP using this. 2494 * 2495 * @param p A {@link ProxyInfo} object defining the new global 2496 * HTTP proxy. A {@code null} value will clear the global HTTP proxy. 2497 * @hide 2498 */ 2499 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 2500 public void setGlobalProxy(ProxyInfo p) { 2501 try { 2502 mService.setGlobalProxy(p); 2503 } catch (RemoteException e) { 2504 throw e.rethrowFromSystemServer(); 2505 } 2506 } 2507 2508 /** 2509 * Retrieve any network-independent global HTTP proxy. 2510 * 2511 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} 2512 * if no global HTTP proxy is set. 2513 * @hide 2514 */ 2515 public ProxyInfo getGlobalProxy() { 2516 try { 2517 return mService.getGlobalProxy(); 2518 } catch (RemoteException e) { 2519 throw e.rethrowFromSystemServer(); 2520 } 2521 } 2522 2523 /** 2524 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a 2525 * network-specific HTTP proxy. If {@code network} is null, the 2526 * network-specific proxy returned is the proxy of the default active 2527 * network. 2528 * 2529 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no 2530 * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, 2531 * or when {@code network} is {@code null}, 2532 * the {@code ProxyInfo} for the default active network. Returns 2533 * {@code null} when no proxy applies or the caller doesn't have 2534 * permission to use {@code network}. 2535 * @hide 2536 */ 2537 public ProxyInfo getProxyForNetwork(Network network) { 2538 try { 2539 return mService.getProxyForNetwork(network); 2540 } catch (RemoteException e) { 2541 throw e.rethrowFromSystemServer(); 2542 } 2543 } 2544 2545 /** 2546 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, 2547 * otherwise if this process is bound to a {@link Network} using 2548 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise 2549 * the default network's proxy is returned. 2550 * 2551 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no 2552 * HTTP proxy is active. 2553 */ 2554 public ProxyInfo getDefaultProxy() { 2555 return getProxyForNetwork(getBoundNetworkForProcess()); 2556 } 2557 2558 /** 2559 * Returns true if the hardware supports the given network type 2560 * else it returns false. This doesn't indicate we have coverage 2561 * or are authorized onto a network, just whether or not the 2562 * hardware supports it. For example a GSM phone without a SIM 2563 * should still return {@code true} for mobile data, but a wifi only 2564 * tablet would return {@code false}. 2565 * 2566 * @param networkType The network type we'd like to check 2567 * @return {@code true} if supported, else {@code false} 2568 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. 2569 * @hide 2570 */ 2571 @Deprecated 2572 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2573 public boolean isNetworkSupported(int networkType) { 2574 try { 2575 return mService.isNetworkSupported(networkType); 2576 } catch (RemoteException e) { 2577 throw e.rethrowFromSystemServer(); 2578 } 2579 } 2580 2581 /** 2582 * Returns if the currently active data network is metered. A network is 2583 * classified as metered when the user is sensitive to heavy data usage on 2584 * that connection due to monetary costs, data limitations or 2585 * battery/performance issues. You should check this before doing large 2586 * data transfers, and warn the user or delay the operation until another 2587 * network is available. 2588 * 2589 * @return {@code true} if large transfers should be avoided, otherwise 2590 * {@code false}. 2591 */ 2592 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 2593 public boolean isActiveNetworkMetered() { 2594 try { 2595 return mService.isActiveNetworkMetered(); 2596 } catch (RemoteException e) { 2597 throw e.rethrowFromSystemServer(); 2598 } 2599 } 2600 2601 /** 2602 * If the LockdownVpn mechanism is enabled, updates the vpn 2603 * with a reload of its profile. 2604 * 2605 * @return a boolean with {@code} indicating success 2606 * 2607 * <p>This method can only be called by the system UID 2608 * {@hide} 2609 */ 2610 public boolean updateLockdownVpn() { 2611 try { 2612 return mService.updateLockdownVpn(); 2613 } catch (RemoteException e) { 2614 throw e.rethrowFromSystemServer(); 2615 } 2616 } 2617 2618 /** 2619 * Check mobile provisioning. 2620 * 2621 * @param suggestedTimeOutMs, timeout in milliseconds 2622 * 2623 * @return time out that will be used, maybe less that suggestedTimeOutMs 2624 * -1 if an error. 2625 * 2626 * {@hide} 2627 */ 2628 public int checkMobileProvisioning(int suggestedTimeOutMs) { 2629 int timeOutMs = -1; 2630 try { 2631 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs); 2632 } catch (RemoteException e) { 2633 throw e.rethrowFromSystemServer(); 2634 } 2635 return timeOutMs; 2636 } 2637 2638 /** 2639 * Get the mobile provisioning url. 2640 * {@hide} 2641 */ 2642 public String getMobileProvisioningUrl() { 2643 try { 2644 return mService.getMobileProvisioningUrl(); 2645 } catch (RemoteException e) { 2646 throw e.rethrowFromSystemServer(); 2647 } 2648 } 2649 2650 /** 2651 * Set sign in error notification to visible or in visible 2652 * 2653 * @param visible 2654 * @param networkType 2655 * 2656 * {@hide} 2657 * @deprecated Doesn't properly deal with multiple connected networks of the same type. 2658 */ 2659 @Deprecated 2660 public void setProvisioningNotificationVisible(boolean visible, int networkType, 2661 String action) { 2662 try { 2663 mService.setProvisioningNotificationVisible(visible, networkType, action); 2664 } catch (RemoteException e) { 2665 throw e.rethrowFromSystemServer(); 2666 } 2667 } 2668 2669 /** 2670 * Set the value for enabling/disabling airplane mode 2671 * 2672 * @param enable whether to enable airplane mode or not 2673 * 2674 * @hide 2675 */ 2676 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 2677 public void setAirplaneMode(boolean enable) { 2678 try { 2679 mService.setAirplaneMode(enable); 2680 } catch (RemoteException e) { 2681 throw e.rethrowFromSystemServer(); 2682 } 2683 } 2684 2685 /** {@hide} */ 2686 public void registerNetworkFactory(Messenger messenger, String name) { 2687 try { 2688 mService.registerNetworkFactory(messenger, name); 2689 } catch (RemoteException e) { 2690 throw e.rethrowFromSystemServer(); 2691 } 2692 } 2693 2694 /** {@hide} */ 2695 public void unregisterNetworkFactory(Messenger messenger) { 2696 try { 2697 mService.unregisterNetworkFactory(messenger); 2698 } catch (RemoteException e) { 2699 throw e.rethrowFromSystemServer(); 2700 } 2701 } 2702 2703 /** 2704 * @hide 2705 * Register a NetworkAgent with ConnectivityService. 2706 * @return NetID corresponding to NetworkAgent. 2707 */ 2708 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp, 2709 NetworkCapabilities nc, int score, NetworkMisc misc) { 2710 try { 2711 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc); 2712 } catch (RemoteException e) { 2713 throw e.rethrowFromSystemServer(); 2714 } 2715 } 2716 2717 /** 2718 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network 2719 * changes. Should be extended by applications wanting notifications. 2720 * 2721 * A {@code NetworkCallback} is registered by calling 2722 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, 2723 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)}, 2724 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is 2725 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}. 2726 * A {@code NetworkCallback} should be registered at most once at any time. 2727 * A {@code NetworkCallback} that has been unregistered can be registered again. 2728 */ 2729 public static class NetworkCallback { 2730 /** 2731 * Called when the framework connects to a new network to evaluate whether it satisfies this 2732 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} 2733 * callback. There is no guarantee that this new network will satisfy any requests, or that 2734 * the network will stay connected for longer than the time necessary to evaluate it. 2735 * <p> 2736 * Most applications <b>should not</b> act on this callback, and should instead use 2737 * {@link #onAvailable}. This callback is intended for use by applications that can assist 2738 * the framework in properly evaluating the network — for example, an application that 2739 * can automatically log in to a captive portal without user intervention. 2740 * 2741 * @param network The {@link Network} of the network that is being evaluated. 2742 * 2743 * @hide 2744 */ 2745 public void onPreCheck(Network network) {} 2746 2747 /** 2748 * Called when the framework connects and has declared a new network ready for use. 2749 * This callback may be called more than once if the {@link Network} that is 2750 * satisfying the request changes. 2751 * 2752 * @param network The {@link Network} of the satisfying network. 2753 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network. 2754 * @param linkProperties The {@link LinkProperties} of the satisfying network. 2755 * @hide 2756 */ 2757 public void onAvailable(Network network, NetworkCapabilities networkCapabilities, 2758 LinkProperties linkProperties) { 2759 // Internally only this method is called when a new network is available, and 2760 // it calls the callback in the same way and order that older versions used 2761 // to call so as not to change the behavior. 2762 onAvailable(network); 2763 if (!networkCapabilities.hasCapability( 2764 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) { 2765 onNetworkSuspended(network); 2766 } 2767 onCapabilitiesChanged(network, networkCapabilities); 2768 onLinkPropertiesChanged(network, linkProperties); 2769 } 2770 2771 /** 2772 * Called when the framework connects and has declared a new network ready for use. 2773 * This callback may be called more than once if the {@link Network} that is 2774 * satisfying the request changes. This will always immediately be followed by a 2775 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a 2776 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}. 2777 * 2778 * @param network The {@link Network} of the satisfying network. 2779 */ 2780 public void onAvailable(Network network) {} 2781 2782 /** 2783 * Called when the network is about to be disconnected. Often paired with an 2784 * {@link NetworkCallback#onAvailable} call with the new replacement network 2785 * for graceful handover. This may not be called if we have a hard loss 2786 * (loss without warning). This may be followed by either a 2787 * {@link NetworkCallback#onLost} call or a 2788 * {@link NetworkCallback#onAvailable} call for this network depending 2789 * on whether we lose or regain it. 2790 * 2791 * @param network The {@link Network} that is about to be disconnected. 2792 * @param maxMsToLive The time in ms the framework will attempt to keep the 2793 * network connected. Note that the network may suffer a 2794 * hard loss at any time. 2795 */ 2796 public void onLosing(Network network, int maxMsToLive) {} 2797 2798 /** 2799 * Called when the framework has a hard loss of the network or when the 2800 * graceful failure ends. 2801 * 2802 * @param network The {@link Network} lost. 2803 */ 2804 public void onLost(Network network) {} 2805 2806 /** 2807 * Called if no network is found in the timeout time specified in 2808 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not 2809 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)} 2810 * without timeout. When this callback is invoked the associated 2811 * {@link NetworkRequest} will have already been removed and released, as if 2812 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called. 2813 */ 2814 public void onUnavailable() {} 2815 2816 /** 2817 * Called when the network the framework connected to for this request 2818 * changes capabilities but still satisfies the stated need. 2819 * 2820 * @param network The {@link Network} whose capabilities have changed. 2821 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this 2822 * network. 2823 */ 2824 public void onCapabilitiesChanged(Network network, 2825 NetworkCapabilities networkCapabilities) {} 2826 2827 /** 2828 * Called when the network the framework connected to for this request 2829 * changes {@link LinkProperties}. 2830 * 2831 * @param network The {@link Network} whose link properties have changed. 2832 * @param linkProperties The new {@link LinkProperties} for this network. 2833 */ 2834 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {} 2835 2836 /** 2837 * Called when the network the framework connected to for this request 2838 * goes into {@link NetworkInfo.State#SUSPENDED}. 2839 * This generally means that while the TCP connections are still live, 2840 * temporarily network data fails to transfer. Specifically this is used 2841 * on cellular networks to mask temporary outages when driving through 2842 * a tunnel, etc. 2843 * @hide 2844 */ 2845 public void onNetworkSuspended(Network network) {} 2846 2847 /** 2848 * Called when the network the framework connected to for this request 2849 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be 2850 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call. 2851 * @hide 2852 */ 2853 public void onNetworkResumed(Network network) {} 2854 2855 private NetworkRequest networkRequest; 2856 } 2857 2858 /** 2859 * Constant error codes used by ConnectivityService to communicate about failures and errors 2860 * across a Binder boundary. 2861 * @hide 2862 */ 2863 public interface Errors { 2864 static int TOO_MANY_REQUESTS = 1; 2865 } 2866 2867 /** @hide */ 2868 public static class TooManyRequestsException extends RuntimeException {} 2869 2870 private static RuntimeException convertServiceException(ServiceSpecificException e) { 2871 switch (e.errorCode) { 2872 case Errors.TOO_MANY_REQUESTS: 2873 return new TooManyRequestsException(); 2874 default: 2875 Log.w(TAG, "Unknown service error code " + e.errorCode); 2876 return new RuntimeException(e); 2877 } 2878 } 2879 2880 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER; 2881 /** @hide */ 2882 public static final int CALLBACK_PRECHECK = BASE + 1; 2883 /** @hide */ 2884 public static final int CALLBACK_AVAILABLE = BASE + 2; 2885 /** @hide arg1 = TTL */ 2886 public static final int CALLBACK_LOSING = BASE + 3; 2887 /** @hide */ 2888 public static final int CALLBACK_LOST = BASE + 4; 2889 /** @hide */ 2890 public static final int CALLBACK_UNAVAIL = BASE + 5; 2891 /** @hide */ 2892 public static final int CALLBACK_CAP_CHANGED = BASE + 6; 2893 /** @hide */ 2894 public static final int CALLBACK_IP_CHANGED = BASE + 7; 2895 /** @hide obj = NetworkCapabilities, arg1 = seq number */ 2896 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8; 2897 /** @hide */ 2898 public static final int CALLBACK_SUSPENDED = BASE + 9; 2899 /** @hide */ 2900 public static final int CALLBACK_RESUMED = BASE + 10; 2901 2902 /** @hide */ 2903 public static String getCallbackName(int whichCallback) { 2904 switch (whichCallback) { 2905 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK"; 2906 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE"; 2907 case CALLBACK_LOSING: return "CALLBACK_LOSING"; 2908 case CALLBACK_LOST: return "CALLBACK_LOST"; 2909 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL"; 2910 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED"; 2911 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED"; 2912 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST"; 2913 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED"; 2914 case CALLBACK_RESUMED: return "CALLBACK_RESUMED"; 2915 default: 2916 return Integer.toString(whichCallback); 2917 } 2918 } 2919 2920 private class CallbackHandler extends Handler { 2921 private static final String TAG = "ConnectivityManager.CallbackHandler"; 2922 private static final boolean DBG = false; 2923 2924 CallbackHandler(Looper looper) { 2925 super(looper); 2926 } 2927 2928 CallbackHandler(Handler handler) { 2929 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper()); 2930 } 2931 2932 @Override 2933 public void handleMessage(Message message) { 2934 if (message.what == EXPIRE_LEGACY_REQUEST) { 2935 expireRequest((NetworkCapabilities) message.obj, message.arg1); 2936 return; 2937 } 2938 2939 final NetworkRequest request = getObject(message, NetworkRequest.class); 2940 final Network network = getObject(message, Network.class); 2941 final NetworkCallback callback; 2942 synchronized (sCallbacks) { 2943 callback = sCallbacks.get(request); 2944 } 2945 if (DBG) { 2946 Log.d(TAG, getCallbackName(message.what) + " for network " + network); 2947 } 2948 if (callback == null) { 2949 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message"); 2950 return; 2951 } 2952 2953 switch (message.what) { 2954 case CALLBACK_PRECHECK: { 2955 callback.onPreCheck(network); 2956 break; 2957 } 2958 case CALLBACK_AVAILABLE: { 2959 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); 2960 LinkProperties lp = getObject(message, LinkProperties.class); 2961 callback.onAvailable(network, cap, lp); 2962 break; 2963 } 2964 case CALLBACK_LOSING: { 2965 callback.onLosing(network, message.arg1); 2966 break; 2967 } 2968 case CALLBACK_LOST: { 2969 callback.onLost(network); 2970 break; 2971 } 2972 case CALLBACK_UNAVAIL: { 2973 callback.onUnavailable(); 2974 break; 2975 } 2976 case CALLBACK_CAP_CHANGED: { 2977 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); 2978 callback.onCapabilitiesChanged(network, cap); 2979 break; 2980 } 2981 case CALLBACK_IP_CHANGED: { 2982 LinkProperties lp = getObject(message, LinkProperties.class); 2983 callback.onLinkPropertiesChanged(network, lp); 2984 break; 2985 } 2986 case CALLBACK_SUSPENDED: { 2987 callback.onNetworkSuspended(network); 2988 break; 2989 } 2990 case CALLBACK_RESUMED: { 2991 callback.onNetworkResumed(network); 2992 break; 2993 } 2994 } 2995 } 2996 2997 private <T> T getObject(Message msg, Class<T> c) { 2998 return (T) msg.getData().getParcelable(c.getSimpleName()); 2999 } 3000 } 3001 3002 private CallbackHandler getDefaultHandler() { 3003 synchronized (sCallbacks) { 3004 if (sCallbackHandler == null) { 3005 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper()); 3006 } 3007 return sCallbackHandler; 3008 } 3009 } 3010 3011 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>(); 3012 private static CallbackHandler sCallbackHandler; 3013 3014 private static final int LISTEN = 1; 3015 private static final int REQUEST = 2; 3016 3017 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback, 3018 int timeoutMs, int action, int legacyType, CallbackHandler handler) { 3019 checkCallbackNotNull(callback); 3020 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities"); 3021 final NetworkRequest request; 3022 try { 3023 synchronized(sCallbacks) { 3024 if (callback.networkRequest != null 3025 && callback.networkRequest != ALREADY_UNREGISTERED) { 3026 // TODO: throw exception instead and enforce 1:1 mapping of callbacks 3027 // and requests (http://b/20701525). 3028 Log.e(TAG, "NetworkCallback was already registered"); 3029 } 3030 Messenger messenger = new Messenger(handler); 3031 Binder binder = new Binder(); 3032 if (action == LISTEN) { 3033 request = mService.listenForNetwork(need, messenger, binder); 3034 } else { 3035 request = mService.requestNetwork( 3036 need, messenger, timeoutMs, binder, legacyType); 3037 } 3038 if (request != null) { 3039 sCallbacks.put(request, callback); 3040 } 3041 callback.networkRequest = request; 3042 } 3043 } catch (RemoteException e) { 3044 throw e.rethrowFromSystemServer(); 3045 } catch (ServiceSpecificException e) { 3046 throw convertServiceException(e); 3047 } 3048 return request; 3049 } 3050 3051 /** 3052 * Helper function to request a network with a particular legacy type. 3053 * 3054 * This is temporarily public @hide so it can be called by system code that uses the 3055 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for 3056 * instead network notifications. 3057 * 3058 * TODO: update said system code to rely on NetworkCallbacks and make this method private. 3059 * 3060 * @hide 3061 */ 3062 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, 3063 int timeoutMs, int legacyType, Handler handler) { 3064 CallbackHandler cbHandler = new CallbackHandler(handler); 3065 NetworkCapabilities nc = request.networkCapabilities; 3066 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler); 3067 } 3068 3069 /** 3070 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. 3071 * 3072 * This {@link NetworkRequest} will live until released via 3073 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A 3074 * version of the method which takes a timeout is 3075 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}. 3076 * Status of the request can be followed by listening to the various 3077 * callbacks described in {@link NetworkCallback}. The {@link Network} 3078 * can be used to direct traffic to the network. 3079 * <p>It is presently unsupported to request a network with mutable 3080 * {@link NetworkCapabilities} such as 3081 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or 3082 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} 3083 * as these {@code NetworkCapabilities} represent states that a particular 3084 * network may never attain, and whether a network will attain these states 3085 * is unknown prior to bringing up the network so the framework does not 3086 * know how to go about satisfing a request with these capabilities. 3087 * 3088 * <p>This method requires the caller to hold either the 3089 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 3090 * or the ability to modify system settings as determined by 3091 * {@link android.provider.Settings.System#canWrite}.</p> 3092 * 3093 * @param request {@link NetworkRequest} describing this request. 3094 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note 3095 * the callback must not be shared - it uniquely specifies this request. 3096 * The callback is invoked on the default internal Handler. 3097 * @throws IllegalArgumentException if {@code request} specifies any mutable 3098 * {@code NetworkCapabilities}. 3099 */ 3100 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) { 3101 requestNetwork(request, networkCallback, getDefaultHandler()); 3102 } 3103 3104 /** 3105 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. 3106 * 3107 * This {@link NetworkRequest} will live until released via 3108 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A 3109 * version of the method which takes a timeout is 3110 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}. 3111 * Status of the request can be followed by listening to the various 3112 * callbacks described in {@link NetworkCallback}. The {@link Network} 3113 * can be used to direct traffic to the network. 3114 * <p>It is presently unsupported to request a network with mutable 3115 * {@link NetworkCapabilities} such as 3116 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or 3117 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} 3118 * as these {@code NetworkCapabilities} represent states that a particular 3119 * network may never attain, and whether a network will attain these states 3120 * is unknown prior to bringing up the network so the framework does not 3121 * know how to go about satisfing a request with these capabilities. 3122 * 3123 * <p>This method requires the caller to hold either the 3124 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 3125 * or the ability to modify system settings as determined by 3126 * {@link android.provider.Settings.System#canWrite}.</p> 3127 * 3128 * @param request {@link NetworkRequest} describing this request. 3129 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note 3130 * the callback must not be shared - it uniquely specifies this request. 3131 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. 3132 * @throws IllegalArgumentException if {@code request} specifies any mutable 3133 * {@code NetworkCapabilities}. 3134 */ 3135 public void requestNetwork( 3136 NetworkRequest request, NetworkCallback networkCallback, Handler handler) { 3137 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); 3138 CallbackHandler cbHandler = new CallbackHandler(handler); 3139 requestNetwork(request, networkCallback, 0, legacyType, cbHandler); 3140 } 3141 3142 /** 3143 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited 3144 * by a timeout. 3145 * 3146 * This function behaves identically to the non-timed-out version 3147 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network 3148 * is not found within the given time (in milliseconds) the 3149 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be 3150 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does 3151 * not have to be released if timed-out (it is automatically released). Unregistering a 3152 * request that timed out is not an error. 3153 * 3154 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small 3155 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided 3156 * for that purpose. Calling this method will attempt to bring up the requested network. 3157 * 3158 * <p>This method requires the caller to hold either the 3159 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 3160 * or the ability to modify system settings as determined by 3161 * {@link android.provider.Settings.System#canWrite}.</p> 3162 * 3163 * @param request {@link NetworkRequest} describing this request. 3164 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note 3165 * the callback must not be shared - it uniquely specifies this request. 3166 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network 3167 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must 3168 * be a positive value (i.e. >0). 3169 */ 3170 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, 3171 int timeoutMs) { 3172 checkTimeout(timeoutMs); 3173 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); 3174 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler()); 3175 } 3176 3177 /** 3178 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited 3179 * by a timeout. 3180 * 3181 * This function behaves identically to the non-timedout version, but if a suitable 3182 * network is not found within the given time (in milliseconds) the 3183 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be 3184 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does 3185 * not have to be released if timed-out (it is automatically released). Unregistering a 3186 * request that timed out is not an error. 3187 * 3188 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small 3189 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided 3190 * for that purpose. Calling this method will attempt to bring up the requested network. 3191 * 3192 * <p>This method requires the caller to hold either the 3193 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 3194 * or the ability to modify system settings as determined by 3195 * {@link android.provider.Settings.System#canWrite}.</p> 3196 * 3197 * @param request {@link NetworkRequest} describing this request. 3198 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note 3199 * the callback must not be shared - it uniquely specifies this request. 3200 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. 3201 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network 3202 * before {@link NetworkCallback#onUnavailable} is called. 3203 */ 3204 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, 3205 Handler handler, int timeoutMs) { 3206 checkTimeout(timeoutMs); 3207 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities); 3208 CallbackHandler cbHandler = new CallbackHandler(handler); 3209 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler); 3210 } 3211 3212 /** 3213 * The lookup key for a {@link Network} object included with the intent after 3214 * successfully finding a network for the applications request. Retrieve it with 3215 * {@link android.content.Intent#getParcelableExtra(String)}. 3216 * <p> 3217 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} 3218 * then you must get a ConnectivityManager instance before doing so. 3219 */ 3220 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; 3221 3222 /** 3223 * The lookup key for a {@link NetworkRequest} object included with the intent after 3224 * successfully finding a network for the applications request. Retrieve it with 3225 * {@link android.content.Intent#getParcelableExtra(String)}. 3226 */ 3227 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; 3228 3229 3230 /** 3231 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. 3232 * 3233 * This function behaves identically to the version that takes a NetworkCallback, but instead 3234 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means 3235 * the request may outlive the calling application and get called back when a suitable 3236 * network is found. 3237 * <p> 3238 * The operation is an Intent broadcast that goes to a broadcast receiver that 3239 * you registered with {@link Context#registerReceiver} or through the 3240 * <receiver> tag in an AndroidManifest.xml file 3241 * <p> 3242 * The operation Intent is delivered with two extras, a {@link Network} typed 3243 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} 3244 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing 3245 * the original requests parameters. It is important to create a new, 3246 * {@link NetworkCallback} based request before completing the processing of the 3247 * Intent to reserve the network or it will be released shortly after the Intent 3248 * is processed. 3249 * <p> 3250 * If there is already a request for this Intent registered (with the equality of 3251 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and 3252 * replaced by this one, effectively releasing the previous {@link NetworkRequest}. 3253 * <p> 3254 * The request may be released normally by calling 3255 * {@link #releaseNetworkRequest(android.app.PendingIntent)}. 3256 * <p>It is presently unsupported to request a network with either 3257 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or 3258 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} 3259 * as these {@code NetworkCapabilities} represent states that a particular 3260 * network may never attain, and whether a network will attain these states 3261 * is unknown prior to bringing up the network so the framework does not 3262 * know how to go about satisfing a request with these capabilities. 3263 * 3264 * <p>This method requires the caller to hold either the 3265 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission 3266 * or the ability to modify system settings as determined by 3267 * {@link android.provider.Settings.System#canWrite}.</p> 3268 * 3269 * @param request {@link NetworkRequest} describing this request. 3270 * @param operation Action to perform when the network is available (corresponds 3271 * to the {@link NetworkCallback#onAvailable} call. Typically 3272 * comes from {@link PendingIntent#getBroadcast}. Cannot be null. 3273 * @throws IllegalArgumentException if {@code request} contains either 3274 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or 3275 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}. 3276 */ 3277 public void requestNetwork(NetworkRequest request, PendingIntent operation) { 3278 checkPendingIntentNotNull(operation); 3279 try { 3280 mService.pendingRequestForNetwork(request.networkCapabilities, operation); 3281 } catch (RemoteException e) { 3282 throw e.rethrowFromSystemServer(); 3283 } catch (ServiceSpecificException e) { 3284 throw convertServiceException(e); 3285 } 3286 } 3287 3288 /** 3289 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} 3290 * <p> 3291 * This method has the same behavior as 3292 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to 3293 * releasing network resources and disconnecting. 3294 * 3295 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the 3296 * PendingIntent passed to 3297 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the 3298 * corresponding NetworkRequest you'd like to remove. Cannot be null. 3299 */ 3300 public void releaseNetworkRequest(PendingIntent operation) { 3301 checkPendingIntentNotNull(operation); 3302 try { 3303 mService.releasePendingNetworkRequest(operation); 3304 } catch (RemoteException e) { 3305 throw e.rethrowFromSystemServer(); 3306 } 3307 } 3308 3309 private static void checkPendingIntentNotNull(PendingIntent intent) { 3310 Preconditions.checkNotNull(intent, "PendingIntent cannot be null."); 3311 } 3312 3313 private static void checkCallbackNotNull(NetworkCallback callback) { 3314 Preconditions.checkNotNull(callback, "null NetworkCallback"); 3315 } 3316 3317 private static void checkTimeout(int timeoutMs) { 3318 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive."); 3319 } 3320 3321 /** 3322 * Registers to receive notifications about all networks which satisfy the given 3323 * {@link NetworkRequest}. The callbacks will continue to be called until 3324 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called. 3325 * 3326 * @param request {@link NetworkRequest} describing this request. 3327 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable 3328 * networks change state. 3329 * The callback is invoked on the default internal Handler. 3330 */ 3331 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 3332 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) { 3333 registerNetworkCallback(request, networkCallback, getDefaultHandler()); 3334 } 3335 3336 /** 3337 * Registers to receive notifications about all networks which satisfy the given 3338 * {@link NetworkRequest}. The callbacks will continue to be called until 3339 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called. 3340 * 3341 * @param request {@link NetworkRequest} describing this request. 3342 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable 3343 * networks change state. 3344 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. 3345 */ 3346 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 3347 public void registerNetworkCallback( 3348 NetworkRequest request, NetworkCallback networkCallback, Handler handler) { 3349 CallbackHandler cbHandler = new CallbackHandler(handler); 3350 NetworkCapabilities nc = request.networkCapabilities; 3351 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler); 3352 } 3353 3354 /** 3355 * Registers a PendingIntent to be sent when a network is available which satisfies the given 3356 * {@link NetworkRequest}. 3357 * 3358 * This function behaves identically to the version that takes a NetworkCallback, but instead 3359 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means 3360 * the request may outlive the calling application and get called back when a suitable 3361 * network is found. 3362 * <p> 3363 * The operation is an Intent broadcast that goes to a broadcast receiver that 3364 * you registered with {@link Context#registerReceiver} or through the 3365 * <receiver> tag in an AndroidManifest.xml file 3366 * <p> 3367 * The operation Intent is delivered with two extras, a {@link Network} typed 3368 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} 3369 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing 3370 * the original requests parameters. 3371 * <p> 3372 * If there is already a request for this Intent registered (with the equality of 3373 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and 3374 * replaced by this one, effectively releasing the previous {@link NetworkRequest}. 3375 * <p> 3376 * The request may be released normally by calling 3377 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. 3378 * @param request {@link NetworkRequest} describing this request. 3379 * @param operation Action to perform when the network is available (corresponds 3380 * to the {@link NetworkCallback#onAvailable} call. Typically 3381 * comes from {@link PendingIntent#getBroadcast}. Cannot be null. 3382 */ 3383 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 3384 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) { 3385 checkPendingIntentNotNull(operation); 3386 try { 3387 mService.pendingListenForNetwork(request.networkCapabilities, operation); 3388 } catch (RemoteException e) { 3389 throw e.rethrowFromSystemServer(); 3390 } catch (ServiceSpecificException e) { 3391 throw convertServiceException(e); 3392 } 3393 } 3394 3395 /** 3396 * Registers to receive notifications about changes in the system default network. The callbacks 3397 * will continue to be called until either the application exits or 3398 * {@link #unregisterNetworkCallback(NetworkCallback)} is called. 3399 * 3400 * @param networkCallback The {@link NetworkCallback} that the system will call as the 3401 * system default network changes. 3402 * The callback is invoked on the default internal Handler. 3403 */ 3404 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 3405 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) { 3406 registerDefaultNetworkCallback(networkCallback, getDefaultHandler()); 3407 } 3408 3409 /** 3410 * Registers to receive notifications about changes in the system default network. The callbacks 3411 * will continue to be called until either the application exits or 3412 * {@link #unregisterNetworkCallback(NetworkCallback)} is called. 3413 * 3414 * @param networkCallback The {@link NetworkCallback} that the system will call as the 3415 * system default network changes. 3416 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. 3417 */ 3418 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 3419 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) { 3420 // This works because if the NetworkCapabilities are null, 3421 // ConnectivityService takes them from the default request. 3422 // 3423 // Since the capabilities are exactly the same as the default request's 3424 // capabilities, this request is guaranteed, at all times, to be 3425 // satisfied by the same network, if any, that satisfies the default 3426 // request, i.e., the system default network. 3427 NetworkCapabilities nullCapabilities = null; 3428 CallbackHandler cbHandler = new CallbackHandler(handler); 3429 sendRequestForNetwork(nullCapabilities, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler); 3430 } 3431 3432 /** 3433 * Requests bandwidth update for a given {@link Network} and returns whether the update request 3434 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying 3435 * network connection for updated bandwidth information. The caller will be notified via 3436 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this 3437 * method assumes that the caller has previously called 3438 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network 3439 * changes. 3440 * 3441 * @param network {@link Network} specifying which network you're interested. 3442 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. 3443 */ 3444 public boolean requestBandwidthUpdate(Network network) { 3445 try { 3446 return mService.requestBandwidthUpdate(network); 3447 } catch (RemoteException e) { 3448 throw e.rethrowFromSystemServer(); 3449 } 3450 } 3451 3452 /** 3453 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from 3454 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and 3455 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls. 3456 * If the given {@code NetworkCallback} had previously been used with 3457 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request 3458 * will be disconnected. 3459 * 3460 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop 3461 * triggering it as soon as this call returns. 3462 * 3463 * @param networkCallback The {@link NetworkCallback} used when making the request. 3464 */ 3465 public void unregisterNetworkCallback(NetworkCallback networkCallback) { 3466 checkCallbackNotNull(networkCallback); 3467 final List<NetworkRequest> reqs = new ArrayList<>(); 3468 // Find all requests associated to this callback and stop callback triggers immediately. 3469 // Callback is reusable immediately. http://b/20701525, http://b/35921499. 3470 synchronized (sCallbacks) { 3471 Preconditions.checkArgument(networkCallback.networkRequest != null, 3472 "NetworkCallback was not registered"); 3473 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED, 3474 "NetworkCallback was already unregistered"); 3475 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) { 3476 if (e.getValue() == networkCallback) { 3477 reqs.add(e.getKey()); 3478 } 3479 } 3480 // TODO: throw exception if callback was registered more than once (http://b/20701525). 3481 for (NetworkRequest r : reqs) { 3482 try { 3483 mService.releaseNetworkRequest(r); 3484 } catch (RemoteException e) { 3485 throw e.rethrowFromSystemServer(); 3486 } 3487 // Only remove mapping if rpc was successful. 3488 sCallbacks.remove(r); 3489 } 3490 networkCallback.networkRequest = ALREADY_UNREGISTERED; 3491 } 3492 } 3493 3494 /** 3495 * Unregisters a callback previously registered via 3496 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. 3497 * 3498 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the 3499 * PendingIntent passed to 3500 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. 3501 * Cannot be null. 3502 */ 3503 public void unregisterNetworkCallback(PendingIntent operation) { 3504 checkPendingIntentNotNull(operation); 3505 releaseNetworkRequest(operation); 3506 } 3507 3508 /** 3509 * Informs the system whether it should switch to {@code network} regardless of whether it is 3510 * validated or not. If {@code accept} is true, and the network was explicitly selected by the 3511 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become 3512 * the system default network regardless of any other network that's currently connected. If 3513 * {@code always} is true, then the choice is remembered, so that the next time the user 3514 * connects to this network, the system will switch to it. 3515 * 3516 * @param network The network to accept. 3517 * @param accept Whether to accept the network even if unvalidated. 3518 * @param always Whether to remember this choice in the future. 3519 * 3520 * @hide 3521 */ 3522 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 3523 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { 3524 try { 3525 mService.setAcceptUnvalidated(network, accept, always); 3526 } catch (RemoteException e) { 3527 throw e.rethrowFromSystemServer(); 3528 } 3529 } 3530 3531 /** 3532 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is 3533 * only meaningful if the system is configured not to penalize such networks, e.g., if the 3534 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code 3535 * NETWORK_AVOID_BAD_WIFI setting is unset}. 3536 * 3537 * @param network The network to accept. 3538 * 3539 * @hide 3540 */ 3541 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 3542 public void setAvoidUnvalidated(Network network) { 3543 try { 3544 mService.setAvoidUnvalidated(network); 3545 } catch (RemoteException e) { 3546 throw e.rethrowFromSystemServer(); 3547 } 3548 } 3549 3550 /** 3551 * Requests that the system open the captive portal app on the specified network. 3552 * 3553 * @param network The network to log into. 3554 * 3555 * @hide 3556 */ 3557 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL) 3558 public void startCaptivePortalApp(Network network) { 3559 try { 3560 mService.startCaptivePortalApp(network); 3561 } catch (RemoteException e) { 3562 throw e.rethrowFromSystemServer(); 3563 } 3564 } 3565 3566 /** 3567 * It is acceptable to briefly use multipath data to provide seamless connectivity for 3568 * time-sensitive user-facing operations when the system default network is temporarily 3569 * unresponsive. The amount of data should be limited (less than one megabyte for every call to 3570 * this method), and the operation should be infrequent to ensure that data usage is limited. 3571 * 3572 * An example of such an operation might be a time-sensitive foreground activity, such as a 3573 * voice command, that the user is performing while walking out of range of a Wi-Fi network. 3574 */ 3575 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0; 3576 3577 /** 3578 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide 3579 * a backup channel for traffic that is primarily going over another network. 3580 * 3581 * An example might be maintaining backup connections to peers or servers for the purpose of 3582 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic 3583 * on backup paths should be negligible compared to the traffic on the main path. 3584 */ 3585 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1; 3586 3587 /** 3588 * It is acceptable to use metered data to improve network latency and performance. 3589 */ 3590 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2; 3591 3592 /** 3593 * Return value to use for unmetered networks. On such networks we currently set all the flags 3594 * to true. 3595 * @hide 3596 */ 3597 public static final int MULTIPATH_PREFERENCE_UNMETERED = 3598 MULTIPATH_PREFERENCE_HANDOVER | 3599 MULTIPATH_PREFERENCE_RELIABILITY | 3600 MULTIPATH_PREFERENCE_PERFORMANCE; 3601 3602 /** @hide */ 3603 @Retention(RetentionPolicy.SOURCE) 3604 @IntDef(flag = true, value = { 3605 MULTIPATH_PREFERENCE_HANDOVER, 3606 MULTIPATH_PREFERENCE_RELIABILITY, 3607 MULTIPATH_PREFERENCE_PERFORMANCE, 3608 }) 3609 public @interface MultipathPreference { 3610 } 3611 3612 /** 3613 * Provides a hint to the calling application on whether it is desirable to use the 3614 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.) 3615 * for multipath data transfer on this network when it is not the system default network. 3616 * Applications desiring to use multipath network protocols should call this method before 3617 * each such operation. 3618 * 3619 * @param network The network on which the application desires to use multipath data. 3620 * If {@code null}, this method will return the a preference that will generally 3621 * apply to metered networks. 3622 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants. 3623 */ 3624 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) 3625 public @MultipathPreference int getMultipathPreference(Network network) { 3626 try { 3627 return mService.getMultipathPreference(network); 3628 } catch (RemoteException e) { 3629 throw e.rethrowFromSystemServer(); 3630 } 3631 } 3632 3633 /** 3634 * Resets all connectivity manager settings back to factory defaults. 3635 * @hide 3636 */ 3637 public void factoryReset() { 3638 try { 3639 mService.factoryReset(); 3640 } catch (RemoteException e) { 3641 throw e.rethrowFromSystemServer(); 3642 } 3643 } 3644 3645 /** 3646 * Binds the current process to {@code network}. All Sockets created in the future 3647 * (and not explicitly bound via a bound SocketFactory from 3648 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to 3649 * {@code network}. All host name resolutions will be limited to {@code network} as well. 3650 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to 3651 * work and all host name resolutions will fail. This is by design so an application doesn't 3652 * accidentally use Sockets it thinks are still bound to a particular {@link Network}. 3653 * To clear binding pass {@code null} for {@code network}. Using individually bound 3654 * Sockets created by Network.getSocketFactory().createSocket() and 3655 * performing network-specific host name resolutions via 3656 * {@link Network#getAllByName Network.getAllByName} is preferred to calling 3657 * {@code bindProcessToNetwork}. 3658 * 3659 * @param network The {@link Network} to bind the current process to, or {@code null} to clear 3660 * the current binding. 3661 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. 3662 */ 3663 public boolean bindProcessToNetwork(Network network) { 3664 // Forcing callers to call thru non-static function ensures ConnectivityManager 3665 // instantiated. 3666 return setProcessDefaultNetwork(network); 3667 } 3668 3669 /** 3670 * Binds the current process to {@code network}. All Sockets created in the future 3671 * (and not explicitly bound via a bound SocketFactory from 3672 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to 3673 * {@code network}. All host name resolutions will be limited to {@code network} as well. 3674 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to 3675 * work and all host name resolutions will fail. This is by design so an application doesn't 3676 * accidentally use Sockets it thinks are still bound to a particular {@link Network}. 3677 * To clear binding pass {@code null} for {@code network}. Using individually bound 3678 * Sockets created by Network.getSocketFactory().createSocket() and 3679 * performing network-specific host name resolutions via 3680 * {@link Network#getAllByName Network.getAllByName} is preferred to calling 3681 * {@code setProcessDefaultNetwork}. 3682 * 3683 * @param network The {@link Network} to bind the current process to, or {@code null} to clear 3684 * the current binding. 3685 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. 3686 * @deprecated This function can throw {@link IllegalStateException}. Use 3687 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} 3688 * is a direct replacement. 3689 */ 3690 @Deprecated 3691 public static boolean setProcessDefaultNetwork(Network network) { 3692 int netId = (network == null) ? NETID_UNSET : network.netId; 3693 if (netId == NetworkUtils.getBoundNetworkForProcess()) { 3694 return true; 3695 } 3696 if (NetworkUtils.bindProcessToNetwork(netId)) { 3697 // Set HTTP proxy system properties to match network. 3698 // TODO: Deprecate this static method and replace it with a non-static version. 3699 try { 3700 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy()); 3701 } catch (SecurityException e) { 3702 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. 3703 Log.e(TAG, "Can't set proxy properties", e); 3704 } 3705 // Must flush DNS cache as new network may have different DNS resolutions. 3706 InetAddress.clearDnsCache(); 3707 // Must flush socket pool as idle sockets will be bound to previous network and may 3708 // cause subsequent fetches to be performed on old network. 3709 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); 3710 return true; 3711 } else { 3712 return false; 3713 } 3714 } 3715 3716 /** 3717 * Returns the {@link Network} currently bound to this process via 3718 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. 3719 * 3720 * @return {@code Network} to which this process is bound, or {@code null}. 3721 */ 3722 public Network getBoundNetworkForProcess() { 3723 // Forcing callers to call thru non-static function ensures ConnectivityManager 3724 // instantiated. 3725 return getProcessDefaultNetwork(); 3726 } 3727 3728 /** 3729 * Returns the {@link Network} currently bound to this process via 3730 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. 3731 * 3732 * @return {@code Network} to which this process is bound, or {@code null}. 3733 * @deprecated Using this function can lead to other functions throwing 3734 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. 3735 * {@code getBoundNetworkForProcess} is a direct replacement. 3736 */ 3737 @Deprecated 3738 public static Network getProcessDefaultNetwork() { 3739 int netId = NetworkUtils.getBoundNetworkForProcess(); 3740 if (netId == NETID_UNSET) return null; 3741 return new Network(netId); 3742 } 3743 3744 private void unsupportedStartingFrom(int version) { 3745 if (Process.myUid() == Process.SYSTEM_UID) { 3746 // The getApplicationInfo() call we make below is not supported in system context, and 3747 // we want to allow the system to use these APIs anyway. 3748 return; 3749 } 3750 3751 if (mContext.getApplicationInfo().targetSdkVersion >= version) { 3752 throw new UnsupportedOperationException( 3753 "This method is not supported in target SDK version " + version + " and above"); 3754 } 3755 } 3756 3757 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, 3758 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. 3759 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and 3760 // remove these exemptions. Note that this check is not secure, and apps can still access these 3761 // functions by accessing ConnectivityService directly. However, it should be clear that doing 3762 // so is unsupported and may break in the future. http://b/22728205 3763 private void checkLegacyRoutingApiAccess() { 3764 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS") 3765 == PackageManager.PERMISSION_GRANTED) { 3766 return; 3767 } 3768 3769 unsupportedStartingFrom(VERSION_CODES.M); 3770 } 3771 3772 /** 3773 * Binds host resolutions performed by this process to {@code network}. 3774 * {@link #bindProcessToNetwork} takes precedence over this setting. 3775 * 3776 * @param network The {@link Network} to bind host resolutions from the current process to, or 3777 * {@code null} to clear the current binding. 3778 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. 3779 * @hide 3780 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. 3781 */ 3782 @Deprecated 3783 public static boolean setProcessDefaultNetworkForHostResolution(Network network) { 3784 return NetworkUtils.bindProcessToNetworkForHostResolution( 3785 network == null ? NETID_UNSET : network.netId); 3786 } 3787 3788 /** 3789 * Device is not restricting metered network activity while application is running on 3790 * background. 3791 */ 3792 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1; 3793 3794 /** 3795 * Device is restricting metered network activity while application is running on background, 3796 * but application is allowed to bypass it. 3797 * <p> 3798 * In this state, application should take action to mitigate metered network access. 3799 * For example, a music streaming application should switch to a low-bandwidth bitrate. 3800 */ 3801 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2; 3802 3803 /** 3804 * Device is restricting metered network activity while application is running on background. 3805 * <p> 3806 * In this state, application should not try to use the network while running on background, 3807 * because it would be denied. 3808 */ 3809 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3; 3810 3811 /** 3812 * A change in the background metered network activity restriction has occurred. 3813 * <p> 3814 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction 3815 * applies to them. 3816 * <p> 3817 * This is only sent to registered receivers, not manifest receivers. 3818 */ 3819 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 3820 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED = 3821 "android.net.conn.RESTRICT_BACKGROUND_CHANGED"; 3822 3823 /** @hide */ 3824 @Retention(RetentionPolicy.SOURCE) 3825 @IntDef(flag = false, value = { 3826 RESTRICT_BACKGROUND_STATUS_DISABLED, 3827 RESTRICT_BACKGROUND_STATUS_WHITELISTED, 3828 RESTRICT_BACKGROUND_STATUS_ENABLED, 3829 }) 3830 public @interface RestrictBackgroundStatus { 3831 } 3832 3833 private INetworkPolicyManager getNetworkPolicyManager() { 3834 synchronized (this) { 3835 if (mNPManager != null) { 3836 return mNPManager; 3837 } 3838 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager 3839 .getService(Context.NETWORK_POLICY_SERVICE)); 3840 return mNPManager; 3841 } 3842 } 3843 3844 /** 3845 * Determines if the calling application is subject to metered network restrictions while 3846 * running on background. 3847 * 3848 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED}, 3849 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED}, 3850 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED} 3851 */ 3852 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() { 3853 try { 3854 return getNetworkPolicyManager().getRestrictBackgroundByCaller(); 3855 } catch (RemoteException e) { 3856 throw e.rethrowFromSystemServer(); 3857 } 3858 } 3859 3860 /** 3861 * The network watchlist is a list of domains and IP addresses that are associated with 3862 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file 3863 * currently used by the system for validation purposes. 3864 * 3865 * @return Hash of network watchlist config file. Null if config does not exist. 3866 */ 3867 public byte[] getNetworkWatchlistConfigHash() { 3868 try { 3869 return mService.getNetworkWatchlistConfigHash(); 3870 } catch (RemoteException e) { 3871 Log.e(TAG, "Unable to get watchlist config hash"); 3872 throw e.rethrowFromSystemServer(); 3873 } 3874 } 3875} 3876