Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java

Issue 2812963002: [Cronet] Move initialization to a new thread rather than the UI thread. (Closed)
Patch Set: mostly renaming Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
33 import org.chromium.base.ThreadUtils; 35 import org.chromium.base.BuildConfig;
34 import org.chromium.base.VisibleForTesting; 36 import org.chromium.base.VisibleForTesting;
35 import org.chromium.base.metrics.RecordHistogram; 37 import org.chromium.base.metrics.RecordHistogram;
36 import org.chromium.net.ConnectionType.ConnectionTypeEnum; 38 import org.chromium.net.ConnectionType.ConnectionTypeEnum;
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 /**
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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 @ConnectionTypeEnum 485 @ConnectionTypeEnum
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 @ConnectionTypeEnum 554 @ConnectionTypeEnum
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
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 private final Looper mLooper;
604 private final Handler mHandler;
605
xunjieli 2017/04/13 18:44:54 nit: maybe get rid of this blank line.
pauljensen 2017/05/01 15:35:53 Done, I also added some comments.
601 private final NetworkConnectivityIntentFilter mIntentFilter; 606 private final NetworkConnectivityIntentFilter mIntentFilter;
602 private final Observer mObserver; 607 private final Observer mObserver;
603 private final Context mContext; 608 private final Context mContext;
604 private final RegistrationPolicy mRegistrationPolicy; 609 private final RegistrationPolicy mRegistrationPolicy;
605 610
606 // mConnectivityManagerDelegates and mWifiManagerDelegate are only non-final for testing. 611 // mConnectivityManagerDelegates and mWifiManagerDelegate are only non-final for testing.
607 private ConnectivityManagerDelegate mConnectivityManagerDelegate; 612 private ConnectivityManagerDelegate mConnectivityManagerDelegate;
608 private WifiManagerDelegate mWifiManagerDelegate; 613 private WifiManagerDelegate mWifiManagerDelegate;
609 // mNetworkCallback and mNetworkRequest are only non-null in Android L and a bove. 614 // mNetworkCallback and mNetworkRequest are only non-null in Android L and a bove.
610 private final MyNetworkCallback mNetworkCallback; 615 private final MyNetworkCallback mNetworkCallback;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 * Called to cause a purge of cached lists of active networks, of any 672 * Called to cause a purge of cached lists of active networks, of any
668 * networks not in the accompanying list of active networks. This is 673 * networks not in the accompanying list of active networks. This is
669 * issued if a period elapsed where disconnected notifications may have 674 * issued if a period elapsed where disconnected notifications may have
670 * been missed, and acts to keep cached lists of active networks 675 * been missed, and acts to keep cached lists of active networks
671 * accurate. Only called on Android L and above. 676 * accurate. Only called on Android L and above.
672 */ 677 */
673 public void purgeActiveNetworkList(long[] activeNetIds); 678 public void purgeActiveNetworkList(long[] activeNetIds);
674 } 679 }
675 680
676 /** 681 /**
677 * Constructs a NetworkChangeNotifierAutoDetect. Should only be called on UI thread. 682 * Constructs a NetworkChangeNotifierAutoDetect. Lives on calling thread.
xunjieli 2017/04/13 18:44:54 Could you add the part about receiving notificatio
pauljensen 2017/05/01 15:35:53 Done.
678 * @param policy The RegistrationPolicy which determines when this class sho uld watch 683 * @param policy The RegistrationPolicy which determines when this class sho uld watch
679 * for network changes (e.g. see (@link RegistrationPolicyAlwaysRegister } and 684 * for network changes (e.g. see (@link RegistrationPolicyAlwaysRegister } and
680 * {@link RegistrationPolicyApplicationStatus}). 685 * {@link RegistrationPolicyApplicationStatus}).
681 */ 686 */
682 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 687 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
683 public NetworkChangeNotifierAutoDetect( 688 public NetworkChangeNotifierAutoDetect(
684 Observer observer, Context context, RegistrationPolicy policy) { 689 Observer observer, Context context, RegistrationPolicy policy) {
685 // Since BroadcastReceiver is always called back on UI thread, ensure 690 mLooper = Looper.myLooper();
686 // running on UI thread so notification logic can be single-threaded. 691 mHandler = new Handler(mLooper);
687 ThreadUtils.assertOnUiThread();
688 mObserver = observer; 692 mObserver = observer;
689 mContext = context.getApplicationContext(); 693 mContext = context.getApplicationContext();
690 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context); 694 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
691 mWifiManagerDelegate = new WifiManagerDelegate(context); 695 mWifiManagerDelegate = new WifiManagerDelegate(context);
692 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 696 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
693 mNetworkCallback = new MyNetworkCallback(); 697 mNetworkCallback = new MyNetworkCallback();
694 mNetworkRequest = new NetworkRequest.Builder() 698 mNetworkRequest = new NetworkRequest.Builder()
695 .addCapability(NET_CAPABILITY_INTERNET) 699 .addCapability(NET_CAPABILITY_INTERNET)
696 // Need to hear about VPNs too. 700 // Need to hear about VPNs too.
697 .removeCapability(NET_CAPABILITY_NOT_VPN) 701 .removeCapability(NET_CAPABILITY_NOT_VPN)
698 .build(); 702 .build();
699 } else { 703 } else {
700 mNetworkCallback = null; 704 mNetworkCallback = null;
701 mNetworkRequest = null; 705 mNetworkRequest = null;
702 } 706 }
703 final NetworkState networkState = getCurrentNetworkState(); 707 final NetworkState networkState = getCurrentNetworkState();
704 mConnectionType = convertToConnectionType(networkState); 708 mConnectionType = convertToConnectionType(networkState);
705 mWifiSSID = networkState.getWifiSsid(); 709 mWifiSSID = networkState.getWifiSsid();
706 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); 710 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
707 mMaxBandwidthConnectionType = mConnectionType; 711 mMaxBandwidthConnectionType = mConnectionType;
708 mIntentFilter = new NetworkConnectivityIntentFilter(); 712 mIntentFilter = new NetworkConnectivityIntentFilter();
709 mIgnoreNextBroadcast = false; 713 mIgnoreNextBroadcast = false;
710 mShouldSignalObserver = false; 714 mShouldSignalObserver = false;
711 mRegistrationPolicy = policy; 715 mRegistrationPolicy = policy;
712 mRegistrationPolicy.init(this); 716 mRegistrationPolicy.init(this);
713 mShouldSignalObserver = true; 717 mShouldSignalObserver = true;
714 } 718 }
715 719
720 private boolean onThread() {
721 return mLooper == Looper.myLooper();
722 }
723
724 private void assertOnThread() {
725 if (BuildConfig.DCHECK_IS_ON && !onThread()) {
726 throw new IllegalStateException(
727 "Must be called on NetworkChangeNotifierAutoDetect thread.") ;
728 }
729 }
730
731 private void runOnThread(Runnable r) {
732 if (onThread()) {
733 r.run();
734 } else {
735 mHandler.post(r);
736 }
737 }
738
716 /** 739 /**
717 * Allows overriding the ConnectivityManagerDelegate for tests. 740 * Allows overriding the ConnectivityManagerDelegate for tests.
718 */ 741 */
719 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele gate) { 742 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele gate) {
720 mConnectivityManagerDelegate = delegate; 743 mConnectivityManagerDelegate = delegate;
721 } 744 }
722 745
723 /** 746 /**
724 * Allows overriding the WifiManagerDelegate for tests. 747 * Allows overriding the WifiManagerDelegate for tests.
725 */ 748 */
726 void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) { 749 void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) {
727 mWifiManagerDelegate = delegate; 750 mWifiManagerDelegate = delegate;
728 } 751 }
729 752
730 @VisibleForTesting 753 @VisibleForTesting
731 RegistrationPolicy getRegistrationPolicy() { 754 RegistrationPolicy getRegistrationPolicy() {
732 return mRegistrationPolicy; 755 return mRegistrationPolicy;
733 } 756 }
734 757
735 /** 758 /**
736 * Returns whether the object has registered to receive network connectivity intents. 759 * Returns whether the object has registered to receive network connectivity intents.
737 */ 760 */
738 @VisibleForTesting 761 @VisibleForTesting
739 boolean isReceiverRegisteredForTesting() { 762 boolean isReceiverRegisteredForTesting() {
740 return mRegistered; 763 return mRegistered;
741 } 764 }
742 765
743 public void destroy() { 766 public void destroy() {
767 assertOnThread();
744 mRegistrationPolicy.destroy(); 768 mRegistrationPolicy.destroy();
745 unregister(); 769 unregister();
746 } 770 }
747 771
748 /** 772 /**
749 * Registers a BroadcastReceiver in the given context. 773 * Registers a BroadcastReceiver in the given context.
750 */ 774 */
751 public void register() { 775 public void register() {
752 ThreadUtils.assertOnUiThread(); 776 assertOnThread();
753 if (mRegistered) return; 777 if (mRegistered) return;
754 778
755 if (mShouldSignalObserver) { 779 if (mShouldSignalObserver) {
756 final NetworkState networkState = getCurrentNetworkState(); 780 final NetworkState networkState = getCurrentNetworkState();
757 connectionTypeChanged(networkState); 781 connectionTypeChanged(networkState);
758 maxBandwidthChanged(networkState); 782 maxBandwidthChanged(networkState);
759 } 783 }
760 // When registering for a sticky broadcast, like CONNECTIVITY_ACTION, if registerReceiver 784 // When registering for a sticky broadcast, like CONNECTIVITY_ACTION, if registerReceiver
761 // returns non-null, it means the broadcast was previously issued and on Receive() will be 785 // returns non-null, it means the broadcast was previously issued and on Receive() will be
762 // immediately called with this previous Intent. Since this initial call back doesn't 786 // immediately called with this previous Intent. Since this initial call back doesn't
(...skipping 19 matching lines...) Expand all
782 } 806 }
783 mObserver.purgeActiveNetworkList(netIds); 807 mObserver.purgeActiveNetworkList(netIds);
784 } 808 }
785 } 809 }
786 } 810 }
787 811
788 /** 812 /**
789 * Unregisters a BroadcastReceiver in the given context. 813 * Unregisters a BroadcastReceiver in the given context.
790 */ 814 */
791 public void unregister() { 815 public void unregister() {
816 assertOnThread();
792 if (!mRegistered) return; 817 if (!mRegistered) return;
793 mContext.unregisterReceiver(this); 818 mContext.unregisterReceiver(this);
794 mRegistered = false; 819 mRegistered = false;
795 if (mNetworkCallback != null) { 820 if (mNetworkCallback != null) {
796 mConnectivityManagerDelegate.unregisterNetworkCallback(mNetworkCallb ack); 821 mConnectivityManagerDelegate.unregisterNetworkCallback(mNetworkCallb ack);
797 } 822 }
798 } 823 }
799 824
800 public NetworkState getCurrentNetworkState() { 825 public NetworkState getCurrentNetworkState() {
801 return mConnectivityManagerDelegate.getNetworkState(mWifiManagerDelegate ); 826 return mConnectivityManagerDelegate.getNetworkState(mWifiManagerDelegate );
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 * instead. For more on NetInfo, see http://w3c.github.io/netinfo/. 1014 * instead. For more on NetInfo, see http://w3c.github.io/netinfo/.
990 */ 1015 */
991 public double getCurrentMaxBandwidthInMbps(NetworkState networkState) { 1016 public double getCurrentMaxBandwidthInMbps(NetworkState networkState) {
992 return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype( 1017 return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype(
993 convertToConnectionSubtype(networkState)); 1018 convertToConnectionSubtype(networkState));
994 } 1019 }
995 1020
996 // BroadcastReceiver 1021 // BroadcastReceiver
997 @Override 1022 @Override
998 public void onReceive(Context context, Intent intent) { 1023 public void onReceive(Context context, Intent intent) {
999 if (mIgnoreNextBroadcast) { 1024 runOnThread(new Runnable() {
1000 mIgnoreNextBroadcast = false; 1025 @Override
1001 return; 1026 public void run() {
1002 } 1027 if (!mRegistered) {
xunjieli 2017/04/13 18:44:54 I think this needs a comment to note why this chec
pauljensen 2017/05/01 15:35:53 Done.
1003 final NetworkState networkState = getCurrentNetworkState(); 1028 return;
1004 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { 1029 }
1005 connectionTypeChanged(networkState); 1030 if (mIgnoreNextBroadcast) {
1006 maxBandwidthChanged(networkState); 1031 mIgnoreNextBroadcast = false;
1007 } 1032 return;
1033 }
1034 final NetworkState networkState = getCurrentNetworkState();
1035 connectionTypeChanged(networkState);
1036 maxBandwidthChanged(networkState);
1037 }
1038 });
1008 } 1039 }
1009 1040
1010 private void connectionTypeChanged(NetworkState networkState) { 1041 private void connectionTypeChanged(NetworkState networkState) {
1011 @ConnectionTypeEnum 1042 @ConnectionTypeEnum
1012 int newConnectionType = convertToConnectionType(networkState); 1043 int newConnectionType = convertToConnectionType(networkState);
1013 String newWifiSSID = networkState.getWifiSsid(); 1044 String newWifiSSID = networkState.getWifiSsid();
1014 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID )) return; 1045 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID )) return;
1015 1046
1016 mConnectionType = newConnectionType; 1047 mConnectionType = newConnectionType;
1017 mWifiSSID = newWifiSSID; 1048 mWifiSSID = newWifiSSID;
(...skipping 30 matching lines...) Expand all
1048 return network.getNetworkHandle(); 1079 return network.getNetworkHandle();
1049 } else { 1080 } else {
1050 // NOTE(pauljensen): This depends on Android framework implementatio n details. These 1081 // NOTE(pauljensen): This depends on Android framework implementatio n details. These
1051 // details cannot change because Lollipop is long since released. 1082 // details cannot change because Lollipop is long since released.
1052 // NetIDs are only 16-bit so use parseInt. This function returns a l ong because 1083 // NetIDs are only 16-bit so use parseInt. This function returns a l ong because
1053 // getNetworkHandle() returns a long. 1084 // getNetworkHandle() returns a long.
1054 return Integer.parseInt(network.toString()); 1085 return Integer.parseInt(network.toString());
1055 } 1086 }
1056 } 1087 }
1057 } 1088 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698