forked from upstream-mirrors/OpenTracks
Implemented MarkerIterator.
This commit is contained in:
@@ -7,6 +7,7 @@ import de.dennisguse.opentracks.data.tables.TrackPointsColumns;
|
||||
/**
|
||||
* A cache of track points indexes.
|
||||
*/
|
||||
//TOOD Check if this actually useful.
|
||||
class CachedTrackPointsIndexes {
|
||||
final int idIndex;
|
||||
final int typeIndex;
|
||||
|
||||
@@ -430,6 +430,11 @@ public class ContentProviderUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public AutoCloseableIterator<Marker> getMarkerIterator(@NonNull Track.Id trackId) {
|
||||
return new MarkerIterator(this, trackId, -1);
|
||||
}
|
||||
|
||||
//TODO Use MarkerIterator instead
|
||||
public Cursor getMarkerCursor(@NonNull Track.Id trackId) {
|
||||
return getMarkerCursor(trackId, -1);
|
||||
}
|
||||
@@ -731,7 +736,7 @@ public class ContentProviderUtils {
|
||||
* @param trackId the track id
|
||||
* @param startTrackPointId the starting trackPoint id. `null` to ignore
|
||||
*/
|
||||
public AutoCloseableIterator<TrackPoint> getTrackPointIterator(final Track.Id trackId, final TrackPoint.Id startTrackPointId) {
|
||||
public AutoCloseableIterator<TrackPoint> getTrackPointIterator(Track.Id trackId, TrackPoint.Id startTrackPointId) {
|
||||
return new TrackPointIterator(this, trackId, startTrackPointId);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package de.dennisguse.opentracks.data;
|
||||
|
||||
import android.database.Cursor;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Marker;
|
||||
import de.dennisguse.opentracks.data.models.Track;
|
||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||
import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
|
||||
class MarkerIterator extends AutoCloseableIterator<Marker> {
|
||||
|
||||
private static final String TAG = MarkerIterator.class.getSimpleName();
|
||||
|
||||
private final ContentProviderUtils contentProviderUtils;
|
||||
private Cursor cursor;
|
||||
|
||||
MarkerIterator(ContentProviderUtils contentProviderUtils, Track.Id trackId, int maxCount) {
|
||||
this.contentProviderUtils = contentProviderUtils;
|
||||
|
||||
cursor = contentProviderUtils.getMarkerCursor(trackId, maxCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (cursor == null) {
|
||||
return false;
|
||||
}
|
||||
return !cursor.isLast() && !cursor.isAfterLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Marker next() {
|
||||
if (cursor == null || !cursor.moveToNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return contentProviderUtils.createMarker(cursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPosition() {
|
||||
return cursor.getPosition();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public int getCount() {
|
||||
return cursor.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
cursor = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -17,16 +17,10 @@ import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
class TrackPointIterator extends AutoCloseableIterator<TrackPoint> {
|
||||
|
||||
private static final String TAG = TrackPointIterator.class.getSimpleName();
|
||||
|
||||
private final ContentProviderUtils contentProviderUtils;
|
||||
private final Track.Id trackId;
|
||||
private final CachedTrackPointsIndexes indexes;
|
||||
private Cursor cursor;
|
||||
|
||||
TrackPointIterator(ContentProviderUtils contentProviderUtils, Track.Id trackId, TrackPoint.Id startTrackPointId) {
|
||||
this.contentProviderUtils = contentProviderUtils;
|
||||
this.trackId = trackId;
|
||||
|
||||
cursor = contentProviderUtils.getTrackPointCursor(trackId, startTrackPointId);
|
||||
indexes = new CachedTrackPointsIndexes(cursor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user