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

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

First pass at alarm persistence.

File size: 3.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 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 byte[][] alarmsData = mDataStore.getRecords();
26 int i;
27 for (i = 0 ; i < alarmsData.length ; ++i) {
28 Alarm alarm = new Alarm();
29 alarm.fromByteArray(alarmsData[i]);
30 alarm.setUID(mDataStore.getRecordUID(i));
31 insertItemSorted(alarm, alarm);
32 alarm.resume();
33 }
34 }
35
36 public static Alarms getList() {
37 if (sAlarmList == null) {
38 Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
39 sSettingsDB = new SettingsDB("settings", true /* auto sync */);
40 sListener = new Listener();
41 sAlarmList = new Alarms();
42 }
43 return sAlarmList;
44 }
45
46 public static void addAlarm(Alarm alarm) {
47 sAlarmList.insertItemSorted(alarm, alarm);
48 }
49 public static void removeAlarm(Alarm alarm) {
50 sAlarmList.removeItem(alarm);
51 }
52
53 // XXX handle EVENT_DATASTORE_RESTORED: note that in the event of
54 // a hard reset, items may come back out of order, so we need to
55 // implement an in-place sort, then insert the current alarm list.
56 // Aiee. Also, what on earth happens when UIDs aren't synchronized?
57
58 protected void onItemAdded(Object item, int index) {
59 if (sAlarmList == null) // restoring from service
60 return;
61 Alarm alarm = (Alarm)item;
62 DEBUG.p("adding: " + alarm +
63 " next UID: " + mDataStore.getAutoSyncNextUID());
64 index = mDataStore.addRecord(alarm.toByteArray(), index);
65 alarm.setUID(mDataStore.getRecordUID(index));
66 DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
67 }
68
69 protected void onItemRemoved(Object item, int index) {
70 Alarm alarm = (Alarm)item;
71 // XXX make sure +/- UIDs are OK
72 int uid = alarm.getUID();
73 if (uid == 0)
74 return;
75 DEBUG.p("removing; " + alarm + " UID " + uid);
76 mDataStore.removeRecordByUID(uid);
77 }
78
79 public void onItemUpdated(Object item, int index) {
80 if (sAlarmList == null) // restoring from service
81 return;
82 Alarm alarm = (Alarm)item;
83 // XXX check that updating a + UID by a - one is OK
84 mDataStore.setRecordDataByUID(alarm.getUID(), alarm.toByteArray(), true);
85 DEBUG.p("updated: " + alarm + " UID " + alarm.getUID());
86 }
87
88 private static String KEY_DEFAULT_ALARM = "default alarm";
89
90 public static Alarm getDefaultAlarm() {
91 Alarm defaultAlarm = new Alarm();
92 try {
93 defaultAlarm.fromByteArray(sSettingsDB.getBytes(KEY_DEFAULT_ALARM));
94 } catch (SettingsDBException e) {
95 defaultAlarm = new Alarm();
96 defaultAlarm.setDate(new Date());
97 defaultAlarm.setPeriod(600, false);
98 }
99 return defaultAlarm;
100 }
101 public static void setDefaultAlarm(Alarm alarm) {
102 sSettingsDB.setBytes(KEY_DEFAULT_ALARM, alarm.toByteArray());
103 }
104
105 static class Listener extends danger.app.Listener
106 implements danger.util.ActiveList.ForEach {
107
108 public void receive(Object item) {
109 ((Alarm)item).timeChanged();
110 }
111
112 public boolean receiveEvent(Event e) {
113 if (e.type == Event.EVENT_TIME_CHANGED) {
114 Alarms.getList().forEach(this);
115 return true;
116 }
117 return super.receiveEvent(e);
118 }
119 }
120}
Note: See TracBrowser for help on using the repository browser.