Fix several bugs and clean up statistics management.

1) Decouple clock from TripStatisticsBuilder.
2) Introduce a new param to constructor to automatically resume at given time
3) Fix various statsbuilder initialization issues in the service (wrong order, missing initialization)
This commit is contained in:
Bartlomiej Niechwiej
2010-10-24 03:30:58 -07:00
parent ee4f334296
commit fc06701009
4 changed files with 27 additions and 42 deletions
@@ -468,8 +468,7 @@ public class GpxImporter extends DefaultHandler {
// initialize start time with time of first track point // initialize start time with time of first track point
if (statsBuilder == null) { if (statsBuilder == null) {
statsBuilder = new TripStatisticsBuilder(); statsBuilder = new TripStatisticsBuilder(time);
statsBuilder.resumeAt(time);
} }
} }
@@ -94,8 +94,8 @@ public class TrackRecordingService extends Service implements LocationListener {
*/ */
private MyTracksProviderUtils providerUtils; private MyTracksProviderUtils providerUtils;
private TripStatisticsBuilder statsBuilder = new TripStatisticsBuilder(); private TripStatisticsBuilder statsBuilder;
private TripStatisticsBuilder waypointStatsBuilder = new TripStatisticsBuilder(); private TripStatisticsBuilder waypointStatsBuilder;
/** /**
* Current length of the recorded track. This length is calculated from the * Current length of the recorded track. This length is calculated from the
@@ -395,8 +395,7 @@ public class TrackRecordingService extends Service implements LocationListener {
"Restoring stats of track with ID: " + track.getId()); "Restoring stats of track with ID: " + track.getId());
TripStatistics stats = track.getStatistics(); TripStatistics stats = track.getStatistics();
statsBuilder = new TripStatisticsBuilder(); statsBuilder = new TripStatisticsBuilder(stats.getStartTime());
statsBuilder.resumeAt(stats.getStartTime());
setUpAnnouncer(); setUpAnnouncer();
signalManager.restore(); signalManager.restore();
@@ -407,11 +406,11 @@ public class TrackRecordingService extends Service implements LocationListener {
Waypoint waypoint = providerUtils.getFirstWaypoint(recordingTrackId); Waypoint waypoint = providerUtils.getFirstWaypoint(recordingTrackId);
if (waypoint != null) { if (waypoint != null) {
currentWaypointId = waypoint.getId(); currentWaypointId = waypoint.getId();
waypointStatsBuilder = new TripStatisticsBuilder(waypoint.getStatistics()); waypointStatsBuilder = new TripStatisticsBuilder(
waypoint.getStatistics());
} else { } else {
// This should never happen, but we got to do something so life goes on: // This should never happen, but we got to do something so life goes on:
waypointStatsBuilder = new TripStatisticsBuilder(); waypointStatsBuilder = new TripStatisticsBuilder(stats.getStartTime());
waypointStatsBuilder.resumeAt(stats.getStartTime());
currentWaypointId = -1; currentWaypointId = -1;
} }
@@ -435,7 +434,7 @@ public class TrackRecordingService extends Service implements LocationListener {
} }
statsBuilder.getStatistics().setMovingTime(stats.getMovingTime()); statsBuilder.getStatistics().setMovingTime(stats.getMovingTime());
statsBuilder.pauseAt(stats.getStopTime()); statsBuilder.pauseAt(stats.getStopTime());
statsBuilder.resume(); statsBuilder.resumeAt(System.currentTimeMillis());
} else { } else {
Log.e(MyTracksConstants.TAG, "Could not get track points cursor."); Log.e(MyTracksConstants.TAG, "Could not get track points cursor.");
} }
@@ -851,8 +850,7 @@ public class TrackRecordingService extends Service implements LocationListener {
Uri uri = providerUtils.insertWaypoint(waypoint); Uri uri = providerUtils.insertWaypoint(waypoint);
// Create a new stats keeper for the next marker // Create a new stats keeper for the next marker
waypointStatsBuilder = new TripStatisticsBuilder(); waypointStatsBuilder = new TripStatisticsBuilder(time);
waypointStatsBuilder.resumeAt(time);
updateCurrentWaypoint(); updateCurrentWaypoint();
return Long.parseLong(uri.getLastPathSegment()); return Long.parseLong(uri.getLastPathSegment());
} }
@@ -972,11 +970,11 @@ public class TrackRecordingService extends Service implements LocationListener {
track.setId(recordingTrackId); track.setId(recordingTrackId);
track.setName(String.format(getString(R.string.new_track), recordingTrackId)); track.setName(String.format(getString(R.string.new_track), recordingTrackId));
providerUtils.updateTrack(track); providerUtils.updateTrack(track);
statsBuilder = new TripStatisticsBuilder(startTime);
waypointStatsBuilder = new TripStatisticsBuilder(startTime);
currentWaypointId = insertStatisticsMarker(null); currentWaypointId = insertStatisticsMarker(null);
isRecording = true; isRecording = true;
isMoving = true; isMoving = true;
statsBuilder = new TripStatisticsBuilder();
statsBuilder.resumeAt(startTime);
setUpAnnouncer(); setUpAnnouncer();
length = 0; length = 0;
showNotification(); showNotification();
@@ -36,13 +36,13 @@ public class TripStatisticsBuilder {
/** /**
* The last location that the gps reported. * The last location that the gps reported.
*/ */
private Location lastLocation = null; private Location lastLocation;
/** /**
* The last location that contributed to the stats. It is also the last * The last location that contributed to the stats. It is also the last
* location the user was found to be moving. * location the user was found to be moving.
*/ */
private Location lastMovingLocation = null; private Location lastMovingLocation;
/** /**
* The current speed in meters/second as reported by the gps. * The current speed in meters/second as reported by the gps.
@@ -90,10 +90,13 @@ public class TripStatisticsBuilder {
private long totalLocations = 0; private long totalLocations = 0;
/** /**
* Creates a new trip starting at the current system time. * Creates a new trip starting at the given time.
*
* @param startTime the start time.
*/ */
public TripStatisticsBuilder() { public TripStatisticsBuilder(long startTime) {
data = new TripStatistics(); data = new TripStatistics();
resumeAt(startTime);
} }
/** /**
@@ -103,6 +106,9 @@ public class TripStatisticsBuilder {
*/ */
public TripStatisticsBuilder(TripStatistics statsData) { public TripStatisticsBuilder(TripStatistics statsData) {
data = new TripStatistics(statsData); data = new TripStatistics(statsData);
if (data.getStartTime() > 0) {
resumeAt(data.getStartTime());
}
} }
/** /**
@@ -140,8 +146,8 @@ public class TripStatisticsBuilder {
// Don't do anything if we didn't move since last fix: // Don't do anything if we didn't move since last fix:
double distance = lastLocation.distanceTo(currentLocation); double distance = lastLocation.distanceTo(currentLocation);
if (distance < MyTracksConstants.MAX_NO_MOVEMENT_DISTANCE if (distance < MyTracksConstants.MAX_NO_MOVEMENT_DISTANCE &&
&& currentSpeed < MyTracksConstants.MAX_NO_MOVEMENT_SPEED) { currentSpeed < MyTracksConstants.MAX_NO_MOVEMENT_SPEED) {
lastLocation = currentLocation; lastLocation = currentLocation;
return false; return false;
} }
@@ -291,13 +297,6 @@ public class TripStatisticsBuilder {
data.updateGradeExtremities(gradeBuffer.getAverage()); data.updateGradeExtremities(gradeBuffer.getAverage());
} }
/**
* Pauses the track at the current time.
*/
public void pause() {
pauseAt(System.currentTimeMillis());
}
/** /**
* Pauses the track at the given time. * Pauses the track at the given time.
* *
@@ -312,13 +311,6 @@ public class TripStatisticsBuilder {
paused = true; paused = true;
} }
/**
* Resumes the current track at the current time.
*/
public void resume() {
resumeAt(System.currentTimeMillis());
}
/** /**
* Resumes the current track at the given time. * Resumes the current track at the given time.
* *
@@ -364,8 +356,7 @@ public class TripStatisticsBuilder {
} }
public TripStatistics getStatistics() { public TripStatistics getStatistics() {
// Take a snapshot - we do't want anyone messing with our internals // Take a snapshot - we don't want anyone messing with our internals
return new TripStatistics(data); return new TripStatistics(data);
} }
} }
@@ -20,13 +20,11 @@ public class TripStatisticsBuilderTest extends TestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
builder = new TripStatisticsBuilder(); builder = new TripStatisticsBuilder(System.currentTimeMillis());
builder.resume();
} }
public void testAddLocationSimple() throws Exception { public void testAddLocationSimple() throws Exception {
builder = new TripStatisticsBuilder(); builder = new TripStatisticsBuilder(1000);
builder.resumeAt(1000);
TripStatistics stats = builder.getStatistics(); TripStatistics stats = builder.getStatistics();
assertEquals(0.0, builder.getSmoothedElevation()); assertEquals(0.0, builder.getSmoothedElevation());
@@ -84,8 +82,7 @@ public class TripStatisticsBuilderTest extends TestCase {
*/ */
public void testElevationSimple() throws Exception { public void testElevationSimple() throws Exception {
for (double elevation = 0; elevation < 1000; elevation += 10) { for (double elevation = 0; elevation < 1000; elevation += 10) {
builder = new TripStatisticsBuilder(); builder = new TripStatisticsBuilder(System.currentTimeMillis());
builder.resume();
for (int j = 0; j < 100; j++) { for (int j = 0; j < 100; j++) {
assertEquals(0.0, builder.updateElevation(elevation)); assertEquals(0.0, builder.updateElevation(elevation));
assertEquals(elevation, builder.getSmoothedElevation()); assertEquals(elevation, builder.getSmoothedElevation());