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.Font;
|
---|
8 | import danger.ui.Pen;
|
---|
9 | import danger.ui.Rect;
|
---|
10 | import danger.ui.Style;
|
---|
11 | import danger.ui.ToolTipWindow;
|
---|
12 | import danger.util.ActiveList;
|
---|
13 | import danger.util.DEBUG;
|
---|
14 |
|
---|
15 | public class AlarmListView extends ActiveListView
|
---|
16 | implements Resources, Commands {
|
---|
17 |
|
---|
18 | // XXX some of these shouldn't conceptually be static
|
---|
19 | private static AlarmListWindow sAlarmListWindow;
|
---|
20 | private static AlarmSetDialog sAlarmSetDialog;
|
---|
21 |
|
---|
22 | private danger.app.Alarm mToolTipAlarm;
|
---|
23 | private ToolTipWindow mToolTipWindow;
|
---|
24 |
|
---|
25 | public void onDecoded() {
|
---|
26 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
27 | mToolTipAlarm = new danger.app.Alarm(0, this);
|
---|
28 | setAutoResize(true);
|
---|
29 | setList(Alarms.getList());
|
---|
30 | Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
|
---|
31 | super.onDecoded();
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected AlarmSetDialog alarmSetDialog() {
|
---|
35 | if (sAlarmSetDialog == null)
|
---|
36 | sAlarmSetDialog = AlarmSetDialog.getDialog();
|
---|
37 | return sAlarmSetDialog;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
41 | switch (widget) {
|
---|
42 | case Event.DEVICE_BUTTON_BACK:
|
---|
43 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
44 | Application.getCurrentApp().returnToLauncher();
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 | return super.eventWidgetUp(widget, e);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public boolean receiveEvent(Event e) {
|
---|
51 | switch (e.type) {
|
---|
52 | case EVENT_NEW_ALARM:
|
---|
53 | // XXX check for max alarms
|
---|
54 | alarmSetDialog().editAlarm(new Alarm(), true);
|
---|
55 | return true;
|
---|
56 | case EVENT_DISCARD_ALARM:
|
---|
57 | mToolTipAlarm.deactivate();
|
---|
58 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
59 | alarm.beginEditing();
|
---|
60 | AlertWindow alert =
|
---|
61 | Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
|
---|
62 | alert.setMessage("Permanently discard \u201c" +
|
---|
63 | alarm.getMessage() + "\u201d?"); // XXX localize
|
---|
64 | alert.show();
|
---|
65 | return true;
|
---|
66 | case EVENT_CONFIRM_DISCARD:
|
---|
67 | Alarms.removeAlarm((Alarm)getFocusedItem());
|
---|
68 | return true;
|
---|
69 | case EVENT_CANCEL_DISCARD:
|
---|
70 | ((Alarm)getFocusedItem()).resume();
|
---|
71 | return true;
|
---|
72 | // XXX for EVENT_TIME_FORMAT_CHANGED, we also need to force a
|
---|
73 | // full redraw (in addition to invalidating the tooltip)
|
---|
74 | case Event.EVENT_TIME_CHANGED:
|
---|
75 | if (mToolTipWindow == null || !mToolTipWindow.isVisible())
|
---|
76 | return true;
|
---|
77 | case Event.EVENT_ALARM: // tooltip invalid
|
---|
78 | showToolTip();
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 | return super.receiveEvent(e);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void itemActivated(Object item) {
|
---|
85 | if (item == null)
|
---|
86 | return;
|
---|
87 | alarmSetDialog().editAlarm((Alarm)item, false);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void itemFocused(Object item) {
|
---|
91 | mToolTipAlarm.deactivate();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void loseFocus() {
|
---|
95 | mToolTipAlarm.deactivate();
|
---|
96 | super.loseFocus();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void showToolTip() {
|
---|
100 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
101 | if (alarm == null)
|
---|
102 | return;
|
---|
103 | Rect rect = new Rect();
|
---|
104 | rect = localToGlobal(getInterestingRect(rect));
|
---|
105 | if (mToolTipWindow != null)
|
---|
106 | mToolTipWindow.hide();
|
---|
107 | mToolTipWindow =
|
---|
108 | ToolTipWindow.showToolTip(alarm.getDateTimeString() + " - " +
|
---|
109 | alarm.getIntervalString(),
|
---|
110 | localToGlobalH(10),
|
---|
111 | localToGlobalV(mToolTipY),
|
---|
112 | 1, sAlarmListWindow);
|
---|
113 | int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
|
---|
114 | if (secondsUntilUpdate == 0)
|
---|
115 | return;
|
---|
116 | mToolTipAlarm.resetWake(secondsUntilUpdate);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected void paintEmptyList(Pen p, int width, int height) {
|
---|
120 | Style style = getStyle();
|
---|
121 | Font font = style.getFont(Style.DISABLED_LABEL_FONT);
|
---|
122 | int y = (height / 4) - (font.getAscent() / 2);
|
---|
123 | if (y < 0) {
|
---|
124 | setHeight(sAlarmListWindow.getHeight());
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | // XXX localize
|
---|
128 | String emptyMessage =
|
---|
129 | "Press " + Font.GLYPH_MENU + " to set an alarm.";
|
---|
130 | int x = (width / 2) - (font.getWidth(emptyMessage) / 2);
|
---|
131 | p.setFont(font);
|
---|
132 | p.setColor(style.getColor(Style.DISABLED_LABEL_COLOR));
|
---|
133 | p.drawText(x, y, emptyMessage);
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected void paintItemLine(Pen p, int left, int top, int right, int bottom,
|
---|
137 | boolean focused, Object item) {
|
---|
138 | if (focused) {
|
---|
139 | paintSelection(p, left, top, right, bottom);
|
---|
140 | p.setColor(getStyle().getColor(Style.BACKGROUND_COLOR));
|
---|
141 | } else {
|
---|
142 | p.setColor(getStyle().getColor(Style.FOREGROUND_COLOR));
|
---|
143 | }
|
---|
144 | Font font = p.getFont();
|
---|
145 | int descent = font.getDescent();
|
---|
146 | int height = descent + font.getAscent();
|
---|
147 | top = ((top + bottom) / 2 + height / 2) - descent;
|
---|
148 | Alarm alarm = (Alarm)item;
|
---|
149 | left += 2;
|
---|
150 | left += p.drawText(left, top, alarm.getDateTimeString(true));
|
---|
151 | p.setFont(font.findVariant(null, -1, Font.F_BOLD));
|
---|
152 | // XXX ellipsize
|
---|
153 | p.drawText(left, top, " " + alarm.getMessage());
|
---|
154 | p.setFont(font);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public int alarmsSet() {
|
---|
158 | return getListSize();
|
---|
159 | }
|
---|
160 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
161 | sAlarmListWindow.updateAlarmCount();
|
---|
162 | super.onItemAdded(list, item, index);
|
---|
163 | // super's default behavior is to preserve the existing selection,
|
---|
164 | // which is admirable in general, but not what we want
|
---|
165 | setFocus(index);
|
---|
166 | }
|
---|
167 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
168 | ((Alarm)item).cancel();
|
---|
169 | sAlarmListWindow.updateAlarmCount();
|
---|
170 | super.onItemRemoved(list, item, index);
|
---|
171 | }
|
---|
172 | }
|
---|