diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java index 3c49dbfa2..411c74293 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java @@ -712,6 +712,29 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { return samples; } + protected List getSamplesofSleep(DBHandler db, GBDevice device) { + int SLEEP_HOUR_LIMIT = 13; + + int tsStart = getTSStart(); + Calendar day = GregorianCalendar.getInstance(); + day.setTimeInMillis(tsStart * 1000L); + day.set(Calendar.HOUR_OF_DAY, SLEEP_HOUR_LIMIT); + day.set(Calendar.MINUTE, 0); + day.set(Calendar.SECOND, 0); + tsStart = toTimestamp(day.getTime()); + + int tsEnd = getTSEnd(); + day.setTimeInMillis(tsEnd* 1000L); + day.set(Calendar.HOUR_OF_DAY, SLEEP_HOUR_LIMIT); + day.set(Calendar.MINUTE, 0); + day.set(Calendar.SECOND, 0); + tsEnd = toTimestamp(day.getTime()); + + List samples = (List) getSamples(db, device, tsStart, tsEnd); + ensureStartAndEndSamples(samples, tsStart, tsEnd); + return samples; + } + protected void ensureStartAndEndSamples(List samples, int tsStart, int tsEnd) { if (samples == null || samples.isEmpty()) { return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java index 3d388c846..c5fb6529b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java @@ -44,6 +44,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; @@ -52,7 +53,6 @@ import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils; import nodomain.freeyourgadget.gadgetbridge.activities.charts.SleepAnalysis.SleepSession; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmount; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; @@ -72,9 +72,18 @@ public class SleepChartFragment extends AbstractChartFragment { @Override protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) { - List samples = getSamples(db, device); + List samples = getSamplesofSleep(db, device); MySleepChartsData mySleepChartsData = refreshSleepAmounts(device, samples); + long tstart = mySleepChartsData.sleepSessions.get(0).getSleepStart().getTime() / 1000; + long tend = mySleepChartsData.sleepSessions.get(mySleepChartsData.sleepSessions.size() - 1).getSleepEnd().getTime() / 1000; + + for (Iterator iterator = (Iterator) samples.iterator(); iterator.hasNext(); ) { + ActivitySample sample = iterator.next(); + if (sample.getTimestamp() < tstart || sample.getTimestamp() > tend) { + iterator.remove(); + } + } DefaultChartsData chartsData = refresh(device, samples); return new MyChartsData(mySleepChartsData, chartsData);