diff --git a/README.md b/README.md index 8bbfb09..20286cf 100644 --- a/README.md +++ b/README.md @@ -25,16 +25,30 @@ This plugin is meant to work with Cordova 3.5.0+. // all responses from the audio player are channeled through successCallback and errorCallback // set wakeup timer - window.wakeuptimer.wakeup( successCallback, - errorCallback, + window.wakeuptimer.wakeup( successCallback, + errorCallback, // a list of alarms to set { alarms : [{ type : 'onetime', time : { hour : 14, minute : 30 }, - extra : { message : 'json containing app-specific information to be posted when alarm triggers' }, + extra : { message : 'json containing app-specific information to be posted when alarm triggers' }, message : 'Alarm has expired!' - }] + }] + } + ); + + // set repeating wakeup timer + window.wakeuptimer.wakeup( successCallback, + errorCallback, + // a list of alarms to set + { + alarms : [{ + type : 'repeating', + time : { minutes : 10 }, + extra : { message : 'json containing app-specific information to be posted when alarm triggers' }, + message : 'Alarm has expired!' + }] } ); @@ -44,7 +58,7 @@ This plugin is meant to work with Cordova 3.5.0+. { alarms : [{ type : 'snooze', - time : { seconds : 60 }, // snooze for 60 seconds + time : { seconds : 60 }, // snooze for 60 seconds extra : { }, // json containing app-specific information to be posted when alarm triggers message : this.get('message'), sound : this.get('sound'), @@ -62,4 +76,4 @@ This plugin is meant to work with Cordova 3.5.0+. } else { console.log('wakeup unhandled type (' + result.type + ')'); } - }; + }; diff --git a/src/android/WakeupPlugin.java b/src/android/WakeupPlugin.java index 62e3b38..444dc74 100644 --- a/src/android/WakeupPlugin.java +++ b/src/android/WakeupPlugin.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.Map; import java.util.TimeZone; +import java.util.concurrent.TimeUnit; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; @@ -33,6 +34,7 @@ public class WakeupPlugin extends CordovaPlugin { protected static final int ID_DAYLIST_OFFSET = 10010; protected static final int ID_ONETIME_OFFSET = 10000; protected static final int ID_SNOOZE_OFFSET = 10001; + protected static final int ID_REPEAT_OFFSET = 10011; public static Map daysOfWeek = new HashMap() { private static final long serialVersionUID = 1L; @@ -182,6 +184,15 @@ protected static void setAlarms(Context context, JSONArray alarms, boolean cance intent.putExtra("type", type); } setNotification(context, type, alarmDate, intent, ID_SNOOZE_OFFSET); + } else if ( type.equals("repeating")) { + Calendar alarmDate = getRepeatingAlertDate(time); + Intent intent = new Intent(context, WakeupReceiver.class); + if(alarm.has("extra")){ + intent.putExtra("extra", alarm.getJSONObject("extra").toString()); + intent.putExtra("type", type); + } + + setNotification(context, type, alarmDate, intent, ID_REPEAT_OFFSET); } } } @@ -189,18 +200,30 @@ protected static void setAlarms(Context context, JSONArray alarms, boolean cance protected static void setNotification(Context context, String type, Calendar alarmDate, Intent intent, int id) throws JSONException{ if(alarmDate!=null){ - SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Log.d(LOG_TAG,"setting alarm at " + sdf.format(alarmDate.getTime()) + "; id " + id); - intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - if (Build.VERSION.SDK_INT>=19) { - alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), sender); + + if (type.equals("repeating")) { + Log.d(LOG_TAG, "setting alarm every " + alarmDate.get(Calendar.MINUTE) + " minutes; id " + id); + + TimeZone defaultTimeZone = TimeZone.getDefault(); + Calendar now = new GregorianCalendar(defaultTimeZone); + now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + alarmDate.get(Calendar.MINUTE)); + + long intervalMillis = TimeUnit.MINUTES.toMillis(alarmDate.get(Calendar.MINUTE)); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), intervalMillis, sender); } else { - alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), sender); + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Log.d(LOG_TAG,"setting alarm at " + sdf.format(alarmDate.getTime()) + "; id " + id); + + if (Build.VERSION.SDK_INT>=19) { + alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), sender); + } else { + alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), sender); + } } - + if(WakeupPlugin.connectionCallbackContext!=null) { JSONObject o=new JSONObject(); o.put("type", "set"); @@ -242,7 +265,20 @@ protected static void cancelSnooze(Context context){ Log.d(LOG_TAG, "cancelling alarm id " + ID_SNOOZE_OFFSET); alarmManager.cancel(sender); } - + + protected static Calendar getRepeatingAlertDate(JSONObject time) throws JSONException { + TimeZone defaultTimeZone = TimeZone.getDefault(); + Calendar calendar = new GregorianCalendar(defaultTimeZone); + + if (time.has("minutes")) { + calendar.set(Calendar.MINUTE, time.getInt("minutes")); + } else { + calendar = null; + } + + return calendar; + } + protected static Calendar getOneTimeAlarmDate( JSONObject time) throws JSONException { TimeZone defaultz = TimeZone.getDefault(); Calendar calendar = new GregorianCalendar(defaultz);