From 98c8377c9d35bec252e86b4df803c95509fc9ea1 Mon Sep 17 00:00:00 2001 From: Marten Gajda Date: Fri, 7 Jun 2019 22:30:59 +0200 Subject: [PATCH] Fix permanent notification, fixes #800 This sets a "notification cancelled" intent which removes the persisted entry when the notification is swiped away. --- .../java/org/dmfs/tasks/actions/NotifyAction.java | 10 ++++++++-- .../org/dmfs/tasks/notification/ActionService.java | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/opentasks/src/main/java/org/dmfs/tasks/actions/NotifyAction.java b/opentasks/src/main/java/org/dmfs/tasks/actions/NotifyAction.java index 053b99f57..32581c74e 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/actions/NotifyAction.java +++ b/opentasks/src/main/java/org/dmfs/tasks/actions/NotifyAction.java @@ -27,8 +27,6 @@ import android.net.Uri; import android.os.Build; import android.os.RemoteException; -import androidx.core.app.NotificationCompat; -import androidx.core.app.NotificationManagerCompat; import android.text.format.DateUtils; import org.dmfs.android.bolts.color.colors.ResourceColor; @@ -52,6 +50,9 @@ import org.dmfs.tasks.notification.signals.Conditional; import org.dmfs.tasks.utils.DateFormatter; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; + /** * Post a notification. @@ -128,6 +129,11 @@ public void execute(Context context, ContentProviderClient contentProviderClient .setData(taskUri), PendingIntent.FLAG_UPDATE_CURRENT)); + // make sure we un-persist the notification when its cancelled + builder.setDeleteIntent(PendingIntent.getBroadcast(context, notificationId, + new Intent(context, ActionReceiver.class).setAction(ActionService.ACTION_REMOVE_NOTIFICATION).setData(taskUri), + PendingIntent.FLAG_UPDATE_CURRENT)); + if (!new TaskIsClosed(data).value()) { builder.addAction( diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java b/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java index 62dea8a02..1036ce402 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java @@ -23,8 +23,6 @@ import android.content.OperationApplicationException; import android.net.Uri; import android.os.RemoteException; -import androidx.annotation.NonNull; -import androidx.core.app.JobIntentService; import android.util.Log; import org.dmfs.android.contentpal.Projection; @@ -54,10 +52,14 @@ import org.dmfs.tasks.actions.OpenAction; import org.dmfs.tasks.actions.PinAction; import org.dmfs.tasks.actions.PostUndoAction; +import org.dmfs.tasks.actions.RemoveNotificationAction; import org.dmfs.tasks.actions.TaskAction; import org.dmfs.tasks.actions.WipeNotificationAction; import org.dmfs.tasks.contract.TaskContract; +import androidx.annotation.NonNull; +import androidx.core.app.JobIntentService; + /** * A {@link JobIntentService} which handles notification actions. @@ -76,6 +78,7 @@ public final class ActionService extends JobIntentService public static final String ACTION_UNPIN = "org.dmfs.tasks.intent.UNPIN"; public static final String ACTION_OPEN_TASK = "org.dmfs.tasks.intent.OPEN_TASK"; public static final String ACTION_OPEN_TASK_CANCEL_NOTIFICATION = "org.dmfs.tasks.intent.OPEN_CANCEL_TASK"; + public static final String ACTION_REMOVE_NOTIFICATION = "org.dmfs.tasks.intent.CANCEL_NOTIFICATION"; public static final String ACTION_NEXT_DAY = "org.dmfs.tasks.intent.ACTION_DAY_CHANGED"; public static final String ACTION_RENOTIFY = "org.dmfs.tasks.intent.NOTIFY"; public static final String ACTION_DEFER_1D = "org.dmfs.tasks.action.notification.DELAY_1D"; @@ -164,6 +167,10 @@ private TaskAction resolveAction(String action) // just open the task and cancel the notification return new Composite(new OpenAction(), new WipeNotificationAction()); + case ACTION_REMOVE_NOTIFICATION: + // remove the notification + return new RemoveNotificationAction(); + case ACTION_DEFER_1D: // defer the due date and remove notification if not pinned and due date is in the future return new Composite(