Skip to content

Commit

Permalink
GH-127: 40261875: Added distance calculator in track segment class
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwad committed Apr 8, 2024
1 parent b3e539d commit 8df20e8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
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;
import java.time.LocalDate;
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 {

Expand All @@ -27,6 +32,8 @@ public class DaySpecificActivity extends AbstractTrackDeleteActivity {
private ContentProviderUtils contentProviderUtils;
private TrackDataHub trackDataHub;
private Track.Id trackId;
private List<TrackSegment> trackSegments;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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");
Expand All @@ -56,15 +102,19 @@ 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<Track> tracks = contentProviderUtils.getTracks();
for (Track track: tracks) {
System.out.println("Track date = " + track.getStartTime().toString());
}
Track track = contentProviderUtils.getTrack(activityDate);
// trackId = track.getId();
if (track == null) {
finish();
} else {
trackId = track.getId();
}
}
}

Expand All @@ -81,7 +131,7 @@ protected void onDeleteConfirmed() {

@Override
protected Track.Id getRecordingTrackId() {
return null;
return trackId;
}

@Override
Expand Down

0 comments on commit 8df20e8

Please sign in to comment.