1 | package net.sabi.pester;
|
---|
2 |
|
---|
3 | import danger.app.Application;
|
---|
4 | import danger.app.Event;
|
---|
5 | import danger.audio.ToneClass;
|
---|
6 | import danger.audio.ToneRights;
|
---|
7 | import danger.internal.Date;
|
---|
8 | import danger.ui.Button;
|
---|
9 | import danger.ui.CheckBox;
|
---|
10 | import danger.ui.Color;
|
---|
11 | import danger.ui.DateTimeEditor;
|
---|
12 | import danger.ui.DatePicker;
|
---|
13 | import danger.ui.DialogWindow;
|
---|
14 | import danger.ui.Font;
|
---|
15 | import danger.ui.Layout;
|
---|
16 | import danger.ui.MenuItem;
|
---|
17 | import danger.ui.NumberEditor;
|
---|
18 | import danger.ui.Pen;
|
---|
19 | import danger.ui.PopupMenu;
|
---|
20 | import danger.ui.RadioButton;
|
---|
21 | import danger.ui.Rect;
|
---|
22 | import danger.ui.RingTonePicker;
|
---|
23 | import danger.ui.StaticTextBox;
|
---|
24 | import danger.ui.TypeAheadTextField;
|
---|
25 | import danger.util.DEBUG;
|
---|
26 |
|
---|
27 | public class AlarmSetDialog extends DialogWindow
|
---|
28 | implements Resources, Commands {
|
---|
29 |
|
---|
30 | private TypeAheadTextField mMessageField;
|
---|
31 | private RadioButton mInButton, mAtButton;
|
---|
32 | private NumberEditor mPeriodField;
|
---|
33 | private PopupMenu mPeriodUnitsPopup;
|
---|
34 | // XXX don't allow <5 second repeating alarms
|
---|
35 | private CheckBox mRepeatCheckBox;
|
---|
36 | private DateTimeEditor mTimeEditor, mDateEditor;
|
---|
37 | private DatePicker mDatePicker;
|
---|
38 | private RingTonePicker mAlertPicker;
|
---|
39 | private Button mDiscardAlarmButton, mSetAlarmButton;
|
---|
40 | private StaticTextBox mStatusBox;
|
---|
41 |
|
---|
42 | private Alarm mEditingAlarm;
|
---|
43 | private boolean mAlarmIsNew;
|
---|
44 | private boolean haveShownWindow = false;
|
---|
45 |
|
---|
46 | public static AlarmSetDialog getDialog() {
|
---|
47 | AlarmSetDialog dialog = (AlarmSetDialog)
|
---|
48 | Application.getCurrentApp().getDialog(ID_ALARM_SET_DIALOG, null);
|
---|
49 | return dialog;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void onDecoded() {
|
---|
53 | setCancelButtonEvent(new Event(this, EVENT_CANCEL_ALARM));
|
---|
54 | mMessageField =
|
---|
55 | (TypeAheadTextField)getDescendantWithID(ID_MESSAGE_FIELD);
|
---|
56 | disableBottomRightButtonOnEmptyField(mMessageField);
|
---|
57 | mInButton = (RadioButton)getDescendantWithID(ID_IN_BUTTON);
|
---|
58 | mAtButton = (RadioButton)getDescendantWithID(ID_AT_BUTTON);
|
---|
59 | mPeriodField = (NumberEditor)getDescendantWithID(ID_PERIOD_FIELD);
|
---|
60 | mPeriodUnitsPopup =
|
---|
61 | (PopupMenu)getDescendantWithID(ID_PERIOD_UNITS_POPUP);
|
---|
62 | mRepeatCheckBox =
|
---|
63 | (CheckBox)getDescendantWithID(ID_PERIOD_REPEAT_CHECKBOX);
|
---|
64 | mTimeEditor = (DateTimeEditor)getDescendantWithID(ID_TIME_EDITOR);
|
---|
65 | mDateEditor = (DateTimeEditor)getDescendantWithID(ID_DATE_EDITOR);
|
---|
66 | mDatePicker = (DatePicker)getDescendantWithID(ID_DATE_PICKER);
|
---|
67 | mAlertPicker = (RingTonePicker)getDescendantWithID(ID_ALERT_PICKER);
|
---|
68 | mSetAlarmButton = (Button)getDescendantWithID(ID_SET_ALARM_BUTTON);
|
---|
69 | mDiscardAlarmButton =
|
---|
70 | (Button)getDescendantWithID(ID_DISCARD_ALARM_BUTTON);
|
---|
71 | mInButton.setValue(1);
|
---|
72 | mDateEditor.limitToDangerEpoch();
|
---|
73 | mAlertPicker.setGroupFilter(ToneClass.FOREGROUND | ToneClass.CUSTOM);
|
---|
74 | mAlertPicker.setRights(ToneRights.FULL);
|
---|
75 | // this doesn't work properly yet
|
---|
76 | // mAlertPicker.setCanRecord(true);
|
---|
77 |
|
---|
78 | mStatusBox = new StaticTextBox();
|
---|
79 | mStatusBox.setTransparent(true);
|
---|
80 | mStatusBox.setHasBorder(false);
|
---|
81 | mStatusBox.setFont(Font.findSystemFont());
|
---|
82 | mStatusBox.setAutoResize(false);
|
---|
83 |
|
---|
84 | super.onDecoded();
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected void editDateOrPeriod(Alarm alarm) {
|
---|
88 | boolean usesPeriod = alarm.getUsesPeriod();
|
---|
89 | if (usesPeriod) {
|
---|
90 | int period = alarm.getPeriod();
|
---|
91 | for (int i = mPeriodUnitsPopup.itemCount() - 1 ; i >= 0 ; --i) {
|
---|
92 | int unitsPerSec = mPeriodUnitsPopup.getItem(i).getEvent().data;
|
---|
93 | if (period % unitsPerSec == 0) {
|
---|
94 | mPeriodUnitsPopup.setValue(i);
|
---|
95 | mPeriodField.setValue(period / unitsPerSec);
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | mRepeatCheckBox.setValue(alarm.getRepeating() ? 1 : 0);
|
---|
100 | }
|
---|
101 |
|
---|
102 | Date date = alarm.getDate();
|
---|
103 | mTimeEditor.setDate(date);
|
---|
104 | mDateEditor.setDate(date);
|
---|
105 | mDatePicker.setDate(date);
|
---|
106 |
|
---|
107 | mInButton.setValue(usesPeriod ? 1 : 0);
|
---|
108 | mAtButton.setValue(usesPeriod ? 0 : 1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void editAlarm(Alarm alarm, boolean asNew) {
|
---|
112 | mEditingAlarm = alarm;
|
---|
113 | mAlarmIsNew = asNew;
|
---|
114 | alarm.beginEditing();
|
---|
115 | if (asNew) {
|
---|
116 | mDiscardAlarmButton.hide();
|
---|
117 | // XXX default to now?
|
---|
118 | } else {
|
---|
119 | mMessageField.setText(alarm.getMessage());
|
---|
120 | mDiscardAlarmButton.show();
|
---|
121 | editDateOrPeriod(alarm);
|
---|
122 | mAlertPicker.setRingTone(mEditingAlarm.getAlert());
|
---|
123 | }
|
---|
124 | validate();
|
---|
125 | show();
|
---|
126 | if (!haveShownWindow) {
|
---|
127 | Rect alarmRect = mSetAlarmButton.getFrame();
|
---|
128 | Rect contentRect = getContentRect();
|
---|
129 | mStatusBox.setPosition(contentRect.left + 8, alarmRect.top + 2);
|
---|
130 | mStatusBox.setSize(alarmRect.left - 8, 11);
|
---|
131 | mStatusBox.show();
|
---|
132 | addChild(mStatusBox);
|
---|
133 | haveShownWindow = true;
|
---|
134 | setAutoSize(false); // don't resize to contain it next time
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected boolean isIn() {
|
---|
139 | return (mInButton.getValue() == 1);
|
---|
140 | }
|
---|
141 |
|
---|
142 | protected int editingInterval() {
|
---|
143 | int itemIndex = mPeriodUnitsPopup.getValue();
|
---|
144 | MenuItem itemAtIndex = mPeriodUnitsPopup.getItem(itemIndex);
|
---|
145 | Event event = itemAtIndex.getEvent();
|
---|
146 | return mPeriodField.getValue() * event.data;
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected Date editingDate() {
|
---|
150 | Date date;
|
---|
151 | if (isIn()) {
|
---|
152 | date = new Date();
|
---|
153 | date.addSeconds(editingInterval());
|
---|
154 | } else {
|
---|
155 | date = mDateEditor.getDate();
|
---|
156 | date.setTime(mTimeEditor.getDate().getTime());
|
---|
157 | date.setSeconds(0);
|
---|
158 | }
|
---|
159 | return date;
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected boolean isValid() {
|
---|
163 | // doesn't redraw properly otherwise
|
---|
164 | invalidate(mStatusBox.getBounds());
|
---|
165 | if (mMessageField.length() == 0) {
|
---|
166 | mStatusBox.setText("Specify a message.");
|
---|
167 | return false;
|
---|
168 | }
|
---|
169 | Date now = new Date();
|
---|
170 | mDateEditor.min(now);
|
---|
171 | mDatePicker.min(now);
|
---|
172 | if (!isIn() && editingDate().compareTo(now) <= 0) {
|
---|
173 | mStatusBox.setText("Specify a future date.");
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 | mStatusBox.setText(Alarm.dateTimeString(editingDate(), true));
|
---|
177 | // XXX invalidate @ alarm date if isIn()
|
---|
178 | // XXX invalidate in 1 sec if isAt()
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 |
|
---|
182 | protected void validate() {
|
---|
183 | mSetAlarmButton.setEnabled(isValid());
|
---|
184 | }
|
---|
185 |
|
---|
186 | public boolean receiveEvent(Event e) {
|
---|
187 | switch (e.type) {
|
---|
188 | case EVENT_SET_ALARM:
|
---|
189 | if (!isValid())
|
---|
190 | return true;
|
---|
191 | mEditingAlarm.setMessage(mMessageField.toString());
|
---|
192 | if (isIn()) {
|
---|
193 | mEditingAlarm.setPeriod(editingInterval(),
|
---|
194 | mRepeatCheckBox.getValue() == 1);
|
---|
195 | } else {
|
---|
196 | mEditingAlarm.setDate(editingDate());
|
---|
197 | }
|
---|
198 | mEditingAlarm.setAlert(mAlertPicker.getRingTone());
|
---|
199 | // schedule before adding so the tooltip is correct
|
---|
200 | mEditingAlarm.schedule();
|
---|
201 | if (mAlarmIsNew) Alarms.addAlarm(mEditingAlarm);
|
---|
202 | mEditingAlarm = null;
|
---|
203 | return true;
|
---|
204 | case EVENT_CANCEL_ALARM:
|
---|
205 | if (!mAlarmIsNew) mEditingAlarm.resume();
|
---|
206 | mEditingAlarm = null;
|
---|
207 | return true;
|
---|
208 | case EVENT_DISCARD_ALARM:
|
---|
209 | // XXX confirmation
|
---|
210 | Alarms.removeAlarm(mEditingAlarm);
|
---|
211 | mEditingAlarm = null;
|
---|
212 | return true;
|
---|
213 | case EVENT_VALIDATE: // something changed
|
---|
214 | validate();
|
---|
215 | return true;
|
---|
216 | case EVENT_IN:
|
---|
217 | mAtButton.setValue(0);
|
---|
218 | validate();
|
---|
219 | return true;
|
---|
220 | case EVENT_AT:
|
---|
221 | mInButton.setValue(0);
|
---|
222 | validate();
|
---|
223 | return true;
|
---|
224 | case EVENT_TIME_EDITOR:
|
---|
225 | validate();
|
---|
226 | return true;
|
---|
227 | case EVENT_DATE_EDITOR_TOOK_FOCUS:
|
---|
228 | Date date = editingDate();
|
---|
229 | if (date.compareTo(new Date()) <= 0) {
|
---|
230 | date.addDays(1);
|
---|
231 | mDatePicker.setDate(date);
|
---|
232 | mDateEditor.setDate(date);
|
---|
233 | }
|
---|
234 | validate();
|
---|
235 | return true;
|
---|
236 | case EVENT_DATE_EDITOR:
|
---|
237 | mDatePicker.setDate(mDateEditor.getDate());
|
---|
238 | validate();
|
---|
239 | return true;
|
---|
240 | case EVENT_DATE_PICKER:
|
---|
241 | mDateEditor.setDate((Date)e.argument);
|
---|
242 | validate();
|
---|
243 | return true;
|
---|
244 | }
|
---|
245 | return super.receiveEvent(e);
|
---|
246 | }
|
---|
247 | }
|
---|