Search in sources :

Example 1 with TaskTriggerViewModel

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();
}
Also used : CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) TaskTriggerViewModel(ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) RemindyDAO(ve.com.abicelis.remindy.database.RemindyDAO)

Example 2 with TaskTriggerViewModel

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);
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Calendar(java.util.Calendar) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Time(ve.com.abicelis.remindy.model.Time) TaskTriggerViewModel(ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel) Cursor(android.database.Cursor)

Aggregations

Calendar (java.util.Calendar)2 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)2 TaskTriggerViewModel (ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel)2 AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)1 Task (ve.com.abicelis.remindy.model.Task)1 Time (ve.com.abicelis.remindy.model.Time)1 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)1 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)1