Bring back snooze button

pull/542/head
Alinson S. Xavier 6 years ago
parent 7076cffec5
commit aadfac68cd

@ -48,9 +48,10 @@ class HabitsModule {
fun getReminderScheduler( fun getReminderScheduler(
sys: IntentScheduler, sys: IntentScheduler,
commandRunner: CommandRunner, commandRunner: CommandRunner,
habitList: HabitList habitList: HabitList,
widgetPreferences: WidgetPreferences
): ReminderScheduler { ): ReminderScheduler {
return ReminderScheduler(commandRunner, habitList, sys) return ReminderScheduler(commandRunner, habitList, sys, widgetPreferences)
} }
@Provides @Provides

@ -148,13 +148,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
updateWeekdayPreference(); updateWeekdayPreference();
if (SDK_INT < Build.VERSION_CODES.O) // Temporarily disable this; we now always ask
findPreference("reminderCustomize").setVisible(false); findPreference("reminderSound").setVisible(false);
else findPreference("pref_snooze_interval").setVisible(false);
{
findPreference("reminderSound").setVisible(false);
findPreference("pref_snooze_interval").setVisible(false);
}
updateSync(); updateSync();
} }

@ -66,8 +66,8 @@ class AndroidNotificationTray
timestamp: Timestamp, timestamp: Timestamp,
reminderTime: Long) { reminderTime: Long) {
val notificationManager = NotificationManagerCompat.from(context) val notificationManager = NotificationManagerCompat.from(context)
val summary = buildSummary(habit, reminderTime) //val summary = buildSummary(habit, reminderTime)
notificationManager.notify(Int.MAX_VALUE, summary) //notificationManager.notify(Int.MAX_VALUE, summary)
val notification = buildNotification(habit, reminderTime, timestamp) val notification = buildNotification(habit, reminderTime, timestamp)
createAndroidNotificationChannel(context) createAndroidNotificationChannel(context)
try { try {
@ -132,13 +132,11 @@ class AndroidNotificationTray
if (preferences.shouldMakeNotificationsLed()) if (preferences.shouldMakeNotificationsLed())
builder.setLights(Color.RED, 1000, 1000) builder.setLights(Color.RED, 1000, 1000)
if (SDK_INT < Build.VERSION_CODES.O) { val snoozeAction = Action(R.drawable.ic_action_snooze,
val snoozeAction = Action(R.drawable.ic_action_snooze, context.getString(R.string.snooze),
context.getString(R.string.snooze), pendingIntents.snoozeNotification(habit))
pendingIntents.snoozeNotification(habit)) wearableExtender.addAction(snoozeAction)
wearableExtender.addAction(snoozeAction) builder.addAction(snoozeAction)
builder.addAction(snoozeAction)
}
builder.extend(wearableExtender) builder.extend(wearableExtender)
return builder.build() return builder.build()

@ -26,6 +26,9 @@ public class SnoozeDelayPickerActivity extends FragmentActivity
private ReminderController reminderController; private ReminderController reminderController;
@Nullable
private AlertDialog dialog;
@Override @Override
protected void onCreate(@Nullable Bundle bundle) protected void onCreate(@Nullable Bundle bundle)
{ {
@ -40,7 +43,7 @@ public class SnoozeDelayPickerActivity extends FragmentActivity
if (habit == null) finish(); if (habit == null) finish();
int theme = R.style.Theme_AppCompat_Light_Dialog_Alert; int theme = R.style.Theme_AppCompat_Light_Dialog_Alert;
AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(this, theme)) dialog = new AlertDialog.Builder(new ContextThemeWrapper(this, theme))
.setTitle(R.string.select_snooze_delay) .setTitle(R.string.select_snooze_delay)
.setItems(R.array.snooze_picker_names, null) .setItems(R.array.snooze_picker_names, null)
.create(); .create();
@ -82,4 +85,11 @@ public class SnoozeDelayPickerActivity extends FragmentActivity
super.finish(); super.finish();
overridePendingTransition(0, 0); overridePendingTransition(0, 0);
} }
@Override
protected void onPause()
{
if (dialog != null) dialog.dismiss();
super.onPause();
}
} }

