Search in sources :

Example 16 with Task

use of ve.com.abicelis.remindy.model.Task 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);
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) RemindyDAO(ve.com.abicelis.remindy.database.RemindyDAO) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException)

Example 17 with Task

use of ve.com.abicelis.remindy.model.Task 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)

Example 18 with Task

use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.

the class RemindyDAO method getLocationBasedTasksAssociatedWithPlace.

/**
     * Returns a List of Tasks (Status:PROGRAMMED) which have Location-Based reminders of a particular Place, set to trigger either entering or exiting or both.
     * @param placeId The ID of the place with which to look for Tasks
     */
public List<Task> getLocationBasedTasksAssociatedWithPlace(int placeId, int geofenceTransition) throws CouldNotGetDataException {
    List<Task> tasks = new ArrayList<>();
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    Cursor cursor = null;
    switch(geofenceTransition) {
        case //Any
        -1:
            cursor = db.query(RemindyContract.LocationBasedReminderTable.TABLE_NAME, null, RemindyContract.LocationBasedReminderTable.COLUMN_NAME_PLACE_FK.getName() + "=?", new String[] { String.valueOf(placeId) }, null, null, null);
            break;
        case Geofence.GEOFENCE_TRANSITION_DWELL:
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            cursor = db.query(RemindyContract.LocationBasedReminderTable.TABLE_NAME, null, RemindyContract.LocationBasedReminderTable.COLUMN_NAME_PLACE_FK.getName() + "=? AND " + RemindyContract.LocationBasedReminderTable.COLUMN_NAME_TRIGGER_ENTERING.getName() + "=?", new String[] { String.valueOf(placeId), "true" }, null, null, null);
            break;
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            cursor = db.query(RemindyContract.LocationBasedReminderTable.TABLE_NAME, null, RemindyContract.LocationBasedReminderTable.COLUMN_NAME_PLACE_FK.getName() + "=? AND " + RemindyContract.LocationBasedReminderTable.COLUMN_NAME_TRIGGER_EXITING.getName() + "=?", new String[] { String.valueOf(placeId), "true" }, null, null, null);
            break;
    }
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                int taskId = cursor.getInt(cursor.getColumnIndex(RemindyContract.LocationBasedReminderTable.COLUMN_NAME_TASK_FK.getName()));
                Task task = getTask(taskId);
                if (task.getStatus().equals(TaskStatus.PROGRAMMED))
                    tasks.add(task);
            }
        } finally {
            cursor.close();
        }
    }
    return tasks;
}
Also used : Task(ve.com.abicelis.remindy.model.Task) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 19 with Task

use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.

the class RemindyDAO method deletePlace.

/* Delete data from database */
/**
     * Deletes a single Place, given its ID, also deletes Location-based reminders associated with place and updates Task ReminderType to NONE
     * @param placeId The ID of the place to delete
     */
public boolean deletePlace(int placeId) throws CouldNotDeleteDataException {
    SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
    List<Task> tasks;
    try {
        tasks = getLocationBasedTasksAssociatedWithPlace(placeId, -1);
    } catch (CouldNotGetDataException e) {
        throw new CouldNotDeleteDataException("Error getting Task list associated with Place. PlaceID=" + placeId, e);
    }
    if (tasks.size() > 0) {
        //Remove Location-based reminders from task, and update task ReminderType to NONE.
        for (Task task : tasks) {
            deleteReminderOfTask(task.getId());
            task.setStatus(TaskStatus.UNPROGRAMMED);
            task.setReminderType(ReminderType.NONE);
            try {
                updateTask(task);
            } catch (CouldNotUpdateDataException e) {
                throw new CouldNotDeleteDataException("Error updating RemidnerType of Task to NONE. TaskID=" + task.getId(), e);
            }
        }
    }
    return db.delete(RemindyContract.PlaceTable.TABLE_NAME, RemindyContract.PlaceTable._ID + " =?", new String[] { String.valueOf(placeId) }) > 0;
}
Also used : CouldNotUpdateDataException(ve.com.abicelis.remindy.exception.CouldNotUpdateDataException) Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) CouldNotDeleteDataException(ve.com.abicelis.remindy.exception.CouldNotDeleteDataException) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 20 with Task

use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.

the class RemindyDAO method getDoneTasks.

/**
     * Returns a List of Tasks (with Reminder and Attachments) which have TaskStatus.DONE
     * @param sortType TaskSortType enum value with which to sort results. By date or location
     * @return A List of TaskViewModel
     */
public List<TaskViewModel> getDoneTasks(@NonNull TaskSortType sortType, Resources resources) throws CouldNotGetDataException, InvalidClassException {
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    List<TaskViewModel> result = new ArrayList<>();
    List<Task> tasks = new ArrayList<>();
    Cursor cursor = db.query(RemindyContract.TaskTable.TABLE_NAME, null, RemindyContract.TaskTable.COLUMN_NAME_STATUS.getName() + "=?", new String[] { TaskStatus.DONE.name() }, null, null, null);
    try {
        while (cursor.moveToNext()) {
            Task current = getTaskFromCursor(cursor);
            //Try to get the attachments, if there are any
            current.setAttachments(getAttachmentsOfTask(current.getId()));
            //If Task has reminder, get it
            if (current.getReminderType() != ReminderType.NONE)
                current.setReminder(getReminderOfTask(current.getId(), current.getReminderType()));
            tasks.add(current);
        }
    } finally {
        cursor.close();
    }
    //Generate List<TaskViewModel> This List will be sorted and grouped!
    result = new TaskSortingUtil().generateDoneTaskHeaderList(tasks, sortType, resources);
    return result;
}
Also used : Task(ve.com.abicelis.remindy.model.Task) TaskViewModel(ve.com.abicelis.remindy.viewmodel.TaskViewModel) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) TaskSortingUtil(ve.com.abicelis.remindy.util.sorting.TaskSortingUtil) Cursor(android.database.Cursor)

Aggregations

Task (ve.com.abicelis.remindy.model.Task)21 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)12 ArrayList (java.util.ArrayList)10 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)9 TaskViewModel (ve.com.abicelis.remindy.viewmodel.TaskViewModel)8 Cursor (android.database.Cursor)7 RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)4 CouldNotDeleteDataException (ve.com.abicelis.remindy.exception.CouldNotDeleteDataException)3 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)3 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)3 TaskSortingUtil (ve.com.abicelis.remindy.util.sorting.TaskSortingUtil)3 NotificationManager (android.app.NotificationManager)2 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)2 Calendar (java.util.Calendar)2 CouldNotUpdateDataException (ve.com.abicelis.remindy.exception.CouldNotUpdateDataException)2 Time (ve.com.abicelis.remindy.model.Time)2 AlertDialog (android.app.AlertDialog)1 PendingIntent (android.app.PendingIntent)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1