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.
*/