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
RevLine 
[237]1package net.sabi.pester;
2
3import java.util.Comparator;
[255]4import danger.app.Application;
[237]5import danger.app.DataStore;
[255]6import danger.app.Event;
[258]7import danger.app.SettingsDB;
8import danger.app.SettingsDBException;
9import danger.internal.Date;
[250]10import danger.util.StdActiveList;
[276]11import danger.util.DEBUG;
[237]12
[250]13public class Alarms extends StdActiveList {
[237]14 // max # records in a datastore
[294]15 public static final int MAX_ALARM_COUNT = 50;
[237]16
[250]17 private static Alarms sAlarmList = null;
[255]18 private static Listener sListener;
[258]19 private static SettingsDB sSettingsDB;
[237]20
[255]21 private DataStore mDataStore;
22
[237]23 private Alarms() {
[255]24 mDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
[278]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
[276]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 }
[283]46 try {
47 MessageFinder.setMessageListFromByteArray(sSettingsDB.getBytes(KEY_RECENT_MESSAGES));
48 } catch (SettingsDBException e) {
49 MessageFinder.setDefaultMessageList();
50 }
[278]51 sAlarmList = this;
[237]52 }
53
[250]54 public static Alarms getList() {
[255]55 if (sAlarmList == null) {
[276]56 sSettingsDB = new SettingsDB("settings", true /* auto sync */);
57 sListener = new Listener();
[278]58 new Alarms();
[295]59 Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
[255]60 }
[250]61 return sAlarmList;
[237]62 }
63
[294]64 public static boolean canCreateAlarm() {
65 return (sAlarmList.size() < MAX_ALARM_COUNT);
66 }
[250]67 public static void addAlarm(Alarm alarm) {
[259]68 sAlarmList.insertItemSorted(alarm, alarm);
[250]69 }
70 public static void removeAlarm(Alarm alarm) {
71 sAlarmList.removeItem(alarm);
72 }
[283]73 public static void recentMessagesChanged() {
74 sSettingsDB.setBytes(KEY_RECENT_MESSAGES,
75 MessageFinder.messageListAsByteArray());
76 }
[250]77
[276]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());
[278]84 index = mDataStore.addRecord(alarm.toByteArray());
[276]85 alarm.setUID(mDataStore.getRecordUID(index));
86 DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
87 }
88
89 protected void onItemRemoved(Object item, int index) {
[278]90 if (sAlarmList == null) // restoring from service (after hard reset)
91 return;
[276]92 Alarm alarm = (Alarm)item;
93 int uid = alarm.getUID();
94 if (uid == 0)
95 return;
96 mDataStore.removeRecordByUID(uid);
[284]97 DEBUG.p("removed UID " + alarm.getUID());
[276]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
[258]108 private static String KEY_DEFAULT_ALARM = "default alarm";
[283]109 private static String KEY_RECENT_MESSAGES = "recent messages";
[258]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
[255]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;
[278]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 }
[255]147 }
148 return super.receiveEvent(e);
149 }
150 }
[237]151}
Note: See TracBrowser for help on using the repository browser.