Cleanup: use Position and cleanup TrackPoint.

This commit is contained in:
Dennis Guse
2025-02-28 22:41:28 +01:00
parent 1d5f65be48
commit e7aa6f20d4
17 changed files with 526 additions and 428 deletions
@@ -23,7 +23,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
@@ -31,13 +30,11 @@ import java.util.Optional;
* Sensor and/or location information for a specific point in time.
* <p>
* Time is created using the {@link de.dennisguse.opentracks.services.handlers.MonotonicClock}, because system time jump backwards.
* GPS time is ignored as for non-GPS events, we could not create timestamps.
* GPS time is ignored as for non-GPS events, we could not create GPS-based timestamps.
*/
//TODO Should be a record (with final properties)
public class TrackPoint {
private static final Duration MAX_LOCATION_AGE = Duration.ofMinutes(1);
@Nullable
private TrackPoint.Id id;
@@ -98,40 +95,11 @@ public class TrackPoint {
//TODO Refactor constructors
public TrackPoint(@NonNull Type type, @NonNull Instant time) {
this.type = type;
this.position = Position.of(time);
this(null, type, Position.of(time));
}
@Deprecated
public TrackPoint(@NonNull Location location, @NonNull Instant time) {
this.type = Type.TRACKPOINT;
this.position = Position.of(location, time);
}
public TrackPoint(@NonNull Position position) {
this(null, Type.TRACKPOINT, position);
}
public TrackPoint(@NonNull Type type, @NonNull Location location, @NonNull Instant time) {
this.type = type;
this.position = Position.of(location, time);
}
//TODO Remove; only used for TrackStatisticUpdaterTest
@Deprecated
@VisibleForTesting
public TrackPoint(double latitude, double longitude, Altitude altitude, Instant time) {
this.type = Type.TRACKPOINT;
this.position = new Position(
time,
latitude,
longitude,
null,
altitude,
null,
null,
null
);
public TrackPoint(@NonNull Type type, @NonNull Position position) {
this(null, type, position);
}
public static TrackPoint createSegmentStartManualWithTime(Instant time) {
@@ -147,6 +115,7 @@ public class TrackPoint {
return type;
}
@Deprecated //Should not be needed.
public TrackPoint setType(@NonNull Type type) {
this.type = type;
return this;
@@ -186,29 +155,12 @@ public class TrackPoint {
return position.latitude();
}
@Deprecated
public TrackPoint setLatitude(double latitude) {
this.position = position.withCoordinates(latitude, getLongitude());
return this;
}
//TODO Should be double
@Deprecated //Use getPosition()
public Double getLongitude() {
return position.longitude();
}
@Deprecated
public TrackPoint setLongitude(double longitude) {
setCoordinates(getLatitude(), longitude);
return this;
}
public TrackPoint setCoordinates(double latitude, double longitude) {
this.position = this.position.withCoordinates(latitude, longitude);
return this;
}
@NonNull
public Position getPosition() {
return position;
@@ -233,6 +185,7 @@ public class TrackPoint {
return altitudeGain_m;
}
@Deprecated
public TrackPoint setAltitudeGain(Float altitudeGain_m) {
this.altitudeGain_m = altitudeGain_m;
return this;
@@ -246,6 +199,7 @@ public class TrackPoint {
return altitudeLoss_m;
}
@Deprecated
public TrackPoint setAltitudeLoss(Float altitudeLoss_m) {
this.altitudeLoss_m = altitudeLoss_m;
return this;
@@ -264,12 +218,14 @@ public class TrackPoint {
return position.altitude();
}
@Deprecated
@VisibleForTesting
public TrackPoint setAltitude(double altitude_m) {
setAltitude(Altitude.WGS84.of(altitude_m));
return this;
}
@Deprecated
public TrackPoint setAltitude(Altitude altitude) {
position = position.with(altitude);
return this;
@@ -283,6 +239,7 @@ public class TrackPoint {
return position.speed();
}
@Deprecated
public TrackPoint setSpeed(Speed speed) {
this.position = position.with(speed);
return this;
@@ -309,6 +266,7 @@ public class TrackPoint {
return position.horizontalAccuracy();
}
@Deprecated
public TrackPoint setHorizontalAccuracy(Distance horizontalAccuracy) {
this.position = this.position.withHorizontalAccuracy(horizontalAccuracy);
return this;
@@ -331,7 +331,9 @@ public class GpxTrackImporter extends DefaultHandler implements XMLImporter.Trac
}
}
TrackPoint trackPoint = new TrackPoint(new Position(
TrackPoint trackPoint = new TrackPoint(
TrackPoint.Type.TRACKPOINT,
new Position(
parsedTime.toInstant(),
latitudeParsed,
longitudeParsed,
@@ -430,7 +432,7 @@ public class GpxTrackImporter extends DefaultHandler implements XMLImporter.Trac
TrackPoint trackPoint = createTrackPoint();
if (!trackPoint.hasLocation()) {
Log.w(TAG, "Marker with invalid coordinates ignored: " + trackPoint.getLocation());
Log.w(TAG, "Marker with invalid coordinates ignored: " + trackPoint.getPosition());
return;
}
Marker marker = new Marker(null, trackPoint);
@@ -35,6 +35,7 @@ import java.util.Locale;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Position;
import de.dennisguse.opentracks.data.models.Speed;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.models.TrackPoint;
@@ -94,7 +95,7 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
private ZoneOffset zoneOffset;
private final ArrayList<Instant> whenList = new ArrayList<>();
private final ArrayList<Location> locationList = new ArrayList<>();
private final ArrayList<Position> positionList = new ArrayList<>();
private String dataType;
@@ -255,13 +256,14 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
return;
}
Location location = createLocation(longitude, latitude, altitude);
if (location == null) {
Log.w(TAG, "Marker with invalid coordinates ignored: " + location);
Position position = createPosition(latitude, longitude, altitude);
if (position == null) {
Log.w(TAG, "Marker with invalid coordinates ignored: " + latitude + " " + longitude);
return;
}
position = position.with(whenList.get(0));
Marker marker = new Marker(null, new TrackPoint(TrackPoint.Type.TRACKPOINT, location, whenList.get(0))); //TODO Creating marker without need
Marker marker = new Marker(null, new TrackPoint(TrackPoint.Type.TRACKPOINT, position)); //TODO Creating marker without need
marker.setName(name != null ? name : "");
marker.setDescription(description != null ? description : "");
marker.setCategory(activityTypeLocalized != null ? activityTypeLocalized : "");
@@ -288,7 +290,7 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
}
private void onTrackSegmentStart() {
locationList.clear();
positionList.clear();
whenList.clear();
trackpointTypeList.clear();
@@ -304,24 +306,23 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
}
private void onTrackSegmentEnd() {
if (locationList.size() != whenList.size()) {
if (positionList.size() != whenList.size()) {
throw new ImportParserException("<coords> and <when> should have the same count.");
}
// Close a track segment by inserting the segment locations
for (int i = 0; i < locationList.size(); i++) {
for (int i = 0; i < positionList.size(); i++) {
Instant time = whenList.get(i);
Location location = locationList.get(i);
Position position = positionList.get(i);
TrackPoint trackPoint;
if (location == null) {
trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, time);
if (position == null) {
trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Position.of(time));
} else {
trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, location, time);
trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, position.with(time));
}
if (i < trackpointTypeList.size() && trackpointTypeList.get(i) != null) {
TrackPoint.Type type = TrackPoint.Type.valueOf(trackpointTypeList.get(i));
trackPoint.setType(type);
}
@@ -363,7 +364,7 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
} else {
type = TrackPoint.Type.SEGMENT_START_AUTOMATIC;
}
} else if (i == locationList.size() - 1 && !trackPoint.wasCreatedManually()) {
} else if (i == positionList.size() - 1 && !trackPoint.wasCreatedManually()) {
//last
type = TrackPoint.Type.SEGMENT_END_MANUAL;
}
@@ -381,18 +382,16 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
altitude = parts.length == 3 ? parts[2] : null;
}
locationList.add(createLocation(longitude, latitude, altitude));
positionList.add(createPosition(latitude, longitude, altitude));
longitude = null;
latitude = null;
altitude = null;
}
private Location createLocation(String longitude, String latitude, String altitude) {
Location location = null;
if (longitude != null || latitude != null) {
location = new Location("import");
private Position createPosition(String latitude, String longitude, String altitude) {
if (longitude != null && latitude != null) {
Location location = new Location("import");
try {
location.setLatitude(Double.parseDouble(latitude));
location.setLongitude(Double.parseDouble(longitude));
@@ -407,8 +406,10 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
throw new ParsingException(createErrorMessage(String.format(Locale.US, "Unable to parse altitude: %s", altitude)), e);
}
}
return Position.of(location, null);
}
return location;
return null;
}
private void onExtendedDataValueEnd() throws SAXException {
@@ -124,7 +124,7 @@ public class GpsManager implements SensorConnector, LocationListenerCompat, GpsS
@Override
public void onLocationChanged(@NonNull Location location) {
// Send each update to the status; please note that this TrackPoint is not stored.
TrackPoint trackPoint = new TrackPoint(location, trackPointCreator.createNow());
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Position.of(location, trackPointCreator.createNow()));
gpsStatusManager.onNewTrackPoint(trackPoint);
if (!trackPoint.getPosition().hasValidLocation()) {
@@ -61,7 +61,7 @@ public class SettingsCustomLayoutEditAdapter extends RecyclerView.Adapter<Recycl
Speed.zero()
);
demoData = new RecordingData(track, new TrackPoint(position), null);
demoData = new RecordingData(track, new TrackPoint(TrackPoint.Type.TRACKPOINT, position), null);
}
private RecordingLayout recordingLayout;
@@ -47,6 +47,7 @@ import java.time.Instant;
import de.dennisguse.opentracks.AbstractActivity;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Position;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.databinding.MarkerEditBinding;
@@ -169,7 +170,7 @@ public class MarkerEditActivity extends AbstractActivity {
}
private LiveData<Marker> createNewMarker() {
TrackPoint trackPoint = new TrackPoint(location, Instant.now());
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, Position.of(location, Instant.now()));
return viewModel.createNewMarker(trackId, trackPoint);
}