2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2017-2020 Andreas Shimokawa, Carsten Pfeiffer, Pavel
|
2019-11-23 21:52:46 +01:00
|
|
|
Elagin, 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/>. */
|
2017-02-21 16:20:42 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
|
|
|
|
2017-03-31 23:42:25 +02:00
|
|
|
import com.github.mikephil.charting.charts.Chart;
|
2018-08-15 17:03:32 +02:00
|
|
|
import com.github.mikephil.charting.components.Legend;
|
2017-03-31 23:42:25 +02:00
|
|
|
import com.github.mikephil.charting.components.LegendEntry;
|
2019-08-24 12:41:26 +02:00
|
|
|
import com.github.mikephil.charting.formatter.ValueFormatter;
|
2017-02-23 08:49:55 +01:00
|
|
|
|
2022-08-24 12:21:59 +02:00
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
|
2017-03-31 23:42:25 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-02-23 08:49:55 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-02-21 16:20:42 +01:00
|
|
|
|
2017-03-11 11:29:50 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2017-02-21 16:20:42 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmount;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
2017-03-11 11:29:50 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2017-02-23 08:49:55 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
2017-02-21 16:20:42 +01:00
|
|
|
|
|
|
|
public class WeekSleepChartFragment extends AbstractWeekChartFragment {
|
|
|
|
@Override
|
|
|
|
public String getTitle() {
|
2019-08-12 23:08:17 +02:00
|
|
|
if (GBApplication.getPrefs().getBoolean("charts_range", true)) {
|
|
|
|
return getString(R.string.weeksleepchart_sleep_a_month);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return getString(R.string.weeksleepchart_sleep_a_week);
|
|
|
|
}
|
2017-02-21 16:20:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 14:21:59 +01:00
|
|
|
@Override
|
|
|
|
String getPieDescription(int targetValue) {
|
2017-04-08 23:16:33 +02:00
|
|
|
return getString(R.string.weeksleepchart_today_sleep_description, DateTimeUtils.formatDurationHoursMinutes(targetValue, TimeUnit.MINUTES));
|
2017-03-03 14:21:59 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 16:20:42 +01:00
|
|
|
@Override
|
|
|
|
int getGoal() {
|
2017-03-11 11:29:50 +01:00
|
|
|
return GBApplication.getPrefs().getInt(ActivityUser.PREF_USER_SLEEP_DURATION, 8) * 60;
|
2017-02-21 16:20:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:33:00 +01:00
|
|
|
@Override
|
|
|
|
int getOffsetHours() {
|
|
|
|
return -12;
|
|
|
|
}
|
|
|
|
|
2018-09-10 23:38:49 +02:00
|
|
|
|
|
|
|
@Override
|
2018-09-16 13:37:08 +02:00
|
|
|
protected long calculateBalance(ActivityAmounts activityAmounts) {
|
2018-09-10 23:38:49 +02:00
|
|
|
long balance = 0;
|
|
|
|
|
|
|
|
for (ActivityAmount amount : activityAmounts.getAmounts()) {
|
2022-08-24 12:21:59 +02:00
|
|
|
if (amount.getActivityKind() == ActivityKind.TYPE_DEEP_SLEEP ||
|
|
|
|
amount.getActivityKind() == ActivityKind.TYPE_LIGHT_SLEEP ||
|
|
|
|
amount.getActivityKind() == ActivityKind.TYPE_REM_SLEEP) {
|
2018-09-10 23:38:49 +02:00
|
|
|
balance += amount.getTotalSeconds();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (int) (balance / 60);
|
|
|
|
}
|
|
|
|
|
2018-09-06 15:07:48 +02:00
|
|
|
@Override
|
2018-09-16 13:37:08 +02:00
|
|
|
protected String getBalanceMessage(long balance, int targetValue) {
|
2018-09-06 15:07:48 +02:00
|
|
|
if (balance > 0) {
|
2020-04-09 18:22:57 +02:00
|
|
|
final long totalBalance = balance - ((long)targetValue * TOTAL_DAYS_FOR_AVERAGE);
|
2018-09-06 15:07:48 +02:00
|
|
|
if (totalBalance > 0)
|
2018-09-16 13:37:08 +02:00
|
|
|
return getString(R.string.overslept, getHM(totalBalance));
|
2018-09-06 15:07:48 +02:00
|
|
|
else
|
2018-09-16 13:37:08 +02:00
|
|
|
return getString(R.string.lack_of_sleep, getHM(Math.abs(totalBalance)));
|
2018-09-06 15:07:48 +02:00
|
|
|
} else
|
|
|
|
return getString(R.string.no_data);
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:20:42 +01:00
|
|
|
@Override
|
2017-02-26 21:41:27 +01:00
|
|
|
float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts) {
|
|
|
|
long totalSecondsDeepSleep = 0;
|
|
|
|
long totalSecondsLightSleep = 0;
|
2022-08-24 12:21:59 +02:00
|
|
|
long totalSecondsRemSleep = 0;
|
2017-02-26 00:40:50 +01:00
|
|
|
for (ActivityAmount amount : activityAmounts.getAmounts()) {
|
2017-02-26 21:41:27 +01:00
|
|
|
if (amount.getActivityKind() == ActivityKind.TYPE_DEEP_SLEEP) {
|
|
|
|
totalSecondsDeepSleep += amount.getTotalSeconds();
|
|
|
|
} else if (amount.getActivityKind() == ActivityKind.TYPE_LIGHT_SLEEP) {
|
|
|
|
totalSecondsLightSleep += amount.getTotalSeconds();
|
2022-08-24 12:21:59 +02:00
|
|
|
} else if (amount.getActivityKind() == ActivityKind.TYPE_REM_SLEEP) {
|
|
|
|
totalSecondsRemSleep += amount.getTotalSeconds();
|
2017-02-21 16:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 15:07:48 +02:00
|
|
|
int totalMinutesDeepSleep = (int) (totalSecondsDeepSleep / 60);
|
|
|
|
int totalMinutesLightSleep = (int) (totalSecondsLightSleep / 60);
|
2022-08-24 12:21:59 +02:00
|
|
|
int totalMinutesRemSleep = (int) (totalSecondsRemSleep / 60);
|
|
|
|
if (supportsRemSleep(getChartsHost().getDevice())) {
|
|
|
|
return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep, totalMinutesRemSleep};
|
|
|
|
} else {
|
|
|
|
return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep};
|
|
|
|
}
|
2017-02-21 16:20:42 +01:00
|
|
|
}
|
2017-02-23 08:49:55 +01:00
|
|
|
|
2017-02-23 21:15:57 +01:00
|
|
|
@Override
|
2018-09-16 13:37:08 +02:00
|
|
|
protected String formatPieValue(long value) {
|
|
|
|
return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES);
|
2017-02-23 21:15:57 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 23:42:25 +02:00
|
|
|
@Override
|
|
|
|
String[] getPieLabels() {
|
2022-08-24 12:21:59 +02:00
|
|
|
String[] labels = {
|
|
|
|
getString(R.string.abstract_chart_fragment_kind_deep_sleep),
|
|
|
|
getString(R.string.abstract_chart_fragment_kind_light_sleep)
|
|
|
|
};
|
|
|
|
if (supportsRemSleep(getChartsHost().getDevice())) {
|
|
|
|
labels = ArrayUtils.add(labels, getString(R.string.abstract_chart_fragment_kind_rem_sleep));
|
|
|
|
}
|
|
|
|
return labels;
|
2017-03-31 23:42:25 +02:00
|
|
|
}
|
|
|
|
|
2017-02-23 08:49:55 +01:00
|
|
|
@Override
|
2019-08-24 12:41:26 +02:00
|
|
|
ValueFormatter getPieValueFormatter() {
|
|
|
|
return new ValueFormatter() {
|
2017-02-23 08:49:55 +01:00
|
|
|
@Override
|
2019-08-24 12:55:33 +02:00
|
|
|
public String getFormattedValue(float value) {
|
2018-09-16 13:37:08 +02:00
|
|
|
return formatPieValue((long) value);
|
2017-02-23 08:49:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-03 14:21:59 +01:00
|
|
|
@Override
|
2019-08-24 12:41:26 +02:00
|
|
|
ValueFormatter getBarValueFormatter() {
|
|
|
|
return new ValueFormatter() {
|
2017-03-03 14:21:59 +01:00
|
|
|
@Override
|
2019-08-24 12:55:33 +02:00
|
|
|
public String getFormattedValue(float value) {
|
2017-03-03 14:21:59 +01:00
|
|
|
return DateTimeUtils.minutesToHHMM((int) value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-08-24 12:41:26 +02:00
|
|
|
ValueFormatter getYAxisFormatter() {
|
|
|
|
return new ValueFormatter() {
|
2017-03-03 14:21:59 +01:00
|
|
|
@Override
|
2019-08-24 12:55:33 +02:00
|
|
|
public String getFormattedValue(float value) {
|
2017-03-03 14:21:59 +01:00
|
|
|
return DateTimeUtils.minutesToHHMM((int) value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-23 08:49:55 +01:00
|
|
|
@Override
|
2017-02-26 21:41:27 +01:00
|
|
|
int[] getColors() {
|
2022-08-24 12:21:59 +02:00
|
|
|
int[] colors = {akDeepSleep.color, akLightSleep.color};
|
|
|
|
if (supportsRemSleep(getChartsHost().getDevice())) {
|
|
|
|
colors = ArrayUtils.add(colors, akRemSleep.color);
|
|
|
|
}
|
|
|
|
return colors;
|
2017-02-23 08:49:55 +01:00
|
|
|
}
|
2017-03-31 23:42:25 +02:00
|
|
|
|
|
|
|
@Override
|
2023-06-17 00:53:28 +02:00
|
|
|
protected void setupLegend(Chart<?> chart) {
|
2017-03-31 23:42:25 +02:00
|
|
|
List<LegendEntry> legendEntries = new ArrayList<>(2);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-08-24 12:21:59 +02:00
|
|
|
if (supportsRemSleep(getChartsHost().getDevice())) {
|
|
|
|
LegendEntry remSleepEntry = new LegendEntry();
|
|
|
|
remSleepEntry.label = akRemSleep.label;
|
|
|
|
remSleepEntry.formColor = akRemSleep.color;
|
|
|
|
legendEntries.add(remSleepEntry);
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:42:25 +02:00
|
|
|
chart.getLegend().setCustom(legendEntries);
|
|
|
|
chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
|
2018-08-15 17:03:32 +02:00
|
|
|
chart.getLegend().setWordWrapEnabled(true);
|
|
|
|
chart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
|
2017-03-31 23:42:25 +02:00
|
|
|
}
|
2018-09-06 15:07:48 +02:00
|
|
|
|
2018-09-16 13:37:08 +02:00
|
|
|
private String getHM(long value) {
|
2018-09-10 23:38:49 +02:00
|
|
|
return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES);
|
2018-09-06 15:07:48 +02:00
|
|
|
}
|
2019-08-11 12:00:00 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
String getAverage(float value) {
|
|
|
|
return getHM((long)value);
|
|
|
|
}
|
|
|
|
|
2017-02-21 16:20:42 +01:00
|
|
|
}
|