diff options
author | dfalcantara@chromium.org <dfalcantara@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-11 00:30:37 +0000 |
---|---|---|
committer | dfalcantara@chromium.org <dfalcantara@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-11 00:30:37 +0000 |
commit | e0535b0393452e6fc8fb1fd71f256bc5f132cb1f (patch) | |
tree | 3e086882153e77eeb2765f4568963c645b1feca6 | |
parent | 0190cc2b997035f81b3d7be1b585a6d241755ab5 (diff) | |
download | chromium_src-e0535b0393452e6fc8fb1fd71f256bc5f132cb1f.zip chromium_src-e0535b0393452e6fc8fb1fd71f256bc5f132cb1f.tar.gz chromium_src-e0535b0393452e6fc8fb1fd71f256bc5f132cb1f.tar.bz2 |
[Android] Improve NetworkChangeNotifier granularity
Original review: https://chromiumcodereview.appspot.com/10915043/
The NetworkChangeNotifier for Android currently cannot identify what type
of connection exists; all it can do is determine whether it's currently got
one. This CL patches it so that we pass this information back from the Java
side, allowing us to properly use NetworkChangeNotifier::ConnectionType.
The connection types (2G, 3G, etc.) are differentiated between using
classifications from Android's TelephonyManager.
BUG=136984
Review URL: https://chromiumcodereview.appspot.com/10913175
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@155881 0039d316-1c4b-4281-b951-d872f2087c98
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 { |