From a25d8eae3042d5e0d6c169b5cdb4ad8aa515df91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Fri, 3 May 2024 20:57:47 +0100 Subject: [PATCH] Activity Summary: Draw fit file if available --- .../ActivitySummariesGpsFragment.java | 52 ++++++++++++++----- .../activities/ActivitySummaryDetail.java | 27 ++++++---- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesGpsFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesGpsFragment.java index 10a1861fe..25fa617fe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesGpsFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummariesGpsFragment.java @@ -34,12 +34,18 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; 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.FitImporter; +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 nodomain.freeyourgadget.gadgetbridge.util.gpx.model.GpxFile; @@ -76,21 +82,43 @@ public class ActivitySummariesGpsFragment extends AbstractGBFragment { new Thread(new Runnable() { @Override public void run() { - final GpxFile gpxFile; - - try (FileInputStream inputStream = new FileInputStream(inputFile)) { - final GpxParser gpxParser = new GpxParser(inputStream); - gpxFile = gpxParser.getGpxFile(); - } catch (final IOException e) { - LOG.error("Failed to open {}", inputFile, e); - return; - } catch (final GpxParseException e) { - LOG.error("Failed to parse gpx file", e); + final List points = new ArrayList<>(); + if (inputFile.getName().endsWith(".gpx")) { + try (FileInputStream inputStream = new FileInputStream(inputFile)) { + final GpxParser gpxParser = new GpxParser(inputStream); + points.addAll(gpxParser.getGpxFile().getPoints()); + } catch (final IOException e) { + LOG.error("Failed to open {}", inputFile, e); + return; + } catch (final GpxParseException e) { + LOG.error("Failed to parse gpx file", e); + return; + } + } else if (inputFile.getName().endsWith(".fit")) { + try { + FitFile fitFile = FitFile.parseIncoming(inputFile); + for (final RecordData record : fitFile.getRecords()) { + if (record instanceof FitRecord) { + final ActivityPoint activityPoint = ((FitRecord) record).toActivityPoint(); + if (activityPoint.getLocation() != null) { + points.add(activityPoint.getLocation()); + } + } + } + } catch (final IOException e) { + LOG.error("Failed to open {}", inputFile, e); + return; + } catch (final Exception e) { + LOG.error("Failed to parse fit file", e); + return; + } + } else { + LOG.warn("Unknown file type {}", inputFile.getName()); return; } - if (!gpxFile.getPoints().isEmpty()) { - drawTrack(canvas, gpxFile.getPoints()); + if (!points.isEmpty()) { + drawTrack(canvas, points); } } }).start(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java index 9a6b372be..e50185a13 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java @@ -184,9 +184,9 @@ public class ActivitySummaryDetail extends AbstractGBActivity { makeSummaryHeader(newItem); makeSummaryContent(newItem); activitySummariesChartFragment.setDateAndGetData(getGBDevice(currentItem.getDevice()), currentItem.getStartTime().getTime() / 1000, currentItem.getEndTime().getTime() / 1000); - if (get_gpx_file() != null) { + if (getTrackFile() != null) { showCanvas(); - activitySummariesGpsFragment.set_data(get_gpx_file()); + activitySummariesGpsFragment.set_data(getTrackFile()); } else { hideCanvas(); } @@ -206,9 +206,9 @@ public class ActivitySummaryDetail extends AbstractGBActivity { makeSummaryHeader(newItem); makeSummaryContent(newItem); activitySummariesChartFragment.setDateAndGetData(getGBDevice(currentItem.getDevice()), currentItem.getStartTime().getTime() / 1000, currentItem.getEndTime().getTime() / 1000); - if (get_gpx_file() != null) { + if (getTrackFile() != null) { showCanvas(); - activitySummariesGpsFragment.set_data(get_gpx_file()); + activitySummariesGpsFragment.set_data(getTrackFile()); } else { hideCanvas(); } @@ -227,9 +227,9 @@ public class ActivitySummaryDetail extends AbstractGBActivity { makeSummaryHeader(currentItem); makeSummaryContent(currentItem); activitySummariesChartFragment.setDateAndGetData(getGBDevice(currentItem.getDevice()), currentItem.getStartTime().getTime() / 1000, currentItem.getEndTime().getTime() / 1000); - if (get_gpx_file() != null) { + if (getTrackFile() != null) { showCanvas(); - activitySummariesGpsFragment.set_data(get_gpx_file()); + activitySummariesGpsFragment.set_data(getTrackFile()); } else { hideCanvas(); } @@ -320,9 +320,9 @@ public class ActivitySummaryDetail extends AbstractGBActivity { public void onClick(DialogInterface dialog, int which) { currentItem.setGpxTrack(selectedGpxFile); currentItem.update(); - if (get_gpx_file() != null) { + if (getTrackFile() != null) { showCanvas(); - activitySummariesGpsFragment.set_data(get_gpx_file()); + activitySummariesGpsFragment.set_data(getTrackFile()); } else { hideCanvas(); } @@ -712,7 +712,7 @@ public class ActivitySummaryDetail extends AbstractGBActivity { gpsView.setLayoutParams(params); } - private File get_gpx_file() { + private File getTrackFile() { final String gpxTrack = currentItem.getGpxTrack(); if (gpxTrack != null) { File file = new File(gpxTrack); @@ -722,6 +722,15 @@ public class ActivitySummaryDetail extends AbstractGBActivity { return null; } } + final String rawDetails = currentItem.getRawDetailsPath(); + if (rawDetails != null && rawDetails.endsWith(".fit")) { + File file = new File(rawDetails); + if (file.exists()) { + return file; + } else { + return null; + } + } return null; }