Store shown notifications in preferences

pull/152/head
M66B 6 years ago
parent 9d17b494f1
commit 28fe881b46

@ -13,7 +13,6 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.LongSparseArray; import android.util.LongSparseArray;
@ -1397,27 +1396,25 @@ class Core {
Widget.update(context, messages.size()); Widget.update(context, messages.size());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
LongSparseArray<List<Long>> notifying = new LongSparseArray<>(); LongSparseArray<List<Long>> notifying = new LongSparseArray<>();
LongSparseArray<String> accountName = new LongSparseArray<>(); LongSparseArray<String> accountName = new LongSparseArray<>();
LongSparseArray<List<TupleMessageEx>> accountMessages = new LongSparseArray<>(); LongSparseArray<List<TupleMessageEx>> accountMessages = new LongSparseArray<>();
// Existing // Previous
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { for (String key : prefs.getAll().keySet())
for (StatusBarNotification notification : nm.getActiveNotifications()) { if (key.startsWith("notifying:")) {
Bundle args = notification.getNotification().extras; long account = Long.parseLong(key.split(":")[1]);
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<Long>()); notifying.put(account, new ArrayList<Long>());
accountMessages.put(account, new ArrayList<TupleMessageEx>());
} for (String id : prefs.getString(key, null).split(","))
notifying.get(account).add(id); notifying.get(account).add(Long.parseLong(id));
}
} editor.remove(key);
} }
// Current // Current
@ -1425,23 +1422,26 @@ class Core {
long account = (message.accountNotify ? message.account : 0); long account = (message.accountNotify ? message.account : 0);
accountName.put(account, account > 0 ? message.accountName : null); accountName.put(account, account > 0 ? message.accountName : null);
if (accountMessages.indexOfKey(account) < 0) { if (accountMessages.indexOfKey(account) < 0) {
notifying.put(account, new ArrayList<Long>());
accountMessages.put(account, new ArrayList<TupleMessageEx>()); accountMessages.put(account, new ArrayList<TupleMessageEx>());
if (notifying.indexOfKey(account) < 0)
notifying.put(account, new ArrayList<Long>());
} }
accountMessages.get(account).add(message); accountMessages.get(account).add(message);
} }
// Difference // Difference
for (int i = 0; i < accountMessages.size(); i++) { for (int i = 0; i < notifying.size(); i++) {
long account = accountMessages.keyAt(i); long account = notifying.keyAt(i);
List<Notification> notifications = getNotificationUnseen( List<Notification> notifications = getNotificationUnseen(
context, account, accountName.get(account), accountMessages.get(account)); context, account, accountName.get(account), accountMessages.get(account));
List<String> all = new ArrayList<>();
List<Long> add = new ArrayList<>(); List<Long> add = new ArrayList<>();
List<Long> remove = notifying.get(account); List<Long> remove = notifying.get(account);
for (Notification notification : notifications) { for (Notification notification : notifications) {
Long id = notification.extras.getLong("id", 0); Long id = notification.extras.getLong("id", 0);
if (id != 0) { if (id != 0) {
all.add(Long.toString(id));
if (remove.contains(id)) { if (remove.contains(id)) {
remove.remove(id); remove.remove(id);
Log.i("Notify existing=" + id); Log.i("Notify existing=" + id);
@ -1473,7 +1473,12 @@ class Core {
if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id)) if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id))
nm.notify("unseen:" + account + ":" + Math.abs(id), 1, notification); 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<Notification> getNotificationUnseen( private static List<Notification> getNotificationUnseen(
@ -1482,7 +1487,7 @@ class Core {
List<TupleMessageEx> messages) { List<TupleMessageEx> messages) {
List<Notification> notifications = new ArrayList<>(); List<Notification> notifications = new ArrayList<>();
if (messages.size() == 0) if (messages == null || messages.size() == 0)
return notifications; return notifications;
boolean pro = Helper.isPro(context); boolean pro = Helper.isPro(context);

Loading…
Cancel
Save