From a4e8f8fb3c77d11c6db16f2e884f44ceedb6ff98 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 24 Aug 2019 09:06:57 +0200 Subject: [PATCH] Get existing notifications on service start --- app/src/main/java/eu/faircode/email/Core.java | 9 +++++-- .../eu/faircode/email/ServiceSynchronize.java | 25 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 003ca800c4..a285a44c0e 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -1967,8 +1967,11 @@ class Core { groupMessages.put(group, new ArrayList()); } - if (message.notifying != 0) - groupNotifying.get(group).add(message.id * message.notifying); + if (message.notifying != 0) { + long id = message.id * message.notifying; + if (!groupNotifying.get(group).contains(id)) + groupNotifying.get(group).add(id); + } if (!(message.ui_seen || message.ui_ignored || message.ui_hide != 0)) { // This assumes the messages are properly ordered @@ -2050,6 +2053,8 @@ class Core { } } } + + groupNotifying.clear(); } private static List getNotificationUnseen(Context context, String group, List messages) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 51dfa1b899..0eb863d6fc 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -34,6 +34,7 @@ import android.net.NetworkRequest; import android.os.Build; import android.os.Handler; import android.os.PowerManager; +import android.service.notification.StatusBarNotification; import androidx.annotation.Nullable; import androidx.core.app.AlarmManagerCompat; @@ -165,8 +166,30 @@ public class ServiceSynchronize extends ServiceBase { } }); + Map> groupNotifying = new HashMap<>(); + + // Get existing notifications + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + for (StatusBarNotification sbn : nm.getActiveNotifications()) { + String tag = sbn.getTag(); + if (tag != null && tag.startsWith("unseen.")) { + String[] p = tag.split(("\\.")); + String group = p[1]; + long id = Long.parseLong(p[2]); + + if (!groupNotifying.containsKey(group)) + groupNotifying.put(group, new ArrayList<>()); + + if (id > 0) { + Log.i("Notify restore " + tag); + groupNotifying.get(group).add(id); + } + } + } + } + db.message().liveUnseenNotify().observe(cowner, new Observer>() { - private Map> groupNotifying = new HashMap<>(); private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);