diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiSampleProvider.java index 8b5167703..c76068829 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiSampleProvider.java @@ -123,10 +123,20 @@ public class XiaomiSampleProvider extends AbstractSampleProvider samples, final int timestamp_from, final int timestamp_to) { - final RangeMap stagesMap = new RangeMap<>(); + final RangeMap stagesMap = new RangeMap<>(RangeMap.Mode.LOWER_BOUND); + final XiaomiSleepTimeSampleProvider sleepTimeSampleProvider = new XiaomiSleepTimeSampleProvider(getDevice(), getSession()); final XiaomiSleepStageSampleProvider sleepStagesSampleProvider = new XiaomiSleepStageSampleProvider(getDevice(), getSession()); + // First populate all samples within this range + final List sleepTimesWithinRange = sleepTimeSampleProvider.getAllSamples(timestamp_from * 1000L, timestamp_to * 1000L); + LOG.trace("Found {} sleep samples between {} and {}", sleepTimesWithinRange.size(), timestamp_from, timestamp_to); + + for (final XiaomiSleepTimeSample sleepTimeSample : sleepTimesWithinRange) { + stagesMap.put(sleepTimeSample.getWakeupTime(), ActivityKind.UNKNOWN); + stagesMap.put(sleepTimeSample.getTimestamp(), ActivityKind.LIGHT_SLEEP); + } + // Retrieve the last stage before this time range, as the user could have been asleep during // the range transition final XiaomiSleepStageSample lastSleepStageBeforeRange = sleepStagesSampleProvider.getLastSampleBefore(timestamp_from * 1000L); @@ -151,13 +161,14 @@ public class XiaomiSampleProvider extends AbstractSampleProvider