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.ActiveListView;
|
---|
7 | import danger.ui.Rect;
|
---|
8 | import danger.ui.ToolTipWindow;
|
---|
9 | import danger.util.ActiveList;
|
---|
10 | import danger.util.DEBUG;
|
---|
11 |
|
---|
12 | public class AlarmListView extends ActiveListView
|
---|
13 | implements Resources, Commands {
|
---|
14 |
|
---|
15 | // XXX some of these shouldn't conceptually be static
|
---|
16 | private static AlarmListWindow sAlarmListWindow;
|
---|
17 | private static AlarmSetDialog sAlarmSetDialog;
|
---|
18 |
|
---|
19 | private danger.app.Alarm mToolTipAlarm;
|
---|
20 | private ToolTipWindow mToolTipWindow;
|
---|
21 |
|
---|
22 | public void onDecoded() {
|
---|
23 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
24 | setList(Alarms.getList());
|
---|
25 | mToolTipAlarm = new danger.app.Alarm(0, this);
|
---|
26 | super.onDecoded();
|
---|
27 | }
|
---|
28 |
|
---|
29 | protected AlarmSetDialog alarmSetDialog() {
|
---|
30 | if (sAlarmSetDialog == null)
|
---|
31 | sAlarmSetDialog = AlarmSetDialog.getDialog();
|
---|
32 | return sAlarmSetDialog;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
36 | switch (widget) {
|
---|
37 | case Event.DEVICE_BUTTON_BACK:
|
---|
38 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
39 | Application.getCurrentApp().returnToLauncher();
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 | return super.eventWidgetUp(widget, e);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public boolean receiveEvent(Event e) {
|
---|
46 | switch (e.type) {
|
---|
47 | case EVENT_NEW_ALARM:
|
---|
48 | alarmSetDialog().editAlarm(new Alarm(), true);
|
---|
49 | return true;
|
---|
50 | case EVENT_CONFIRM_DISCARD_ALARM:
|
---|
51 | AlertWindow alert =
|
---|
52 | Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
|
---|
53 | // XXX localize
|
---|
54 | alert.setMessage("Permanently discard \u201c" +
|
---|
55 | ((Alarm)getFocusedItem()).getMessage() +
|
---|
56 | "\u201d?");
|
---|
57 | alert.show();
|
---|
58 | return true;
|
---|
59 | case EVENT_DISCARD_ALARM:
|
---|
60 | Alarms.removeAlarm((Alarm)getFocusedItem());
|
---|
61 | return true;
|
---|
62 | case Event.EVENT_ALARM: // tooltip invalid
|
---|
63 | // XXX should do this too if the time changes
|
---|
64 | showToolTip();
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 | return super.receiveEvent(e);
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected void itemActivated(Object item) {
|
---|
71 | if (item == null)
|
---|
72 | return;
|
---|
73 | alarmSetDialog().editAlarm((Alarm)item, false);
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected void itemFocused(Object item) {
|
---|
77 | mToolTipAlarm.deactivate();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void loseFocus() {
|
---|
81 | mToolTipAlarm.deactivate();
|
---|
82 | super.loseFocus();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void showToolTip() {
|
---|
86 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
87 | if (alarm == null)
|
---|
88 | return;
|
---|
89 | Rect rect = new Rect();
|
---|
90 | rect = localToGlobal(getInterestingRect(rect));
|
---|
91 | if (mToolTipWindow != null)
|
---|
92 | mToolTipWindow.hide();
|
---|
93 | mToolTipWindow =
|
---|
94 | ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
|
---|
95 | alarm.getIntervalString(),
|
---|
96 | 10, rect.bottom, 1, sAlarmListWindow);
|
---|
97 | int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
|
---|
98 | if (secondsUntilUpdate == 0)
|
---|
99 | return;
|
---|
100 | mToolTipAlarm.resetWake(secondsUntilUpdate);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public int alarmsSet() {
|
---|
104 | return getListSize();
|
---|
105 | }
|
---|
106 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
107 | sAlarmListWindow.updateAlarmCount();
|
---|
108 | super.onItemAdded(list, item, index);
|
---|
109 | // super's default behavior is to preserve the existing selection,
|
---|
110 | // which is admirable in general, but not what we want
|
---|
111 | setFocus(index);
|
---|
112 | }
|
---|
113 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
114 | ((Alarm)item).cancel();
|
---|
115 | sAlarmListWindow.updateAlarmCount();
|
---|
116 | super.onItemRemoved(list, item, index);
|
---|
117 | }
|
---|
118 | }
|
---|