forked from upstream-mirrors/OpenTracks
Position: use for Marker.
This commit is contained in:
@@ -21,7 +21,6 @@ import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.location.Location;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
|
||||
@@ -46,6 +45,7 @@ import de.dennisguse.opentracks.data.models.Cadence;
|
||||
import de.dennisguse.opentracks.data.models.Distance;
|
||||
import de.dennisguse.opentracks.data.models.HeartRate;
|
||||
import de.dennisguse.opentracks.data.models.Marker;
|
||||
import de.dennisguse.opentracks.data.models.Position;
|
||||
import de.dennisguse.opentracks.data.models.Power;
|
||||
import de.dennisguse.opentracks.data.models.Speed;
|
||||
import de.dennisguse.opentracks.data.models.Track;
|
||||
@@ -385,23 +385,39 @@ public class ContentProviderUtils {
|
||||
int bearingIndex = cursor.getColumnIndexOrThrow(MarkerColumns.BEARING);
|
||||
int photoUrlIndex = cursor.getColumnIndexOrThrow(MarkerColumns.PHOTOURL);
|
||||
|
||||
Track.Id trackId = new Track.Id(cursor.getLong(trackIdIndex));
|
||||
Marker marker = new Marker(trackId, Instant.ofEpochMilli(cursor.getLong(timeIndex)));
|
||||
|
||||
Double latitude = null;
|
||||
Double longitude = null;
|
||||
Altitude.WGS84 altitude = null;
|
||||
Distance horizontalAccuracy = null;
|
||||
Float bearing = null;
|
||||
if (!cursor.isNull(longitudeIndex) && !cursor.isNull(latitudeIndex)) {
|
||||
marker.setLongitude(((double) cursor.getInt(longitudeIndex)) / 1E6);
|
||||
marker.setLatitude(((double) cursor.getInt(latitudeIndex)) / 1E6);
|
||||
latitude = (((double) cursor.getInt(latitudeIndex)) / 1E6);
|
||||
longitude = (((double) cursor.getInt(longitudeIndex)) / 1E6);
|
||||
}
|
||||
if (!cursor.isNull(altitudeIndex)) {
|
||||
marker.setAltitude(Altitude.WGS84.of(cursor.getFloat(altitudeIndex)));
|
||||
altitude = Altitude.WGS84.of(cursor.getFloat(altitudeIndex));
|
||||
}
|
||||
if (!cursor.isNull(accuracyIndex)) {
|
||||
marker.setAccuracy(Distance.of(cursor.getFloat(accuracyIndex)));
|
||||
horizontalAccuracy = Distance.of(cursor.getFloat(accuracyIndex));
|
||||
}
|
||||
if (!cursor.isNull(bearingIndex)) {
|
||||
marker.setBearing(cursor.getFloat(bearingIndex));
|
||||
bearing = cursor.getFloat(bearingIndex);
|
||||
}
|
||||
|
||||
Position position = new Position(
|
||||
Instant.ofEpochMilli(cursor.getLong(timeIndex)),
|
||||
latitude,
|
||||
longitude,
|
||||
horizontalAccuracy,
|
||||
altitude,
|
||||
null,
|
||||
bearing,
|
||||
null);
|
||||
|
||||
Track.Id trackId = new Track.Id(cursor.getLong(trackIdIndex));
|
||||
Marker marker = new Marker(trackId, position);
|
||||
|
||||
if (!cursor.isNull(idIndex)) {
|
||||
marker.setId(new Marker.Id(cursor.getLong(idIndex)));
|
||||
}
|
||||
@@ -420,6 +436,8 @@ public class ContentProviderUtils {
|
||||
if (!cursor.isNull(photoUrlIndex)) {
|
||||
marker.setPhotoUrl(Uri.parse(cursor.getString(photoUrlIndex)));
|
||||
}
|
||||
|
||||
|
||||
return marker;
|
||||
}
|
||||
|
||||
@@ -532,8 +550,8 @@ public class ContentProviderUtils {
|
||||
values.put(MarkerColumns.CATEGORY, marker.getCategory());
|
||||
values.put(MarkerColumns.ICON, marker.getIcon());
|
||||
values.put(MarkerColumns.TRACKID, marker.getTrackId().id());
|
||||
values.put(MarkerColumns.LONGITUDE, (int) (marker.getLongitude() * 1E6));
|
||||
values.put(MarkerColumns.LATITUDE, (int) (marker.getLatitude() * 1E6));
|
||||
values.put(MarkerColumns.LONGITUDE, (int) (marker.getPosition().longitude() * 1E6));
|
||||
values.put(MarkerColumns.LATITUDE, (int) (marker.getPosition().latitude() * 1E6));
|
||||
values.put(MarkerColumns.TIME, marker.getTime().toEpochMilli());
|
||||
if (marker.hasAltitude()) {
|
||||
values.put(MarkerColumns.ALTITUDE, marker.getAltitude().toM());
|
||||
@@ -692,15 +710,11 @@ public class ContentProviderUtils {
|
||||
|
||||
/**
|
||||
* Gets the trackPoint id for a location.
|
||||
*
|
||||
* @param trackId the track id
|
||||
* @param location the location
|
||||
* @return trackPoint id if the location is in the track. -1L otherwise.
|
||||
*/
|
||||
@Deprecated
|
||||
public TrackPoint.Id getTrackPointId(Track.Id trackId, Location location) {
|
||||
public TrackPoint.Id getTrackPointId(Track.Id trackId, Position position) {
|
||||
String selection = TrackPointsColumns._ID + "=(SELECT MAX(" + TrackPointsColumns._ID + ") FROM " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns.TIME + "=?)";
|
||||
String[] selectionArgs = new String[]{Long.toString(trackId.id()), Long.toString(location.getTime())};
|
||||
String[] selectionArgs = new String[]{Long.toString(trackId.id()), Long.toString(position.time().toEpochMilli())};
|
||||
try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return new TrackPoint.Id(cursor.getLong(cursor.getColumnIndexOrThrow(TrackPointsColumns._ID)));
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package de.dennisguse.opentracks.data.models;
|
||||
|
||||
import android.location.Location;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
@@ -25,9 +24,10 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* NOTE: A marker is indirectly (via it's location) assigned to one {@link TrackPoint} with trackPoint.hasLocation() == true.
|
||||
* NOTE: A marker is indirectly (via it's {@link Position}) assigned to one {@link TrackPoint} via position.time.
|
||||
*
|
||||
* @author Leif Hendrik Wilden
|
||||
* @author Rodrigo Damazio
|
||||
@@ -42,34 +42,27 @@ public final class Marker {
|
||||
private String icon = "";
|
||||
private Track.Id trackId;
|
||||
|
||||
private final Instant time;
|
||||
private Double latitude;
|
||||
private Double longitude;
|
||||
@Deprecated //Not needed
|
||||
private Distance accuracy;
|
||||
private Altitude altitude;
|
||||
private Float bearing;
|
||||
//Some data might not be used.
|
||||
private final Position position;
|
||||
|
||||
private Uri photoUrl = null;
|
||||
|
||||
public Marker(@Nullable Track.Id trackId, Instant time) {
|
||||
this.trackId = trackId;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public Marker(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint) {
|
||||
this.trackId = trackId;
|
||||
|
||||
this.time = trackPoint.getTime();
|
||||
|
||||
if (!trackPoint.hasLocation())
|
||||
throw new RuntimeException("Marker requires a trackpoint with a location.");
|
||||
|
||||
setTrackPoint(trackPoint);
|
||||
this.position = trackPoint.getPosition();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Marker(String name, String description, String category, String icon, @NonNull Track.Id trackId, @NonNull TrackPoint trackPoint, Uri photoUrl) {
|
||||
public Marker(@Nullable Track.Id trackId, @NonNull Position position) {
|
||||
this.trackId = trackId;
|
||||
Objects.requireNonNull(position);
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public Marker(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint, String name, String description, String category, String icon, Uri photoUrl) {
|
||||
this(trackId, trackPoint);
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
@@ -78,17 +71,8 @@ public final class Marker {
|
||||
this.photoUrl = photoUrl;
|
||||
}
|
||||
|
||||
//TODO Is somehow part of the initialization process. Can we at least limit visibility?
|
||||
public void setTrackPoint(TrackPoint trackPoint) {
|
||||
this.latitude = trackPoint.getLatitude();
|
||||
this.longitude = trackPoint.getLongitude();
|
||||
if (trackPoint.hasHorizontalAccuracy()) this.accuracy = trackPoint.getHorizontalAccuracy();
|
||||
if (trackPoint.hasAltitude()) this.altitude = trackPoint.getAltitude();
|
||||
if (trackPoint.hasBearing()) this.bearing = trackPoint.getBearing();
|
||||
}
|
||||
|
||||
/**
|
||||
* May be null if the it was not loaded from the database.
|
||||
* May be null if the Marker was not loaded from the database.
|
||||
*/
|
||||
@Nullable
|
||||
public Id getId() {
|
||||
@@ -100,7 +84,7 @@ public final class Marker {
|
||||
}
|
||||
|
||||
public Instant getTime() {
|
||||
return time;
|
||||
return position.time();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -144,80 +128,40 @@ public final class Marker {
|
||||
this.trackId = trackId;
|
||||
}
|
||||
|
||||
public boolean hasLocation() {
|
||||
return latitude != null || longitude != null;
|
||||
}
|
||||
|
||||
public Location getLocation() {
|
||||
Location location = new Location("");
|
||||
location.setTime(time.toEpochMilli());
|
||||
if (hasLocation()) {
|
||||
location.setLatitude(latitude);
|
||||
location.setLongitude(longitude);
|
||||
}
|
||||
if (hasBearing()) {
|
||||
location.setBearing(bearing);
|
||||
}
|
||||
if (hasAccuracy()) {
|
||||
location.setAccuracy((float) accuracy.toM());
|
||||
}
|
||||
if (hasAltitude()) {
|
||||
location.setAltitude(altitude.toM());
|
||||
}
|
||||
|
||||
return location;
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
return position.latitude();
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
return position.longitude();
|
||||
}
|
||||
|
||||
public boolean hasAccuracy() {
|
||||
return accuracy != null;
|
||||
return position.hasHorizontalAccuracy();
|
||||
}
|
||||
|
||||
public Distance getAccuracy() {
|
||||
return accuracy;
|
||||
}
|
||||
|
||||
public void setAccuracy(Distance accuracy) {
|
||||
this.accuracy = accuracy;
|
||||
return position.horizontalAccuracy();
|
||||
}
|
||||
|
||||
public boolean hasAltitude() {
|
||||
return altitude != null;
|
||||
return position.hasAltitude();
|
||||
}
|
||||
|
||||
public Altitude getAltitude() {
|
||||
return altitude;
|
||||
}
|
||||
|
||||
public void setAltitude(Altitude altitude) {
|
||||
this.altitude = altitude;
|
||||
return position.altitude();
|
||||
}
|
||||
|
||||
public boolean hasBearing() {
|
||||
return bearing != null;
|
||||
return position.hasBearing();
|
||||
}
|
||||
|
||||
public Float getBearing() {
|
||||
return bearing;
|
||||
}
|
||||
|
||||
public void setBearing(float bearing) {
|
||||
this.bearing = bearing;
|
||||
return position.bearing();
|
||||
}
|
||||
|
||||
public Uri getPhotoUrl() {
|
||||
|
||||
@@ -353,6 +353,13 @@ public class TrackPoint {
|
||||
return hasHorizontalAccuracy() && horizontalAccuracy.lessThan(thresholdHorizontalAccuracy);
|
||||
}
|
||||
|
||||
public Optional<Float> bearingTo(@NonNull Position dest) {
|
||||
if (!dest.hasLocation() || !hasLocation()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return bearingTo(dest.toLocation());
|
||||
}
|
||||
|
||||
public Optional<Float> bearingTo(@NonNull TrackPoint dest) {
|
||||
if (!dest.hasLocation() || !hasLocation()) {
|
||||
return Optional.empty();
|
||||
|
||||
Reference in New Issue
Block a user