diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java index 3712d4f91..b226fa99a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java @@ -128,6 +128,29 @@ public abstract class AbstractSampleProvider i return sample; } + @Nullable + @Override + public T getLatestActivitySample(final int until) { + QueryBuilder qb = getSampleDao().queryBuilder(); + Device dbDevice = DBHelper.findDevice(getDevice(), getSession()); + if (dbDevice == null) { + // no device, no sample + return null; + } + Property deviceProperty = getDeviceIdentifierSampleProperty(); + Property timestampProperty = getTimestampSampleProperty(); + qb.where(timestampProperty.le(until)) + .where(deviceProperty.eq(dbDevice.getId())) + .orderDesc(timestampProperty).limit(1); + List samples = qb.build().list(); + if (samples.isEmpty()) { + return null; + } + T sample = samples.get(0); + sample.setProvider(this); + return sample; + } + @Nullable @Override public T getFirstActivitySample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleToTimeSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleToTimeSampleProvider.java index 8546875da..005d88001 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleToTimeSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleToTimeSampleProvider.java @@ -88,6 +88,13 @@ public abstract class AbstractSampleToTimeSampleProvider i return samples.get(0); } + @Nullable + @Override + public T getLatestSample(final long until) { + final QueryBuilder qb = getSampleDao().queryBuilder(); + final Device dbDevice = DBHelper.findDevice(getDevice(), getSession()); + if (dbDevice == null) { + // no device, no sample + return null; + } + final Property deviceProperty = getDeviceIdentifierSampleProperty(); + qb.where(getTimestampSampleProperty().le(until)) + .where(deviceProperty.eq(dbDevice.getId())) + .orderDesc(getTimestampSampleProperty()).limit(1); + final List samples = qb.build().list(); + if (samples.isEmpty()) { + return null; + } + return samples.get(0); + } + @Nullable public T getLastSampleBefore(final long timestampTo) { final Device dbDevice = DBHelper.findDevice(getDevice(), getSession()); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java index 2ec813eda..e35e6cd48 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java @@ -105,6 +105,13 @@ public interface SampleProvider { @Nullable T getLatestActivitySample(); + /** + * Returns the activity sample with the highest timestamp, until a limit (inclusive). or null if none + * @return the latest sample or null + */ + @Nullable + T getLatestActivitySample(int until); + /** * Returns the activity sample with the oldest timestamp or null if none * @return the oldest sample or null diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/TimeSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/TimeSampleProvider.java index 08f82b6c6..475c64603 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/TimeSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/TimeSampleProvider.java @@ -79,6 +79,16 @@ public interface TimeSampleProvider { @Nullable T getLatestSample(); + /** + * Returns the sample with the highest timestamp until a limit, or null if none. + * + * @param until maximum timestamp of the sample, inclusive + * + * @return the latest sample, or null if none is found. + */ + @Nullable + T getLatestSample(long until); + /** * Returns the sample with the oldest timestamp, or null if none. * diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java index 77e6935d4..d56035c48 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java @@ -99,6 +99,12 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { return null; } + @Nullable + @Override + public AbstractActivitySample getLatestActivitySample(final int until) { + return null; + } + @Nullable @Override public AbstractActivitySample getFirstActivitySample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/Vo2MaxSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/Vo2MaxSampleProvider.java index 276ba6ee6..bddd6be45 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/Vo2MaxSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/Vo2MaxSampleProvider.java @@ -7,4 +7,9 @@ import nodomain.freeyourgadget.gadgetbridge.model.Vo2MaxSample; public interface Vo2MaxSampleProvider extends TimeSampleProvider { @Nullable T getLatestSample(Vo2MaxSample.Type type, long until); + + @Nullable + default T getLatestSample(long until) { + return getLatestSample(Vo2MaxSample.Type.ANY, until); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTemperatureSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTemperatureSampleProvider.java index 1f99b9cd8..0ee2824ba 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTemperatureSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTemperatureSampleProvider.java @@ -8,27 +8,19 @@ import org.slf4j.LoggerFactory; import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; -import de.greenrobot.dao.Property; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; -import nodomain.freeyourgadget.gadgetbridge.entities.Device; -import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiActivitySample; -import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictData; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataDao; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataValues; import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataValuesDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample; -import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.p2p.HuaweiP2PDataDictionarySyncService; public class HuaweiTemperatureSampleProvider implements TimeSampleProvider { @@ -159,6 +151,37 @@ public class HuaweiTemperatureSampleProvider implements TimeSampleProvider qb = this.session.getHuaweiDictDataDao().queryBuilder(); + qb.where(HuaweiDictDataDao.Properties.StartTimestamp.le(until)) + .where(HuaweiDictDataDao.Properties.DeviceId.eq(deviceId)) + .where(HuaweiDictDataDao.Properties.UserId.eq(userId)) + .where(HuaweiDictDataDao.Properties.DictClass.eq(HuaweiDictTypes.SKIN_TEMPERATURE_CLASS)); + qb.orderDesc(HuaweiDictDataDao.Properties.StartTimestamp).limit(1); + + final List data = qb.build().list(); + if (data.isEmpty()) + return null; + + + QueryBuilder qbv = this.session.getHuaweiDictDataValuesDao().queryBuilder(); + qbv.where(HuaweiDictDataValuesDao.Properties.DictType.eq(HuaweiDictTypes.SKIN_TEMPERATURE_VALUE)).where(HuaweiDictDataValuesDao.Properties.Tag.eq(10)).where(HuaweiDictDataValuesDao.Properties.DictId.eq(data.get(0).getDictId())); + final List valuesData = qbv.build().list(); + + if (valuesData.isEmpty()) + return null; + + return new HuaweiTemperatureSample(valuesData.get(0).getHuaweiDictData().getStartTimestamp(), (float) conv2Double(valuesData.get(0).getValue())); + } + @Nullable @Override public TemperatureSample getFirstSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/AbstractTestSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/AbstractTestSampleProvider.java new file mode 100644 index 000000000..ae3b3f2df --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/AbstractTestSampleProvider.java @@ -0,0 +1,48 @@ +/* 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.test.samples; + +import androidx.annotation.Nullable; + +import java.util.List; + +import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; +import nodomain.freeyourgadget.gadgetbridge.model.TimeSample; + +public abstract class AbstractTestSampleProvider implements TimeSampleProvider { + @Nullable + @Override + public S getLatestSample(final long until) { + final List allSamples = getAllSamples(until - 60, until); + return !allSamples.isEmpty() ? allSamples.get(allSamples.size() - 1) : null; + } + + @Override + public void addSample(final S timeSample) { + throw new UnsupportedOperationException("read-only sample provider"); + } + + @Override + public void addSamples(final List timeSamples) { + throw new UnsupportedOperationException("read-only sample provider"); + } + + @Override + public S createSample() { + throw new UnsupportedOperationException("read-only sample provider"); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestBodyEnergySampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestBodyEnergySampleProvider.java index 9a26109e8..f754dabde 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestBodyEnergySampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestBodyEnergySampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.BodyEnergySample; -public class TestBodyEnergySampleProvider implements TimeSampleProvider { +public class TestBodyEnergySampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -39,21 +38,6 @@ public class TestBodyEnergySampleProvider implements TimeSampleProvider timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public BodyEnergySample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public BodyEnergySample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvSummarySampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvSummarySampleProvider.java index 6ad6fc9f6..c937052e8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvSummarySampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvSummarySampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.HrvSummarySample; -public class TestHrvSummarySampleProvider implements TimeSampleProvider { +public class TestHrvSummarySampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -39,21 +38,6 @@ public class TestHrvSummarySampleProvider implements TimeSampleProvider timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public HrvSummarySample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public HrvSummarySample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvValueSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvValueSampleProvider.java index e39b64772..00d845b26 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvValueSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestHrvValueSampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.HrvValueSample; -public class TestHrvValueSampleProvider implements TimeSampleProvider { +public class TestHrvValueSampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -42,21 +41,6 @@ public class TestHrvValueSampleProvider implements TimeSampleProvider timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public HrvValueSample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public HrvValueSample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestPaiSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestPaiSampleProvider.java index 2eac30e91..de9c9b85f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestPaiSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestPaiSampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.PaiSample; -public class TestPaiSampleProvider implements TimeSampleProvider { +public class TestPaiSampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -56,21 +55,6 @@ public class TestPaiSampleProvider implements TimeSampleProvider { return samples; } - @Override - public void addSample(final PaiSample timeSample) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public void addSamples(final List timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public PaiSample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public PaiSample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestRespiratoryRateSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestRespiratoryRateSampleProvider.java index bf880777b..dc736b101 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestRespiratoryRateSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestRespiratoryRateSampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.RespiratoryRateSample; -public class TestRespiratoryRateSampleProvider implements TimeSampleProvider { +public class TestRespiratoryRateSampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -39,21 +38,6 @@ public class TestRespiratoryRateSampleProvider implements TimeSampleProvider timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public RespiratoryRateSample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public RespiratoryRateSample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestSpo2SampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestSpo2SampleProvider.java index ca271fe41..2e66f792e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestSpo2SampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestSpo2SampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.Spo2Sample; -public class TestSpo2SampleProvider implements TimeSampleProvider { +public class TestSpo2SampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -44,21 +43,6 @@ public class TestSpo2SampleProvider implements TimeSampleProvider { return samples; } - @Override - public void addSample(final Spo2Sample timeSample) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public void addSamples(final List timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public Spo2Sample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public Spo2Sample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestStressSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestStressSampleProvider.java index 8ae8ec9be..5a890c32c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestStressSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestStressSampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.StressSample; -public class TestStressSampleProvider implements TimeSampleProvider { +public class TestStressSampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -44,21 +43,6 @@ public class TestStressSampleProvider implements TimeSampleProvider timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public StressSample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public StressSample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestTemperatureSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestTemperatureSampleProvider.java index f7efdac76..6e7005f0b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestTemperatureSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/samples/TestTemperatureSampleProvider.java @@ -22,11 +22,10 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; -import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand; import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample; -public class TestTemperatureSampleProvider implements TimeSampleProvider { +public class TestTemperatureSampleProvider extends AbstractTestSampleProvider { @NonNull @Override public List getAllSamples(final long timestampFrom, final long timestampTo) { @@ -45,21 +44,6 @@ public class TestTemperatureSampleProvider implements TimeSampleProvider timeSamples) { - throw new UnsupportedOperationException("read-only sample provider"); - } - - @Override - public TemperatureSample createSample() { - throw new UnsupportedOperationException("read-only sample provider"); - } - @Nullable @Override public TemperatureSample getLatestSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiHeartRateRestingSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiHeartRateRestingSampleProvider.java index eebac3a04..1a66b8251 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiHeartRateRestingSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiHeartRateRestingSampleProvider.java @@ -71,6 +71,16 @@ public class XiaomiHeartRateRestingSampleProvider implements TimeSampleProvider< return null; } + @Nullable + @Override + public HeartRateSample getLatestSample(final long until) { + final XiaomiDailySummarySample sample = dailySummarySampleProvider.getLatestSample(until); + if (sample != null) { + return new XiaomiHeartRateRestingSample(sample); + } + return null; + } + @Nullable @Override public HeartRateSample getFirstSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiManualSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiManualSampleProvider.java index 6fc53fd2d..55c0d7643 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiManualSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiManualSampleProvider.java @@ -84,6 +84,26 @@ public class XiaomiManualSampleProvider extends AbstractTimeSampleProvider qb = getSampleDao().queryBuilder(); + final Device dbDevice = DBHelper.findDevice(getDevice(), getSession()); + if (dbDevice == null) { + // no device, no sample + return null; + } + final Property deviceProperty = getDeviceIdentifierSampleProperty(); + qb.where(deviceProperty.eq(dbDevice.getId())) + .where(XiaomiManualSampleDao.Properties.Timestamp.le(until)) + .where(XiaomiManualSampleDao.Properties.Type.eq(type)) + .orderDesc(getTimestampSampleProperty()).limit(1); + final List samples = qb.build().list(); + if (samples.isEmpty()) { + return null; + } + return samples.get(0); + } + @Nullable public XiaomiManualSample getFirstSample(final int type) { final QueryBuilder qb = getSampleDao().queryBuilder(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiPaiSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiPaiSampleProvider.java index 0eaa3cc01..190804621 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiPaiSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiPaiSampleProvider.java @@ -71,6 +71,16 @@ public class XiaomiPaiSampleProvider implements TimeSampleProvider { return null; } + @Nullable + @Override + public PaiSample getLatestSample(final long until) { + final XiaomiDailySummarySample sample = dailySummarySampleProvider.getLatestSample(until); + if (sample != null) { + return new XiaomiPaiSample(sample); + } + return null; + } + @Nullable @Override public PaiSample getFirstSample() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiTemperatureSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiTemperatureSampleProvider.java index 2a37ed00e..78ce1cc1e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiTemperatureSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiTemperatureSampleProvider.java @@ -76,6 +76,16 @@ public class XiaomiTemperatureSampleProvider implements TimeSampleProvider