forked from upstream-mirrors/OpenTracks
TrackPoint is now a record.
This commit is contained in:
@@ -51,10 +51,10 @@ import de.dennisguse.opentracks.data.models.Speed;
|
||||
import de.dennisguse.opentracks.data.models.Statistics;
|
||||
import de.dennisguse.opentracks.data.models.Track;
|
||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||
import de.dennisguse.opentracks.data.statistics.SensorStatistics;
|
||||
import de.dennisguse.opentracks.data.tables.MarkerColumns;
|
||||
import de.dennisguse.opentracks.data.tables.TrackPointsColumns;
|
||||
import de.dennisguse.opentracks.data.tables.TracksColumns;
|
||||
import de.dennisguse.opentracks.data.statistics.SensorStatistics;
|
||||
import de.dennisguse.opentracks.ui.markers.MarkerUtils;
|
||||
import de.dennisguse.opentracks.util.FileUtils;
|
||||
|
||||
@@ -544,7 +544,12 @@ public class ContentProviderUtils {
|
||||
int altitudeGainIndex = cursor.getColumnIndexOrThrow(TrackPointsColumns.ALTITUDE_GAIN);
|
||||
int altitudeLossIndex = cursor.getColumnIndexOrThrow(TrackPointsColumns.ALTITUDE_LOSS);
|
||||
|
||||
TrackPoint trackPoint = new TrackPoint(
|
||||
AltitudeGainLoss altitudeGainLoss = null;
|
||||
if (!cursor.isNull(altitudeGainIndex) && !cursor.isNull(altitudeLossIndex)) {
|
||||
altitudeGainLoss = new AltitudeGainLoss(cursor.getFloat(altitudeGainIndex), cursor.getFloat(altitudeLossIndex));
|
||||
}
|
||||
|
||||
return new TrackPoint(
|
||||
new TrackPoint.Id(cursor.getInt(idIndex)),
|
||||
TrackPoint.Type.getById(cursor.getInt(typeIndex)),
|
||||
new Position(
|
||||
@@ -556,26 +561,13 @@ public class ContentProviderUtils {
|
||||
!cursor.isNull(accuracyVerticalIndex) ? Distance.of(cursor.getFloat(accuracyVerticalIndex)) : null,
|
||||
!cursor.isNull(bearingIndex) ? cursor.getFloat(bearingIndex) : null,
|
||||
!cursor.isNull(speedIndex) ? Speed.of(cursor.getFloat(speedIndex)) : null
|
||||
));
|
||||
|
||||
if (!cursor.isNull(sensorHeartRateIndex)) {
|
||||
trackPoint.setHeartRate(cursor.getFloat(sensorHeartRateIndex));
|
||||
}
|
||||
if (!cursor.isNull(sensorCadenceIndex)) {
|
||||
trackPoint.setCadence(cursor.getFloat(sensorCadenceIndex));
|
||||
}
|
||||
if (!cursor.isNull(sensorDistanceIndex)) {
|
||||
trackPoint.setSensorDistance(Distance.of(cursor.getFloat(sensorDistanceIndex)));
|
||||
}
|
||||
if (!cursor.isNull(sensorPowerIndex)) {
|
||||
trackPoint.setPower(cursor.getFloat(sensorPowerIndex));
|
||||
}
|
||||
|
||||
if (!cursor.isNull(altitudeGainIndex) && !cursor.isNull(altitudeLossIndex)) {
|
||||
trackPoint.setAltitudeGainLoss(cursor.getFloat(altitudeGainIndex), cursor.getFloat(altitudeLossIndex));
|
||||
}
|
||||
|
||||
return trackPoint;
|
||||
),
|
||||
!cursor.isNull(sensorDistanceIndex) ? Distance.of(cursor.getFloat(sensorDistanceIndex)) : null,
|
||||
!cursor.isNull(sensorHeartRateIndex) ? HeartRate.of(cursor.getFloat(sensorHeartRateIndex)) : null,
|
||||
!cursor.isNull(sensorCadenceIndex) ? Cadence.of(cursor.getFloat(sensorCadenceIndex)) : null,
|
||||
!cursor.isNull(sensorPowerIndex) ? Power.of(cursor.getFloat(sensorPowerIndex)) : null,
|
||||
altitudeGainLoss
|
||||
);
|
||||
}
|
||||
|
||||
//TODO Only used for file import; might be better to replace it.
|
||||
@@ -637,42 +629,42 @@ public class ContentProviderUtils {
|
||||
private ContentValues createContentValues(TrackPoint trackPoint, Track.Id trackId) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(TrackPointsColumns.TRACKID, trackId.id());
|
||||
values.put(TrackPointsColumns.TYPE, trackPoint.getType().type_db);
|
||||
values.put(TrackPointsColumns.TYPE, trackPoint.type().type_db);
|
||||
|
||||
if (trackPoint.hasLocation()) {
|
||||
values.put(TrackPointsColumns.LATITUDE, (int) (trackPoint.getPosition().latitude() * 1E6));
|
||||
values.put(TrackPointsColumns.LONGITUDE, (int) (trackPoint.getPosition().longitude() * 1E6));
|
||||
if (trackPoint.position().hasLocation()) {
|
||||
values.put(TrackPointsColumns.LATITUDE, (int) (trackPoint.position().latitude() * 1E6));
|
||||
values.put(TrackPointsColumns.LONGITUDE, (int) (trackPoint.position().longitude() * 1E6));
|
||||
}
|
||||
values.put(TrackPointsColumns.TIME, trackPoint.getTime().toEpochMilli());
|
||||
if (trackPoint.hasAltitude()) {
|
||||
values.put(TrackPointsColumns.ALTITUDE, trackPoint.getAltitude().toM());
|
||||
if (trackPoint.position().hasAltitude()) {
|
||||
values.put(TrackPointsColumns.ALTITUDE, trackPoint.position().altitude().toM());
|
||||
}
|
||||
if (trackPoint.hasHorizontalAccuracy()) {
|
||||
values.put(TrackPointsColumns.HORIZONTAL_ACCURACY, trackPoint.getHorizontalAccuracy().toM());
|
||||
if (trackPoint.position().hasHorizontalAccuracy()) {
|
||||
values.put(TrackPointsColumns.HORIZONTAL_ACCURACY, trackPoint.position().horizontalAccuracy().toM());
|
||||
}
|
||||
if (trackPoint.hasSpeed()) {
|
||||
values.put(TrackPointsColumns.SPEED, trackPoint.getSpeed().toMPS());
|
||||
if (trackPoint.position().hasSpeed()) {
|
||||
values.put(TrackPointsColumns.SPEED, trackPoint.position().speed().toMPS());
|
||||
}
|
||||
if (trackPoint.hasBearing()) {
|
||||
values.put(TrackPointsColumns.BEARING, trackPoint.getBearing());
|
||||
if (trackPoint.position().hasBearing()) {
|
||||
values.put(TrackPointsColumns.BEARING, trackPoint.position().bearing());
|
||||
}
|
||||
|
||||
if (trackPoint.hasHeartRate()) {
|
||||
values.put(TrackPointsColumns.SENSOR_HEARTRATE, trackPoint.getHeartRate().getBPM());
|
||||
if (trackPoint.heartRate() != null) {
|
||||
values.put(TrackPointsColumns.SENSOR_HEARTRATE, trackPoint.heartRate().getBPM());
|
||||
}
|
||||
if (trackPoint.hasCadence()) {
|
||||
values.put(TrackPointsColumns.SENSOR_CADENCE, trackPoint.getCadence().getRPM());
|
||||
if (trackPoint.cadence() != null) {
|
||||
values.put(TrackPointsColumns.SENSOR_CADENCE, trackPoint.cadence().getRPM());
|
||||
}
|
||||
if (trackPoint.hasSensorDistance()) {
|
||||
values.put(TrackPointsColumns.SENSOR_DISTANCE, trackPoint.getSensorDistance().toM());
|
||||
if (trackPoint.sensorDistance() != null) {
|
||||
values.put(TrackPointsColumns.SENSOR_DISTANCE, trackPoint.sensorDistance().toM());
|
||||
}
|
||||
if (trackPoint.hasPower()) {
|
||||
values.put(TrackPointsColumns.SENSOR_POWER, trackPoint.getPower().getW());
|
||||
if (trackPoint.power() != null) {
|
||||
values.put(TrackPointsColumns.SENSOR_POWER, trackPoint.power().getW());
|
||||
}
|
||||
|
||||
if (trackPoint.hasAltitudeGainLoss()) {
|
||||
values.put(TrackPointsColumns.ALTITUDE_GAIN, trackPoint.getAltitudeGainLoss().gain_m());
|
||||
values.put(TrackPointsColumns.ALTITUDE_LOSS, trackPoint.getAltitudeGainLoss().loss_m());
|
||||
if (trackPoint.altitudeGainLoss() != null) {
|
||||
values.put(TrackPointsColumns.ALTITUDE_GAIN, trackPoint.altitudeGainLoss().gain_m());
|
||||
values.put(TrackPointsColumns.ALTITUDE_LOSS, trackPoint.altitudeGainLoss().loss_m());
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ import java.util.Set;
|
||||
import de.dennisguse.opentracks.data.models.Statistics;
|
||||
import de.dennisguse.opentracks.data.models.Track;
|
||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||
import de.dennisguse.opentracks.data.statistics.TrackStatisticsUpdater;
|
||||
import de.dennisguse.opentracks.data.tables.TrackPointsColumns;
|
||||
import de.dennisguse.opentracks.data.tables.TracksColumns;
|
||||
import de.dennisguse.opentracks.services.RecordingStatus;
|
||||
import de.dennisguse.opentracks.services.TrackRecordingService;
|
||||
import de.dennisguse.opentracks.services.handlers.AltitudeCorrectionManager;
|
||||
import de.dennisguse.opentracks.data.statistics.TrackStatisticsUpdater;
|
||||
|
||||
/**
|
||||
* Track data hub.
|
||||
@@ -288,14 +288,14 @@ public class TrackDataHub {
|
||||
}
|
||||
|
||||
trackPoint = trackPointIterator.next();
|
||||
TrackPoint.Id trackPointId = trackPoint.getId();
|
||||
TrackPoint.Id trackPointId = trackPoint.id();
|
||||
|
||||
// Stop if past the last wanted point
|
||||
if (maxPointId != null && trackPointId.id() > maxPointId.id()) {
|
||||
break;
|
||||
}
|
||||
|
||||
egm2008Correction.correctAltitude(context, trackPoint);
|
||||
trackPoint = egm2008Correction.correctAltitude(context, trackPoint);
|
||||
|
||||
if (localFirstSeenTrackPointId == null) {
|
||||
localFirstSeenTrackPointId = trackPointId;
|
||||
@@ -319,7 +319,7 @@ public class TrackDataHub {
|
||||
}
|
||||
|
||||
if (trackPoint != null) {
|
||||
localLastSeenTrackPointIdId = trackPoint.getId();
|
||||
localLastSeenTrackPointIdId = trackPoint.id();
|
||||
}
|
||||
|
||||
if (updateSamplingState) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.dennisguse.opentracks.data.models;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public record HeartRate(float value) {
|
||||
public static HeartRate INVALID = HeartRate.of(0.0f);
|
||||
|
||||
@@ -7,6 +9,12 @@ public record HeartRate(float value) {
|
||||
return new HeartRate(value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static HeartRate ofOrNull(Float value) {
|
||||
if (value == null) return null;
|
||||
return new HeartRate(value);
|
||||
}
|
||||
|
||||
public float getBPM() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -51,10 +51,10 @@ public final class MarkerBuilder {
|
||||
this.typeLocalized = "";
|
||||
this.photoUrl = null;
|
||||
|
||||
if (!trackPoint.hasLocation())
|
||||
if (!trackPoint.position().hasLocation())
|
||||
throw new RuntimeException("Marker requires a trackpoint with a location.");
|
||||
|
||||
this.position = trackPoint.getPosition();
|
||||
this.position = trackPoint.position();
|
||||
}
|
||||
|
||||
public MarkerBuilder(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint, String name, String description, String typeLocalized, Uri photoUrl) {
|
||||
|
||||
@@ -17,8 +17,9 @@ public record Position(
|
||||
@Nullable Float bearing,
|
||||
@Nullable Speed speed
|
||||
) {
|
||||
@Deprecated
|
||||
public static Position empty() {
|
||||
return of((Instant) null);
|
||||
return of(null);
|
||||
}
|
||||
|
||||
public static Position of(@NonNull Instant time) {
|
||||
@@ -34,10 +35,6 @@ public record Position(
|
||||
);
|
||||
}
|
||||
|
||||
public static Position of(@NonNull Location location) {
|
||||
return of(location, Instant.ofEpochMilli(location.getTime()));
|
||||
}
|
||||
|
||||
public static Position of(@NonNull Location location, @NonNull Instant time) {
|
||||
return new Position(
|
||||
time,
|
||||
@@ -132,9 +129,8 @@ public record Position(
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
|
||||
//TODO Use double
|
||||
public Position withCoordinates(Double latitude, Double longitude) {
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
public Position withCoordinates(Position position) {
|
||||
return new Position(time, position.latitude, position.longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
|
||||
//TODO Use float
|
||||
|
||||
@@ -22,23 +22,168 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.time.Instant;
|
||||
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 GPS-based timestamps.
|
||||
* GPS time is ignored as for non-GPS events, we cannot create GPS-based timestamps.
|
||||
*/
|
||||
//TODO Should be a record (with final properties)
|
||||
public class TrackPoint {
|
||||
public record TrackPoint(
|
||||
|
||||
@Nullable
|
||||
private final TrackPoint.Id id;
|
||||
@Nullable
|
||||
TrackPoint.Id id,
|
||||
|
||||
@NonNull
|
||||
TrackPoint.Type type,
|
||||
|
||||
//Requires: position.time must be non-null
|
||||
@NonNull
|
||||
Position position,
|
||||
|
||||
Distance sensorDistance,
|
||||
HeartRate heartRate,
|
||||
Cadence cadence,
|
||||
Power power,
|
||||
AltitudeGainLoss altitudeGainLoss
|
||||
) {
|
||||
|
||||
public TrackPoint(Type type, Position position) {
|
||||
this(
|
||||
null,
|
||||
type,
|
||||
position,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
@Deprecated //TOOD Private?
|
||||
public TrackPoint(Type type, Instant now) {
|
||||
this(
|
||||
null,
|
||||
type,
|
||||
Position.of(now),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
public static TrackPoint createSegmentStartManualWithTime(Instant time) {
|
||||
return new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, time);
|
||||
}
|
||||
|
||||
public static TrackPoint with(TrackPoint trackPoint, Altitude.EGM2008 correctAltitude) {
|
||||
return new TrackPoint(
|
||||
trackPoint.id,
|
||||
trackPoint.type,
|
||||
new Position(
|
||||
trackPoint.position.time(),
|
||||
trackPoint.position.latitude(),
|
||||
trackPoint.position.longitude(),
|
||||
trackPoint.position.horizontalAccuracy(),
|
||||
correctAltitude,
|
||||
trackPoint.position.verticalAccuracy(),
|
||||
trackPoint.position.bearing(),
|
||||
trackPoint.position.speed()
|
||||
),
|
||||
trackPoint.sensorDistance,
|
||||
trackPoint.heartRate,
|
||||
trackPoint.cadence,
|
||||
trackPoint.power,
|
||||
trackPoint.altitudeGainLoss
|
||||
);
|
||||
}
|
||||
|
||||
public TrackPoint with(Type newType) {
|
||||
return new TrackPoint(
|
||||
id,
|
||||
newType,
|
||||
position,
|
||||
sensorDistance,
|
||||
heartRate,
|
||||
cadence,
|
||||
power,
|
||||
altitudeGainLoss
|
||||
);
|
||||
}
|
||||
|
||||
public TrackPoint with(Position newPosition) {
|
||||
return new TrackPoint(
|
||||
id,
|
||||
type,
|
||||
newPosition,
|
||||
sensorDistance,
|
||||
heartRate,
|
||||
cadence,
|
||||
power,
|
||||
altitudeGainLoss
|
||||
);
|
||||
}
|
||||
|
||||
//Requires: position.time must be non-null
|
||||
@NonNull
|
||||
private Position position;
|
||||
public Instant getTime() {
|
||||
return position.time();
|
||||
}
|
||||
|
||||
|
||||
public TrackPoint minusCumulativeSensorData(@NonNull TrackPoint lastTrackPoint) {
|
||||
Distance newSensorDistance = sensorDistance;
|
||||
if (sensorDistance != null && lastTrackPoint.sensorDistance != null) {
|
||||
newSensorDistance = sensorDistance.minus(lastTrackPoint.sensorDistance);
|
||||
}
|
||||
AltitudeGainLoss newAltitudeGainLoss = altitudeGainLoss;
|
||||
if (altitudeGainLoss != null && lastTrackPoint.altitudeGainLoss() != null) {
|
||||
newAltitudeGainLoss = new AltitudeGainLoss(
|
||||
altitudeGainLoss.gain_m() - lastTrackPoint.altitudeGainLoss.gain_m(),
|
||||
altitudeGainLoss.loss_m() - lastTrackPoint.altitudeGainLoss.loss_m());
|
||||
}
|
||||
return new TrackPoint(
|
||||
id,
|
||||
type,
|
||||
position,
|
||||
newSensorDistance,
|
||||
heartRate,
|
||||
cadence,
|
||||
power,
|
||||
newAltitudeGainLoss
|
||||
);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Distance distanceToPrevious(@NonNull TrackPoint previous) {
|
||||
if (sensorDistance() != null) {
|
||||
return sensorDistance();
|
||||
}
|
||||
return distanceToPreviousFromLocation(previous);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Distance distanceToPreviousFromLocation(@NonNull TrackPoint previous) {
|
||||
if (!position.hasLocation() || !previous.position().hasLocation()) {
|
||||
throw new RuntimeException("Cannot compute distance.");
|
||||
}
|
||||
|
||||
return Distance.of(position().toLocation().distanceTo(previous.position().toLocation()));
|
||||
}
|
||||
|
||||
public boolean isIdleTriggered() {
|
||||
return type == TrackPoint.Type.IDLE;
|
||||
}
|
||||
|
||||
public boolean isSegmentManualStart() {
|
||||
return type == TrackPoint.Type.SEGMENT_START_MANUAL;
|
||||
}
|
||||
|
||||
public boolean isSegmentManualEnd() {
|
||||
return type == TrackPoint.Type.SEGMENT_END_MANUAL;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
SEGMENT_START_MANUAL(-2), //Start of a segment due to user interaction (start, resume)
|
||||
@@ -75,298 +220,6 @@ public class TrackPoint {
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Type type;
|
||||
|
||||
private Distance sensorDistance;
|
||||
private HeartRate heartRate = null;
|
||||
private Cadence cadence = null;
|
||||
private Power power = null;
|
||||
private AltitudeGainLoss altitudeGainLoss = null;
|
||||
|
||||
public TrackPoint(@Nullable TrackPoint.Id id, @NonNull Type type, @NonNull Position position) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public TrackPoint(@NonNull Type type, @NonNull Instant time) {
|
||||
this(null, type, Position.of(time));
|
||||
}
|
||||
|
||||
public TrackPoint(@NonNull Type type, @NonNull Position position) {
|
||||
this(null, type, position);
|
||||
}
|
||||
|
||||
public static TrackPoint createSegmentStartManualWithTime(Instant time) {
|
||||
return new TrackPoint(Type.SEGMENT_START_MANUAL, time);
|
||||
}
|
||||
|
||||
public static TrackPoint createSegmentEndWithTime(Instant time) {
|
||||
return new TrackPoint(Type.SEGMENT_END_MANUAL, time);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Deprecated //Should not be needed.
|
||||
public TrackPoint setType(@NonNull Type type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isIdleTriggered() {
|
||||
return type == Type.IDLE;
|
||||
}
|
||||
|
||||
public boolean isSegmentManualStart() {
|
||||
return type == Type.SEGMENT_START_MANUAL;
|
||||
}
|
||||
|
||||
public boolean isSegmentManualEnd() {
|
||||
return type == Type.SEGMENT_END_MANUAL;
|
||||
}
|
||||
|
||||
//TODO Check if naming is correct.
|
||||
public boolean wasCreatedManually() {
|
||||
return hasLocation() || hasSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* May be null if the track was not loaded from the database.
|
||||
*/
|
||||
@Nullable
|
||||
public TrackPoint.Id getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean hasLocation() {
|
||||
return position.hasLocation();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public TrackPoint setPosition(Position position) {
|
||||
this.position = position.with(this.position.time());
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasAltitudeGainLoss() {
|
||||
return altitudeGainLoss != null;
|
||||
}
|
||||
|
||||
public AltitudeGainLoss getAltitudeGainLoss() {
|
||||
return altitudeGainLoss;
|
||||
}
|
||||
|
||||
public TrackPoint setAltitudeGainLoss(AltitudeGainLoss altitudeGainLoss) {
|
||||
this.altitudeGainLoss = altitudeGainLoss;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrackPoint setAltitudeGainLoss(float altitudeGain_m, float altitudeLoss_m) {
|
||||
setAltitudeGainLoss(new AltitudeGainLoss(altitudeGain_m, altitudeLoss_m));
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Instant getTime() {
|
||||
return position.time();
|
||||
}
|
||||
|
||||
public boolean hasAltitude() {
|
||||
return position.hasAltitude();
|
||||
}
|
||||
|
||||
public Altitude getAltitude() {
|
||||
return position.altitude();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint setAltitude(Altitude altitude) {
|
||||
position = position.with(altitude);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasSpeed() {
|
||||
return position.hasSpeed();
|
||||
}
|
||||
|
||||
public Speed getSpeed() {
|
||||
return position.speed();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint setSpeed(Speed speed) {
|
||||
this.position = position.with(speed);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasBearing() {
|
||||
return position.hasBearing();
|
||||
}
|
||||
|
||||
public float getBearing() {
|
||||
return position.bearing();
|
||||
}
|
||||
|
||||
public TrackPoint setBearing(Float bearing) {
|
||||
this.position = this.position.withBearing(bearing);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasHorizontalAccuracy() {
|
||||
return position.hasHorizontalAccuracy();
|
||||
}
|
||||
|
||||
public Distance getHorizontalAccuracy() {
|
||||
return position.horizontalAccuracy();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint setHorizontalAccuracy(Distance horizontalAccuracy) {
|
||||
this.position = this.position.withHorizontalAccuracy(horizontalAccuracy);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasVerticalAccuracy() {
|
||||
return position.hasVerticalAccuracy();
|
||||
}
|
||||
|
||||
public Distance getVerticalAccuracy() {
|
||||
return position.verticalAccuracy();
|
||||
}
|
||||
|
||||
public TrackPoint setVerticalAccuracy(Distance verticalAccuracy) {
|
||||
this.position = this.position.withVerticalAccuracy(verticalAccuracy);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Distance distanceToPrevious(@NonNull TrackPoint previous) {
|
||||
if (hasSensorDistance()) {
|
||||
return getSensorDistance();
|
||||
}
|
||||
return distanceToPreviousFromLocation(previous);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Distance distanceToPreviousFromLocation(@NonNull TrackPoint previous) {
|
||||
if (!hasLocation() || hasLocation() != previous.hasLocation()) {
|
||||
throw new RuntimeException("Cannot compute distance.");
|
||||
}
|
||||
|
||||
return Distance.of(getPosition().toLocation().distanceTo(previous.getPosition().toLocation()));
|
||||
}
|
||||
|
||||
public boolean fulfillsAccuracy(Distance thresholdHorizontalAccuracy) {
|
||||
return position.fulfillsAccuracy(thresholdHorizontalAccuracy);
|
||||
}
|
||||
|
||||
public Optional<Float> bearingTo(@NonNull TrackPoint dest) {
|
||||
if (!dest.hasLocation() || !hasLocation()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(getPosition().toLocation().bearingTo(dest.getPosition().toLocation()));
|
||||
}
|
||||
|
||||
// Sensor data
|
||||
public boolean hasSensorDistance() {
|
||||
return sensorDistance != null;
|
||||
}
|
||||
|
||||
public Distance getSensorDistance() {
|
||||
return sensorDistance;
|
||||
}
|
||||
|
||||
public TrackPoint setSensorDistance(Distance distance_m) {
|
||||
this.sensorDistance = distance_m;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrackPoint minusCumulativeSensorData(@NonNull TrackPoint lastTrackPoint) {
|
||||
if (hasSensorDistance() && lastTrackPoint.hasSensorDistance()) {
|
||||
sensorDistance = sensorDistance.minus(lastTrackPoint.getSensorDistance());
|
||||
}
|
||||
if (hasAltitudeGainLoss() && lastTrackPoint.hasAltitudeGainLoss()) {
|
||||
altitudeGainLoss = new AltitudeGainLoss(
|
||||
altitudeGainLoss.gain_m() - lastTrackPoint.altitudeGainLoss.gain_m(),
|
||||
altitudeGainLoss.loss_m() - lastTrackPoint.altitudeGainLoss.loss_m());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasHeartRate() {
|
||||
return heartRate != null;
|
||||
}
|
||||
|
||||
public HeartRate getHeartRate() {
|
||||
return heartRate;
|
||||
}
|
||||
|
||||
public TrackPoint setHeartRate(HeartRate heartRate) {
|
||||
this.heartRate = heartRate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrackPoint setHeartRate(float heartRate) {
|
||||
return setHeartRate(HeartRate.of(heartRate));
|
||||
}
|
||||
|
||||
public boolean hasCadence() {
|
||||
return cadence != null;
|
||||
}
|
||||
|
||||
public Cadence getCadence() {
|
||||
return cadence;
|
||||
}
|
||||
|
||||
public TrackPoint setCadence(Cadence cadence) {
|
||||
this.cadence = cadence;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrackPoint setCadence(float cadence) {
|
||||
return setCadence(Cadence.of(cadence));
|
||||
}
|
||||
|
||||
public boolean hasPower() {
|
||||
return power != null;
|
||||
}
|
||||
|
||||
public Power getPower() {
|
||||
return power;
|
||||
}
|
||||
|
||||
public TrackPoint setPower(Power power) {
|
||||
this.power = power;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrackPoint setPower(float power) {
|
||||
return setPower(Power.of(power));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TrackPoint{" +
|
||||
"id=" + id +
|
||||
", type=" + type +
|
||||
", position=" + position +
|
||||
", sensorDistance=" + sensorDistance +
|
||||
", heartRate=" + heartRate +
|
||||
", cadence=" + cadence +
|
||||
", power=" + power +
|
||||
", altitudeGainLoss=" + altitudeGainLoss +
|
||||
'}';
|
||||
}
|
||||
|
||||
public record Id(long id) implements Parcelable {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -154,7 +154,7 @@ public class SegmentStatisticUpdater {
|
||||
Duration movingDuration = Duration.between(lastTrackPoint.getTime(), trackPoint.getTime());
|
||||
|
||||
if (movingDuration.isNegative()) {
|
||||
throw new RuntimeException("Moving time cannot be negative");
|
||||
throw new RuntimeException("Moving time cannot be negative: " + lastTrackPoint.getTime() + " is after " + trackPoint.getTime());
|
||||
}
|
||||
this.movingDuration = this.movingDuration.plus(movingDuration);
|
||||
}
|
||||
|
||||
+17
-17
@@ -111,32 +111,32 @@ public class TrackStatisticsUpdater {
|
||||
currentSegment.updateTotalTime(trackPoint.getTime());
|
||||
|
||||
// Process sensor data: barometer
|
||||
if (trackPoint.hasAltitudeGainLoss()) {
|
||||
currentSegment.addTotalAltitudeGainLoss(trackPoint.getAltitudeGainLoss());
|
||||
if (trackPoint.altitudeGainLoss() != null) {
|
||||
currentSegment.addTotalAltitudeGainLoss(trackPoint.altitudeGainLoss());
|
||||
}
|
||||
|
||||
//Update absolute (GPS-based) altitude
|
||||
if (trackPoint.hasAltitude()) {
|
||||
currentSegment.updateAltitudeExtremities(trackPoint.getAltitude());
|
||||
// Update absolute (GPS-based) altitude
|
||||
if (trackPoint.position().hasAltitude()) {
|
||||
currentSegment.updateAltitudeExtremities(trackPoint.position().altitude());
|
||||
}
|
||||
|
||||
// Update heart rate
|
||||
if (trackPoint.hasHeartRate() && lastTrackPoint != null) {
|
||||
if (trackPoint.heartRate() != null && lastTrackPoint != null) {
|
||||
Duration trackPointDuration = Duration.between(lastTrackPoint.getTime(), trackPoint.getTime());
|
||||
Duration newTotalDuration = totalHeartRateDuration.plus(trackPointDuration);
|
||||
|
||||
averageHeartRateBPM = (totalHeartRateDuration.toMillis() * averageHeartRateBPM + trackPointDuration.toMillis() * trackPoint.getHeartRate().getBPM()) / newTotalDuration.toMillis();
|
||||
averageHeartRateBPM = (totalHeartRateDuration.toMillis() * averageHeartRateBPM + trackPointDuration.toMillis() * trackPoint.heartRate().getBPM()) / newTotalDuration.toMillis();
|
||||
totalHeartRateDuration = newTotalDuration;
|
||||
|
||||
currentSegment.setAverageHeartRate(HeartRate.of(averageHeartRateBPM));
|
||||
}
|
||||
|
||||
// Update power
|
||||
if (trackPoint.hasPower() && lastTrackPoint != null) {
|
||||
if (trackPoint.power() != null && lastTrackPoint != null) {
|
||||
Duration trackPointDuration = Duration.between(lastTrackPoint.getTime(), trackPoint.getTime());
|
||||
Duration newTotalDuration = totalPowerDuration.plus(trackPointDuration);
|
||||
|
||||
averagePowerW = (totalPowerDuration.toMillis() * averagePowerW + trackPointDuration.toMillis() * trackPoint.getPower().getW()) / newTotalDuration.toMillis();
|
||||
averagePowerW = (totalPowerDuration.toMillis() * averagePowerW + trackPointDuration.toMillis() * trackPoint.power().getW()) / newTotalDuration.toMillis();
|
||||
totalPowerDuration = newTotalDuration;
|
||||
|
||||
currentSegment.setAveragePower(Power.of(averagePowerW));
|
||||
@@ -145,11 +145,11 @@ public class TrackStatisticsUpdater {
|
||||
{
|
||||
// Update total distance
|
||||
Distance movingDistance = null;
|
||||
if (trackPoint.hasSensorDistance()) {
|
||||
movingDistance = trackPoint.getSensorDistance();
|
||||
if (trackPoint.sensorDistance() != null) {
|
||||
movingDistance = trackPoint.sensorDistance();
|
||||
} else if (lastTrackPoint != null
|
||||
&& lastTrackPoint.hasLocation()
|
||||
&& trackPoint.hasLocation()) {
|
||||
&& lastTrackPoint.position().hasLocation()
|
||||
&& trackPoint.position().hasLocation()) {
|
||||
// GPS-based distance/speed
|
||||
movingDistance = trackPoint.distanceToPrevious(lastTrackPoint);
|
||||
}
|
||||
@@ -173,7 +173,7 @@ public class TrackStatisticsUpdater {
|
||||
}
|
||||
}
|
||||
|
||||
if (trackPoint.hasSpeed()) {
|
||||
if (trackPoint.position().hasSpeed()) {
|
||||
updateSpeed(trackPoint);
|
||||
}
|
||||
}
|
||||
@@ -205,7 +205,7 @@ public class TrackStatisticsUpdater {
|
||||
* Updates a speed reading while assuming the user is moving.
|
||||
*/
|
||||
private void updateSpeed(@NonNull TrackPoint trackPoint) {
|
||||
Speed currentSpeed = trackPoint.getSpeed();
|
||||
Speed currentSpeed = trackPoint.position().speed();
|
||||
if (currentSpeed.greaterThan(currentSegment.getMaxSpeed())) {
|
||||
currentSegment.setMaxSpeed(currentSpeed);
|
||||
}
|
||||
@@ -215,8 +215,8 @@ public class TrackStatisticsUpdater {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TrackStatisticsUpdater{" +
|
||||
"segmentStatisticUpdater=" + statisticsWithoutCurrentSegment +
|
||||
", currentSegment=" + currentSegment +
|
||||
"currentSegment=" + currentSegment +
|
||||
", segmentStatisticUpdater=" + statisticsWithoutCurrentSegment +
|
||||
", lastTrackPoint=" + lastTrackPoint +
|
||||
'}';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user