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

Unified Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java

Issue 816543004: Update from https://crrev.com/308996 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years 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 side-by-side diff with in-line comments
Download patch
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..aea674adb4d5be5e83f5b1b49ab8451797695562 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,8 +170,13 @@ 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);
+ mIntentFilter =
+ new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasWifiPermission());
+
if (alwaysWatchForChanges) {
registerReceiver();
} else {
@@ -211,9 +222,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 +274,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 +284,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 +339,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 +380,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);
}
}
}

Powered by Google App Engine
This is Rietveld 408576698