[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
|
---|
| 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 | }
|
---|
[278] | 46 | sAlarmList = this;
|
---|
[237] | 47 | }
|
---|
| 48 |
|
---|
[250] | 49 | public static Alarms getList() {
|
---|
[255] | 50 | if (sAlarmList == null) {
|
---|
[276] | 51 | Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
|
---|
| 52 | sSettingsDB = new SettingsDB("settings", true /* auto sync */);
|
---|
| 53 | sListener = new Listener();
|
---|
[278] | 54 | new Alarms();
|
---|
[255] | 55 | }
|
---|
[250] | 56 | return sAlarmList;
|
---|
[237] | 57 | }
|
---|
| 58 |
|
---|
[250] | 59 | public static void addAlarm(Alarm alarm) {
|
---|
[259] | 60 | sAlarmList.insertItemSorted(alarm, alarm);
|
---|
[250] | 61 | }
|
---|
| 62 | public static void removeAlarm(Alarm alarm) {
|
---|
| 63 | sAlarmList.removeItem(alarm);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[276] | 66 | protected void onItemAdded(Object item, int index) {
|
---|
| 67 | if (sAlarmList == null) // restoring from service
|
---|
| 68 | return;
|
---|
| 69 | Alarm alarm = (Alarm)item;
|
---|
| 70 | DEBUG.p("adding: " + alarm +
|
---|
| 71 | " next UID: " + mDataStore.getAutoSyncNextUID());
|
---|
[278] | 72 | index = mDataStore.addRecord(alarm.toByteArray());
|
---|
[276] | 73 | alarm.setUID(mDataStore.getRecordUID(index));
|
---|
| 74 | DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected void onItemRemoved(Object item, int index) {
|
---|
[278] | 78 | if (sAlarmList == null) // restoring from service (after hard reset)
|
---|
| 79 | return;
|
---|
[276] | 80 | Alarm alarm = (Alarm)item;
|
---|
| 81 | int uid = alarm.getUID();
|
---|
| 82 | if (uid == 0)
|
---|
| 83 | return;
|
---|
| 84 | mDataStore.removeRecordByUID(uid);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public void onItemUpdated(Object item, int index) {
|
---|
| 88 | if (sAlarmList == null) // restoring from service
|
---|
| 89 | return;
|
---|
| 90 | Alarm alarm = (Alarm)item;
|
---|
| 91 | mDataStore.setRecordDataByUID(alarm.getUID(), alarm.toByteArray(), true);
|
---|
| 92 | DEBUG.p("updated: " + alarm + " UID " + alarm.getUID());
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[258] | 95 | private static String KEY_DEFAULT_ALARM = "default alarm";
|
---|
| 96 |
|
---|
| 97 | public static Alarm getDefaultAlarm() {
|
---|
| 98 | Alarm defaultAlarm = new Alarm();
|
---|
| 99 | try {
|
---|
| 100 | defaultAlarm.fromByteArray(sSettingsDB.getBytes(KEY_DEFAULT_ALARM));
|
---|
| 101 | } catch (SettingsDBException e) {
|
---|
| 102 | defaultAlarm = new Alarm();
|
---|
| 103 | defaultAlarm.setDate(new Date());
|
---|
| 104 | defaultAlarm.setPeriod(600, false);
|
---|
| 105 | }
|
---|
| 106 | return defaultAlarm;
|
---|
| 107 | }
|
---|
| 108 | public static void setDefaultAlarm(Alarm alarm) {
|
---|
| 109 | sSettingsDB.setBytes(KEY_DEFAULT_ALARM, alarm.toByteArray());
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[255] | 112 | static class Listener extends danger.app.Listener
|
---|
| 113 | implements danger.util.ActiveList.ForEach {
|
---|
| 114 |
|
---|
| 115 | public void receive(Object item) {
|
---|
| 116 | ((Alarm)item).timeChanged();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public boolean receiveEvent(Event e) {
|
---|
| 120 | if (e.type == Event.EVENT_TIME_CHANGED) {
|
---|
| 121 | Alarms.getList().forEach(this);
|
---|
| 122 | return true;
|
---|
[278] | 123 | } else if (e.type == Event.EVENT_DATASTORE_RESTORED) {
|
---|
| 124 | String dbName = (String)e.argument;
|
---|
| 125 | DEBUG.p("DATASTORE_RESTORED: " + dbName);
|
---|
| 126 | if (!dbName.endsWith("alarms")) {
|
---|
| 127 | // because we only get/set the default alarm on demand
|
---|
| 128 | // there's no need to handle conflicts with the settings...
|
---|
| 129 | // XXX but what happens if we set a (default) alarm, then
|
---|
| 130 | // the SettingsDB restores?
|
---|
| 131 | Alarms.getList().refreshFromDataStore(true);
|
---|
| 132 | }
|
---|
[255] | 133 | }
|
---|
| 134 | return super.receiveEvent(e);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[237] | 137 | }
|
---|