1 | package net.sabi.pester;
|
---|
2 |
|
---|
3 | import danger.app.Application;
|
---|
4 | import danger.app.Event;
|
---|
5 | import danger.ui.AlertWindow;
|
---|
6 | import danger.ui.Container;
|
---|
7 | import danger.ui.Control;
|
---|
8 | import danger.ui.Font;
|
---|
9 | import danger.ui.Layout;
|
---|
10 | import danger.ui.RecordingPen;
|
---|
11 | import danger.ui.ScreenWindow;
|
---|
12 | import danger.ui.StaticText;
|
---|
13 | import danger.ui.TextField;
|
---|
14 | import danger.util.DEBUG;
|
---|
15 | import danger.util.TextInputFilter;
|
---|
16 |
|
---|
17 | public class Pester extends Application implements Resources, Commands {
|
---|
18 | private static ScreenWindow sAlarmListWindow;
|
---|
19 |
|
---|
20 | public Pester() {
|
---|
21 | sAlarmListWindow = (ScreenWindow)getResources().getScreen(ID_ALARM_LIST_SCREEN);
|
---|
22 | sAlarmListWindow.show();
|
---|
23 | }
|
---|
24 |
|
---|
25 | private void showAlertForAlarm(Alarm alarm) {
|
---|
26 | AlertWindow alert = (AlertWindow)getAlert(ID_ALARM_ALERT, this);
|
---|
27 | alert.setTitle(alarm.getDateTimeString());
|
---|
28 | alert.setMessage(alarm.getMessage());
|
---|
29 | alert.setShowTone(alarm.getAlert());
|
---|
30 |
|
---|
31 | Font font = Font.findBoldSystemFont();
|
---|
32 | StaticText label = new StaticText(getString(ID_SNOOZE_LABEL_LEFT), font);
|
---|
33 | TextField field = new TextField();
|
---|
34 | field.setAlignment(TextField.JUSTIFY_RIGHT);
|
---|
35 | field.attachInputFilter(TextInputFilter.newLengthFilter(2));
|
---|
36 | field.attachInputFilter(TextInputFilter.newNumericFilter());
|
---|
37 | label.setLeft(35); // XXX alert.getMessageView().getLeft()
|
---|
38 | Layout.positionToRight(field, label, 3);
|
---|
39 | Layout.alignBaseline(label, field);
|
---|
40 | alert.addChildBelowMessageArea(label, label.getLeft(), label.getTop());
|
---|
41 | alert.addChildBelowMessageArea(field, field.getLeft(), field.getTop());
|
---|
42 | label = new StaticText(getString(ID_SNOOZE_LABEL_RIGHT), font);
|
---|
43 | Layout.positionToRight(label, field, 3);
|
---|
44 | Layout.alignBaseline(label, field);
|
---|
45 | alert.addChildBelowMessageArea(label, label.getLeft(), label.getTop());
|
---|
46 |
|
---|
47 | alert.setEventArgForControlWithID(ID_RESCHEDULE_BUTTON, alarm);
|
---|
48 | alert.setEventArgForControlWithID(ID_DISMISS_BUTTON, alarm);
|
---|
49 | alert.setDefaultFocus(field);
|
---|
50 |
|
---|
51 | alert.paint(new RecordingPen()); // force layout
|
---|
52 | int fieldID = alert.incrementMaxChildID();
|
---|
53 | alert.removeChild(field);
|
---|
54 | alert.addChild(field, fieldID);
|
---|
55 | Control snoozeButton = alert.getControlWithID(ID_SNOOZE_BUTTON);
|
---|
56 | Event event = snoozeButton.getEvent();
|
---|
57 | event.argument = alert;
|
---|
58 | event.data = fieldID;
|
---|
59 | snoozeButton.setEvent(event);
|
---|
60 | alert.disableButtonOnEmptyField(ID_SNOOZE_BUTTON, fieldID);
|
---|
61 |
|
---|
62 | alert.show(AlertWindow.SYSTEM_ALERT_TYPE_ALARM);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean receiveEvent(Event e) {
|
---|
66 | switch (e.type) {
|
---|
67 | case Event.EVENT_ALARM:
|
---|
68 | showAlertForAlarm((Alarm)e.argument);
|
---|
69 | return true;
|
---|
70 | case EVENT_SET_ALARM:
|
---|
71 | AlarmSetDialog.getDialog().rescheduleAlarm((Alarm)e.argument);
|
---|
72 | return true;
|
---|
73 | case EVENT_SNOOZE_ALARM:
|
---|
74 | AlertWindow alert = (AlertWindow)e.argument;
|
---|
75 | int snoozeMinutes =
|
---|
76 | Integer.parseInt(alert.getControlWithID(e.data).toString());
|
---|
77 | Control dismissButton = alert.getControlWithID(ID_DISMISS_BUTTON);
|
---|
78 | Alarm alarm = (Alarm)dismissButton.getEvent().argument;
|
---|
79 | alarm.snoozeForMinutes(snoozeMinutes);
|
---|
80 | return true;
|
---|
81 | case EVENT_DISMISS_ALARM:
|
---|
82 | ((Alarm)e.argument).dismiss();
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 | return super.receiveEvent(e);
|
---|
86 | }
|
---|
87 | }
|
---|