Bug 713687 - Part 4 - Network API Android backend: use enable/disable notifications to prevent listening when not needed. r=dougt

This commit is contained in:
Mounir Lamouri 2012-01-16 15:01:07 +01:00
parent f8190ab1af
commit 5c5c80c069
7 changed files with 132 additions and 4 deletions

View File

@ -1780,4 +1780,12 @@ public class GeckoAppShell
public static double[] getCurrentNetworkInformation() { public static double[] getCurrentNetworkInformation() {
return GeckoNetworkManager.getInstance().getCurrentInformation(); return GeckoNetworkManager.getInstance().getCurrentInformation();
} }
public static void enableNetworkNotifications() {
GeckoNetworkManager.getInstance().enableNotifications();
}
public static void disableNetworkNotifications() {
GeckoNetworkManager.getInstance().disableNotifications();
}
} }

View File

@ -132,6 +132,11 @@ public class GeckoNetworkManager
private NetworkType mNetworkType = NetworkType.NETWORK_NONE; private NetworkType mNetworkType = NetworkType.NETWORK_NONE;
private IntentFilter mNetworkFilter = new IntentFilter(); 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() { public static GeckoNetworkManager getInstance() {
return sInstance; return sInstance;
@ -149,12 +154,27 @@ public class GeckoNetworkManager
} }
public void start() { public void start() {
mShouldBeListening = true;
updateNetworkType(); updateNetworkType();
if (mShouldNotify) {
startListening();
}
}
private void startListening() {
GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter); GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter);
} }
public void stop() { public void stop() {
mShouldBeListening = false;
if (mShouldNotify) {
stopListening();
}
}
private void stopListening() {
GeckoApp.mAppContext.unregisterReceiver(sInstance); GeckoApp.mAppContext.unregisterReceiver(sInstance);
} }
@ -162,7 +182,7 @@ public class GeckoNetworkManager
NetworkType previousNetworkType = mNetworkType; NetworkType previousNetworkType = mNetworkType;
mNetworkType = getNetworkType(); mNetworkType = getNetworkType();
if (mNetworkType == previousNetworkType) { if (mNetworkType == previousNetworkType || !mShouldNotify) {
return; return;
} }
@ -175,6 +195,25 @@ public class GeckoNetworkManager
isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 }; 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() { private static NetworkType getNetworkType() {
ConnectivityManager cm = ConnectivityManager cm =
(ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);

View File

@ -142,11 +142,25 @@ SetScreenBrightness(double brightness)
void void
EnableNetworkNotifications() EnableNetworkNotifications()
{} {
AndroidBridge* bridge = AndroidBridge::Bridge();
if (!bridge) {
return;
}
bridge->EnableNetworkNotifications();
}
void void
DisableNetworkNotifications() DisableNetworkNotifications()
{} {
AndroidBridge* bridge = AndroidBridge::Bridge();
if (!bridge) {
return;
}
bridge->DisableNetworkNotifications();
}
void void
GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo) GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo)

View File

@ -1751,4 +1751,12 @@ public class GeckoAppShell
public static double[] getCurrentNetworkInformation() { public static double[] getCurrentNetworkInformation() {
return GeckoNetworkManager.getInstance().getCurrentInformation(); return GeckoNetworkManager.getInstance().getCurrentInformation();
} }
public static void enableNetworkNotifications() {
GeckoNetworkManager.getInstance().enableNotifications();
}
public static void disableNetworkNotifications() {
GeckoNetworkManager.getInstance().disableNotifications();
}
} }

View File

@ -132,6 +132,11 @@ public class GeckoNetworkManager
private NetworkType mNetworkType = NetworkType.NETWORK_NONE; private NetworkType mNetworkType = NetworkType.NETWORK_NONE;
private IntentFilter mNetworkFilter = new IntentFilter(); 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() { public static GeckoNetworkManager getInstance() {
return sInstance; return sInstance;
@ -149,12 +154,27 @@ public class GeckoNetworkManager
} }
public void start() { public void start() {
mShouldBeListening = true;
updateNetworkType(); updateNetworkType();
if (mShouldNotify) {
startListening();
}
}
private void startListening() {
GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter); GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter);
} }
public void stop() { public void stop() {
mShouldBeListening = false;
if (mShouldNotify) {
stopListening();
}
}
private void stopListening() {
GeckoApp.mAppContext.unregisterReceiver(sInstance); GeckoApp.mAppContext.unregisterReceiver(sInstance);
} }
@ -162,7 +182,7 @@ public class GeckoNetworkManager
NetworkType previousNetworkType = mNetworkType; NetworkType previousNetworkType = mNetworkType;
mNetworkType = getNetworkType(); mNetworkType = getNetworkType();
if (mNetworkType == previousNetworkType) { if (mNetworkType == previousNetworkType || !mShouldNotify) {
return; return;
} }
@ -175,6 +195,25 @@ public class GeckoNetworkManager
isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 }; 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() { private static NetworkType getNetworkType() {
ConnectivityManager cm = ConnectivityManager cm =
(ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);

View File

@ -177,6 +177,8 @@ AndroidBridge::Init(JNIEnv *jEnv,
jClearMessageList = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "clearMessageList", "(I)V"); jClearMessageList = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "clearMessageList", "(I)V");
jGetCurrentNetworkInformation = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getCurrentNetworkInformation", "()[D"); 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")); jEGLContextClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGLContext"));
jEGL10Class = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGL10")); 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); 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 * void *
AndroidBridge::LockBitmap(jobject bitmap) AndroidBridge::LockBitmap(jobject bitmap)
{ {

View File

@ -353,6 +353,8 @@ public:
bool IsTablet(); bool IsTablet();
void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo); void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo);
void EnableNetworkNotifications();
void DisableNetworkNotifications();
protected: protected:
static AndroidBridge *sBridge; static AndroidBridge *sBridge;
@ -449,6 +451,8 @@ protected:
jmethodID jClearMessageList; jmethodID jClearMessageList;
jmethodID jGetCurrentNetworkInformation; jmethodID jGetCurrentNetworkInformation;
jmethodID jEnableNetworkNotifications;
jmethodID jDisableNetworkNotifications;
// stuff we need for CallEglCreateWindowSurface // stuff we need for CallEglCreateWindowSurface
jclass jEGLSurfaceImplClass; jclass jEGLSurfaceImplClass;