2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 0nse, Andreas Shimokawa, Carsten Pfeiffer,
|
2017-05-14 23:09:27 +02:00
|
|
|
Daniele Gobbetti, Vebryn
|
|
|
|
|
|
|
|
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/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import com.github.mikephil.charting.charts.Chart;
|
2017-05-15 22:19:50 +02:00
|
|
|
import com.github.mikephil.charting.charts.HorizontalBarChart;
|
2017-05-14 23:09:27 +02:00
|
|
|
import com.github.mikephil.charting.components.XAxis;
|
2017-05-18 23:26:04 +02:00
|
|
|
import com.github.mikephil.charting.components.YAxis;
|
2017-05-14 23:09:27 +02:00
|
|
|
import com.github.mikephil.charting.data.BarData;
|
|
|
|
import com.github.mikephil.charting.data.BarDataSet;
|
|
|
|
import com.github.mikephil.charting.data.BarEntry;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
2017-08-23 23:56:24 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2017-05-18 23:26:04 +02:00
|
|
|
|
2017-05-14 23:09:27 +02:00
|
|
|
|
2023-06-17 00:53:28 +02:00
|
|
|
public class SpeedZonesFragment extends AbstractActivityChartFragment<ChartsData> {
|
2017-08-26 23:55:11 +02:00
|
|
|
protected static final Logger LOG = LoggerFactory.getLogger(SpeedZonesFragment.class);
|
2017-05-14 23:09:27 +02:00
|
|
|
|
|
|
|
private HorizontalBarChart mStatsChart;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
|
|
|
|
List<? extends ActivitySample> samples = getSamples(db, device);
|
|
|
|
|
2017-05-18 23:26:04 +02:00
|
|
|
MySpeedZonesData mySpeedZonesData = refreshStats(samples);
|
2017-05-14 23:09:27 +02:00
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
return new MyChartsData(mySpeedZonesData);
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:26:04 +02:00
|
|
|
private MySpeedZonesData refreshStats(List<? extends ActivitySample> samples) {
|
2017-05-14 23:09:27 +02:00
|
|
|
ActivityAnalysis analysis = new ActivityAnalysis();
|
|
|
|
analysis.calculateActivityAmounts(samples);
|
|
|
|
BarData data = new BarData();
|
2017-05-18 23:26:04 +02:00
|
|
|
data.setValueTextColor(CHART_TEXT_COLOR);
|
2017-05-14 23:09:27 +02:00
|
|
|
List<BarEntry> entries = new ArrayList<>();
|
|
|
|
|
2017-08-23 23:56:24 +02:00
|
|
|
ActivityUser user = new ActivityUser();
|
|
|
|
/*double distanceFactorCm;
|
|
|
|
if (user.getGender() == user.GENDER_MALE){
|
|
|
|
distanceFactorCm = user.getHeightCm() * user.GENDER_MALE_DISTANCE_FACTOR / 1000;
|
|
|
|
} else {
|
|
|
|
distanceFactorCm = user.getHeightCm() * user.GENDER_FEMALE_DISTANCE_FACTOR / 1000;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
for (Map.Entry<Integer, Long> entry : analysis.stats.entrySet()) {
|
|
|
|
entries.add(new BarEntry(entry.getKey(), entry.getValue() / 60));
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BarDataSet set = new BarDataSet(entries, "");
|
2017-05-18 23:26:04 +02:00
|
|
|
set.setValueTextColor(CHART_TEXT_COLOR);
|
2017-05-14 23:09:27 +02:00
|
|
|
set.setColors(getColorFor(ActivityKind.TYPE_ACTIVITY));
|
|
|
|
//set.setDrawValues(false);
|
|
|
|
//data.setBarWidth(0.1f);
|
|
|
|
data.addDataSet(set);
|
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
return new MySpeedZonesData(data);
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void updateChartsnUIThread(ChartsData chartsData) {
|
|
|
|
MyChartsData mcd = (MyChartsData) chartsData;
|
2017-05-15 22:19:50 +02:00
|
|
|
mStatsChart.setData(mcd.getChartsData().getBarData());
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getTitle() {
|
|
|
|
return getString(R.string.stats_title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_statschart, container, false);
|
|
|
|
|
|
|
|
mStatsChart = (HorizontalBarChart) rootView.findViewById(R.id.statschart);
|
|
|
|
setupStatsChart();
|
|
|
|
|
|
|
|
// refresh immediately instead of use refreshIfVisible(), for perceived performance
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupStatsChart() {
|
|
|
|
mStatsChart.setBackgroundColor(BACKGROUND_COLOR);
|
|
|
|
mStatsChart.getDescription().setTextColor(DESCRIPTION_COLOR);
|
|
|
|
mStatsChart.setNoDataText("");
|
|
|
|
mStatsChart.getLegend().setEnabled(false);
|
|
|
|
mStatsChart.setTouchEnabled(false);
|
|
|
|
mStatsChart.getDescription().setText("");
|
2017-05-18 23:26:04 +02:00
|
|
|
|
2017-08-27 17:26:24 +02:00
|
|
|
XAxis right = mStatsChart.getXAxis(); //believe it or not, the X axis is vertical for HorizontalBarChart
|
|
|
|
right.setTextColor(CHART_TEXT_COLOR);
|
2017-05-18 23:26:04 +02:00
|
|
|
|
2017-08-27 17:26:24 +02:00
|
|
|
YAxis bottom = mStatsChart.getAxisRight();
|
|
|
|
bottom.setTextColor(CHART_TEXT_COLOR);
|
|
|
|
bottom.setGranularity(1f);
|
|
|
|
|
|
|
|
YAxis top = mStatsChart.getAxisLeft();
|
|
|
|
top.setTextColor(CHART_TEXT_COLOR);
|
|
|
|
top.setGranularity(1f);
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-27 00:04:08 +02:00
|
|
|
protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
|
|
|
|
return super.getAllSamples(db, device, tsFrom, tsTo);
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-06-17 00:53:28 +02:00
|
|
|
protected void setupLegend(Chart<?> chart) {
|
2017-08-27 00:04:08 +02:00
|
|
|
// no legend here, it is all about the steps here
|
|
|
|
chart.getLegend().setEnabled(false);
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void renderCharts() {
|
|
|
|
mStatsChart.invalidate();
|
|
|
|
}
|
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
private static class MySpeedZonesData extends ChartsData {
|
|
|
|
private final BarData barData;
|
2017-05-14 23:09:27 +02:00
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
MySpeedZonesData(BarData barData) {
|
|
|
|
this.barData = barData;
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
BarData getBarData() {
|
|
|
|
return barData;
|
2017-05-14 23:09:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class MyChartsData extends ChartsData {
|
2017-05-15 22:19:50 +02:00
|
|
|
private final MySpeedZonesData chartsData;
|
2017-05-14 23:09:27 +02:00
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
MyChartsData(MySpeedZonesData chartsData) {
|
2017-05-14 23:09:27 +02:00
|
|
|
this.chartsData = chartsData;
|
|
|
|
}
|
|
|
|
|
2017-05-15 22:19:50 +02:00
|
|
|
MySpeedZonesData getChartsData() {
|
2017-05-14 23:09:27 +02:00
|
|
|
return chartsData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|