summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--watchapp/src/watchapp.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/watchapp/src/watchapp.c b/watchapp/src/watchapp.c
index 63e693f..ad7cbc4 100644
--- a/watchapp/src/watchapp.c
+++ b/watchapp/src/watchapp.c
@@ -8,6 +8,35 @@ static TextLayer *time_layer;
static Layer *line_layer;
static TextLayer *step_layer;
+static ResourceId current_battery_id = INVALID_RESOURCE;
+static GBitmap *current_battery_image;
+static BitmapLayer *battery_layer;
+
+static void handle_battery_update(BatteryChargeState charge)
+{
+ ResourceId target_battery_id = INVALID_RESOURCE;
+ GBitmap *new_image;
+ if (charge.is_charging)
+ target_battery_id = RESOURCE_ID_ICON_BATTERY_CHARGING;
+ else if (charge.is_plugged)
+ target_battery_id = RESOURCE_ID_ICON_BATTERY_CHARGED;
+ else if (charge.charge_percent > 70)
+ target_battery_id = RESOURCE_ID_ICON_BATTERY_FULL;
+ else if (charge.charge_percent > 20)
+ target_battery_id = RESOURCE_ID_ICON_BATTERY_MID;
+ else
+ target_battery_id = RESOURCE_ID_ICON_BATTERY_CRITICAL;
+ if (current_battery_id == target_battery_id)
+ return;
+ new_image = gbitmap_create_with_resource(target_battery_id);
+ bitmap_layer_set_bitmap(battery_layer, new_image);
+ if (current_battery_id != INVALID_RESOURCE) {
+ gbitmap_destroy(current_battery_image);
+ }
+ current_battery_image = new_image;
+ current_battery_id = target_battery_id;
+}
+
static void draw_line(Layer *layer, GContext *ctx)
{
graphics_context_set_stroke_color(ctx, GColorWhite);
@@ -92,19 +121,30 @@ static void window_load(Window *window)
line_layer = layer_create(GRect(4, 168-49-1, 144-8, 2));
layer_set_update_proc(line_layer, draw_line);
+ // Now prepare the status icon layers
+ battery_layer = bitmap_layer_create(GRect(120,0,24,16));
+ bitmap_layer_set_background_color(battery_layer, GColorClear);
+ handle_battery_update(battery_state_service_peek());
+ battery_state_service_subscribe(handle_battery_update);
+
// And stuff the layers into the window
layer_add_child(window_get_root_layer(window), text_layer_get_layer(time_layer));
layer_add_child(window_get_root_layer(window), text_layer_get_layer(date_layer));
layer_add_child(window_get_root_layer(window), text_layer_get_layer(step_layer));
layer_add_child(window_get_root_layer(window), line_layer);
+ layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(battery_layer));
}
static void window_unload(Window *window)
{
+ battery_state_service_unsubscribe();
tick_timer_service_unsubscribe();
text_layer_destroy(time_layer);
text_layer_destroy(date_layer);
text_layer_destroy(step_layer);
+ bitmap_layer_destroy(battery_layer);
+ if (current_battery_id != INVALID_RESOURCE)
+ gbitmap_destroy(current_battery_image);
layer_destroy(line_layer);
}