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