Skip to content

Commit

Permalink
Java Record for Track.Id and TrackPoint.Id.
Browse files Browse the repository at this point in the history
Part of #1526.
  • Loading branch information
dennisguse committed Jul 25, 2023
1 parent 252f71e commit a68f2ab
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TestDataUtil {
public static Track createTrack(Track.Id trackId) {
Track track = new Track();
track.setId(trackId);
track.setName("Test: " + trackId.getId());
track.setName("Test: " + trackId.id());

return track;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void testIterator(Track.Id trackId, int numPoints) {
assertNotNull(trackPoint);
locations.add(trackPoint);
// Make sure the IDs are returned in the right order.
assertEquals(lastPointId.getId() - numPoints + locations.size(), trackPoint.getId().getId());
assertEquals(lastPointId.id() - numPoints + locations.size(), trackPoint.getId().id());
}
assertEquals(numPoints, locations.size());
}
Expand All @@ -128,7 +128,7 @@ private void testIterator(Track.Id trackId, int numPoints) {
private TrackPoint.Id initializeTrack(Track.Id id, int numPoints) {
Track track = new Track();
track.setId(id);
track.setName("Test: " + id.getId());
track.setName("Test: " + id.id());
contentProviderUtils.insertTrack(track);
track = contentProviderUtils.getTrack(id);
assertNotNull(track);
Expand All @@ -155,7 +155,7 @@ private TrackPoint.Id initializeTrack(Track.Id id, int numPoints) {
}
}

assertTrue(numPoints == 0 || lastPointId.getId() > 0);
assertTrue(numPoints == 0 || lastPointId.id() > 0);
assertEquals(numPoints, counter);

return lastPointId;
Expand All @@ -172,7 +172,7 @@ public void testCreateTrack() {
// Id
when(cursorMock.getColumnIndexOrThrow(TracksColumns._ID)).thenReturn(columnIndex);
when(cursorMock.isNull(columnIndex)).thenReturn(false);
when(cursorMock.getLong(columnIndex)).thenReturn(trackId.getId());
when(cursorMock.getLong(columnIndex)).thenReturn(trackId.id());

//Uuid
columnIndex++;
Expand All @@ -182,7 +182,7 @@ public void testCreateTrack() {

// Name
columnIndex++;
String name = NAME_PREFIX + trackId.getId();
String name = NAME_PREFIX + trackId.id();
when(cursorMock.getColumnIndexOrThrow(TracksColumns.NAME)).thenReturn(columnIndex);
when(cursorMock.isNull(columnIndex)).thenReturn(false);
when(cursorMock.getString(columnIndex)).thenReturn(name);
Expand Down Expand Up @@ -460,7 +460,7 @@ public void testCreateMarker() {
Marker marker = contentProviderUtils.createMarker(cursorMock);
assertEquals(id, marker.getId().id());
assertEquals(name, marker.getName());
assertEquals(trackId, marker.getTrackId().getId());
assertEquals(trackId, marker.getTrackId().id());
}

/**
Expand Down Expand Up @@ -640,7 +640,7 @@ public void testUpdateMarker_withPhoto() throws IOException {
marker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));

File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.getId());
File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.id());
assertTrue(dir.exists());
assertTrue(dir.isDirectory());
assertEquals(1, dir.list().length);
Expand Down Expand Up @@ -675,7 +675,7 @@ public void testUpdateMarker_delPhotoAndDir() throws IOException {
marker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));

File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.getId());
File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.id());
assertTrue(dir.exists());
assertTrue(dir.isDirectory());
assertEquals(1, dir.list().length);
Expand Down Expand Up @@ -712,7 +712,7 @@ public void testUpdateMarker_delPhotoNotDir() throws IOException {
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));
contentProviderUtils.insertMarker(otherMarker);

File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.getId());
File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.id());
assertTrue(dir.exists());
assertTrue(dir.isDirectory());
assertEquals(2, dir.list().length);
Expand Down Expand Up @@ -889,7 +889,7 @@ public void testGetTrackPointLocationIterator_asc() {
for (int i = 0; i < trackpointIds.size(); i++) {
assertTrue(trackPointIterator.hasNext());
TrackPoint trackPoint = trackPointIterator.next();
assertEquals(startTrackPointId.getId() + i, trackPoint.getId().getId());
assertEquals(startTrackPointId.id() + i, trackPoint.getId().id());

checkLocation(i, trackPoint.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public void testTrackName_date_iso_8601() {
@Test
public void testTrackName_number() {
PreferencesUtils.setString(R.string.track_name_key, CONTEXT.getString(R.string.settings_recording_track_name_number_value));
assertEquals("Track " + TRACK_ID.getId(), TrackNameUtils.getTrackName(CONTEXT, TRACK_ID, START_TIME));
assertEquals("Track " + TRACK_ID.id(), TrackNameUtils.getTrackName(CONTEXT, TRACK_ID, START_TIME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void onCreate(Bundle bundle) {
contentProviderUtils = new ContentProviderUtils(this);
track = contentProviderUtils.getTrack(trackId);
if (track == null) {
Log.e(TAG, "No track for " + trackId.getId());
Log.e(TAG, "No track for " + trackId.id());
finish();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ public void deleteTracks(Context context, @NonNull List<Track.Id> trackIds) {
}

String whereClause = String.format(TracksColumns._ID + " IN (%s)", TextUtils.join(",", Collections.nCopies(trackIds.size(), "?")));
contentResolver.delete(TracksColumns.CONTENT_URI, whereClause, trackIds.stream().map(id -> Long.toString(id.getId())).toArray(String[]::new));
contentResolver.delete(TracksColumns.CONTENT_URI, whereClause, trackIds.stream().map(trackId -> Long.toString(trackId.id())).toArray(String[]::new));
}

public void deleteTrack(Context context, @NonNull Track.Id trackId) {
// Delete track folder resources.
FileUtils.deleteDirectoryRecurse(FileUtils.getPhotoDir(context, trackId));
contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", new String[]{Long.toString(trackId.getId())});
contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", new String[]{Long.toString(trackId.id())});
}

//TODO Only use for tests; also move to tests.
Expand Down Expand Up @@ -229,7 +229,7 @@ public List<Track> getTracks(ContentProviderSelectionInterface selection) {
}

public Track getTrack(@NonNull Track.Id trackId) {
try (Cursor cursor = getTrackCursor(TracksColumns._ID + "=?", new String[]{Long.toString(trackId.getId())}, null)) {
try (Cursor cursor = getTrackCursor(TracksColumns._ID + "=?", new String[]{Long.toString(trackId.id())}, null)) {
if (cursor != null && cursor.moveToNext()) {
return createTrack(cursor);
}
Expand Down Expand Up @@ -278,15 +278,15 @@ public Track.Id insertTrack(Track track) {
* @param track the track
*/
public void updateTrack(Track track) {
contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(track), TracksColumns._ID + "=?", new String[]{Long.toString(track.getId().getId())});
contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(track), TracksColumns._ID + "=?", new String[]{Long.toString(track.getId().id())});
}

private ContentValues createContentValues(Track track) {
ContentValues values = new ContentValues();
TrackStatistics trackStatistics = track.getTrackStatistics();

if (track.getId() != null) {
values.put(TracksColumns._ID, track.getId().getId());
values.put(TracksColumns._ID, track.getId().id());
}
values.put(TracksColumns.UUID, UUIDUtils.toBytes(track.getUuid()));
values.put(TracksColumns.NAME, track.getName());
Expand Down Expand Up @@ -315,7 +315,7 @@ private ContentValues createContentValues(Track track) {
}

public void updateTrackStatistics(@NonNull Track.Id trackId, @NonNull TrackStatistics trackStatistics) {
contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(trackStatistics), TracksColumns._ID + "=?", new String[]{Long.toString(trackId.getId())});
contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(trackStatistics), TracksColumns._ID + "=?", new String[]{Long.toString(trackId.id())});
}

private ContentValues createContentValues(TrackStatistics trackStatistics) {
Expand Down Expand Up @@ -413,7 +413,7 @@ public void deleteMarker(Context context, Marker.Id markerId) {
public Integer getNextMarkerNumber(@NonNull Track.Id trackId) {
String[] projection = {MarkerColumns._ID};
String selection = MarkerColumns.TRACKID + "=?";
String[] selectionArgs = new String[]{Long.toString(trackId.getId())};
String[] selectionArgs = new String[]{Long.toString(trackId.id())};
try (Cursor cursor = getMarkerCursor(projection, selection, selectionArgs, MarkerColumns._ID, -1)) {
if (cursor != null) {
return cursor.getCount();
Expand Down Expand Up @@ -443,10 +443,10 @@ public Cursor getMarkerCursor(@NonNull Track.Id trackId, @Nullable Marker.Id min
String[] selectionArgs;
if (minMarkerId != null) {
selection = MarkerColumns.TRACKID + "=? AND " + MarkerColumns._ID + ">=?";
selectionArgs = new String[]{Long.toString(trackId.getId()), Long.toString(minMarkerId.id())};
selectionArgs = new String[]{Long.toString(trackId.id()), Long.toString(minMarkerId.id())};
} else {
selection = MarkerColumns.TRACKID + "=?";
selectionArgs = new String[]{Long.toString(trackId.getId())};
selectionArgs = new String[]{Long.toString(trackId.id())};
}
return getMarkerCursor(null, selection, selectionArgs, MarkerColumns._ID, maxCount);
}
Expand Down Expand Up @@ -510,7 +510,7 @@ ContentValues createContentValues(@NonNull Marker marker) {
values.put(MarkerColumns.DESCRIPTION, marker.getDescription());
values.put(MarkerColumns.CATEGORY, marker.getCategory());
values.put(MarkerColumns.ICON, marker.getIcon());
values.put(MarkerColumns.TRACKID, marker.getTrackId().getId());
values.put(MarkerColumns.TRACKID, marker.getTrackId().id());
values.put(MarkerColumns.LENGTH, marker.getLength().toM());
values.put(MarkerColumns.DURATION, marker.getDuration().toMillis());

Expand Down Expand Up @@ -632,7 +632,7 @@ public int bulkInsertMarkers(List<Marker> markers, Track.Id trackId) {
@Deprecated
public TrackPoint.Id getLastTrackPointId(@NonNull Track.Id trackId) {
String selection = TrackPointsColumns._ID + "=(SELECT MAX(" + TrackPointsColumns._ID + ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=?)";
String[] selectionArgs = new String[]{Long.toString(trackId.getId())};
String[] selectionArgs = new String[]{Long.toString(trackId.id())};
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 All @@ -651,7 +651,7 @@ public TrackPoint.Id getLastTrackPointId(@NonNull Track.Id trackId) {
@Deprecated
public TrackPoint.Id getTrackPointId(Track.Id trackId, Location location) {
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.getId()), Long.toString(location.getTime())};
String[] selectionArgs = new String[]{Long.toString(trackId.id()), Long.toString(location.getTime())};
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 Expand Up @@ -681,10 +681,10 @@ public Cursor getTrackPointCursor(@NonNull Track.Id trackId, TrackPoint.Id start
String[] selectionArgs;
if (startTrackPointId != null) {
selection = TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns._ID + ">=?";
selectionArgs = new String[]{Long.toString(trackId.getId()), Long.toString(startTrackPointId.getId())};
selectionArgs = new String[]{Long.toString(trackId.id()), Long.toString(startTrackPointId.id())};
} else {
selection = TrackPointsColumns.TRACKID + "=?";
selectionArgs = new String[]{Long.toString(trackId.getId())};
selectionArgs = new String[]{Long.toString(trackId.id())};
}

return getTrackPointCursor(null, selection, selectionArgs, TrackPointsColumns.DEFAULT_SORT_ORDER);
Expand All @@ -699,7 +699,7 @@ public Cursor getTrackPointCursor(@NonNull Track.Id trackId, TrackPoint.Id start
@Deprecated
public TrackPoint getLastValidTrackPoint(Track.Id trackId) {
String selection = TrackPointsColumns._ID + "=(SELECT MAX(" + TrackPointsColumns._ID + ") FROM " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns.TYPE + " IN (" + TrackPoint.Type.SEGMENT_START_AUTOMATIC.type_db + "," + TrackPoint.Type.TRACKPOINT.type_db + "))";
String[] selectionArgs = new String[]{Long.toString(trackId.getId())};
String[] selectionArgs = new String[]{Long.toString(trackId.id())};
return findTrackPointBy(selection, selectionArgs);
}

Expand All @@ -722,7 +722,7 @@ public Uri insertTrackPoint(TrackPoint trackPoint, Track.Id trackId) {
*/
private ContentValues createContentValues(TrackPoint trackPoint, Track.Id trackId) {
ContentValues values = new ContentValues();
values.put(TrackPointsColumns.TRACKID, trackId.getId());
values.put(TrackPointsColumns.TRACKID, trackId.id());
values.put(TrackPointsColumns.TYPE, trackPoint.getType().type_db);

if (trackPoint.hasLocation()) {
Expand Down Expand Up @@ -805,7 +805,7 @@ private Cursor getTrackPointCursor(String[] projection, String selection, String
public static String formatIdListForUri(Track.Id... trackIds) {
long[] ids = new long[trackIds.length];
for (int i = 0; i < trackIds.length; i++) {
ids[i] = trackIds[i].getId();
ids[i] = trackIds[i].id();
}

return formatIdListForUri(ids);
Expand Down Expand Up @@ -834,7 +834,7 @@ public static String[] parseTrackIdsFromUri(Uri url) {

public SensorStatistics getSensorStats(@NonNull Track.Id trackId) {
SensorStatistics sensorStatistics = null;
try (Cursor cursor = contentResolver.query(ContentUris.withAppendedId(TracksColumns.CONTENT_URI_SENSOR_STATS, trackId.getId()), null, null, null, null)) {
try (Cursor cursor = contentResolver.query(ContentUris.withAppendedId(TracksColumns.CONTENT_URI_SENSOR_STATS, trackId.id()), null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
final int MAX_HR_INDEX = cursor.getColumnIndexOrThrow(TrackPointsColumns.ALIAS_MAX_HR);
final int AVG_HR_INDEX = cursor.getColumnIndexOrThrow(TrackPointsColumns.ALIAS_AVG_HR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void upgradeFrom25to26(SQLiteDatabase db) {
Track.Id trackId = new Track.Id(cursor.getLong(trackIdIndex));
ContentValues cv = new ContentValues();
cv.put("uuid", UUIDUtils.toBytes(UUID.randomUUID()));
db.update("tracks", cv, "_id = ?", new String[]{String.valueOf(trackId.getId())});
db.update("tracks", cv, "_id = ?", new String[]{String.valueOf(trackId.id())});
} while (cursor.moveToNext());
}
}
Expand Down Expand Up @@ -450,7 +450,7 @@ private void upgradeFrom32to33(SQLiteDatabase db) {

ContentValues cv = new ContentValues();
cv.put("starttime_offset", zoneRules.getOffset(Instant.ofEpochMilli(startTime)).getTotalSeconds());
db.update("tracks", cv, "_id = ?", new String[]{String.valueOf(trackId.getId())});
db.update("tracks", cv, "_id = ?", new String[]{String.valueOf(trackId.id())});
} while (cursor.moveToNext());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static Pair<Uri, String> createURI(Set<Track.Id> trackIds, String filenam

StringBuilder trackIdBuilder = new StringBuilder();
for (Track.Id trackId : trackIds) {
trackIdBuilder.append(trackId.getId()).append(TRACKID_DELIMITER);
trackIdBuilder.append(trackId.id()).append(TRACKID_DELIMITER);
}
trackIdBuilder.deleteCharAt(trackIdBuilder.lastIndexOf(TRACKID_DELIMITER));

Expand Down Expand Up @@ -199,7 +199,7 @@ public String getType(@NonNull Uri uri) {
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
Set<Track.Id> trackIds = parseURI(uri);
final ArrayList<Track> tracks = new ArrayList<>();
String[] trackIdsString = trackIds.stream().map(Track.Id::toString).toArray(String[]::new);
String[] trackIdsString = trackIds.stream().map(id -> String.valueOf(id.id())).toArray(String[]::new);
String whereClause = String.format(TracksColumns._ID + " IN (%s)", TextUtils.join(",", Collections.nCopies(trackIds.size(), "?")));

try (Cursor cursor = super.query(TracksColumns.CONTENT_URI, null, whereClause, trackIdsString, TracksColumns._ID)) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/dennisguse/opentracks/data/TrackDataHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void stop() {
public void loadTrack(final @NonNull Track.Id trackId) {
handler.post(() -> {
if (trackId.equals(selectedTrackId)) {
Log.i(TAG, "Not reloading track " + trackId.getId());
Log.i(TAG, "Not reloading track " + trackId.id());
return;
}
selectedTrackId = trackId;
Expand Down Expand Up @@ -326,7 +326,7 @@ private void notifyTrackPointsTableUpdate(boolean updateSamplingState, Set<Liste

TrackPoint.Id next = null;
if (localLastSeenTrackPointIdId != null) {
next = new TrackPoint.Id(localLastSeenTrackPointIdId.getId() + 1); //TODO startTrackPointId + 1 is an assumption assumption; should be derived from the DB.
next = new TrackPoint.Id(localLastSeenTrackPointIdId.id() + 1); //TODO startTrackPointId + 1 is an assumption assumption; should be derived from the DB.
}

TrackPoint trackPoint = null;
Expand All @@ -344,7 +344,7 @@ private void notifyTrackPointsTableUpdate(boolean updateSamplingState, Set<Liste
TrackPoint.Id trackPointId = trackPoint.getId();

// Stop if past the last wanted point
if (maxPointId != null && trackPointId.getId() > maxPointId.getId()) {
if (maxPointId != null && trackPointId.id() > maxPointId.id()) {
break;
}

Expand All @@ -355,7 +355,7 @@ private void notifyTrackPointsTableUpdate(boolean updateSamplingState, Set<Liste
}

if (samplingFrequency == -1) {
long numTotalPoints = Math.max(0L, lastTrackPointId.getId() - localFirstSeenTrackPointId.getId()); //TODO That is an assumption; should be derived from the DB.
long numTotalPoints = Math.max(0L, lastTrackPointId.id() - localFirstSeenTrackPointId.id()); //TODO That is an assumption; should be derived from the DB.
samplingFrequency = 1 + (int) (numTotalPoints / targetNumPoints);
}

Expand Down
Loading

0 comments on commit a68f2ab

Please sign in to comment.