From 07a2eda5ea2c90006ff91f15cb3c915d8dfc1ca8 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 17 May 2024 03:00:04 -0500 Subject: [PATCH] Cancel notifications in TaskCompleter --- app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt | 8 +++----- .../java/com/todoroo/astrid/service/TaskCompleter.kt | 7 ++++--- .../java/com/todoroo/astrid/service/TaskDeleter.kt | 10 ++++------ .../main/java/com/todoroo/astrid/timers/TimerPlugin.kt | 2 +- .../org/tasks/notifications/NotificationManager.kt | 9 +++++++-- .../notifications/ThrottledNotificationManager.kt | 5 +++++ 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt index c0f1ca502..14c03c656 100644 --- a/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt +++ b/app/src/main/java/com/todoroo/astrid/dao/TaskDao.kt @@ -8,14 +8,14 @@ package com.todoroo.astrid.dao import com.todoroo.astrid.api.Filter import com.todoroo.astrid.timers.TimerPlugin import org.tasks.LocalBroadcastManager -import org.tasks.data.entity.Task import org.tasks.data.TaskContainer import org.tasks.data.dao.TaskDao +import org.tasks.data.db.SuspendDbUtils.eachChunk +import org.tasks.data.entity.Task import org.tasks.data.fetchFiltered import org.tasks.data.fetchTasks import org.tasks.data.setCollapsed import org.tasks.date.DateTimeUtils.isAfterNow -import org.tasks.data.db.SuspendDbUtils.eachChunk import org.tasks.jobs.WorkManager import org.tasks.location.GeofenceApi import org.tasks.notifications.NotificationManager @@ -108,12 +108,10 @@ class TaskDao @Inject constructor( val completionDateModified = task.completionDate != (original?.completionDate ?: 0) val deletionDateModified = task.deletionDate != (original?.deletionDate ?: 0) val justCompleted = completionDateModified && task.isCompleted - val justDeleted = deletionDateModified && task.isDeleted if (task.calendarURI?.isNotBlank() == true) { workManager.updateCalendar(task) } - if (justCompleted || justDeleted) { - notificationManager.cancel(task.id) + if (justCompleted) { if (task.timerStart > 0) { timerPlugin.stopTimer(task) } diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt b/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt index 684daafbe..687659351 100644 --- a/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt +++ b/app/src/main/java/com/todoroo/astrid/service/TaskCompleter.kt @@ -1,20 +1,20 @@ package com.todoroo.astrid.service -import android.app.NotificationManager import android.app.NotificationManager.INTERRUPTION_FILTER_ALL import android.content.Context import android.media.AudioAttributes import android.media.AudioAttributes.USAGE_NOTIFICATION_EVENT import android.media.RingtoneManager import androidx.room.withTransaction -import org.tasks.data.db.Database import com.todoroo.astrid.dao.TaskDao -import org.tasks.data.entity.Task import com.todoroo.astrid.gcal.GCalHelper import com.todoroo.astrid.repeats.RepeatTaskHelper import dagger.hilt.android.qualifiers.ApplicationContext import org.tasks.LocalBroadcastManager import org.tasks.data.dao.CaldavDao +import org.tasks.data.db.Database +import org.tasks.data.entity.Task +import org.tasks.notifications.NotificationManager import org.tasks.preferences.Preferences import org.tasks.time.DateTimeUtils2.currentTimeMillis import timber.log.Timber @@ -64,6 +64,7 @@ class TaskCompleter @Inject internal constructor( if (tasks.isEmpty()) { return } + tasks.forEach { notificationManager.cancel(it.id) } val completed = completionDate > 0 val modified = currentTimeMillis() database.withTransaction { diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskDeleter.kt b/app/src/main/java/com/todoroo/astrid/service/TaskDeleter.kt index b61ab42c2..9d96d588c 100644 --- a/app/src/main/java/com/todoroo/astrid/service/TaskDeleter.kt +++ b/app/src/main/java/com/todoroo/astrid/service/TaskDeleter.kt @@ -2,22 +2,21 @@ package com.todoroo.astrid.service import android.content.Context import androidx.room.withTransaction -import org.tasks.data.db.Database -import org.tasks.data.entity.Task -import com.todoroo.astrid.timers.TimerPlugin import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.withContext import org.tasks.BuildConfig import org.tasks.LocalBroadcastManager import org.tasks.caldav.VtodoCache -import org.tasks.data.entity.CaldavAccount -import org.tasks.data.entity.CaldavCalendar import org.tasks.data.dao.DeletionDao import org.tasks.data.dao.LocationDao import org.tasks.data.dao.TaskDao import org.tasks.data.dao.UserActivityDao +import org.tasks.data.db.Database import org.tasks.data.db.SuspendDbUtils.chunkedMap +import org.tasks.data.entity.CaldavAccount +import org.tasks.data.entity.CaldavCalendar +import org.tasks.data.entity.Task import org.tasks.files.FileHelper import org.tasks.location.GeofenceApi import org.tasks.notifications.NotificationManager @@ -34,7 +33,6 @@ class TaskDeleter @Inject constructor( private val vtodoCache: VtodoCache, private val notificationManager: NotificationManager, private val geofenceApi: GeofenceApi, - private val timerPlugin: TimerPlugin, private val userActivityDao: UserActivityDao, private val locationDao: LocationDao, ) { diff --git a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt index 91634182f..2535262c1 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.kt @@ -5,8 +5,8 @@ */ package com.todoroo.astrid.timers -import org.tasks.data.entity.Task import org.tasks.data.dao.TaskDao +import org.tasks.data.entity.Task import org.tasks.notifications.NotificationManager import org.tasks.time.DateTimeUtils2.currentTimeMillis import javax.inject.Inject diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.kt b/app/src/main/java/org/tasks/notifications/NotificationManager.kt index 346392890..95626c992 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.kt +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.kt @@ -5,6 +5,7 @@ import android.app.PendingIntent import android.content.Context import android.content.Intent import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat.InterruptionFilter import com.todoroo.andlib.utility.AndroidUtilities import com.todoroo.andlib.utility.AndroidUtilities.preUpsideDownCake import com.todoroo.astrid.core.BuiltInFilterExposer @@ -12,11 +13,11 @@ import com.todoroo.astrid.utility.Constants import dagger.hilt.android.qualifiers.ApplicationContext import org.tasks.LocalBroadcastManager import org.tasks.R -import org.tasks.data.entity.Alarm import org.tasks.data.dao.LocationDao -import org.tasks.data.entity.Notification import org.tasks.data.dao.NotificationDao import org.tasks.data.dao.TaskDao +import org.tasks.data.entity.Alarm +import org.tasks.data.entity.Notification import org.tasks.intents.TaskIntents import org.tasks.markdown.MarkdownProvider import org.tasks.preferences.PermissionChecker @@ -45,6 +46,10 @@ class NotificationManager @Inject constructor( private val markdownProvider: MarkdownProvider, private val permissionChecker: PermissionChecker, ) { + @InterruptionFilter + val currentInterruptionFilter: Int + get() = notificationManager.currentInterruptionFilter + private val colorProvider = ColorProvider(context, preferences) private val queue = NotificationLimiter(MAX_NOTIFICATIONS) diff --git a/app/src/main/java/org/tasks/notifications/ThrottledNotificationManager.kt b/app/src/main/java/org/tasks/notifications/ThrottledNotificationManager.kt index bd0c103fa..0ab54afb2 100644 --- a/app/src/main/java/org/tasks/notifications/ThrottledNotificationManager.kt +++ b/app/src/main/java/org/tasks/notifications/ThrottledNotificationManager.kt @@ -3,6 +3,7 @@ package org.tasks.notifications import android.app.Notification import android.content.Context import androidx.core.app.NotificationManagerCompat +import androidx.core.app.NotificationManagerCompat.InterruptionFilter import dagger.hilt.android.qualifiers.ApplicationContext import java.util.concurrent.Executors.newSingleThreadExecutor import javax.inject.Inject @@ -14,6 +15,10 @@ class ThrottledNotificationManager @Inject constructor( private val executor = newSingleThreadExecutor() private val throttle = Throttle(NOTIFICATIONS_PER_SECOND, executor = executor, tag = "NOTIFY") + @InterruptionFilter + val currentInterruptionFilter: Int + get() = notificationManagerCompat.currentInterruptionFilter + fun cancel(id: Int) { executor.execute { notificationManagerCompat.cancel(id)