Search in sources :

Example 11 with CouldNotGetDataException

use of ve.com.abicelis.remindy.exception.CouldNotGetDataException 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 12 with CouldNotGetDataException

use of ve.com.abicelis.remindy.exception.CouldNotGetDataException 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 13 with CouldNotGetDataException

use of ve.com.abicelis.remindy.exception.CouldNotGetDataException in project Remindy by abicelis.

the class RemindyDAO method getReminderOfTask.

/**
     * Returns a Reminder given its taskId and reminderType
     * @param taskId The ID of the task
     * @param reminderType The Type of reminder
     */
public Reminder getReminderOfTask(int taskId, @NonNull ReminderType reminderType) throws CouldNotGetDataException, SQLiteConstraintException {
    SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
    Reminder reminder;
    String tableName, whereClause;
    switch(reminderType) {
        case ONE_TIME:
            whereClause = RemindyContract.OneTimeReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
            tableName = RemindyContract.OneTimeReminderTable.TABLE_NAME;
            break;
        case REPEATING:
            whereClause = RemindyContract.RepeatingReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
            tableName = RemindyContract.RepeatingReminderTable.TABLE_NAME;
            break;
        case LOCATION_BASED:
            whereClause = RemindyContract.LocationBasedReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
            tableName = RemindyContract.LocationBasedReminderTable.TABLE_NAME;
            break;
        default:
            throw new CouldNotGetDataException("ReminderType is invalid. Type=" + reminderType);
    }
    Cursor cursor = db.query(tableName, null, whereClause, new String[] { String.valueOf(taskId) }, null, null, null);
    try {
        if (cursor.getCount() == 0)
            throw new CouldNotGetDataException("Specified Reminder not found in the database. Passed id=" + taskId);
        if (cursor.getCount() > 1)
            throw new SQLiteConstraintException("Database UNIQUE constraint failure, more than one Reminder found. Passed id=" + taskId);
        cursor.moveToNext();
        switch(reminderType) {
            case ONE_TIME:
                reminder = getOneTimeReminderFromCursor(cursor);
                break;
            case REPEATING:
                reminder = getRepeatingReminderFromCursor(cursor);
                break;
            case LOCATION_BASED:
                reminder = getLocationBasedReminderFromCursor(cursor);
                int placeId = ((LocationBasedReminder) reminder).getPlaceId();
                try {
                    ((LocationBasedReminder) reminder).setPlace(getPlace(placeId));
                } catch (PlaceNotFoundException | SQLiteConstraintException e) {
                    throw new CouldNotGetDataException("Error trying to get Place for Location-based Reminder", e);
                }
                break;
            default:
                throw new CouldNotGetDataException("ReminderType is invalid. Type=" + reminderType);
        }
    } finally {
        cursor.close();
    }
    return reminder;
}
Also used : CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) Reminder(ve.com.abicelis.remindy.model.reminder.Reminder) LocationBasedReminder(ve.com.abicelis.remindy.model.reminder.LocationBasedReminder) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) LocationBasedReminder(ve.com.abicelis.remindy.model.reminder.LocationBasedReminder) PlaceNotFoundException(ve.com.abicelis.remindy.exception.PlaceNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor)

Example 14 with CouldNotGetDataException

use of ve.com.abicelis.remindy.exception.CouldNotGetDataException 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 15 with CouldNotGetDataException

use of ve.com.abicelis.remindy.exception.CouldNotGetDataException in project Remindy by abicelis.

the class HomeListFragment method updateViewholderItem.

/* Called from HomeActivity.onActivityResult() */
public void updateViewholderItem(int position) {
    //Task was edited, refresh task info and refresh recycler
    try {
        Task task = mDao.getTask(mTasks.get(position).getTask().getId());
        TaskViewModel taskViewModel = new TaskViewModel(task, ConversionUtil.taskReminderTypeToTaskViewmodelType(task.getReminderType()));
        mTasks.set(position, taskViewModel);
        mAdapter.notifyItemChanged(position);
    } catch (CouldNotGetDataException e) {
        SnackbarUtil.showSnackbar(mRecyclerView, SnackbarUtil.SnackbarType.ERROR, R.string.error_problem_updating_task_from_database, SnackbarUtil.SnackbarDuration.LONG, null);
    }
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) TaskViewModel(ve.com.abicelis.remindy.viewmodel.TaskViewModel)

Aggregations

CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)15 Task (ve.com.abicelis.remindy.model.Task)12 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)9 Cursor (android.database.Cursor)7 ArrayList (java.util.ArrayList)6 RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)5 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)4 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)4 TaskViewModel (ve.com.abicelis.remindy.viewmodel.TaskViewModel)4 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)3 CouldNotDeleteDataException (ve.com.abicelis.remindy.exception.CouldNotDeleteDataException)3 Calendar (java.util.Calendar)2 CouldNotUpdateDataException (ve.com.abicelis.remindy.exception.CouldNotUpdateDataException)2 Time (ve.com.abicelis.remindy.model.Time)2 TaskSortingUtil (ve.com.abicelis.remindy.util.sorting.TaskSortingUtil)2 TaskTriggerViewModel (ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel)2 AlarmManager (android.app.AlarmManager)1 AlertDialog (android.app.AlertDialog)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1