source: trunk/hiptop/pester/net/sabi/pester/Alarms.java@ 295

Last change on this file since 295 was 295, checked in by Nicholas Riley, 18 years ago

Move discard alert text to resource. Fix listener for
EVENT_TIME_CHANGED I broke a few days ago. Only use
Alarm.mAbsoluteFireTime to measure real time if the alarm was
scheduled/rescheduled (through user action, not restoration from a
datastore) since boot. Don't display dates in the past as if they're
in the following week.

File size: 4.6 KB
Line 
1package net.sabi.pester;
2
3import java.util.Comparator;
4import danger.app.Application;
5import danger.app.DataStore;
6import danger.app.Event;
7import danger.app.SettingsDB;
8import danger.app.SettingsDBException;
9import danger.internal.Date;
10import danger.util.StdActiveList;
11import danger.util.DEBUG;
12
13public class Alarms extends StdActiveList {
14 // max # records in a datastore
15 public 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 sSettingsDB = new SettingsDB("settings", true /* auto sync */);
57 sListener = new Listener();
58 new Alarms();
59 Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
60 }
61 return sAlarmList;
62 }
63
64 public static boolean canCreateAlarm() {
65 return (sAlarmList.size() < MAX_ALARM_COUNT);
66 }
67 public static void addAlarm(Alarm alarm) {
68 sAlarmList.insertItemSorted(alarm, alarm);
69 }
70 public static void removeAlarm(Alarm alarm) {
71 sAlarmList.removeItem(alarm);
72 }
73 public static void recentMessagesChanged() {
74 sSettingsDB.setBytes(KEY_RECENT_MESSAGES,
75 MessageFinder.messageListAsByteArray());
76 }
77
78 protected void onItemAdded(Object item, int index) {
79 if (sAlarmList == null) // restoring from service
80 return;
81 Alarm alarm = (Alarm)item;
82 DEBUG.p("adding: " + alarm +
83 " next UID: " + mDataStore.getAutoSyncNextUID());
84 index = mDataStore.addRecord(alarm.toByteArray());
85 alarm.setUID(mDataStore.getRecordUID(index));
86 DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
87 }
88
89 protected void onItemRemoved(Object item, int index) {
90 if (sAlarmList == null) // restoring from service (after hard reset)
91 return;
92 Alarm alarm = (Alarm)item;
93 int uid = alarm.getUID();
94 if (uid == 0)
95 return;
96 mDataStore.removeRecordByUID(uid);
97 DEBUG.p("removed UID " + alarm.getUID());
98 }
99
100 public void onItemUpdated(Object item, int index) {
101 if (sAlarmList == null) // restoring from service
102 return;
103 Alarm alarm = (Alarm)item;
104 mDataStore.setRecordDataByUID(alarm.getUID(), alarm.toByteArray(), true);
105 DEBUG.p("updated: " + alarm + " UID " + alarm.getUID());
106 }
107
108 private static String KEY_DEFAULT_ALARM = "default alarm";
109 private static String KEY_RECENT_MESSAGES = "recent messages";
110
111 public static Alarm getDefaultAlarm() {
112 Alarm defaultAlarm = new Alarm();
113 try {
114 defaultAlarm.fromByteArray(sSettingsDB.getBytes(KEY_DEFAULT_ALARM));
115 } catch (SettingsDBException e) {
116 defaultAlarm = new Alarm();
117 defaultAlarm.setDate(new Date());
118 defaultAlarm.setPeriod(600, false);
119 }
120 return defaultAlarm;
121 }
122 public static void setDefaultAlarm(Alarm alarm) {
123 sSettingsDB.setBytes(KEY_DEFAULT_ALARM, alarm.toByteArray());
124 }
125
126 static class Listener extends danger.app.Listener
127 implements danger.util.ActiveList.ForEach {
128
129 public void receive(Object item) {
130 ((Alarm)item).timeChanged();
131 }
132
133 public boolean receiveEvent(Event e) {
134 if (e.type == Event.EVENT_TIME_CHANGED) {
135 Alarms.getList().forEach(this);
136 return true;
137 } else if (e.type == Event.EVENT_DATASTORE_RESTORED) {
138 String dbName = (String)e.argument;
139 DEBUG.p("DATASTORE_RESTORED: " + dbName);
140 if (!dbName.endsWith("alarms")) {
141 // because we only get/set the default alarm on demand
142 // there's no need to handle conflicts with the settings...
143 // XXX but what happens if we set a (default) alarm, then
144 // the SettingsDB restores?
145 Alarms.getList().refreshFromDataStore(true);
146 }
147 }
148 return super.receiveEvent(e);
149 }
150 }
151}
Note: See TracBrowser for help on using the repository browser.