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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java

Issue 2648683002: Workaround NetworkConnectionManager race condition (Closed)
Patch Set: Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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());
+ }
+ }
+
+ 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.
+ 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698