From 1a89bb02bea0c12a9cb1e9d79f2922bcbaa3923c Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 11 Sep 2016 10:40:43 -0400 Subject: [PATCH] Add option to make notifications sticky --- .../uhabits/intents/PendingIntentFactory.java | 6 +- .../notifications/NotificationTray.java | 73 +++++++++++++++---- .../uhabits/preferences/Preferences.java | 12 +++ .../uhabits/receivers/ReminderController.java | 2 +- app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/preferences.xml | 6 ++ 6 files changed, 83 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/isoron/uhabits/intents/PendingIntentFactory.java b/app/src/main/java/org/isoron/uhabits/intents/PendingIntentFactory.java index 1d077b4ae..415f9237a 100644 --- a/app/src/main/java/org/isoron/uhabits/intents/PendingIntentFactory.java +++ b/app/src/main/java/org/isoron/uhabits/intents/PendingIntentFactory.java @@ -50,19 +50,19 @@ public class PendingIntentFactory public PendingIntent addCheckmark(@NonNull Habit habit, @Nullable Long timestamp) { - Uri data = habit.getUri(); Intent checkIntent = new Intent(context, WidgetReceiver.class); - checkIntent.setData(data); + checkIntent.setData(habit.getUri()); checkIntent.setAction(WidgetReceiver.ACTION_ADD_REPETITION); if (timestamp != null) checkIntent.putExtra("timestamp", timestamp); return PendingIntent.getBroadcast(context, 1, checkIntent, FLAG_UPDATE_CURRENT); } - public PendingIntent dismissNotification() + public PendingIntent dismissNotification(@NonNull Habit habit) { Intent deleteIntent = new Intent(context, ReminderReceiver.class); deleteIntent.setAction(WidgetReceiver.ACTION_DISMISS_REMINDER); + deleteIntent.setData(habit.getUri()); return PendingIntent.getBroadcast(context, 0, deleteIntent, FLAG_UPDATE_CURRENT); } diff --git a/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java b/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java index b0ae806fc..9ad698e51 100644 --- a/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java +++ b/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java @@ -29,16 +29,20 @@ import org.isoron.uhabits.*; import org.isoron.uhabits.commands.*; import org.isoron.uhabits.intents.*; import org.isoron.uhabits.models.*; +import org.isoron.uhabits.preferences.*; import org.isoron.uhabits.tasks.*; import org.isoron.uhabits.utils.*; +import java.util.*; + import javax.inject.*; import static android.graphics.BitmapFactory.*; import static org.isoron.uhabits.utils.RingtoneUtils.*; @AppScope -public class NotificationTray implements CommandRunner.Listener +public class NotificationTray + implements CommandRunner.Listener, Preferences.Listener { @NonNull private final Context context; @@ -49,55 +53,74 @@ public class NotificationTray implements CommandRunner.Listener @NonNull private final PendingIntentFactory pendingIntents; - private CommandRunner commandRunner; + @NonNull + private final CommandRunner commandRunner; + + @NonNull + private final Preferences preferences; + + @NonNull + private final HashMap active; @Inject public NotificationTray(@AppContext @NonNull Context context, @NonNull TaskRunner taskRunner, @NonNull PendingIntentFactory pendingIntents, - @NonNull CommandRunner commandRunner) + @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<>(); } 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)) - return; + if (!(command instanceof ToggleRepetitionCommand)) return; ToggleRepetitionCommand toggleCommand = (ToggleRepetitionCommand) command; Habit habit = toggleCommand.getHabit(); - if(habit.getCheckmarks().getTodayValue() != Checkmark.UNCHECKED) + if (habit.getCheckmarks().getTodayValue() != Checkmark.UNCHECKED) cancel(habit); } + @Override + public void onNotificationsChanged() + { + reshowAll(); + } + public void show(@NonNull Habit habit, long timestamp, long reminderTime) { - taskRunner.execute( - new ShowNotificationTask(habit, timestamp, 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) @@ -107,6 +130,28 @@ public class NotificationTray implements CommandRunner.Listener 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; @@ -117,13 +162,11 @@ public class NotificationTray implements CommandRunner.Listener private final long reminderTime; - public ShowNotificationTask(Habit habit, - long timestamp, - long reminderTime) + public ShowNotificationTask(Habit habit, NotificationData data) { this.habit = habit; - this.timestamp = timestamp; - this.reminderTime = reminderTime; + this.timestamp = data.timestamp; + this.reminderTime = data.reminderTime; } @Override @@ -148,7 +191,7 @@ public class NotificationTray implements CommandRunner.Listener .setContentTitle(habit.getName()) .setContentText(habit.getDescription()) .setContentIntent(pendingIntents.showHabit(habit)) - .setDeleteIntent(pendingIntents.dismissNotification()) + .setDeleteIntent(pendingIntents.dismissNotification(habit)) .addAction(R.drawable.ic_action_check, context.getString(R.string.check), pendingIntents.addCheckmark(habit, timestamp)) @@ -159,6 +202,7 @@ public class NotificationTray implements CommandRunner.Listener .extend(wearableExtender) .setWhen(reminderTime) .setShowWhen(true) + .setOngoing(preferences.shouldMakeNotificationsSticky()) .build(); NotificationManager notificationManager = @@ -179,6 +223,5 @@ public class NotificationTray implements CommandRunner.Listener return reminderDays[weekday]; } - } } diff --git a/app/src/main/java/org/isoron/uhabits/preferences/Preferences.java b/app/src/main/java/org/isoron/uhabits/preferences/Preferences.java index 9b5b7ea09..86e5e5278 100644 --- a/app/src/main/java/org/isoron/uhabits/preferences/Preferences.java +++ b/app/src/main/java/org/isoron/uhabits/preferences/Preferences.java @@ -169,6 +169,11 @@ public class Preferences shouldReverseCheckmarks = null; for(Listener l : listeners) l.onCheckmarkOrderChanged(); } + + if(key.equals("pref_sticky_notifications")) + { + for(Listener l : listeners) l.onNotificationsChanged(); + } } public void removeListener(Listener listener) @@ -200,6 +205,11 @@ public class Preferences return shouldReverseCheckmarks; } + public boolean shouldMakeNotificationsSticky() + { + return prefs.getBoolean("pref_sticky_notifications", false); + } + public void updateLastAppVersion() { prefs.edit().putInt("last_version", BuildConfig.VERSION_CODE).apply(); @@ -223,5 +233,7 @@ public class Preferences public interface Listener { default void onCheckmarkOrderChanged() {} + + default void onNotificationsChanged() {} } } diff --git a/app/src/main/java/org/isoron/uhabits/receivers/ReminderController.java b/app/src/main/java/org/isoron/uhabits/receivers/ReminderController.java index 64d02d425..22fd4bcd0 100644 --- a/app/src/main/java/org/isoron/uhabits/receivers/ReminderController.java +++ b/app/src/main/java/org/isoron/uhabits/receivers/ReminderController.java @@ -75,6 +75,6 @@ public class ReminderController public void onDismiss(@NonNull Habit habit) { - // nop + notificationTray.cancel(habit); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7aa0c5978..5c42c0963 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -185,4 +185,7 @@ Filter Hide completed Hide archived + + Make notifications sticky + Prevents notifications from being swiped away. \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 330b9680f..16b874cc2 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -62,6 +62,12 @@ android:key="reminderSound" android:title="@string/reminder_sound"/> + +