From 28fe881b463ccb4024d6352f95548776e8452492 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 2 Mar 2019 09:45:14 +0000 Subject: [PATCH] Store shown notifications in preferences --- app/src/main/java/eu/faircode/email/Core.java | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 490db25075..255c40d379 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -13,7 +13,6 @@ import android.os.Build; import android.os.Bundle; import android.os.SystemClock; import android.preference.PreferenceManager; -import android.service.notification.StatusBarNotification; import android.text.TextUtils; import android.util.LongSparseArray; @@ -1397,51 +1396,52 @@ class Core { Widget.update(context, messages.size()); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = prefs.edit(); + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); LongSparseArray> notifying = new LongSparseArray<>(); LongSparseArray accountName = new LongSparseArray<>(); LongSparseArray> accountMessages = new LongSparseArray<>(); - // Existing - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - for (StatusBarNotification notification : nm.getActiveNotifications()) { - Bundle args = notification.getNotification().extras; - long id = args.getLong("id", 0); - long account = args.getLong("account", 0); - Log.i("Notify showing=" + id); - if (id != 0) { - if (notifying.indexOfKey(account) < 0) { - notifying.put(account, new ArrayList()); - accountMessages.put(account, new ArrayList()); - } - notifying.get(account).add(id); - } + // Previous + for (String key : prefs.getAll().keySet()) + if (key.startsWith("notifying:")) { + long account = Long.parseLong(key.split(":")[1]); + notifying.put(account, new ArrayList()); + + for (String id : prefs.getString(key, null).split(",")) + notifying.get(account).add(Long.parseLong(id)); + + editor.remove(key); } - } // Current for (TupleMessageEx message : messages) { long account = (message.accountNotify ? message.account : 0); accountName.put(account, account > 0 ? message.accountName : null); if (accountMessages.indexOfKey(account) < 0) { - notifying.put(account, new ArrayList()); accountMessages.put(account, new ArrayList()); + if (notifying.indexOfKey(account) < 0) + notifying.put(account, new ArrayList()); } accountMessages.get(account).add(message); } // Difference - for (int i = 0; i < accountMessages.size(); i++) { - long account = accountMessages.keyAt(i); + for (int i = 0; i < notifying.size(); i++) { + long account = notifying.keyAt(i); List notifications = getNotificationUnseen( context, account, accountName.get(account), accountMessages.get(account)); + List all = new ArrayList<>(); List add = new ArrayList<>(); List remove = notifying.get(account); for (Notification notification : notifications) { Long id = notification.extras.getLong("id", 0); if (id != 0) { + all.add(Long.toString(id)); if (remove.contains(id)) { remove.remove(id); Log.i("Notify existing=" + id); @@ -1473,7 +1473,12 @@ class Core { if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id)) nm.notify("unseen:" + account + ":" + Math.abs(id), 1, notification); } + + if (all.size() > 0) + editor.putString("notifying:" + account, TextUtils.join(",", all)); } + + editor.apply(); } private static List getNotificationUnseen( @@ -1482,7 +1487,7 @@ class Core { List messages) { List notifications = new ArrayList<>(); - if (messages.size() == 0) + if (messages == null || messages.size() == 0) return notifications; boolean pro = Helper.isPro(context);