1 | package net.sabi.pester;
|
---|
2 |
|
---|
3 | import danger.app.Application;
|
---|
4 | import danger.app.Event;
|
---|
5 | import danger.internal.Date;
|
---|
6 | import danger.ui.ActiveListView;
|
---|
7 | import danger.ui.Button;
|
---|
8 | import danger.ui.CheckBox;
|
---|
9 | import danger.ui.DateTimeEditor;
|
---|
10 | import danger.ui.DatePicker;
|
---|
11 | import danger.ui.DialogWindow;
|
---|
12 | import danger.ui.Menu;
|
---|
13 | import danger.ui.MenuItem;
|
---|
14 | import danger.ui.NumberEditor;
|
---|
15 | import danger.ui.PopupMenu;
|
---|
16 | import danger.ui.RadioButton;
|
---|
17 | import danger.ui.Rect;
|
---|
18 | import danger.ui.TypeAheadTextField;
|
---|
19 | import danger.util.ActiveList;
|
---|
20 | import danger.util.StdActiveList;
|
---|
21 | import danger.util.DEBUG;
|
---|
22 |
|
---|
23 | public class AlarmListView extends ActiveListView
|
---|
24 | implements Resources, Commands {
|
---|
25 | private static Pester sPester;
|
---|
26 | private static AlarmListWindow sAlarmListWindow;
|
---|
27 | // XXX some of these shouldn't conceptually be static, but who cares
|
---|
28 | private static StdActiveList sAlarmList;
|
---|
29 | private static Alarms sAlarms;
|
---|
30 |
|
---|
31 | private static DialogWindow sEditWindow;
|
---|
32 | private TypeAheadTextField sMessageField;
|
---|
33 | private RadioButton sInButton, sAtButton;
|
---|
34 | private NumberEditor sPeriodField;
|
---|
35 | private Menu sPeriodUnitsMenu;
|
---|
36 | private PopupMenu sPeriodUnitsPopup;
|
---|
37 | // XXX don't allow <5 second repeating alarms
|
---|
38 | private CheckBox sRepeatCheckBox;
|
---|
39 | private DateTimeEditor sTimeEditor, sDateEditor;
|
---|
40 | private DatePicker sDatePicker;
|
---|
41 | private Button sDiscardAlarmButton, sSetAlarmButton;
|
---|
42 |
|
---|
43 | private static Alarm sFocusedAlarm, sEditingAlarm;
|
---|
44 |
|
---|
45 | private static final int EVENT_CANCEL_ALARM = -1; // XXX move this?
|
---|
46 |
|
---|
47 | public void onDecoded() {
|
---|
48 | sPester = (Pester)Application.getCurrentApp();
|
---|
49 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
50 | sAlarmList = new StdActiveList();
|
---|
51 | setList(sAlarmList);
|
---|
52 |
|
---|
53 | sEditWindow = sPester.getDialog(ID_ALARM_SET_DIALOG, this);
|
---|
54 | sEditWindow.setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
|
---|
55 | sMessageField = (TypeAheadTextField)sEditWindow.getDescendantWithID(ID_MESSAGE_FIELD);
|
---|
56 | sEditWindow.disableBottomRightButtonOnEmptyField(sMessageField);
|
---|
57 | sInButton = (RadioButton)sEditWindow.getDescendantWithID(ID_IN_BUTTON);
|
---|
58 | sAtButton = (RadioButton)sEditWindow.getDescendantWithID(ID_AT_BUTTON);
|
---|
59 | sPeriodField = (NumberEditor)sEditWindow.getDescendantWithID(ID_PERIOD_FIELD);
|
---|
60 | sPeriodUnitsMenu = (Menu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_MENU);
|
---|
61 | sPeriodUnitsPopup = (PopupMenu)sEditWindow.getDescendantWithID(ID_PERIOD_UNITS_POPUP);
|
---|
62 | sRepeatCheckBox = (CheckBox)sEditWindow.getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
|
---|
63 | sTimeEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_TIME_EDITOR);
|
---|
64 | sDateEditor = (DateTimeEditor)sEditWindow.getDescendantWithID(ID_DATE_EDITOR);
|
---|
65 | sDatePicker = (DatePicker)sEditWindow.getDescendantWithID(ID_DATE_PICKER);
|
---|
66 | sSetAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_SET_ALARM_BUTTON);
|
---|
67 | sDiscardAlarmButton = (Button)sEditWindow.getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
|
---|
68 | sInButton.setValue(1);
|
---|
69 | sDateEditor.limitToDangerEpoch();
|
---|
70 |
|
---|
71 | super.onDecoded();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
75 | switch (widget) {
|
---|
76 | case Event.DEVICE_BUTTON_BACK:
|
---|
77 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
78 | sPester.returnToLauncher();
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 | return super.eventWidgetUp(widget, e);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void editDateOrPeriod(Alarm alarm) {
|
---|
85 | boolean usesPeriod = alarm.getUsesPeriod();
|
---|
86 | if (usesPeriod) {
|
---|
87 | int period = alarm.getPeriod();
|
---|
88 | for (int i = sPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
|
---|
89 | MenuItem item = sPeriodUnitsPopup.getItem(i);
|
---|
90 | int unitsPerSec = item.getEvent().data;
|
---|
91 | if (period % unitsPerSec == 0) {
|
---|
92 | sPeriodUnitsPopup.setValueWithItemID(sPeriodUnitsMenu.getIDOfItem(item));
|
---|
93 | sPeriodField.setValue(period / unitsPerSec);
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | sRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
|
---|
98 | }
|
---|
99 |
|
---|
100 | Date date = alarm.getDate();
|
---|
101 | sTimeEditor.setDate(date);
|
---|
102 | sDateEditor.setDate(date);
|
---|
103 | sDatePicker.setDate(date);
|
---|
104 |
|
---|
105 | sInButton.setValue(usesPeriod ? 1 : 0);
|
---|
106 | sAtButton.setValue(usesPeriod ? 0 : 1);
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected void editAlarm(Alarm alarm, boolean asNew) {
|
---|
110 | sEditingAlarm = alarm;
|
---|
111 | alarm.beginEditing();
|
---|
112 | sMessageField.setText(alarm.getMessage());
|
---|
113 | if (asNew) {
|
---|
114 | sDiscardAlarmButton.hide();
|
---|
115 | } else {
|
---|
116 | sDiscardAlarmButton.show();
|
---|
117 | editDateOrPeriod(alarm);
|
---|
118 | }
|
---|
119 | constrainDate();
|
---|
120 | sEditWindow.show();
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected Date editingDate() {
|
---|
124 | Date date = sDateEditor.getDate();
|
---|
125 | date.setTime(sTimeEditor.getDate().getTime());
|
---|
126 | date.setSeconds(0);
|
---|
127 | return date;
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected void constrainDate() {
|
---|
131 | // XXX schedule every minute (second?) if in AT mode
|
---|
132 | // XXX could use onPeriodicPulse() perhaps
|
---|
133 | Date now = new Date();
|
---|
134 | sDateEditor.min(now);
|
---|
135 | sDatePicker.min(now);
|
---|
136 | sSetAlarmButton.setEnabled(sInButton.getValue() == 1 ||
|
---|
137 | editingDate().compareTo(now) > 0);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public boolean receiveEvent(Event e) {
|
---|
141 | switch (e.type) {
|
---|
142 | case EVENT_NEW_ALARM:
|
---|
143 | editAlarm(new Alarm(), true);
|
---|
144 | return true;
|
---|
145 | case EVENT_SET_ALARM:
|
---|
146 | sEditingAlarm.setMessage(sMessageField.toString());
|
---|
147 | if (sInButton.getValue() == 1) {
|
---|
148 | int itemID = sPeriodUnitsPopup.getValueAsItemID();
|
---|
149 | MenuItem itemWithID = sPeriodUnitsMenu.getItemWithID(itemID);
|
---|
150 | Event event = itemWithID.getEvent();
|
---|
151 | sEditingAlarm.setPeriod(sPeriodField.getValue() * event.data,
|
---|
152 | sRepeatCheckBox.getValue() == 1);
|
---|
153 | } else {
|
---|
154 | sEditingAlarm.setDate(editingDate());
|
---|
155 | }
|
---|
156 | // schedule before adding so the tooltip is correct
|
---|
157 | sEditingAlarm.schedule();
|
---|
158 | if (sEditingAlarm != sFocusedAlarm) { // new alarm
|
---|
159 | sAlarmList.addItem(sEditingAlarm);
|
---|
160 | setFocusedItem(sEditingAlarm);
|
---|
161 | sFocusedAlarm = sEditingAlarm;
|
---|
162 | }
|
---|
163 | return true;
|
---|
164 | case EVENT_CANCEL_ALARM:
|
---|
165 | if (sEditingAlarm == sFocusedAlarm) // existing alarm
|
---|
166 | sEditingAlarm.resume();
|
---|
167 | sEditingAlarm = null;
|
---|
168 | return true;
|
---|
169 | case EVENT_DISCARD_ALARM:
|
---|
170 | // XXX confirmation
|
---|
171 | sAlarmList.removeItem(sFocusedAlarm);
|
---|
172 | return true;
|
---|
173 | case EVENT_IN:
|
---|
174 | sAtButton.setValue(0);
|
---|
175 | constrainDate();
|
---|
176 | return true;
|
---|
177 | case EVENT_AT:
|
---|
178 | sInButton.setValue(0);
|
---|
179 | constrainDate();
|
---|
180 | return true;
|
---|
181 | case EVENT_TIME_EDITOR:
|
---|
182 | Date date = editingDate();
|
---|
183 | // XXX do this on exit from the field, not on change
|
---|
184 | if (date.compareTo(new Date()) <= 0) {
|
---|
185 | date.addDays(1);
|
---|
186 | sDatePicker.setDate(date);
|
---|
187 | sDateEditor.setDate(date);
|
---|
188 | }
|
---|
189 | constrainDate();
|
---|
190 | return true;
|
---|
191 | case EVENT_DATE_EDITOR:
|
---|
192 | sDatePicker.setDate(sDateEditor.getDate());
|
---|
193 | constrainDate();
|
---|
194 | return true;
|
---|
195 | case EVENT_DATE_PICKER:
|
---|
196 | sDateEditor.setDate((Date)e.argument);
|
---|
197 | constrainDate();
|
---|
198 | return true;
|
---|
199 | }
|
---|
200 | return super.receiveEvent(e);
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected void itemActivated(Object item) {
|
---|
204 | editAlarm(sFocusedAlarm, false);
|
---|
205 | }
|
---|
206 | protected void itemFocused(Object item) {
|
---|
207 | sFocusedAlarm = (Alarm)item;
|
---|
208 | }
|
---|
209 |
|
---|
210 | public int alarmsSet() {
|
---|
211 | return getListSize();
|
---|
212 | }
|
---|
213 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
214 | sAlarmListWindow.updateAlarmCount();
|
---|
215 | super.onItemAdded(list, item, index);
|
---|
216 | }
|
---|
217 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
218 | ((Alarm)item).cancel();
|
---|
219 | sAlarmListWindow.updateAlarmCount();
|
---|
220 | super.onItemRemoved(list, item, index);
|
---|
221 | }
|
---|
222 |
|
---|
223 | protected CharSequence getToolTipForItem(Object item) {
|
---|
224 | if (item == null) // this is dumb, why do we get asked for a tooltip?
|
---|
225 | return null;
|
---|
226 | // XXX need "in XXX minutes" too
|
---|
227 | return ((Alarm)item).getDateTimeString();
|
---|
228 | }
|
---|
229 | }
|
---|