Merge pull request #685 from jotomo/notification-store-race

NotificationStore: add synchronisation when accessing internal store.
This commit is contained in:
Milos Kozak 2018-02-12 13:24:59 +01:00 committed by GitHub
commit 1bf73b8269
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,8 +21,6 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.AlarmSoundService;
import info.nightscout.androidaps.plugins.Wear.WearPlugin;
//Added by Rumen for snooze time
import info.nightscout.utils.SP;
/**
@ -44,16 +42,12 @@ public class NotificationStore {
}
}
public Notification get(int index) {
return store.get(index);
}
public void add(Notification n) {
public synchronized void add(Notification n) {
log.info("Notification received: " + n.text);
for (int i = 0; i < store.size(); i++) {
if (get(i).id == n.id) {
get(i).date = n.date;
get(i).validTo = n.validTo;
for (Notification storeNotification : store) {
if (storeNotification.id == n.id) {
storeNotification.date = n.date;
storeNotification.validTo = n.validTo;
return;
}
}
@ -72,6 +66,44 @@ public class NotificationStore {
Collections.sort(store, new NotificationComparator());
}
public synchronized boolean remove(int id) {
for (int i = 0; i < store.size(); i++) {
if (store.get(i).id == id) {
if (store.get(i).soundId != null) {
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);
MainApp.instance().stopService(alarm);
}
store.remove(i);
return true;
}
}
return false;
}
public synchronized void removeExpired() {
for (int i = 0; i < store.size(); i++) {
Notification n = store.get(i);
if (n.validTo.getTime() != 0 && n.validTo.getTime() < System.currentTimeMillis()) {
store.remove(i);
i--;
}
}
}
public void snoozeTo(long timeToSnooze) {
log.debug("Snoozing alarm until: " + timeToSnooze);
SP.putLong("snoozedTo", timeToSnooze);
}
public void unSnooze() {
if (Notification.isAlarmForStaleData()) {
Notification notification = new Notification(Notification.NSALARM, MainApp.sResources.getString(R.string.nsalarm_staledata), Notification.URGENT);
SP.putLong("snoozedTo", System.currentTimeMillis());
add(notification);
log.debug("Snoozed to current time and added back notification!");
}
}
private void raiseSystemNotification(Notification n) {
Context context = MainApp.instance().getApplicationContext();
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@ -95,42 +127,4 @@ public class NotificationStore {
}
mgr.notify(n.id, notificationBuilder.build());
}
public boolean remove(int id) {
for (int i = 0; i < store.size(); i++) {
if (get(i).id == id) {
if (get(i).soundId != null) {
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);
MainApp.instance().stopService(alarm);
}
store.remove(i);
return true;
}
}
return false;
}
public void removeExpired() {
for (int i = 0; i < store.size(); i++) {
Notification n = get(i);
if (n.validTo.getTime() != 0 && n.validTo.getTime() < System.currentTimeMillis()) {
store.remove(i);
i--;
}
}
}
public void snoozeTo(long timeToSnooze) {
log.debug("Snoozing alarm until: " + timeToSnooze);
SP.putLong("snoozedTo", timeToSnooze);
}
public void unSnooze() {
if (Notification.isAlarmForStaleData()) {
Notification notification = new Notification(Notification.NSALARM, MainApp.sResources.getString(R.string.nsalarm_staledata), Notification.URGENT);
SP.putLong("snoozedTo", System.currentTimeMillis());
add(notification);
log.debug("Snoozed to current time and added back notification!");
}
}
}