Statistics requires startTime and stopTime.

This is now enforced for import as wells.
This commit is contained in:
Dennis Guse
2025-11-24 19:53:46 +01:00
parent 860b90da37
commit a6d4a05839
6 changed files with 20 additions and 31 deletions
@@ -120,14 +120,14 @@ public class ContentProviderUtils {
int altitudeLossIndex = cursor.getColumnIndexOrThrow(TracksColumns.ALTITUDE_LOSS);
Statistics trackStatistics = new Statistics(
cursor.isNull(startTimeIndex) ? null : Instant.ofEpochMilli(cursor.getLong(startTimeIndex)),
cursor.isNull(stopTimeIndex) ? null : Instant.ofEpochMilli(cursor.getLong(stopTimeIndex)),
cursor.isNull(totalTimeIndex) ? null : Duration.ofMillis(cursor.getLong(totalTimeIndex)),
cursor.isNull(movingTimeIndex) ? null : Duration.ofMillis(cursor.getLong(movingTimeIndex)),
Instant.ofEpochMilli(cursor.getLong(startTimeIndex)),
Instant.ofEpochMilli(cursor.getLong(stopTimeIndex)),
Duration.ofMillis(cursor.getLong(totalTimeIndex)),
Duration.ofMillis(cursor.getLong(movingTimeIndex)),
cursor.isNull(totalDistanceIndex) ? null : Distance.of(cursor.getFloat(totalDistanceIndex)),
Distance.of(cursor.getFloat(totalDistanceIndex)),
cursor.isNull(maxSpeedIndex) ? null : Speed.of(cursor.getFloat(maxSpeedIndex)),
Speed.of(cursor.getFloat(maxSpeedIndex)),
cursor.isNull(minAltitudeIndex) || cursor.isNull(maxAltitudeIndex) ? null : new AltitudeExtremities(cursor.getFloat(minAltitudeIndex), cursor.getFloat(maxAltitudeIndex)),
cursor.isNull(altitudeGainIndex) || cursor.isNull(altitudeLossIndex) ? null : new AltitudeGainLoss(cursor.getFloat(altitudeGainIndex), cursor.getFloat(altitudeLossIndex)),
@@ -136,12 +136,8 @@ public class ContentProviderUtils {
);
Track track = new Track(ZoneOffset.ofTotalSeconds(cursor.getInt(startTimeOffsetIndex)), trackStatistics);
if (!cursor.isNull(idIndex)) {
track.setId(new Track.Id(cursor.getLong(idIndex)));
}
if (!cursor.isNull(uuidIndex)) {
track.setUuid(UUIDUtils.fromBytes(cursor.getBlob(uuidIndex)));
}
track.setId(new Track.Id(cursor.getLong(idIndex)));
track.setUuid(UUIDUtils.fromBytes(cursor.getBlob(uuidIndex)));
if (!cursor.isNull(nameIndex)) {
track.setName(cursor.getString(nameIndex));
}
@@ -310,12 +306,8 @@ public class ContentProviderUtils {
private ContentValues createContentValues(Statistics trackStatistics) {
ContentValues values = new ContentValues();
if (trackStatistics.startTime() != null) {
values.put(TracksColumns.STARTTIME, trackStatistics.startTime().toEpochMilli());
}
if (trackStatistics.stopTime() != null) {
values.put(TracksColumns.STOPTIME, trackStatistics.stopTime().toEpochMilli());
}
values.put(TracksColumns.STARTTIME, trackStatistics.startTime().toEpochMilli());
values.put(TracksColumns.STOPTIME, trackStatistics.stopTime().toEpochMilli());
values.put(TracksColumns.TOTALDISTANCE, trackStatistics.totalDistance().toM());
values.put(TracksColumns.TOTALTIME, trackStatistics.totalDuration().toMillis());
values.put(TracksColumns.MOVINGTIME, trackStatistics.movingDuration().toMillis());
@@ -40,8 +40,8 @@ public record Statistics(
@Deprecated
public static final Statistics DEFAULT =
new Statistics(
null,
null,
Instant.EPOCH,
Instant.EPOCH,
Duration.ZERO,
Duration.ZERO,
Distance.ZERO,
@@ -122,8 +122,8 @@ public record Statistics(
}
return new Statistics(
startTime == null ? other.startTime : startTime.isBefore(other.startTime) ? startTime : other.startTime,
stopTime == null ? other.stopTime : stopTime.isAfter(other.stopTime) ? stopTime : other.stopTime,
startTime.isBefore(other.startTime) ? startTime : other.startTime,
stopTime.isAfter(other.stopTime) ? stopTime : other.stopTime,
totalDuration.plus(other.totalDuration),
movingDuration.plus(other.movingDuration),
totalDistance.plus(other.totalDistance),
@@ -55,12 +55,6 @@ public class Track {
@NonNull
private Statistics trackStatistics;
@Deprecated
@VisibleForTesting
public Track() {
this(ZoneOffset.UTC);
}
@VisibleForTesting
public Track(Statistics statistics) {
this(ZoneOffset.UTC, statistics);
@@ -3,6 +3,7 @@ package de.dennisguse.opentracks.io.file;
import androidx.annotation.NonNull;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -89,7 +90,7 @@ public class TrackFilenameGenerator {
}
public String getExample() {
Track track = new Track(new Statistics(Instant.EPOCH, null, null, null, null, null, null, null, null, null));
Track track = new Track(ZoneOffset.UTC, Statistics.DEFAULT);
track.setName("Berlin");
track.setUuid(UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6"));