From 944a112dbcd859fda838ca83f7cdf178cf6ddfa8 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 9 Nov 2018 08:18:48 -0600 Subject: [PATCH] Synchronize access to refresh scheduler --- .../tasks/scheduling/RefreshScheduler.java | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java b/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java index d5d97d2ef..c56e48aac 100644 --- a/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java +++ b/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java @@ -1,6 +1,5 @@ package org.tasks.scheduling; -import static com.google.common.collect.Lists.newArrayList; import static com.todoroo.andlib.utility.DateUtilities.ONE_MINUTE; import static org.tasks.time.DateTimeUtils.currentTimeMillis; @@ -22,7 +21,7 @@ public class RefreshScheduler { this.workManager = workManager; } - public void scheduleRefresh(Task task) { + public synchronized void scheduleRefresh(Task task) { if (task.isCompleted()) { scheduleRefresh(task.getCompletionDate() + ONE_MINUTE); } else if (task.hasDueDate()) { @@ -33,26 +32,21 @@ public class RefreshScheduler { } } - private void scheduleRefresh(Long refreshTime) { + private void scheduleRefresh(Long timestamp) { long now = currentTimeMillis(); - if (now < refreshTime) { - refreshTime += 1000; // this is ghetto - schedule(refreshTime); + if (now < timestamp) { + SortedSet upcoming = jobs.tailSet(now); + boolean reschedule = upcoming.isEmpty() || timestamp < upcoming.first(); + jobs.add(timestamp); + if (reschedule) { + scheduleNext(); + } } } - private void schedule(long timestamp) { - SortedSet upcoming = jobs.tailSet(currentTimeMillis()); - boolean reschedule = upcoming.isEmpty() || timestamp < upcoming.first(); - jobs.add(timestamp); - if (reschedule) { - scheduleNext(); - } - } - - public void scheduleNext() { - long now = currentTimeMillis(); - jobs.removeAll(newArrayList(jobs.headSet(now + 1))); + public synchronized void scheduleNext() { + SortedSet lapsed = jobs.headSet(currentTimeMillis() + 1); + jobs.removeAll(lapsed); if (!jobs.isEmpty()) { workManager.scheduleRefresh(jobs.first()); }