浏览代码

GET_TASK permission not needed anymore for Android (solves #123)

Sebastián Katzer 11 年之前
父节点
当前提交
8950fdeba0
共有 4 个文件被更改,包括 31 次插入15 次删除
  1. 1 0
      CHANGELOG.md
  2. 0 1
      plugin.xml
  3. 16 14
      src/android/LocalNotification.java
  4. 14 0
      www/local-notification.js

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@
 - [enhancement:] Support for bigview style notifications for Android devices.
 - [bugfix:] Sound didnt play properly on iOS/Android.
 - [bugfix:] click event on iOS wasn't fired if app was not running.
+- [enhancement:] GET_TASK permission not needed anymore for Android.
 
 #### Version 0.7.2 (09.02.2014)
 - [enhancement:] Avoid blocking the main thread (on Android) **(dpogue)**.

+ 0 - 1
plugin.xml

@@ -74,7 +74,6 @@
         </config-file>
 
         <config-file target="AndroidManifest.xml" parent="/manifest">
-            <uses-permission android:name="android.permission.GET_TASKS" />
             <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
         </config-file>
 

+ 16 - 14
src/android/LocalNotification.java

@@ -34,7 +34,6 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import android.app.ActivityManager;
 import android.app.AlarmManager;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -56,7 +55,7 @@ public class LocalNotification extends CordovaPlugin {
     private   static CordovaWebView webView = null;
     private   static Boolean deviceready = false;
     protected static Context context = null;
-
+    protected static Boolean isInBackground = true;
     private   static ArrayList<String> eventQueue = new ArrayList<String>();
 
     @Override
@@ -131,6 +130,18 @@ public class LocalNotification extends CordovaPlugin {
             return true;
         }
 
+        if (action.equalsIgnoreCase("pause")) {
+            isInBackground = true;
+
+            return true;
+        }
+
+        if (action.equalsIgnoreCase("resume")) {
+            isInBackground = false;
+
+            return true;
+        }
+
         // Returning false results in a "MethodNotFound" error.
         return false;
     }
@@ -306,10 +317,12 @@ public class LocalNotification extends CordovaPlugin {
      * @param {String} json  A custom (JSON) string
      */
     public static void fireEvent (String event, String id, String json) {
-        String state  = isInBackground() ? "background" : "foreground";
+        String state  = isInBackground ? "background" : "foreground";
         String params = "\"" + id + "\",\"" + state + "\",\\'" + JSONObject.quote(json) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
         String js     = "setTimeout('plugin.notification.local.on" + event + "(" + params + ")',0)";
 
+        // webview may available, but callbacks needs to be executed
+        // after deviceready
         if (deviceready == false) {
             eventQueue.add(js);
         } else {
@@ -346,15 +359,4 @@ public class LocalNotification extends CordovaPlugin {
     protected static NotificationManager getNotificationManager () {
         return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
     }
-
-    /**
-     * Specifies whether the App is running in the background
-     */
-    private static boolean isInBackground () {
-        try {
-            return !context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName());
-        } catch (Exception e) {
-            return true;
-        }
-    }
 }

+ 14 - 0
www/local-notification.js

@@ -216,4 +216,18 @@ channel.deviceready.subscribe( function () {
     cordova.exec(null, null, 'LocalNotification', 'deviceready', []);
 });
 
+channel.onCordovaReady.subscribe( function () {
+    if (device.platform != 'iOS') {
+        channel.onPause.subscribe( function () {
+            cordova.exec(null, null, 'LocalNotification', 'pause', []);
+        });
+
+        channel.onResume.subscribe( function () {
+            cordova.exec(null, null, 'LocalNotification', 'resume', []);
+        });
+
+        cordova.exec(null, null, 'LocalNotification', 'resume', []);
+    }
+});
+
 module.exports = plugin;