From a68f2abfa671af8981729bb63ee242ce449189ec Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 29 Jun 2023 20:57:24 +0200 Subject: [PATCH] Java Record for Track.Id and TrackPoint.Id. Part of #1526. --- .../opentracks/content/data/TestDataUtil.java | 2 +- .../data/CustomContentProviderUtilsTest.java | 20 ++++----- .../opentracks/util/TrackNameUtilsTest.java | 2 +- .../opentracks/TrackEditActivity.java | 2 +- .../opentracks/data/ContentProviderUtils.java | 36 +++++++-------- .../data/CustomSQLiteOpenHelper.java | 4 +- .../opentracks/data/ShareContentProvider.java | 4 +- .../opentracks/data/TrackDataHub.java | 8 ++-- .../opentracks/data/TrackSelection.java | 2 +- .../opentracks/data/models/Track.java | 37 +--------------- .../opentracks/data/models/TrackPoint.java | 44 +++++++------------ .../services/TrackRecordingManager.java | 2 +- .../opentracks/share/ShareUtils.java | 2 +- .../ui/markers/MarkerListActivity.java | 2 +- .../dennisguse/opentracks/util/FileUtils.java | 2 +- .../opentracks/util/TrackNameUtils.java | 2 +- 16 files changed, 62 insertions(+), 109 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java b/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java index 3fdc19ad48..3523dec11c 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java +++ b/src/androidTest/java/de/dennisguse/opentracks/content/data/TestDataUtil.java @@ -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; } diff --git a/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java b/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java index d62442f5d2..48f1e26f9b 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java @@ -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()); } @@ -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); @@ -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; @@ -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++; @@ -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); @@ -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()); } /** @@ -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); @@ -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); @@ -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); @@ -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()); } diff --git a/src/androidTest/java/de/dennisguse/opentracks/util/TrackNameUtilsTest.java b/src/androidTest/java/de/dennisguse/opentracks/util/TrackNameUtilsTest.java index 9e585aca6d..9cf731210e 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/util/TrackNameUtilsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/util/TrackNameUtilsTest.java @@ -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)); } } diff --git a/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java b/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java index 1288ac5477..e1ac6612a6 100644 --- a/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java +++ b/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java @@ -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; } diff --git a/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java b/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java index cd59fc763f..4b95f6096b 100644 --- a/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java +++ b/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java @@ -189,13 +189,13 @@ public void deleteTracks(Context context, @NonNull List 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. @@ -229,7 +229,7 @@ public List 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); } @@ -278,7 +278,7 @@ 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) { @@ -286,7 +286,7 @@ private ContentValues createContentValues(Track track) { 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()); @@ -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) { @@ -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(); @@ -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); } @@ -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()); @@ -632,7 +632,7 @@ public int bulkInsertMarkers(List 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))); @@ -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))); @@ -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); @@ -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); } @@ -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()) { @@ -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); @@ -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); diff --git a/src/main/java/de/dennisguse/opentracks/data/CustomSQLiteOpenHelper.java b/src/main/java/de/dennisguse/opentracks/data/CustomSQLiteOpenHelper.java index f530b9854c..a77caa4952 100644 --- a/src/main/java/de/dennisguse/opentracks/data/CustomSQLiteOpenHelper.java +++ b/src/main/java/de/dennisguse/opentracks/data/CustomSQLiteOpenHelper.java @@ -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()); } } @@ -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()); } } diff --git a/src/main/java/de/dennisguse/opentracks/data/ShareContentProvider.java b/src/main/java/de/dennisguse/opentracks/data/ShareContentProvider.java index 1405d7a9b5..f9d7f157ab 100644 --- a/src/main/java/de/dennisguse/opentracks/data/ShareContentProvider.java +++ b/src/main/java/de/dennisguse/opentracks/data/ShareContentProvider.java @@ -86,7 +86,7 @@ public static Pair createURI(Set 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)); @@ -199,7 +199,7 @@ public String getType(@NonNull Uri uri) { public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException { Set trackIds = parseURI(uri); final ArrayList 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)) { diff --git a/src/main/java/de/dennisguse/opentracks/data/TrackDataHub.java b/src/main/java/de/dennisguse/opentracks/data/TrackDataHub.java index 24a37975f0..7abc8f647f 100644 --- a/src/main/java/de/dennisguse/opentracks/data/TrackDataHub.java +++ b/src/main/java/de/dennisguse/opentracks/data/TrackDataHub.java @@ -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; @@ -326,7 +326,7 @@ private void notifyTrackPointsTableUpdate(boolean updateSamplingState, Set maxPointId.getId()) { + if (maxPointId != null && trackPointId.id() > maxPointId.id()) { break; } @@ -355,7 +355,7 @@ private void notifyTrackPointsTableUpdate(boolean updateSamplingState, Set args = trackIds.stream().map(id -> Long.toString(id.getId())).collect(Collectors.toCollection(ArrayList::new)); + ArrayList args = trackIds.stream().map(id -> Long.toString(id.id())).collect(Collectors.toCollection(ArrayList::new)); args.addAll(categories); args.addAll(fromToArgs); selectionArgs = args.stream().toArray(String[]::new); diff --git a/src/main/java/de/dennisguse/opentracks/data/models/Track.java b/src/main/java/de/dennisguse/opentracks/data/models/Track.java index 054ea8cf04..c072d86c9a 100644 --- a/src/main/java/de/dennisguse/opentracks/data/models/Track.java +++ b/src/main/java/de/dennisguse/opentracks/data/models/Track.java @@ -143,53 +143,20 @@ public int hashCode() { return Objects.hash(id); } - public static class Id implements Parcelable { - private final long id; - - public Id(long id) { - this.id = id; - } - - protected Id(Parcel in) { - id = in.readLong(); - } - - //TOOD Limit visibility to TrackRecordingService / ContentProvider - public long getId() { - return id; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Id id1 = (Id) o; - return id == id1.id; - } - - @Override - public int hashCode() { - return Objects.hash(id); - } + public record Id(long id) implements Parcelable { @Override public int describeContents() { return 0; } - @NonNull - @Override - public String toString() { - return String.valueOf(id); - } - @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeLong(id); } - public static final Parcelable.Creator CREATOR = new Parcelable.Creator<>() { + public static final Creator CREATOR = new Creator<>() { public Track.Id createFromParcel(Parcel in) { return new Track.Id(in.readLong()); } diff --git a/src/main/java/de/dennisguse/opentracks/data/models/TrackPoint.java b/src/main/java/de/dennisguse/opentracks/data/models/TrackPoint.java index f7f42d54a9..e3b4b1b957 100644 --- a/src/main/java/de/dennisguse/opentracks/data/models/TrackPoint.java +++ b/src/main/java/de/dennisguse/opentracks/data/models/TrackPoint.java @@ -18,6 +18,7 @@ import android.location.Location; import android.os.Build; import android.os.Parcel; +import android.os.Parcelable; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -25,7 +26,6 @@ import java.time.Duration; import java.time.Instant; -import java.util.Objects; /** * Sensor and/or location information for a specific point in time. @@ -458,40 +458,26 @@ public String toString() { '}'; } - public static class Id { - - private final long id; - - public Id(long id) { - this.id = id; - } - - protected Id(Parcel in) { - id = in.readLong(); - } - - //TOOD Limit visibility to TrackRecordingService / ContentProvider - public long getId() { - return id; - } + public record Id(long id) implements Parcelable { @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TrackPoint.Id id1 = (TrackPoint.Id) o; - return id == id1.id; + public int describeContents() { + return 0; } @Override - public int hashCode() { - return Objects.hash(id); + public void writeToParcel(Parcel parcel, int i) { + parcel.writeLong(id); } - @NonNull - @Override - public String toString() { - return String.valueOf(id); - } + public static final Creator CREATOR = new Creator<>() { + public Id createFromParcel(Parcel in) { + return new Id(in.readLong()); + } + + public Id[] newArray(int size) { + return new Id[size]; + } + }; } } diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java index d660c7c216..fa591ce929 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java @@ -90,7 +90,7 @@ boolean resumeExistingTrack(@NonNull Track.Id resumeTrackId, @NonNull TrackPoint trackId = resumeTrackId; Track track = contentProviderUtils.getTrack(trackId); if (track == null) { - Log.e(TAG, "Ignore resumeTrack. Track " + trackId.getId() + " does not exists."); + Log.e(TAG, "Ignore resumeTrack. Track " + trackId.id() + " does not exists."); return false; } diff --git a/src/main/java/de/dennisguse/opentracks/share/ShareUtils.java b/src/main/java/de/dennisguse/opentracks/share/ShareUtils.java index ec57d64d41..7a87241aa0 100644 --- a/src/main/java/de/dennisguse/opentracks/share/ShareUtils.java +++ b/src/main/java/de/dennisguse/opentracks/share/ShareUtils.java @@ -50,7 +50,7 @@ public static Intent newShareFileIntent(Context context, Track.Id... trackIds) { for (Track.Id trackId : trackIds) { Track track = contentProviderUtils.getTrack(trackId); if (track == null) { - Log.e(TAG, "TrackId " + trackId.getId() + " could not be resolved."); + Log.e(TAG, "TrackId " + trackId.id() + " could not be resolved."); continue; } diff --git a/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListActivity.java b/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListActivity.java index 767c715c22..aa97b87790 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListActivity.java +++ b/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListActivity.java @@ -327,7 +327,7 @@ public Loader onCreateLoader(int arg0, Bundle arg1) { if (searchQuery == null) { if (track != null) { - return new CursorLoader(MarkerListActivity.this, MarkerColumns.CONTENT_URI, PROJECTION, MarkerColumns.TRACKID + "=?", new String[]{String.valueOf(track.getId().getId())}, null); + return new CursorLoader(MarkerListActivity.this, MarkerColumns.CONTENT_URI, PROJECTION, MarkerColumns.TRACKID + "=?", new String[]{String.valueOf(track.getId().id())}, null); } else { return new CursorLoader(MarkerListActivity.this, MarkerColumns.CONTENT_URI, PROJECTION, null, null, null); } diff --git a/src/main/java/de/dennisguse/opentracks/util/FileUtils.java b/src/main/java/de/dennisguse/opentracks/util/FileUtils.java index f8751a14c7..d27ca84818 100644 --- a/src/main/java/de/dennisguse/opentracks/util/FileUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/FileUtils.java @@ -59,7 +59,7 @@ public static File getPhotoDir(Context context) { } public static File getPhotoDir(Context context, Track.Id trackId) { - File photoDirectory = new File(getPhotoDir(context), "" + trackId.getId()); + File photoDirectory = new File(getPhotoDir(context), "" + trackId.id()); photoDirectory.mkdirs(); return photoDirectory; } diff --git a/src/main/java/de/dennisguse/opentracks/util/TrackNameUtils.java b/src/main/java/de/dennisguse/opentracks/util/TrackNameUtils.java index 264ceb5827..cc3f4c0310 100644 --- a/src/main/java/de/dennisguse/opentracks/util/TrackNameUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/TrackNameUtils.java @@ -44,7 +44,7 @@ public static String getTrackName(Context context, Track.Id trackId, OffsetDateT } else if (trackName.equals(context.getString(R.string.settings_recording_track_name_date_iso_8601_value))) { return startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmX")); } else { - return context.getString(R.string.track_name_format, trackId.getId()); + return context.getString(R.string.track_name_format, trackId.id()); } } }