diff --git a/src/main/java/de/dennisguse/opentracks/data/models/TrackSegment.java b/src/main/java/de/dennisguse/opentracks/data/models/TrackSegment.java index 211a3b64c..5962bc7d1 100644 --- a/src/main/java/de/dennisguse/opentracks/data/models/TrackSegment.java +++ b/src/main/java/de/dennisguse/opentracks/data/models/TrackSegment.java @@ -1,12 +1,17 @@ package de.dennisguse.opentracks.data.models; +import android.util.Log; + import androidx.annotation.NonNull; import java.time.Instant; import java.util.List; import java.util.ArrayList; +import de.dennisguse.opentracks.io.file.importer.TrackImporter; + public class TrackSegment { + private static final String TAG = TrackSegment.class.getSimpleName(); private TrackPoint.Id id; @NonNull private final Instant time; @@ -29,6 +34,17 @@ public int getTrackPointCount() { public Boolean hasTrackPoints() { return !trackPoints.isEmpty(); } + + public Distance getDistanceBetweenFirstAndLast() { + if (trackPoints == null) { + return null; + } + TrackPoint first = trackPoints.get(0); + TrackPoint last = trackPoints.get(trackPoints.size() - 1); + Distance distance = last.distanceToPrevious(first); + Log.d(TAG, "Kevin: distance between first and last in meters: " + distance.toM()); + return distance; + } } diff --git a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/daySpecificStats/DaySpecificActivity.java b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/daySpecificStats/DaySpecificActivity.java index f16b92f38..d08f29232 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/daySpecificStats/DaySpecificActivity.java +++ b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/daySpecificStats/DaySpecificActivity.java @@ -9,7 +9,10 @@ import de.dennisguse.opentracks.R; import de.dennisguse.opentracks.data.ContentProviderUtils; import de.dennisguse.opentracks.data.TrackDataHub; +import de.dennisguse.opentracks.data.TrackPointIterator; import de.dennisguse.opentracks.data.models.Track; +import de.dennisguse.opentracks.data.models.TrackPoint; +import de.dennisguse.opentracks.data.models.TrackSegment; import de.dennisguse.opentracks.databinding.DaySpecificActivityBinding; import java.time.format.DateTimeFormatter; @@ -17,6 +20,8 @@ import java.util.Date; import java.time.ZoneId; import java.util.List; +import java.util.ArrayList; +import android.widget.Toast; public class DaySpecificActivity extends AbstractTrackDeleteActivity { @@ -27,6 +32,8 @@ public class DaySpecificActivity extends AbstractTrackDeleteActivity { private ContentProviderUtils contentProviderUtils; private TrackDataHub trackDataHub; private Track.Id trackId; + private List trackSegments; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,14 +43,53 @@ protected void onCreate(Bundle savedInstanceState) { handleIntent(getIntent()); trackDataHub = new TrackDataHub(this); setSupportActionBar(viewBinding.bottomAppBarLayout.bottomAppBar); + trackSegments = new ArrayList<>(); } @Override protected void onStart() { super.onStart(); - trackDataHub.start(); } + @Override + protected void onResume() { + super.onResume(); + updateTrackSegments(); + } + + public void updateTrackSegments() { + try (TrackPointIterator trackPointIterator = contentProviderUtils.getTrackPointLocationIterator(trackId, null)) { + TrackSegment currentSegment = null; + while (trackPointIterator.hasNext()) { + TrackPoint nextPoint = trackPointIterator.next(); + + switch (nextPoint.getType()) { + case SEGMENT_START_AUTOMATIC: + case SEGMENT_START_MANUAL: + if (currentSegment != null) { + trackSegments.add(currentSegment); + } + currentSegment = new TrackSegment(nextPoint.getTime()); + break; + + case SEGMENT_END_MANUAL: + trackSegments.add(currentSegment); + currentSegment = null; + + case TRACKPOINT: + if (currentSegment != null) { + currentSegment.addTrackPoint(nextPoint); + } + break; + + default: + Log.d(TAG, "No Action for TrackPoint IDLE/SENSORPOINT while recording segments"); + } + } + trackSegments.forEach(segment -> System.out.println("KEVIN: Distance for segment: " + segment.getDistanceBetweenFirstAndLast().toM())); + System.out.println("Segments count: " + trackSegments.size()); + } + } private Date getDummyDate() { String dateString = "2024-03-02"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); @@ -56,7 +102,7 @@ private void handleIntent(Intent intent) { trackId = intent.getParcelableExtra(EXTRA_TRACK_ID); if (trackId == null) { Log.e(TAG, DaySpecificActivity.class.getSimpleName() + " needs EXTRA_TRACK_ID."); -// finish(); + // None provided, we will assume a specific date on our own activityDate = getDummyDate(); List tracks = contentProviderUtils.getTracks(); @@ -64,7 +110,11 @@ private void handleIntent(Intent intent) { System.out.println("Track date = " + track.getStartTime().toString()); } Track track = contentProviderUtils.getTrack(activityDate); -// trackId = track.getId(); + if (track == null) { + finish(); + } else { + trackId = track.getId(); + } } } @@ -81,7 +131,7 @@ protected void onDeleteConfirmed() { @Override protected Track.Id getRecordingTrackId() { - return null; + return trackId; } @Override