[237] | 1 | package net.sabi.pester;
|
---|
| 2 |
|
---|
| 3 | import java.util.Comparator;
|
---|
[255] | 4 | import danger.app.Application;
|
---|
[237] | 5 | import danger.app.DataStore;
|
---|
[255] | 6 | import danger.app.Event;
|
---|
[250] | 7 | import danger.util.StdActiveList;
|
---|
[237] | 8 |
|
---|
[250] | 9 | public class Alarms extends StdActiveList {
|
---|
[237] | 10 | // max # records in a datastore
|
---|
| 11 | private static final int MAX_ALARM_COUNT = 50;
|
---|
| 12 |
|
---|
[250] | 13 | private static Alarms sAlarmList = null;
|
---|
[255] | 14 | private static Listener sListener;
|
---|
[237] | 15 |
|
---|
[255] | 16 | private DataStore mDataStore;
|
---|
| 17 |
|
---|
[237] | 18 | private Alarms() {
|
---|
[255] | 19 | mDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
|
---|
[237] | 20 | }
|
---|
| 21 |
|
---|
[250] | 22 | public static Alarms getList() {
|
---|
[255] | 23 | if (sAlarmList == null) {
|
---|
[250] | 24 | sAlarmList = new Alarms();
|
---|
[255] | 25 | sListener = new Listener();
|
---|
| 26 | Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
|
---|
| 27 | }
|
---|
[250] | 28 | return sAlarmList;
|
---|
[237] | 29 | }
|
---|
| 30 |
|
---|
[250] | 31 | public static void addAlarm(Alarm alarm) {
|
---|
| 32 | sAlarmList.addItem(alarm);
|
---|
| 33 | }
|
---|
| 34 | public static void removeAlarm(Alarm alarm) {
|
---|
| 35 | sAlarmList.removeItem(alarm);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[237] | 38 | /*
|
---|
| 39 | private static Alarm sAlarm0 = new Alarm();
|
---|
| 40 | private static Alarm sAlarm1 = new Alarm();
|
---|
| 41 | public int compare(Object arg0, Object arg1) {
|
---|
| 42 |
|
---|
| 43 | return Collator.getInstance().compare(
|
---|
| 44 | }
|
---|
| 45 | */
|
---|
[255] | 46 |
|
---|
| 47 | static class Listener extends danger.app.Listener
|
---|
| 48 | implements danger.util.ActiveList.ForEach {
|
---|
| 49 |
|
---|
| 50 | public void receive(Object item) {
|
---|
| 51 | ((Alarm)item).timeChanged();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public boolean receiveEvent(Event e) {
|
---|
| 55 | if (e.type == Event.EVENT_TIME_CHANGED) {
|
---|
| 56 | // XXX do we get this when the time zone changes?
|
---|
| 57 | Alarms.getList().forEach(this);
|
---|
| 58 | return true;
|
---|
| 59 | }
|
---|
| 60 | return super.receiveEvent(e);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[237] | 63 | }
|
---|
[255] | 64 |
|
---|