Replace long time_ms with Instant and Duration, respectively.

Fixes #561.
This commit is contained in:
Dennis Guse
2021-01-09 13:27:14 +01:00
parent 3bbcbfe202
commit e990b413cd
47 changed files with 442 additions and 434 deletions
@@ -22,6 +22,8 @@ import android.util.Log;
import androidx.annotation.NonNull;
import java.io.OutputStream;
import java.time.Duration;
import java.time.Instant;
import de.dennisguse.opentracks.content.data.Marker;
import de.dennisguse.opentracks.content.data.Track;
@@ -71,9 +73,9 @@ public class FileTrackExporter implements TrackExporter {
trackWriter.writeMultiTrackBegin();
}
//TODO Why use startTime of first track for the others?
long startTime = tracks[0].getTrackStatistics().getStartTime_ms();
Instant startTime = tracks[0].getTrackStatistics().getStartTime();
for (Track track : tracks) {
long offset = track.getTrackStatistics().getStartTime_ms() - startTime;
Duration offset = Duration.between(track.getTrackStatistics().getStartTime(), startTime);
writeLocations(track, offset);
}
if (hasMultipleTracks) {
@@ -120,7 +122,7 @@ public class FileTrackExporter implements TrackExporter {
/**
* Writes the locations.
*/
private void writeLocations(Track track, long offset) throws InterruptedException {
private void writeLocations(Track track, Duration offset) throws InterruptedException {
boolean wroteTrack = false;
boolean wroteSegment = false;
@@ -180,9 +182,9 @@ public class FileTrackExporter implements TrackExporter {
* @param offset the time offset
*/
//TODO Why?
private void setLocationTime(TrackPoint trackPoint, long offset) {
private void setLocationTime(TrackPoint trackPoint, Duration offset) {
if (trackPoint != null) {
trackPoint.setTime(trackPoint.getTime() - offset);
trackPoint.setTime(trackPoint.getTime().minus(offset));
}
}
}
@@ -21,6 +21,7 @@ import android.location.Location;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.time.Instant;
import java.util.Locale;
import de.dennisguse.opentracks.content.data.Marker;
@@ -137,7 +138,7 @@ public class GpxTrackWriter implements TrackWriter {
if (location.hasAltitude()) {
printWriter.println("<ele>" + ELEVATION_FORMAT.format(location.getAltitude()) + "</ele>");
}
printWriter.println("<time>" + StringUtils.formatDateTimeIso8601(location.getTime()) + "</time>");
printWriter.println("<time>" + StringUtils.formatDateTimeIso8601(Instant.ofEpochMilli(location.getTime())) + "</time>");
printWriter.println("<name>" + StringUtils.formatCData(marker.getName()) + "</name>");
printWriter.println("<desc>" + StringUtils.formatCData(marker.getDescription()) + "</desc>");
printWriter.println("<type>" + StringUtils.formatCData(marker.getCategory()) + "</type>");
@@ -20,6 +20,7 @@ import android.location.Location;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -385,9 +386,9 @@ public class KmlTrackWriter implements TrackWriter {
*/
private String getTime(Location location) {
if (exportTrackDetail) {
return StringUtils.formatDateTimeIso8601(location.getTime());
return StringUtils.formatDateTimeIso8601(Instant.ofEpochMilli(location.getTime()));
} else {
return StringUtils.formatDateTimeIso8601(location.getTime() - startTrackPoint.getTime());
return StringUtils.formatDateTimeIso8601(Instant.ofEpochMilli(location.getTime() - startTrackPoint.getTime().toEpochMilli()));
}
}
@@ -28,8 +28,9 @@ import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
@@ -50,7 +51,6 @@ import de.dennisguse.opentracks.util.LocationUtils;
import de.dennisguse.opentracks.util.PreferencesUtils;
import de.dennisguse.opentracks.util.StringUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;
import de.dennisguse.opentracks.util.UnitConversions;
/**
* Abstract class for file track importers.
@@ -185,9 +185,9 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
trackStatisticsUpdater.addTrackPoint(trackPoint, recordingDistanceInterval);
}
if (marker.getLocation().getTime() > trackPoint.getTime()) {
if ((marker.getTime()).isAfter(trackPoint.getTime())) {
trackPoint = null;
} else if (marker.getLocation().getTime() < trackPoint.getTime()) {
} else if (marker.getTime().isBefore(trackPoint.getTime())) {
Log.w(TAG, "Ignoring marker: current trackPoint was after marker.");
marker = null;
} else {
@@ -203,7 +203,7 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
String markerDescription = marker.getDescription();
String icon = context.getString(R.string.marker_icon_url);
double length = trackStatisticsUpdater.getTrackStatistics().getTotalDistance();
long duration = trackStatisticsUpdater.getTrackStatistics().getTotalTime();
long duration = trackStatisticsUpdater.getTrackStatistics().getTotalTime().toMillis();
// Insert marker
Marker newMarker = new Marker(marker.getName(), markerDescription, marker.getCategory(), icon, track.getId(), length, duration, trackPoint.getLocation(), marker.getPhotoUrl());
@@ -338,8 +338,7 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
TrackPoint trackPoint = createTrackPoint();
if (trackPoint.hasLocation()) {
long time = trackPoint.getTime();
Date d = new Date(time);
Instant time = trackPoint.getTime();
if (trackPoint.getLatitude() == 100) {
//TODO Remove by 31st December 2021.
trackPoint = new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL);
@@ -355,12 +354,12 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
}
// Calculate derived attributes from the previous point
if (trackData.lastLocationInCurrentSegment != null && trackData.lastLocationInCurrentSegment.getTime() != 0) {
if (trackData.lastLocationInCurrentSegment != null && trackData.lastLocationInCurrentSegment.getTime() == null) {
if (!trackPoint.hasSpeed()) {
long timeDifference = trackPoint.getTime() - trackData.lastLocationInCurrentSegment.getTime();
Duration timeDifference = Duration.between(trackData.lastLocationInCurrentSegment.getTime(), trackPoint.getTime());
// Check for negative time change
if (timeDifference <= 0) {
if (timeDifference.isNegative()) {
Log.w(TAG, "Time difference not positive.");
} else {
@@ -368,9 +367,8 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
* We don't have a speed and bearing in GPX, make something up from the last two points.
* GPS points tend to have some inherent imprecision, speed and bearing will likely be off, so the statistics for things like max speed will also be off.
*/
double duration = timeDifference * UnitConversions.MS_TO_S;
if (trackPoint.hasLocation() && trackData.lastLocationInCurrentSegment.hasLocation()) {
double speed = trackData.lastLocationInCurrentSegment.distanceTo(trackPoint) / duration;
float speed = trackData.lastLocationInCurrentSegment.distanceTo(trackPoint) / timeDifference.toMillis();
trackPoint.setSpeed((float) speed);
}
}