diff options
4 files changed, 107 insertions, 47 deletions
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java index 4a975e5..9521231 100644 --- a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java +++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java @@ -20,9 +20,18 @@ import org.chromium.base.NativeClassQualifiedName; */ @JNINamespace("net") public class NetworkChangeNotifier { + // These constants must always match the ones in network_change_notifier.h. + public static final int CONNECTION_UNKNOWN = 0; + public static final int CONNECTION_ETHERNET = 1; + public static final int CONNECTION_WIFI = 2; + public static final int CONNECTION_2G = 3; + public static final int CONNECTION_3G = 4; + public static final int CONNECTION_4G = 5; + public static final int CONNECTION_NONE = 6; + private final Context mContext; private int mNativeChangeNotifier; - private boolean mIsConnected; + private int mConnectionType; private NetworkChangeNotifierAutoDetect mAutoDetector; private static NetworkChangeNotifier sInstance; @@ -32,7 +41,7 @@ public class NetworkChangeNotifier { private NetworkChangeNotifier(Context context, int nativeChangeNotifier) { mContext = context; mNativeChangeNotifier = nativeChangeNotifier; - mIsConnected = true; + mConnectionType = CONNECTION_UNKNOWN; sInstance = this; } @@ -78,8 +87,9 @@ public class NetworkChangeNotifier { } private void forceConnectivityStateInternal(boolean forceOnline) { - if (mIsConnected != forceOnline) { - mIsConnected = forceOnline; + boolean connectionCurrentlyExists = mConnectionType != CONNECTION_NONE; + if (connectionCurrentlyExists != forceOnline) { + mConnectionType = forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE; notifyNativeObservers(); } } @@ -91,11 +101,11 @@ public class NetworkChangeNotifier { } @CalledByNative - private boolean isConnected() { + private int connectionType() { if (mAutoDetector != null) { - return mAutoDetector.isConnected(); + return mAutoDetector.connectionType(); } - return mIsConnected; + return mConnectionType; } @CalledByNative diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java index 25b25a8..c9e1016 100644 --- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java +++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java @@ -10,6 +10,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.telephony.TelephonyManager; import android.util.Log; import org.chromium.base.ActivityStatus; @@ -31,12 +32,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver private final Context mContext; private boolean mRegistered; - private boolean mIsConnected; + private int mConnectionType; public NetworkChangeNotifierAutoDetect(NetworkChangeNotifier owner, Context context) { mOwner = owner; mContext = context; - mIsConnected = checkIfConnected(mContext); + mConnectionType = currentConnectionType(context); ActivityStatus status = ActivityStatus.getInstance(); if (!status.isPaused()) { @@ -45,25 +46,14 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver status.registerListener(this); } - public boolean isConnected() { - return mIsConnected; + public int connectionType() { + return mConnectionType; } public void destroy() { unregisterReceiver(); } - private boolean checkIfConnected(Context context) { - ConnectivityManager manager = (ConnectivityManager) - context.getSystemService(Context.CONNECTIVITY_SERVICE); - for (NetworkInfo info: manager.getAllNetworkInfo()) { - if (info.isConnected()) { - return true; - } - } - return false; - } - /** * Register a BroadcastReceiver in the given context. */ @@ -84,22 +74,63 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver } } + private int currentConnectionType(Context context) { + // Track exactly what type of connection we have. + ConnectivityManager connectivityManager = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + if (activeNetworkInfo == null) { + return NetworkChangeNotifier.CONNECTION_NONE; + } + + switch (activeNetworkInfo.getType()) { + case ConnectivityManager.TYPE_ETHERNET: + return NetworkChangeNotifier.CONNECTION_ETHERNET; + case ConnectivityManager.TYPE_WIFI: + return NetworkChangeNotifier.CONNECTION_WIFI; + case ConnectivityManager.TYPE_WIMAX: + return NetworkChangeNotifier.CONNECTION_4G; + case ConnectivityManager.TYPE_MOBILE: + // Use information from TelephonyManager to classify the connection. + switch (activeNetworkInfo.getSubtype()) { + case TelephonyManager.NETWORK_TYPE_GPRS: + case TelephonyManager.NETWORK_TYPE_EDGE: + case TelephonyManager.NETWORK_TYPE_CDMA: + case TelephonyManager.NETWORK_TYPE_1xRTT: + case TelephonyManager.NETWORK_TYPE_IDEN: + return NetworkChangeNotifier.CONNECTION_2G; + case TelephonyManager.NETWORK_TYPE_UMTS: + case TelephonyManager.NETWORK_TYPE_EVDO_0: + case TelephonyManager.NETWORK_TYPE_EVDO_A: + case TelephonyManager.NETWORK_TYPE_HSDPA: + case TelephonyManager.NETWORK_TYPE_HSUPA: + case TelephonyManager.NETWORK_TYPE_HSPA: + case TelephonyManager.NETWORK_TYPE_EVDO_B: + case TelephonyManager.NETWORK_TYPE_EHRPD: + case TelephonyManager.NETWORK_TYPE_HSPAP: + return NetworkChangeNotifier.CONNECTION_3G; + case TelephonyManager.NETWORK_TYPE_LTE: + return NetworkChangeNotifier.CONNECTION_4G; + default: + return NetworkChangeNotifier.CONNECTION_UNKNOWN; + } + default: + return NetworkChangeNotifier.CONNECTION_UNKNOWN; + } + } + // BroadcastReceiver @Override public void onReceive(Context context, Intent intent) { - if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) { - if (mIsConnected) { - mIsConnected = false; - Log.d(TAG, "Network connectivity changed, no connectivity."); - mOwner.notifyNativeObservers(); - } - } else { - boolean isConnected = checkIfConnected(context); - if (isConnected != mIsConnected) { - mIsConnected = isConnected; - Log.d(TAG, "Network connectivity changed, status is: " + isConnected); - mOwner.notifyNativeObservers(); - } + boolean noConnection = + intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); + int newConnectionType = noConnection ? + NetworkChangeNotifier.CONNECTION_NONE : currentConnectionType(context); + + if (newConnectionType != mConnectionType) { + mConnectionType = newConnectionType; + Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType); + mOwner.notifyNativeObservers(); } } diff --git a/net/android/network_change_notifier_android.cc b/net/android/network_change_notifier_android.cc index 8e98bf5..473fa64 100644 --- a/net/android/network_change_notifier_android.cc +++ b/net/android/network_change_notifier_android.cc @@ -37,11 +37,30 @@ void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) { net::NetworkChangeNotifier::ConnectionType NetworkChangeNotifier::GetCurrentConnectionType() const { JNIEnv* env = base::android::AttachCurrentThread(); - // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. - return Java_NetworkChangeNotifier_isConnected( - env, java_network_change_notifier_.obj()) ? - net::NetworkChangeNotifier::CONNECTION_UNKNOWN : - net::NetworkChangeNotifier::CONNECTION_NONE; + + // Pull the connection type from the Java-side then convert it to a + // native-side NetworkChangeNotifier::ConnectionType. + jint connection_type = Java_NetworkChangeNotifier_connectionType( + env, java_network_change_notifier_.obj()); + switch (connection_type) { + case CONNECTION_UNKNOWN: + return CONNECTION_UNKNOWN; + case CONNECTION_ETHERNET: + return CONNECTION_ETHERNET; + case CONNECTION_WIFI: + return CONNECTION_WIFI; + case CONNECTION_2G: + return CONNECTION_2G; + case CONNECTION_3G: + return CONNECTION_3G; + case CONNECTION_4G: + return CONNECTION_4G; + case CONNECTION_NONE: + return CONNECTION_NONE; + default: + NOTREACHED() << "Unknown connection type received: " << connection_type; + return CONNECTION_NONE; + } } // static diff --git a/net/base/network_change_notifier.h b/net/base/network_change_notifier.h index a23841f..5a407218 100644 --- a/net/base/network_change_notifier.h +++ b/net/base/network_change_notifier.h @@ -33,13 +33,13 @@ class NET_EXPORT NetworkChangeNotifier { // Using the terminology of the Network Information API: // http://www.w3.org/TR/netinfo-api. enum ConnectionType { - CONNECTION_UNKNOWN, // A connection exists, but its type is unknown. - CONNECTION_ETHERNET, - CONNECTION_WIFI, - CONNECTION_2G, - CONNECTION_3G, - CONNECTION_4G, - CONNECTION_NONE // No connection. + CONNECTION_UNKNOWN = 0, // A connection exists, but its type is unknown. + CONNECTION_ETHERNET = 1, + CONNECTION_WIFI = 2, + CONNECTION_2G = 3, + CONNECTION_3G = 4, + CONNECTION_4G = 5, + CONNECTION_NONE = 6 // No connection. }; class NET_EXPORT IPAddressObserver { |