diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesChartFragment.java index 8bad0a76d..a357cf45f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesChartFragment.java @@ -44,6 +44,7 @@ import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractChartFragment; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsData; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost; +import nodomain.freeyourgadget.gadgetbridge.activities.charts.DefaultChartsData; import nodomain.freeyourgadget.gadgetbridge.database.DBAccess; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BatteryInfoChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BatteryInfoChartFragment.java index e41a0f42d..e367629c4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BatteryInfoChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BatteryInfoChartFragment.java @@ -52,7 +52,8 @@ import java.util.List; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractChartFragment; +import nodomain.freeyourgadget.gadgetbridge.activities.charts.DefaultChartsData; +import nodomain.freeyourgadget.gadgetbridge.activities.charts.TimestampTranslation; import nodomain.freeyourgadget.gadgetbridge.database.DBAccess; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; @@ -94,7 +95,7 @@ public class BatteryInfoChartFragment extends AbstractGBFragment { } private DefaultBatteryChartsData fill_dcd(List samples) { - AbstractChartFragment.TimestampTranslation tsTranslation = new AbstractChartFragment.TimestampTranslation(); + TimestampTranslation tsTranslation = new TimestampTranslation(); List entries = new ArrayList(); int firstTs = 0; @@ -192,11 +193,11 @@ public class BatteryInfoChartFragment extends AbstractGBFragment { } protected static class customFormatter extends ValueFormatter { - private final AbstractChartFragment.TimestampTranslation tsTranslation; + private final TimestampTranslation tsTranslation; SimpleDateFormat annotationDateFormat = new SimpleDateFormat("dd.MM HH:mm"); Calendar cal = GregorianCalendar.getInstance(); - public customFormatter(AbstractChartFragment.TimestampTranslation tsTranslation) { + public customFormatter(TimestampTranslation tsTranslation) { this.tsTranslation = tsTranslation; } @@ -239,7 +240,7 @@ public class BatteryInfoChartFragment extends AbstractGBFragment { } } - private class DefaultBatteryChartsData extends AbstractChartFragment.DefaultChartsData { + private class DefaultBatteryChartsData extends DefaultChartsData { public int firstTs; public DefaultBatteryChartsData(ChartData data, ValueFormatter xValueFormatter, int ts) { @@ -259,7 +260,7 @@ public class BatteryInfoChartFragment extends AbstractGBFragment { public batteryValuesAndDateMarker(Context context, int layoutResource, int ts) { super(context, layoutResource); - AbstractChartFragment.TimestampTranslation tsTranslation = new AbstractChartFragment.TimestampTranslation(); + TimestampTranslation tsTranslation = new TimestampTranslation(); formatter = new customFormatter(tsTranslation); top_text = (TextView) findViewById(R.id.chart_marker_item_top); bottom_text = (TextView) findViewById(R.id.chart_marker_item_bottom); 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 d4cdd06be..ed211e41e 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 @@ -35,7 +35,6 @@ import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.BarLineChartBase; import com.github.mikephil.charting.charts.Chart; import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.ChartData; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; @@ -45,7 +44,6 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -826,88 +824,4 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { private int toTimestamp(Date date) { return (int) ((date.getTime() / 1000)); } - - public static class DefaultChartsData> extends ChartsData { - private final T data; - private ValueFormatter xValueFormatter; - - public DefaultChartsData(T data, ValueFormatter xValueFormatter) { - this.xValueFormatter = xValueFormatter; - this.data = data; - } - - public ValueFormatter getXValueFormatter() { - return xValueFormatter; - } - - public T getData() { - return data; - } - } - - protected static class SampleXLabelFormatter extends ValueFormatter { - private final TimestampTranslation tsTranslation; - SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm"); -// SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); - Calendar cal = GregorianCalendar.getInstance(); - - public SampleXLabelFormatter(TimestampTranslation tsTranslation) { - this.tsTranslation = tsTranslation; - - } - // TODO: this does not work. Cannot use precomputed labels - @Override - public String getFormattedValue(float value) { - cal.clear(); - int ts = (int) value; - cal.setTimeInMillis(tsTranslation.toOriginalValue(ts) * 1000L); - Date date = cal.getTime(); - String dateString = annotationDateFormat.format(date); - return dateString; - } - } - - protected static class PreformattedXIndexLabelFormatter extends ValueFormatter { - private ArrayList xLabels; - - public PreformattedXIndexLabelFormatter(ArrayList xLabels) { - this.xLabels = xLabels; - - } - @Override - public String getFormattedValue(float value) { - int index = (int) value; - if (xLabels == null || index >= xLabels.size()) { - return String.valueOf(value); - } - return xLabels.get(index); - } - } - - /** - * Awkward class that helps in translating long timestamp - * values to float (sic!) values. It basically rebases all - * timestamps to a base (the very first) timestamp value. - * - * It does this so that the large timestamp values can be used - * floating point values, where the mantissa is just 24 bits. - */ - public static class TimestampTranslation { - private int tsOffset = -1; - - public int shorten(int timestamp) { - if (tsOffset == -1) { - tsOffset = timestamp; - return 0; - } - return timestamp - tsOffset; - } - - public int toOriginalValue(int timestamp) { - if (tsOffset == -1) { - return timestamp; - } - return timestamp + tsOffset; - } - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/DefaultChartsData.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/DefaultChartsData.java new file mode 100644 index 000000000..e73c11466 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/DefaultChartsData.java @@ -0,0 +1,39 @@ +/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer, + Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly + + 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 . */ +package nodomain.freeyourgadget.gadgetbridge.activities.charts; + +import com.github.mikephil.charting.data.ChartData; +import com.github.mikephil.charting.formatter.ValueFormatter; + +public class DefaultChartsData> extends ChartsData { + private final T data; + private final ValueFormatter xValueFormatter; + + public DefaultChartsData(final T data, final ValueFormatter xValueFormatter) { + this.xValueFormatter = xValueFormatter; + this.data = data; + } + + public ValueFormatter getXValueFormatter() { + return xValueFormatter; + } + + public T getData() { + return data; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/PreformattedXIndexLabelFormatter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/PreformattedXIndexLabelFormatter.java new file mode 100644 index 000000000..2d013208a --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/PreformattedXIndexLabelFormatter.java @@ -0,0 +1,39 @@ +/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer, + Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly + + 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 . */ +package nodomain.freeyourgadget.gadgetbridge.activities.charts; + +import com.github.mikephil.charting.formatter.ValueFormatter; + +import java.util.ArrayList; + +class PreformattedXIndexLabelFormatter extends ValueFormatter { + private final ArrayList xLabels; + + public PreformattedXIndexLabelFormatter(ArrayList xLabels) { + this.xLabels = xLabels; + } + + @Override + public String getFormattedValue(float value) { + int index = (int) value; + if (xLabels == null || index >= xLabels.size()) { + return String.valueOf(value); + } + return xLabels.get(index); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SampleXLabelFormatter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SampleXLabelFormatter.java new file mode 100644 index 000000000..801e6f9a5 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SampleXLabelFormatter.java @@ -0,0 +1,49 @@ +/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer, + Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly + + 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 . */ +package nodomain.freeyourgadget.gadgetbridge.activities.charts; + +import android.annotation.SuppressLint; + +import com.github.mikephil.charting.formatter.ValueFormatter; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +class SampleXLabelFormatter extends ValueFormatter { + private final TimestampTranslation tsTranslation; + @SuppressLint("SimpleDateFormat") + private final SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm"); + // SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + private final Calendar cal = GregorianCalendar.getInstance(); + + public SampleXLabelFormatter(final TimestampTranslation tsTranslation) { + this.tsTranslation = tsTranslation; + } + + // TODO: this does not work. Cannot use precomputed labels + @Override + public String getFormattedValue(final float value) { + cal.clear(); + final int ts = (int) value; + cal.setTimeInMillis(tsTranslation.toOriginalValue(ts) * 1000L); + final Date date = cal.getTime(); + return annotationDateFormat.format(date); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TimestampTranslation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TimestampTranslation.java new file mode 100644 index 000000000..6a9e51605 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/TimestampTranslation.java @@ -0,0 +1,45 @@ +/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer, + Daniele Gobbetti, Dikay900, Pavel Elagin, vanous, walkjivefly + + 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 . */ +package nodomain.freeyourgadget.gadgetbridge.activities.charts; + +/** + * Awkward class that helps in translating long timestamp + * values to float (sic!) values. It basically rebases all + * timestamps to a base (the very first) timestamp value. + *

+ * It does this so that the large timestamp values can be used + * floating point values, where the mantissa is just 24 bits. + */ +public class TimestampTranslation { + private int tsOffset = -1; + + public int shorten(int timestamp) { + if (tsOffset == -1) { + tsOffset = timestamp; + return 0; + } + return timestamp - tsOffset; + } + + public int toOriginalValue(int timestamp) { + if (tsOffset == -1) { + return timestamp; + } + return timestamp + tsOffset; + } +}