From ef62f3851e83013ace2dd91be3b31b72f5598c3f Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 11 Jun 2019 14:04:49 -0500 Subject: [PATCH] Fix notification manager cancellation --- .../tasks/notifications/NotificationDao.java | 6 +-- .../notifications/NotificationManager.java | 46 ++++++++----------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/tasks/notifications/NotificationDao.java b/app/src/main/java/org/tasks/notifications/NotificationDao.java index 34f41fef4..a99a16f72 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationDao.java +++ b/app/src/main/java/org/tasks/notifications/NotificationDao.java @@ -9,8 +9,8 @@ import java.util.List; @Dao public interface NotificationDao { - @Query("SELECT * FROM notification") - List getAll(); + @Query("SELECT task FROM notification") + List getAll(); @Query("SELECT * FROM notification ORDER BY timestamp DESC") List getAllOrdered(); @@ -22,7 +22,7 @@ public interface NotificationDao { int delete(long taskId); @Query("DELETE FROM notification WHERE task IN(:taskIds)") - int deleteAll(List taskIds); + void deleteAll(List taskIds); @Query("SELECT MAX(timestamp) FROM notification") long latestTimestamp(); diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.java b/app/src/main/java/org/tasks/notifications/NotificationManager.java index d69e7a0c9..78820d225 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.java +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.java @@ -9,7 +9,10 @@ import static com.google.common.collect.Lists.transform; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastNougat; import static com.todoroo.astrid.reminders.ReminderService.TYPE_GEOFENCE_ENTER; import static com.todoroo.astrid.reminders.ReminderService.TYPE_GEOFENCE_EXIT; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; +import android.annotation.SuppressLint; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; @@ -26,6 +29,7 @@ import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.reminders.ReminderService; import io.reactivex.Completable; +import io.reactivex.Single; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import java.util.ArrayList; @@ -87,40 +91,30 @@ public class NotificationManager { notificationManagerCompat = NotificationManagerCompat.from(context); } + @SuppressLint("CheckResult") public void cancel(long id) { - notificationManagerCompat.cancel((int) id); - queue.remove(id); - Completable.fromAction( - () -> { - if (id == SUMMARY_NOTIFICATION_ID) { - List tasks = transform(notificationDao.getAll(), n -> n.taskId); - for (Long task : tasks) { - notificationManagerCompat.cancel(task.intValue()); - } - notificationDao.deleteAll(tasks); - } else if (notificationDao.delete(id) > 0) { - notifyTasks(Collections.emptyList(), false, false, false); - } - }) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(); + if (id == SUMMARY_NOTIFICATION_ID) { + //noinspection ResultOfMethodCallIgnored + Single.fromCallable(() -> concat(notificationDao.getAll(), singletonList(id))) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::cancel); + } else { + cancel(singletonList(id)); + } } - public void cancel(List ids) { + @SuppressLint("CheckResult") + public void cancel(Iterable ids) { for (Long id : ids) { notificationManagerCompat.cancel(id.intValue()); queue.remove(id); } - Completable.fromAction( - () -> { - if (notificationDao.deleteAll(ids) > 0) { - notifyTasks(Collections.emptyList(), false, false, false); - } - }) - .observeOn(AndroidSchedulers.mainThread()) + + //noinspection ResultOfMethodCallIgnored + Completable.fromAction(() -> notificationDao.deleteAll(newArrayList(ids))) .subscribeOn(Schedulers.io()) - .subscribe(); + .subscribe(() -> notifyTasks(emptyList(), false, false, false)); } public void restoreNotifications(boolean cancelExisting) {