Commit 6c919c8d by Sebastián Katzer

Fix app state not showing up correctly when tapping on notification [#477]

parent 3eb95003
...@@ -23,7 +23,10 @@ package de.appplant.cordova.plugin.localnotification; ...@@ -23,7 +23,10 @@ package de.appplant.cordova.plugin.localnotification;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.util.Pair; import android.util.Pair;
import android.view.View;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
...@@ -34,6 +37,7 @@ import org.json.JSONArray; ...@@ -34,6 +37,7 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -60,9 +64,6 @@ public class LocalNotification extends CordovaPlugin { ...@@ -60,9 +64,6 @@ public class LocalNotification extends CordovaPlugin {
// 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;
// To inform the user about the state of the app in callbacks
private static Boolean isInBackground = true;
// Queues all events before deviceready // Queues all events before deviceready
private static ArrayList<String> eventQueue = new ArrayList<String>(); private static ArrayList<String> eventQueue = new ArrayList<String>();
...@@ -80,26 +81,13 @@ public class LocalNotification extends CordovaPlugin { ...@@ -80,26 +81,13 @@ public class LocalNotification extends CordovaPlugin {
} }
/** /**
* Called when the system is about to start resuming a previous activity.
*
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
isInBackground = true;
}
/**
* Called when the activity will start interacting with the user. * Called when the activity will start interacting with the user.
* *
* @param multitasking * @param multitasking Flag indicating if multitasking is turned on for app.
* Flag indicating if multitasking is turned on for app
*/ */
@Override @Override
public void onResume(boolean multitasking) { public void onResume (boolean multitasking) {
super.onResume(multitasking); super.onResume(multitasking);
isInBackground = false;
deviceready(); deviceready();
} }
...@@ -109,7 +97,6 @@ public class LocalNotification extends CordovaPlugin { ...@@ -109,7 +97,6 @@ public class LocalNotification extends CordovaPlugin {
@Override @Override
public void onDestroy() { public void onDestroy() {
deviceready = false; deviceready = false;
isInBackground = true;
} }
/** /**
...@@ -487,7 +474,6 @@ public class LocalNotification extends CordovaPlugin { ...@@ -487,7 +474,6 @@ public class LocalNotification extends CordovaPlugin {
* Call all pending callbacks after the deviceready event has been fired. * Call all pending callbacks after the deviceready event has been fired.
*/ */
private static synchronized void deviceready () { private static synchronized void deviceready () {
isInBackground = false;
deviceready = true; deviceready = true;
for (String js : eventQueue) { for (String js : eventQueue) {
...@@ -528,7 +514,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -528,7 +514,7 @@ public class LocalNotification extends CordovaPlugin {
try { try {
data.put("event", event); data.put("event", event);
data.put("foreground", !isInBackground); data.put("foreground", isInForeground());
data.put("queued", !deviceready); data.put("queued", !deviceready);
if (toast != null) { if (toast != null) {
...@@ -574,6 +560,24 @@ public class LocalNotification extends CordovaPlugin { ...@@ -574,6 +560,24 @@ public class LocalNotification extends CordovaPlugin {
} }
/** /**
* If the app is running in foreground.
*/
private static boolean isInForeground() {
if (!deviceready)
return false;
KeyguardManager km = (KeyguardManager) webView.getContext()
.getSystemService(Context.KEYGUARD_SERVICE);
//noinspection SimplifiableIfStatement
if (km.isKeyguardLocked())
return false;
return webView.getView().getWindowVisibility() == View.VISIBLE;
}
/**
* Convert JSON array of integers to List. * Convert JSON array of integers to List.
* *
* @param ary Array of integers. * @param ary Array of integers.
......
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