Rewrote alarm scheduling logic, now we schedule all alarms on astrid startup

pull/14/head
Tim Su 14 years ago
parent ff4e6ba5ea
commit b2ea285641

@ -39,6 +39,7 @@ public final class ReminderService {
private static final Property<?>[] PROPERTIES = new Property<?>[] { private static final Property<?>[] PROPERTIES = new Property<?>[] {
Task.ID, Task.ID,
Task.CREATION_DATE,
Task.COMPLETION_DATE, Task.COMPLETION_DATE,
Task.DELETION_DATE, Task.DELETION_DATE,
Task.DUE_DATE, Task.DUE_DATE,
@ -186,8 +187,11 @@ public final class ReminderService {
} }
/** /**
* Calculate the next alarm time for overdue reminders. If the due date * Calculate the next alarm time for overdue reminders.
* has passed, we schedule a reminder some time in the next 4 - 24 hours. * <p>
* We schedule an alarm for after the due date (which could be in the past),
* with the exception that if a reminder was recently issued, we move
* the alarm time to the near future.
* *
* @param task * @param task
* @return * @return
@ -195,17 +199,31 @@ public final class ReminderService {
private long calculateNextOverdueReminder(Task task) { private long calculateNextOverdueReminder(Task task) {
if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AFTER_DEADLINE)) { if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AFTER_DEADLINE)) {
long dueDate = task.getValue(Task.DUE_DATE); long dueDate = task.getValue(Task.DUE_DATE);
if(dueDate < DateUtilities.now()) long lastReminder = task.getValue(Task.REMINDER_LAST);;
dueDate = DateUtilities.now();
return dueDate + (long)((4 + 30 * random.nextFloat()) * DateUtilities.ONE_HOUR); if(dueDate > DateUtilities.now())
return dueDate + (long)((0.5f + 2f * random.nextFloat()) * DateUtilities.ONE_HOUR);
if(lastReminder < dueDate)
return DateUtilities.now();
if(DateUtilities.now() - lastReminder < 6 * DateUtilities.ONE_HOUR)
return DateUtilities.now() + (long)((1.0f + 3f * random.nextFloat()) * DateUtilities.ONE_HOUR);
return DateUtilities.now();
} }
return NO_ALARM; return NO_ALARM;
} }
/** /**
* Calculate the next alarm time for due date reminders. If the due date * Calculate the next alarm time for due date reminders.
* has not already passed, we return the due date, altering the time * <p>
* if the date was indicated to not have a due time * This alarm always returns the due date, and is triggered if
* the last reminder time occurred before the due date. This means it is
* possible to return due dates in the past.
* <p>
* If the date was indicated to not have a due time, we read from
* preferences and assign a time.
* *
* @param task * @param task
* @return * @return
@ -213,7 +231,9 @@ public final class ReminderService {
private long calculateNextDueDateReminder(Task task) { private long calculateNextDueDateReminder(Task task) {
if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE)) { if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE)) {
long dueDate = task.getValue(Task.DUE_DATE); long dueDate = task.getValue(Task.DUE_DATE);
if(dueDate < DateUtilities.now()) long lastReminder = task.getValue(Task.REMINDER_LAST);;
if(lastReminder > dueDate)
return NO_ALARM; return NO_ALARM;
else if(task.hasDueTime()) else if(task.hasDueTime())
// return due date straight up // return due date straight up
@ -230,9 +250,11 @@ public final class ReminderService {
} }
/** /**
* Calculate the next alarm time for random reminders. We take the last * Calculate the next alarm time for random reminders.
* random reminder time and add approximately the reminder period, until * <p>
* we get a time that's in the future. * We take the last reminder time and add approximately the reminder
* period. If it's still in the past, we set it to some time in the near
* future.
* *
* @param task * @param task
* @return * @return
@ -242,14 +264,16 @@ public final class ReminderService {
if((reminderPeriod) > 0) { if((reminderPeriod) > 0) {
long when = task.getValue(Task.REMINDER_LAST); long when = task.getValue(Task.REMINDER_LAST);
// get or make up a last notification time if(when == 0)
if(when == 0) { when = task.getValue(Task.CREATION_DATE);
when = DateUtilities.now();
task.setValue(Task.REMINDER_LAST, when);
taskDao.saveExisting(task);
}
when += (long)(reminderPeriod * (0.85f + 0.3f * random.nextFloat())); when += (long)(reminderPeriod * (0.85f + 0.3f * random.nextFloat()));
if(when < DateUtilities.now()) {
when = DateUtilities.now() + (long)((0.5f +
6 * random.nextFloat()) * DateUtilities.ONE_HOUR);
}
return when; return when;
} }
return NO_ALARM; return NO_ALARM;
@ -309,10 +333,8 @@ public final class ReminderService {
if(time == 0 || time == NO_ALARM) if(time == 0 || time == NO_ALARM)
am.cancel(pendingIntent); am.cancel(pendingIntent);
else { else {
if(time < DateUtilities.now()) { if(time < DateUtilities.now())
time = DateUtilities.now() + (long)((0.5f + time = DateUtilities.now() + 5000L;
4 * random.nextFloat()) * DateUtilities.ONE_HOUR);
}
Log.e("Astrid", "Alarm (" + task.getId() + ", " + type + Log.e("Astrid", "Alarm (" + task.getId() + ", " + type +
") set for " + new Date(time)); ") set for " + new Date(time));

@ -20,10 +20,12 @@ import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.ExceptionService; import com.todoroo.andlib.service.ExceptionService;
import com.todoroo.andlib.service.ExceptionService.TodorooUncaughtExceptionHandler; import com.todoroo.andlib.service.ExceptionService.TodorooUncaughtExceptionHandler;
import com.todoroo.astrid.alarms.AlarmService;
import com.todoroo.astrid.backup.BackupService; import com.todoroo.astrid.backup.BackupService;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.producteev.ProducteevBackgroundService; import com.todoroo.astrid.producteev.ProducteevBackgroundService;
import com.todoroo.astrid.producteev.ProducteevUtilities; import com.todoroo.astrid.producteev.ProducteevUtilities;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.rmilk.MilkBackgroundService; import com.todoroo.astrid.rmilk.MilkBackgroundService;
import com.todoroo.astrid.rmilk.MilkUtilities; import com.todoroo.astrid.rmilk.MilkUtilities;
import com.todoroo.astrid.utility.Constants; import com.todoroo.astrid.utility.Constants;
@ -113,6 +115,15 @@ public class StartupService {
database.openForWriting(); database.openForWriting();
taskService.cleanup(); taskService.cleanup();
// schedule alarms
try {
ReminderService.getInstance().scheduleAllAlarms();
AlarmService.getInstance().scheduleAllAlarms();
} catch (Exception e) {
DependencyInjectionService.getInstance().inject(this);
exceptionService.reportError("reminder-startup", e); //$NON-NLS-1$
}
} }
}).start(); }).start();

@ -35,7 +35,7 @@ public final class Constants {
/** /**
* Whether to turn on debugging logging and UI * Whether to turn on debugging logging and UI
*/ */
public static final boolean DEBUG = true; public static final boolean DEBUG = false;
/** /**
* Upgrade time * Upgrade time

Loading…
Cancel
Save