OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import static android.net.ConnectivityManager.TYPE_VPN; | 7 import static android.net.ConnectivityManager.TYPE_VPN; |
8 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; | 8 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
9 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; | 9 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; |
10 import static android.net.NetworkCapabilities.TRANSPORT_VPN; | 10 import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
11 | 11 |
12 import android.Manifest.permission; | 12 import android.Manifest.permission; |
13 import android.annotation.SuppressLint; | 13 import android.annotation.SuppressLint; |
14 import android.annotation.TargetApi; | 14 import android.annotation.TargetApi; |
15 import android.content.BroadcastReceiver; | 15 import android.content.BroadcastReceiver; |
16 import android.content.Context; | 16 import android.content.Context; |
17 import android.content.Intent; | 17 import android.content.Intent; |
18 import android.content.IntentFilter; | 18 import android.content.IntentFilter; |
19 import android.content.pm.PackageManager; | 19 import android.content.pm.PackageManager; |
20 import android.net.ConnectivityManager; | 20 import android.net.ConnectivityManager; |
21 import android.net.ConnectivityManager.NetworkCallback; | 21 import android.net.ConnectivityManager.NetworkCallback; |
22 import android.net.Network; | 22 import android.net.Network; |
23 import android.net.NetworkCapabilities; | 23 import android.net.NetworkCapabilities; |
24 import android.net.NetworkInfo; | 24 import android.net.NetworkInfo; |
25 import android.net.NetworkRequest; | 25 import android.net.NetworkRequest; |
26 import android.net.wifi.WifiInfo; | 26 import android.net.wifi.WifiInfo; |
27 import android.net.wifi.WifiManager; | 27 import android.net.wifi.WifiManager; |
28 import android.os.Build; | 28 import android.os.Build; |
| 29 import android.os.Handler; |
| 30 import android.os.Looper; |
29 import android.telephony.TelephonyManager; | 31 import android.telephony.TelephonyManager; |
30 | 32 |
31 import org.chromium.base.ApplicationState; | 33 import org.chromium.base.ApplicationState; |
32 import org.chromium.base.ApplicationStatus; | 34 import org.chromium.base.ApplicationStatus; |
| 35 import org.chromium.base.BuildConfig; |
33 import org.chromium.base.ContextUtils; | 36 import org.chromium.base.ContextUtils; |
34 import org.chromium.base.ThreadUtils; | |
35 import org.chromium.base.VisibleForTesting; | 37 import org.chromium.base.VisibleForTesting; |
36 import org.chromium.base.metrics.RecordHistogram; | 38 import org.chromium.base.metrics.RecordHistogram; |
37 | 39 |
38 import java.io.IOException; | 40 import java.io.IOException; |
39 import java.util.Arrays; | 41 import java.util.Arrays; |
40 | 42 |
41 import javax.annotation.concurrent.GuardedBy; | 43 import javax.annotation.concurrent.GuardedBy; |
42 | 44 |
43 /** | 45 /** |
44 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv
ity. | 46 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv
ity. |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 permission.ACCESS_WIFI_STATE, mContext.
getPackageName()) | 360 permission.ACCESS_WIFI_STATE, mContext.
getPackageName()) |
359 == PackageManager.PERMISSION_GRANTED; | 361 == PackageManager.PERMISSION_GRANTED; |
360 mWifiManager = mHasWifiPermission | 362 mWifiManager = mHasWifiPermission |
361 ? (WifiManager) mContext.getSystemService(Context.WIFI_SERVI
CE) | 363 ? (WifiManager) mContext.getSystemService(Context.WIFI_SERVI
CE) |
362 : null; | 364 : null; |
363 mHasWifiPermissionComputed = true; | 365 mHasWifiPermissionComputed = true; |
364 return mHasWifiPermission; | 366 return mHasWifiPermission; |
365 } | 367 } |
366 | 368 |
367 String getWifiSsid() { | 369 String getWifiSsid() { |
368 // Synchronized because this method can be called on multiple thread
s (e.g. UI thread | 370 // Synchronized because this method can be called on multiple thread
s (e.g. mLooper |
369 // from a private caller, and another thread calling a public API li
ke | 371 // from a private caller, and another thread calling a public API li
ke |
370 // getCurrentNetworkState) and is otherwise racy. | 372 // getCurrentNetworkState) and is otherwise racy. |
371 synchronized (mLock) { | 373 synchronized (mLock) { |
372 // If app has permission it's faster to query WifiManager direct
ly. | 374 // If app has permission it's faster to query WifiManager direct
ly. |
373 if (hasPermissionLocked()) { | 375 if (hasPermissionLocked()) { |
374 WifiInfo wifiInfo = getWifiInfoLocked(); | 376 WifiInfo wifiInfo = getWifiInfoLocked(); |
375 if (wifiInfo != null) { | 377 if (wifiInfo != null) { |
376 return wifiInfo.getSSID(); | 378 return wifiInfo.getSSID(); |
377 } | 379 } |
378 return ""; | 380 return ""; |
(...skipping 21 matching lines...) Expand all Loading... |
400 RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo2ndSu
ccess", false); | 402 RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo2ndSu
ccess", false); |
401 return null; | 403 return null; |
402 } | 404 } |
403 } | 405 } |
404 } | 406 } |
405 } | 407 } |
406 | 408 |
407 // This class gets called back by ConnectivityManager whenever networks come | 409 // This class gets called back by ConnectivityManager whenever networks come |
408 // and go. It gets called back on a special handler thread | 410 // and go. It gets called back on a special handler thread |
409 // ConnectivityManager creates for making the callbacks. The callbacks in | 411 // ConnectivityManager creates for making the callbacks. The callbacks in |
410 // turn post to the UI thread where mObserver lives. | 412 // turn post to mLooper where mObserver lives. |
411 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 413 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
412 private class MyNetworkCallback extends NetworkCallback { | 414 private class MyNetworkCallback extends NetworkCallback { |
413 // If non-null, this indicates a VPN is in place for the current user, a
nd no other | 415 // If non-null, this indicates a VPN is in place for the current user, a
nd no other |
414 // networks are accessible. | 416 // networks are accessible. |
415 private Network mVpnInPlace; | 417 private Network mVpnInPlace; |
416 | 418 |
417 // Initialize mVpnInPlace. | 419 // Initialize mVpnInPlace. |
418 void initializeVpnInPlace() { | 420 void initializeVpnInPlace() { |
419 final Network[] networks = getAllNetworksFiltered(mConnectivityManag
erDelegate, null); | 421 final Network[] networks = getAllNetworksFiltered(mConnectivityManag
erDelegate, null); |
420 mVpnInPlace = null; | 422 mVpnInPlace = null; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 if (ignoreConnectedNetwork(network, capabilities)) { | 477 if (ignoreConnectedNetwork(network, capabilities)) { |
476 return; | 478 return; |
477 } | 479 } |
478 final boolean makeVpnDefault = capabilities.hasTransport(TRANSPORT_V
PN); | 480 final boolean makeVpnDefault = capabilities.hasTransport(TRANSPORT_V
PN); |
479 if (makeVpnDefault) { | 481 if (makeVpnDefault) { |
480 mVpnInPlace = network; | 482 mVpnInPlace = network; |
481 } | 483 } |
482 final long netId = networkToNetId(network); | 484 final long netId = networkToNetId(network); |
483 @ConnectionType | 485 @ConnectionType |
484 final int connectionType = mConnectivityManagerDelegate.getConnectio
nType(network); | 486 final int connectionType = mConnectivityManagerDelegate.getConnectio
nType(network); |
485 ThreadUtils.postOnUiThread(new Runnable() { | 487 runOnThread(new Runnable() { |
486 @Override | 488 @Override |
487 public void run() { | 489 public void run() { |
488 mObserver.onNetworkConnect(netId, connectionType); | 490 mObserver.onNetworkConnect(netId, connectionType); |
489 if (makeVpnDefault) { | 491 if (makeVpnDefault) { |
490 // Make VPN the default network. | 492 // Make VPN the default network. |
491 mObserver.onConnectionTypeChanged(connectionType); | 493 mObserver.onConnectionTypeChanged(connectionType); |
492 // Purge all other networks as they're inaccessible to C
hrome now. | 494 // Purge all other networks as they're inaccessible to C
hrome now. |
493 mObserver.purgeActiveNetworkList(new long[] {netId}); | 495 mObserver.purgeActiveNetworkList(new long[] {netId}); |
494 } | 496 } |
495 } | 497 } |
496 }); | 498 }); |
497 } | 499 } |
498 | 500 |
499 @Override | 501 @Override |
500 public void onCapabilitiesChanged( | 502 public void onCapabilitiesChanged( |
501 Network network, NetworkCapabilities networkCapabilities) { | 503 Network network, NetworkCapabilities networkCapabilities) { |
502 if (ignoreConnectedNetwork(network, networkCapabilities)) { | 504 if (ignoreConnectedNetwork(network, networkCapabilities)) { |
503 return; | 505 return; |
504 } | 506 } |
505 // A capabilities change may indicate the ConnectionType has changed
, | 507 // A capabilities change may indicate the ConnectionType has changed
, |
506 // so forward the new ConnectionType along to observer. | 508 // so forward the new ConnectionType along to observer. |
507 final long netId = networkToNetId(network); | 509 final long netId = networkToNetId(network); |
508 final int connectionType = mConnectivityManagerDelegate.getConnectio
nType(network); | 510 final int connectionType = mConnectivityManagerDelegate.getConnectio
nType(network); |
509 ThreadUtils.postOnUiThread(new Runnable() { | 511 runOnThread(new Runnable() { |
510 @Override | 512 @Override |
511 public void run() { | 513 public void run() { |
512 mObserver.onNetworkConnect(netId, connectionType); | 514 mObserver.onNetworkConnect(netId, connectionType); |
513 } | 515 } |
514 }); | 516 }); |
515 } | 517 } |
516 | 518 |
517 @Override | 519 @Override |
518 public void onLosing(Network network, int maxMsToLive) { | 520 public void onLosing(Network network, int maxMsToLive) { |
519 if (ignoreConnectedNetwork(network, null)) { | 521 if (ignoreConnectedNetwork(network, null)) { |
520 return; | 522 return; |
521 } | 523 } |
522 final long netId = networkToNetId(network); | 524 final long netId = networkToNetId(network); |
523 ThreadUtils.postOnUiThread(new Runnable() { | 525 runOnThread(new Runnable() { |
524 @Override | 526 @Override |
525 public void run() { | 527 public void run() { |
526 mObserver.onNetworkSoonToDisconnect(netId); | 528 mObserver.onNetworkSoonToDisconnect(netId); |
527 } | 529 } |
528 }); | 530 }); |
529 } | 531 } |
530 | 532 |
531 @Override | 533 @Override |
532 public void onLost(final Network network) { | 534 public void onLost(final Network network) { |
533 if (ignoreNetworkDueToVpn(network)) { | 535 if (ignoreNetworkDueToVpn(network)) { |
534 return; | 536 return; |
535 } | 537 } |
536 ThreadUtils.postOnUiThread(new Runnable() { | 538 runOnThread(new Runnable() { |
537 @Override | 539 @Override |
538 public void run() { | 540 public void run() { |
539 mObserver.onNetworkDisconnect(networkToNetId(network)); | 541 mObserver.onNetworkDisconnect(networkToNetId(network)); |
540 } | 542 } |
541 }); | 543 }); |
542 // If the VPN is going away, inform observer that other networks tha
t were previously | 544 // If the VPN is going away, inform observer that other networks tha
t were previously |
543 // hidden by ignoreNetworkDueToVpn() are now available for use, now
that this user's | 545 // hidden by ignoreNetworkDueToVpn() are now available for use, now
that this user's |
544 // traffic is not forced into the VPN. | 546 // traffic is not forced into the VPN. |
545 if (mVpnInPlace != null) { | 547 if (mVpnInPlace != null) { |
546 assert network.equals(mVpnInPlace); | 548 assert network.equals(mVpnInPlace); |
547 mVpnInPlace = null; | 549 mVpnInPlace = null; |
548 for (Network newNetwork : | 550 for (Network newNetwork : |
549 getAllNetworksFiltered(mConnectivityManagerDelegate, net
work)) { | 551 getAllNetworksFiltered(mConnectivityManagerDelegate, net
work)) { |
550 onAvailable(newNetwork); | 552 onAvailable(newNetwork); |
551 } | 553 } |
552 @ConnectionType | 554 @ConnectionType |
553 final int newConnectionType = convertToConnectionType(getCurrent
NetworkState()); | 555 final int newConnectionType = convertToConnectionType(getCurrent
NetworkState()); |
554 ThreadUtils.postOnUiThread(new Runnable() { | 556 runOnThread(new Runnable() { |
555 @Override | 557 @Override |
556 public void run() { | 558 public void run() { |
557 mObserver.onConnectionTypeChanged(newConnectionType); | 559 mObserver.onConnectionTypeChanged(newConnectionType); |
558 } | 560 } |
559 }); | 561 }); |
560 } | 562 } |
561 } | 563 } |
562 } | 564 } |
563 | 565 |
564 /** | 566 /** |
(...skipping 23 matching lines...) Expand all Loading... |
588 * Initializes the policy with the notifier, overriding subclasses shoul
d always | 590 * Initializes the policy with the notifier, overriding subclasses shoul
d always |
589 * call this method. | 591 * call this method. |
590 */ | 592 */ |
591 protected void init(NetworkChangeNotifierAutoDetect notifier) { | 593 protected void init(NetworkChangeNotifierAutoDetect notifier) { |
592 mNotifier = notifier; | 594 mNotifier = notifier; |
593 } | 595 } |
594 | 596 |
595 protected abstract void destroy(); | 597 protected abstract void destroy(); |
596 } | 598 } |
597 | 599 |
598 private static final String TAG = "NetworkChangeNotifierAutoDetect"; | 600 private static final String TAG = NetworkChangeNotifierAutoDetect.class.getS
impleName(); |
599 private static final int UNKNOWN_LINK_SPEED = -1; | 601 private static final int UNKNOWN_LINK_SPEED = -1; |
600 | 602 |
| 603 // {@link Looper} for the thread this object lives on. |
| 604 private final Looper mLooper; |
| 605 // Used to post to the thread this object lives on. |
| 606 private final Handler mHandler; |
| 607 // {@link IntentFilter} for incoming global broadcast {@link Intent}s this o
bject listens for. |
601 private final NetworkConnectivityIntentFilter mIntentFilter; | 608 private final NetworkConnectivityIntentFilter mIntentFilter; |
| 609 // Notifications are sent to this {@link Observer}. |
602 private final Observer mObserver; | 610 private final Observer mObserver; |
603 private final RegistrationPolicy mRegistrationPolicy; | 611 private final RegistrationPolicy mRegistrationPolicy; |
604 | 612 |
605 // mConnectivityManagerDelegates and mWifiManagerDelegate are only non-final
for testing. | 613 // mConnectivityManagerDelegates and mWifiManagerDelegate are only non-final
for testing. |
606 private ConnectivityManagerDelegate mConnectivityManagerDelegate; | 614 private ConnectivityManagerDelegate mConnectivityManagerDelegate; |
607 private WifiManagerDelegate mWifiManagerDelegate; | 615 private WifiManagerDelegate mWifiManagerDelegate; |
608 // mNetworkCallback and mNetworkRequest are only non-null in Android L and a
bove. | 616 // mNetworkCallback and mNetworkRequest are only non-null in Android L and a
bove. |
609 private final MyNetworkCallback mNetworkCallback; | 617 private final MyNetworkCallback mNetworkCallback; |
610 private final NetworkRequest mNetworkRequest; | 618 private final NetworkRequest mNetworkRequest; |
611 private boolean mRegistered; | 619 private boolean mRegistered; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 * Called to cause a purge of cached lists of active networks, of any | 674 * Called to cause a purge of cached lists of active networks, of any |
667 * networks not in the accompanying list of active networks. This is | 675 * networks not in the accompanying list of active networks. This is |
668 * issued if a period elapsed where disconnected notifications may have | 676 * issued if a period elapsed where disconnected notifications may have |
669 * been missed, and acts to keep cached lists of active networks | 677 * been missed, and acts to keep cached lists of active networks |
670 * accurate. Only called on Android L and above. | 678 * accurate. Only called on Android L and above. |
671 */ | 679 */ |
672 public void purgeActiveNetworkList(long[] activeNetIds); | 680 public void purgeActiveNetworkList(long[] activeNetIds); |
673 } | 681 } |
674 | 682 |
675 /** | 683 /** |
676 * Constructs a NetworkChangeNotifierAutoDetect. Should only be called on UI
thread. | 684 * Constructs a NetworkChangeNotifierAutoDetect. Lives on calling thread, r
eceives broadcast |
| 685 * notifications on the UI thread and forwards the notifications to be proce
ssed on the calling |
| 686 * thread. |
677 * @param policy The RegistrationPolicy which determines when this class sho
uld watch | 687 * @param policy The RegistrationPolicy which determines when this class sho
uld watch |
678 * for network changes (e.g. see (@link RegistrationPolicyAlwaysRegister
} and | 688 * for network changes (e.g. see (@link RegistrationPolicyAlwaysRegister
} and |
679 * {@link RegistrationPolicyApplicationStatus}). | 689 * {@link RegistrationPolicyApplicationStatus}). |
680 */ | 690 */ |
681 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 691 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
682 public NetworkChangeNotifierAutoDetect(Observer observer, RegistrationPolicy
policy) { | 692 public NetworkChangeNotifierAutoDetect(Observer observer, RegistrationPolicy
policy) { |
683 // Since BroadcastReceiver is always called back on UI thread, ensure | 693 mLooper = Looper.myLooper(); |
684 // running on UI thread so notification logic can be single-threaded. | 694 mHandler = new Handler(mLooper); |
685 ThreadUtils.assertOnUiThread(); | |
686 mObserver = observer; | 695 mObserver = observer; |
687 mConnectivityManagerDelegate = | 696 mConnectivityManagerDelegate = |
688 new ConnectivityManagerDelegate(ContextUtils.getApplicationConte
xt()); | 697 new ConnectivityManagerDelegate(ContextUtils.getApplicationConte
xt()); |
689 mWifiManagerDelegate = new WifiManagerDelegate(ContextUtils.getApplicati
onContext()); | 698 mWifiManagerDelegate = new WifiManagerDelegate(ContextUtils.getApplicati
onContext()); |
690 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 699 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
691 mNetworkCallback = new MyNetworkCallback(); | 700 mNetworkCallback = new MyNetworkCallback(); |
692 mNetworkRequest = new NetworkRequest.Builder() | 701 mNetworkRequest = new NetworkRequest.Builder() |
693 .addCapability(NET_CAPABILITY_INTERNET) | 702 .addCapability(NET_CAPABILITY_INTERNET) |
694 // Need to hear about VPNs too. | 703 // Need to hear about VPNs too. |
695 .removeCapability(NET_CAPABILITY_NOT_VPN) | 704 .removeCapability(NET_CAPABILITY_NOT_VPN) |
696 .build(); | 705 .build(); |
697 } else { | 706 } else { |
698 mNetworkCallback = null; | 707 mNetworkCallback = null; |
699 mNetworkRequest = null; | 708 mNetworkRequest = null; |
700 } | 709 } |
701 final NetworkState networkState = getCurrentNetworkState(); | 710 final NetworkState networkState = getCurrentNetworkState(); |
702 mConnectionType = convertToConnectionType(networkState); | 711 mConnectionType = convertToConnectionType(networkState); |
703 mWifiSSID = networkState.getWifiSsid(); | 712 mWifiSSID = networkState.getWifiSsid(); |
704 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); | 713 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); |
705 mMaxBandwidthConnectionType = mConnectionType; | 714 mMaxBandwidthConnectionType = mConnectionType; |
706 mIntentFilter = new NetworkConnectivityIntentFilter(); | 715 mIntentFilter = new NetworkConnectivityIntentFilter(); |
707 mIgnoreNextBroadcast = false; | 716 mIgnoreNextBroadcast = false; |
708 mShouldSignalObserver = false; | 717 mShouldSignalObserver = false; |
709 mRegistrationPolicy = policy; | 718 mRegistrationPolicy = policy; |
710 mRegistrationPolicy.init(this); | 719 mRegistrationPolicy.init(this); |
711 mShouldSignalObserver = true; | 720 mShouldSignalObserver = true; |
712 } | 721 } |
713 | 722 |
| 723 private boolean onThread() { |
| 724 return mLooper == Looper.myLooper(); |
| 725 } |
| 726 |
| 727 private void assertOnThread() { |
| 728 if (BuildConfig.DCHECK_IS_ON && !onThread()) { |
| 729 throw new IllegalStateException( |
| 730 "Must be called on NetworkChangeNotifierAutoDetect thread.")
; |
| 731 } |
| 732 } |
| 733 |
| 734 private void runOnThread(Runnable r) { |
| 735 if (onThread()) { |
| 736 r.run(); |
| 737 } else { |
| 738 mHandler.post(r); |
| 739 } |
| 740 } |
| 741 |
714 /** | 742 /** |
715 * Allows overriding the ConnectivityManagerDelegate for tests. | 743 * Allows overriding the ConnectivityManagerDelegate for tests. |
716 */ | 744 */ |
717 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele
gate) { | 745 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele
gate) { |
718 mConnectivityManagerDelegate = delegate; | 746 mConnectivityManagerDelegate = delegate; |
719 } | 747 } |
720 | 748 |
721 /** | 749 /** |
722 * Allows overriding the WifiManagerDelegate for tests. | 750 * Allows overriding the WifiManagerDelegate for tests. |
723 */ | 751 */ |
724 void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) { | 752 void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) { |
725 mWifiManagerDelegate = delegate; | 753 mWifiManagerDelegate = delegate; |
726 } | 754 } |
727 | 755 |
728 @VisibleForTesting | 756 @VisibleForTesting |
729 RegistrationPolicy getRegistrationPolicy() { | 757 RegistrationPolicy getRegistrationPolicy() { |
730 return mRegistrationPolicy; | 758 return mRegistrationPolicy; |
731 } | 759 } |
732 | 760 |
733 /** | 761 /** |
734 * Returns whether the object has registered to receive network connectivity
intents. | 762 * Returns whether the object has registered to receive network connectivity
intents. |
735 */ | 763 */ |
736 @VisibleForTesting | 764 @VisibleForTesting |
737 boolean isReceiverRegisteredForTesting() { | 765 boolean isReceiverRegisteredForTesting() { |
738 return mRegistered; | 766 return mRegistered; |
739 } | 767 } |
740 | 768 |
741 public void destroy() { | 769 public void destroy() { |
| 770 assertOnThread(); |
742 mRegistrationPolicy.destroy(); | 771 mRegistrationPolicy.destroy(); |
743 unregister(); | 772 unregister(); |
744 } | 773 } |
745 | 774 |
746 /** | 775 /** |
747 * Registers a BroadcastReceiver in the given context. | 776 * Registers a BroadcastReceiver in the given context. |
748 */ | 777 */ |
749 public void register() { | 778 public void register() { |
750 ThreadUtils.assertOnUiThread(); | 779 assertOnThread(); |
751 if (mRegistered) return; | 780 if (mRegistered) return; |
752 | 781 |
753 if (mShouldSignalObserver) { | 782 if (mShouldSignalObserver) { |
754 final NetworkState networkState = getCurrentNetworkState(); | 783 final NetworkState networkState = getCurrentNetworkState(); |
755 connectionTypeChanged(networkState); | 784 connectionTypeChanged(networkState); |
756 maxBandwidthChanged(networkState); | 785 maxBandwidthChanged(networkState); |
757 } | 786 } |
758 // When registering for a sticky broadcast, like CONNECTIVITY_ACTION, if
registerReceiver | 787 // When registering for a sticky broadcast, like CONNECTIVITY_ACTION, if
registerReceiver |
759 // returns non-null, it means the broadcast was previously issued and on
Receive() will be | 788 // returns non-null, it means the broadcast was previously issued and on
Receive() will be |
760 // immediately called with this previous Intent. Since this initial call
back doesn't | 789 // immediately called with this previous Intent. Since this initial call
back doesn't |
(...skipping 20 matching lines...) Expand all Loading... |
781 } | 810 } |
782 mObserver.purgeActiveNetworkList(netIds); | 811 mObserver.purgeActiveNetworkList(netIds); |
783 } | 812 } |
784 } | 813 } |
785 } | 814 } |
786 | 815 |
787 /** | 816 /** |
788 * Unregisters a BroadcastReceiver in the given context. | 817 * Unregisters a BroadcastReceiver in the given context. |
789 */ | 818 */ |
790 public void unregister() { | 819 public void unregister() { |
| 820 assertOnThread(); |
791 if (!mRegistered) return; | 821 if (!mRegistered) return; |
792 ContextUtils.getApplicationContext().unregisterReceiver(this); | 822 ContextUtils.getApplicationContext().unregisterReceiver(this); |
793 mRegistered = false; | 823 mRegistered = false; |
794 if (mNetworkCallback != null) { | 824 if (mNetworkCallback != null) { |
795 mConnectivityManagerDelegate.unregisterNetworkCallback(mNetworkCallb
ack); | 825 mConnectivityManagerDelegate.unregisterNetworkCallback(mNetworkCallb
ack); |
796 } | 826 } |
797 } | 827 } |
798 | 828 |
799 public NetworkState getCurrentNetworkState() { | 829 public NetworkState getCurrentNetworkState() { |
800 return mConnectivityManagerDelegate.getNetworkState(mWifiManagerDelegate
); | 830 return mConnectivityManagerDelegate.getNetworkState(mWifiManagerDelegate
); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 * instead. For more on NetInfo, see http://w3c.github.io/netinfo/. | 1018 * instead. For more on NetInfo, see http://w3c.github.io/netinfo/. |
989 */ | 1019 */ |
990 public double getCurrentMaxBandwidthInMbps(NetworkState networkState) { | 1020 public double getCurrentMaxBandwidthInMbps(NetworkState networkState) { |
991 return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype( | 1021 return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype( |
992 convertToConnectionSubtype(networkState)); | 1022 convertToConnectionSubtype(networkState)); |
993 } | 1023 } |
994 | 1024 |
995 // BroadcastReceiver | 1025 // BroadcastReceiver |
996 @Override | 1026 @Override |
997 public void onReceive(Context context, Intent intent) { | 1027 public void onReceive(Context context, Intent intent) { |
998 if (mIgnoreNextBroadcast) { | 1028 runOnThread(new Runnable() { |
999 mIgnoreNextBroadcast = false; | 1029 @Override |
1000 return; | 1030 public void run() { |
1001 } | 1031 // Once execution begins on the correct thread, make sure unregi
ster() hasn't |
1002 final NetworkState networkState = getCurrentNetworkState(); | 1032 // been called in the mean time. Ignore the broadcast if unregis
ter() was called. |
1003 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
{ | 1033 if (!mRegistered) { |
1004 connectionTypeChanged(networkState); | 1034 return; |
1005 maxBandwidthChanged(networkState); | 1035 } |
1006 } | 1036 if (mIgnoreNextBroadcast) { |
| 1037 mIgnoreNextBroadcast = false; |
| 1038 return; |
| 1039 } |
| 1040 final NetworkState networkState = getCurrentNetworkState(); |
| 1041 connectionTypeChanged(networkState); |
| 1042 maxBandwidthChanged(networkState); |
| 1043 } |
| 1044 }); |
1007 } | 1045 } |
1008 | 1046 |
1009 private void connectionTypeChanged(NetworkState networkState) { | 1047 private void connectionTypeChanged(NetworkState networkState) { |
1010 @ConnectionType | 1048 @ConnectionType |
1011 int newConnectionType = convertToConnectionType(networkState); | 1049 int newConnectionType = convertToConnectionType(networkState); |
1012 String newWifiSSID = networkState.getWifiSsid(); | 1050 String newWifiSSID = networkState.getWifiSsid(); |
1013 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID
)) return; | 1051 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID
)) return; |
1014 | 1052 |
1015 mConnectionType = newConnectionType; | 1053 mConnectionType = newConnectionType; |
1016 mWifiSSID = newWifiSSID; | 1054 mWifiSSID = newWifiSSID; |
(...skipping 30 matching lines...) Expand all Loading... |
1047 return network.getNetworkHandle(); | 1085 return network.getNetworkHandle(); |
1048 } else { | 1086 } else { |
1049 // NOTE(pauljensen): This depends on Android framework implementatio
n details. These | 1087 // NOTE(pauljensen): This depends on Android framework implementatio
n details. These |
1050 // details cannot change because Lollipop is long since released. | 1088 // details cannot change because Lollipop is long since released. |
1051 // NetIDs are only 16-bit so use parseInt. This function returns a l
ong because | 1089 // NetIDs are only 16-bit so use parseInt. This function returns a l
ong because |
1052 // getNetworkHandle() returns a long. | 1090 // getNetworkHandle() returns a long. |
1053 return Integer.parseInt(network.toString()); | 1091 return Integer.parseInt(network.toString()); |
1054 } | 1092 } |
1055 } | 1093 } |
1056 } | 1094 } |
OLD | NEW |