1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-28 18:47:32 +01:00

Prevent heart rate average from using invalid samples

This commit is contained in:
José Rebelo 2024-11-16 14:08:06 +00:00
parent 87bc2e6ed7
commit 1a3a7dec05

View File

@ -38,6 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.HeartRateSample; import nodomain.freeyourgadget.gadgetbridge.model.HeartRateSample;
import nodomain.freeyourgadget.gadgetbridge.util.Accumulator;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDailyFragment.HeartRateData> { public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDailyFragment.HeartRateData> {
@ -215,16 +216,16 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
final TimestampTranslation tsTranslation = new TimestampTranslation(); final TimestampTranslation tsTranslation = new TimestampTranslation();
final List<Entry> lineEntries = new ArrayList<>(); final List<Entry> lineEntries = new ArrayList<>();
List<? extends ActivitySample> samples = data.samples; List<? extends ActivitySample> samples = data.samples;
int average = 0; final Accumulator accumulator = new Accumulator();
int minimum = 0;
int maximum = 0;
int sum = 0;
int n = 0;
int lastHrSampleIndex = -1; int lastHrSampleIndex = -1;
for (int i =0; i < samples.size(); i++) { for (int i =0; i < samples.size(); i++) {
ActivitySample sample = samples.get(i); final ActivitySample sample = samples.get(i);
int ts = tsTranslation.shorten(sample.getTimestamp()); final int ts = tsTranslation.shorten(sample.getTimestamp());
if (sample.getKind() != ActivityKind.NOT_WORN && heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) { if (!heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
continue;
}
if (sample.getKind() != ActivityKind.NOT_WORN) {
if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 1800 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) { if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 1800 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
lineEntries.add(new Entry(lastHrSampleIndex + 1, 0 )); lineEntries.add(new Entry(lastHrSampleIndex + 1, 0 ));
lineEntries.add(new Entry(ts - 1, 0)); lineEntries.add(new Entry(ts - 1, 0));
@ -232,17 +233,7 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
lineEntries.add(new Entry(ts, sample.getHeartRate())); lineEntries.add(new Entry(ts, sample.getHeartRate()));
lastHrSampleIndex = ts; lastHrSampleIndex = ts;
} }
if (sample.getHeartRate() <= 0) { accumulator.add(sample.getHeartRate());
continue;
}
n++;
sum += sample.getHeartRate();
if (sample.getHeartRate() > maximum) {
maximum = sample.getHeartRate();
}
if (minimum == 0 || sample.getHeartRate() < minimum) {
minimum = sample.getHeartRate();
}
} }
LineDataSet dataSet = new LineDataSet(lineEntries, "Heart Rate"); LineDataSet dataSet = new LineDataSet(lineEntries, "Heart Rate");
@ -255,16 +246,15 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
dataSet.setColor(HEARTRATE_COLOR); dataSet.setColor(HEARTRATE_COLOR);
dataSet.setValueTextColor(CHART_TEXT_COLOR); dataSet.setValueTextColor(CHART_TEXT_COLOR);
if (n > 0 && sum > 0) { final int average = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getAverage()) : -1;
average = sum / n; final int minimum = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getMin()) : -1;
} final int maximum = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getMax()) : -1;
hrAverage.setText(average > 0 ? getString(R.string.bpm_value_unit, average) : "-"); hrAverage.setText(average > 0 ? getString(R.string.bpm_value_unit, average) : "-");
hrMinimum.setText(minimum > 0 ? getString(R.string.bpm_value_unit, minimum) : "-"); hrMinimum.setText(minimum > 0 ? getString(R.string.bpm_value_unit, minimum) : "-");
hrMaximum.setText(maximum > 0 ? getString(R.string.bpm_value_unit, maximum) : "-"); hrMaximum.setText(maximum > 0 ? getString(R.string.bpm_value_unit, maximum) : "-");
hrResting.setText(data.restingHeartRate > 0 ? getString(R.string.bpm_value_unit, data.restingHeartRate) : "-"); hrResting.setText(data.restingHeartRate > 0 ? getString(R.string.bpm_value_unit, data.restingHeartRate) : "-");
if (minimum > 0) { if (minimum > 0) {
hrLineChart.getAxisLeft().setAxisMinimum(Math.max(minimum - 30, 0)); hrLineChart.getAxisLeft().setAxisMinimum(Math.max(minimum - 30, 0));
hrLineChart.getAxisRight().setAxisMinimum(Math.max(minimum - 30, 0)); hrLineChart.getAxisRight().setAxisMinimum(Math.max(minimum - 30, 0));
@ -279,7 +269,7 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
hrLineChart.getAxisLeft().removeAllLimitLines(); hrLineChart.getAxisLeft().removeAllLimitLines();
if (GBApplication.getPrefs().getBoolean("charts_show_average", true)) { if (average > 0 && GBApplication.getPrefs().getBoolean("charts_show_average", true)) {
final LimitLine averageLine = new LimitLine(average); final LimitLine averageLine = new LimitLine(average);
averageLine.setLineWidth(1.5f); averageLine.setLineWidth(1.5f);
averageLine.enableDashedLine(15f, 10f, 0f); averageLine.enableDashedLine(15f, 10f, 0f);