|
|
@ -24,6 +24,7 @@ import android.support.annotation.*;
|
|
|
|
import org.isoron.uhabits.core.*;
|
|
|
|
import org.isoron.uhabits.core.*;
|
|
|
|
import org.isoron.uhabits.core.commands.*;
|
|
|
|
import org.isoron.uhabits.core.commands.*;
|
|
|
|
import org.isoron.uhabits.core.models.*;
|
|
|
|
import org.isoron.uhabits.core.models.*;
|
|
|
|
|
|
|
|
import org.isoron.uhabits.core.preferences.*;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
@ -34,6 +35,8 @@ import static org.isoron.uhabits.core.utils.DateUtils.*;
|
|
|
|
@AppScope
|
|
|
|
@AppScope
|
|
|
|
public class ReminderScheduler implements CommandRunner.Listener
|
|
|
|
public class ReminderScheduler implements CommandRunner.Listener
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
private final WidgetPreferences widgetPreferences;
|
|
|
|
|
|
|
|
|
|
|
|
private CommandRunner commandRunner;
|
|
|
|
private CommandRunner commandRunner;
|
|
|
|
|
|
|
|
|
|
|
|
private HabitList habitList;
|
|
|
|
private HabitList habitList;
|
|
|
@ -43,15 +46,17 @@ public class ReminderScheduler implements CommandRunner.Listener
|
|
|
|
@Inject
|
|
|
|
@Inject
|
|
|
|
public ReminderScheduler(@NonNull CommandRunner commandRunner,
|
|
|
|
public ReminderScheduler(@NonNull CommandRunner commandRunner,
|
|
|
|
@NonNull HabitList habitList,
|
|
|
|
@NonNull HabitList habitList,
|
|
|
|
@NonNull SystemScheduler sys)
|
|
|
|
@NonNull SystemScheduler sys,
|
|
|
|
|
|
|
|
@NonNull WidgetPreferences widgetPreferences)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.commandRunner = commandRunner;
|
|
|
|
this.commandRunner = commandRunner;
|
|
|
|
this.habitList = habitList;
|
|
|
|
this.habitList = habitList;
|
|
|
|
this.sys = sys;
|
|
|
|
this.sys = sys;
|
|
|
|
|
|
|
|
this.widgetPreferences = widgetPreferences;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onCommandExecuted(@NonNull Command command,
|
|
|
|
public synchronized void onCommandExecuted(@NonNull Command command,
|
|
|
|
@Nullable Long refreshKey)
|
|
|
|
@Nullable Long refreshKey)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (command instanceof ToggleRepetitionCommand) return;
|
|
|
|
if (command instanceof ToggleRepetitionCommand) return;
|
|
|
@ -59,27 +64,59 @@ public class ReminderScheduler implements CommandRunner.Listener
|
|
|
|
scheduleAll();
|
|
|
|
scheduleAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void schedule(@NonNull Habit habit)
|
|
|
|
public synchronized void schedule(@NonNull Habit habit)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (habit.getId() == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sys.log("ReminderScheduler", "Habit has null id. Returning.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!habit.hasReminder())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!habit.hasReminder()) {
|
|
|
|
|
|
|
|
sys.log("ReminderScheduler", "habit=" + habit.id + " has no reminder. Skipping.");
|
|
|
|
sys.log("ReminderScheduler", "habit=" + habit.id + " has no reminder. Skipping.");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long reminderTime = habit.getReminder().getTimeInMillis();
|
|
|
|
long reminderTime = habit.getReminder().getTimeInMillis();
|
|
|
|
|
|
|
|
long snoozeReminderTime = widgetPreferences.getSnoozeTime(habit.getId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (snoozeReminderTime != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
long now = applyTimezone(getLocalTime());
|
|
|
|
|
|
|
|
sys.log("ReminderScheduler", String.format(
|
|
|
|
|
|
|
|
Locale.US,
|
|
|
|
|
|
|
|
"Habit %d has been snoozed until %d",
|
|
|
|
|
|
|
|
habit.getId(),
|
|
|
|
|
|
|
|
snoozeReminderTime));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (snoozeReminderTime > now)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sys.log("ReminderScheduler", "Snooze time is in the future. Accepting.");
|
|
|
|
|
|
|
|
reminderTime = snoozeReminderTime;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sys.log("ReminderScheduler", "Snooze time is in the past. Discarding.");
|
|
|
|
|
|
|
|
widgetPreferences.removeSnoozeTime(habit.getId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
scheduleAtTime(habit, reminderTime);
|
|
|
|
scheduleAtTime(habit, reminderTime);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void scheduleAtTime(@NonNull Habit habit, long reminderTime)
|
|
|
|
public synchronized void scheduleAtTime(@NonNull Habit habit, long reminderTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sys.log("ReminderScheduler", "Scheduling alarm for habit=" + habit.id);
|
|
|
|
sys.log("ReminderScheduler", "Scheduling alarm for habit=" + habit.id);
|
|
|
|
|
|
|
|
|
|
|
|
if (!habit.hasReminder()) {
|
|
|
|
if (!habit.hasReminder())
|
|
|
|
|
|
|
|
{
|
|
|
|
sys.log("ReminderScheduler", "habit=" + habit.id + " has no reminder. Skipping.");
|
|
|
|
sys.log("ReminderScheduler", "habit=" + habit.id + " has no reminder. Skipping.");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (habit.isArchived()) {
|
|
|
|
if (habit.isArchived())
|
|
|
|
|
|
|
|
{
|
|
|
|
sys.log("ReminderScheduler", "habit=" + habit.id + " is archived. Skipping.");
|
|
|
|
sys.log("ReminderScheduler", "habit=" + habit.id + " is archived. Skipping.");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -105,21 +142,22 @@ public class ReminderScheduler implements CommandRunner.Listener
|
|
|
|
schedule(habit);
|
|
|
|
schedule(habit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void startListening()
|
|
|
|
public synchronized void startListening()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
commandRunner.addListener(this);
|
|
|
|
commandRunner.addListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void stopListening()
|
|
|
|
public synchronized void stopListening()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
commandRunner.removeListener(this);
|
|
|
|
commandRunner.removeListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void scheduleMinutesFromNow(Habit habit, long minutes)
|
|
|
|
public synchronized void snoozeReminder(Habit habit, long minutes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
long now = applyTimezone(getLocalTime());
|
|
|
|
long now = applyTimezone(getLocalTime());
|
|
|
|
long reminderTime = now + minutes * 60 * 1000;
|
|
|
|
long snoozedUntil = now + minutes * 60 * 1000;
|
|
|
|
scheduleAtTime(habit, reminderTime);
|
|
|
|
widgetPreferences.setSnoozeTime(habit.getId(), snoozedUntil);
|
|
|
|
|
|
|
|
schedule(habit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface SystemScheduler
|
|
|
|
public interface SystemScheduler
|
|
|
|