Move notifications and reminders to uhabits-core

pull/87/merge
Alinson S. Xavier 8 years ago
parent b88b3a683d
commit 6875fc0428

@ -26,9 +26,10 @@ import com.activeandroid.*;
import org.isoron.androidbase.*; import org.isoron.androidbase.*;
import org.isoron.uhabits.core.preferences.*; import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.tasks.*; import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.models.sqlite.*; import org.isoron.uhabits.models.sqlite.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*; import org.isoron.uhabits.widgets.*;
@ -47,7 +48,7 @@ public class HabitsApplication extends Application
private ReminderScheduler reminderScheduler; private ReminderScheduler reminderScheduler;
private AndroidNotificationTray notificationTray; private NotificationTray notificationTray;
public HabitsComponent getComponent() public HabitsComponent getComponent()
{ {
@ -106,7 +107,7 @@ public class HabitsApplication extends Application
reminderScheduler = component.getReminderScheduler(); reminderScheduler = component.getReminderScheduler();
reminderScheduler.startListening(); reminderScheduler.startListening();
notificationTray = component.getAndroidNotificationTray(); notificationTray = component.getNotificationTray();
notificationTray.startListening(); notificationTray.startListening();
Preferences prefs = component.getPreferences(); Preferences prefs = component.getPreferences();

@ -26,6 +26,7 @@ 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 org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.tasks.*; import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*; import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.ui.screens.habits.list.*; import org.isoron.uhabits.core.ui.screens.habits.list.*;
@ -36,7 +37,6 @@ import org.isoron.uhabits.models.sqlite.*;
import org.isoron.uhabits.notifications.*; import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.sync.*; import org.isoron.uhabits.sync.*;
import org.isoron.uhabits.tasks.*; import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*; import org.isoron.uhabits.widgets.*;
import dagger.*; import dagger.*;
@ -50,8 +50,6 @@ import dagger.*;
}) })
public interface HabitsComponent public interface HabitsComponent
{ {
AndroidNotificationTray getAndroidNotificationTray();
BaseSystem getBaseSystem(); BaseSystem getBaseSystem();
CommandRunner getCommandRunner(); CommandRunner getCommandRunner();

@ -20,8 +20,13 @@
package org.isoron.uhabits; package org.isoron.uhabits;
import org.isoron.uhabits.core.*; import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*; import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*; import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.notifications.*; import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.preferences.*; import org.isoron.uhabits.preferences.*;
@ -39,9 +44,22 @@ public class HabitsModule
@Provides @Provides
@AppScope @AppScope
public static NotificationTray getTray(AndroidNotificationTray tray) public static ReminderScheduler getReminderScheduler(IntentScheduler sys,
CommandRunner commandRunner,
HabitList habitList)
{ {
return tray; return new ReminderScheduler(commandRunner, habitList, sys);
}
@Provides
@AppScope
public static NotificationTray getTray(TaskRunner taskRunner,
CommandRunner commandRunner,
Preferences preferences,
AndroidNotificationTray screen)
{
return new NotificationTray(taskRunner, commandRunner, preferences,
screen);
} }
@Provides @Provides

@ -25,30 +25,51 @@ import android.os.*;
import android.support.annotation.*; import android.support.annotation.*;
import org.isoron.androidbase.*; import org.isoron.androidbase.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.*; import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.models.*;
import javax.inject.*; import javax.inject.*;
import static android.app.AlarmManager.*; import static android.app.AlarmManager.*;
import static android.content.Context.*; import static android.content.Context.*;
import static android.os.Build.VERSION_CODES.M; import static android.os.Build.VERSION_CODES.*;
@AppScope @AppScope
public class IntentScheduler public class IntentScheduler
implements org.isoron.uhabits.core.reminders.ReminderScheduler.SystemScheduler
{ {
private final AlarmManager manager; private final AlarmManager manager;
@NonNull
private final PendingIntentFactory pendingIntents;
private HabitLogger logger;
@Inject @Inject
public IntentScheduler(@AppContext Context context) public IntentScheduler(@AppContext Context context,
@NonNull PendingIntentFactory pendingIntents,
@NonNull HabitLogger logger)
{ {
manager = (AlarmManager) context.getSystemService(ALARM_SERVICE); manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
this.pendingIntents = pendingIntents;
this.logger = logger;
} }
public void schedule(@NonNull Long timestamp, PendingIntent intent) public void schedule(@NonNull Long timestamp, PendingIntent intent)
{ {
if (Build.VERSION.SDK_INT >= M) if (Build.VERSION.SDK_INT >= M)
manager.setExactAndAllowWhileIdle(RTC_WAKEUP, timestamp, intent); manager.setExactAndAllowWhileIdle(RTC_WAKEUP, timestamp, intent);
else else manager.setExact(RTC_WAKEUP, timestamp, intent);
manager.setExact(RTC_WAKEUP, timestamp, intent); }
@Override
public void scheduleShowReminder(long reminderTime,
@NonNull Habit habit,
long timestamp)
{
schedule(reminderTime,
pendingIntents.showReminder(habit, reminderTime, timestamp));
logger.logReminderScheduled(habit, reminderTime);
} }
} }

@ -29,177 +29,49 @@ import android.support.v4.app.NotificationCompat.*;
import org.isoron.androidbase.*; import org.isoron.androidbase.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.core.*; import org.isoron.uhabits.core.*;
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 org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*; import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*; import org.isoron.uhabits.intents.*;
import java.util.*;
import javax.inject.*; import javax.inject.*;
import static android.graphics.BitmapFactory.*; import static android.graphics.BitmapFactory.*;
import static org.isoron.uhabits.notifications.RingtoneManager.*; import static org.isoron.uhabits.notifications.RingtoneManager.*;
@AppScope @AppScope
public class AndroidNotificationTray public class AndroidNotificationTray implements NotificationTray.SystemTray
implements CommandRunner.Listener, Preferences.Listener,
NotificationTray
{ {
@NonNull @NonNull
private final Context context; private final Context context;
@NonNull
private final TaskRunner taskRunner;
@NonNull @NonNull
private final PendingIntentFactory pendingIntents; private final PendingIntentFactory pendingIntents;
@NonNull
private final CommandRunner commandRunner;
@NonNull @NonNull
private final Preferences preferences; private final Preferences preferences;
@NonNull
private final HashMap<Habit, NotificationData> active;
@Inject @Inject
public AndroidNotificationTray(@AppContext @NonNull Context context, public AndroidNotificationTray(@AppContext @NonNull Context context,
@NonNull TaskRunner taskRunner,
@NonNull PendingIntentFactory pendingIntents, @NonNull PendingIntentFactory pendingIntents,
@NonNull CommandRunner commandRunner,
@NonNull Preferences preferences) @NonNull Preferences preferences)
{ {
this.context = context; this.context = context;
this.taskRunner = taskRunner;
this.pendingIntents = pendingIntents; this.pendingIntents = pendingIntents;
this.commandRunner = commandRunner;
this.preferences = preferences; this.preferences = preferences;
this.active = new HashMap<>();
}
@Override
public void cancel(@NonNull Habit habit)
{
int notificationId = getNotificationId(habit);
NotificationManagerCompat.from(context).cancel(notificationId);
active.remove(habit);
}
@Override
public void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey)
{
if (command instanceof ToggleRepetitionCommand)
{
ToggleRepetitionCommand toggleCmd =
(ToggleRepetitionCommand) command;
Habit habit = toggleCmd.getHabit();
taskRunner.execute(() ->
{
if (habit.getCheckmarks().getTodayValue() !=
Checkmark.UNCHECKED) cancel(habit);
});
}
if (command instanceof DeleteHabitsCommand)
{
DeleteHabitsCommand deleteCommand = (DeleteHabitsCommand) command;
List<Habit> deleted = deleteCommand.getSelected();
for (Habit habit : deleted)
cancel(habit);
}
}
@Override
public void onNotificationsChanged()
{
reshowAll();
}
public void show(@NonNull Habit habit, long timestamp, long reminderTime)
{
NotificationData data = new NotificationData(timestamp, reminderTime);
active.put(habit, data);
taskRunner.execute(new ShowNotificationTask(habit, data));
}
public void startListening()
{
commandRunner.addListener(this);
preferences.addListener(this);
}
public void stopListening()
{
commandRunner.removeListener(this);
preferences.removeListener(this);
}
private int getNotificationId(Habit habit)
{
Long id = habit.getId();
if (id == null) return 0;
return (int) (id % Integer.MAX_VALUE);
}
private void reshowAll()
{
for (Habit habit : active.keySet())
{
NotificationData data = active.get(habit);
taskRunner.execute(new ShowNotificationTask(habit, data));
}
}
class NotificationData
{
public final long timestamp;
public final long reminderTime;
public NotificationData(long timestamp, long reminderTime)
{
this.timestamp = timestamp;
this.reminderTime = reminderTime;
}
}
private class ShowNotificationTask implements Task
{
int todayValue;
private final Habit habit;
private final long timestamp;
private final long reminderTime;
public ShowNotificationTask(Habit habit, NotificationData data)
{
this.habit = habit;
this.timestamp = data.timestamp;
this.reminderTime = data.reminderTime;
} }
@Override @Override
public void doInBackground() public void removeNotification(int id)
{ {
todayValue = habit.getCheckmarks().getTodayValue(); NotificationManagerCompat.from(context).cancel(id);
} }
@Override public void showNotification(@NonNull Habit habit,
public void onPostExecute() int notificationId,
long timestamp,
long reminderTime)
{ {
if (todayValue != Checkmark.UNCHECKED) return;
if (!shouldShowReminderToday()) return;
if (!habit.hasReminder()) return;
Action checkAction = new Action(R.drawable.ic_action_check, Action checkAction = new Action(R.drawable.ic_action_check,
context.getString(R.string.check), context.getString(R.string.check),
pendingIntents.addCheckmark(habit, timestamp)); pendingIntents.addCheckmark(habit, timestamp));
@ -238,19 +110,6 @@ public class AndroidNotificationTray
(NotificationManager) context.getSystemService( (NotificationManager) context.getSystemService(
Activity.NOTIFICATION_SERVICE); Activity.NOTIFICATION_SERVICE);
int notificationId = getNotificationId(habit);
notificationManager.notify(notificationId, notification); notificationManager.notify(notificationId, notification);
} }
private boolean shouldShowReminderToday()
{
if (!habit.hasReminder()) return false;
Reminder reminder = habit.getReminder();
boolean reminderDays[] = reminder.getDays().toArray();
int weekday = DateUtils.getWeekday(timestamp);
return reminderDays[weekday];
}
}
} }

@ -23,9 +23,9 @@ import android.support.annotation.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*; import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*; import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.utils.*;
import javax.inject.*; import javax.inject.*;
@ -36,13 +36,13 @@ public class ReminderController
private final ReminderScheduler reminderScheduler; private final ReminderScheduler reminderScheduler;
@NonNull @NonNull
private final AndroidNotificationTray notificationTray; private final NotificationTray notificationTray;
private Preferences preferences; private Preferences preferences;
@Inject @Inject
public ReminderController(@NonNull ReminderScheduler reminderScheduler, public ReminderController(@NonNull ReminderScheduler reminderScheduler,
@NonNull AndroidNotificationTray notificationTray, @NonNull NotificationTray notificationTray,
@NonNull Preferences preferences) @NonNull Preferences preferences)
{ {
this.reminderScheduler = reminderScheduler; this.reminderScheduler = reminderScheduler;

@ -22,9 +22,9 @@ package org.isoron.uhabits.receivers;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*; import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*; import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.utils.*;
import org.junit.*; import org.junit.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -36,7 +36,7 @@ public class ReminderControllerTest extends BaseAndroidUnitTest
private ReminderScheduler reminderScheduler; private ReminderScheduler reminderScheduler;
private AndroidNotificationTray notificationTray; private NotificationTray notificationTray;
private Preferences preferences; private Preferences preferences;
@ -46,7 +46,7 @@ public class ReminderControllerTest extends BaseAndroidUnitTest
super.setUp(); super.setUp();
reminderScheduler = mock(ReminderScheduler.class); reminderScheduler = mock(ReminderScheduler.class);
notificationTray = mock(AndroidNotificationTray.class); notificationTray = mock(NotificationTray.class);
preferences = mock(Preferences.class); preferences = mock(Preferences.class);
controller = new ReminderController(reminderScheduler, controller = new ReminderController(reminderScheduler,

@ -22,7 +22,7 @@ package org.isoron.uhabits.receivers;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
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.notifications.*; import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.ui.widgets.*; import org.isoron.uhabits.core.ui.widgets.*;
import org.isoron.uhabits.core.utils.*; import org.isoron.uhabits.core.utils.*;
import org.junit.*; import org.junit.*;
@ -42,7 +42,7 @@ public class WidgetControllerTest extends BaseAndroidUnitTest
private long today; private long today;
private AndroidNotificationTray notificationTray; private NotificationTray notificationTray;
@Override @Override
public void setUp() public void setUp()
@ -52,7 +52,7 @@ public class WidgetControllerTest extends BaseAndroidUnitTest
today = DateUtils.getStartOfToday(); today = DateUtils.getStartOfToday();
habit = fixtures.createEmptyHabit(); habit = fixtures.createEmptyHabit();
commandRunner = mock(CommandRunner.class); commandRunner = mock(CommandRunner.class);
notificationTray = mock(AndroidNotificationTray.class); notificationTray = mock(NotificationTray.class);
controller = new WidgetBehavior(commandRunner, notificationTray); controller = new WidgetBehavior(commandRunner, notificationTray);
} }

@ -17,17 +17,14 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.utils; package org.isoron.uhabits.core.reminders;
import android.app.*;
import android.support.annotation.*; import android.support.annotation.*;
import org.isoron.uhabits.*;
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.utils.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*; import java.util.*;
@ -38,28 +35,20 @@ import static org.isoron.uhabits.core.utils.DateUtils.*;
@AppScope @AppScope
public class ReminderScheduler implements CommandRunner.Listener public class ReminderScheduler implements CommandRunner.Listener
{ {
private final PendingIntentFactory pendingIntentFactory;
private final IntentScheduler intentScheduler;
private final HabitLogger logger;
private CommandRunner commandRunner; private CommandRunner commandRunner;
private HabitList habitList; private HabitList habitList;
private SystemScheduler sys;
@Inject @Inject
public ReminderScheduler(@NonNull PendingIntentFactory pendingIntentFactory, public ReminderScheduler(@NonNull CommandRunner commandRunner,
@NonNull IntentScheduler intentScheduler, @NonNull HabitList habitList,
@NonNull HabitLogger logger, @NonNull SystemScheduler sys)
@NonNull CommandRunner commandRunner,
@NonNull HabitList habitList)
{ {
this.pendingIntentFactory = pendingIntentFactory;
this.intentScheduler = intentScheduler;
this.logger = logger;
this.commandRunner = commandRunner; this.commandRunner = commandRunner;
this.habitList = habitList; this.habitList = habitList;
this.sys = sys;
} }
@Override @Override
@ -79,10 +68,7 @@ public class ReminderScheduler implements CommandRunner.Listener
if (reminderTime == null) reminderTime = getReminderTime(reminder); if (reminderTime == null) reminderTime = getReminderTime(reminder);
long timestamp = getStartOfDay(removeTimezone(reminderTime)); long timestamp = getStartOfDay(removeTimezone(reminderTime));
PendingIntent intent = sys.scheduleShowReminder(reminderTime, habit, timestamp);
pendingIntentFactory.showReminder(habit, reminderTime, timestamp);
intentScheduler.schedule(reminderTime, intent);
logger.logReminderScheduled(habit, reminderTime);
} }
public void scheduleAll() public void scheduleAll()
@ -112,8 +98,14 @@ public class ReminderScheduler implements CommandRunner.Listener
calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis(); Long time = calendar.getTimeInMillis();
if (DateUtils.getLocalTime() > time) time += AlarmManager.INTERVAL_DAY; if (DateUtils.getLocalTime() > time)
time += DateUtils.millisecondsInOneDay;
return applyTimezone(time); return applyTimezone(time);
} }
public interface SystemScheduler
{
void scheduleShowReminder(long reminderTime, Habit habit, long timestamp);
}
} }

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com> * Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
* *
* This file is part of Loop Habit Tracker. * This file is part of Loop Habit Tracker.
* *
@ -19,9 +19,190 @@
package org.isoron.uhabits.core.ui; package org.isoron.uhabits.core.ui;
import android.support.annotation.*;
import org.isoron.uhabits.core.*;
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 org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*;
import javax.inject.*;
@AppScope
public class NotificationTray
implements CommandRunner.Listener, Preferences.Listener
{
@NonNull
private final TaskRunner taskRunner;
@NonNull
private final CommandRunner commandRunner;
@NonNull
private final Preferences preferences;
private SystemTray systemTray;
@NonNull
private final HashMap<Habit, NotificationData> active;
@Inject
public NotificationTray(@NonNull TaskRunner taskRunner,
@NonNull CommandRunner commandRunner,
@NonNull Preferences preferences,
@NonNull SystemTray systemTray)
{
this.taskRunner = taskRunner;
this.commandRunner = commandRunner;
this.preferences = preferences;
this.systemTray = systemTray;
this.active = new HashMap<>();
}
public void cancel(@NonNull Habit habit)
{
int notificationId = getNotificationId(habit);
systemTray.removeNotification(notificationId);
active.remove(habit);
}
@Override
public void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey)
{
if (command instanceof ToggleRepetitionCommand)
{
ToggleRepetitionCommand toggleCmd =
(ToggleRepetitionCommand) command;
Habit habit = toggleCmd.getHabit();
taskRunner.execute(() ->
{
if (habit.getCheckmarks().getTodayValue() !=
Checkmark.UNCHECKED) cancel(habit);
});
}
if (command instanceof DeleteHabitsCommand)
{
DeleteHabitsCommand deleteCommand = (DeleteHabitsCommand) command;
List<Habit> deleted = deleteCommand.getSelected();
for (Habit habit : deleted)
cancel(habit);
}
}
public interface NotificationTray @Override
public void onNotificationsChanged()
{ {
void cancel(Habit habit); reshowAll();
}
public void show(@NonNull Habit habit, long timestamp, long reminderTime)
{
NotificationData data = new NotificationData(timestamp, reminderTime);
active.put(habit, data);
taskRunner.execute(new ShowNotificationTask(habit, data));
}
public void startListening()
{
commandRunner.addListener(this);
preferences.addListener(this);
}
public void stopListening()
{
commandRunner.removeListener(this);
preferences.removeListener(this);
}
private int getNotificationId(Habit habit)
{
Long id = habit.getId();
if (id == null) return 0;
return (int) (id % Integer.MAX_VALUE);
}
private void reshowAll()
{
for (Habit habit : active.keySet())
{
NotificationData data = active.get(habit);
taskRunner.execute(new ShowNotificationTask(habit, data));
}
}
public interface SystemTray
{
void removeNotification(int notificationId);
void showNotification(Habit habit,
int notificationId,
long timestamp,
long reminderTime);
}
class NotificationData
{
public final long timestamp;
public final long reminderTime;
public NotificationData(long timestamp, long reminderTime)
{
this.timestamp = timestamp;
this.reminderTime = reminderTime;
}
}
private class ShowNotificationTask implements Task
{
int todayValue;
private final Habit habit;
private final long timestamp;
private final long reminderTime;
public ShowNotificationTask(Habit habit, NotificationData data)
{
this.habit = habit;
this.timestamp = data.timestamp;
this.reminderTime = data.reminderTime;
}
@Override
public void doInBackground()
{
todayValue = habit.getCheckmarks().getTodayValue();
}
@Override
public void onPostExecute()
{
if (todayValue != Checkmark.UNCHECKED) return;
if (!shouldShowReminderToday()) return;
if (!habit.hasReminder()) return;
systemTray.showNotification(habit, getNotificationId(habit), timestamp,
reminderTime);
}
private boolean shouldShowReminderToday()
{
if (!habit.hasReminder()) return false;
Reminder reminder = habit.getReminder();
boolean reminderDays[] = reminder.getDays().toArray();
int weekday = DateUtils.getWeekday(timestamp);
return reminderDays[weekday];
}
}
} }

