forked from upstream-mirrors/OpenTracks
Cleanup: AutoCloseableIterator now handles the cursor.
This commit is contained in:
@@ -56,7 +56,6 @@ import de.dennisguse.opentracks.data.tables.TrackPointsColumns;
|
||||
import de.dennisguse.opentracks.data.tables.TracksColumns;
|
||||
import de.dennisguse.opentracks.stats.SensorStatistics;
|
||||
import de.dennisguse.opentracks.ui.markers.MarkerUtils;
|
||||
import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
import de.dennisguse.opentracks.util.FileUtils;
|
||||
|
||||
/**
|
||||
@@ -180,7 +179,7 @@ public class ContentProviderUtils {
|
||||
return tracks;
|
||||
}
|
||||
|
||||
public AutoCloseableIterator<TrackListIterator.Item> searchTracks(String searchQuery) {
|
||||
public TrackListIterator searchTracks(String searchQuery) {
|
||||
// Needed, because MARKER_COUNT is a virtual column and has to be explicitly requested.
|
||||
final String[] PROJECTION = new String[]{
|
||||
TracksColumns._ID,
|
||||
@@ -401,11 +400,11 @@ public class ContentProviderUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public AutoCloseableIterator<Marker> getMarkerIterator(@NonNull Track.Id trackId) {
|
||||
public MarkerIterator getMarkerIterator(@NonNull Track.Id trackId) {
|
||||
return new MarkerIterator(this, trackId, -1);
|
||||
}
|
||||
|
||||
public AutoCloseableIterator<Marker> getMarkerIterator(@NonNull Track.Id trackId, int maxCount) {
|
||||
public MarkerIterator getMarkerIterator(@NonNull Track.Id trackId, int maxCount) {
|
||||
return new MarkerIterator(this, trackId, maxCount);
|
||||
}
|
||||
|
||||
@@ -674,7 +673,7 @@ public class ContentProviderUtils {
|
||||
* @param trackId the track id
|
||||
* @param startTrackPointId the starting trackPoint id. `null` to ignore
|
||||
*/
|
||||
public AutoCloseableIterator<TrackPoint> getTrackPointIterator(Track.Id trackId, TrackPoint.Id startTrackPointId) {
|
||||
public TrackPointIterator getTrackPointIterator(Track.Id trackId, TrackPoint.Id startTrackPointId) {
|
||||
return new TrackPointIterator(this, trackId, startTrackPointId);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,6 @@ 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;
|
||||
@@ -13,20 +10,16 @@ import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
|
||||
public class MarkerIterator extends AutoCloseableIterator<Marker> {
|
||||
|
||||
private static final String TAG = MarkerIterator.class.getSimpleName();
|
||||
|
||||
private final ContentProviderUtils contentProviderUtils;
|
||||
private Cursor cursor;
|
||||
|
||||
MarkerIterator(ContentProviderUtils contentProviderUtils, Cursor cursor) {
|
||||
super(cursor);
|
||||
this.contentProviderUtils = contentProviderUtils;
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
MarkerIterator(ContentProviderUtils contentProviderUtils, Track.Id trackId, int maxCount) {
|
||||
super(contentProviderUtils.getMarkerCursor(trackId, maxCount));
|
||||
this.contentProviderUtils = contentProviderUtils;
|
||||
|
||||
cursor = contentProviderUtils.getMarkerCursor(trackId, maxCount);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -34,49 +27,4 @@ public class MarkerIterator extends AutoCloseableIterator<Marker> {
|
||||
public Marker get() {
|
||||
return contentProviderUtils.createMarker(cursor);
|
||||
}
|
||||
|
||||
@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 get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPosition() {
|
||||
return cursor.getPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean moveToPosition(int position) {
|
||||
return cursor.moveToPosition(position);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ import de.dennisguse.opentracks.services.RecordingStatus;
|
||||
import de.dennisguse.opentracks.services.TrackRecordingService;
|
||||
import de.dennisguse.opentracks.services.handlers.AltitudeCorrectionManager;
|
||||
import de.dennisguse.opentracks.stats.TrackStatisticsUpdater;
|
||||
import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
|
||||
/**
|
||||
* Track data hub.
|
||||
@@ -279,7 +278,7 @@ public class TrackDataHub {
|
||||
}
|
||||
|
||||
TrackPoint trackPoint = null;
|
||||
try (AutoCloseableIterator<TrackPoint> trackPointIterator = contentProviderUtils.getTrackPointIterator(selectedTrackId, next)) {
|
||||
try (TrackPointIterator trackPointIterator = contentProviderUtils.getTrackPointIterator(selectedTrackId, next)) {
|
||||
long numTotalPoints = trackPointIterator.getCount();
|
||||
int samplingFrequency = 1 + (int) (numTotalPoints / targetNumPoints);
|
||||
|
||||
|
||||
@@ -3,12 +3,10 @@ package de.dennisguse.opentracks.data;
|
||||
import android.database.Cursor;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.ActivityType;
|
||||
import de.dennisguse.opentracks.data.models.Distance;
|
||||
@@ -18,12 +16,8 @@ import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
|
||||
public class TrackListIterator extends AutoCloseableIterator<TrackListIterator.Item> {
|
||||
|
||||
private static final String TAG = TrackListIterator.class.getSimpleName();
|
||||
|
||||
private Cursor cursor;
|
||||
|
||||
TrackListIterator(Cursor cursor) {
|
||||
this.cursor = cursor;
|
||||
super(cursor);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -54,51 +48,6 @@ public class TrackListIterator extends AutoCloseableIterator<TrackListIterator.I
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (cursor == null) {
|
||||
return false;
|
||||
}
|
||||
return !cursor.isLast() && !cursor.isAfterLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Item next() {
|
||||
if (cursor == null || !cursor.moveToNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPosition() {
|
||||
return cursor.getPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean moveToPosition(int position) {
|
||||
return cursor.moveToPosition(position);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
public record Item(
|
||||
@NonNull
|
||||
Track.Id id,
|
||||
@@ -128,7 +77,6 @@ public class TrackListIterator extends AutoCloseableIterator<TrackListIterator.I
|
||||
Distance totalDistance,
|
||||
|
||||
int markerCount
|
||||
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -1,78 +1,23 @@
|
||||
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.Track;
|
||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||
import de.dennisguse.opentracks.util.AutoCloseableIterator;
|
||||
|
||||
/**
|
||||
* A lightweight wrapper around the original {@link Cursor}.
|
||||
*/
|
||||
class TrackPointIterator extends AutoCloseableIterator<TrackPoint> {
|
||||
public class TrackPointIterator extends AutoCloseableIterator<TrackPoint> {
|
||||
|
||||
private static final String TAG = TrackPointIterator.class.getSimpleName();
|
||||
private final CachedTrackPointsIndexes indexes;
|
||||
private Cursor cursor;
|
||||
|
||||
TrackPointIterator(ContentProviderUtils contentProviderUtils, Track.Id trackId, TrackPoint.Id startTrackPointId) {
|
||||
cursor = contentProviderUtils.getTrackPointCursor(trackId, startTrackPointId);
|
||||
super(contentProviderUtils.getTrackPointCursor(trackId, startTrackPointId));
|
||||
indexes = new CachedTrackPointsIndexes(cursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (cursor == null) {
|
||||
return false;
|
||||
}
|
||||
return !cursor.isLast() && !cursor.isAfterLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public TrackPoint get() {
|
||||
return ContentProviderUtils.fillTrackPoint(cursor, indexes);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public TrackPoint next() {
|
||||
if (cursor == null || !cursor.moveToNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPosition() {
|
||||
return cursor.getPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean moveToPosition(int position) {
|
||||
return cursor.moveToPosition(position);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user