From 71b393932f6a3c08aa048d8c5ee2fac1ed9f8e3b Mon Sep 17 00:00:00 2001 From: vanous Date: Sat, 26 Oct 2019 10:39:15 +0200 Subject: [PATCH] Add switch to set old/new style of sleep detection range --- CHANGELOG.md | 3 +-- .../charts/AbstractChartFragment.java | 2 +- .../activities/charts/SleepChartFragment.java | 27 +++++++++++++------ app/src/main/res/values/strings.xml | 4 +++ app/src/main/res/xml/charts_preferences.xml | 11 ++++++++ 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abe86b871..647bb40bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,7 @@ * Makibes HR3: Support the english version * Makibes HR3: Enable bluetooth pairing for working reconnection * Work around crash when trying to display changelog -* "Limit sleep charts range" (FIXME) -* " Check for non empty list of sleep sessions" (FIXME) +* Sleep detection settings: Rolling 24 hours (existing style) or Noon to noon #### Version 0.37.1 * Amazfit Bip Lite: Support flashing firmware and watchfaces 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 6a08744ec..ff2174330 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 @@ -720,7 +720,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { } protected List getSamplesofSleep(DBHandler db, GBDevice device) { - int SLEEP_HOUR_LIMIT = 13; + int SLEEP_HOUR_LIMIT = 12; int tsStart = getTSStart(); Calendar day = GregorianCalendar.getInstance(); 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 a4588850f..b8c24d649 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 @@ -48,6 +48,7 @@ import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils; import nodomain.freeyourgadget.gadgetbridge.activities.charts.SleepAnalysis.SleepSession; @@ -56,6 +57,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class SleepChartFragment extends AbstractChartFragment { @@ -72,17 +74,26 @@ public class SleepChartFragment extends AbstractChartFragment { @Override protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) { - List samples = getSamplesofSleep(db, device); + Prefs prefs = GBApplication.getPrefs(); + List samples; + if (prefs.getBoolean("chart_sleep_range_24h", false)) { + samples = getSamples(db, device); + }else{ + samples = getSamplesofSleep(db, device); + } MySleepChartsData mySleepChartsData = refreshSleepAmounts(device, samples); - if (mySleepChartsData.sleepSessions.size()>0) { - 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(); + if (!prefs.getBoolean("chart_sleep_range_24h", false)) { + if (mySleepChartsData.sleepSessions.size() > 0) { + 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(); + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0103a073b..25ab31ab3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -532,6 +532,10 @@ Heart rate color Steps per month Sleep per month + Sleep range + Past 24 hours + Noon to noon + Authenticating Authentication required Preferred sleep duration in hours diff --git a/app/src/main/res/xml/charts_preferences.xml b/app/src/main/res/xml/charts_preferences.xml index c3cbd2971..675fee091 100644 --- a/app/src/main/res/xml/charts_preferences.xml +++ b/app/src/main/res/xml/charts_preferences.xml @@ -31,6 +31,15 @@ android:defaultValue="true" android:key="charts_allow_swipe" android:title="@string/pref_title_charts_swipe" /> + + + + +