diff --git a/Android/LocalNotification/AlarmReceiver.java b/Android/LocalNotification/AlarmReceiver.java index 1c604b1d..1627fb1c 100644 --- a/Android/LocalNotification/AlarmReceiver.java +++ b/Android/LocalNotification/AlarmReceiver.java @@ -1,7 +1,6 @@ package com.phonegap.plugin.localnotification; import java.util.Calendar; - import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -10,7 +9,6 @@ import android.content.Intent; import android.os.Bundle; import android.util.Log; - /** * The alarm receiver is triggered when a scheduled alarm is fired. This class * reads the information in the intent and displays this information in the @@ -21,67 +19,67 @@ */ public class AlarmReceiver extends BroadcastReceiver { - public static final String TITLE = "ALARM_TITLE"; - public static final String SUBTITLE = "ALARM_SUBTITLE"; - public static final String TICKER_TEXT = "ALARM_TICKER"; - public static final String NOTIFICATION_ID = "NOTIFICATION_ID"; + public static final String TITLE = "ALARM_TITLE"; + public static final String SUBTITLE = "ALARM_SUBTITLE"; + public static final String TICKER_TEXT = "ALARM_TICKER"; + public static final String NOTIFICATION_ID = "NOTIFICATION_ID"; - /* Contains time in 24hour format 'HH:mm' e.g. '04:30' or '18:23' */ - public static final String HOUR_OF_DAY = "HOUR_OF_DAY"; - public static final String MINUTE = "MINUTES"; + /* Contains time in 24hour format 'HH:mm' e.g. '04:30' or '18:23' */ + public static final String HOUR_OF_DAY = "HOUR_OF_DAY"; + public static final String MINUTE = "MINUTES"; - @Override - public void onReceive(Context context, Intent intent) { - Log.d("AlarmReceiver", "AlarmReceiver invoked!"); + @Override + public void onReceive(Context context, Intent intent) { + Log.d("AlarmReceiver", "AlarmReceiver invoked!"); - final Bundle bundle = intent.getExtras(); - final Object systemService = context.getSystemService(Context.NOTIFICATION_SERVICE); + final Bundle bundle = intent.getExtras(); + final Object systemService = context.getSystemService(Context.NOTIFICATION_SERVICE); - // Retrieve notification details from the intent - final String tickerText = bundle.getString(TICKER_TEXT); - final String notificationTitle = bundle.getString(TITLE); - final String notificationSubText = bundle.getString(SUBTITLE); - int notificationId = 0; + // Retrieve notification details from the intent + final String tickerText = bundle.getString(TICKER_TEXT); + final String notificationTitle = bundle.getString(TITLE); + final String notificationSubText = bundle.getString(SUBTITLE); + int notificationId = 0; - try { - notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID)); - } catch (Exception e) { - Log.d("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID)); - } + try { + notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID)); + } catch (Exception e) { + Log.d("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID)); + } - Calendar currentCal = Calendar.getInstance(); - int alarmHour = bundle.getInt(HOUR_OF_DAY); - int alarmMin = bundle.getInt(MINUTE); - int currentHour = currentCal.get(Calendar.HOUR_OF_DAY); - int currentMin = currentCal.get(Calendar.MINUTE); + Calendar currentCal = Calendar.getInstance(); + int alarmHour = bundle.getInt(HOUR_OF_DAY); + int alarmMin = bundle.getInt(MINUTE); + int currentHour = currentCal.get(Calendar.HOUR_OF_DAY); + int currentMin = currentCal.get(Calendar.MINUTE); - if (currentHour != alarmHour && currentMin != alarmMin) { - /* - * If you set a repeating alarm at 11:00 in the morning and it - * should trigger every morning at 08:00 o'clock, it will - * immediately fire. E.g. Android tries to make up for the - * 'forgotten' reminder for that day. Therefore we ignore the event - * if Android tries to 'catch up'. - */ - Log.d(LocalNotification.PLUGIN_NAME, "AlarmReceiver, ignoring alarm since it is due"); - return; - } + if (currentHour != alarmHour && currentMin != alarmMin) { + /* + * If you set a repeating alarm at 11:00 in the morning and it + * should trigger every morning at 08:00 o'clock, it will + * immediately fire. E.g. Android tries to make up for the + * 'forgotten' reminder for that day. Therefore we ignore the event + * if Android tries to 'catch up'. + */ + Log.d(LocalNotification.PLUGIN_NAME, "AlarmReceiver, ignoring alarm since it is due"); + return; + } - // Construct the notification and notificationManager objects - final NotificationManager notificationMgr = (NotificationManager) systemService; - final Notification notification = new Notification(R.drawable.ic_launcher, tickerText, - System.currentTimeMillis()); - final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); - notification.defaults |= Notification.DEFAULT_SOUND; - notification.vibrate = new long[] { 0, 100, 200, 300 }; - notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent); + // Construct the notification and notificationManager objects + final NotificationManager notificationMgr = (NotificationManager) systemService; + final Notification notification = new Notification(R.drawable.ic_launcher, tickerText, + System.currentTimeMillis()); + final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); + notification.defaults |= Notification.DEFAULT_SOUND; + notification.vibrate = new long[] { 0, 100, 200, 300 }; + notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent); - /* - * If you want all reminders to stay in the notification bar, you should - * generate a random ID. If you want do replace an existing - * notification, make sure the ID below matches the ID that you store in - * the alarm intent. - */ - notificationMgr.notify(notificationId, notification); - } + /* + * If you want all reminders to stay in the notification bar, you should + * generate a random ID. If you want do replace an existing + * notification, make sure the ID below matches the ID that you store in + * the alarm intent. + */ + notificationMgr.notify(notificationId, notification); + } } diff --git a/Android/LocalNotification/LocalNotification.java b/Android/LocalNotification/LocalNotification.java index 5a91f7ad..0fef5f78 100644 --- a/Android/LocalNotification/LocalNotification.java +++ b/Android/LocalNotification/LocalNotification.java @@ -32,35 +32,35 @@ public class LocalNotification extends Plugin { @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); - - PluginResult result = null; - - final AlarmOptions alarmOptions = new AlarmOptions(); - alarmOptions.parseOptions(optionsArr); - - /* - * 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(); - } - - return result; + alarm = new AlarmHelper(this.ctx.getContext()); + Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action); + + PluginResult result = null; + + final AlarmOptions alarmOptions = new AlarmOptions(); + alarmOptions.parseOptions(optionsArr); + + /* + * 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(); + } + + return result; } /** @@ -81,19 +81,19 @@ public PluginResult execute(String action, JSONArray optionsArr, String callBack * @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); - } + 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); + } } /** @@ -104,35 +104,35 @@ public PluginResult add(boolean repeatDaily, String alarmTitle, String alarmSubT * registered using addNotification() */ 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); - } + 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); + } } /** * Cancel all notifications that were created by this plugin. */ public PluginResult cancelAllNotifications() { - Log.d(PLUGIN_NAME, "cancelAllNotifications: cancelling all events for this application"); - /* - * 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); - - if (result) { - return new PluginResult(PluginResult.Status.OK); - } else { - return new PluginResult(PluginResult.Status.ERROR); - } + Log.d(PLUGIN_NAME, "cancelAllNotifications: cancelling all events for this application"); + /* + * 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.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE); + final boolean result = alarm.cancelAll(alarmSettings); + + if (result) { + return new PluginResult(PluginResult.Status.OK); + } else { + return new PluginResult(PluginResult.Status.ERROR); + } } /** @@ -148,11 +148,11 @@ public PluginResult cancelAllNotifications() { * @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()); - - return alarmSettingsEditor.commit(); + final Editor alarmSettingsEditor = this.ctx.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); + + alarmSettingsEditor.putString(alarmId, optionsArr.toString()); + + return alarmSettingsEditor.commit(); } /** @@ -164,12 +164,12 @@ private boolean persistAlarm(String alarmId, JSONArray optionsArr) { * @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); - - return alarmSettingsEditor.commit(); - } + final Editor alarmSettingsEditor = this.ctx.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); + + alarmSettingsEditor.remove(alarmId); + + return alarmSettingsEditor.commit(); + } /** * Clear all alarms from the Android shared Preferences @@ -177,10 +177,10 @@ private boolean unpersistAlarm(String alarmId) { * @return true when successfull, otherwise false */ private boolean unpersistAlarmAll() { - final Editor alarmSettingsEditor = this.ctx.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); - - alarmSettingsEditor.clear(); - - return alarmSettingsEditor.commit(); + final Editor alarmSettingsEditor = this.ctx.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit(); + + alarmSettingsEditor.clear(); + + return alarmSettingsEditor.commit(); } }