forked from upstream-mirrors/OpenTracks
Position: use for TrackPoint.
This commit is contained in:
@@ -622,31 +622,19 @@ public class ContentProviderUtils {
|
||||
* @param indexes the cached trackPoints indexes
|
||||
*/
|
||||
static TrackPoint fillTrackPoint(Cursor cursor, CachedTrackPointsIndexes indexes) {
|
||||
Instant time = Instant.ofEpochMilli(cursor.getLong(indexes.timeIndex));
|
||||
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.getById(cursor.getInt(indexes.typeIndex)), time);
|
||||
trackPoint.setId(new TrackPoint.Id(cursor.getInt(indexes.idIndex)));
|
||||
|
||||
if (!cursor.isNull(indexes.longitudeIndex)) {
|
||||
trackPoint.setLongitude(((double) cursor.getInt(indexes.longitudeIndex)) / 1E6);
|
||||
}
|
||||
if (!cursor.isNull(indexes.latitudeIndex)) {
|
||||
trackPoint.setLatitude(((double) cursor.getInt(indexes.latitudeIndex)) / 1E6);
|
||||
}
|
||||
if (!cursor.isNull(indexes.altitudeIndex)) {
|
||||
trackPoint.setAltitude(Altitude.WGS84.of(cursor.getFloat(indexes.altitudeIndex)));
|
||||
}
|
||||
if (!cursor.isNull(indexes.accuracyIndex)) {
|
||||
trackPoint.setHorizontalAccuracy(Distance.of(cursor.getFloat(indexes.accuracyIndex)));
|
||||
}
|
||||
if (!cursor.isNull(indexes.accuracyVerticalIndex)) {
|
||||
trackPoint.setVerticalAccuracy(Distance.of(cursor.getFloat(indexes.accuracyVerticalIndex)));
|
||||
}
|
||||
if (!cursor.isNull(indexes.speedIndex)) {
|
||||
trackPoint.setSpeed(Speed.of(cursor.getFloat(indexes.speedIndex)));
|
||||
}
|
||||
if (!cursor.isNull(indexes.bearingIndex)) {
|
||||
trackPoint.setBearing(cursor.getFloat(indexes.bearingIndex));
|
||||
}
|
||||
TrackPoint trackPoint = new TrackPoint(
|
||||
new TrackPoint.Id(cursor.getInt(indexes.idIndex)),
|
||||
TrackPoint.Type.getById(cursor.getInt(indexes.typeIndex)),
|
||||
new Position(
|
||||
Instant.ofEpochMilli(cursor.getLong(indexes.timeIndex)),
|
||||
!cursor.isNull(indexes.latitudeIndex) ? ((double) cursor.getInt(indexes.latitudeIndex)) / 1E6 : null,
|
||||
!cursor.isNull(indexes.longitudeIndex) ? ((double) cursor.getInt(indexes.longitudeIndex)) / 1E6 : null,
|
||||
!cursor.isNull(indexes.accuracyIndex) ? Distance.of(cursor.getFloat(indexes.accuracyIndex)) : null,
|
||||
!cursor.isNull(indexes.altitudeIndex) ? Altitude.WGS84.of(cursor.getFloat(indexes.altitudeIndex)) : null,
|
||||
!cursor.isNull(indexes.accuracyVerticalIndex) ? Distance.of(cursor.getFloat(indexes.accuracyVerticalIndex)) : null,
|
||||
!cursor.isNull(indexes.bearingIndex) ? cursor.getFloat(indexes.bearingIndex) : null,
|
||||
!cursor.isNull(indexes.speedIndex) ? Speed.of(cursor.getFloat(indexes.speedIndex)) : null
|
||||
));
|
||||
|
||||
if (!cursor.isNull(indexes.sensorHeartRateIndex)) {
|
||||
trackPoint.setHeartRate(cursor.getFloat(indexes.sensorHeartRateIndex));
|
||||
|
||||
@@ -2,22 +2,29 @@ package de.dennisguse.opentracks.data.models;
|
||||
|
||||
import android.location.Location;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
//TODO Use everywhere instead of android.location.Location.
|
||||
public record Position(
|
||||
Instant time,
|
||||
Double latitude,
|
||||
Double longitude,
|
||||
Distance horizontalAccuracy,
|
||||
Altitude altitude,
|
||||
Distance verticalAccuracy,
|
||||
Float bearing,
|
||||
Speed speed
|
||||
@Nullable Instant time,
|
||||
@Nullable Double latitude,
|
||||
@Nullable Double longitude,
|
||||
@Nullable Distance horizontalAccuracy,
|
||||
@Nullable Altitude altitude,
|
||||
@Nullable Distance verticalAccuracy,
|
||||
@Nullable Float bearing,
|
||||
@Nullable Speed speed
|
||||
) {
|
||||
public static Position empty() {
|
||||
return of((Instant) null);
|
||||
}
|
||||
|
||||
public static Position of(@NonNull Instant time) {
|
||||
return new Position(
|
||||
null,
|
||||
time,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
@@ -28,9 +35,13 @@ public record Position(
|
||||
);
|
||||
}
|
||||
|
||||
public static Position of(Location location) {
|
||||
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(
|
||||
Instant.ofEpochMilli(location.getTime()),
|
||||
time,
|
||||
location.getLatitude(),
|
||||
location.getLongitude(),
|
||||
location.hasAccuracy() ? Distance.of(location.getAccuracy()) : null,
|
||||
@@ -53,6 +64,10 @@ public record Position(
|
||||
return altitude != null;
|
||||
}
|
||||
|
||||
public boolean hasVerticalAccuracy() {
|
||||
return verticalAccuracy != null;
|
||||
}
|
||||
|
||||
public boolean hasBearing() {
|
||||
return bearing != null;
|
||||
}
|
||||
@@ -67,7 +82,9 @@ public record Position(
|
||||
}
|
||||
|
||||
Location location = new Location("");
|
||||
location.setTime(time.toEpochMilli());
|
||||
if (time != null) {
|
||||
location.setTime(time.toEpochMilli());
|
||||
}
|
||||
location.setLatitude(latitude);
|
||||
location.setLongitude(longitude);
|
||||
if (hasBearing()) {
|
||||
@@ -103,4 +120,34 @@ public record Position(
|
||||
.lessThan(thresholdHorizontalAccuracy);
|
||||
|
||||
}
|
||||
|
||||
public Position with(Instant time) {
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
|
||||
public Position with(Altitude altitude) {
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
|
||||
public Position with(Speed speed) {
|
||||
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);
|
||||
}
|
||||
|
||||
//TODO Use float
|
||||
public Position withBearing(Float bearing) {
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
|
||||
public Position withHorizontalAccuracy(Distance horizontalAccuracy) {
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
|
||||
public Position withVerticalAccuracy(Distance verticalAccuracy) {
|
||||
return new Position(time, latitude, longitude, horizontalAccuracy, altitude, verticalAccuracy, bearing, speed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,20 +38,12 @@ public class TrackPoint {
|
||||
|
||||
private static final Duration MAX_LOCATION_AGE = Duration.ofMinutes(1);
|
||||
|
||||
@Nullable
|
||||
private TrackPoint.Id id;
|
||||
|
||||
//Requires: position.time must be non-null
|
||||
@NonNull
|
||||
private final Instant time;
|
||||
|
||||
//TODO We may use Position for these items
|
||||
private Double latitude;
|
||||
private Double longitude;
|
||||
private Distance horizontalAccuracy;
|
||||
private Distance verticalAccuracy;
|
||||
private Altitude altitude; //TODO use Altitude.WGS84
|
||||
private Speed speed;
|
||||
private Float bearing;
|
||||
private Distance sensorDistance;
|
||||
private Position position;
|
||||
|
||||
public enum Type {
|
||||
SEGMENT_START_MANUAL(-2), //Start of a segment due to user interaction (start, resume)
|
||||
@@ -73,6 +65,7 @@ public class TrackPoint {
|
||||
this.type_db = type_db;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return name() + "(" + type_db + ")";
|
||||
@@ -90,33 +83,55 @@ public class TrackPoint {
|
||||
@NonNull
|
||||
private Type type;
|
||||
|
||||
private Distance sensorDistance;
|
||||
private HeartRate heartRate = null;
|
||||
private Cadence cadence = null;
|
||||
private Power power = null;
|
||||
private Float altitudeGain_m = null;
|
||||
private Float altitudeLoss_m = null;
|
||||
|
||||
public TrackPoint(@NonNull Type type, @NonNull Instant time) {
|
||||
public TrackPoint(@Nullable TrackPoint.Id id, @NonNull Type type, @NonNull Position position) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.time = time;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
//TODO Refactor constructors
|
||||
public TrackPoint(@NonNull Type type, @NonNull Instant time) {
|
||||
this.type = type;
|
||||
this.position = Position.of(time);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint(@NonNull Location location, @NonNull Instant time) {
|
||||
this(Type.TRACKPOINT, location, 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, time);
|
||||
|
||||
setLocation(location);
|
||||
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.TRACKPOINT, time);
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
this.altitude = altitude;
|
||||
this.type = Type.TRACKPOINT;
|
||||
this.position = new Position(
|
||||
time,
|
||||
latitude,
|
||||
longitude,
|
||||
null,
|
||||
altitude,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
public static TrackPoint createSegmentStartManualWithTime(Instant time) {
|
||||
@@ -161,54 +176,46 @@ public class TrackPoint {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(TrackPoint.Id id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean hasLocation() {
|
||||
return latitude != null && longitude != null;
|
||||
return position.hasLocation();
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
//TODO Should be double
|
||||
@Deprecated //Use getPosition()
|
||||
public Double getLatitude() {
|
||||
return position.latitude();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
this.position = position.withCoordinates(latitude, getLongitude());
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
//TODO Should be double
|
||||
@Deprecated //Use getPosition()
|
||||
public Double getLongitude() {
|
||||
return position.longitude();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint setLongitude(double longitude) {
|
||||
this.longitude = 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 new Position(
|
||||
time,
|
||||
latitude,
|
||||
longitude,
|
||||
horizontalAccuracy,
|
||||
altitude,
|
||||
verticalAccuracy,
|
||||
bearing,
|
||||
speed
|
||||
);
|
||||
return position;
|
||||
}
|
||||
|
||||
public TrackPoint setPosition(Position location) {
|
||||
this.latitude = location.latitude();
|
||||
this.longitude = location.longitude();
|
||||
this.altitude = location.altitude();
|
||||
this.speed = location.speed();
|
||||
this.horizontalAccuracy = location.horizontalAccuracy();
|
||||
this.verticalAccuracy = location.verticalAccuracy();
|
||||
|
||||
public TrackPoint setPosition(Position position) {
|
||||
this.position = position.with(this.position.time());
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -218,12 +225,6 @@ public class TrackPoint {
|
||||
return getPosition().toLocation();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public TrackPoint setLocation(@NonNull Location location) {
|
||||
setPosition(Position.of(location));
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasAltitudeGain() {
|
||||
return altitudeGain_m != null;
|
||||
}
|
||||
@@ -252,83 +253,77 @@ public class TrackPoint {
|
||||
|
||||
@NonNull
|
||||
public Instant getTime() {
|
||||
return time;
|
||||
return position.time();
|
||||
}
|
||||
|
||||
public boolean isRecent() {
|
||||
return Instant.now()
|
||||
.isBefore(time.plus(MAX_LOCATION_AGE));
|
||||
}
|
||||
|
||||
|
||||
public boolean hasAltitude() {
|
||||
return altitude != null;
|
||||
return position.hasAltitude();
|
||||
}
|
||||
|
||||
public Altitude getAltitude() {
|
||||
return altitude;
|
||||
return position.altitude();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public TrackPoint setAltitude(double altitude_m) {
|
||||
this.altitude = Altitude.WGS84.of(altitude_m);
|
||||
setAltitude(Altitude.WGS84.of(altitude_m));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TrackPoint setAltitude(Altitude altitude) {
|
||||
this.altitude = altitude;
|
||||
position = position.with(altitude);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasSpeed() {
|
||||
return speed != null;
|
||||
return position.hasSpeed();
|
||||
}
|
||||
|
||||
public Speed getSpeed() {
|
||||
return speed;
|
||||
return position.speed();
|
||||
}
|
||||
|
||||
public TrackPoint setSpeed(Speed speed) {
|
||||
this.speed = speed;
|
||||
this.position = position.with(speed);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasBearing() {
|
||||
return bearing != null;
|
||||
return position.hasBearing();
|
||||
}
|
||||
|
||||
public float getBearing() {
|
||||
return bearing;
|
||||
return position.bearing();
|
||||
}
|
||||
|
||||
public TrackPoint setBearing(Float bearing) {
|
||||
this.bearing = bearing;
|
||||
this.position = this.position.withBearing(bearing);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasHorizontalAccuracy() {
|
||||
return horizontalAccuracy != null;
|
||||
return position.hasHorizontalAccuracy();
|
||||
}
|
||||
|
||||
public Distance getHorizontalAccuracy() {
|
||||
return horizontalAccuracy;
|
||||
return position.horizontalAccuracy();
|
||||
}
|
||||
|
||||
public TrackPoint setHorizontalAccuracy(Distance horizontalAccuracy) {
|
||||
this.horizontalAccuracy = horizontalAccuracy;
|
||||
this.position = this.position.withHorizontalAccuracy(horizontalAccuracy);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasVerticalAccuracy() {
|
||||
return verticalAccuracy != null;
|
||||
return position.hasVerticalAccuracy();
|
||||
}
|
||||
|
||||
public Distance getVerticalAccuracy() {
|
||||
return verticalAccuracy;
|
||||
return position.verticalAccuracy();
|
||||
}
|
||||
|
||||
public TrackPoint setVerticalAccuracy(Distance horizontalAccuracy) {
|
||||
this.verticalAccuracy = horizontalAccuracy;
|
||||
public TrackPoint setVerticalAccuracy(Distance verticalAccuracy) {
|
||||
this.position = this.position.withVerticalAccuracy(verticalAccuracy);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -350,7 +345,7 @@ public class TrackPoint {
|
||||
}
|
||||
|
||||
public boolean fulfillsAccuracy(Distance thresholdHorizontalAccuracy) {
|
||||
return hasHorizontalAccuracy() && horizontalAccuracy.lessThan(thresholdHorizontalAccuracy);
|
||||
return position.fulfillsAccuracy(thresholdHorizontalAccuracy);
|
||||
}
|
||||
|
||||
public Optional<Float> bearingTo(@NonNull Position dest) {
|
||||
@@ -459,14 +454,7 @@ public class TrackPoint {
|
||||
public String toString() {
|
||||
return "TrackPoint{" +
|
||||
"id=" + id +
|
||||
", time=" + time +
|
||||
", latitude=" + latitude +
|
||||
", longitude=" + longitude +
|
||||
", horizontalAccuracy=" + horizontalAccuracy +
|
||||
", verticalAccuracy=" + verticalAccuracy +
|
||||
", altitude=" + altitude +
|
||||
", speed=" + speed +
|
||||
", bearing=" + bearing +
|
||||
", position=" + position +
|
||||
", sensorDistance=" + sensorDistance +
|
||||
", type=" + type +
|
||||
", heartRate=" + heartRate +
|
||||
|
||||
Reference in New Issue
Block a user