Loading src/android/LocalNotification.java +69 −92 Changes for src/android/LocalNotification.java: 69 added lines, 92 removed lines. Original line number Diff line number Diff line package de.appplant.cordova.plugin; /** * LocalNotification.java * Cordova LocalNotification Plugin * * Created by Sebastian Katzer (github.com/katzer) on 31/08/2013. * Copyright 2013 Sebastian Katzer. All rights reserved. * GPL v2 licensed */ import java.util.Calendar; package de.appplant.cordova.plugin; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; /** * This plugin utilizes the Android AlarmManager in combination with StatusBar * notifications. When a local notification is scheduled the alarm manager takes * care of firing the event. When the event is processed, a notification is put * in the Android status bar. * * @author Daniel van 't Oever */ public class LocalNotification extends Plugin { public class LocalNotification extends CordovaPlugin { public static final String PLUGIN_NAME = "LocalNotification"; Loading @@ -31,36 +37,40 @@ public class LocalNotification extends Plugin { private AlarmHelper alarm = null; @Override public PluginResult execute(String action, JSONArray optionsArr, String callBackId) { alarm = new AlarmHelper(this.ctx); Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action); public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { JSONObject arguments = args.getJSONObject(0); LocalNotificationOptions options = new LocalNotificationOptions(); PluginResult result = null; options.parse(arguments); final AlarmOptions alarmOptions = new AlarmOptions(); alarmOptions.parseOptions(optionsArr); String alarmId = options.getNotificationId(); alarm = new AlarmHelper(cordova.getActivity()); /* * Determine which action of the plugin needs to be invoked */ String alarmId = alarmOptions.getNotificationId(); if (action.equalsIgnoreCase("add")) { final boolean daily = alarmOptions.isRepeatDaily(); final String title = alarmOptions.getAlarmTitle(); final String subTitle = alarmOptions.getAlarmSubTitle(); final String ticker = alarmOptions.getAlarmTicker(); persistAlarm(alarmId, optionsArr); return this.add(daily, title, subTitle, ticker, alarmId, alarmOptions.getCal()); } else if (action.equalsIgnoreCase("cancel")) { unpersistAlarm(alarmId); return this.cancelNotification(alarmId); } else if (action.equalsIgnoreCase("cancelall")) { unpersistAlarmAll(); return this.cancelAllNotifications(); persist(alarmId, args); add(options); return true; } return result; if (action.equalsIgnoreCase("cancel")) { unpersist(alarmId); cancel(alarmId); return true; } if (action.equalsIgnoreCase("cancelall")) { unpersistAll(); cancelAll(); return true; } // Returning false results in a "MethodNotFound" error. return false; } /** Loading @@ -68,32 +78,19 @@ public class LocalNotification extends Plugin { * * @param repeatDaily * If true, the alarm interval will be set to one day. * @param alarmTitle * @param title * The title of the alarm as shown in the Android notification * panel * @param alarmSubTitle * @param subTitle * The subtitle of the alarm * @param alarmId * The unique ID of the notification * @param cal * @param calender * A calendar object that represents the time at which the alarm * should first be started * @return A pluginresult. */ public PluginResult add(boolean repeatDaily, String alarmTitle, String alarmSubTitle, String alarmTicker, String alarmId, Calendar cal) { final long triggerTime = cal.getTimeInMillis(); final String recurring = repeatDaily ? "daily" : "onetime"; Log.d(PLUGIN_NAME, "Adding " + recurring + " notification: '" + alarmTitle + alarmSubTitle + "' with id: " + alarmId + " at timestamp: " + triggerTime); boolean result = alarm.addAlarm(repeatDaily, alarmTitle, alarmSubTitle, alarmTicker, alarmId, cal); if (result) { return new PluginResult(PluginResult.Status.OK); } else { return new PluginResult(PluginResult.Status.ERROR); } public void add(LocalNotificationOptions options) { alarm.add(options); } /** Loading @@ -101,86 +98,66 @@ public class LocalNotification extends Plugin { * * @param notificationId * The original ID of the notification that was used when it was * registered using addNotification() * registered using add() */ public PluginResult cancelNotification(String notificationId) { Log.d(PLUGIN_NAME, "cancelNotification: Canceling event with id: " + notificationId); boolean result = alarm.cancelAlarm(notificationId); if (result) { return new PluginResult(PluginResult.Status.OK); } else { return new PluginResult(PluginResult.Status.ERROR); } public void cancel (String notificationId) { alarm.cancel(notificationId); } /** * Cancel all notifications that were created by this plugin. */ public PluginResult cancelAllNotifications() { Log.d(PLUGIN_NAME, "cancelAllNotifications: cancelling all events for this application"); public void cancelAll() { /* * Android can only unregister a specific alarm. There is no such thing * as cancelAll. Therefore we rely on the Shared Preferences which holds * all our alarms to loop through these alarms and unregister them one * by one. */ final SharedPreferences alarmSettings = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE); final boolean result = alarm.cancelAll(alarmSettings); SharedPreferences settings = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE); if (result) { return new PluginResult(PluginResult.Status.OK); } else { return new PluginResult(PluginResult.Status.ERROR); } alarm.cancelAll(settings); } /** * Persist the information of this alarm to the Android Shared Preferences. * This will allow the application to restore the alarm upon device reboot. * Also this is used by the cancelAllNotifications method. * Also this is used by the cancelAll method. * * @see #cancelAllNotifications() * * @param optionsArr * The assumption is that parseOptions has been called already. * The assumption is that parse has been called already. * * @return true when successfull, otherwise false */ private boolean persistAlarm(String alarmId, JSONArray optionsArr) { final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); alarmSettingsEditor.putString(alarmId, optionsArr.toString()); private void persist (String alarmId, JSONArray optionsArr) { Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); return alarmSettingsEditor.commit(); editor.putString(alarmId, optionsArr.toString()); editor.commit(); } /** * Remove a specific alarm from the Android shared Preferences * Remove a specific alarm from the Android shared Preferences. * * @param alarmId * The Id of the notification that must be removed. * * @return true when successfull, otherwise false */ private boolean unpersistAlarm(String alarmId) { final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); alarmSettingsEditor.remove(alarmId); private void unpersist (String alarmId) { Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); return alarmSettingsEditor.commit(); editor.remove(alarmId); editor.commit(); } /** * Clear all alarms from the Android shared Preferences * * @return true when successfull, otherwise false * Clear all alarms from the Android shared Preferences. */ private boolean unpersistAlarmAll() { final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); alarmSettingsEditor.clear(); private void unpersistAll () { Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); return alarmSettingsEditor.commit(); editor.clear(); editor.commit(); } } No newline at end of file Loading
src/android/LocalNotification.java +69 −92 Changes for src/android/LocalNotification.java: 69 added lines, 92 removed lines. Original line number Diff line number Diff line package de.appplant.cordova.plugin; /** * LocalNotification.java * Cordova LocalNotification Plugin * * Created by Sebastian Katzer (github.com/katzer) on 31/08/2013. * Copyright 2013 Sebastian Katzer. All rights reserved. * GPL v2 licensed */ import java.util.Calendar; package de.appplant.cordova.plugin; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; /** * This plugin utilizes the Android AlarmManager in combination with StatusBar * notifications. When a local notification is scheduled the alarm manager takes * care of firing the event. When the event is processed, a notification is put * in the Android status bar. * * @author Daniel van 't Oever */ public class LocalNotification extends Plugin { public class LocalNotification extends CordovaPlugin { public static final String PLUGIN_NAME = "LocalNotification"; Loading @@ -31,36 +37,40 @@ public class LocalNotification extends Plugin { private AlarmHelper alarm = null; @Override public PluginResult execute(String action, JSONArray optionsArr, String callBackId) { alarm = new AlarmHelper(this.ctx); Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action); public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { JSONObject arguments = args.getJSONObject(0); LocalNotificationOptions options = new LocalNotificationOptions(); PluginResult result = null; options.parse(arguments); final AlarmOptions alarmOptions = new AlarmOptions(); alarmOptions.parseOptions(optionsArr); String alarmId = options.getNotificationId(); alarm = new AlarmHelper(cordova.getActivity()); /* * Determine which action of the plugin needs to be invoked */ String alarmId = alarmOptions.getNotificationId(); if (action.equalsIgnoreCase("add")) { final boolean daily = alarmOptions.isRepeatDaily(); final String title = alarmOptions.getAlarmTitle(); final String subTitle = alarmOptions.getAlarmSubTitle(); final String ticker = alarmOptions.getAlarmTicker(); persistAlarm(alarmId, optionsArr); return this.add(daily, title, subTitle, ticker, alarmId, alarmOptions.getCal()); } else if (action.equalsIgnoreCase("cancel")) { unpersistAlarm(alarmId); return this.cancelNotification(alarmId); } else if (action.equalsIgnoreCase("cancelall")) { unpersistAlarmAll(); return this.cancelAllNotifications(); persist(alarmId, args); add(options); return true; } return result; if (action.equalsIgnoreCase("cancel")) { unpersist(alarmId); cancel(alarmId); return true; } if (action.equalsIgnoreCase("cancelall")) { unpersistAll(); cancelAll(); return true; } // Returning false results in a "MethodNotFound" error. return false; } /** Loading @@ -68,32 +78,19 @@ public class LocalNotification extends Plugin { * * @param repeatDaily * If true, the alarm interval will be set to one day. * @param alarmTitle * @param title * The title of the alarm as shown in the Android notification * panel * @param alarmSubTitle * @param subTitle * The subtitle of the alarm * @param alarmId * The unique ID of the notification * @param cal * @param calender * A calendar object that represents the time at which the alarm * should first be started * @return A pluginresult. */ public PluginResult add(boolean repeatDaily, String alarmTitle, String alarmSubTitle, String alarmTicker, String alarmId, Calendar cal) { final long triggerTime = cal.getTimeInMillis(); final String recurring = repeatDaily ? "daily" : "onetime"; Log.d(PLUGIN_NAME, "Adding " + recurring + " notification: '" + alarmTitle + alarmSubTitle + "' with id: " + alarmId + " at timestamp: " + triggerTime); boolean result = alarm.addAlarm(repeatDaily, alarmTitle, alarmSubTitle, alarmTicker, alarmId, cal); if (result) { return new PluginResult(PluginResult.Status.OK); } else { return new PluginResult(PluginResult.Status.ERROR); } public void add(LocalNotificationOptions options) { alarm.add(options); } /** Loading @@ -101,86 +98,66 @@ public class LocalNotification extends Plugin { * * @param notificationId * The original ID of the notification that was used when it was * registered using addNotification() * registered using add() */ public PluginResult cancelNotification(String notificationId) { Log.d(PLUGIN_NAME, "cancelNotification: Canceling event with id: " + notificationId); boolean result = alarm.cancelAlarm(notificationId); if (result) { return new PluginResult(PluginResult.Status.OK); } else { return new PluginResult(PluginResult.Status.ERROR); } public void cancel (String notificationId) { alarm.cancel(notificationId); } /** * Cancel all notifications that were created by this plugin. */ public PluginResult cancelAllNotifications() { Log.d(PLUGIN_NAME, "cancelAllNotifications: cancelling all events for this application"); public void cancelAll() { /* * Android can only unregister a specific alarm. There is no such thing * as cancelAll. Therefore we rely on the Shared Preferences which holds * all our alarms to loop through these alarms and unregister them one * by one. */ final SharedPreferences alarmSettings = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE); final boolean result = alarm.cancelAll(alarmSettings); SharedPreferences settings = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE); if (result) { return new PluginResult(PluginResult.Status.OK); } else { return new PluginResult(PluginResult.Status.ERROR); } alarm.cancelAll(settings); } /** * Persist the information of this alarm to the Android Shared Preferences. * This will allow the application to restore the alarm upon device reboot. * Also this is used by the cancelAllNotifications method. * Also this is used by the cancelAll method. * * @see #cancelAllNotifications() * * @param optionsArr * The assumption is that parseOptions has been called already. * The assumption is that parse has been called already. * * @return true when successfull, otherwise false */ private boolean persistAlarm(String alarmId, JSONArray optionsArr) { final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); alarmSettingsEditor.putString(alarmId, optionsArr.toString()); private void persist (String alarmId, JSONArray optionsArr) { Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); return alarmSettingsEditor.commit(); editor.putString(alarmId, optionsArr.toString()); editor.commit(); } /** * Remove a specific alarm from the Android shared Preferences * Remove a specific alarm from the Android shared Preferences. * * @param alarmId * The Id of the notification that must be removed. * * @return true when successfull, otherwise false */ private boolean unpersistAlarm(String alarmId) { final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); alarmSettingsEditor.remove(alarmId); private void unpersist (String alarmId) { Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); return alarmSettingsEditor.commit(); editor.remove(alarmId); editor.commit(); } /** * Clear all alarms from the Android shared Preferences * * @return true when successfull, otherwise false * Clear all alarms from the Android shared Preferences. */ private boolean unpersistAlarmAll() { final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); alarmSettingsEditor.clear(); private void unpersistAll () { Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); return alarmSettingsEditor.commit(); editor.clear(); editor.commit(); } } No newline at end of file