mirror of https://github.com/tasks/tasks
Limit number of active notifications
parent
1407b3a85e
commit
1580b12f95
@ -0,0 +1,43 @@
|
||||
package org.tasks.notifications;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
class NotificationLimiter {
|
||||
|
||||
private final Queue<Long> queue = new LinkedList<>();
|
||||
private final int maxSize;
|
||||
private boolean summary = false;
|
||||
|
||||
NotificationLimiter(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
synchronized List<Long> add(long id) {
|
||||
if (id == NotificationManager.SUMMARY_NOTIFICATION_ID) {
|
||||
summary = true;
|
||||
} else {
|
||||
remove(id);
|
||||
queue.add(id);
|
||||
}
|
||||
List<Long> evicted = new ArrayList<>();
|
||||
for (int i = 0 ; i < size() - maxSize ; i++) {
|
||||
evicted.add(queue.remove());
|
||||
}
|
||||
return evicted;
|
||||
}
|
||||
|
||||
synchronized void remove(long id) {
|
||||
if (id == NotificationManager.SUMMARY_NOTIFICATION_ID) {
|
||||
summary = false;
|
||||
} else {
|
||||
queue.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
private int size() {
|
||||
return queue.size() + (summary ? 1 : 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue