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

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

A few tweaks to autocompletion - use a sorted list, and select by default. Users can still hit menu to stop it.

File size: 4.5 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
15 private static final int MAX_ALARM_COUNT = 50;
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 Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
57 sSettingsDB = new SettingsDB("settings", true /* auto sync */);
58 sListener = new Listener();
[278]59 new Alarms();
[255]60 }
[250]61 return sAlarmList;
[237]62 }
63
[250]64 public static void addAlarm(Alarm alarm) {
[259]65 sAlarmList.insertItemSorted(alarm, alarm);
[250]66 }
67 public static void removeAlarm(Alarm alarm) {
68 sAlarmList.removeItem(alarm);
69 }
[283]70 public static void recentMessagesChanged() {
71 sSettingsDB.setBytes(KEY_RECENT_MESSAGES,
72 MessageFinder.messageListAsByteArray());
73 }
[250]74
[276]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());
[278]81 index = mDataStore.addRecord(alarm.toByteArray());
[276]82 alarm.setUID(mDataStore.getRecordUID(index));
83 DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
84 }
85
86 protected void onItemRemoved(Object item, int index) {
[278]87 if (sAlarmList == null) // restoring from service (after hard reset)
88 return;
[276]89 Alarm alarm = (Alarm)item;
90 int uid = alarm.getUID();
91 if (uid == 0)
92 return;
93 mDataStore.removeRecordByUID(uid);
[284]94 DEBUG.p("removed UID " + alarm.getUID());
[276]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
[258]105 private static String KEY_DEFAULT_ALARM = "default alarm";
[283]106 private static String KEY_RECENT_MESSAGES = "recent messages";
[258]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
[255]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;
[278]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 }
[255]144 }
145 return super.receiveEvent(e);
146 }
147 }
[237]148}
Note: See TracBrowser for help on using the repository browser.