[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;
|
---|
[237] | 11 |
|
---|
[250] | 12 | public class Alarms extends StdActiveList {
|
---|
[237] | 13 | // max # records in a datastore
|
---|
| 14 | private static final int MAX_ALARM_COUNT = 50;
|
---|
| 15 |
|
---|
[250] | 16 | private static Alarms sAlarmList = null;
|
---|
[255] | 17 | private static Listener sListener;
|
---|
[258] | 18 | private static SettingsDB sSettingsDB;
|
---|
[237] | 19 |
|
---|
[255] | 20 | private DataStore mDataStore;
|
---|
| 21 |
|
---|
[237] | 22 | private Alarms() {
|
---|
[255] | 23 | mDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
|
---|
[237] | 24 | }
|
---|
| 25 |
|
---|
[250] | 26 | public static Alarms getList() {
|
---|
[255] | 27 | if (sAlarmList == null) {
|
---|
[250] | 28 | sAlarmList = new Alarms();
|
---|
[255] | 29 | sListener = new Listener();
|
---|
[258] | 30 | sSettingsDB = new SettingsDB("settings", true /* auto sync */);
|
---|
[255] | 31 | Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
|
---|
| 32 | }
|
---|
[250] | 33 | return sAlarmList;
|
---|
[237] | 34 | }
|
---|
| 35 |
|
---|
[250] | 36 | public static void addAlarm(Alarm alarm) {
|
---|
| 37 | sAlarmList.addItem(alarm);
|
---|
| 38 | }
|
---|
| 39 | public static void removeAlarm(Alarm alarm) {
|
---|
| 40 | sAlarmList.removeItem(alarm);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[258] | 43 | private static String KEY_DEFAULT_ALARM = "default alarm";
|
---|
| 44 |
|
---|
| 45 | public static Alarm getDefaultAlarm() {
|
---|
| 46 | Alarm defaultAlarm = new Alarm();
|
---|
| 47 | try {
|
---|
| 48 | defaultAlarm.fromByteArray(sSettingsDB.getBytes(KEY_DEFAULT_ALARM));
|
---|
| 49 | } catch (SettingsDBException e) {
|
---|
| 50 | defaultAlarm = new Alarm();
|
---|
| 51 | defaultAlarm.setDate(new Date());
|
---|
| 52 | defaultAlarm.setPeriod(600, false);
|
---|
| 53 | }
|
---|
| 54 | return defaultAlarm;
|
---|
| 55 | }
|
---|
| 56 | public static void setDefaultAlarm(Alarm alarm) {
|
---|
| 57 | sSettingsDB.setBytes(KEY_DEFAULT_ALARM, alarm.toByteArray());
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[237] | 60 | /*
|
---|
| 61 | private static Alarm sAlarm0 = new Alarm();
|
---|
| 62 | private static Alarm sAlarm1 = new Alarm();
|
---|
| 63 | public int compare(Object arg0, Object arg1) {
|
---|
| 64 |
|
---|
| 65 | return Collator.getInstance().compare(
|
---|
| 66 | }
|
---|
| 67 | */
|
---|
[255] | 68 |
|
---|
| 69 | static class Listener extends danger.app.Listener
|
---|
| 70 | implements danger.util.ActiveList.ForEach {
|
---|
| 71 |
|
---|
| 72 | public void receive(Object item) {
|
---|
| 73 | ((Alarm)item).timeChanged();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public boolean receiveEvent(Event e) {
|
---|
| 77 | if (e.type == Event.EVENT_TIME_CHANGED) {
|
---|
| 78 | // XXX do we get this when the time zone changes?
|
---|
| 79 | Alarms.getList().forEach(this);
|
---|
| 80 | return true;
|
---|
| 81 | }
|
---|
| 82 | return super.receiveEvent(e);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[237] | 85 | }
|
---|
[255] | 86 |
|
---|