mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 17:48:52 -06:00
Move notifications and reminders to uhabits-core
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user