diff --git a/embedding/android/GeckoAppShell.java b/embedding/android/GeckoAppShell.java index 60650758da3..5aaa7e4cc1c 100644 --- a/embedding/android/GeckoAppShell.java +++ b/embedding/android/GeckoAppShell.java @@ -1780,4 +1780,12 @@ public class GeckoAppShell public static double[] getCurrentNetworkInformation() { return GeckoNetworkManager.getInstance().getCurrentInformation(); } + + public static void enableNetworkNotifications() { + GeckoNetworkManager.getInstance().enableNotifications(); + } + + public static void disableNetworkNotifications() { + GeckoNetworkManager.getInstance().disableNotifications(); + } } diff --git a/embedding/android/GeckoNetworkManager.java b/embedding/android/GeckoNetworkManager.java index 8e4883cee4b..1d16b416bf5 100644 --- a/embedding/android/GeckoNetworkManager.java +++ b/embedding/android/GeckoNetworkManager.java @@ -132,6 +132,11 @@ public class GeckoNetworkManager private NetworkType mNetworkType = NetworkType.NETWORK_NONE; private IntentFilter mNetworkFilter = new IntentFilter(); + // Whether the manager should be listening to Network Information changes. + private boolean mShouldBeListening = false; + // Whether the manager should notify Gecko that a change in Network + // Information happened. + private boolean mShouldNotify = false; public static GeckoNetworkManager getInstance() { return sInstance; @@ -149,12 +154,27 @@ public class GeckoNetworkManager } public void start() { + mShouldBeListening = true; updateNetworkType(); + if (mShouldNotify) { + startListening(); + } + } + + private void startListening() { GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter); } public void stop() { + mShouldBeListening = false; + + if (mShouldNotify) { + stopListening(); + } + } + + private void stopListening() { GeckoApp.mAppContext.unregisterReceiver(sInstance); } @@ -162,7 +182,7 @@ public class GeckoNetworkManager NetworkType previousNetworkType = mNetworkType; mNetworkType = getNetworkType(); - if (mNetworkType == previousNetworkType) { + if (mNetworkType == previousNetworkType || !mShouldNotify) { return; } @@ -175,6 +195,25 @@ public class GeckoNetworkManager isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 }; } + public void enableNotifications() { + // We set mShouldNotify *after* calling updateNetworkType() to make sure we + // don't notify an eventual change in mNetworkType. + updateNetworkType(); + mShouldNotify = true; + + if (mShouldBeListening) { + startListening(); + } + } + + public void disableNotifications() { + mShouldNotify = false; + + if (mShouldBeListening) { + stopListening(); + } + } + private static NetworkType getNetworkType() { ConnectivityManager cm = (ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/hal/android/AndroidHal.cpp b/hal/android/AndroidHal.cpp index b232ccd5771..dc86465e05a 100644 --- a/hal/android/AndroidHal.cpp +++ b/hal/android/AndroidHal.cpp @@ -142,11 +142,25 @@ SetScreenBrightness(double brightness) void EnableNetworkNotifications() -{} +{ + AndroidBridge* bridge = AndroidBridge::Bridge(); + if (!bridge) { + return; + } + + bridge->EnableNetworkNotifications(); +} void DisableNetworkNotifications() -{} +{ + AndroidBridge* bridge = AndroidBridge::Bridge(); + if (!bridge) { + return; + } + + bridge->DisableNetworkNotifications(); +} void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo) diff --git a/mobile/android/base/GeckoAppShell.java b/mobile/android/base/GeckoAppShell.java index 3111af2c2e8..541d6b0c05b 100644 --- a/mobile/android/base/GeckoAppShell.java +++ b/mobile/android/base/GeckoAppShell.java @@ -1751,4 +1751,12 @@ public class GeckoAppShell public static double[] getCurrentNetworkInformation() { return GeckoNetworkManager.getInstance().getCurrentInformation(); } + + public static void enableNetworkNotifications() { + GeckoNetworkManager.getInstance().enableNotifications(); + } + + public static void disableNetworkNotifications() { + GeckoNetworkManager.getInstance().disableNotifications(); + } } diff --git a/mobile/android/base/GeckoNetworkManager.java b/mobile/android/base/GeckoNetworkManager.java index 8e4883cee4b..1d16b416bf5 100644 --- a/mobile/android/base/GeckoNetworkManager.java +++ b/mobile/android/base/GeckoNetworkManager.java @@ -132,6 +132,11 @@ public class GeckoNetworkManager private NetworkType mNetworkType = NetworkType.NETWORK_NONE; private IntentFilter mNetworkFilter = new IntentFilter(); + // Whether the manager should be listening to Network Information changes. + private boolean mShouldBeListening = false; + // Whether the manager should notify Gecko that a change in Network + // Information happened. + private boolean mShouldNotify = false; public static GeckoNetworkManager getInstance() { return sInstance; @@ -149,12 +154,27 @@ public class GeckoNetworkManager } public void start() { + mShouldBeListening = true; updateNetworkType(); + if (mShouldNotify) { + startListening(); + } + } + + private void startListening() { GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter); } public void stop() { + mShouldBeListening = false; + + if (mShouldNotify) { + stopListening(); + } + } + + private void stopListening() { GeckoApp.mAppContext.unregisterReceiver(sInstance); } @@ -162,7 +182,7 @@ public class GeckoNetworkManager NetworkType previousNetworkType = mNetworkType; mNetworkType = getNetworkType(); - if (mNetworkType == previousNetworkType) { + if (mNetworkType == previousNetworkType || !mShouldNotify) { return; } @@ -175,6 +195,25 @@ public class GeckoNetworkManager isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 }; } + public void enableNotifications() { + // We set mShouldNotify *after* calling updateNetworkType() to make sure we + // don't notify an eventual change in mNetworkType. + updateNetworkType(); + mShouldNotify = true; + + if (mShouldBeListening) { + startListening(); + } + } + + public void disableNotifications() { + mShouldNotify = false; + + if (mShouldBeListening) { + stopListening(); + } + } + private static NetworkType getNetworkType() { ConnectivityManager cm = (ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/widget/android/AndroidBridge.cpp b/widget/android/AndroidBridge.cpp index b7a442d32bf..733ac7de8a1 100644 --- a/widget/android/AndroidBridge.cpp +++ b/widget/android/AndroidBridge.cpp @@ -177,6 +177,8 @@ AndroidBridge::Init(JNIEnv *jEnv, jClearMessageList = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "clearMessageList", "(I)V"); jGetCurrentNetworkInformation = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getCurrentNetworkInformation", "()[D"); + jEnableNetworkNotifications = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "enableNetworkNotifications", "()V"); + jDisableNetworkNotifications = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "disableNetworkNotifications", "()V"); jEGLContextClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGLContext")); jEGL10Class = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGL10")); @@ -1454,6 +1456,20 @@ AndroidBridge::GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInf mJNIEnv->ReleaseDoubleArrayElements(arr, info, 0); } +void +AndroidBridge::EnableNetworkNotifications() +{ + ALOG_BRIDGE("AndroidBridge::EnableNetworkNotifications"); + mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jEnableNetworkNotifications); +} + +void +AndroidBridge::DisableNetworkNotifications() +{ + ALOG_BRIDGE("AndroidBridge::DisableNetworkNotifications"); + mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jDisableNetworkNotifications); +} + void * AndroidBridge::LockBitmap(jobject bitmap) { diff --git a/widget/android/AndroidBridge.h b/widget/android/AndroidBridge.h index 1c7afa07826..02d29d3bf0e 100644 --- a/widget/android/AndroidBridge.h +++ b/widget/android/AndroidBridge.h @@ -353,6 +353,8 @@ public: bool IsTablet(); void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo); + void EnableNetworkNotifications(); + void DisableNetworkNotifications(); protected: static AndroidBridge *sBridge; @@ -449,6 +451,8 @@ protected: jmethodID jClearMessageList; jmethodID jGetCurrentNetworkInformation; + jmethodID jEnableNetworkNotifications; + jmethodID jDisableNetworkNotifications; // stuff we need for CallEglCreateWindowSurface jclass jEGLSurfaceImplClass;