In TrackRecordingService, make sure all fields of a waypoint

are set consistently when creating a marker.
This commit is contained in:
Jimmy Shih
2012-08-09 14:17:19 -07:00
parent 80f2237e2b
commit bafcd0e37f
14 changed files with 137 additions and 132 deletions
@@ -165,7 +165,7 @@ public class MarkerEditActivity extends AbstractMyTracksActivity {
waypointName.getText().toString(),
waypointMarkerType.getText().toString(),
waypointDescription.getText().toString(),
getString(R.string.marker_waypoint_icon_url));
null);
TrackRecordingServiceConnectionUtils.addMarker(
this, trackRecordingServiceConnection, waypointCreationRequest);
}
@@ -76,8 +76,8 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
}
@Override
public String generateWaypointDescription(Waypoint waypoint) {
return generateTripStatisticsDescription(waypoint.getTripStatistics(), false);
public String generateWaypointDescription(TripStatistics tripStatistics) {
return generateTripStatisticsDescription(tripStatistics, false);
}
/**
@@ -574,7 +574,7 @@ public class TrackDataHub implements DataSourceListener {
long localLastSeenLocationId = updateSamplingState ? lastSeenLocationId : -1L;
long maxPointId = updateSamplingState ? -1L : lastSeenLocationId;
long lastLocationId = myTracksProviderUtils.getLastLocationId(selectedTrackId);
long lastLocationId = myTracksProviderUtils.getLastTrackLocationId(selectedTrackId);
int samplingFrequency = -1;
LocationIterator iterator = myTracksProviderUtils.getLocationIterator(
selectedTrackId, localLastSeenLocationId + 1, false, locationFactory);
@@ -527,7 +527,7 @@ public class GpxImporter extends DefaultHandler {
*/
private long getLastPointId() {
flushPoints();
return myTracksProviderUtils.getLastLocationId(track.getId());
return myTracksProviderUtils.getLastTrackLocationId(track.getId());
}
/**
@@ -413,33 +413,61 @@ public class TrackRecordingService extends Service {
if (!isRecording()) {
throw new IllegalStateException("Cannot insert marker when not recording!");
}
Waypoint waypoint = new Waypoint();
if (waypointCreationRequest.getType() == WaypointType.WAYPOINT) {
buildWaypointMarker(waypoint, waypointCreationRequest);
} else {
buildStatisticsMarker(waypoint, waypointCreationRequest);
}
waypoint.setTrackId(recordingTrackId);
waypoint.setLength(length);
boolean isStatistics = waypointCreationRequest.getType() == WaypointType.STATISTICS;
if (lastLocation != null && trackTripStatisticsBuilder != null
String name;
if (waypointCreationRequest.getName() != null) {
name = waypointCreationRequest.getName();
} else {
int nextMarkerNumber = myTracksProviderUtils.getNextMarkerNumber(
recordingTrackId, isStatistics);
if (nextMarkerNumber == -1) {
nextMarkerNumber = 0;
}
name = getString(
isStatistics ? R.string.marker_split_name_format : R.string.marker_name_format,
nextMarkerNumber);
}
TripStatistics tripStatistics;
String description;
if (isStatistics) {
long now = System.currentTimeMillis();
markerTripStatisticsBuilder.pauseAt(now);
tripStatistics = markerTripStatisticsBuilder.getTripStatistics();
markerTripStatisticsBuilder = new TripStatisticsBuilder(now);
description = new DescriptionGeneratorImpl(this).generateWaypointDescription(tripStatistics);
} else {
tripStatistics = null;
description = waypointCreationRequest.getDescription() != null ? waypointCreationRequest
.getDescription()
: "";
}
String category = waypointCreationRequest.getCategory() != null ? waypointCreationRequest
.getCategory()
: "";
String icon = getString(
isStatistics ? R.string.marker_statistics_icon_url : R.string.marker_waypoint_icon_url);
int type = isStatistics ? Waypoint.TYPE_STATISTICS : Waypoint.TYPE_WAYPOINT;
long duration;
Location location = myTracksProviderUtils.getLastTrackLocation(recordingTrackId);
if (location != null && trackTripStatisticsBuilder != null
&& trackTripStatisticsBuilder.getTripStatistics() != null) {
waypoint.setLocation(lastLocation);
waypoint.setDuration(
lastLocation.getTime() - trackTripStatisticsBuilder.getTripStatistics().getStartTime());
duration = location.getTime() - trackTripStatisticsBuilder.getTripStatistics().getStartTime();
} else {
if (!waypointCreationRequest.isTrackStatistics()) {
return -1L;
}
/*
* For track statistics, a null location is OK. Make it an impossible
* location.
*/
Location location = new Location("");
// For track statistics, make it an impossible location
location = new Location("");
location.setLatitude(100);
location.setLongitude(180);
waypoint.setLocation(location);
duration = 0;
}
Waypoint waypoint = new Waypoint(name, description, category, icon, recordingTrackId, type,
length, duration, -1L, -1L, location, tripStatistics);
Uri uri = myTracksProviderUtils.insertWaypoint(waypoint);
return Long.parseLong(uri.getLastPathSegment());
}
@@ -642,7 +670,7 @@ public class TrackRecordingService extends Service {
splitExecutor.shutdown();
Track track = myTracksProviderUtils.getTrack(recordingTrackId);
if (track != null) {
long lastLocationId = myTracksProviderUtils.getLastLocationId(recordingTrackId);
long lastLocationId = myTracksProviderUtils.getLastTrackLocationId(recordingTrackId);
if (lastLocationId >= 0 && track.getStopId() >= 0) {
track.setStopId(lastLocationId);
}
@@ -852,79 +880,6 @@ public class TrackRecordingService extends Service {
return true;
}
/**
* Builds a waypoint marker.
*
* @param waypoint the waypoint
* @param waypointCreationRequest the waypoint creation request
*/
private void buildWaypointMarker(
Waypoint waypoint, WaypointCreationRequest waypointCreationRequest) {
waypoint.setType(Waypoint.TYPE_WAYPOINT);
String name;
if (waypointCreationRequest.getName() != null) {
name = waypointCreationRequest.getName();
} else {
int nextMarkerNumber = myTracksProviderUtils.getNextMarkerNumber(recordingTrackId, false);
if (nextMarkerNumber == -1) {
nextMarkerNumber = 0;
}
name = getString(R.string.marker_name_format, nextMarkerNumber);
}
waypoint.setName(name);
if (waypointCreationRequest.getDescription() != null) {
waypoint.setDescription(waypointCreationRequest.getDescription());
}
if (waypointCreationRequest.getCategory() != null) {
waypoint.setCategory(waypointCreationRequest.getCategory());
}
String icon = waypointCreationRequest.getIconUrl();
if (icon == null) {
icon = getString(R.string.marker_waypoint_icon_url);
}
waypoint.setIcon(icon);
}
/**
* Build a statistics marker. A statistics marker holds the stats for the last
* segment up to this statistics marker.
*
* @param waypoint the waypoint
* @param waypointCreationRequest the waypoint creation request
*/
private void buildStatisticsMarker(
Waypoint waypoint, WaypointCreationRequest waypointCreationRequest) {
waypoint.setType(Waypoint.TYPE_STATISTICS);
String name;
if (waypointCreationRequest.getName() != null) {
name = waypointCreationRequest.getName();
} else {
int nextMarkerNumber = myTracksProviderUtils.getNextMarkerNumber(recordingTrackId, true);
if (nextMarkerNumber == -1) {
nextMarkerNumber = 0;
}
name = getString(R.string.marker_split_name_format, nextMarkerNumber);
}
waypoint.setName(name);
long now = System.currentTimeMillis();
markerTripStatisticsBuilder.pauseAt(now);
waypoint.setTripStatistics(markerTripStatisticsBuilder.getTripStatistics());
// Update description after updating tripStatistics
waypoint.setDescription(
new DescriptionGeneratorImpl(this).generateWaypointDescription(waypoint));
waypoint.setIcon(getString(R.string.marker_statistics_icon_url));
waypoint.setStartId(myTracksProviderUtils.getLastLocationId(recordingTrackId));
// Update duration (from the the beginning of the track)
waypoint.setDuration(now - trackTripStatisticsBuilder.getTripStatistics().getStartTime());
// Create a new markerTripStatisticsBuilder for the next statistics marker
markerTripStatisticsBuilder = new TripStatisticsBuilder(now);
}
/**
* Registers the location listener.
*/
@@ -15,6 +15,8 @@
*/
package com.google.android.apps.mytracks.content;
import com.google.android.apps.mytracks.stats.TripStatistics;
import java.util.Vector;
/**
@@ -37,9 +39,9 @@ public interface DescriptionGenerator {
Track track, Vector<Double> distances, Vector<Double> elevations, boolean html);
/**
* Generate a waypoint description.
* Generate a waypoint description from a trip statistics.
*
* @param waypoint the waypoint
* @param tripStatistics the trip statistics
*/
public String generateWaypointDescription(Waypoint waypoint);
public String generateWaypointDescription(TripStatistics tripStatistics);
}
@@ -293,7 +293,14 @@ public interface MyTracksProviderUtils {
*
* @param trackId the track id
*/
public long getLastLocationId(long trackId);
public long getLastTrackLocationId(long trackId);
/**
* Gets the last location for a track. Returns -1L if it doesn't exist.
*
* @param trackId the track id
*/
public Location getLastTrackLocation(long trackId);
/**
* Inserts a track point.
@@ -556,7 +556,8 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
Log.d(TAG, "Unable to find the next statistics marker after deleting one.");
} else {
nextWaypoint.getTripStatistics().merge(waypoint.getTripStatistics());
nextWaypoint.setDescription(descriptionGenerator.generateWaypointDescription(nextWaypoint));
nextWaypoint.setDescription(
descriptionGenerator.generateWaypointDescription(nextWaypoint.getTripStatistics()));
if (!updateWaypoint(nextWaypoint)) {
Log.e(TAG, "Unable to update the next statistics marker after deleting one.");
}
@@ -928,7 +929,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
}
@Override
public long getLastLocationId(long trackId) {
public long getLastTrackLocationId(long trackId) {
if (trackId < 0) {
return -1L;
}
@@ -950,6 +951,18 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
}
return -1L;
}
@Override
public Location getLastTrackLocation(long trackId) {
if (trackId < 0) {
return null;
}
String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID
+ ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID
+ "=?)";
String[] selectionArgs = new String[] { Long.toString(trackId) };
return findLocationBy(selection, selectionArgs);
}
@Override
public Uri insertTrackPoint(Location location, long trackId) {
@@ -33,22 +33,39 @@ public final class Waypoint implements Parcelable {
public static final int TYPE_WAYPOINT = 0;
public static final int TYPE_STATISTICS = 1;
private long id = -1;
private long id = -1L;
private String name = "";
private String description = "";
private String category = "";
private String icon = "";
private long trackId = -1;
private long trackId = -1L;
private int type = 0;
private double length = 0.0;
private long duration = 0;
private long startId = -1;
private long stopId = -1;
private long startId = -1L;
private long stopId = -1L;
private Location location = null;
private TripStatistics tripStatistics = null;
public Waypoint() {}
public Waypoint(String name, String description, String category, String icon, long trackId,
int type, double length, long duration, long startId, long stopId, Location location,
TripStatistics tripStatistics) {
this.name = name;
this.description = description;
this.category = category;
this.icon = icon;
this.trackId = trackId;
this.type = type;
this.length = length;
this.duration = duration;
this.startId = startId;
this.stopId = stopId;
this.location = location;
this.tripStatistics = tripStatistics;
}
private Waypoint(Parcel source) {
id = source.readLong();
name = source.readString();
@@ -112,7 +112,8 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase {
+ "Max grade: 42 %\n"
+ "Min grade: 11 %\n"
+ "Recorded: " + StringUtils.formatDateTime(getContext(), START_TIME) + "\n";
assertEquals(expected, descriptionGenerator.generateWaypointDescription(waypoint));
assertEquals(
expected, descriptionGenerator.generateWaypointDescription(waypoint.getTripStatistics()));
}
/**
@@ -594,7 +594,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
DescriptionGenerator descriptionGenerator = new DescriptionGenerator() {
@Override
public String generateWaypointDescription(Waypoint waypoint) {
public String generateWaypointDescription(TripStatistics tripStatistics) {
return MyTracksProviderUtilsImplTest.MOCK_DESC;
}
@@ -650,7 +650,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
// Delete
DescriptionGenerator descriptionGenerator = new DescriptionGenerator() {
@Override
public String generateWaypointDescription(Waypoint waypoint) {
public String generateWaypointDescription(TripStatistics tripStatistics) {
return MyTracksProviderUtilsImplTest.MOCK_DESC;
}
@@ -879,20 +879,30 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
}
assertFalse(locationIterator.hasNext());
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#getLastLocationId(long)}.
* Tests the method {@link MyTracksProviderUtilsImpl#getLastTrackLocationId(long)}.
*/
public void testGetLastLocationId() {
public void testGetLastTrackLocationId() {
// Insert track, point at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
assertEquals(10, providerUtils.getLastLocationId(trackId));
assertEquals(10, providerUtils.getLastTrackLocationId(trackId));
}
/**
* Tests the method {@link MyTracksProviderUtilsImpl#getLastTrackLocation(long)}.
*/
public void testGetLastTrackLocation() {
// Insert track, point at first.
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
insertTrackWithLocations(track);
checkLocation(9, providerUtils.getLastTrackLocation(trackId));
}
/**
* Tests the method
* {@link MyTracksProviderUtilsImpl#insertTrackPoint(Location, long)}. This
@@ -289,7 +289,7 @@ public class TrackDataHubTest extends AndroidTestCase {
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 10, 5);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(10L);
trackDataListener1.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener1);
trackDataListener1.onNewTrackPointsDone();
@@ -304,7 +304,7 @@ public class TrackDataHubTest extends AndroidTestCase {
locationIterator = new FixedSizeLocationIterator(1, 10, 5);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(10L);
trackDataListener2.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener2);
trackDataListener2.onNewTrackPointsDone();
@@ -319,7 +319,7 @@ public class TrackDataHubTest extends AndroidTestCase {
locationIterator = new FixedSizeLocationIterator(11, 10, 1);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(11L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(20L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(20L);
locationIterator.expectLocationsDelivered(trackDataListener1);
locationIterator.expectLocationsDelivered(trackDataListener2);
trackDataListener1.onNewTrackPointsDone();
@@ -334,7 +334,7 @@ public class TrackDataHubTest extends AndroidTestCase {
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID + 1), eq(0L), eq(false), isA(LocationFactory.class)))
.andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID + 1)).andReturn(110L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID + 1)).andReturn(110L);
trackDataListener2.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener2);
trackDataListener2.onNewTrackPointsDone();
@@ -359,7 +359,7 @@ public class TrackDataHubTest extends AndroidTestCase {
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 10, 5);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(10L);
trackDataListener1.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener1);
@@ -385,7 +385,7 @@ public class TrackDataHubTest extends AndroidTestCase {
locationIterator = new FixedSizeLocationIterator(1, 10, 5);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(10L);
trackDataListener1.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener1);
trackDataListener1.onNewTrackPointsDone();
@@ -410,7 +410,7 @@ public class TrackDataHubTest extends AndroidTestCase {
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 10, 5);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(10L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(10L);
trackDataListener1.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener1);
trackDataListener1.onNewTrackPointsDone();
@@ -436,7 +436,7 @@ public class TrackDataHubTest extends AndroidTestCase {
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID + 1), eq(0L), eq(false), isA(LocationFactory.class)))
.andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID + 1)).andReturn(10L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID + 1)).andReturn(10L);
trackDataListener1.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener1);
trackDataListener1.onNewTrackPointsDone();
@@ -457,7 +457,7 @@ public class TrackDataHubTest extends AndroidTestCase {
dataSource.registerContentObserver(
eq(TrackPointsColumns.CONTENT_URI), capture(contentObserverCapture));
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(200L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(200L);
AndroidMock.expectLastCall().anyTimes();
FixedSizeLocationIterator locationIterator1 = new FixedSizeLocationIterator(
1, 200, 4, 25, 71, 120);
@@ -497,7 +497,7 @@ public class TrackDataHubTest extends AndroidTestCase {
FixedSizeLocationIterator locationIterator = new FixedSizeLocationIterator(1, 30, 5);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(30L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(30L);
trackDataListener1.clearTrackPoints();
locationIterator.expectLocationsDelivered(trackDataListener1);
@@ -514,7 +514,7 @@ public class TrackDataHubTest extends AndroidTestCase {
locationIterator = new FixedSizeLocationIterator(31, 30);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(31L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(60L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(60L);
locationIterator.expectSampledLocationsDelivered(trackDataListener1, 2, false);
trackDataListener1.onNewTrackPointsDone();
replay();
@@ -526,7 +526,7 @@ public class TrackDataHubTest extends AndroidTestCase {
locationIterator = new FixedSizeLocationIterator(1, 90);
expect(myTracksProviderUtils.getLocationIterator(
eq(TRACK_ID), eq(0L), eq(false), isA(LocationFactory.class))).andReturn(locationIterator);
expect(myTracksProviderUtils.getLastLocationId(TRACK_ID)).andReturn(90L);
expect(myTracksProviderUtils.getLastTrackLocationId(TRACK_ID)).andReturn(90L);
trackDataListener1.clearTrackPoints();
locationIterator.expectSampledLocationsDelivered(trackDataListener1, 2, false);
trackDataListener1.onNewTrackPointsDone();
@@ -148,7 +148,7 @@ public class GpxImporterTest extends AndroidTestCase {
expect(providerUtils.insertTrack(AndroidMock.capture(trackParam)))
.andReturn(TRACK_ID_URI);
expect(providerUtils.getLastLocationId(TRACK_ID)).andReturn(TRACK_POINT_ID_1).andReturn(TRACK_POINT_ID_2);
expect(providerUtils.getLastTrackLocationId(TRACK_ID)).andReturn(TRACK_POINT_ID_1).andReturn(TRACK_POINT_ID_2);
// A flush happens after the first insertion to get the starting point ID,
// which is why we get two calls
@@ -225,7 +225,7 @@ public class GpxImporterTest extends AndroidTestCase {
expect(providerUtils.bulkInsertTrackPoints((Location[]) AndroidMock.anyObject(),
AndroidMock.anyInt(), AndroidMock.anyLong())).andStubReturn(1);
expect(providerUtils.getLastLocationId(TRACK_ID)).andStubReturn(TRACK_POINT_ID_1);
expect(providerUtils.getLastTrackLocationId(TRACK_ID)).andStubReturn(TRACK_POINT_ID_1);
providerUtils.deleteTrack(TRACK_ID);
@@ -17,7 +17,7 @@ package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.content.DescriptionGenerator;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.stats.TripStatistics;
import android.annotation.TargetApi;
import android.location.Location;
@@ -49,7 +49,7 @@ public class KmlTrackWriterTest extends TrackFormatWriterTest {
}
@Override
public String generateWaypointDescription(Waypoint waypoint) {
public String generateWaypointDescription(TripStatistics tripStatistics) {
return null;
}
}