From 8cfd54e802ce6b186bf7014350a4a2dac1fd21a1 Mon Sep 17 00:00:00 2001 From: Scott Gilroy Date: Thu, 20 Oct 2016 22:06:29 -0400 Subject: [PATCH] Support for "nutrition.calories" and "nutrition.carbohydrates" in `queryAggregated()` [https://github.com/dariosalvi78/cordova-plugin-health/issues/17] --- src/android/HealthPlugin.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/android/HealthPlugin.java b/src/android/HealthPlugin.java index b546098a..2708f690 100644 --- a/src/android/HealthPlugin.java +++ b/src/android/HealthPlugin.java @@ -648,6 +648,8 @@ private void queryAggregated(final JSONArray args, final CallbackContext callbac builder.aggregate(DataType.TYPE_BASAL_METABOLIC_RATE, DataType.AGGREGATE_BASAL_METABOLIC_RATE_SUMMARY); } else if (datatype.equalsIgnoreCase("activity")) { builder.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY); + } else if (nutritiondatatypes.get(datatype)) { + builder.aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY); } else { callbackContext.error("Datatype " + datatype + " not supported"); return; @@ -740,6 +742,9 @@ private void queryAggregated(final JSONArray args, final CallbackContext callbac } else if (datatype.equalsIgnoreCase("activity")) { retBucket.put("value", new JSONObject()); retBucket.put("unit", "activitySummary"); + } else if (nutritiondatatypes.get(datatype)) { + // TODO: set the correct unit for each nutrition type + retBucket.put("unit", "kcal"); } } } @@ -764,6 +769,24 @@ private void queryAggregated(final JSONArray args, final CallbackContext callbac float ncal = datapoint.getValue(Field.FIELD_AVERAGE).asFloat(); double ocal = retBucket.getDouble("value"); retBucket.put("value", ocal + ncal); + } else if (nutritiondatatypes.get(datatype)) { + Value nutrients = datapoint.getValue(Field.FIELD_NUTRIENTS); + String field = null, unit = null; + switch (datatype) { + case "nutrition.calories": + field = Field.NUTRIENT_CALORIES; + unit = "kcal"; + break; + case "nutrition.carbohydrates": + field = Field.NUTRIENT_TOTAL_CARBS; + unit = "g"; + break; + } + if (field != null) { + float value = nutrients.getKeyValue(field); + double total = retBucket.getDouble("value"); + retBucket.put("value", total + value); + } } else if (datatype.equalsIgnoreCase("activity")) { String activity = datapoint.getValue(Field.FIELD_ACTIVITY).asActivity(); JSONObject actobj = retBucket.getJSONObject("value");