mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-12 13:53:07 +02:00
Extracted LocationIterator from ContentProviderUtils.
Also refactored LocationFactory to TrackPointFactory.
This commit is contained in:
@@ -928,19 +928,16 @@ public class ContentProviderUtils {
|
||||
* Creates a new read-only iterator over a given track's points.
|
||||
* It provides a lightweight way of iterating over long tracks without failing due to the underlying cursor limitations.
|
||||
* Since it's a read-only iterator, {@link Iterator#remove()} always throws {@link UnsupportedOperationException}.
|
||||
* Each call to {@link LocationIterator#next()} may advance to the next DB record, and if so, the iterator calls {@link LocationFactory#createLocation()} and populates it with information retrieved from the record.
|
||||
* When done with iteration, {@link LocationIterator#close()} must be called.
|
||||
* Each call to {@link TrackPointIterator#next()} may advance to the next DB record, and if so, the iterator calls {@link TrackPointFactory#createLocation()} and populates it with information retrieved from the record.
|
||||
* When done with iteration, {@link TrackPointIterator#close()} must be called.
|
||||
*
|
||||
* @param trackId the track id
|
||||
* @param startTrackPointId the starting track point id. -1L to ignore
|
||||
* @param descending true to sort the result in descending order (latest location first)
|
||||
* @param locationFactory the location factory
|
||||
* @param trackPointFactory the location factory
|
||||
*/
|
||||
public LocationIterator getTrackPointLocationIterator(final long trackId, final long startTrackPointId, final boolean descending, final LocationFactory locationFactory) {
|
||||
if (locationFactory == null) {
|
||||
throw new IllegalArgumentException("locationFactory is null");
|
||||
}
|
||||
return new LocationIterator(this, trackId, startTrackPointId, descending, locationFactory);
|
||||
public TrackPointIterator getTrackPointLocationIterator(final long trackId, final long startTrackPointId, final boolean descending, final TrackPointFactory trackPointFactory) {
|
||||
return new TrackPointIterator(this, trackId, startTrackPointId, descending, trackPointFactory);
|
||||
}
|
||||
|
||||
private Location findTrackPointBy(String selection, String[] selectionArgs) {
|
||||
|
||||
@@ -450,11 +450,11 @@ public class TrackDataHub implements DataSourceListener {
|
||||
int samplingFrequency = -1;
|
||||
boolean includeNextPoint = false;
|
||||
|
||||
try (LocationIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(selectedTrackId, localLastSeenLocationId + 1, false, LocationFactory.DEFAULT_LOCATION_FACTORY)) {
|
||||
try (TrackPointIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(selectedTrackId, localLastSeenLocationId + 1, false, TrackPointFactory.DEFAULT_LOCATION_FACTORY)) {
|
||||
|
||||
while (locationIterator.hasNext()) {
|
||||
Location location = locationIterator.next();
|
||||
long locationId = locationIterator.getLocationId();
|
||||
long locationId = locationIterator.getTrackPointId();
|
||||
|
||||
// Stop if past the last wanted point
|
||||
if (maxPointId != -1L && locationId > maxPointId) {
|
||||
|
||||
+4
-5
@@ -1,6 +1,5 @@
|
||||
package de.dennisguse.opentracks.content;
|
||||
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
|
||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||
@@ -9,14 +8,14 @@ import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||
* Creates a new {@link TrackPoint}.
|
||||
* An implementation can create new instances or reuse existing instances for optimization.
|
||||
*/
|
||||
public class LocationFactory {
|
||||
public class TrackPointFactory {
|
||||
|
||||
/**
|
||||
* The default {@link LocationFactory} which creates a location each time.
|
||||
* The default {@link TrackPointFactory} which creates a location each time.
|
||||
*/
|
||||
public static LocationFactory DEFAULT_LOCATION_FACTORY = new LocationFactory();
|
||||
public static TrackPointFactory DEFAULT_LOCATION_FACTORY = new TrackPointFactory();
|
||||
|
||||
public Location createLocation() {
|
||||
public TrackPoint createLocation() {
|
||||
return new TrackPoint(LocationManager.GPS_PROVIDER);
|
||||
}
|
||||
}
|
||||
+16
-11
@@ -1,33 +1,38 @@
|
||||
package de.dennisguse.opentracks.content;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||
|
||||
/**
|
||||
* A lightweight wrapper around the original {@link Cursor} with a method to clean up.
|
||||
*/
|
||||
public class LocationIterator implements Iterator<Location>, AutoCloseable {
|
||||
public class TrackPointIterator implements Iterator<TrackPoint>, AutoCloseable {
|
||||
|
||||
private static final String TAG = LocationIterator.class.getSimpleName();
|
||||
private static final String TAG = TrackPointIterator.class.getSimpleName();
|
||||
|
||||
private final ContentProviderUtils contentProviderUtils;
|
||||
private final long trackId;
|
||||
private final boolean descending;
|
||||
private final LocationFactory locationFactory;
|
||||
private final TrackPointFactory trackPointFactory;
|
||||
private final ContentProviderUtils.CachedTrackPointsIndexes indexes;
|
||||
private long lastTrackPointId = -1L;
|
||||
private Cursor cursor;
|
||||
|
||||
|
||||
public LocationIterator(ContentProviderUtils contentProviderUtils, long trackId, long startTrackPointId, boolean descending, LocationFactory locationFactory) {
|
||||
public TrackPointIterator(ContentProviderUtils contentProviderUtils, long trackId, long startTrackPointId, boolean descending, TrackPointFactory trackPointFactory) {
|
||||
if (trackPointFactory == null) {
|
||||
throw new IllegalArgumentException("trackPointFactory is null");
|
||||
}
|
||||
|
||||
this.contentProviderUtils = contentProviderUtils;
|
||||
this.trackId = trackId;
|
||||
this.descending = descending;
|
||||
this.locationFactory = locationFactory;
|
||||
this.trackPointFactory = trackPointFactory;
|
||||
|
||||
cursor = getCursor(startTrackPointId);
|
||||
indexes = cursor != null ? new ContentProviderUtils.CachedTrackPointsIndexes(cursor)
|
||||
@@ -54,7 +59,7 @@ public class LocationIterator implements Iterator<Location>, AutoCloseable {
|
||||
return cursor != null;
|
||||
}
|
||||
|
||||
public long getLocationId() {
|
||||
public long getTrackPointId() {
|
||||
return lastTrackPointId;
|
||||
}
|
||||
|
||||
@@ -76,7 +81,7 @@ public class LocationIterator implements Iterator<Location>, AutoCloseable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location next() {
|
||||
public TrackPoint next() {
|
||||
if (cursor == null) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
@@ -86,9 +91,9 @@ public class LocationIterator implements Iterator<Location>, AutoCloseable {
|
||||
}
|
||||
}
|
||||
lastTrackPointId = cursor.getLong(indexes.idIndex);
|
||||
Location location = locationFactory.createLocation();
|
||||
ContentProviderUtils.fillTrackPoint(cursor, indexes, location);
|
||||
return location;
|
||||
TrackPoint trackPoint = trackPointFactory.createLocation();
|
||||
ContentProviderUtils.fillTrackPoint(cursor, indexes, trackPoint);
|
||||
return trackPoint;
|
||||
}
|
||||
|
||||
@Override
|
||||
Reference in New Issue
Block a user