use of ve.com.abicelis.remindy.model.reminder.RepeatingReminder in project Remindy by abicelis.
the class RemindyDAO method getRepeatingReminderFromCursor.
private RepeatingReminder getRepeatingReminderFromCursor(Cursor cursor) {
int id = cursor.getInt(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable._ID));
int taskId = cursor.getInt(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_TASK_FK.getName()));
Calendar date = Calendar.getInstance();
date.setTimeInMillis(cursor.getLong(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_DATE.getName())));
Time time = new Time(cursor.getInt(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_TIME.getName())));
time.setDisplayTimeFormat(SharedPreferenceUtil.getTimeFormat(mContext));
ReminderRepeatType repeatType = ReminderRepeatType.valueOf(cursor.getString(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_REPEAT_TYPE.getName())));
int repeatInterval = cursor.getInt(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_REPEAT_INTERVAL.getName()));
ReminderRepeatEndType repeatEndType = ReminderRepeatEndType.valueOf(cursor.getString(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_REPEAT_END_TYPE.getName())));
int repeatEndNumberOfEvents = -1;
if (repeatEndType == ReminderRepeatEndType.FOR_X_EVENTS)
repeatEndNumberOfEvents = cursor.getInt(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_REPEAT_END_NUMBER_OF_EVENTS.getName()));
Calendar repeatEndDate = null;
if (repeatEndType == ReminderRepeatEndType.UNTIL_DATE) {
repeatEndDate = Calendar.getInstance();
repeatEndDate.setTimeInMillis(cursor.getLong(cursor.getColumnIndex(RemindyContract.RepeatingReminderTable.COLUMN_NAME_REPEAT_END_DATE.getName())));
}
return new RepeatingReminder(id, taskId, date, time, repeatType, repeatInterval, repeatEndType, repeatEndNumberOfEvents, repeatEndDate);
}
use of ve.com.abicelis.remindy.model.reminder.RepeatingReminder in project Remindy by abicelis.
the class TasksByReminderDateComparator method compare.
@Override
public int compare(Task o1, Task o2) {
if (o1.getReminder() == null || (o1.getReminderType() != ReminderType.ONE_TIME && o1.getReminderType() != ReminderType.REPEATING))
return 1;
if (o2.getReminder() == null || (o2.getReminderType() != ReminderType.ONE_TIME && o2.getReminderType() != ReminderType.REPEATING))
return -1;
Calendar o1Date = null;
if (o1.getReminderType() == ReminderType.ONE_TIME)
o1Date = ((OneTimeReminder) o1.getReminder()).getDate();
else {
//Repeating reminder
Calendar cal = TaskUtil.getRepeatingReminderNextCalendar(((RepeatingReminder) o1.getReminder()));
o1Date = (cal != null ? cal : TaskUtil.getRepeatingReminderEndCalendar(((RepeatingReminder) o1.getReminder())));
}
Calendar o2Date = null;
if (o2.getReminderType() == ReminderType.ONE_TIME)
o2Date = ((OneTimeReminder) o2.getReminder()).getDate();
else {
//Repeating reminder
Calendar cal = TaskUtil.getRepeatingReminderNextCalendar(((RepeatingReminder) o2.getReminder()));
o2Date = (cal != null ? cal : TaskUtil.getRepeatingReminderEndCalendar(((RepeatingReminder) o2.getReminder())));
}
//Calendar o2Date = (o2.getReminderType() == ReminderType.ONE_TIME ? ((OneTimeReminder) o2.getReminder()).getDate() : TaskUtil.getRepeatingReminderNextCalendar(((RepeatingReminder) o2.getReminder())) );
return o1Date.compareTo(o2Date);
}
use of ve.com.abicelis.remindy.model.reminder.RepeatingReminder in project Remindy by abicelis.
the class TaskActionsIntentService method onHandleIntent.
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent == null || intent.getAction() == null)
return;
if (intent.getAction().equals(ACTION_SET_TASK_DONE)) {
int taskId = intent.getIntExtra(PARAM_TASK_ID, -1);
if (taskId != -1) {
RemindyDAO dao = new RemindyDAO(getApplicationContext());
try {
Task task = dao.getTask(taskId);
task.setDoneDate(CalendarUtil.getNewInstanceZeroedCalendar());
task.setStatus(TaskStatus.DONE);
dao.updateTask(task);
} catch (CouldNotGetDataException | CouldNotUpdateDataException e) {
Toast.makeText(this, getResources().getString(R.string.task_actions_service_could_not_set_done), Toast.LENGTH_SHORT).show();
}
//Dismiss the notification
NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.cancel(taskId);
}
} else if (intent.getAction().equals(ACTION_POSTPONE_TASK)) {
int taskId = intent.getIntExtra(PARAM_TASK_ID, -1);
if (taskId != -1) {
RemindyDAO dao = new RemindyDAO(getApplicationContext());
try {
Task task = dao.getTask(taskId);
Time time;
switch(task.getReminderType()) {
case REPEATING:
time = ((RepeatingReminder) task.getReminder()).getTime();
break;
case ONE_TIME:
time = ((OneTimeReminder) task.getReminder()).getTime();
break;
default:
//TODO: Show some kind of error?
return;
}
int timeInMinutes = time.getTimeInMinutes();
timeInMinutes += POSTPONE_MINUTES;
//Set max per day to 24:59:00
timeInMinutes = (timeInMinutes >= 1440 ? 1339 : timeInMinutes);
time.setTimeInMinutes(timeInMinutes);
dao.updateTask(task);
//Remove task from triggeredTasks list
SharedPreferenceUtil.removeIdFromTriggeredTasks(getApplicationContext(), task.getId());
//Update alarms
AlarmManagerUtil.updateAlarms(getApplicationContext());
} catch (CouldNotGetDataException | CouldNotUpdateDataException e) {
Toast.makeText(this, getResources().getString(R.string.task_actions_service_could_not_set_done), Toast.LENGTH_SHORT).show();
}
//Dismiss the notification
NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.cancel(taskId);
}
}
}
use of ve.com.abicelis.remindy.model.reminder.RepeatingReminder in project Remindy by abicelis.
the class TriggerTaskNotificationReceiver method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "TriggerTaskNotificationReceiver");
//Get TASK_ID_EXTRA
int taskId;
try {
taskId = intent.getIntExtra(TASK_ID_EXTRA, -1);
} catch (Exception e) {
taskId = -1;
}
if (taskId != -1) {
Task task;
try {
task = new RemindyDAO(context).getTask(taskId);
} catch (CouldNotGetDataException e) {
//TODO: Show some kind of error here
return;
}
if (task != null) {
Log.d(TAG, "Triggering task ID " + taskId);
String triggerTime;
switch(task.getReminderType()) {
case ONE_TIME:
triggerTime = ((OneTimeReminder) task.getReminder()).getTime().toString();
break;
case REPEATING:
triggerTime = ((RepeatingReminder) task.getReminder()).getTime().toString();
break;
default:
//TODO: Show some kind of error here
return;
}
String contentTitle = String.format(Locale.getDefault(), context.getResources().getString(R.string.notification_service_normal_title), task.getTitle());
int triggerMinutesBeforeNotification = SharedPreferenceUtil.getTriggerMinutesBeforeNotification(context).getMinutes();
String contentText = String.format(Locale.getDefault(), context.getResources().getString(R.string.notification_service_normal_text), triggerMinutesBeforeNotification, triggerTime);
NotificationUtil.displayNotification(context, task, contentTitle, contentText);
//Add task to triggeredTasks list
List<Integer> triggeredTasks = SharedPreferenceUtil.getTriggeredTaskList(context);
triggeredTasks.add(task.getId());
SharedPreferenceUtil.setTriggeredTaskList(triggeredTasks, context);
}
} else {
Log.d(TAG, "TriggerTaskNotificationReceiver triggered with no TASK_ID_EXTRA!");
}
//Set next alarm
AlarmManagerUtil.updateAlarms(context);
}
use of ve.com.abicelis.remindy.model.reminder.RepeatingReminder 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