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

Repeating wakeup timer #23

Open
wants to merge 2 commits 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
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
}]
}
);

Expand All @@ -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'),
Expand All @@ -62,4 +76,4 @@ This plugin is meant to work with Cordova 3.5.0+.
} else {
console.log('wakeup unhandled type (' + result.type + ')');
}
};
};
52 changes: 44 additions & 8 deletions src/android/WakeupPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String , Integer> daysOfWeek = new HashMap<String , Integer>() {
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -182,25 +184,46 @@ 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);
}
}
}


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");
Expand Down Expand Up @@ -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);
Expand Down