2626struct Alarm {
2727 time_t scheduled_time ;
2828 WakeupId wakeup_id ;
29+ char * name ;
2930 bool is_timer ; // what's the difference between an alarm and a timer? the user's intention.
3031};
3132
@@ -44,8 +45,8 @@ static void prv_handle_app_message_inbox_received(DictionaryIterator *iterator,
4445static void prv_send_alarm_response (StatusCode response );
4546static void prv_wakeup_handler (WakeupId wakeup_id , int32_t cookie );
4647
47-
4848#define MAX_ALARMS 8
49+ #define ALARM_NAME_SIZE 32
4950
5051void alarm_manager_init () {
5152 wakeup_service_subscribe (prv_wakeup_handler );
@@ -55,7 +56,7 @@ void alarm_manager_init() {
5556 prv_load_alarms ();
5657}
5758
58- int alarm_manager_add_alarm (time_t when , bool is_timer , bool conversational ) {
59+ int alarm_manager_add_alarm (time_t when , bool is_timer , char * name , bool conversational ) {
5960 if (s_manager .pending_alarm_count >= MAX_ALARMS ) {
6061 APP_LOG (APP_LOG_LEVEL_WARNING , "Not scheduling alarm because MAX_ALARMS (%d) was already reached." , MAX_ALARMS );
6162 return E_OUT_OF_RESOURCES ;
@@ -82,6 +83,15 @@ int alarm_manager_add_alarm(time_t when, bool is_timer, bool conversational) {
8283 alarm -> scheduled_time = when ;
8384 alarm -> is_timer = is_timer ;
8485 alarm -> wakeup_id = id ;
86+ alarm -> name = NULL ;
87+ size_t name_len = 0 ;
88+ if (name ) {
89+ name_len = strlen (name );
90+ if (name_len > 0 ) {
91+ alarm -> name = malloc (name_len + 1 );
92+ strncpy (alarm -> name , name , name_len + 1 );
93+ }
94+ }
8595
8696 if (conversational && conversation_manager_get_current ()) {
8797 ConversationManager * conversation_manager = conversation_manager_get_current ();
@@ -92,9 +102,16 @@ int alarm_manager_add_alarm(time_t when, bool is_timer, bool conversational) {
92102 .time = alarm -> scheduled_time ,
93103 .is_timer = alarm -> is_timer ,
94104 .deleted = false,
105+ .name = NULL ,
95106 }
96107 }
97108 };
109+ // The alarm manager and conversation manager both expect to own the alarm name, so it has to be copied to both
110+ // places here.
111+ if (name ) {
112+ action .action .set_alarm .name = malloc (name_len + 1 );
113+ strncpy (action .action .set_alarm .name , name , name_len + 1 );
114+ }
98115 conversation_manager_add_action (conversation_manager , & action );
99116 }
100117
@@ -144,10 +161,12 @@ static void prv_load_alarms() {
144161 time_t times [MAX_ALARMS ];
145162 WakeupId wakeup_ids [MAX_ALARMS ];
146163 bool is_timers [MAX_ALARMS ];
164+ char names [MAX_ALARMS ][ALARM_NAME_SIZE ];
147165
148166 persist_read_data (PERSIST_KEY_ALARM_TIMES , & times , sizeof (times ));
149167 persist_read_data (PERSIST_KEY_ALARM_WAKEUP_IDS , & wakeup_ids , sizeof (wakeup_ids ));
150168 persist_read_data (PERSIST_KEY_ALARM_IS_TIMERS , & is_timers , sizeof (is_timers ));
169+ persist_read_data (PERSIST_KEY_ALARM_NAMES , & names , sizeof (names ));
151170
152171 s_manager .pending_alarms = malloc (sizeof (Alarm ) * alarm_count );
153172 s_manager .pending_alarm_count = alarm_count ;
@@ -169,11 +188,21 @@ static void prv_load_alarms() {
169188 alarm -> scheduled_time = times [i ];
170189 alarm -> wakeup_id = wakeup_ids [i ];
171190 alarm -> is_timer = is_timers [i ];
191+ size_t name_len = strlen (names [i ]);
192+ if (name_len >= ALARM_NAME_SIZE ) {
193+ name_len = ALARM_NAME_SIZE - 1 ;
194+ }
195+ if (name_len == 0 ) {
196+ alarm -> name = NULL ;
197+ } else {
198+ alarm -> name = malloc (name_len + 1 );
199+ strncpy (alarm -> name , names [i ], name_len + 1 );
200+ }
172201 }
173202 s_manager .pending_alarm_count = j ;
174203
175204 if (did_drop_entries ) {
176- APP_LOG (APP_LOG_LEVEL_INFO , "Updating saved data after droping entries." );
205+ APP_LOG (APP_LOG_LEVEL_INFO , "Updating saved data after dropping entries." );
177206 prv_save_alarms ();
178207 }
179208}
@@ -185,23 +214,33 @@ static void prv_save_alarms() {
185214 persist_delete (PERSIST_KEY_ALARM_TIMES );
186215 persist_delete (PERSIST_KEY_ALARM_WAKEUP_IDS );
187216 persist_delete (PERSIST_KEY_ALARM_IS_TIMERS );
217+ persist_delete (PERSIST_KEY_ALARM_NAMES );
188218 persist_delete (PERSIST_KEY_ALARM_COUNT_TWO );
189219 wakeup_cancel_all ();
190220 }
191221 time_t times [MAX_ALARMS ];
192222 WakeupId wakeup_ids [MAX_ALARMS ];
193223 bool is_timers [MAX_ALARMS ];
224+ char names [MAX_ALARMS ][ALARM_NAME_SIZE ];
225+ memset (names , 0 , sizeof (names ));
194226 for (int i = 0 ; i < s_manager .pending_alarm_count ; ++ i ) {
195227 Alarm * alarm = & s_manager .pending_alarms [i ];
196228 times [i ] = alarm -> scheduled_time ;
197229 wakeup_ids [i ] = alarm -> wakeup_id ;
198230 is_timers [i ] = alarm -> is_timer ;
231+ if (alarm -> name ) {
232+ strncpy (names [i ], alarm -> name , ALARM_NAME_SIZE );
233+ names [i ][ALARM_NAME_SIZE - 1 ] = '\0' ;
234+ } else {
235+ names [i ][0 ] = '\0' ;
236+ }
199237 }
200238
201239 persist_write_int (PERSIST_KEY_ALARM_COUNT_ONE , s_manager .pending_alarm_count );
202240 persist_write_data (PERSIST_KEY_ALARM_TIMES , & times , sizeof (times ));
203241 persist_write_data (PERSIST_KEY_ALARM_WAKEUP_IDS , & wakeup_ids , sizeof (wakeup_ids ));
204242 persist_write_data (PERSIST_KEY_ALARM_IS_TIMERS , & is_timers , sizeof (is_timers ));
243+ persist_write_data (PERSIST_KEY_ALARM_NAMES , & names , sizeof (names ));
205244 persist_write_int (PERSIST_KEY_ALARM_COUNT_TWO , s_manager .pending_alarm_count );
206245 APP_LOG (APP_LOG_LEVEL_INFO , "Wrote %d alarms." , s_manager .pending_alarm_count );
207246}
@@ -221,12 +260,22 @@ static void prv_remove_alarm(int to_remove) {
221260 .time = alarm -> scheduled_time ,
222261 .is_timer = alarm -> is_timer ,
223262 .deleted = true,
263+ .name = NULL ,
224264 }
225265 }
226266 };
267+ if (alarm -> name ) {
268+ size_t name_len = strlen (alarm -> name );
269+ action .action .set_alarm .name = malloc (name_len + 1 );
270+ strncpy (action .action .set_alarm .name , alarm -> name , name_len + 1 );
271+ }
227272 conversation_manager_add_action (conversation_manager , & action );
228273 }
229274
275+ if (alarm -> name ) {
276+ free (alarm -> name );
277+ }
278+
230279 if (s_manager .pending_alarm_count == 1 ) {
231280 free (s_manager .pending_alarms );
232281 s_manager .pending_alarms = NULL ;
@@ -263,7 +312,7 @@ bool alarm_manager_maybe_alarm() {
263312 APP_LOG (APP_LOG_LEVEL_INFO , "comparing %d == %d" , alarm -> wakeup_id , id );
264313 if (alarm -> wakeup_id == id ) {
265314 APP_LOG (APP_LOG_LEVEL_INFO , "alarm found! alarming..." );
266- alarm_window_push (alarm -> scheduled_time , alarm -> is_timer );
315+ alarm_window_push (alarm -> scheduled_time , alarm -> is_timer , alarm -> name );
267316 prv_remove_alarm (i );
268317 return true;
269318 }
@@ -279,6 +328,10 @@ bool alarm_is_timer(Alarm* alarm) {
279328 return alarm -> is_timer ;
280329}
281330
331+ char * alarm_get_name (Alarm * alarm ) {
332+ return alarm -> name ;
333+ }
334+
282335static void prv_handle_set_alarm_request (DictionaryIterator * iterator , void * context ) {
283336 Tuple * tuple = dict_find (iterator , MESSAGE_KEY_SET_ALARM_TIME );
284337 if (tuple == NULL ) {
@@ -294,7 +347,12 @@ static void prv_handle_set_alarm_request(DictionaryIterator *iterator, void *con
294347 if (is_timer ) {
295348 alarm_time += time (NULL );
296349 }
297- StatusCode result = alarm_manager_add_alarm (alarm_time , is_timer ,true);
350+ tuple = dict_find (iterator , MESSAGE_KEY_SET_ALARM_NAME );
351+ char * name = NULL ;
352+ if (tuple != NULL && strlen (tuple -> value -> cstring ) > 0 ) {
353+ name = tuple -> value -> cstring ;
354+ }
355+ StatusCode result = alarm_manager_add_alarm (alarm_time , is_timer , name , true);
298356 prv_send_alarm_response (result );
299357 if (result == S_SUCCESS ) {
300358 APP_LOG (APP_LOG_LEVEL_INFO , "Set alarm for %d (is timer: %d)" , alarm_time , is_timer );
@@ -315,7 +373,9 @@ static void prv_handle_get_alarm_request(int16_t is_timer, void* context) {
315373 for (int i = 0 ; i < s_manager .pending_alarm_count ; ++ i ) {
316374 Alarm * alarm = & s_manager .pending_alarms [i ];
317375 if (alarm -> is_timer == is_timer ) {
318- dict_write_int32 (iter , MESSAGE_KEY_GET_ALARM_RESULT + ++ write_index , alarm -> scheduled_time );
376+ ++ write_index ;
377+ dict_write_int32 (iter , MESSAGE_KEY_GET_ALARM_RESULT + write_index , alarm -> scheduled_time );
378+ dict_write_cstring (iter , MESSAGE_KEY_GET_ALARM_NAME + write_index , alarm -> name );
319379 }
320380 }
321381 dict_write_int16 (iter , MESSAGE_KEY_GET_ALARM_RESULT , write_index );
@@ -394,7 +454,7 @@ static void prv_wakeup_handler(WakeupId wakeup_id, int32_t cookie) {
394454 APP_LOG (APP_LOG_LEVEL_INFO , "comparing %d == %d" , alarm -> wakeup_id , wakeup_id );
395455 if (alarm -> wakeup_id == wakeup_id ) {
396456 APP_LOG (APP_LOG_LEVEL_INFO , "alarm found! alarming..." );
397- alarm_window_push (alarm -> scheduled_time , alarm -> is_timer );
457+ alarm_window_push (alarm -> scheduled_time , alarm -> is_timer , alarm -> name );
398458 prv_remove_alarm (i );
399459 break ;
400460 }
0 commit comments