Commit 5d6a0c79 by Sebastián Katzer

Refactored LocalNotification class

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