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.app.SettingsDB;
|
---|
8 | import danger.app.SettingsDBException;
|
---|
9 | import danger.internal.Date;
|
---|
10 | import danger.util.StdActiveList;
|
---|
11 | import danger.util.DEBUG;
|
---|
12 |
|
---|
13 | public class Alarms extends StdActiveList {
|
---|
14 | // max # records in a datastore
|
---|
15 | private static final int MAX_ALARM_COUNT = 50;
|
---|
16 |
|
---|
17 | private static Alarms sAlarmList = null;
|
---|
18 | private static Listener sListener;
|
---|
19 | private static SettingsDB sSettingsDB;
|
---|
20 |
|
---|
21 | private DataStore mDataStore;
|
---|
22 |
|
---|
23 | private Alarms() {
|
---|
24 | mDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
|
---|
25 | // register us for Event.EVENT_DATASTORE_RESTORED, which only
|
---|
26 | // seems to be documented at:
|
---|
27 | // <http://developer.danger.com/forum/index.php?t=msg&th=27>
|
---|
28 | mDataStore.setAutoSyncNotifyee(sListener);
|
---|
29 | refreshFromDataStore(false);
|
---|
30 | }
|
---|
31 |
|
---|
32 | void refreshFromDataStore(boolean datastoreRestored) {
|
---|
33 | sAlarmList = null;
|
---|
34 | removeAllItems();
|
---|
35 | if (datastoreRestored)
|
---|
36 | mDataStore.doneResolvingConflict(); // resolves UID conflicts
|
---|
37 | byte[][] alarmsData = mDataStore.getRecords();
|
---|
38 | int i;
|
---|
39 | for (i = 0 ; i < alarmsData.length ; ++i) {
|
---|
40 | Alarm alarm = new Alarm();
|
---|
41 | alarm.fromByteArray(alarmsData[i]);
|
---|
42 | alarm.setUID(mDataStore.getRecordUID(i));
|
---|
43 | insertItemSorted(alarm, alarm);
|
---|
44 | alarm.resume();
|
---|
45 | }
|
---|
46 | try {
|
---|
47 | MessageFinder.setMessageListFromByteArray(sSettingsDB.getBytes(KEY_RECENT_MESSAGES));
|
---|
48 | } catch (SettingsDBException e) {
|
---|
49 | MessageFinder.setDefaultMessageList();
|
---|
50 | }
|
---|
51 | sAlarmList = this;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static Alarms getList() {
|
---|
55 | if (sAlarmList == null) {
|
---|
56 | Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
|
---|
57 | sSettingsDB = new SettingsDB("settings", true /* auto sync */);
|
---|
58 | sListener = new Listener();
|
---|
59 | new Alarms();
|
---|
60 | }
|
---|
61 | return sAlarmList;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static void addAlarm(Alarm alarm) {
|
---|
65 | sAlarmList.insertItemSorted(alarm, alarm);
|
---|
66 | }
|
---|
67 | public static void removeAlarm(Alarm alarm) {
|
---|
68 | sAlarmList.removeItem(alarm);
|
---|
69 | }
|
---|
70 | public static void recentMessagesChanged() {
|
---|
71 | sSettingsDB.setBytes(KEY_RECENT_MESSAGES,
|
---|
72 | MessageFinder.messageListAsByteArray());
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void onItemAdded(Object item, int index) {
|
---|
76 | if (sAlarmList == null) // restoring from service
|
---|
77 | return;
|
---|
78 | Alarm alarm = (Alarm)item;
|
---|
79 | DEBUG.p("adding: " + alarm +
|
---|
80 | " next UID: " + mDataStore.getAutoSyncNextUID());
|
---|
81 | index = mDataStore.addRecord(alarm.toByteArray());
|
---|
82 | alarm.setUID(mDataStore.getRecordUID(index));
|
---|
83 | DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected void onItemRemoved(Object item, int index) {
|
---|
87 | if (sAlarmList == null) // restoring from service (after hard reset)
|
---|
88 | return;
|
---|
89 | Alarm alarm = (Alarm)item;
|
---|
90 | int uid = alarm.getUID();
|
---|
91 | if (uid == 0)
|
---|
92 | return;
|
---|
93 | mDataStore.removeRecordByUID(uid);
|
---|
94 | DEBUG.p("removed UID " + alarm.getUID());
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void onItemUpdated(Object item, int index) {
|
---|
98 | if (sAlarmList == null) // restoring from service
|
---|
99 | return;
|
---|
100 | Alarm alarm = (Alarm)item;
|
---|
101 | mDataStore.setRecordDataByUID(alarm.getUID(), alarm.toByteArray(), true);
|
---|
102 | DEBUG.p("updated: " + alarm + " UID " + alarm.getUID());
|
---|
103 | }
|
---|
104 |
|
---|
105 | private static String KEY_DEFAULT_ALARM = "default alarm";
|
---|
106 | private static String KEY_RECENT_MESSAGES = "recent messages";
|
---|
107 |
|
---|
108 | public static Alarm getDefaultAlarm() {
|
---|
109 | Alarm defaultAlarm = new Alarm();
|
---|
110 | try {
|
---|
111 | defaultAlarm.fromByteArray(sSettingsDB.getBytes(KEY_DEFAULT_ALARM));
|
---|
112 | } catch (SettingsDBException e) {
|
---|
113 | defaultAlarm = new Alarm();
|
---|
114 | defaultAlarm.setDate(new Date());
|
---|
115 | defaultAlarm.setPeriod(600, false);
|
---|
116 | }
|
---|
117 | return defaultAlarm;
|
---|
118 | }
|
---|
119 | public static void setDefaultAlarm(Alarm alarm) {
|
---|
120 | sSettingsDB.setBytes(KEY_DEFAULT_ALARM, alarm.toByteArray());
|
---|
121 | }
|
---|
122 |
|
---|
123 | static class Listener extends danger.app.Listener
|
---|
124 | implements danger.util.ActiveList.ForEach {
|
---|
125 |
|
---|
126 | public void receive(Object item) {
|
---|
127 | ((Alarm)item).timeChanged();
|
---|
128 | }
|
---|
129 |
|
---|
130 | public boolean receiveEvent(Event e) {
|
---|
131 | if (e.type == Event.EVENT_TIME_CHANGED) {
|
---|
132 | Alarms.getList().forEach(this);
|
---|
133 | return true;
|
---|
134 | } else if (e.type == Event.EVENT_DATASTORE_RESTORED) {
|
---|
135 | String dbName = (String)e.argument;
|
---|
136 | DEBUG.p("DATASTORE_RESTORED: " + dbName);
|
---|
137 | if (!dbName.endsWith("alarms")) {
|
---|
138 | // because we only get/set the default alarm on demand
|
---|
139 | // there's no need to handle conflicts with the settings...
|
---|
140 | // XXX but what happens if we set a (default) alarm, then
|
---|
141 | // the SettingsDB restores?
|
---|
142 | Alarms.getList().refreshFromDataStore(true);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return super.receiveEvent(e);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|