Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to PhoneGap 2.0. #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 54 additions & 56 deletions Android/LocalNotification/AlarmReceiver.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}
162 changes: 81 additions & 81 deletions Android/LocalNotification/LocalNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -164,23 +164,23 @@ 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
*
* @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();
}
}