| Index: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| index 12ae4654a20f6c3caa9622dd9f13eeab478dcfd5..cdd85f63372af335b2ff8732654e3faa006f0285 100644
|
| --- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| +++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| @@ -130,12 +130,15 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| // though the connection type hasn't changed.
|
| return wifiInfo.getLinkSpeed();
|
| }
|
| +
|
| + boolean getHasWifiPermission() {
|
| + return mHasWifiPermission;
|
| + }
|
| }
|
|
|
| private static final String TAG = "NetworkChangeNotifierAutoDetect";
|
| private static final int UNKNOWN_LINK_SPEED = -1;
|
| - private final NetworkConnectivityIntentFilter mIntentFilter =
|
| - new NetworkConnectivityIntentFilter();
|
| + private final NetworkConnectivityIntentFilter mIntentFilter;
|
|
|
| private final Observer mObserver;
|
|
|
| @@ -145,12 +148,15 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| private boolean mRegistered;
|
| private int mConnectionType;
|
| private String mWifiSSID;
|
| + private double mMaxBandwidthMbps;
|
|
|
| /**
|
| - * Observer notified on the UI thread whenever a new connection type was detected.
|
| + * Observer notified on the UI thread whenever a new connection type was detected or max
|
| + * bandwidth is changed.
|
| */
|
| public static interface Observer {
|
| public void onConnectionTypeChanged(int newConnectionType);
|
| + public void onMaxBandwidthChanged(double maxBandwidthMbps);
|
| }
|
|
|
| /**
|
| @@ -164,13 +170,17 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| mContext = context.getApplicationContext();
|
| mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
|
| mWifiManagerDelegate = new WifiManagerDelegate(context);
|
| - mConnectionType = getCurrentConnectionType();
|
| - mWifiSSID = getCurrentWifiSSID();
|
| + final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
|
| + mConnectionType = getCurrentConnectionType(networkState);
|
| + mWifiSSID = getCurrentWifiSSID(networkState);
|
| + mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
|
| if (alwaysWatchForChanges) {
|
| registerReceiver();
|
| } else {
|
| ApplicationStatus.registerApplicationStateListener(this);
|
| }
|
| + mIntentFilter =
|
| + new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasWifiPermission());
|
| }
|
|
|
| /**
|
| @@ -211,9 +221,11 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| }
|
| }
|
|
|
| - public int getCurrentConnectionType() {
|
| - // Track exactly what type of connection we have.
|
| - final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
|
| + public NetworkState getCurrentNetworkState() {
|
| + return mConnectivityManagerDelegate.getNetworkState();
|
| + }
|
| +
|
| + public int getCurrentConnectionType(NetworkState networkState) {
|
| if (!networkState.isConnected()) {
|
| return ConnectionType.CONNECTION_NONE;
|
| }
|
| @@ -261,13 +273,9 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| * derived from the NetInfo v3 specification's mapping from network type to
|
| * max link speed. In cases where more information is available, such as wifi,
|
| * that is used instead. For more on NetInfo, see http://w3c.github.io/netinfo/.
|
| - *
|
| - * TODO(jkarlin): Add a notification of bandwidth change to the NetworkChangeNotifier.
|
| - * Without that the MaxBandwidth value will be stale until the network type or address
|
| - * changes again.
|
| */
|
| - public double getCurrentMaxBandwidthInMbps() {
|
| - if (getCurrentConnectionType() == ConnectionType.CONNECTION_WIFI) {
|
| + public double getCurrentMaxBandwidthInMbps(NetworkState networkState) {
|
| + if (getCurrentConnectionType(networkState) == ConnectionType.CONNECTION_WIFI) {
|
| final int link_speed = mWifiManagerDelegate.getLinkSpeedInMbps();
|
| if (link_speed != UNKNOWN_LINK_SPEED) {
|
| return link_speed;
|
| @@ -275,11 +283,10 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| }
|
|
|
| return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype(
|
| - getCurrentConnectionSubtype());
|
| + getCurrentConnectionSubtype(networkState));
|
| }
|
|
|
| - private int getCurrentConnectionSubtype() {
|
| - final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
|
| + private int getCurrentConnectionSubtype(NetworkState networkState) {
|
| if (!networkState.isConnected()) {
|
| return ConnectionSubtype.SUBTYPE_NONE;
|
| }
|
| @@ -331,34 +338,40 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| }
|
| }
|
|
|
| - private String getCurrentWifiSSID() {
|
| - if (getCurrentConnectionType() != ConnectionType.CONNECTION_WIFI)
|
| - return "";
|
| + private String getCurrentWifiSSID(NetworkState networkState) {
|
| + if (getCurrentConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return "";
|
| return mWifiManagerDelegate.getWifiSSID();
|
| }
|
|
|
| // BroadcastReceiver
|
| @Override
|
| public void onReceive(Context context, Intent intent) {
|
| - connectionTypeChanged();
|
| + final NetworkState networkState = getCurrentNetworkState();
|
| + if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
|
| + connectionTypeChanged(networkState);
|
| + maxBandwidthChanged(networkState);
|
| + } else if (WifiManager.RSSI_CHANGED_ACTION.equals(intent.getAction())) {
|
| + maxBandwidthChanged(networkState);
|
| + }
|
| }
|
|
|
| // ApplicationStatus.ApplicationStateListener
|
| @Override
|
| public void onApplicationStateChange(int newState) {
|
| + final NetworkState networkState = getCurrentNetworkState();
|
| if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
|
| - connectionTypeChanged();
|
| + connectionTypeChanged(networkState);
|
| + maxBandwidthChanged(networkState);
|
| registerReceiver();
|
| } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
|
| unregisterReceiver();
|
| }
|
| }
|
|
|
| - private void connectionTypeChanged() {
|
| - int newConnectionType = getCurrentConnectionType();
|
| - String newWifiSSID = getCurrentWifiSSID();
|
| - if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID))
|
| - return;
|
| + private void connectionTypeChanged(NetworkState networkState) {
|
| + int newConnectionType = getCurrentConnectionType(networkState);
|
| + String newWifiSSID = getCurrentWifiSSID(networkState);
|
| + if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID)) return;
|
|
|
| mConnectionType = newConnectionType;
|
| mWifiSSID = newWifiSSID;
|
| @@ -366,9 +379,17 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
|
| mObserver.onConnectionTypeChanged(newConnectionType);
|
| }
|
|
|
| + private void maxBandwidthChanged(NetworkState networkState) {
|
| + double newMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
|
| + if (newMaxBandwidthMbps == mMaxBandwidthMbps) return;
|
| + mMaxBandwidthMbps = newMaxBandwidthMbps;
|
| + mObserver.onMaxBandwidthChanged(newMaxBandwidthMbps);
|
| + }
|
| +
|
| private static class NetworkConnectivityIntentFilter extends IntentFilter {
|
| - NetworkConnectivityIntentFilter() {
|
| + NetworkConnectivityIntentFilter(boolean monitorRSSI) {
|
| addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
| + if (monitorRSSI) addAction(WifiManager.RSSI_CHANGED_ACTION);
|
| }
|
| }
|
| }
|
|
|