Browse code

Add the activity record in the exported data line

Natasha Kerensikova authored on22/03/2016 22:04:21
Showing1 changed files
... ...
@@ -28,6 +28,7 @@ static Window *window;
28 28
 static TextLayer *modal_text_layer;
29 29
 static char modal_text[256];
30 30
 static HealthMinuteData minute_data[1440];
31
+static HealthActivityMask minute_activity[1440];
31 32
 static uint16_t minute_data_size = 0;
32 33
 static uint16_t minute_index = 0;
33 34
 static time_t minute_first = 0, minute_last = 0;
... ...
@@ -123,7 +124,7 @@ set_modal_message(const char *msg) {
123 124
 /*    format: RFC-3339 time, step count, yaw, pitch, vmc, ambient light */
124 125
 static uint16_t
125 126
 minute_data_image(char *buffer, size_t size,
126
-    HealthMinuteData *data, time_t key) {
127
+    HealthMinuteData *data, HealthActivityMask activity_mask, time_t key) {
127 128
 	struct tm *tm;
128 129
 	size_t ret;
129 130
 	if (!buffer || !data) return 0;
... ...
@@ -154,12 +155,13 @@ minute_data_image(char *buffer, size_t size,
154 155
 	uint16_t pitch = data->orientation >> 4;
155 156
 
156 157
 	int i = snprintf(buffer + ret, size - ret,
157
-	    ",%" PRIu8 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%d",
158
+	    ",%" PRIu8 ",%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%d,%" PRIu32,
158 159
 	    data->steps,
159 160
 	    yaw,
160 161
 	    pitch,
161 162
 	    data->vmc,
162
-	    (int)data->light);
163
+	    (int)data->light,
164
+	    activity_mask);
163 165
 
164 166
 	if (i <= 0) {
165 167
 		APP_LOG(APP_LOG_LEVEL_ERROR, "minute_data_image: "
... ...
@@ -172,7 +174,8 @@ minute_data_image(char *buffer, size_t size,
172 174
 
173 175
 /* send_minute_data - use AppMessage to send the given minute data to phone */
174 176
 static void
175
-send_minute_data(HealthMinuteData *data, time_t key) {
177
+send_minute_data(HealthMinuteData *data, HealthActivityMask activity_mask,
178
+    time_t key) {
176 179
 	int32_t int_key = key / 60;
177 180
 
178 181
 	if (sent > 10) return;
... ...
@@ -184,7 +187,7 @@ send_minute_data(HealthMinuteData *data, time_t key) {
184 187
 	}
185 188
 
186 189
 	uint16_t size = minute_data_image(global_buffer, sizeof global_buffer,
187
-	    data, key);
190
+	    data, activity_mask, key);
188 191
 	if (!size) return;
189 192
 
190 193
 	AppMessageResult msg_result;
... ...
@@ -231,6 +234,31 @@ send_minute_data(HealthMinuteData *data, time_t key) {
231 234
 	sent += 1;
232 235
 }
233 236
 
237
+static bool
238
+record_activity(HealthActivity activity, time_t start_time, time_t end_time,
239
+    void *context) {
240
+	uint16_t first_index, last_index;
241
+	(void)context;
242
+
243
+	if (start_time <= minute_first) {
244
+		first_index = 0;
245
+	} else {
246
+		first_index = (start_time - minute_first) / 60;
247
+	}
248
+	if (first_index >= minute_data_size) return true;
249
+
250
+	last_index = (end_time - minute_first + 59) / 60;
251
+	if (last_index > minute_data_size) {
252
+		last_index = minute_data_size;
253
+	}
254
+
255
+	for (uint16_t i = first_index; i < last_index; i += 1) {
256
+		minute_activity[i] |= activity;
257
+	}
258
+
259
+	return true;
260
+}
261
+
234 262
 static bool
235 263
 load_minute_data_page(time_t start) {
236 264
 	minute_first = start;
... ...
@@ -240,6 +268,17 @@ load_minute_data_page(time_t start) {
240 268
 	    &minute_first, &minute_last);
241 269
 	minute_index = 0;
242 270
 
271
+	memset(minute_activity, 0, sizeof minute_activity);
272
+	if (health_service_any_activity_accessible(HealthActivityMaskAll,
273
+	    minute_first, minute_last)
274
+	    == HealthServiceAccessibilityMaskAvailable) {
275
+		health_service_activities_iterate(HealthActivityMaskAll,
276
+		    minute_first, minute_last,
277
+		    HealthIterationDirectionFuture,
278
+		    &record_activity,
279
+		    0);
280
+	}
281
+
243 282
 	if (!minute_data_size) {
244 283
 		APP_LOG(APP_LOG_LEVEL_ERROR,
245 284
 		    "health_service_get_minute_history returned 0");
... ...
@@ -258,6 +297,7 @@ send_next_line(void) {
258 297
 	}
259 298
 
260 299
 	send_minute_data(minute_data + minute_index,
300
+	    minute_activity[minute_index],
261 301
 	    minute_first + 60 * minute_index);
262 302
 	minute_index += 1;
263 303
 }