diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtils.java index 431fb1b50..04ad9c201 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtils.java @@ -41,7 +41,7 @@ public class SendToGoogleUtils { /** * Prepares a list of locations to send to Google Maps or Google Fusion * Tables. Splits the locations into segments if necessary. - * + * * @param track the track * @param locations the list of locations * @return an array of split segments. @@ -50,83 +50,66 @@ public class SendToGoogleUtils { ArrayList splitTracks = new ArrayList(); // Create a new segment + Track segment = createNewSegment( + track, locations.size() > 0 ? locations.get(0).getTime() : -1L); + + for (Location location : locations) { + /* + * Latitude is greater than 90 if the location is a pause/resume + * separator. + */ + if (location.getLatitude() > 90) { + endSegment(segment, location.getTime(), splitTracks); + segment = createNewSegment(track, location.getTime()); + } else { + segment.addLocation(location); + } + } + endSegment(segment, locations.size() > 0 ? locations.get(locations.size() - 1).getTime() : -1L, + splitTracks); + return splitTracks; + } + + /** + * Creates a new segment for a track. + * + * @param track the track + * @param startTime the segment start time + */ + private static Track createNewSegment(Track track, long startTime) { Track segment = new Track(); segment.setId(track.getId()); segment.setName(track.getName()); segment.setDescription(""); segment.setCategory(track.getCategory()); - - TripStatistics segmentStats = segment.getTripStatistics(); - TripStatistics trackStats = track.getTripStatistics(); - segmentStats.setStartTime(trackStats.getStartTime()); - segmentStats.setStopTime(trackStats.getStopTime()); - boolean startNewTrackSegment = false; - for (Location loc : locations) { - // Latitude is greater than 90 if the location is invalid. Do not add to - // the segment. - if (loc.getLatitude() > 90) { - startNewTrackSegment = true; - } - - if (startNewTrackSegment) { - // Close the last segment - prepareTrackSegment(segment, splitTracks); - - startNewTrackSegment = false; - segment = new Track(); - segment.setId(track.getId()); - segment.setName(track.getName()); - segment.setDescription(""); - segment.setCategory(track.getCategory()); - segmentStats = segment.getTripStatistics(); - } - - if (loc.getLatitude() <= 90) { - segment.addLocation(loc); - - // For a new segment, sets its start time using the first available - // location time. - if (segmentStats.getStartTime() < 0) { - segmentStats.setStartTime(loc.getTime()); - } - } - } - - prepareTrackSegment(segment, splitTracks); - - return splitTracks; + TripStatistics segmentTripStatistics = segment.getTripStatistics(); + segmentTripStatistics.setStartTime(startTime); + return segment; } /** - * Prepares a track segment for sending to Google Maps or Google Fusion - * Tables. The main steps are: - * - * The prepared track will be added to the splitTracks. - * - * @param segment the track segment - * @param splitTracks an array of track segments + * Ends a segment. Adds to the array of track segments if the segment is + * valid. + * + * @param segment the segment + * @param stopTime the stop time + * @param splitTracks the array of track segments */ @VisibleForTesting - static boolean prepareTrackSegment(Track segment, ArrayList splitTracks) { + static boolean endSegment(Track segment, long stopTime, ArrayList splitTracks) { // Make sure the segment has at least 2 points if (segment.getLocations().size() < 2) { Log.d(TAG, "segment has less than 2 points"); return false; } - // For a new segment, sets it stop time - TripStatistics segmentStats = segment.getTripStatistics(); - if (segmentStats.getStopTime() < 0) { - Location lastLocation = segment.getLocations().get(segment.getLocations().size() - 1); - segmentStats.setStopTime(lastLocation.getTime()); - } + // Set its stop time + segment.getTripStatistics().setStopTime(stopTime); - // Decimate to 2 meter precision. Google Maps and Google Fusion Tables do - // not like the locations to be too precise. + /* + * Decimate to 2 meter precision. Google Maps and Google Fusion Tables do + * not like the locations to be too precise. + */ LocationUtils.decimate(segment, 2.0); splitTracks.add(segment); diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/tasks/TimerTaskExecutor.java b/MyTracks/src/com/google/android/apps/mytracks/services/tasks/TimerTaskExecutor.java index 8335116fc..50bcd85af 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/tasks/TimerTaskExecutor.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/tasks/TimerTaskExecutor.java @@ -72,11 +72,7 @@ public class TimerTaskExecutor { return; } - long now = System.currentTimeMillis(); - long next = tripStatistics.getStartTime(); - if (next < now) { - next = now + interval - ((now - next) % interval); - } + long next = System.currentTimeMillis() + interval - (tripStatistics.getTotalTime() % interval); timer.scheduleAtFixedRate(new PeriodicTimerTask(), new Date(next), interval); } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java b/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java index f493307c9..67cecdf5e 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java @@ -607,7 +607,7 @@ public class EndToEndTestUtils { return findResult; } showMenuItem(); - findResult = SOLO.getText(menuName) != null; + findResult = SOLO.searchText(menuName); } else { // Non-ICS phone. SOLO.sendKey(KeyEvent.KEYCODE_MENU); @@ -615,7 +615,7 @@ public class EndToEndTestUtils { findResult = true; } else if (SOLO.searchText(MENU_MORE)) { SOLO.clickOnText(MENU_MORE); - findResult = SOLO.getText(menuName) != null; + findResult = SOLO.searchText(menuName); isMoreMenuOpened = true; } } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtilsTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtilsTest.java index 3038bd8e8..5577d5c64 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtilsTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendToGoogleUtilsTest.java @@ -41,47 +41,30 @@ public class SendToGoogleUtilsTest extends TestCase { /** * Tests the method - * {@link SendToGoogleUtils#prepareTrackSegment(Track, java.util.ArrayList)} + * {@link SendToGoogleUtils#endSegment(Track, long, ArrayList)} * when there is only one location in a segment. */ public void testPrepareTrackSegment_onlyOneLocation() { - Track trackStub = TrackStubUtils.createTrack(1); - assertFalse(SendToGoogleUtils.prepareTrackSegment(trackStub, null)); + Track segment = TrackStubUtils.createTrack(1); + assertFalse(SendToGoogleUtils.endSegment(segment, -1L, null)); } /** * Tests the method - * {@link SendToGoogleUtils#prepareTrackSegment(Track, ArrayList)} when there + * {@link SendToGoogleUtils#endSegment(Track, long, ArrayList)} when there * is no stop time. */ public void testPrepareTrackSegment_noStopTime() { - Track trackStub = TrackStubUtils.createTrack(2); - assertEquals(-1L, trackStub.getTripStatistics().getStopTime()); + Track segment = TrackStubUtils.createTrack(2); + assertEquals(-1L, segment.getTripStatistics().getStopTime()); ArrayList tracksArray = new ArrayList(); - assertTrue(SendToGoogleUtils.prepareTrackSegment(trackStub, tracksArray)); - assertEquals(trackStub, tracksArray.get(0)); + assertTrue(SendToGoogleUtils.endSegment( + segment, segment.getLocations().get(1).getTime(), tracksArray)); + assertEquals(segment, tracksArray.get(0)); // The stop time should be the time of last location assertEquals( - trackStub.getLocations().get(1).getTime(), trackStub.getTripStatistics().getStopTime()); - } - - /** - * Tests the method - * {@link SendToGoogleUtils#prepareTrackSegment(Track, java.util.ArrayList)} - * when there is stop time. - */ - public void testPrepareTrackSegment_hasStopTime() { - Track trackStub = TrackStubUtils.createTrack(2); - // Gives a margin to make sure the this time will be not same with the last - // location in the track. - long stopTime = System.currentTimeMillis() + 1000; - trackStub.getTripStatistics().setStopTime(stopTime); - - ArrayList tracksArray = new ArrayList(); - SendToGoogleUtils.prepareTrackSegment(trackStub, tracksArray); - assertEquals(trackStub, tracksArray.get(0)); - assertEquals(stopTime, trackStub.getTripStatistics().getStopTime()); + segment.getLocations().get(1).getTime(), segment.getTripStatistics().getStopTime()); } /**