From 4542fb1046e79f4e56501a2670de2bdc84181cd6 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 26 Sep 2017 12:14:15 -0500 Subject: [PATCH] Dont cancel notifications on application startup --- .../astrid/reminders/ReminderPreferences.java | 14 +++++++------- .../tasks/notifications/NotificationManager.java | 14 ++++++++------ .../org/tasks/scheduling/BackgroundScheduler.java | 2 +- .../NotificationSchedulerIntentService.java | 12 +++++++++--- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java b/app/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java index 71a80aad4..f8118e089 100644 --- a/app/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java +++ b/app/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java @@ -26,10 +26,8 @@ import org.tasks.preferences.ActivityPermissionRequestor; import org.tasks.preferences.Device; import org.tasks.preferences.PermissionChecker; import org.tasks.preferences.PermissionRequestor; -import org.tasks.preferences.Preferences; import org.tasks.scheduling.GeofenceSchedulingIntentService; import org.tasks.scheduling.NotificationSchedulerIntentService; -import org.tasks.themes.ThemeCache; import org.tasks.time.DateTime; import org.tasks.ui.TimePreference; @@ -50,8 +48,6 @@ public class ReminderPreferences extends InjectingPreferenceActivity { @Inject Device device; @Inject ActivityPermissionRequestor permissionRequestor; @Inject PermissionChecker permissionChecker; - @Inject Preferences preferences; - @Inject ThemeCache themeCache; private CheckBoxPreference fieldMissedCalls; @@ -66,8 +62,7 @@ public class ReminderPreferences extends InjectingPreferenceActivity { R.string.p_rmd_enable_quiet, R.string.p_rmd_quietStart, R.string.p_rmd_quietEnd, - R.string.p_rmd_persistent, - R.string.p_bundle_notifications); + R.string.p_rmd_persistent); resetGeofencesOnChange( R.string.p_geofence_radius, R.string.p_geofence_responsiveness); @@ -84,6 +79,11 @@ public class ReminderPreferences extends InjectingPreferenceActivity { findPreference(R.string.notification_channel_settings).setOnPreferenceClickListener(this::openNotificationChannelSettings); findPreference(R.string.battery_optimization_settings).setOnPreferenceClickListener(this::openBatteryOptimizationSettings); + findPreference(R.string.p_bundle_notifications).setOnPreferenceChangeListener((preference, o) -> { + NotificationSchedulerIntentService.enqueueWork(this, true); + return true; + }); + requires(device.supportsLocationServices(), R.string.geolocation_reminders); requires(atLeastOreo(), R.string.notification_channel_settings); requires(atLeastMarshmallow(), R.string.battery_optimization_settings); @@ -110,7 +110,7 @@ public class ReminderPreferences extends InjectingPreferenceActivity { private void rescheduleNotificationsOnChange(int... resIds) { for (int resId : resIds) { findPreference(getString(resId)).setOnPreferenceChangeListener((preference, newValue) -> { - NotificationSchedulerIntentService.enqueueWork(this); + NotificationSchedulerIntentService.enqueueWork(this, false); return true; }); } diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.java b/app/src/main/java/org/tasks/notifications/NotificationManager.java index 8c556c28d..aa8bd114f 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.java +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.java @@ -135,19 +135,21 @@ public class NotificationManager { .subscribe(); } - public void restoreNotifications() { + public void restoreNotifications(boolean cancelExisting) { List notifications = notificationDao.getAllOrdered(); - for (org.tasks.notifications.Notification notification : notifications) { - notificationManagerCompat.cancel(notification.taskId.intValue()); + if (cancelExisting) { + for (org.tasks.notifications.Notification notification : notifications) { + notificationManagerCompat.cancel(notification.taskId.intValue()); + } } if (preferences.bundleNotifications() && notifications.size() > 1) { updateSummary(false, false, false, Collections.emptyList()); + createNotifications(notifications, false, false, false, true); } else { + createNotifications(notifications, false, false, false, false); notificationManagerCompat.cancel(SUMMARY_NOTIFICATION_ID); } - - createNotifications(notifications, false, false, false, true); } public void notifyTasks(List newNotifications, boolean alert, boolean nonstop, boolean fiveTimes) { @@ -183,6 +185,7 @@ public class NotificationManager { for (org.tasks.notifications.Notification notification : notifications) { NotificationCompat.Builder builder = getTaskNotification(notification); if (builder == null) { + notificationManagerCompat.cancel(notification.taskId.intValue()); notificationDao.delete(notification.taskId); } else { builder.setGroup(useGroupKey ? GROUP_KEY : notification.taskId.toString()) @@ -267,7 +270,6 @@ public class NotificationManager { .setGroupSummary(true) .setGroup(GROUP_KEY) .setTicker(Joiner.on(", ").join(ticker)) - .setPriority(NotificationCompat.PRIORITY_HIGH) .setGroupAlertBehavior(notify ? NotificationCompat.GROUP_ALERT_SUMMARY : NotificationCompat.GROUP_ALERT_CHILDREN); Intent snoozeIntent = new Intent(context, SnoozeActivity.class); diff --git a/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java b/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java index 0204b5022..7953f2353 100644 --- a/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java +++ b/app/src/main/java/org/tasks/scheduling/BackgroundScheduler.java @@ -36,7 +36,7 @@ public class BackgroundScheduler extends InjectingJobIntentService { Timber.d("onHandleWork(%s)", intent); - NotificationSchedulerIntentService.enqueueWork(context); + NotificationSchedulerIntentService.enqueueWork(context, false); CalendarNotificationIntentService.enqueueWork(context); GeofenceSchedulingIntentService.enqueueWork(context); diff --git a/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java b/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java index fe0d13974..4b7a02e63 100644 --- a/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java +++ b/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java @@ -20,8 +20,12 @@ import timber.log.Timber; public class NotificationSchedulerIntentService extends InjectingJobIntentService { - public static void enqueueWork(Context context) { - JobIntentService.enqueueWork(context, NotificationSchedulerIntentService.class, JobManager.JOB_ID_NOTIFICATION_SCHEDULER, new Intent()); + public static final String EXTRA_CANCEL_EXISTING_NOTIFICATIONS = "extra_cancel_existing_notifications"; + + public static void enqueueWork(Context context, boolean cancelNotifications) { + Intent intent = new Intent(); + intent.putExtra(EXTRA_CANCEL_EXISTING_NOTIFICATIONS, cancelNotifications); + JobIntentService.enqueueWork(context, NotificationSchedulerIntentService.class, JobManager.JOB_ID_NOTIFICATION_SCHEDULER, intent); } @Inject AlarmService alarmService; @@ -38,7 +42,9 @@ public class NotificationSchedulerIntentService extends InjectingJobIntentServic jobQueue.clear(); - notificationManager.restoreNotifications(); + boolean cancelExistingNotifications = intent.getBooleanExtra(EXTRA_CANCEL_EXISTING_NOTIFICATIONS, false); + + notificationManager.restoreNotifications(cancelExistingNotifications); reminderService.scheduleAllAlarms(taskDao); alarmService.scheduleAllAlarms(); }