@ -63,6 +63,7 @@ public class BaseUnitTest
@After @After
public void tearDown() public void tearDown()
{ {
validateMockitoUsage();
DateUtils.setFixedLocalTime(null); DateUtils.setFixedLocalTime(null);
} }

@ -17,60 +17,68 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.utils; package org.isoron.uhabits.core.reminders;
import android.app.*;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*; import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*;
import org.junit.*; import org.junit.*;
import org.junit.runner.*; import org.junit.runner.*;
import org.mockito.*;
import org.mockito.junit.*;
import java.util.*; import java.util.*;
import static java.util.Arrays.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@RunWith(AndroidJUnit4.class) @RunWith(MockitoJUnitRunner.class)
@MediumTest public class ReminderSchedulerTest extends BaseUnitTest
public class ReminderSchedulerTest extends BaseAndroidTest
{ {
private Habit habit; private Habit habit;
private ReminderScheduler reminderScheduler; private ReminderScheduler reminderScheduler;
private HabitLogger logger; @Mock
private ReminderScheduler.SystemScheduler sys;
private PendingIntentFactory pendingIntentFactory;
private IntentScheduler intentScheduler;
private CommandRunner commandRunner;
@Before @Before
@Override @Override
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
logger = mock(HabitLogger.class); habit = fixtures.createEmptyHabit();
intentScheduler = mock(IntentScheduler.class);
commandRunner = mock(CommandRunner.class);
pendingIntentFactory =
new PendingIntentFactory(targetContext, new IntentFactory());
reminderScheduler = reminderScheduler =
new ReminderScheduler(pendingIntentFactory, intentScheduler, logger, new ReminderScheduler(commandRunner, habitList, sys);
commandRunner, habitList);
habit = fixtures.createEmptyHabit();
DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT-4")); DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT-4"));
} }
@Test
public void testScheduleAll()
{
long now = timestamp(2015, 1, 26, 13, 0);
DateUtils.setFixedLocalTime(now);
Habit h1 = fixtures.createEmptyHabit();
Habit h2 = fixtures.createEmptyHabit();
Habit h3 = fixtures.createEmptyHabit();
h1.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
h2.setReminder(new Reminder(18, 30, WeekdayList.EVERY_DAY));
h3.setReminder(null);
habitList.add(h1);
habitList.add(h2);
habitList.add(h3);
reminderScheduler.scheduleAll();
verify(sys).scheduleShowReminder(eq(timestamp(2015, 1, 27, 12, 30)),
eq(h1), anyLong());
verify(sys).scheduleShowReminder(eq(timestamp(2015, 1, 26, 22, 30)),
eq(h2), anyLong());
Mockito.verifyNoMoreInteractions(sys);
}
@Test @Test
public void testSchedule_atSpecificTime() public void testSchedule_atSpecificTime()
{ {
@ -91,32 +99,9 @@ public class ReminderSchedulerTest extends BaseAndroidTest
long expectedReminderTime = timestamp(2015, 1, 26, 12, 30); long expectedReminderTime = timestamp(2015, 1, 26, 12, 30);
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY)); habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime); scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime);
} }
@Test
public void testScheduleAll()
{
long now = timestamp(2015, 1, 26, 13, 0);
DateUtils.setFixedLocalTime(now);
fixtures.purgeHabits(habitList);
Habit h1 = fixtures.createEmptyHabit();
Habit h2 = fixtures.createEmptyHabit();
Habit h3 = fixtures.createEmptyHabit();
h1.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
h2.setReminder(new Reminder(18, 30, WeekdayList.EVERY_DAY));
h3.setReminder(null);
habitList.update(asList(h1, h2, h3));
reminderScheduler.scheduleAll();
verify(intentScheduler).schedule(eq(timestamp(2015, 1, 27, 12, 30)), any());
verify(intentScheduler).schedule(eq(timestamp(2015, 1, 26, 22, 30)), any());
verifyNoMoreInteractions(intentScheduler);
}
@Test @Test
public void testSchedule_tomorrow() public void testSchedule_tomorrow()
{ {
@ -134,7 +119,7 @@ public class ReminderSchedulerTest extends BaseAndroidTest
public void testSchedule_withoutReminder() public void testSchedule_withoutReminder()
{ {
reminderScheduler.schedule(habit, null); reminderScheduler.schedule(habit, null);
verifyZeroInteractions(intentScheduler); Mockito.verifyZeroInteractions(sys);
} }
public long timestamp(int year, int month, int day, int hour, int minute) public long timestamp(int year, int month, int day, int hour, int minute)
@ -148,13 +133,8 @@ public class ReminderSchedulerTest extends BaseAndroidTest
long expectedCheckmarkTime, long expectedCheckmarkTime,
long expectedReminderTime) long expectedReminderTime)
{ {
PendingIntent intent =
pendingIntentFactory.showReminder(habit, expectedReminderTime,
expectedCheckmarkTime);
reminderScheduler.schedule(habit, atTime); reminderScheduler.schedule(habit, atTime);
verify(sys).scheduleShowReminder(expectedReminderTime, habit,
verify(logger).logReminderScheduled(habit, expectedReminderTime); expectedCheckmarkTime);
verify(intentScheduler).schedule(expectedReminderTime, intent);
} }
} }
Loading…
Cancel
Save