1 | package net.sabi.pester;
|
---|
2 |
|
---|
3 | import java.util.Comparator;
|
---|
4 | import danger.app.Application;
|
---|
5 | import danger.app.DataStore;
|
---|
6 | import danger.app.Event;
|
---|
7 | import danger.util.StdActiveList;
|
---|
8 |
|
---|
9 | public class Alarms extends StdActiveList {
|
---|
10 | // max # records in a datastore
|
---|
11 | private static final int MAX_ALARM_COUNT = 50;
|
---|
12 |
|
---|
13 | private static Alarms sAlarmList = null;
|
---|
14 | private static Listener sListener;
|
---|
15 |
|
---|
16 | private DataStore mDataStore;
|
---|
17 |
|
---|
18 | private Alarms() {
|
---|
19 | mDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
|
---|
20 | }
|
---|
21 |
|
---|
22 | public static Alarms getList() {
|
---|
23 | if (sAlarmList == null) {
|
---|
24 | sAlarmList = new Alarms();
|
---|
25 | sListener = new Listener();
|
---|
26 | Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
|
---|
27 | }
|
---|
28 | return sAlarmList;
|
---|
29 | }
|
---|
30 |
|
---|
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 |
|
---|
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 | */
|
---|
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 | }
|
---|
63 | }
|
---|
64 |
|
---|