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<?>[] {
Task.ID,
Task.CREATION_DATE,
Task.COMPLETION_DATE,
Task.DELETION_DATE,
Task.DUE_DATE,
@ -186,8 +187,11 @@ public final class ReminderService {
}
/**
* Calculate the next alarm time for overdue reminders. If the due date
* has passed, we schedule a reminder some time in the next 4 - 24 hours.
* Calculate the next alarm time for overdue reminders.
* <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
* @return
@ -195,17 +199,31 @@ public final class ReminderService {
private long calculateNextOverdueReminder(Task task) {
if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AFTER_DEADLINE)) {
long dueDate = task.getValue(Task.DUE_DATE);
if(dueDate < DateUtilities.now())
dueDate = DateUtilities.now();
return dueDate + (long)((4 + 30 * random.nextFloat()) * DateUtilities.ONE_HOUR);
long lastReminder = task.getValue(Task.REMINDER_LAST);;
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;
}
/**
* Calculate the next alarm time for due date reminders. If the due date
* has not already passed, we return the due date, altering the time
* if the date was indicated to not have a due time
* Calculate the next alarm time for due date reminders.
* <p>
* 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
* @return
@ -213,7 +231,9 @@ public final class ReminderService {
private long calculateNextDueDateReminder(Task task) {
if(task.hasDueDate() && task.getFlag(Task.REMINDER_FLAGS, Task.NOTIFY_AT_DEADLINE)) {
long dueDate = task.getValue(Task.DUE_DATE);
if(dueDate < DateUtilities.now())
long lastReminder = task.getValue(Task.REMINDER_LAST);;
if(lastReminder > dueDate)
return NO_ALARM;
else if(task.hasDueTime())
// 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
* random reminder time and add approximately the reminder period, until
* we get a time that's in the future.
* Calculate the next alarm time for random reminders.
* <p>
* 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
* @return
@ -242,14 +264,16 @@ public final class ReminderService {
if((reminderPeriod) > 0) {
long when = task.getValue(Task.REMINDER_LAST);
// get or make up a last notification time
if(when == 0) {
when = DateUtilities.now();
task.setValue(Task.REMINDER_LAST, when);
taskDao.saveExisting(task);
}
if(when == 0)
when = task.getValue(Task.CREATION_DATE);
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 NO_ALARM;
@ -309,10 +333,8 @@ public final class ReminderService {
if(time == 0 || time == NO_ALARM)
am.cancel(pendingIntent);
else {
if(time < DateUtilities.now()) {
time = DateUtilities.now() + (long)((0.5f +
4 * random.nextFloat()) * DateUtilities.ONE_HOUR);
}
if(time < DateUtilities.now())
time = DateUtilities.now() + 5000L;
Log.e("Astrid", "Alarm (" + task.getId() + ", " + type +
") 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.ExceptionService;
import com.todoroo.andlib.service.ExceptionService.TodorooUncaughtExceptionHandler;
import com.todoroo.astrid.alarms.AlarmService;
import com.todoroo.astrid.backup.BackupService;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.producteev.ProducteevBackgroundService;
import com.todoroo.astrid.producteev.ProducteevUtilities;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.rmilk.MilkBackgroundService;
import com.todoroo.astrid.rmilk.MilkUtilities;
import com.todoroo.astrid.utility.Constants;
@ -113,6 +115,15 @@ public class StartupService {
database.openForWriting();
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();

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

Loading…
Cancel
Save