2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer,
|
2019-11-23 21:52:46 +01:00
|
|
|
Daniele Gobbetti, Dikay900, Pavel Elagin, Q-er, vanous
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>. */
|
2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-10-31 23:44:06 +01:00
|
|
|
import android.graphics.Color;
|
2015-06-23 23:03:05 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2020-10-31 23:44:06 +01:00
|
|
|
import android.widget.ImageView;
|
2018-08-16 16:59:56 +02:00
|
|
|
import android.widget.TextView;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2020-11-01 23:58:54 +01:00
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
import com.github.mikephil.charting.animation.Easing;
|
2015-07-14 00:29:32 +02:00
|
|
|
import com.github.mikephil.charting.charts.Chart;
|
2017-11-01 17:34:46 +01:00
|
|
|
import com.github.mikephil.charting.charts.LineChart;
|
2015-07-14 00:29:32 +02:00
|
|
|
import com.github.mikephil.charting.charts.PieChart;
|
2016-09-18 23:37:41 +02:00
|
|
|
import com.github.mikephil.charting.components.LegendEntry;
|
2020-10-31 23:44:06 +01:00
|
|
|
import com.github.mikephil.charting.components.LimitLine;
|
2015-06-23 23:03:05 +02:00
|
|
|
import com.github.mikephil.charting.components.XAxis;
|
|
|
|
import com.github.mikephil.charting.components.YAxis;
|
2017-11-01 17:34:46 +01:00
|
|
|
import com.github.mikephil.charting.data.LineData;
|
2015-07-14 00:29:32 +02:00
|
|
|
import com.github.mikephil.charting.data.PieData;
|
|
|
|
import com.github.mikephil.charting.data.PieDataSet;
|
2016-09-18 23:37:41 +02:00
|
|
|
import com.github.mikephil.charting.data.PieEntry;
|
2019-08-24 12:41:26 +02:00
|
|
|
import com.github.mikephil.charting.formatter.ValueFormatter;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
import org.apache.commons.lang3.tuple.Triple;
|
2015-06-23 23:03:05 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
import java.text.DecimalFormat;
|
2015-06-23 23:03:05 +02:00
|
|
|
import java.util.ArrayList;
|
2019-09-17 00:47:10 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
2019-10-12 07:22:13 +02:00
|
|
|
import java.util.Iterator;
|
2015-06-23 23:03:05 +02:00
|
|
|
import java.util.List;
|
2015-07-20 23:11:16 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2019-10-26 10:39:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-06-23 23:03:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2016-04-12 23:12:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils;
|
2019-09-17 00:47:10 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.charts.SleepAnalysis.SleepSession;
|
2015-08-17 02:22:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-11-09 23:52:00 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
2015-07-27 23:49:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
2015-08-17 02:22:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
2019-10-26 10:39:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
public class SleepChartFragment extends AbstractChartFragment {
|
|
|
|
protected static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2017-11-01 17:34:46 +01:00
|
|
|
private LineChart mActivityChart;
|
2015-07-14 00:29:32 +02:00
|
|
|
private PieChart mSleepAmountChart;
|
2018-08-16 16:59:56 +02:00
|
|
|
private TextView mSleepchartInfo;
|
2020-10-31 23:44:06 +01:00
|
|
|
private TextView heartRateAverageLabel;
|
2020-11-01 23:58:54 +01:00
|
|
|
private ImageView heartRateIcon;
|
2020-11-06 14:28:51 +01:00
|
|
|
private TextView intensityTotalLabel;
|
|
|
|
private ImageView intensityTotalIcon;
|
2020-11-01 23:58:54 +01:00
|
|
|
private int heartRateMin = 0;
|
|
|
|
private int heartRateMax = 0;
|
2020-11-06 14:28:51 +01:00
|
|
|
private float intensityTotal = 0;
|
|
|
|
|
2020-11-01 23:58:54 +01:00
|
|
|
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
private int mSmartAlarmFrom = -1;
|
|
|
|
private int mSmartAlarmTo = -1;
|
|
|
|
private int mTimestampFrom = -1;
|
|
|
|
private int mSmartAlarmGoneOff = -1;
|
2020-10-31 23:44:06 +01:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
private boolean CHARTS_SLEEP_RANGE_24H = prefs.getBoolean("chart_sleep_range_24h", false);
|
|
|
|
private boolean SHOW_CHARTS_AVERAGE = GBApplication.getPrefs().getBoolean("charts_show_average", true);
|
|
|
|
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
@Override
|
2016-04-11 23:18:12 +02:00
|
|
|
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
|
2019-10-26 10:39:15 +02:00
|
|
|
List<? extends ActivitySample> samples;
|
2020-10-31 23:44:06 +01:00
|
|
|
if (CHARTS_SLEEP_RANGE_24H) {
|
2019-10-26 10:39:15 +02:00
|
|
|
samples = getSamples(db, device);
|
2020-10-31 23:44:06 +01:00
|
|
|
} else {
|
2019-10-26 10:39:15 +02:00
|
|
|
samples = getSamplesofSleep(db, device);
|
|
|
|
}
|
2015-07-14 00:29:32 +02:00
|
|
|
|
2016-04-11 23:18:12 +02:00
|
|
|
MySleepChartsData mySleepChartsData = refreshSleepAmounts(device, samples);
|
2019-10-26 10:39:15 +02:00
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
if (!CHARTS_SLEEP_RANGE_24H) {
|
2019-10-26 10:39:15 +02:00
|
|
|
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<ActivitySample> iterator = (Iterator<ActivitySample>) samples.iterator(); iterator.hasNext(); ) {
|
|
|
|
ActivitySample sample = iterator.next();
|
|
|
|
if (sample.getTimestamp() < tstart || sample.getTimestamp() > tend) {
|
|
|
|
iterator.remove();
|
|
|
|
}
|
2019-10-15 21:32:27 +02:00
|
|
|
}
|
2019-10-12 07:22:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-11 23:18:12 +02:00
|
|
|
DefaultChartsData chartsData = refresh(device, samples);
|
2020-10-31 23:44:06 +01:00
|
|
|
Triple<Float, Integer, Integer> hrData = calculateHrData(samples);
|
2020-11-06 14:28:51 +01:00
|
|
|
Triple<Float, Float, Float> intensityData = calculateIntensityData(samples);
|
|
|
|
return new MyChartsData(mySleepChartsData, chartsData, hrData.getLeft(), hrData.getMiddle(), hrData.getRight(), intensityData.getLeft(), intensityData.getMiddle(), intensityData.getRight());
|
2015-07-14 00:29:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
|
|
|
|
|
2016-05-16 23:36:54 +02:00
|
|
|
private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List<? extends ActivitySample> samples) {
|
2019-09-17 00:47:10 +02:00
|
|
|
SleepAnalysis sleepAnalysis = new SleepAnalysis();
|
|
|
|
List<SleepSession> sleepSessions = sleepAnalysis.calculateSleepSessions(samples);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
PieData data = new PieData();
|
2019-09-17 00:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
final long lightSleepDuration = calculateLightSleepDuration(sleepSessions);
|
|
|
|
final long deepSleepDuration = calculateDeepSleepDuration(sleepSessions);
|
|
|
|
|
|
|
|
final long totalSeconds = lightSleepDuration + deepSleepDuration;
|
|
|
|
|
|
|
|
final List<PieEntry> entries;
|
|
|
|
final List<Integer> colors;
|
|
|
|
|
|
|
|
if (sleepSessions.isEmpty()) {
|
|
|
|
entries = Collections.emptyList();
|
|
|
|
colors = Collections.emptyList();
|
|
|
|
} else {
|
|
|
|
entries = Arrays.asList(
|
|
|
|
new PieEntry(lightSleepDuration, getActivity().getString(R.string.abstract_chart_fragment_kind_light_sleep)),
|
|
|
|
new PieEntry(deepSleepDuration, getActivity().getString(R.string.abstract_chart_fragment_kind_deep_sleep))
|
|
|
|
);
|
|
|
|
colors = Arrays.asList(
|
|
|
|
getColorFor(ActivityKind.TYPE_LIGHT_SLEEP),
|
|
|
|
getColorFor(ActivityKind.TYPE_DEEP_SLEEP)
|
|
|
|
);
|
2015-07-14 00:29:32 +02:00
|
|
|
}
|
2019-09-17 00:47:10 +02:00
|
|
|
|
|
|
|
|
2015-11-09 23:52:00 +01:00
|
|
|
String totalSleep = DateTimeUtils.formatDurationHoursMinutes(totalSeconds, TimeUnit.SECONDS);
|
2015-07-20 23:11:16 +02:00
|
|
|
PieDataSet set = new PieDataSet(entries, "");
|
2019-08-24 12:41:26 +02:00
|
|
|
set.setValueFormatter(new ValueFormatter() {
|
2015-07-20 23:11:16 +02:00
|
|
|
@Override
|
2019-08-24 12:55:33 +02:00
|
|
|
public String getFormattedValue(float value) {
|
2015-08-17 02:22:16 +02:00
|
|
|
return DateTimeUtils.formatDurationHoursMinutes((long) value, TimeUnit.SECONDS);
|
2015-07-20 23:11:16 +02:00
|
|
|
}
|
|
|
|
});
|
2015-07-14 00:29:32 +02:00
|
|
|
set.setColors(colors);
|
2017-04-05 00:08:17 +02:00
|
|
|
set.setValueTextColor(DESCRIPTION_COLOR);
|
|
|
|
set.setValueTextSize(13f);
|
|
|
|
set.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
|
|
|
|
set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
|
2015-07-14 00:29:32 +02:00
|
|
|
data.setDataSet(set);
|
|
|
|
|
2015-07-21 21:24:35 +02:00
|
|
|
//setupLegend(pieChart);
|
2019-09-17 00:47:10 +02:00
|
|
|
return new MySleepChartsData(totalSleep, data, sleepSessions);
|
|
|
|
}
|
|
|
|
|
|
|
|
private long calculateLightSleepDuration(List<SleepSession> sleepSessions) {
|
|
|
|
long result = 0;
|
|
|
|
for (SleepSession sleepSession : sleepSessions) {
|
|
|
|
result += sleepSession.getLightSleepDuration();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private long calculateDeepSleepDuration(List<SleepSession> sleepSessions) {
|
|
|
|
long result = 0;
|
|
|
|
for (SleepSession sleepSession : sleepSessions) {
|
|
|
|
result += sleepSession.getDeepSleepDuration();
|
|
|
|
}
|
|
|
|
return result;
|
2016-04-11 23:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void updateChartsnUIThread(ChartsData chartsData) {
|
|
|
|
MyChartsData mcd = (MyChartsData) chartsData;
|
2019-09-17 00:47:10 +02:00
|
|
|
MySleepChartsData pieData = mcd.getPieData();
|
|
|
|
mSleepAmountChart.setCenterText(pieData.getTotalSleep());
|
|
|
|
mSleepAmountChart.setData(pieData.getPieData());
|
2016-09-29 22:16:08 +02:00
|
|
|
mActivityChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317
|
2016-10-03 21:06:18 +02:00
|
|
|
mActivityChart.getXAxis().setValueFormatter(mcd.getChartsData().getXValueFormatter());
|
2016-10-07 21:46:00 +02:00
|
|
|
mActivityChart.setData(mcd.getChartsData().getData());
|
2020-10-31 23:44:06 +01:00
|
|
|
mSleepchartInfo.setText(buildYouSleptText(pieData));
|
2020-11-01 23:58:54 +01:00
|
|
|
heartRateMin = mcd.getHeartRateAxisMin();
|
|
|
|
heartRateMax = mcd.getHeartRateAxisMax();
|
2020-11-06 14:28:51 +01:00
|
|
|
intensityTotal = mcd.getIntensityTotal();
|
2020-10-31 23:44:06 +01:00
|
|
|
|
|
|
|
if (!CHARTS_SLEEP_RANGE_24H
|
|
|
|
&& supportsHeartrate(getChartsHost().getDevice())
|
|
|
|
&& SHOW_CHARTS_AVERAGE) {
|
|
|
|
if (mcd.getHeartRateAxisMax() != 0 || mcd.getHeartRateAxisMin() != 0) {
|
|
|
|
mActivityChart.getAxisRight().setAxisMaximum(mcd.getHeartRateAxisMax());
|
|
|
|
mActivityChart.getAxisRight().setAxisMinimum(mcd.getHeartRateAxisMin());
|
|
|
|
}
|
|
|
|
LimitLine hrAverage_line = new LimitLine(mcd.getHeartRateAverage());
|
|
|
|
hrAverage_line.setLineColor(Color.RED);
|
|
|
|
hrAverage_line.setLineWidth(0.1f);
|
|
|
|
mActivityChart.getAxisRight().removeAllLimitLines();
|
|
|
|
mActivityChart.getAxisRight().addLimitLine(hrAverage_line);
|
2020-11-06 14:28:51 +01:00
|
|
|
DecimalFormat df = new DecimalFormat("###.#");
|
2020-10-31 23:44:06 +01:00
|
|
|
heartRateAverageLabel.setText(df.format(mcd.getHeartRateAverage()));
|
2020-11-06 14:28:51 +01:00
|
|
|
intensityTotalLabel.setText(df.format(mcd.getIntensityTotal()));
|
2020-10-31 23:44:06 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 16:59:56 +02:00
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
private Triple<Float, Integer, Integer> calculateHrData(List<? extends ActivitySample> samples) {
|
|
|
|
if (samples.toArray().length < 1) {
|
|
|
|
return Triple.of(0f, 0, 0);
|
|
|
|
}
|
2019-09-17 00:47:10 +02:00
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
List<Integer> heartRateValues = new ArrayList<>();
|
|
|
|
HeartRateUtils heartRateUtilsInstance = HeartRateUtils.getInstance();
|
|
|
|
for (ActivitySample sample : samples) {
|
|
|
|
if (sample.getKind() == ActivityKind.TYPE_LIGHT_SLEEP || sample.getKind() == ActivityKind.TYPE_DEEP_SLEEP) {
|
|
|
|
int heartRate = sample.getHeartRate();
|
|
|
|
if (heartRateUtilsInstance.isValidHeartRateValue(heartRate)) {
|
|
|
|
heartRateValues.add(heartRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (heartRateValues.toArray().length < 1) {
|
|
|
|
return Triple.of(0f, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int min = Collections.min(heartRateValues);
|
|
|
|
int max = Collections.max(heartRateValues);
|
|
|
|
int count = heartRateValues.toArray().length;
|
2020-11-07 17:31:15 +01:00
|
|
|
float sum = calculateSumOfInts(heartRateValues);
|
2020-10-31 23:44:06 +01:00
|
|
|
float average = sum / count;
|
|
|
|
return Triple.of(average, min, max);
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:28:51 +01:00
|
|
|
private float calculateIntensitySum(List<Float> samples) {
|
|
|
|
float result = 0;
|
|
|
|
for (Float sample : samples) {
|
|
|
|
result += sample;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-07 17:31:15 +01:00
|
|
|
private float calculateSumOfInts(List<Integer> samples) {
|
2020-10-31 23:44:06 +01:00
|
|
|
float result = 0;
|
|
|
|
for (Integer sample : samples) {
|
|
|
|
result += sample;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:28:51 +01:00
|
|
|
private Triple<Float, Float, Float> calculateIntensityData(List<? extends ActivitySample> samples) {
|
|
|
|
if (samples.toArray().length < 1) {
|
|
|
|
return Triple.of(0f, 0f, 0f);
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:44:06 +01:00
|
|
|
List<Float> allIntensities = new ArrayList<>();
|
|
|
|
|
|
|
|
for (ActivitySample s : samples) {
|
|
|
|
if (s.getKind() == ActivityKind.TYPE_LIGHT_SLEEP || s.getKind() == ActivityKind.TYPE_DEEP_SLEEP) {
|
2020-11-06 14:28:51 +01:00
|
|
|
float intensity = s.getIntensity();
|
|
|
|
allIntensities.add(intensity);
|
2020-10-31 23:44:06 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 14:28:51 +01:00
|
|
|
if (allIntensities.toArray().length < 1) {
|
|
|
|
return Triple.of(0f, 0f, 0f);
|
|
|
|
}
|
2020-10-31 23:44:06 +01:00
|
|
|
|
|
|
|
Float min = Collections.min(allIntensities);
|
|
|
|
Float max = Collections.max(allIntensities);
|
2020-11-06 14:28:51 +01:00
|
|
|
Float sum = calculateIntensitySum(allIntensities);
|
|
|
|
|
|
|
|
return Triple.of(sum, min, max);
|
2019-09-17 00:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private String buildYouSleptText(MySleepChartsData pieData) {
|
|
|
|
final StringBuilder result = new StringBuilder();
|
|
|
|
if (pieData.getSleepSessions().isEmpty()) {
|
|
|
|
result.append(getContext().getString(R.string.you_did_not_sleep));
|
2018-08-16 16:59:56 +02:00
|
|
|
} else {
|
2019-09-17 00:47:10 +02:00
|
|
|
for (SleepSession sleepSession : pieData.getSleepSessions()) {
|
|
|
|
result.append(getContext().getString(
|
|
|
|
R.string.you_slept,
|
|
|
|
DateTimeUtils.timeToString(sleepSession.getSleepStart()),
|
|
|
|
DateTimeUtils.timeToString(sleepSession.getSleepEnd())));
|
|
|
|
result.append('\n');
|
|
|
|
}
|
2018-08-16 16:59:56 +02:00
|
|
|
}
|
2019-09-17 00:47:10 +02:00
|
|
|
return result.toString();
|
2015-07-14 00:29:32 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 00:33:37 +02:00
|
|
|
@Override
|
|
|
|
public String getTitle() {
|
|
|
|
return getString(R.string.sleepchart_your_sleep);
|
|
|
|
}
|
|
|
|
|
2015-06-23 23:03:05 +02:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
2015-07-14 00:29:32 +02:00
|
|
|
View rootView = inflater.inflate(R.layout.fragment_sleepchart, container, false);
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2018-08-16 23:53:54 +02:00
|
|
|
mActivityChart = rootView.findViewById(R.id.sleepchart);
|
|
|
|
mSleepAmountChart = rootView.findViewById(R.id.sleepchart_pie_light_deep);
|
|
|
|
mSleepchartInfo = rootView.findViewById(R.id.sleepchart_info);
|
2020-11-01 23:58:54 +01:00
|
|
|
heartRateIcon = rootView.findViewById(R.id.heartrate_widget_icon);
|
2020-10-31 23:44:06 +01:00
|
|
|
heartRateAverageLabel = rootView.findViewById(R.id.heartrate_widget_label);
|
2020-11-06 14:28:51 +01:00
|
|
|
intensityTotalIcon = rootView.findViewById(R.id.intensity_widget_icon);
|
|
|
|
intensityTotalLabel = rootView.findViewById(R.id.intensity_widget_label);
|
|
|
|
|
|
|
|
ConstraintLayout intensityTotalWidgetLayout = rootView.findViewById(R.id.intensity_widget_layout);
|
2020-11-01 23:58:54 +01:00
|
|
|
ConstraintLayout heartRateWidgetLayout = rootView.findViewById(R.id.heartrate_widget_layout);
|
|
|
|
|
2020-11-06 14:28:51 +01:00
|
|
|
View.OnClickListener listener = new View.OnClickListener() {
|
2020-11-01 23:58:54 +01:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2020-11-06 14:28:51 +01:00
|
|
|
DecimalFormat df = new DecimalFormat("###.#");
|
|
|
|
String detailedDuration = String.format(getString(R.string.charts_min_max_heartrate_popup), heartRateMin, heartRateMax, df.format(intensityTotal));
|
2020-11-01 23:58:54 +01:00
|
|
|
new ChartsActivity.ShowDurationDialog(detailedDuration, getContext()).show();
|
|
|
|
}
|
2020-11-06 14:28:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
heartRateWidgetLayout.setOnClickListener(listener);
|
|
|
|
intensityTotalWidgetLayout.setOnClickListener(listener);
|
|
|
|
intensityTotalIcon.setOnClickListener(listener);
|
|
|
|
intensityTotalLabel.setOnClickListener(listener);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
|
|
|
|
setupActivityChart();
|
|
|
|
setupSleepAmountChart();
|
2015-06-23 23:03:05 +02:00
|
|
|
|
2015-08-25 01:03:58 +02:00
|
|
|
// refresh immediately instead of use refreshIfVisible(), for perceived performance
|
2015-07-14 00:29:32 +02:00
|
|
|
refresh();
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2015-08-17 02:22:16 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if (action.equals(ChartsHost.REFRESH)) {
|
|
|
|
// TODO: use LimitLines to visualize smart alarms?
|
|
|
|
mSmartAlarmFrom = intent.getIntExtra("smartalarm_from", -1);
|
|
|
|
mSmartAlarmTo = intent.getIntExtra("smartalarm_to", -1);
|
|
|
|
mTimestampFrom = intent.getIntExtra("recording_base_timestamp", -1);
|
|
|
|
mSmartAlarmGoneOff = intent.getIntExtra("alarm_gone_off", -1);
|
|
|
|
refresh();
|
|
|
|
} else {
|
|
|
|
super.onReceive(context, intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
private void setupSleepAmountChart() {
|
|
|
|
mSleepAmountChart.setBackgroundColor(BACKGROUND_COLOR);
|
2016-09-18 23:37:41 +02:00
|
|
|
mSleepAmountChart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
2017-04-08 15:50:13 +02:00
|
|
|
mSleepAmountChart.setEntryLabelColor(DESCRIPTION_COLOR);
|
2016-09-18 23:37:41 +02:00
|
|
|
mSleepAmountChart.getDescription().setText("");
|
|
|
|
// mSleepAmountChart.getDescription().setNoDataTextDescription("");
|
2015-07-20 23:11:16 +02:00
|
|
|
mSleepAmountChart.setNoDataText("");
|
2016-04-11 23:18:12 +02:00
|
|
|
mSleepAmountChart.getLegend().setEnabled(false);
|
2015-07-14 00:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setupActivityChart() {
|
|
|
|
mActivityChart.setBackgroundColor(BACKGROUND_COLOR);
|
2016-09-18 23:37:41 +02:00
|
|
|
mActivityChart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
2015-07-14 00:29:32 +02:00
|
|
|
configureBarLineChartDefaults(mActivityChart);
|
|
|
|
|
|
|
|
XAxis x = mActivityChart.getXAxis();
|
2015-06-23 23:03:05 +02:00
|
|
|
x.setDrawLabels(true);
|
|
|
|
x.setDrawGridLines(false);
|
|
|
|
x.setEnabled(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
x.setTextColor(CHART_TEXT_COLOR);
|
2015-06-23 23:03:05 +02:00
|
|
|
x.setDrawLimitLinesBehindData(true);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
YAxis y = mActivityChart.getAxisLeft();
|
2015-06-23 23:03:05 +02:00
|
|
|
y.setDrawGridLines(false);
|
|
|
|
// y.setDrawLabels(false);
|
|
|
|
// TODO: make fixed max value optional
|
2016-10-04 00:09:56 +02:00
|
|
|
y.setAxisMaximum(1f);
|
|
|
|
y.setAxisMinimum(0);
|
2015-06-23 23:03:05 +02:00
|
|
|
y.setDrawTopYLabelEntry(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
y.setTextColor(CHART_TEXT_COLOR);
|
2015-06-23 23:03:05 +02:00
|
|
|
|
|
|
|
// y.setLabelCount(5);
|
|
|
|
y.setEnabled(true);
|
|
|
|
|
2015-07-14 00:29:32 +02:00
|
|
|
YAxis yAxisRight = mActivityChart.getAxisRight();
|
2015-06-23 23:03:05 +02:00
|
|
|
yAxisRight.setDrawGridLines(false);
|
2016-06-19 23:22:28 +02:00
|
|
|
yAxisRight.setEnabled(supportsHeartrate(getChartsHost().getDevice()));
|
2016-04-04 23:05:00 +02:00
|
|
|
yAxisRight.setDrawLabels(true);
|
|
|
|
yAxisRight.setDrawTopYLabelEntry(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
yAxisRight.setTextColor(CHART_TEXT_COLOR);
|
2020-10-31 23:44:06 +01:00
|
|
|
yAxisRight.setAxisMaximum(HeartRateUtils.getInstance().getMaxHeartRate());
|
|
|
|
yAxisRight.setAxisMinimum(HeartRateUtils.getInstance().getMinHeartRate());
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 23:37:41 +02:00
|
|
|
@Override
|
2015-07-14 00:29:32 +02:00
|
|
|
protected void setupLegend(Chart chart) {
|
2016-09-18 23:37:41 +02:00
|
|
|
List<LegendEntry> legendEntries = new ArrayList<>(3);
|
|
|
|
LegendEntry lightSleepEntry = new LegendEntry();
|
|
|
|
lightSleepEntry.label = akLightSleep.label;
|
|
|
|
lightSleepEntry.formColor = akLightSleep.color;
|
|
|
|
legendEntries.add(lightSleepEntry);
|
|
|
|
|
|
|
|
LegendEntry deepSleepEntry = new LegendEntry();
|
|
|
|
deepSleepEntry.label = akDeepSleep.label;
|
|
|
|
deepSleepEntry.formColor = akDeepSleep.color;
|
|
|
|
legendEntries.add(deepSleepEntry);
|
2020-11-01 23:58:54 +01:00
|
|
|
heartRateIcon.setVisibility(View.GONE); //hide heart icon
|
2020-11-11 20:59:59 +01:00
|
|
|
intensityTotalIcon.setVisibility(View.GONE); //hide intensity icon
|
2016-09-18 23:37:41 +02:00
|
|
|
|
2016-06-19 23:22:28 +02:00
|
|
|
if (supportsHeartrate(getChartsHost().getDevice())) {
|
2016-09-18 23:37:41 +02:00
|
|
|
LegendEntry hrEntry = new LegendEntry();
|
|
|
|
hrEntry.label = HEARTRATE_LABEL;
|
|
|
|
hrEntry.formColor = HEARTRATE_COLOR;
|
|
|
|
legendEntries.add(hrEntry);
|
2020-10-31 23:44:06 +01:00
|
|
|
if (!CHARTS_SLEEP_RANGE_24H && SHOW_CHARTS_AVERAGE) {
|
|
|
|
LegendEntry hrAverageEntry = new LegendEntry();
|
|
|
|
hrAverageEntry.label = HEARTRATE_AVERAGE_LABEL;
|
|
|
|
hrAverageEntry.formColor = Color.RED;
|
|
|
|
legendEntries.add(hrAverageEntry);
|
2020-11-01 23:58:54 +01:00
|
|
|
heartRateIcon.setVisibility(View.VISIBLE);
|
2020-11-11 20:59:59 +01:00
|
|
|
intensityTotalIcon.setVisibility(View.VISIBLE);
|
2020-10-31 23:44:06 +01:00
|
|
|
}
|
2016-04-04 23:05:00 +02:00
|
|
|
}
|
2016-09-18 23:37:41 +02:00
|
|
|
chart.getLegend().setCustom(legendEntries);
|
2015-07-14 00:29:32 +02:00
|
|
|
chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2015-06-23 23:03:05 +02:00
|
|
|
}
|
2015-07-13 21:54:46 +02:00
|
|
|
|
|
|
|
@Override
|
2016-05-16 23:36:54 +02:00
|
|
|
protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
2015-11-09 23:52:00 +01:00
|
|
|
// temporary fix for totally wrong sleep amounts
|
|
|
|
// return super.getSleepSamples(db, device, tsFrom, tsTo);
|
|
|
|
return super.getAllSamples(db, device, tsFrom, tsTo);
|
2015-08-03 01:17:02 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 23:37:41 +02:00
|
|
|
@Override
|
2015-08-03 01:17:02 +02:00
|
|
|
protected void renderCharts() {
|
2019-08-24 12:41:26 +02:00
|
|
|
mActivityChart.animateX(ANIM_TIME, Easing.EaseInOutQuart);
|
2015-08-03 01:17:02 +02:00
|
|
|
mSleepAmountChart.invalidate();
|
2015-07-13 21:54:46 +02:00
|
|
|
}
|
2016-04-11 23:18:12 +02:00
|
|
|
|
|
|
|
private static class MySleepChartsData extends ChartsData {
|
|
|
|
private String totalSleep;
|
|
|
|
private final PieData pieData;
|
2019-09-17 00:47:10 +02:00
|
|
|
private final List<SleepSession> sleepSessions;
|
2016-04-11 23:18:12 +02:00
|
|
|
|
2019-09-17 00:47:10 +02:00
|
|
|
public MySleepChartsData(String totalSleep, PieData pieData, List<SleepSession> sleepSessions) {
|
2016-04-11 23:18:12 +02:00
|
|
|
this.totalSleep = totalSleep;
|
|
|
|
this.pieData = pieData;
|
2019-09-17 00:47:10 +02:00
|
|
|
this.sleepSessions = sleepSessions;
|
2016-04-11 23:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public PieData getPieData() {
|
|
|
|
return pieData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CharSequence getTotalSleep() {
|
|
|
|
return totalSleep;
|
|
|
|
}
|
2018-08-16 16:59:56 +02:00
|
|
|
|
2019-09-17 00:47:10 +02:00
|
|
|
public List<SleepSession> getSleepSessions() {
|
|
|
|
return sleepSessions;
|
2018-08-16 16:59:56 +02:00
|
|
|
}
|
2016-04-11 23:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class MyChartsData extends ChartsData {
|
2017-11-01 17:34:46 +01:00
|
|
|
private final DefaultChartsData<LineData> chartsData;
|
2016-04-11 23:18:12 +02:00
|
|
|
private final MySleepChartsData pieData;
|
2020-10-31 23:44:06 +01:00
|
|
|
private final float heartRateAverage;
|
2020-11-01 23:58:54 +01:00
|
|
|
private int heartRateAxisMax;
|
|
|
|
private int heartRateAxisMin;
|
2020-10-31 23:44:06 +01:00
|
|
|
private float intensityAxisMax;
|
|
|
|
private float intensityAxisMin;
|
2020-11-06 14:28:51 +01:00
|
|
|
private float intensityTotal;
|
2016-04-11 23:18:12 +02:00
|
|
|
|
2020-11-06 14:28:51 +01:00
|
|
|
public MyChartsData(MySleepChartsData pieData, DefaultChartsData<LineData> chartsData, float heartRateAverage, int heartRateAxisMin, int heartRateAxisMax, float intensityTotal, float intensityAxisMin, float intensityAxisMax) {
|
2016-04-11 23:18:12 +02:00
|
|
|
this.pieData = pieData;
|
|
|
|
this.chartsData = chartsData;
|
2020-10-31 23:44:06 +01:00
|
|
|
this.heartRateAverage = heartRateAverage;
|
|
|
|
this.heartRateAxisMax = heartRateAxisMax;
|
|
|
|
this.heartRateAxisMin = heartRateAxisMin;
|
2020-11-06 14:28:51 +01:00
|
|
|
this.intensityTotal = intensityTotal;
|
|
|
|
this.intensityAxisMin = intensityAxisMin;
|
|
|
|
this.intensityAxisMax = intensityAxisMax;
|
2016-04-11 23:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public MySleepChartsData getPieData() {
|
|
|
|
return pieData;
|
|
|
|
}
|
|
|
|
|
2017-11-01 17:34:46 +01:00
|
|
|
public DefaultChartsData<LineData> getChartsData() {
|
2016-04-11 23:18:12 +02:00
|
|
|
return chartsData;
|
|
|
|
}
|
2020-10-31 23:44:06 +01:00
|
|
|
|
|
|
|
public float getHeartRateAverage() {
|
|
|
|
return heartRateAverage;
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:58:54 +01:00
|
|
|
public int getHeartRateAxisMax() {
|
2020-10-31 23:44:06 +01:00
|
|
|
return heartRateAxisMax;
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:58:54 +01:00
|
|
|
public int getHeartRateAxisMin() { return heartRateAxisMin; }
|
2020-10-31 23:44:06 +01:00
|
|
|
|
|
|
|
public float getIntensityAxisMax() {
|
|
|
|
return intensityAxisMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getIntensityAxisMin() {
|
|
|
|
return intensityAxisMin;
|
|
|
|
}
|
2020-11-06 14:28:51 +01:00
|
|
|
|
|
|
|
public float getIntensityTotal() { return intensityTotal;}
|
|
|
|
|
2016-04-11 23:18:12 +02:00
|
|
|
}
|
2015-08-03 01:17:02 +02:00
|
|
|
}
|