-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathalarms.c
More file actions
476 lines (400 loc) · 17.9 KB
/
alarms.c
File metadata and controls
476 lines (400 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* SPDX-FileCopyrightText: 2024 Google LLC */
/* SPDX-License-Identifier: Apache-2.0 */
#include "alarms.h"
#include "alarm_detail.h"
#include "alarm_editor.h"
#include "applib/app.h"
#include "applib/graphics/gtypes.h"
#include "applib/preferred_content_size.h"
#include "applib/ui/app_window_stack.h"
#include "applib/ui/dialogs/dialog.h"
#include "applib/ui/dialogs/expandable_dialog.h"
#include "applib/ui/dialogs/simple_dialog.h"
#include "applib/ui/ui.h"
#include "kernel/pbl_malloc.h"
#include "process_state/app_state/app_state.h"
#include "resource/resource_ids.auto.h"
#include "pbl/services/clock.h"
#include "pbl/services/i18n/i18n.h"
#include "pbl/services/alarms/alarm.h"
#include "pbl/services/timeline/timeline.h"
#include "shell/prefs.h"
#include "shell/system_theme.h"
#include "system/logging.h"
#include "system/passert.h"
#include "util/string.h"
#include "util/time/time.h"
#include "util/trig.h"
#include <stdio.h>
#include <string.h>
// Alarms app versions
// 0: Initial version or never opened
// 1: Smart alarms
#define CURRENT_ALARMS_APP_VERSION 1
typedef struct {
ListNode node;
AlarmId id;
AlarmInfo info;
bool scheduled_days[DAYS_PER_WEEK];
} AlarmNode;
typedef struct AlarmsAppData {
Window window;
MenuLayer menu_layer;
StatusBarLayer status_layer;
GBitmap plus_icon;
GBitmap smart_alarm_icon;
AlarmNode *alarm_list_head;
MenuIndex selected_index;
bool show_limit_reached_text;
bool can_schedule_alarm;
uint32_t current_plus_icon_resource_id;
EventServiceInfo alarm_event_info;
} AlarmsAppData;
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Alarm list functions
static int prv_alarm_comparator(void *a, void *b) {
AlarmNode *alarm_a = (AlarmNode*) a;
AlarmNode *alarm_b = (AlarmNode*) b;
// Sort by alarm time, with 12:00AM being the starting point
if (alarm_a->info.hour > alarm_b->info.hour) {
return true;
}
if (alarm_a->info.hour == alarm_b->info.hour && alarm_a->info.minute > alarm_b->info.minute) {
return true;
}
return false;
}
static void prv_clear_alarm_list(AlarmsAppData* data) {
while (data->alarm_list_head) {
AlarmNode* old_head = data->alarm_list_head;
data->alarm_list_head = (AlarmNode*) list_pop_head((ListNode *) old_head);
task_free(old_head);
old_head = NULL;
}
}
static void prv_add_alarm_to_list(AlarmId id, const AlarmInfo *info, void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
AlarmNode *new_node = task_malloc_check(sizeof(AlarmNode));
list_init((ListNode*) new_node);
new_node->id = id;
new_node->info = *info;
memcpy(&new_node->scheduled_days, info->scheduled_days, sizeof(new_node->scheduled_days));
new_node->info.scheduled_days = &new_node->scheduled_days;
data->alarm_list_head = (AlarmNode *)list_sorted_add((ListNode *)data->alarm_list_head,
(ListNode *)new_node,
prv_alarm_comparator,
false);
}
static void prv_update_alarm_list(AlarmsAppData *data) {
prv_clear_alarm_list(data);
alarm_for_each(prv_add_alarm_to_list, data);
data->can_schedule_alarm = alarm_can_schedule();
}
static bool prv_are_alarms_scheduled(AlarmsAppData *data) {
return (list_count((ListNode*) data->alarm_list_head) > 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//! General helper functions
static void prv_show_deleted_dialog(void) {
SimpleDialog *simple_dialog = simple_dialog_create("AlarmDelete");
Dialog *dialog = simple_dialog_get_dialog(simple_dialog);
const char *delete_text = i18n_noop("Alarm Deleted");
dialog_set_text(dialog, i18n_get(delete_text, dialog));
i18n_free(delete_text, dialog);
dialog_set_icon(dialog, RESOURCE_ID_RESULT_SHREDDED_LARGE);
dialog_set_background_color(dialog, ALARMS_APP_HIGHLIGHT_COLOR);
dialog_set_timeout(dialog, DIALOG_TIMEOUT_DEFAULT);
app_simple_dialog_push(simple_dialog);
}
static int prv_get_list_idx_of_alarm_id(AlarmsAppData* data, AlarmId id) {
if (id == ALARM_INVALID_ID) {
return 0;
}
AlarmNode *cur_node = data->alarm_list_head;
// Starting at one because of the "+" cell
int list_idx = 1;
while (cur_node) {
if (cur_node->id == id) {
return list_idx;
}
list_idx++;
cur_node = (AlarmNode* )list_get_next((ListNode*) cur_node);
}
return 0;
}
static void prv_update_menu_layer(AlarmsAppData* data, AlarmId select_alarm) {
MenuIndex selected_menu_index = {0, prv_get_list_idx_of_alarm_id(data, select_alarm)};
menu_layer_reload_data(&data->menu_layer);
menu_layer_set_selected_index(&data->menu_layer, selected_menu_index,
MenuRowAlignCenter, false);
}
static void prv_handle_alarm_editor_complete(AlarmEditorResult result, AlarmId id,
void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
if (result == CANCELLED && !prv_are_alarms_scheduled(data)) {
// In the case the user had no alarms set, and didn't finish creating one.
// We want to exit the app without showing an empty alarm list
app_window_stack_remove(&data->window, true);
} else if (result == DELETED) {
prv_update_alarm_list(data);
if (!prv_are_alarms_scheduled(data)) {
// The user deleted their last alarm, show a dialog and setup the create new alarm screen.
// We don't want to show an empty alarm list
prv_show_deleted_dialog();
Window *editor = alarm_editor_create_new_alarm(prv_handle_alarm_editor_complete, data);
app_window_stack_insert_next(editor);
} else {
prv_update_menu_layer(data, ALARM_INVALID_ID);
}
} else { // Created / Edited
prv_update_alarm_list(data);
prv_update_menu_layer(data, id);
}
}
static void prv_handle_alarm_event(PebbleEvent *e, void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
prv_update_alarm_list(data);
}
static void prv_create_new_alarm(AlarmsAppData* data) {
Window *editor = alarm_editor_create_new_alarm(prv_handle_alarm_editor_complete, data);
app_window_stack_push(editor, true);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Menu Layer Callbacks
static bool prv_is_add_alarm_cell(MenuIndex *cell_index) {
return cell_index->row == 0;
}
static uint16_t prv_alarm_list_get_num_sections_callback(struct MenuLayer *menu_layer,
void *callback_context) {
return 1;
}
static uint16_t prv_alarm_list_get_num_rows_callback(struct MenuLayer *menu_layer,
uint16_t section_index,
void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
// Number of alarms + the add alarm header
return list_count((ListNode*) data->alarm_list_head) + 1;
}
static int16_t prv_alarm_list_get_cell_height_callback(struct MenuLayer *menu_layer,
MenuIndex *cell_index,
void *callback_context) {
return menu_cell_basic_cell_height();
}
static void prv_alarm_list_draw_row_callback(GContext *ctx, const Layer *cell_layer,
MenuIndex *cell_index, void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
if (prv_is_add_alarm_cell(cell_index)) {
GRect box;
uint32_t new_bitmap_resource = RESOURCE_ID_PLUS_ICON_BLACK;
if (!data->can_schedule_alarm) { // alarm limit reached
if (menu_cell_layer_is_highlighted(cell_layer)) {
if (data->show_limit_reached_text) {
// Trying to add a new alarm when list is already full
const GFont font =
system_theme_get_font_for_default_size(TextStyleFont_MenuCellSubtitle);
box = GRect(0, 0, cell_layer->bounds.size.w, fonts_get_font_height(font));
const char *text = i18n_get("Limit reached.", data);
box.size = graphics_text_layout_get_max_used_size(ctx, text, font, box,
GTextOverflowModeTrailingEllipsis,
GTextAlignmentCenter, NULL);
grect_align(&box, &cell_layer->bounds, GAlignCenter, true /* clip */);
box.origin.y -= fonts_get_font_cap_offset(font);
graphics_draw_text(ctx, text, font, box,
GTextOverflowModeFill, GTextAlignmentCenter, NULL);
return;
} else { // "add alarm" cell highlighted
new_bitmap_resource = RESOURCE_ID_PLUS_ICON_DOTTED;
}
} else { // "add alarm" cell not highlighted
// Have to manually override the tint color as we're using a color that differs
// from the ones the MenuLayer uses.
graphics_context_set_tint_color(ctx, GColorLightGray);
}
}
if (new_bitmap_resource != data->current_plus_icon_resource_id) {
// Change the icon to the dotted one
data->current_plus_icon_resource_id = new_bitmap_resource;
gbitmap_deinit(&data->plus_icon);
gbitmap_init_with_resource(&data->plus_icon, data->current_plus_icon_resource_id);
}
box.origin = GPoint((cell_layer->bounds.size.w - data->plus_icon.bounds.size.w) / 2,
(cell_layer->bounds.size.h - data->plus_icon.bounds.size.h) / 2);
box.size = data->plus_icon.bounds.size;
graphics_context_set_compositing_mode(ctx, GCompOpTint);
graphics_draw_bitmap_in_rect(ctx, &data->plus_icon, &box);
return;
}
AlarmNode *node = (AlarmNode*) list_get_at((ListNode*)data->alarm_list_head, cell_index->row - 1);
// Format 1: 10:34 AM
// Format 2: 14:56
char alarm_time_text[9];
clock_format_time(alarm_time_text, sizeof(alarm_time_text),
node->info.hour, node->info.minute, true);
const char *enabled = node->info.enabled ? i18n_get("ON", data) : i18n_get("OFF", data);
graphics_context_set_compositing_mode(ctx, GCompOpTint);
// If the alarm is not smart, use the icon as spacing but don't render it.
// Otherwise if the alarm is smart draw according to the menu highlight.
graphics_context_set_tint_color(ctx, !node->info.is_smart ? GColorClear :
(cell_layer->is_highlighted ? GColorWhite : GColorBlack));
char alarm_day_text[32] = {0};
MenuCellLayerConfig config = {
.title = alarm_time_text,
.value = enabled,
.icon = &data->smart_alarm_icon,
.icon_align = MenuCellLayerIconAlign_TopLeft,
.icon_box_model = &(GBoxModel) { .offset = { 0, 5 }, .margin = { 6, 0 } },
.icon_form_fit = true,
.horizontal_inset = PBL_IF_ROUND_ELSE(-6, 0),
.overflow_mode = GTextOverflowModeTrailingEllipsis,
};
if (node->info.kind != ALARM_KIND_CUSTOM) {
const bool all_caps = false;
config.subtitle = i18n_get(alarm_get_string_for_kind(node->info.kind, all_caps), data);
} else {
alarm_get_string_for_custom(node->scheduled_days, alarm_day_text);
config.subtitle = alarm_day_text;
}
menu_cell_layer_draw(ctx, cell_layer, &config);
}
static void prv_alarm_list_select_callback(MenuLayer *menu_layer, MenuIndex *cell_index,
void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
if (prv_is_add_alarm_cell(cell_index)) {
if (!data->can_schedule_alarm) {
data->show_limit_reached_text = true;
layer_mark_dirty(menu_layer_get_layer(&data->menu_layer));
} else {
prv_create_new_alarm(data);
}
return;
}
// Minus 1 because of the "add alarm" cell
AlarmNode *node =
(AlarmNode *)list_get_at((ListNode *)data->alarm_list_head, cell_index->row - 1);
alarm_detail_window_push(node->id, &node->info, prv_handle_alarm_editor_complete, data);
}
static void prv_alarm_list_selection_changed_callback(MenuLayer *menu_layer, MenuIndex new_index,
MenuIndex old_index, void *callback_context) {
AlarmsAppData *data = (AlarmsAppData *)callback_context;
if (prv_is_add_alarm_cell(&old_index)) {
data->show_limit_reached_text = false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Smart Alarm first use dialog
typedef struct FirstUseDialog {
ExpandableDialog dialog;
AlarmsAppData *data;
} FirstUseDialog;
static void prv_alarms_app_opened_click_handler(ClickRecognizerRef recognizer, void *context) {
ExpandableDialog *expandable_dialog = context;
expandable_dialog_pop(expandable_dialog);
}
static void prv_push_alarms_app_opened_dialog(AlarmsAppData *data) {
const char *first_use_text = i18n_get(
"Let us wake you in your lightest sleep so you're fully refreshed! "
"Smart Alarm wakes you up to 30min before your alarm.", data);
const char *header = i18n_get("Smart Alarm", data);
ExpandableDialog *expandable_dialog = expandable_dialog_create_with_params(
header, RESOURCE_ID_SMART_ALARM_TINY, first_use_text,
system_theme_get_fg_color(), system_theme_get_bg_color(), NULL, RESOURCE_ID_ACTION_BAR_ICON_CHECK,
prv_alarms_app_opened_click_handler);
expandable_dialog_set_action_bar_background_color(expandable_dialog, ALARMS_APP_HIGHLIGHT_COLOR);
expandable_dialog_set_header(expandable_dialog, header);
#if defined(PBL_ROUND)
expandable_dialog_set_header_font(expandable_dialog,
fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
#endif
// Show immediately since this is the first window and there is already a compositor animation
app_window_stack_push(&expandable_dialog->dialog.window, false /* animated */);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//! App boilerplate
static void prv_handle_init(void) {
AlarmsAppData *data = app_malloc_check(sizeof(*data));
*data = (AlarmsAppData) {{ .user_data = NULL }};
Window *window = &data->window;
window_init(window, WINDOW_NAME("Alarms"));
window_set_user_data(window, data);
data->alarm_list_head = NULL;
// Alarm list must be updated before menu layer is initialized
prv_update_alarm_list(data);
const GRect bounds = grect_inset(data->window.layer.bounds,
GEdgeInsets(STATUS_BAR_LAYER_HEIGHT, 0,
PBL_IF_ROUND_ELSE(STATUS_BAR_LAYER_HEIGHT, 0), 0));
menu_layer_init(&data->menu_layer, &bounds);
menu_layer_set_callbacks(&data->menu_layer, data, &(MenuLayerCallbacks) {
.get_num_sections = prv_alarm_list_get_num_sections_callback,
.get_num_rows = prv_alarm_list_get_num_rows_callback,
.get_cell_height = prv_alarm_list_get_cell_height_callback,
.draw_row = prv_alarm_list_draw_row_callback,
.select_click = prv_alarm_list_select_callback,
.selection_changed = prv_alarm_list_selection_changed_callback
});
menu_layer_set_highlight_colors(&data->menu_layer, ALARMS_APP_HIGHLIGHT_COLOR, GColorWhite);
menu_layer_set_click_config_onto_window(&data->menu_layer, &data->window);
menu_layer_set_scroll_wrap_around(&data->menu_layer, shell_prefs_get_menu_scroll_wrap_around_enable());
menu_layer_set_scroll_vibe_on_wrap(&data->menu_layer, shell_prefs_get_menu_scroll_vibe_behavior() == MenuScrollVibeOnWrapAround);
menu_layer_set_scroll_vibe_on_blocked(&data->menu_layer, shell_prefs_get_menu_scroll_vibe_behavior() == MenuScrollVibeOnLocked);
layer_add_child(&data->window.layer, menu_layer_get_layer(&data->menu_layer));
status_bar_layer_init(&data->status_layer);
status_bar_layer_set_separator_mode(&data->status_layer, StatusBarLayerSeparatorModeNone);
layer_add_child(&data->window.layer, status_bar_layer_get_layer(&data->status_layer));
gbitmap_init_with_resource(&data->smart_alarm_icon, RESOURCE_ID_SMART_ALARM_ICON_BLACK);
gbitmap_init_with_resource(&data->plus_icon, RESOURCE_ID_PLUS_ICON_BLACK);
data->current_plus_icon_resource_id = RESOURCE_ID_PLUS_ICON_BLACK;
app_state_set_user_data(data);
data->alarm_event_info = (EventServiceInfo) {
.type = PEBBLE_ALARM_CLOCK_EVENT,
.handler = prv_handle_alarm_event,
.context = data,
};
event_service_client_subscribe(&data->alarm_event_info);
if (prv_are_alarms_scheduled(data)) {
int list_idx = 1; // Default to first alarm entry in list
if (app_launch_reason() == APP_LAUNCH_TIMELINE_ACTION) {
AlarmId alarm_id = (AlarmId)app_launch_get_args();
list_idx = prv_get_list_idx_of_alarm_id(data, alarm_id);
if (list_idx == 0) {
list_idx = 1; // Default to first alarm if idx not found
}
}
app_window_stack_push(&data->window, true);
menu_layer_set_selected_index(&data->menu_layer, MenuIndex(0, list_idx),
MenuRowAlignCenter, false);
} else {
Window *editor = alarm_editor_create_new_alarm(prv_handle_alarm_editor_complete, data);
app_window_stack_push(editor, true);
app_window_stack_insert_next(&data->window);
}
uint32_t version = alarm_prefs_get_alarms_app_opened();
if (version == 0) {
prv_push_alarms_app_opened_dialog(data);
}
alarm_prefs_set_alarms_app_opened(CURRENT_ALARMS_APP_VERSION);
}
static void prv_handle_deinit(void) {
AlarmsAppData *data = app_state_get_user_data();
status_bar_layer_deinit(&data->status_layer);
menu_layer_deinit(&data->menu_layer);
i18n_free_all(data);
prv_clear_alarm_list(data);
event_service_client_unsubscribe(&data->alarm_event_info);
app_free(data);
}
static void s_main(void) {
prv_handle_init();
app_event_loop();
prv_handle_deinit();
}
const PebbleProcessMd* alarms_app_get_info() {
static const PebbleProcessMdSystem s_alarms_app_info = {
.common = {
.main_func = s_main,
.uuid = UUID_ALARMS_DATA_SOURCE,
},
.name = i18n_noop("Alarms"),
.icon_resource_id = RESOURCE_ID_ALARM_CLOCK_TINY,
};
return (const PebbleProcessMd*) &s_alarms_app_info;
}