@ -70,17 +70,13 @@ public class ReminderController
public void onSnoozePressed(@NonNull Habit habit, final Context context) public void onSnoozePressed(@NonNull Habit habit, final Context context)
{ {
long delay = preferences.getSnoozeInterval(); showSnoozeDelayPicker(habit, context);
if (delay < 0)
showSnoozeDelayPicker(habit, context);
else
scheduleReminderMinutesFromNow(habit, delay);
} }
public void onSnoozeDelayPicked(Habit habit, int delay) public void onSnoozeDelayPicked(Habit habit, int delayInMinutes)
{ {
scheduleReminderMinutesFromNow(habit, delay); reminderScheduler.snoozeReminder(habit, delayInMinutes);
notificationTray.cancel(habit);
} }
public void onSnoozeTimePicked(Habit habit, int hour, int minute) public void onSnoozeTimePicked(Habit habit, int hour, int minute)
@ -95,12 +91,6 @@ public class ReminderController
notificationTray.cancel(habit); notificationTray.cancel(habit);
} }
private void scheduleReminderMinutesFromNow(Habit habit, long minutes)
{
reminderScheduler.scheduleMinutesFromNow(habit, minutes);
notificationTray.cancel(habit);
}
private void showSnoozeDelayPicker(@NonNull Habit habit, Context context) private void showSnoozeDelayPicker(@NonNull Habit habit, Context context)
{ {
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

@ -61,21 +61,6 @@ public class ReminderControllerTest extends BaseAndroidJVMTest
verifyNoMoreInteractions(preferences); verifyNoMoreInteractions(preferences);
} }
@Test
public void testOnSnooze() throws Exception
{
Habit habit = mock(Habit.class);
long now = timestamp(2015, 1, 1);
long nowTz = DateUtils.applyTimezone(now);
DateUtils.setFixedLocalTime(now);
when(preferences.getSnoozeInterval()).thenReturn(15L);
controller.onSnoozePressed(habit,null);
verify(reminderScheduler).scheduleMinutesFromNow(habit, 15L);
verify(notificationTray).cancel(habit);
}
@Test @Test
public void testOnShowReminder() throws Exception public void testOnShowReminder() throws Exception
{ {

@ -57,7 +57,27 @@ public class WidgetPreferences {
storage.remove(habitIdKey); storage.remove(habitIdKey);
} }
public long getSnoozeTime(long id)
{
return storage.getLong(getSnoozeKey(id), 0);
}
private String getHabitIdKey(int id) { private String getHabitIdKey(int id) {
return String.format("widget-%06d-habit", id); return String.format("widget-%06d-habit", id);
} }
private String getSnoozeKey(long id)
{
return String.format("snooze-%06d", id);
}
public void removeSnoozeTime(long id)
{
storage.putLong(getSnoozeKey(id), 0);
}
public void setSnoozeTime(Long id, long time)
{
storage.putLong(getSnoozeKey(id), time);
}
} }

@ -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,43 +46,77 @@ 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;
if (command instanceof ChangeHabitColorCommand) return; if (command instanceof ChangeHabitColorCommand) return;
scheduleAll(); scheduleAll();
} }
public void schedule(@NonNull Habit habit) public synchronized void schedule(@NonNull Habit habit)
{ {
if (!habit.hasReminder()) { if (habit.getId() == null)
{
sys.log("ReminderScheduler", "Habit has null id. Returning.");
return;
}
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;
} }
@ -100,26 +137,27 @@ public class ReminderScheduler implements CommandRunner.Listener
{ {
sys.log("ReminderScheduler", "Scheduling all alarms"); sys.log("ReminderScheduler", "Scheduling all alarms");
HabitList reminderHabits = HabitList reminderHabits =
habitList.getFiltered(HabitMatcher.WITH_ALARM); habitList.getFiltered(HabitMatcher.WITH_ALARM);
for (Habit habit : reminderHabits) for (Habit habit : reminderHabits)
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

Loading…
Cancel
Save