Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| index da4b9163a6744c972e380ef9affb974ccee23ff8..2b0c6c0eac50a29133d96c69f59bc71e49ffac56 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| @@ -8,6 +8,8 @@ import android.app.Activity; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.IntentFilter; |
| +import android.net.ConnectivityManager; |
| +import android.net.NetworkInfo; |
| import android.net.Uri; |
| import android.os.AsyncTask; |
| import android.os.BatteryManager; |
| @@ -636,9 +638,41 @@ public class OfflinePageUtils { |
| Intent batteryStatus = context.registerReceiver(null, filter); |
| if (batteryStatus == null) return null; |
| - return new DeviceConditions(isPowerConnected(batteryStatus), |
| - batteryPercentage(batteryStatus), |
| - NetworkChangeNotifier.getInstance().getCurrentConnectionType()); |
| + // Get the connection type from chromium's internal object. |
| + int connectionType = NetworkChangeNotifier.getInstance().getCurrentConnectionType(); |
| + |
| + // Sometimes the NetworkConnectionNotifier lags the actual connection type, especially when |
| + // the GCM NM wakes us from doze state. If we are really connected, report the connection |
| + // type from android. |
| + if (connectionType == ConnectionType.CONNECTION_NONE) { |
| + // Get the connection type from android in case chromium's type is not yet set. |
| + ConnectivityManager cm = |
| + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); |
| + boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); |
| + if (isConnected) { |
| + connectionType = convertAndroidNetworkTypeToConnectionType(activeNetwork.getType()); |
|
dougarnett
2017/01/13 23:13:16
With the convert method now why bother with Networ
Pete Williamson
2017/01/13 23:15:39
My thinking is that if NCN is available, it has be
dougarnett
2017/01/14 00:59:11
Ah, yuck, looked a bit into the multi-version mess
Pete Williamson
2017/01/14 01:08:10
One thing I got from the bug I filed on NCN is tha
|
| + } |
| + } |
| + |
| + return new DeviceConditions( |
| + isPowerConnected(batteryStatus), batteryPercentage(batteryStatus), connectionType); |
| + } |
| + |
| + /** Returns the NCN network type corresponding to the connectivity manager network type */ |
| + protected int convertAndroidNetworkTypeToConnectionType(int connectivityManagerNetworkType) { |
| + if (connectivityManagerNetworkType == ConnectivityManager.TYPE_WIFI) { |
| + return ConnectionType.CONNECTION_WIFI; |
| + } |
| + // for mobile, we don't know if it is 2G, 3G, or 4G, default to worst case of 2G. |
|
dougarnett
2017/01/14 00:59:11
Possible TODO idea - look at exposing mapping util
Pete Williamson
2017/01/14 01:08:10
Noted, but I'd prefer to wait until I checkout the
|
| + if (connectivityManagerNetworkType == ConnectivityManager.TYPE_MOBILE) { |
| + return ConnectionType.CONNECTION_2G; |
| + } |
| + if (connectivityManagerNetworkType == ConnectivityManager.TYPE_BLUETOOTH) { |
| + return ConnectionType.CONNECTION_BLUETOOTH; |
| + } |
| + // Since NetworkConnectivityManager doesn't understand the other types, call them UNKNOWN. |
| + return ConnectionType.CONNECTION_UNKNOWN; |
| } |
| @VisibleForTesting |