Commit ced7c19a by Sebastián Katzer

Reference webView as weak

parent 6c919c8d
...@@ -59,7 +59,7 @@ import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERE ...@@ -59,7 +59,7 @@ import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERE
public class LocalNotification extends CordovaPlugin { public class LocalNotification extends CordovaPlugin {
// Reference to the web view for static access // Reference to the web view for static access
private static CordovaWebView webView = null; private static WeakReference<CordovaWebView> webView = null;
// Indicates if the device is ready (to receive events) // Indicates if the device is ready (to receive events)
private static Boolean deviceready = false; private static Boolean deviceready = false;
...@@ -77,7 +77,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -77,7 +77,7 @@ public class LocalNotification extends CordovaPlugin {
*/ */
@Override @Override
public void initialize (CordovaInterface cordova, CordovaWebView webView) { public void initialize (CordovaInterface cordova, CordovaWebView webView) {
LocalNotification.webView = super.webView; LocalNotification.webView = new WeakReference<CordovaWebView>(webView);
} }
/** /**
...@@ -547,14 +547,16 @@ public class LocalNotification extends CordovaPlugin { ...@@ -547,14 +547,16 @@ public class LocalNotification extends CordovaPlugin {
*/ */
private static synchronized void sendJavascript(final String js) { private static synchronized void sendJavascript(final String js) {
if (!deviceready) { if (!deviceready || webView == null) {
eventQueue.add(js); eventQueue.add(js);
return; return;
} }
((Activity)(webView.getContext())).runOnUiThread(new Runnable() { final CordovaWebView view = webView.get();
((Activity)(view.getContext())).runOnUiThread(new Runnable() {
public void run() { public void run() {
webView.loadUrl("javascript:" + js); view.loadUrl("javascript:" + js);
} }
}); });
} }
...@@ -564,17 +566,19 @@ public class LocalNotification extends CordovaPlugin { ...@@ -564,17 +566,19 @@ public class LocalNotification extends CordovaPlugin {
*/ */
private static boolean isInForeground() { private static boolean isInForeground() {
if (!deviceready) if (!deviceready || webView == null)
return false; return false;
KeyguardManager km = (KeyguardManager) webView.getContext() CordovaWebView view = webView.get();
KeyguardManager km = (KeyguardManager) view.getContext()
.getSystemService(Context.KEYGUARD_SERVICE); .getSystemService(Context.KEYGUARD_SERVICE);
//noinspection SimplifiableIfStatement //noinspection SimplifiableIfStatement
if (km.isKeyguardLocked()) if (km.isKeyguardLocked())
return false; return false;
return webView.getView().getWindowVisibility() == View.VISIBLE; return view.getView().getWindowVisibility() == View.VISIBLE;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment