[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 */);
|
---|
[276] | 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 | }
|
---|
[237] | 34 | }
|
---|
| 35 |
|
---|
[250] | 36 | public static Alarms getList() {
|
---|
[255] | 37 | if (sAlarmList == null) {
|
---|
[276] | 38 | Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
|
---|
| 39 | sSettingsDB = new SettingsDB("settings", true /* auto sync */);
|
---|
| 40 | sListener = new Listener();
|
---|
[250] | 41 | sAlarmList = new Alarms();
|
---|
[255] | 42 | }
|
---|
[250] | 43 | return sAlarmList;
|
---|
[237] | 44 | }
|
---|
| 45 |
|
---|
[250] | 46 | public static void addAlarm(Alarm alarm) {
|
---|
[259] | 47 | sAlarmList.insertItemSorted(alarm, alarm);
|
---|
[250] | 48 | }
|
---|
| 49 | public static void removeAlarm(Alarm alarm) {
|
---|
| 50 | sAlarmList.removeItem(alarm);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[276] | 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 |
|
---|
[258] | 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 |
|
---|
[255] | 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 | }
|
---|
[237] | 120 | }
|
---|