[237] | 1 | package net.sabi.pester;
|
---|
| 2 |
|
---|
| 3 | import java.util.Comparator;
|
---|
[255] | 4 | import danger.app.Application;
|
---|
[237] | 5 | import danger.app.DataStore;
|
---|
[255] | 6 | import danger.app.Event;
|
---|
[258] | 7 | import danger.app.SettingsDB;
|
---|
| 8 | import danger.app.SettingsDBException;
|
---|
| 9 | import danger.internal.Date;
|
---|
[250] | 10 | import danger.util.StdActiveList;
|
---|
[276] | 11 | import danger.util.DEBUG;
|
---|
[237] | 12 |
|
---|
[250] | 13 | public 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 | }
|
---|