Move notifications and reminders to uhabits-core

This commit is contained in:
2017-06-02 19:30:39 -04:00
parent b88b3a683d
commit 6875fc0428
12 changed files with 341 additions and 290 deletions

View File

@@ -1,160 +0,0 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.utils;
import android.app.*;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*;
import org.junit.*;
import org.junit.runner.*;
import java.util.*;
import static java.util.Arrays.*;
import static org.mockito.Mockito.*;
@RunWith(AndroidJUnit4.class)
@MediumTest
public class ReminderSchedulerTest extends BaseAndroidTest
{
private Habit habit;
private ReminderScheduler reminderScheduler;
private HabitLogger logger;
private PendingIntentFactory pendingIntentFactory;
private IntentScheduler intentScheduler;
private CommandRunner commandRunner;
@Before
@Override
public void setUp()
{
super.setUp();
logger = mock(HabitLogger.class);
intentScheduler = mock(IntentScheduler.class);
commandRunner = mock(CommandRunner.class);
pendingIntentFactory =
new PendingIntentFactory(targetContext, new IntentFactory());
reminderScheduler =
new ReminderScheduler(pendingIntentFactory, intentScheduler, logger,
commandRunner, habitList);
habit = fixtures.createEmptyHabit();
DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT-4"));
}
@Test
public void testSchedule_atSpecificTime()
{
long atTime = timestamp(2015, 1, 30, 11, 30);
long expectedCheckmarkTime = timestamp(2015, 1, 30, 0, 0);
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
scheduleAndVerify(atTime, expectedCheckmarkTime, atTime);
}
@Test
public void testSchedule_laterToday()
{
long now = timestamp(2015, 1, 26, 6, 30);
DateUtils.setFixedLocalTime(now);
long expectedCheckmarkTime = timestamp(2015, 1, 26, 0, 0);
long expectedReminderTime = timestamp(2015, 1, 26, 12, 30);
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
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
public void testSchedule_tomorrow()
{
long now = timestamp(2015, 1, 26, 13, 0);
DateUtils.setFixedLocalTime(now);
long expectedCheckmarkTime = timestamp(2015, 1, 27, 0, 0);
long expectedReminderTime = timestamp(2015, 1, 27, 12, 30);
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
scheduleAndVerify(null, expectedCheckmarkTime, expectedReminderTime);
}
@Test
public void testSchedule_withoutReminder()
{
reminderScheduler.schedule(habit, null);
verifyZeroInteractions(intentScheduler);
}
public long timestamp(int year, int month, int day, int hour, int minute)
{
Calendar cal = DateUtils.getStartOfTodayCalendar();
cal.set(year, month, day, hour, minute);
return cal.getTimeInMillis();
}
private void scheduleAndVerify(Long atTime,
long expectedCheckmarkTime,
long expectedReminderTime)
{
PendingIntent intent =
pendingIntentFactory.showReminder(habit, expectedReminderTime,
expectedCheckmarkTime);
reminderScheduler.schedule(habit, atTime);
verify(logger).logReminderScheduled(habit, expectedReminderTime);
verify(intentScheduler).schedule(expectedReminderTime, intent);
}
}

View File

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

View File

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

View File

@@ -20,8 +20,13 @@
package org.isoron.uhabits;
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.reminders.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.preferences.*;
@@ -39,9 +44,22 @@ public class HabitsModule
@Provides
@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

View File

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

View File

@@ -29,228 +29,87 @@ import android.support.v4.app.NotificationCompat.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.*;
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.tasks.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*;
import java.util.*;
import javax.inject.*;
import static android.graphics.BitmapFactory.*;
import static org.isoron.uhabits.notifications.RingtoneManager.*;
@AppScope
public class AndroidNotificationTray
implements CommandRunner.Listener, Preferences.Listener,
NotificationTray
public class AndroidNotificationTray implements NotificationTray.SystemTray
{
@NonNull
private final Context context;
@NonNull
private final TaskRunner taskRunner;
@NonNull
private final PendingIntentFactory pendingIntents;
@NonNull
private final CommandRunner commandRunner;
@NonNull
private final Preferences preferences;
@NonNull
private final HashMap<Habit, NotificationData> active;
@Inject
public AndroidNotificationTray(@AppContext @NonNull Context context,
@NonNull TaskRunner taskRunner,
@NonNull PendingIntentFactory pendingIntents,
@NonNull CommandRunner commandRunner,
@NonNull Preferences preferences)
{
this.context = context;
this.taskRunner = taskRunner;
this.pendingIntents = pendingIntents;
this.commandRunner = commandRunner;
this.preferences = preferences;
this.active = new HashMap<>();
}
@Override
public void cancel(@NonNull Habit habit)
public void removeNotification(int id)
{
int notificationId = getNotificationId(habit);
NotificationManagerCompat.from(context).cancel(notificationId);
active.remove(habit);
NotificationManagerCompat.from(context).cancel(id);
}
@Override
public void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey)
public void showNotification(@NonNull Habit habit,
int notificationId,
long timestamp,
long reminderTime)
{
if (command instanceof ToggleRepetitionCommand)
{
ToggleRepetitionCommand toggleCmd =
(ToggleRepetitionCommand) command;
Action checkAction = new Action(R.drawable.ic_action_check,
context.getString(R.string.check),
pendingIntents.addCheckmark(habit, timestamp));
Habit habit = toggleCmd.getHabit();
taskRunner.execute(() ->
{
if (habit.getCheckmarks().getTodayValue() !=
Checkmark.UNCHECKED) cancel(habit);
});
}
Action snoozeAction = new Action(R.drawable.ic_action_snooze,
context.getString(R.string.snooze),
pendingIntents.snoozeNotification(habit));
if (command instanceof DeleteHabitsCommand)
{
DeleteHabitsCommand deleteCommand = (DeleteHabitsCommand) command;
List<Habit> deleted = deleteCommand.getSelected();
for (Habit habit : deleted)
cancel(habit);
}
}
Bitmap wearableBg =
decodeResource(context.getResources(), R.drawable.stripe);
@Override
public void onNotificationsChanged()
{
reshowAll();
}
// Even though the set of actions is the same on the phone and
// on the watch, Pebble requires us to add them to the
// WearableExtender.
WearableExtender wearableExtender = new WearableExtender()
.setBackground(wearableBg)
.addAction(checkAction)
.addAction(snoozeAction);
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));
}
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(habit.getName())
.setContentText(habit.getDescription())
.setContentIntent(pendingIntents.showHabit(habit))
.setDeleteIntent(pendingIntents.dismissNotification(habit))
.addAction(checkAction)
.addAction(snoozeAction)
.setSound(getRingtoneUri(context))
.extend(wearableExtender)
.setWhen(reminderTime)
.setShowWhen(true)
.setOngoing(preferences.shouldMakeNotificationsSticky())
.build();
public void startListening()
{
commandRunner.addListener(this);
preferences.addListener(this);
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(
Activity.NOTIFICATION_SERVICE);
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
public void doInBackground()
{
todayValue = habit.getCheckmarks().getTodayValue();
}
@Override
public void onPostExecute()
{
if (todayValue != Checkmark.UNCHECKED) return;
if (!shouldShowReminderToday()) return;
if (!habit.hasReminder()) return;
Action checkAction = new Action(R.drawable.ic_action_check,
context.getString(R.string.check),
pendingIntents.addCheckmark(habit, timestamp));
Action snoozeAction = new Action(R.drawable.ic_action_snooze,
context.getString(R.string.snooze),
pendingIntents.snoozeNotification(habit));
Bitmap wearableBg =
decodeResource(context.getResources(), R.drawable.stripe);
// Even though the set of actions is the same on the phone and
// on the watch, Pebble requires us to add them to the
// WearableExtender.
WearableExtender wearableExtender = new WearableExtender()
.setBackground(wearableBg)
.addAction(checkAction)
.addAction(snoozeAction);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(habit.getName())
.setContentText(habit.getDescription())
.setContentIntent(pendingIntents.showHabit(habit))
.setDeleteIntent(pendingIntents.dismissNotification(habit))
.addAction(checkAction)
.addAction(snoozeAction)
.setSound(getRingtoneUri(context))
.extend(wearableExtender)
.setWhen(reminderTime)
.setShowWhen(true)
.setOngoing(preferences.shouldMakeNotificationsSticky())
.build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(
Activity.NOTIFICATION_SERVICE);
int notificationId = getNotificationId(habit);
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];
}
notificationManager.notify(notificationId, notification);
}
}

