[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;
|
---|
[237] | 14 |
|
---|
| 15 | public class AlarmListView extends ActiveListView
|
---|
| 16 | implements Resources, Commands {
|
---|
[250] | 17 |
|
---|
| 18 | // XXX some of these shouldn't conceptually be static
|
---|
[237] | 19 | private static AlarmListWindow sAlarmListWindow;
|
---|
[250] | 20 | private static AlarmSetDialog sAlarmSetDialog;
|
---|
[237] | 21 |
|
---|
[273] | 22 | private danger.app.Alarm mToolTipAlarm;
|
---|
| 23 | private ToolTipWindow mToolTipWindow;
|
---|
| 24 |
|
---|
[237] | 25 | public void onDecoded() {
|
---|
| 26 | sAlarmListWindow = (AlarmListWindow)getWindow();
|
---|
[276] | 27 | mToolTipAlarm = new danger.app.Alarm(0, this);
|
---|
[274] | 28 | setAutoResize(true);
|
---|
[250] | 29 | setList(Alarms.getList());
|
---|
[275] | 30 | Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
|
---|
[237] | 31 | super.onDecoded();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[250] | 34 | protected AlarmSetDialog alarmSetDialog() {
|
---|
| 35 | if (sAlarmSetDialog == null)
|
---|
| 36 | sAlarmSetDialog = AlarmSetDialog.getDialog();
|
---|
| 37 | return sAlarmSetDialog;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[237] | 40 | public boolean eventWidgetUp(int widget, Event e) {
|
---|
| 41 | switch (widget) {
|
---|
| 42 | case Event.DEVICE_BUTTON_BACK:
|
---|
| 43 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
[250] | 44 | Application.getCurrentApp().returnToLauncher();
|
---|
[237] | 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:
|
---|
[277] | 53 | // XXX check for max alarms
|
---|
[250] | 54 | alarmSetDialog().editAlarm(new Alarm(), true);
|
---|
[237] | 55 | return true;
|
---|
[278] | 56 | case EVENT_DISCARD_ALARM:
|
---|
| 57 | mToolTipAlarm.deactivate();
|
---|
| 58 | Alarm alarm = (Alarm)getFocusedItem();
|
---|
| 59 | alarm.beginEditing();
|
---|
[260] | 60 | AlertWindow alert =
|
---|
| 61 | Application.getCurrentApp().getAlert(ID_DISCARD_ALERT, this);
|
---|
| 62 | alert.setMessage("Permanently discard \u201c" +
|
---|
[278] | 63 | alarm.getMessage() + "\u201d?"); // XXX localize
|
---|
[260] | 64 | alert.show();
|
---|
| 65 | return true;
|
---|
[278] | 66 | case EVENT_CONFIRM_DISCARD:
|
---|
[250] | 67 | Alarms.removeAlarm((Alarm)getFocusedItem());
|
---|
[237] | 68 | return true;
|
---|
[278] | 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:
|
---|
[275] | 75 | if (mToolTipWindow == null || !mToolTipWindow.isVisible())
|
---|
| 76 | return true;
|
---|
[273] | 77 | case Event.EVENT_ALARM: // tooltip invalid
|
---|
| 78 | showToolTip();
|
---|
| 79 | return true;
|
---|
[237] | 80 | }
|
---|
| 81 | return super.receiveEvent(e);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | protected void itemActivated(Object item) {
|
---|
[250] | 85 | if (item == null)
|
---|
| 86 | return;
|
---|
| 87 | alarmSetDialog().editAlarm((Alarm)item, false);
|
---|
[237] | 88 | }
|
---|
| 89 |
|
---|
[273] | 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(),
|
---|
[277] | 110 | localToGlobalH(10),
|
---|
| 111 | localToGlobalV(mToolTipY),
|
---|
| 112 | 1, sAlarmListWindow);
|
---|
[273] | 113 | int secondsUntilUpdate = alarm.getSecondsUntilNextIntervalStringUpdate();
|
---|
| 114 | if (secondsUntilUpdate == 0)
|
---|
| 115 | return;
|
---|
| 116 | mToolTipAlarm.resetWake(secondsUntilUpdate);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[278] | 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 | }
|
---|
[286] | 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 | }
|
---|
[278] | 156 |
|
---|
[237] | 157 | public int alarmsSet() {
|
---|
| 158 | return getListSize();
|
---|
| 159 | }
|
---|
| 160 | public void onItemAdded(ActiveList list, Object item, int index) {
|
---|
| 161 | sAlarmListWindow.updateAlarmCount();
|
---|
[259] | 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
|
---|
[250] | 165 | setFocus(index);
|
---|
[237] | 166 | }
|
---|
| 167 | public void onItemRemoved(ActiveList list, Object item, int index) {
|
---|
[242] | 168 | ((Alarm)item).cancel();
|
---|
[237] | 169 | sAlarmListWindow.updateAlarmCount();
|
---|
| 170 | super.onItemRemoved(list, item, index);
|
---|
| 171 | }
|
---|
| 172 | }
|
---|