wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*  Copyright (C) 2017-2024 Andreas Shimokawa, Daniele Gobbetti, José Rebelo
 
    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 <https://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;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
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;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
 
 
public class SpeedZonesFragment extends AbstractActivityChartFragment<ChartsData> {
    protected static final Logger LOG = LoggerFactory.getLogger(SpeedZonesFragment.class);
 
    private HorizontalBarChart mStatsChart;
 
    @Override
    protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
        List<? extends ActivitySample> samples = getSamples(db, device);
 
        MySpeedZonesData mySpeedZonesData = refreshStats(samples);
 
        return new MyChartsData(mySpeedZonesData);
    }
 
    private MySpeedZonesData refreshStats(List<? extends ActivitySample> samples) {
        ActivityAnalysis analysis = new ActivityAnalysis();
        analysis.calculateActivityAmounts(samples);
        BarData data = new BarData();
        data.setValueTextColor(CHART_TEXT_COLOR);
        List<BarEntry> entries = new ArrayList<>();
 
        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));
        }
 
        BarDataSet set = new BarDataSet(entries, "");
        set.setValueTextColor(CHART_TEXT_COLOR);
        set.setColors(getColorFor(ActivityKind.ACTIVITY));
        //set.setDrawValues(false);
        //data.setBarWidth(0.1f);
        data.addDataSet(set);
 
        return new MySpeedZonesData(data);
    }
 
    @Override
    protected void updateChartsnUIThread(ChartsData chartsData) {
        MyChartsData mcd = (MyChartsData) chartsData;
        mStatsChart.setData(mcd.getChartsData().getBarData());
    }
 
    @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("");
 
        XAxis right = mStatsChart.getXAxis(); //believe it or not, the X axis is vertical for HorizontalBarChart
        right.setTextColor(CHART_TEXT_COLOR);
 
        YAxis bottom = mStatsChart.getAxisRight();
        bottom.setTextColor(CHART_TEXT_COLOR);
        bottom.setGranularity(1f);
 
        YAxis top = mStatsChart.getAxisLeft();
        top.setTextColor(CHART_TEXT_COLOR);
        top.setGranularity(1f);
    }
 
    @Override
    protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
        return super.getAllSamples(db, device, tsFrom, tsTo);
    }
 
    @Override
    protected void setupLegend(Chart<?> chart) {
        // no legend here, it is all about the steps here
        chart.getLegend().setEnabled(false);
    }
 
    @Override
    protected void renderCharts() {
        mStatsChart.invalidate();
    }
 
    private static class MySpeedZonesData extends ChartsData {
        private final BarData barData;
 
        MySpeedZonesData(BarData barData) {
            this.barData = barData;
        }
 
        BarData getBarData() {
            return barData;
        }
    }
 
    private static class MyChartsData extends ChartsData {
        private final MySpeedZonesData chartsData;
 
        MyChartsData(MySpeedZonesData chartsData) {
            this.chartsData = chartsData;
        }
 
        MySpeedZonesData getChartsData() {
            return chartsData;
        }
    }
}