From e6c8458ce70a80e44ba701102487656f5125cf95 Mon Sep 17 00:00:00 2001 From: bartn Date: Thu, 16 Dec 2010 14:13:09 -0800 Subject: [PATCH] Initial draft of the upcoming changes to simplify track points management. --- .../content/MyTracksProviderUtils.java | 58 +++++++++++++++++++ .../content/MyTracksProviderUtilsImpl.java | 6 ++ 2 files changed, 64 insertions(+) diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java index 56c70db13..488048355 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java @@ -21,6 +21,7 @@ import android.database.Cursor; import android.location.Location; import android.net.Uri; +import java.util.Iterator; import java.util.List; /** @@ -309,6 +310,63 @@ public interface MyTracksProviderUtils { * @return a new waypoint object */ Waypoint createWaypoint(Cursor cursor); + + /** + * A lightweight wrapper around the original {@link Iterator} with a method to clean up. + */ + interface LocationIterator extends Iterator { + /** + * Should be called in case the underlying iterator hasn't reached the last record. + */ + void close(); + } + + /** + * A factory for creating new {@class Location}s. + */ + interface LocationFactory { + Location createLocation(); + } + + /** + * The default {@class Location}s factory, which creates a new location of 'gps' type. + */ + LocationFactory DEFAULT_LOCATION_FACTORY = new LocationFactory() { + @Override + public Location createLocation() { + return new Location("gps"); + } + }; + + /** + * Creates a new read-only iterator over all track points for the given track. It provides + * a lightweight way of iterating over large tracks without failing due to the underlying cursor + * limitations. Since it's a read-only iterator, {@link Iterator#remove()} always throws + * {@class UnsupportedOperationException}. + * + * Once constructed, the user should either iterate over all elements, or call + * {@link LocationIterator#close()} to make sure that all resources are properly deallocated. + * + * Example use: + * + * ... + * LocationIterator it = providerUtils.getLocationIterator( + * 1, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); + * try { + * for (Location loc : it) { + * ... // Do something useful with the location. + * } + * } finally { + * it.close(); + * } + * ... + * + * + * @param trackId the ID of a track to retrieve locations for. + * @param locationFactory the factory for creating new locations. + * @return the read-only iterator over the given track's points. + */ + LocationIterator getLocationIterator(long trackId, LocationFactory locationFactory); /** * A factory which can produce instances of {@link MyTracksProviderUtils}, diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java index f9e466dce..0b186618a 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java @@ -934,4 +934,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(track), "_id=" + track.getId(), null); } + + @Override + public LocationIterator getLocationIterator(long trackId, + LocationFactory locationFactory) { + throw new UnsupportedOperationException(); + } }