public final class NutritionSample
- Object
- HealthSample
- SessionSample
- NutritionSample
A logged food or meal, carrying whichever nutrients are known about it.
Sparse by design
A NutritionSample holds only the nutrients actually set. A logged
apple sets four fields; a packaged food scanned from a barcode might set
thirty. Modelling this as forty nullable fields – which is roughly what
both platforms do – would make every consumer write forty null checks,
so this exposes a map instead and getNutrients() returns only what is
present.
NutritionSample lunch = NutritionSample.create(start, end);
lunch.setTitle("Chicken salad");
lunch.setNutrient(Nutrient.ENERGY, 420);
lunch.setNutrient(Nutrient.PROTEIN, 35);
lunch.setNutrient(Nutrient.SODIUM, 610);
Meal type
Health Connect records which meal an entry belongs to and HealthKit
does not, so getMealType() round-trips through the local store and
would ride in sample metadata on iOS once that mapping exists.
Local and simulator only in this release
Neither phone carries this shape yet – see the package
documentation. A read or write of
HealthDataType.NUTRITION on iOS or Android is refused with
HealthError.TYPE_NOT_SUPPORTED; everything here works against the
local store.
Fields
public static final int MEAL_UNKNOWN = 0 | The entry is not attributed to a particular meal. |
public static final int MEAL_BREAKFAST = 1 | Breakfast. |
public static final int MEAL_LUNCH = 2 | Lunch. |
public static final int MEAL_DINNER = 3 | Dinner. |
public static final int MEAL_SNACK = 4 | A snack between meals. |
Methods
Inherited methods
Field details
MEAL_UNKNOWN
public static final int MEAL_UNKNOWN = 0MEAL_BREAKFAST
public static final int MEAL_BREAKFAST = 1MEAL_LUNCH
public static final int MEAL_LUNCH = 2MEAL_DINNER
public static final int MEAL_DINNER = 3MEAL_SNACK
public static final int MEAL_SNACK = 4Method details
create
public static NutritionSample create(long startMillis, long endMillis)[startMillis, endMillis].create
public static NutritionSample create(long atMillis)setNutrient
public NutritionSample setNutrient(Nutrient nutrient, double amount)Nutrient.getUnit().setNutrient
public NutritionSample setNutrient(Nutrient nutrient, double amount, HealthUnit unit)Throws
IllegalArgumentException- if
unitmeasures a different dimension than the nutrient – protein in millilitres, say.
getNutrient
public HealthQuantity getNutrient(Nutrient nutrient)The amount of nutrient, or null when this entry does not record
it.
Null rather than zero, for the same reason aggregate buckets return null: “this food’s sodium was never measured” and “this food contains no sodium” are different claims, and only one of them is safe to show someone managing their intake.
hasNutrient
public boolean hasNutrient(Nutrient nutrient)nutrient is recorded on this entry.getNutrients
public List<Nutrient> getNutrients()getNutrientCount
public int getNutrientCount()getMealType
public int getMealType()MEAL_ constant.setMealType
public NutritionSample setMealType(int mealType)MEAL_ constant.getFoodName
public String getFoodName()setFoodName
public NutritionSample setFoodName(String foodName)toString
public String toString()