Extracted LocationIterator from ContentProviderUtils.

Also refactored LocationFactory to TrackPointFactory.
This commit is contained in:
Dennis Guse
2020-01-03 22:14:23 +01:00
parent 8bc2937bb4
commit 971460848b
8 changed files with 70 additions and 68 deletions
@@ -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) {
@@ -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);
}
}
@@ -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
@@ -26,8 +26,8 @@ import androidx.annotation.NonNull;
import java.io.OutputStream;
import de.dennisguse.opentracks.content.ContentProviderUtils;
import de.dennisguse.opentracks.content.LocationFactory;
import de.dennisguse.opentracks.content.LocationIterator;
import de.dennisguse.opentracks.content.TrackPointFactory;
import de.dennisguse.opentracks.content.TrackPointIterator;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.data.Waypoint;
@@ -126,10 +126,10 @@ public class FileTrackExporter implements TrackExporter {
boolean wroteTrack = false;
boolean wroteSegment = false;
boolean isLastLocationValid = false;
TrackWriterLocationFactory locationFactory = new TrackWriterLocationFactory();
TrackWriterTrackPointFactory trackPointFactory = new TrackWriterTrackPointFactory();
int locationNumber = 0;
try (LocationIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false, locationFactory)) {
try (TrackPointIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false, trackPointFactory)) {
while (locationIterator.hasNext()) {
if (Thread.interrupted()) {
@@ -144,7 +144,7 @@ public class FileTrackExporter implements TrackExporter {
boolean isSegmentValid = isLocationValid && isLastLocationValid;
if (!wroteTrack && isSegmentValid) {
// Found the first two consecutive locations that are valid
trackWriter.writeBeginTrack(track, locationFactory.lastLocation);
trackWriter.writeBeginTrack(track, trackPointFactory.lastLocation);
wroteTrack = true;
}
@@ -155,7 +155,7 @@ public class FileTrackExporter implements TrackExporter {
wroteSegment = true;
// Write the previous location, which we had previously skipped
trackWriter.writeLocation(locationFactory.lastLocation);
trackWriter.writeLocation(trackPointFactory.lastLocation);
}
// Write the current location
@@ -169,7 +169,7 @@ public class FileTrackExporter implements TrackExporter {
wroteSegment = false;
}
}
locationFactory.swapLocations();
trackPointFactory.swapLocations();
isLastLocationValid = isLocationValid;
}
@@ -207,12 +207,12 @@ public class FileTrackExporter implements TrackExporter {
*
* @author Jimmy Shih
*/
private class TrackWriterLocationFactory extends LocationFactory {
Location currentLocation;
Location lastLocation;
private class TrackWriterTrackPointFactory extends TrackPointFactory {
TrackPoint currentLocation;
TrackPoint lastLocation;
@Override
public Location createLocation() {
public TrackPoint createLocation() {
if (currentLocation == null) {
currentLocation = new TrackPoint("");
}
@@ -220,7 +220,7 @@ public class FileTrackExporter implements TrackExporter {
}
void swapLocations() {
Location tempLocation = lastLocation;
TrackPoint tempLocation = lastLocation;
lastLocation = currentLocation;
currentLocation = tempLocation;
if (currentLocation != null) {
@@ -39,8 +39,8 @@ import javax.xml.parsers.SAXParserFactory;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.content.ContentProviderUtils;
import de.dennisguse.opentracks.content.LocationFactory;
import de.dennisguse.opentracks.content.LocationIterator;
import de.dennisguse.opentracks.content.TrackPointFactory;
import de.dennisguse.opentracks.content.TrackPointIterator;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.Waypoint;
import de.dennisguse.opentracks.services.TrackRecordingService;
@@ -162,7 +162,7 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
TripStatisticsUpdater trackTripStatisticstrackUpdater = new TripStatisticsUpdater(track.getTripStatistics().getStartTime());
TripStatisticsUpdater markerTripStatisticsUpdater = new TripStatisticsUpdater(track.getTripStatistics().getStartTime());
try (LocationIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false, LocationFactory.DEFAULT_LOCATION_FACTORY)) {
try (TrackPointIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false, TrackPointFactory.DEFAULT_LOCATION_FACTORY)) {
while (true) {
if (waypoint == null) {
@@ -44,8 +44,8 @@ import de.dennisguse.opentracks.TrackDetailActivity;
import de.dennisguse.opentracks.TrackListActivity;
import de.dennisguse.opentracks.content.ContentProviderUtils;
import de.dennisguse.opentracks.content.CustomContentProvider;
import de.dennisguse.opentracks.content.LocationFactory;
import de.dennisguse.opentracks.content.LocationIterator;
import de.dennisguse.opentracks.content.TrackPointFactory;
import de.dennisguse.opentracks.content.TrackPointIterator;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.data.Waypoint;
@@ -373,7 +373,7 @@ public class TrackRecordingService extends Service {
TripStatistics tripStatistics = track.getTripStatistics();
trackTripStatisticsUpdater = new TripStatisticsUpdater(tripStatistics.getStartTime());
try (LocationIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false, LocationFactory.DEFAULT_LOCATION_FACTORY)) {
try (TrackPointIterator locationIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false, TrackPointFactory.DEFAULT_LOCATION_FACTORY)) {
while (locationIterator.hasNext()) {
Location location = locationIterator.next();