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