Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marker uses Position. #2063

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public static TrackData createTestingTrack(Track.Id trackId) {
stats.setTotalDistance(Distance.of(0));
stats.setTotalTime(Duration.ofMillis(0));
List<Marker> markers = List.of(
new Marker("Marker 1", "Marker description 1", "Marker category 3", "", trackId, trackPoints.get(1), null),
new Marker("Marker 2", "Marker description 2", "Marker category 3", "", trackId, trackPoints.get(4), null),
new Marker("Marker 3", "Marker description 3", "Marker category 3", "", trackId, trackPoints.get(5), null)
new Marker(trackId, trackPoints.get(1), "Marker 1", "Marker description 1", "Marker category 3", "", null),
new Marker(trackId, trackPoints.get(4), "Marker 2", "Marker description 2", "Marker category 3", "", null),
new Marker(trackId, trackPoints.get(5), "Marker 3", "Marker description 3", "Marker category 3", "", null)
);

return new TrackData(track, trackPoints, markers);
Expand Down Expand Up @@ -145,14 +145,13 @@ public static Marker createMarkerWithPhoto(Context context, Track.Id trackId, Tr
File dstFile = new File(MarkerUtils.getImageUrl(context, trackId));
dstFile.createNewFile();
Uri photoUri = FileUtils.getUriForFile(context, dstFile);
String photoUrl = photoUri.toString();

//TODO Use TrackStatisticsUpdater
TrackStatistics stats = new TrackStatistics();
stats.setTotalDistance(Distance.of(0));
stats.setTotalTime(Duration.ofMillis(0));

return new Marker("Marker name", "Marker description", "Marker category", "", trackId, trackPoint, photoUrl);
return new Marker(trackId, trackPoint, "Marker name", "Marker description", "Marker category", "", photoUri);
}

public static List<TrackPoint> getTrackPoints(ContentProviderUtils contentProviderUtils, Track.Id trackId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,40 +429,6 @@ public void testCreateContentValues_marker() {
assertEquals(TEST_DESC, contentValues.get(MarkerColumns.DESCRIPTION));
}

/**
* Tests the method {@link ContentProviderUtils#createMarker(Cursor)}.
*/
@Test
public void testCreateMarker() {
int startColumnIndex = 1;
int columnIndex = startColumnIndex;
when(cursorMock.getColumnIndexOrThrow(MarkerColumns._ID)).thenReturn(columnIndex++);
when(cursorMock.getColumnIndexOrThrow(MarkerColumns.NAME)).thenReturn(columnIndex++);
when(cursorMock.getColumnIndexOrThrow(MarkerColumns.TRACKID)).thenReturn(columnIndex++);
columnIndex = startColumnIndex;
// Id
when(cursorMock.isNull(columnIndex++)).thenReturn(false);
// Name
when(cursorMock.isNull(columnIndex++)).thenReturn(false);
// trackIdIndex
when(cursorMock.isNull(columnIndex++)).thenReturn(false);
long id = System.currentTimeMillis();
columnIndex = startColumnIndex;
// Id
when(cursorMock.getLong(columnIndex++)).thenReturn(id);
// Name
String name = NAME_PREFIX + id;
when(cursorMock.getString(columnIndex++)).thenReturn(name);
// trackIdIndex
long trackId = 11L;
when(cursorMock.getLong(columnIndex++)).thenReturn(trackId);

Marker marker = contentProviderUtils.createMarker(cursorMock);
assertEquals(id, marker.getId().id());
assertEquals(name, marker.getName());
assertEquals(trackId, marker.getTrackId().id());
}

/**
* Tests the method
* {@link ContentProviderUtils#deleteMarker(Context, Marker.Id)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.dennisguse.opentracks.io.file.importer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

Expand Down Expand Up @@ -139,7 +140,7 @@ public void setUp() throws TimeoutException {
Distance sensorDistance = Distance.of(10); // recording distance interval

sendLocation(trackPointCreator, "2020-02-02T02:02:03Z", 3, 14, 10, 13, 15, 10, 1f);
contentProviderUtils.insertMarker(new Marker("Marker 1", "Marker 1 desc", "Marker 1 category", null, trackId, service.getLastStoredTrackPointWithLocation(), ""));
contentProviderUtils.insertMarker(new Marker(trackId, service.getLastStoredTrackPointWithLocation(), "Marker 1", "Marker 1 desc", "Marker 1 category", null, null));

// A sensor-only TrackPoint
trackPointCreator.setClock("2020-02-02T02:02:04Z");
Expand All @@ -154,7 +155,7 @@ public void setUp() throws TimeoutException {
mockSensorData(trackPointCreator, 5f, Distance.of(2), 69f, 3f, 50f, null); // Distance will be added to next TrackPoint

sendLocation(trackPointCreator, "2020-02-02T02:02:17Z", 3, 14.001, 10, 13, 15, 10, 0f);
contentProviderUtils.insertMarker(new Marker("Marker 2", "Marker 2 desc", "Marker 2 category", null, trackId, service.getLastStoredTrackPointWithLocation(), ""));
contentProviderUtils.insertMarker(new Marker(trackId, service.getLastStoredTrackPointWithLocation(), "Marker 2", "Marker 2 desc", "Marker 2 category", null, null));

trackPointCreator.setClock("2020-02-02T02:02:18Z");
trackPointCreator.getSensorManager().sensorDataSet = new SensorDataSet(trackPointCreator);
Expand Down Expand Up @@ -537,11 +538,11 @@ private void assertMarkers() {
assertEquals(marker.getDescription(), importMarker.getDescription());
// assertEquals(marker.getIcon(), importMarker.getIcon()); // TODO for KML
assertEquals(marker.getName(), importMarker.getName());
assertEquals("", importMarker.getPhotoUrl());
assertFalse(importMarker.hasPhoto());

assertEquals(marker.getLocation().getLatitude(), importMarker.getLocation().getLatitude(), 0.001);
assertEquals(marker.getLocation().getLongitude(), importMarker.getLocation().getLongitude(), 0.001);
assertEquals(marker.getLocation().getAltitude(), importMarker.getLocation().getAltitude(), 0.1);
assertEquals(marker.getPosition().latitude(), importMarker.getPosition().latitude(), 0.001);
assertEquals(marker.getPosition().longitude(), importMarker.getPosition().longitude(), 0.001);
assertEquals(marker.getPosition().altitude().toM(), importMarker.getPosition().altitude().toM(), 0.1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.net.Uri;
import android.text.TextUtils;

Expand All @@ -46,6 +45,7 @@
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;
Expand Down Expand Up @@ -385,23 +385,39 @@ public Marker createMarker(Cursor cursor) {
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)));
}
Expand All @@ -418,8 +434,10 @@ public Marker createMarker(Cursor cursor) {
marker.setIcon(cursor.getString(iconIndex));
}
if (!cursor.isNull(photoUrlIndex)) {
marker.setPhotoUrl(cursor.getString(photoUrlIndex));
marker.setPhotoUrl(Uri.parse(cursor.getString(photoUrlIndex)));
}


return marker;
}

Expand Down Expand Up @@ -496,7 +514,7 @@ public Marker.Id insertMarker(@NonNull Marker marker) {

private void deleteMarkerPhoto(Context context, Marker marker) {
if (marker != null && marker.hasPhoto()) {
Uri uri = marker.getPhotoURI();
Uri uri = marker.getPhotoUrl();
File file = MarkerUtils.buildInternalPhotoFile(context, marker.getTrackId(), uri);
if (file.exists()) {
File parent = file.getParentFile();
Expand Down Expand Up @@ -532,8 +550,8 @@ ContentValues createContentValues(@NonNull Marker marker) {
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());
Expand All @@ -545,7 +563,9 @@ ContentValues createContentValues(@NonNull Marker marker) {
values.put(MarkerColumns.BEARING, marker.getBearing());
}

values.put(MarkerColumns.PHOTOURL, marker.getPhotoUrl());
if (marker.hasPhoto()) {
values.put(MarkerColumns.PHOTOURL, marker.getPhotoUrl().toString());
}
return values;
}

Expand Down Expand Up @@ -690,15 +710,11 @@ public TrackPoint.Id getLastTrackPointId(@NonNull Track.Id trackId) {

/**
* 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)));
Expand Down
Loading
Loading