Search in sources :

Example 11 with RemindyDAO

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

Example 12 with RemindyDAO

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

Aggregations

RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)12 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)5 Intent (android.content.Intent)4 BaseTransientBottomBar (android.support.design.widget.BaseTransientBottomBar)4 Snackbar (android.support.design.widget.Snackbar)4 Task (ve.com.abicelis.remindy.model.Task)4 CouldNotUpdateDataException (ve.com.abicelis.remindy.exception.CouldNotUpdateDataException)3 Place (ve.com.abicelis.remindy.model.Place)3 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 ArrayList (java.util.ArrayList)2 CouldNotDeleteDataException (ve.com.abicelis.remindy.exception.CouldNotDeleteDataException)2 CouldNotInsertDataException (ve.com.abicelis.remindy.exception.CouldNotInsertDataException)2 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)2 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)2 AlarmManager (android.app.AlarmManager)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 StringRes (android.support.annotation.StringRes)1 Status (com.google.android.gms.common.api.Status)1