From df138e9c7920d02b97039b121284c31f84890b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sat, 16 Nov 2024 23:47:05 +0000 Subject: [PATCH] Add DefaultRestingMetabolicRateProvider --- .../devices/AbstractDeviceCoordinator.java | 2 +- .../DefaultRestingMetabolicRateProvider.java | 145 ++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DefaultRestingMetabolicRateProvider.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java index 7c2d78b6a..9f6e5b98e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java @@ -283,7 +283,7 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator { @Override public TimeSampleProvider getRestingMetabolicRateProvider(final GBDevice device, final DaoSession session) { - return null; // FIXME return new DefaultRestingMetabolicRateProvider(device, session); + return new DefaultRestingMetabolicRateProvider(device, session); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DefaultRestingMetabolicRateProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DefaultRestingMetabolicRateProvider.java new file mode 100644 index 000000000..ff7f9f48b --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DefaultRestingMetabolicRateProvider.java @@ -0,0 +1,145 @@ +/* Copyright (C) 2024 José Rebelo + + This file is part of Gadgetbridge. + + Gadgetbridge is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Gadgetbridge is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ +package nodomain.freeyourgadget.gadgetbridge.devices; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.Collections; +import java.util.List; + +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; +import nodomain.freeyourgadget.gadgetbridge.model.RestingMetabolicRateSample; + +/** + * Provides a default resting metabolic rate, for devices that do not provide it. Currently it uses the + * Mifflin St Jeor equation. TODO: use the user data at that timestamp, and make the algorithm configurable. + */ +public class DefaultRestingMetabolicRateProvider implements TimeSampleProvider { + private final DaoSession mSession; + private final GBDevice mDevice; + + protected DefaultRestingMetabolicRateProvider(final GBDevice device, final DaoSession session) { + mDevice = device; + mSession = session; + } + + public GBDevice getDevice() { + return mDevice; + } + + public DaoSession getSession() { + return mSession; + } + + @NonNull + @Override + public List getAllSamples(final long timestampFrom, final long timestampTo) { + return Collections.singletonList(getLatestSample()); + } + + @Override + public void addSample(final RestingMetabolicRateSample timeSample) { + throw new UnsupportedOperationException("addSample not supported"); + } + + @Override + public void addSamples(final List timeSamples) { + throw new UnsupportedOperationException("addSamples not supported"); + } + + @Override + public RestingMetabolicRateSample createSample() { + throw new UnsupportedOperationException("createSample not supported"); + } + + @Nullable + @Override + public RestingMetabolicRateSample getLatestSample() { + return new DefaultRestingMetabolicRateSample(); + } + + @Nullable + @Override + public RestingMetabolicRateSample getFirstSample() { + return getLatestSample(); + } + + private static class DefaultRestingMetabolicRateSample extends RestingMetabolicRateSample { + private long timestamp; + private final int rate; + + private DefaultRestingMetabolicRateSample() { + this.timestamp = System.currentTimeMillis(); + ActivityUser activityUser = new ActivityUser(); + final int weightKg = activityUser.getWeightKg(); + final int heightCm = activityUser.getHeightCm(); + final int age = activityUser.getAge(); + final int s; + switch (activityUser.getGender()) { + case ActivityUser.GENDER_FEMALE: + s = -161; + break; + case ActivityUser.GENDER_MALE: + s = 5; + break; + default: + s = (5 - 161) / 2; // average it? + break; + } + // Mifflin St Jeor equation + this.rate = (int) Math.round(10.0d * weightKg + 6.25 * heightCm - 5d * age + s); + } + + @Override + public int getRestingMetabolicRate() { + return rate; + } + + @Override + public void setTimestamp(final long timestamp) { + this.timestamp = timestamp; + } + + @Override + public long getUserId() { + return 0; + } + + @Override + public void setUserId(final long userId) { + + } + + @Override + public long getDeviceId() { + return 0; + } + + @Override + public void setDeviceId(final long deviceId) { + + } + + @Override + public long getTimestamp() { + return timestamp; + } + } +}