View File

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

View File

@@ -1,119 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.utils;
import android.app.*;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.core.models.*;
import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.utils.DateUtils.*;
@AppScope
public class ReminderScheduler implements CommandRunner.Listener
{
private final PendingIntentFactory pendingIntentFactory;
private final IntentScheduler intentScheduler;
private final HabitLogger logger;
private CommandRunner commandRunner;
private HabitList habitList;
@Inject
public ReminderScheduler(@NonNull PendingIntentFactory pendingIntentFactory,
@NonNull IntentScheduler intentScheduler,
@NonNull HabitLogger logger,
@NonNull CommandRunner commandRunner,
@NonNull HabitList habitList)
{
this.pendingIntentFactory = pendingIntentFactory;
this.intentScheduler = intentScheduler;
this.logger = logger;
this.commandRunner = commandRunner;
this.habitList = habitList;
}
@Override
public void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey)
{
if(command instanceof ToggleRepetitionCommand) return;
if(command instanceof ChangeHabitColorCommand) return;
scheduleAll();
}
public void schedule(@NonNull Habit habit, @Nullable Long reminderTime)
{
if (!habit.hasReminder()) return;
if (habit.isArchived()) return;
Reminder reminder = habit.getReminder();
if (reminderTime == null) reminderTime = getReminderTime(reminder);
long timestamp = getStartOfDay(removeTimezone(reminderTime));
PendingIntent intent =
pendingIntentFactory.showReminder(habit, reminderTime, timestamp);
intentScheduler.schedule(reminderTime, intent);
logger.logReminderScheduled(habit, reminderTime);
}
public void scheduleAll()
{
HabitList reminderHabits =
habitList.getFiltered(HabitMatcher.WITH_ALARM);
for (Habit habit : reminderHabits)
schedule(habit, null);
}
public void startListening()
{
commandRunner.addListener(this);
}
public void stopListening()
{
commandRunner.removeListener(this);
}
@NonNull
private Long getReminderTime(@NonNull Reminder reminder)
{
Calendar calendar = DateUtils.getStartOfTodayCalendar();
calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
calendar.set(Calendar.MINUTE, reminder.getMinute());
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
if (DateUtils.getLocalTime() > time) time += AlarmManager.INTERVAL_DAY;
return applyTimezone(time);
}
}

View File

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

View File

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