Search in sources :

Example 6 with CouldNotGetDataException

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

the class RemindyDAO method getActivePlaces.

/**
     * Returns a List of Places associated with PROGRAMMED tasks with location-based reminders
     */
public List<Place> getActivePlaces() {
    List<Place> places = new ArrayList<>();
    int taskCount;
    Place place;
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    Cursor cursor = db.query(RemindyContract.PlaceTable.TABLE_NAME, null, null, null, null, null, null);
    try {
        while (cursor.moveToNext()) {
            place = getPlaceFromCursor(cursor);
            try {
                taskCount = getLocationBasedTasksAssociatedWithPlace(place.getId(), -1).size();
            } catch (CouldNotGetDataException e) {
                taskCount = 0;
            }
            if (taskCount > 0)
                places.add(place);
        }
    } finally {
        cursor.close();
    }
    return places;
}
Also used : CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Place(ve.com.abicelis.remindy.model.Place)

Example 7 with CouldNotGetDataException

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

the class RemindyDAO method getUnprogrammedTasks.

/* Get data from database */
/**
     * Returns a List of Tasks (with Reminder and Attachments) which have TaskStatus.UNPROGRAMMED. Sorted Alphabetically by Task Title
     * @return A List of TaskViewModel
     */
public List<TaskViewModel> getUnprogrammedTasks() throws CouldNotGetDataException {
    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.UNPROGRAMMED.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 !ReminderType.NONE, throw an error.
            if (current.getReminderType() != ReminderType.NONE)
                throw new CouldNotGetDataException("Error, found task with TaskStatus=UNPROGRAMMED with ReminderType != NONE");
            tasks.add(current);
        }
    } finally {
        cursor.close();
    }
    //Sort tasks by title
    Collections.sort(tasks, new UnprogrammedTasksByTitleComparator());
    //Create viewModel
    for (int i = 0; i < tasks.size(); i++) {
        result.add(new TaskViewModel(tasks.get(i), TaskViewModelType.UNPROGRAMMED_REMINDER));
    }
    return result;
}
Also used : UnprogrammedTasksByTitleComparator(ve.com.abicelis.remindy.model.UnprogrammedTasksByTitleComparator) Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) TaskViewModel(ve.com.abicelis.remindy.viewmodel.TaskViewModel) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 8 with CouldNotGetDataException

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

the class RemindyDAO method getLocationBasedTasks.

/**
     * Returns a List of Tasks (with Reminder and Attachments) which have TaskStatus.PROGRAMMED AND ReminderType.LOCATION_BASED, sorted by Location
     * @return A List of TaskViewModel
     */
public List<TaskViewModel> getLocationBasedTasks(Resources resources) throws CouldNotGetDataException, InvalidClassException {
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    List<TaskViewModel> result;
    List<Task> tasks = new ArrayList<>();
    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 NON location-based task
            current.getReminderType() != ReminderType.LOCATION_BASED)
                continue;
            //Try to get the attachments, if there are any
            current.setAttachments(getAttachmentsOfTask(current.getId()));
            //If Task ReminderType.NONE, throw an error.
            if (current.getReminderType() == ReminderType.NONE)
                throw new CouldNotGetDataException("Error, Task with TaskStatus=PROGRAMMED has ReminderType=NONE");
            else
                current.setReminder(getReminderOfTask(current.getId(), current.getReminderType()));
            tasks.add(current);
        }
    } finally {
        cursor.close();
    }
    //Generate List<TaskViewModel>
    result = new TaskSortingUtil().generateProgrammedTaskHeaderList(tasks, TaskSortType.PLACE, resources);
    return result;
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) 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)

Example 9 with CouldNotGetDataException

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

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

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