Browse code

Implement the new automatic wakeup feature

Natasha Kerensikova authored on28/04/2016 23:21:37
Showing3 changed files
... ...
@@ -20,7 +20,8 @@
20 20
     "dataLine": 220,
21 21
     "cfgStart": 301,
22 22
     "cfgEnd": 302,
23
-    "cfgAutoClose": 310
23
+    "cfgAutoClose": 310,
24
+    "cfgWakeupTime": 320
24 25
   },
25 26
   "resources": {
26 27
     "media": []
... ...
@@ -233,6 +233,7 @@ Pebble.addEventListener("webviewclosed", function(e) {
233 233
          if (wakeupH >= 0 && wakeupH < 24 && wakeupM >= 0 && wakeupM < 60) {
234 234
             cfg_wakeup_time = wakeupH * 60 + wakeupM;
235 235
             localStorage.setItem("cfgWakeupTime", cfg_wakeup_time);
236
+            msg.cfgWakeupTime = cfg_wakeup_time;
236 237
          }
237 238
          else
238 239
             console.log("Invalid wakeupTime \"" + configData.wakeupTime + "\"");
... ...
@@ -29,6 +29,7 @@
29 29
 #define MSG_KEY_CFG_START	301
30 30
 #define MSG_KEY_CFG_END		302
31 31
 #define MSG_KEY_CFG_AUTO_CLOSE	310
32
+#define MSG_KEY_CFG_WAKEUP_TIME	320
32 33
 
33 34
 static Window *window;
34 35
 static TextLayer *modal_text_layer;
... ...
@@ -45,6 +46,7 @@ static bool sending_data = false;
45 46
 static bool cfg_auto_close = false;
46 47
 static bool auto_close = false;
47 48
 static bool configuring = false;
49
+static int cfg_wakeup_time = -1;
48 50
 
49 51
 static struct widget {
50 52
 	char		label[64];
... ...
@@ -469,6 +471,13 @@ handle_received_tuple(Tuple *tuple) {
469 471
 			close_app();
470 472
 		break;
471 473
 
474
+	    case MSG_KEY_CFG_WAKEUP_TIME:
475
+		cfg_wakeup_time = tuple_int(tuple);
476
+		persist_write_int(MSG_KEY_CFG_WAKEUP_TIME, cfg_wakeup_time + 1);
477
+		APP_LOG(APP_LOG_LEVEL_INFO,
478
+		    "wrote cfg_wakeup_time %i", cfg_wakeup_time);
479
+		break;
480
+
472 481
 	    case MSG_KEY_CFG_START:
473 482
 		APP_LOG(APP_LOG_LEVEL_INFO, "Starting configuration");
474 483
 		auto_close = false;
... ...
@@ -524,7 +533,10 @@ tick_handler(struct tm *tick_time, TimeUnits units_changed) {
524 533
 static void
525 534
 init(void) {
526 535
 	cfg_auto_close = persist_read_bool(MSG_KEY_CFG_AUTO_CLOSE);
527
-	auto_close = cfg_auto_close;
536
+	cfg_wakeup_time = persist_read_int(MSG_KEY_CFG_WAKEUP_TIME) - 1;
537
+	APP_LOG(APP_LOG_LEVEL_INFO,
538
+	    "read cfg_wakeup_time %i", cfg_wakeup_time);
539
+	auto_close = (cfg_auto_close || launch_reason() == APP_LAUNCH_WAKEUP);
528 540
 
529 541
 	app_message_register_inbox_received(inbox_received_handler);
530 542
 	app_message_register_outbox_failed(outbox_failed_handler);
... ...
@@ -539,10 +551,33 @@ init(void) {
539 551
 	});
540 552
 	window_stack_push(window, true);
541 553
 	tick_timer_service_subscribe(SECOND_UNIT, &tick_handler);
554
+	wakeup_cancel_all();
542 555
 }
543 556
 
544 557
 static void deinit(void) {
545 558
 	window_destroy(window);
559
+
560
+	if (cfg_wakeup_time >= 0) {
561
+		WakeupId res;
562
+		time_t now = time(0);
563
+		time_t t = clock_to_timestamp(TODAY,
564
+		    cfg_wakeup_time / 60, cfg_wakeup_time % 60);
565
+
566
+		if (t - now > 6 * 86400)
567
+			t -= 6 * 86400;
568
+		else if (t - now <= 120)
569
+			t += 86400;
570
+
571
+		res = wakeup_schedule(t, 0, true);
572
+
573
+		if (res < 0)
574
+			APP_LOG(APP_LOG_LEVEL_ERROR,
575
+			    "wakeup_schedule(%" PRIi32 ", 0, true)"
576
+			    " returned %" PRIi32,
577
+			    t, res);
578
+	} else {
579
+		APP_LOG(APP_LOG_LEVEL_INFO, "No wakeup to setup");
580
+	}
546 581
 }
547 582
 
548 583
 int