Просмотр исходного кода

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

Sebastián Katzer 8 лет назад
Родитель
Сommit
6c919c8d40
2 измененных файлов с 28 добавлено и 24 удалено
  1. 2 2
      src/android/ClickReceiver.java
  2. 26 22
      src/android/LocalNotification.java

+ 2 - 2
src/android/ClickReceiver.java

@@ -48,8 +48,8 @@ public class ClickReceiver extends AbstractClickReceiver {
      */
     @Override
     public void onClick(Notification notification, Bundle bundle) {
-        String action    = getAction();
-        JSONObject data  = new JSONObject();
+        String action   = getAction();
+        JSONObject data = new JSONObject();
 
         setTextInput(action, data);
         launchAppIf();

+ 26 - 22
src/android/LocalNotification.java

@@ -23,7 +23,10 @@ package de.appplant.cordova.plugin.localnotification;
 
 import android.annotation.SuppressLint;
 import android.app.Activity;
+import android.app.KeyguardManager;
+import android.content.Context;
 import android.util.Pair;
+import android.view.View;
 
 import org.apache.cordova.CallbackContext;
 import org.apache.cordova.CordovaInterface;
@@ -34,6 +37,7 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -60,9 +64,6 @@ public class LocalNotification extends CordovaPlugin {
     // Indicates if the device is ready (to receive events)
     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
     private static ArrayList<String> eventQueue = new ArrayList<String>();
 
@@ -79,27 +80,14 @@ public class LocalNotification extends CordovaPlugin {
         LocalNotification.webView = super.webView;
     }
 
-    /**
-     * 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.
      *
-     * @param multitasking
-     *      Flag indicating if multitasking is turned on for app
+     * @param multitasking Flag indicating if multitasking is turned on for app.
      */
     @Override
-    public void onResume(boolean multitasking) {
+    public void onResume (boolean multitasking) {
         super.onResume(multitasking);
-        isInBackground = false;
         deviceready();
     }
 
@@ -108,8 +96,7 @@ public class LocalNotification extends CordovaPlugin {
      */
     @Override
     public void onDestroy() {
-        deviceready    = false;
-        isInBackground = true;
+        deviceready = false;
     }
 
     /**
@@ -487,7 +474,6 @@ public class LocalNotification extends CordovaPlugin {
      * Call all pending callbacks after the deviceready event has been fired.
      */
     private static synchronized void deviceready () {
-        isInBackground = false;
         deviceready = true;
 
         for (String js : eventQueue) {
@@ -528,7 +514,7 @@ public class LocalNotification extends CordovaPlugin {
 
         try {
             data.put("event", event);
-            data.put("foreground", !isInBackground);
+            data.put("foreground", isInForeground());
             data.put("queued", !deviceready);
 
             if (toast != null) {
@@ -573,6 +559,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.
      *