|
|
@@ -1,30 +1,19 @@
|
|
|
/**
|
|
|
- * LocalNotification.java
|
|
|
* Cordova LocalNotification Plugin
|
|
|
*
|
|
|
- * Created by Sebastian Katzer (github.com/katzer) on 31/08/2013.
|
|
|
+ * Created by Sebastian Katzer (github.com/katzer).
|
|
|
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
|
|
- * GPL v2 licensed
|
|
|
+ * LGPL v2.1 licensed
|
|
|
*/
|
|
|
|
|
|
package de.appplant.cordova.plugin.localnotification;
|
|
|
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
-
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
-import android.content.Intent;
|
|
|
import android.content.Context;
|
|
|
-import android.app.Activity;
|
|
|
-import android.app.AlarmManager;
|
|
|
-import android.app.PendingIntent;
|
|
|
-import android.content.SharedPreferences;
|
|
|
-import android.content.SharedPreferences.Editor;
|
|
|
|
|
|
-import org.apache.cordova.CordovaInterface;
|
|
|
import org.apache.cordova.CordovaPlugin;
|
|
|
import org.apache.cordova.CallbackContext;
|
|
|
import org.apache.cordova.CordovaWebView;
|
|
|
@@ -39,15 +28,24 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
|
|
|
public static final String PLUGIN_NAME = "LocalNotification";
|
|
|
|
|
|
- public static CordovaInterface cordova = null;
|
|
|
public static CordovaWebView webView = null;
|
|
|
+ public static Context context = null;
|
|
|
|
|
|
@Override
|
|
|
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
|
rememberCordovaVarsForStaticUse();
|
|
|
|
|
|
if (action.equalsIgnoreCase("add")) {
|
|
|
- addWithoutBlocking(args);
|
|
|
+ JSONObject arguments = args.optJSONObject(0);
|
|
|
+ final Options options = new Options(context).parse(arguments);
|
|
|
+
|
|
|
+ persist(options.getId(), args);
|
|
|
+
|
|
|
+ cordova.getThreadPool().execute( new Runnable() {
|
|
|
+ public void run() {
|
|
|
+ add(options);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
@@ -72,40 +70,16 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- private static void addWithoutBlocking (final JSONArray args) {
|
|
|
- cordova.getThreadPool().execute(new Runnable() {
|
|
|
- public void run() {
|
|
|
- JSONObject arguments = args.optJSONObject(0);
|
|
|
- Activity activity = cordova.getActivity();
|
|
|
- LocalNotificationOptions options = new LocalNotificationOptions(activity).parse(arguments);
|
|
|
-
|
|
|
- persist(options.getId(), args);
|
|
|
- add(options);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * Set an alarm
|
|
|
+ * Set an alarm.
|
|
|
*
|
|
|
* @param options
|
|
|
* The options that can be specified per alarm.
|
|
|
*/
|
|
|
- public static void add (LocalNotificationOptions options) {
|
|
|
- long triggerTime = options.getDate();
|
|
|
- Intent intent = new Intent(cordova.getActivity(), LocalNotificationReceiver.class);
|
|
|
-
|
|
|
- intent.setAction("" + options.getId());
|
|
|
- intent.putExtra(LocalNotificationReceiver.OPTIONS, options.getJSONObject().toString());
|
|
|
-
|
|
|
- AlarmManager am = getAlarmManager();
|
|
|
- PendingIntent pi = PendingIntent.getBroadcast(cordova.getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
+ public static void add (Options options) {
|
|
|
+ Helper helper = new Helper(context);
|
|
|
|
|
|
- if (options.getInterval() > 0) {
|
|
|
- am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, options.getInterval(), pi);
|
|
|
- } else {
|
|
|
- am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
|
|
|
- }
|
|
|
+ helper.add(options);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -116,22 +90,9 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* registered using add()
|
|
|
*/
|
|
|
public static void cancel (String notificationId) {
|
|
|
- /*
|
|
|
- * Create an intent that looks similar, to the one that was registered
|
|
|
- * using add. Making sure the notification id in the action is the same.
|
|
|
- * Now we can search for such an intent using the 'getService' method
|
|
|
- * and cancel it.
|
|
|
- */
|
|
|
- Intent intent = new Intent(cordova.getActivity(), LocalNotificationReceiver.class);
|
|
|
-
|
|
|
- intent.setAction("" + notificationId);
|
|
|
-
|
|
|
- PendingIntent pi = PendingIntent.getBroadcast(cordova.getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
- AlarmManager am = getAlarmManager();
|
|
|
-
|
|
|
- try {
|
|
|
- am.cancel(pi);
|
|
|
- } catch (Exception e) {}
|
|
|
+ Helper helper = new Helper(context);
|
|
|
+
|
|
|
+ helper.cancel(notificationId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -143,13 +104,9 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* by one.
|
|
|
*/
|
|
|
public static void cancelAll() {
|
|
|
- SharedPreferences settings = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
|
|
|
- Map<String, ?> alarms = settings.getAll();
|
|
|
- Set<String> alarmIds = alarms.keySet();
|
|
|
+ Helper helper = new Helper(context);
|
|
|
|
|
|
- for (String alarmId : alarmIds) {
|
|
|
- cancel(alarmId);
|
|
|
- }
|
|
|
+ helper.cancelAll();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -161,12 +118,9 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* The assumption is that parse has been called already.
|
|
|
*/
|
|
|
public static void persist (String alarmId, JSONArray args) {
|
|
|
- Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
|
|
|
+ Helper helper = new Helper(context);
|
|
|
|
|
|
- if (alarmId != null) {
|
|
|
- editor.putString(alarmId, args.toString());
|
|
|
- editor.commit();
|
|
|
- }
|
|
|
+ helper.persist(alarmId, args);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -176,33 +130,25 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* The Id of the notification that must be removed.
|
|
|
*/
|
|
|
public static void unpersist (String alarmId) {
|
|
|
- Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
|
|
|
+ Helper helper = new Helper(context);
|
|
|
|
|
|
- if (alarmId != null) {
|
|
|
- editor.remove(alarmId);
|
|
|
- editor.commit();
|
|
|
- }
|
|
|
+ helper.unpersist(alarmId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Clear all alarms from the Android shared Preferences.
|
|
|
*/
|
|
|
public static void unpersistAll () {
|
|
|
- Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
|
|
|
-
|
|
|
- editor.clear();
|
|
|
- editor.commit();
|
|
|
- }
|
|
|
+ Helper helper = new Helper(context);
|
|
|
|
|
|
- private static AlarmManager getAlarmManager () {
|
|
|
- return (AlarmManager) cordova.getActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+ helper.unpersistAll();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Save required Cordova specific variables for later use.
|
|
|
*/
|
|
|
private void rememberCordovaVarsForStaticUse () {
|
|
|
- LocalNotification.cordova = super.cordova;
|
|
|
LocalNotification.webView = super.webView;
|
|
|
+ LocalNotification.context = super.cordova.getActivity();
|
|
|
}
|
|
|
}
|