2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2019-02-13 20:43:30 +01:00
|
|
|
Gobbetti, Dikay900, Pavel Elagin
|
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-11 23:34:16 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2015-07-11 00:03:41 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2015-06-11 23:34:16 +02:00
|
|
|
|
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;
|
2018-08-15 17:03:32 +02:00
|
|
|
import com.github.mikephil.charting.components.Legend;
|
2016-09-18 23:37:41 +02:00
|
|
|
import com.github.mikephil.charting.components.LegendEntry;
|
2015-06-11 23:34:16 +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-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2016-04-12 23:12:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils;
|
2015-08-03 01:17:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2015-08-17 02:22:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-07-27 23:49:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
public class ActivitySleepChartFragment extends AbstractChartFragment {
|
|
|
|
protected static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2017-11-01 17:34:46 +01:00
|
|
|
private LineChart mChart;
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
private int mSmartAlarmFrom = -1;
|
|
|
|
private int mSmartAlarmTo = -1;
|
|
|
|
private int mTimestampFrom = -1;
|
|
|
|
private int mSmartAlarmGoneOff = -1;
|
|
|
|
|
|
|
|
@Override
|
2015-07-11 00:03:41 +02:00
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2017-11-01 17:34:46 +01:00
|
|
|
mChart = (LineChart) rootView.findViewById(R.id.activitysleepchart);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
setupChart();
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
return rootView;
|
|
|
|
}
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-08-25 00:33:37 +02:00
|
|
|
@Override
|
|
|
|
public String getTitle() {
|
|
|
|
return getString(R.string.activity_sleepchart_activity_and_sleep);
|
|
|
|
}
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
private void setupChart() {
|
|
|
|
mChart.setBackgroundColor(BACKGROUND_COLOR);
|
2016-09-18 23:37:41 +02:00
|
|
|
mChart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
2015-07-11 00:03:41 +02:00
|
|
|
configureBarLineChartDefaults(mChart);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
XAxis x = mChart.getXAxis();
|
|
|
|
x.setDrawLabels(true);
|
|
|
|
x.setDrawGridLines(false);
|
|
|
|
x.setEnabled(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
x.setTextColor(CHART_TEXT_COLOR);
|
2015-06-18 23:41:54 +02:00
|
|
|
x.setDrawLimitLinesBehindData(true);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
YAxis y = mChart.getAxisLeft();
|
|
|
|
y.setDrawGridLines(false);
|
2015-06-16 21:04:44 +02:00
|
|
|
// y.setDrawLabels(false);
|
2015-06-17 00:05:55 +02:00
|
|
|
// TODO: make fixed max value optional
|
2016-10-03 21:06:18 +02:00
|
|
|
y.setAxisMaximum(1f);
|
|
|
|
y.setAxisMinimum(0);
|
2015-06-11 23:34:16 +02:00
|
|
|
y.setDrawTopYLabelEntry(false);
|
2015-07-11 00:03:41 +02:00
|
|
|
y.setTextColor(CHART_TEXT_COLOR);
|
2015-06-11 23:34:16 +02:00
|
|
|
|
|
|
|
// y.setLabelCount(5);
|
|
|
|
y.setEnabled(true);
|
|
|
|
|
|
|
|
YAxis yAxisRight = mChart.getAxisRight();
|
|
|
|
yAxisRight.setDrawGridLines(false);
|
2016-06-19 23:22:28 +02:00
|
|
|
yAxisRight.setEnabled(supportsHeartrate(getChartsHost().getDevice()));
|
2016-02-27 11:18:16 +01:00
|
|
|
yAxisRight.setDrawLabels(true);
|
|
|
|
yAxisRight.setDrawTopYLabelEntry(true);
|
2015-07-11 00:03:41 +02:00
|
|
|
yAxisRight.setTextColor(CHART_TEXT_COLOR);
|
2018-08-16 13:13:12 +02:00
|
|
|
yAxisRight.setAxisMaximum(HeartRateUtils.getInstance().getMaxHeartRate());
|
|
|
|
yAxisRight.setAxisMinimum(HeartRateUtils.getInstance().getMinHeartRate());
|
2015-06-11 23:34:16 +02:00
|
|
|
|
2015-08-25 01:03:58 +02:00
|
|
|
// refresh immediately instead of use refreshIfVisible(), for perceived performance
|
2015-08-03 01:17:02 +02:00
|
|
|
refresh();
|
2015-06-11 23:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-17 02:22:16 +02:00
|
|
|
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-06-11 23:34:16 +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) {
|
2016-05-16 23:36:54 +02:00
|
|
|
List<? extends ActivitySample> samples = getSamples(db, device);
|
2016-04-11 23:18:12 +02:00
|
|
|
return refresh(device, samples);
|
|
|
|
}
|
2015-08-03 01:17:02 +02:00
|
|
|
|
2016-04-11 23:18:12 +02:00
|
|
|
@Override
|
|
|
|
protected void updateChartsnUIThread(ChartsData chartsData) {
|
|
|
|
DefaultChartsData dcd = (DefaultChartsData) chartsData;
|
2015-08-03 01:17:02 +02:00
|
|
|
mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2016-09-29 22:16:08 +02:00
|
|
|
mChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317
|
2016-10-03 21:06:18 +02:00
|
|
|
mChart.getXAxis().setValueFormatter(dcd.getXValueFormatter());
|
2017-11-01 17:34:46 +01:00
|
|
|
mChart.setData((LineData) dcd.getData());
|
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
|
|
|
mChart.animateX(ANIM_TIME, Easing.EaseInOutQuart);
|
2016-10-03 21:06:18 +02:00
|
|
|
// mChart.invalidate();
|
2015-08-03 01:17:02 +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<>(5);
|
|
|
|
|
|
|
|
LegendEntry activityEntry = new LegendEntry();
|
|
|
|
activityEntry.label = akActivity.label;
|
|
|
|
activityEntry.formColor = akActivity.color;
|
|
|
|
legendEntries.add(activityEntry);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
LegendEntry notWornEntry = new LegendEntry();
|
|
|
|
notWornEntry.label = akNotWorn.label;
|
|
|
|
notWornEntry.formColor = akNotWorn.color;
|
|
|
|
legendEntries.add(notWornEntry);
|
|
|
|
|
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);
|
2016-04-04 23:05:00 +02:00
|
|
|
}
|
2016-09-18 23:37:41 +02:00
|
|
|
chart.getLegend().setCustom(legendEntries);
|
2018-08-15 17:03:32 +02:00
|
|
|
chart.getLegend().setWordWrapEnabled(true);
|
|
|
|
chart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
|
2015-06-17 00:05:55 +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-08-03 01:17:02 +02:00
|
|
|
return getAllSamples(db, device, tsFrom, tsTo);
|
2015-07-13 21:54:46 +02:00
|
|
|
}
|
2015-06-11 23:34:16 +02:00
|
|
|
}
|