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
175
176
177
178
179
/*  Copyright (C) 2020-2024 José Rebelo, Petr Vaněk
 
    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;
 
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
 
import androidx.annotation.Nullable;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint;
import nodomain.freeyourgadget.gadgetbridge.model.GPSCoordinate;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.FitFile;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordData;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.messages.FitRecord;
import nodomain.freeyourgadget.gadgetbridge.util.gpx.GpxParseException;
import nodomain.freeyourgadget.gadgetbridge.util.gpx.GpxParser;
 
import static android.graphics.Bitmap.createBitmap;
 
 
public class ActivitySummariesGpsFragment extends AbstractGBFragment {
    private static final Logger LOG = LoggerFactory.getLogger(ActivitySummariesGpsFragment.class);
    private ImageView gpsView;
    private final int CANVAS_SIZE = 360;
    private File inputFile;
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_gps, container, false);
        gpsView = rootView.findViewById(R.id.activitygpsview);
        if (inputFile != null) {
            processInBackgroundThread();
        }
        return rootView;
    }
 
    public void set_data(File inputFile) {
        this.inputFile = inputFile;
        if (gpsView != null) { //first fragment inflate is AFTER this is called
            processInBackgroundThread();
        }
    }
 
    private void processInBackgroundThread() {
        final Canvas canvas = createCanvas(gpsView);
        new Thread(() -> {
            final List<GPSCoordinate> points = getActivityPoints(inputFile)
                    .stream()
                    .map(ActivityPoint::getLocation)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
 
            if (!points.isEmpty()) {
                drawTrack(canvas, points);
            }
        }).start();
    }
 
    public static List<ActivityPoint> getActivityPoints(final File trackFile) {
        final List<ActivityPoint> points = new ArrayList<>();
        if (trackFile.getName().endsWith(".gpx")) {
            try (FileInputStream inputStream = new FileInputStream(trackFile)) {
                final GpxParser gpxParser = new GpxParser(inputStream);
                points.addAll(gpxParser.getGpxFile().getActivityPoints());
            } catch (final IOException e) {
                LOG.error("Failed to open {}", trackFile, e);
            } catch (final GpxParseException e) {
                LOG.error("Failed to parse gpx file", e);
            }
        } else if (trackFile.getName().endsWith(".fit")) {
            try {
                FitFile fitFile = FitFile.parseIncoming(trackFile);
                for (final RecordData record : fitFile.getRecords()) {
                    if (record instanceof FitRecord) {
                        points.add(((FitRecord) record).toActivityPoint());
                    }
                }
            } catch (final IOException e) {
                LOG.error("Failed to open {}", trackFile, e);
            } catch (final Exception e) {
                LOG.error("Failed to parse fit file", e);
            }
        } else {
            LOG.warn("Unknown file type {}", trackFile.getName());
        }
 
        return points;
    }
 
    private void drawTrack(Canvas canvas, List<? extends GPSCoordinate> trackPoints) {
        double maxLat = (Collections.max(trackPoints, new GPSCoordinate.compareLatitude())).getLatitude();
        double minLat = (Collections.min(trackPoints, new GPSCoordinate.compareLatitude())).getLatitude();
        double maxLon = (Collections.max(trackPoints, new GPSCoordinate.compareLongitude())).getLongitude();
        double minLon = (Collections.min(trackPoints, new GPSCoordinate.compareLongitude())).getLongitude();
        double maxAlt = (Collections.max(trackPoints, new GPSCoordinate.compareElevation())).getAltitude();
        double minAlt = (Collections.min(trackPoints, new GPSCoordinate.compareElevation())).getAltitude();
        float scale_factor_w = (float) ((maxLon - minLon) / (maxLat - minLat));
        float scale_factor_h = (float) ((maxLat - minLat) / (maxLon - minLon));
 
        if (scale_factor_h > scale_factor_w) { //scaling to draw proportionally
            scale_factor_h = 1;
        } else {
            scale_factor_w = 1;
        }
 
 
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(1);
        paint.setColor(getResources().getColor(R.color.chart_activity_light));
 
        for (GPSCoordinate p : trackPoints) {
            float lat = (float) ((p.getLatitude() - minLat) / (maxLat - minLat));
            float lon = (float) ((p.getLongitude() - minLon) / (maxLon - minLon));
            float alt = (float) ((p.getAltitude() - minAlt) / (maxAlt - minAlt));
            paint.setStrokeWidth(1 + alt); //make thicker with higher altitude, we could do more here
            canvas.drawPoint(CANVAS_SIZE * lon * scale_factor_w, CANVAS_SIZE * lat * scale_factor_h, paint);
        }
    }
 
    private Canvas createCanvas(ImageView imageView) {
        Bitmap bitmap = createBitmap(CANVAS_SIZE, CANVAS_SIZE, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(GBApplication.getWindowBackgroundColor(requireActivity()));
        //frame around, but it doesn't look so nice
        /*
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(0);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(getResources().getColor(R.color.chart_activity_light));
        canvas.drawRect(0,0,360,360,paint);
         */
        imageView.setImageBitmap(bitmap);
        imageView.setScaleY(-1f); //flip the canvas
 
        return canvas;
    }
 
    @Nullable
    @Override
    protected CharSequence getTitle() {
        return null;
    }
 
}