use of ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel in project Remindy by abicelis.
the class AlarmManagerUtil method updateAlarms.
public static void updateAlarms(Context context) {
Log.d(TAG, "Updating alarms.");
List<Integer> triggeredTasks = SharedPreferenceUtil.getTriggeredTaskList(context);
Log.d(TAG, "TriggeredTask IDs = " + TextUtils.join(",", triggeredTasks));
//Get next task to trigger
TaskTriggerViewModel task;
try {
task = new RemindyDAO(context).getNextTaskToTrigger(triggeredTasks);
} catch (CouldNotGetDataException e) {
task = null;
}
if (task != null) {
Log.d(TAG, "Got Task ID " + task.getTask().getId() + ". Title:" + task.getTask().getTitle());
int triggerMinutesBeforeNotification = SharedPreferenceUtil.getTriggerMinutesBeforeNotification(context).getMinutes();
Calendar triggerTime = Calendar.getInstance();
CalendarUtil.copyCalendar(task.getTriggerDateWithTime(), triggerTime);
triggerTime.add(Calendar.MINUTE, -triggerMinutesBeforeNotification);
//Build intent
Intent triggerIntent = new Intent(context, TriggerTaskNotificationReceiver.class);
triggerIntent.putExtra(TriggerTaskNotificationReceiver.TASK_ID_EXTRA, task.getTask().getId());
if (triggerTime.compareTo(Calendar.getInstance()) <= 0) {
//Trigger now
Log.d(TAG, "Triggering now!");
context.sendBroadcast(triggerIntent);
} else {
//Set Alarm
Log.d(TAG, "Programming alarm for " + triggerTime.getTime());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, triggerIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, triggerTime.getTimeInMillis(), pendingIntent);
}
}
Toast.makeText(context, "Alarms updated!", Toast.LENGTH_SHORT).show();
}
use of ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel in project Remindy by abicelis.
the class RemindyDAO method getNextTaskToTrigger.
/**
* Returns the next PROGRAMMED task(With ONE-TIME or REPEATING reminder) to occur
* @param alreadyTriggeredTaskList an optional task list to not include in the search
* @return A single TaskTriggerViewModel or null of there are no tasks
*/
public TaskTriggerViewModel getNextTaskToTrigger(@NonNull List<Integer> alreadyTriggeredTaskList) throws CouldNotGetDataException {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Task nextTaskToTrigger = null;
Calendar triggerDate = null;
Time triggerTime = null;
Cursor cursor = db.query(RemindyContract.TaskTable.TABLE_NAME, null, RemindyContract.TaskTable.COLUMN_NAME_STATUS.getName() + "=?", new String[] { TaskStatus.PROGRAMMED.name() }, null, null, null);
try {
while (cursor.moveToNext()) {
Task current = getTaskFromCursor(cursor);
if (//Skip
alreadyTriggeredTaskList.contains(current.getId()))
continue;
try {
current.setReminder(getReminderOfTask(current.getId(), current.getReminderType()));
} catch (CouldNotGetDataException | SQLiteConstraintException e) {
throw new CouldNotGetDataException("Error fetching reminder for task ID" + current.getId(), e);
}
//TODO: this filter could be made on db query.
if (current.getReminderType().equals(ReminderType.ONE_TIME) || current.getReminderType().equals(ReminderType.REPEATING)) {
if (//Skip overdue reminders
TaskUtil.checkIfOverdue(current.getReminder()))
continue;
if (nextTaskToTrigger == null) {
nextTaskToTrigger = current;
triggerDate = (current.getReminderType().equals(ReminderType.ONE_TIME) ? ((OneTimeReminder) current.getReminder()).getDate() : TaskUtil.getRepeatingReminderNextCalendar((RepeatingReminder) current.getReminder()));
triggerTime = (current.getReminderType().equals(ReminderType.ONE_TIME) ? ((OneTimeReminder) current.getReminder()).getTime() : ((RepeatingReminder) current.getReminder()).getTime());
continue;
}
if (current.getReminderType().equals(ReminderType.ONE_TIME)) {
OneTimeReminder otr = (OneTimeReminder) current.getReminder();
Calendar currentDate = CalendarUtil.getCalendarFromDateAndTime(otr.getDate(), otr.getTime());
if (currentDate.compareTo(triggerDate) < 0) {
nextTaskToTrigger = current;
triggerDate = currentDate;
triggerTime = otr.getTime();
continue;
}
}
if (current.getReminderType().equals(ReminderType.REPEATING)) {
RepeatingReminder rr = (RepeatingReminder) current.getReminder();
Calendar currentDate = TaskUtil.getRepeatingReminderNextCalendar(rr);
//Overdue
if (currentDate == null)
continue;
if (currentDate.compareTo(triggerDate) < 0) {
nextTaskToTrigger = current;
triggerDate = currentDate;
triggerTime = rr.getTime();
continue;
}
}
}
}
} finally {
cursor.close();
}
if (nextTaskToTrigger == null)
return null;
return new TaskTriggerViewModel(nextTaskToTrigger, triggerDate, triggerTime);
}
Aggregations