From feb479d9b9ec60cdd8d495ed8a67d2b53e7a9969 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Tue, 7 May 2013 15:58:37 -0700 Subject: [PATCH] Change TrackFormatWriter to be able to write multiple tracks to a file --- .../apps/mytracks/io/file/CsvTrackWriter.java | 10 ++-- .../apps/mytracks/io/file/GpxTrackWriter.java | 10 ++-- .../apps/mytracks/io/file/KmlTrackWriter.java | 10 ++-- .../apps/mytracks/io/file/TcxTrackWriter.java | 12 ++-- .../mytracks/io/file/TrackFormatWriter.java | 59 +++++++++++-------- .../apps/mytracks/io/file/TrackWriter.java | 12 ++-- .../io/file/TrackFormatWriterTest.java | 8 +-- .../mytracks/io/file/TrackWriterTest.java | 51 ++++++++++++---- 8 files changed, 99 insertions(+), 73 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/CsvTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/CsvTrackWriter.java index 641b24d72..5f44604ed 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/CsvTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/CsvTrackWriter.java @@ -58,7 +58,6 @@ public class CsvTrackWriter implements TrackFormatWriter { private final Context context; private PrintWriter printWriter; - private Track track; private int segmentIndex; private int pointIndex; @@ -72,8 +71,7 @@ public class CsvTrackWriter implements TrackFormatWriter { } @Override - public void prepare(Track aTrack, OutputStream out) { - track = aTrack; + public void prepare(OutputStream out) { printWriter = new PrintWriter(out); segmentIndex = 0; pointIndex = 0; @@ -85,7 +83,7 @@ public class CsvTrackWriter implements TrackFormatWriter { } @Override - public void writeHeader() { + public void writeHeader(Track track) { writeCommaSeparatedLine(context.getString(R.string.generic_name), context.getString(R.string.track_edit_activity_type_hint), context.getString(R.string.generic_description)); @@ -133,7 +131,7 @@ public class CsvTrackWriter implements TrackFormatWriter { } @Override - public void writeBeginTrack(Location firstPoint) { + public void writeBeginTrack(Track track, Location firstPoint) { writeCommaSeparatedLine(context.getString(R.string.description_track_segment), context.getString(R.string.description_track_point), context.getString(R.string.description_location_latitude), @@ -149,7 +147,7 @@ public class CsvTrackWriter implements TrackFormatWriter { } @Override - public void writeEndTrack(Location lastPoint) { + public void writeEndTrack(Track track, Location lastPoint) { // Do nothing } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxTrackWriter.java index ab61e4140..7161f77e2 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxTrackWriter.java @@ -52,7 +52,6 @@ public class GpxTrackWriter implements TrackFormatWriter { } private final Context context; - private Track track; private PrintWriter printWriter; public GpxTrackWriter(Context context) { @@ -65,8 +64,7 @@ public class GpxTrackWriter implements TrackFormatWriter { } @Override - public void prepare(Track aTrack, OutputStream outputStream) { - this.track = aTrack; + public void prepare(OutputStream outputStream) { this.printWriter = new PrintWriter(outputStream); } @@ -79,7 +77,7 @@ public class GpxTrackWriter implements TrackFormatWriter { } @Override - public void writeHeader() { + public void writeHeader(Track track) { if (printWriter != null) { printWriter.println(""); printWriter.println(""); printWriter.println("" + StringUtils.formatCData(track.getName()) + ""); @@ -120,7 +118,7 @@ public class GpxTrackWriter implements TrackFormatWriter { } @Override - public void writeEndTrack(Location lastLocation) { + public void writeEndTrack(Track track, Location lastLocation) { if (printWriter != null) { printWriter.println(""); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java index 25e7f0539..d3307fee6 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java @@ -70,7 +70,6 @@ public class KmlTrackWriter implements TrackFormatWriter { private final Context context; private final DescriptionGenerator descriptionGenerator; - private Track track; private PrintWriter printWriter; private ArrayList powerList = new ArrayList(); private ArrayList cadenceList = new ArrayList(); @@ -95,8 +94,7 @@ public class KmlTrackWriter implements TrackFormatWriter { } @Override - public void prepare(Track aTrack, OutputStream outputStream) { - this.track = aTrack; + public void prepare(OutputStream outputStream) { this.printWriter = new PrintWriter(outputStream); } @@ -109,7 +107,7 @@ public class KmlTrackWriter implements TrackFormatWriter { } @Override - public void writeHeader() { + public void writeHeader(Track track) { if (printWriter != null) { printWriter.println(""); printWriter.println(""); printWriter.println(""); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/TcxTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/TcxTrackWriter.java index 000245351..27dfa8b60 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/TcxTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/TcxTrackWriter.java @@ -84,7 +84,6 @@ public class TcxTrackWriter implements TrackFormatWriter { R.string.activity_type_walking }; private final Context context; - private Track track; private PrintWriter printWriter; private SportType sportType; @@ -93,10 +92,8 @@ public class TcxTrackWriter implements TrackFormatWriter { } @Override - public void prepare(Track aTrack, OutputStream out) { - this.track = aTrack; + public void prepare(OutputStream out) { this.printWriter = new PrintWriter(out); - this.sportType = getSportType(track.getCategory()); } @Override @@ -113,7 +110,7 @@ public class TcxTrackWriter implements TrackFormatWriter { } @Override - public void writeHeader() { + public void writeHeader(Track track) { if (printWriter != null) { printWriter.println(""); printWriter.println(""); printWriter.println("" + StringUtils.formatCData(track.getDescription()) + ""); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackFormatWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackFormatWriter.java index bcbe7b95d..79b61b5f3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackFormatWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackFormatWriter.java @@ -23,26 +23,30 @@ import android.location.Location; import java.io.OutputStream; /** - * Interface for writing a track to file. + * Interface for writing tracks to a file. * * The expected sequence of calls is: - *
    - *
  1. {@link #prepare} - *
  2. {@link #writeHeader} - *
  3. {@link #writeBeginWaypoints} - *
  4. For each waypoint: {@link #writeWaypoint} - *
  5. {@link #writeEndWaypoints} - *
  6. {@link #writeBeginTrack} - *
  7. For each segment: - *
      - *
    1. {@link #writeOpenSegment} - *
    2. For each location in the segment: {@link #writeLocation} - *
    3. {@link #writeCloseSegment} - *
    - *
  8. {@link #writeEndTrack} - *
  9. {@link #writeFooter} - *
  10. {@link #close} - *
+ *
    + *
  • {@link #prepare}
  • + *
  • {@link #writeHeader}
  • + *
  • For each track:
  • + *
      + *
    • {@link #writeBeginWaypoints} + *
    • For each waypoint: {@link #writeWaypoint} + *
    • {@link #writeEndWaypoints} + *
    • {@link #writeBeginTrack} + *
    • For each segment: + *
        + *
      • {@link #writeOpenSegment}
      • + *
      • For each location in the segment: {@link #writeLocation}
      • + *
      • {@link #writeCloseSegment}
      • + *
      + *
    • + *
    • {@link #writeEndTrack}
    • + *
    + *
  • {@link #writeFooter}
  • + *
  • {@link #close}
  • + *
* * @author Rodrigo Damazio */ @@ -54,12 +58,11 @@ public interface TrackFormatWriter { public String getExtension(); /** - * Sets up the writer to write the given track. + * Sets up the file handler. * - * @param track the track to write - * @param outputStream the output stream to write the track to + * @param outputStream the output stream for the file handler */ - public void prepare(Track track, OutputStream outputStream); + public void prepare(OutputStream outputStream); /** * Closes the underlying file handler. @@ -67,9 +70,11 @@ public interface TrackFormatWriter { public void close(); /** - * Writes the header. + * Writes the header for a file + * + * @param track the track */ - public void writeHeader(); + public void writeHeader(Track track); /** * Writes the footer. @@ -96,16 +101,18 @@ public interface TrackFormatWriter { /** * Writes the beginning of the track. * + * @param track the track * @param firstLocation the first location */ - public void writeBeginTrack(Location firstLocation); + public void writeBeginTrack(Track track, Location firstLocation); /** * Writes the end of the track. * + * @param track the track * @param lastLocation the last location */ - public void writeEndTrack(Location lastLocation); + public void writeEndTrack(Track track, Location lastLocation); /** * Writes the statements necessary to open a new segment. diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackWriter.java index e937ef44e..807d44b6a 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/TrackWriter.java @@ -105,8 +105,8 @@ public class TrackWriter { @Override public void run() { try { - trackFormatWriter.prepare(track, outputStream); - trackFormatWriter.writeHeader(); + trackFormatWriter.prepare(outputStream); + trackFormatWriter.writeHeader(track); writeWaypoints(); writeLocations(); trackFormatWriter.writeFooter(); @@ -206,7 +206,7 @@ public class TrackWriter { boolean isSegmentValid = isLocationValid && isLastLocationValid; if (!wroteTrack && isSegmentValid) { // Found the first two consecutive locations that are valid - trackFormatWriter.writeBeginTrack(locationFactory.lastLocation); + trackFormatWriter.writeBeginTrack(track, locationFactory.lastLocation); wroteTrack = true; } @@ -241,11 +241,11 @@ public class TrackWriter { } if (wroteTrack) { Location lastValidTrackPoint = myTracksProviderUtils.getLastValidTrackPoint(track.getId()); - trackFormatWriter.writeEndTrack(lastValidTrackPoint); + trackFormatWriter.writeEndTrack(track, lastValidTrackPoint); } else { // Write an empty track - trackFormatWriter.writeBeginTrack(null); - trackFormatWriter.writeEndTrack(null); + trackFormatWriter.writeBeginTrack(track, null); + trackFormatWriter.writeEndTrack(track, null); } } finally { iterator.close(); diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackFormatWriterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackFormatWriterTest.java index c74feb0f1..41c7e6a92 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackFormatWriterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackFormatWriterTest.java @@ -115,13 +115,13 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase { */ protected String writeTrack(TrackFormatWriter writer) throws Exception { OutputStream output = new ByteArrayOutputStream(BUFFER_SIZE); - writer.prepare(track, output); - writer.writeHeader(); + writer.prepare(output); + writer.writeHeader(track); writer.writeBeginWaypoints(); writer.writeWaypoint(wp1); writer.writeWaypoint(wp2); writer.writeEndWaypoints(); - writer.writeBeginTrack(location1); + writer.writeBeginTrack(track, location1); writer.writeOpenSegment(); writer.writeLocation(location1); writer.writeLocation(location2); @@ -130,7 +130,7 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase { writer.writeLocation(location3); writer.writeLocation(location4); writer.writeCloseSegment(); - writer.writeEndTrack(location4); + writer.writeEndTrack(track, location4); writer.writeFooter(); writer.close(); return output.toString(); diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackWriterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackWriterTest.java index 9c25bf72a..fdb0eb19a 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackWriterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/TrackWriterTest.java @@ -91,10 +91,10 @@ public class TrackWriterTest extends AndroidTestCase { public void testWriteTrack_emptyTrack() throws Exception { // Set expected mock behavior - trackFormatWriter.prepare(track, outputStream); - trackFormatWriter.writeHeader(); - trackFormatWriter.writeBeginTrack(null); - trackFormatWriter.writeEndTrack(null); + trackFormatWriter.prepare(outputStream); + trackFormatWriter.writeHeader(track); + trackFormatWriter.writeBeginTrack(track, null); + trackFormatWriter.writeEndTrack(track, null); trackFormatWriter.writeFooter(); trackFormatWriter.close(); @@ -123,10 +123,10 @@ public class TrackWriterTest extends AndroidTestCase { myTracksProviderUtils.bulkInsertTrackPoint(locations, locations.length, TRACK_ID)); // Set expected mock behavior - trackFormatWriter.prepare(track, outputStream); - trackFormatWriter.writeHeader(); - trackFormatWriter.writeBeginTrack(null); - trackFormatWriter.writeEndTrack(null); + trackFormatWriter.prepare(outputStream); + trackFormatWriter.writeHeader(track); + trackFormatWriter.writeBeginTrack(track, null); + trackFormatWriter.writeEndTrack(track, null); trackFormatWriter.writeFooter(); trackFormatWriter.close(); @@ -162,8 +162,8 @@ public class TrackWriterTest extends AndroidTestCase { waypoint.setId(i + 1); } - trackFormatWriter.prepare(track, outputStream); - trackFormatWriter.writeHeader(); + trackFormatWriter.prepare(outputStream); + trackFormatWriter.writeHeader(track); // Expect reading/writing of the waypoints (except the first) trackFormatWriter.writeBeginWaypoints(); @@ -172,7 +172,7 @@ public class TrackWriterTest extends AndroidTestCase { trackFormatWriter.writeEndWaypoints(); // Begin the track - trackFormatWriter.writeBeginTrack(locationEq(locations[0])); + trackFormatWriter.writeBeginTrack(trackEq(track), locationEq(locations[0])); // Write locations 1-2 trackFormatWriter.writeOpenSegment(); @@ -190,7 +190,7 @@ public class TrackWriterTest extends AndroidTestCase { trackFormatWriter.writeCloseSegment(); // End the track - trackFormatWriter.writeEndTrack(locationEq(locations[5])); + trackFormatWriter.writeEndTrack(trackEq(track), locationEq(locations[5])); trackFormatWriter.writeFooter(); trackFormatWriter.close(); @@ -266,6 +266,33 @@ public class TrackWriterTest extends AndroidTestCase { return null; } + /** + * Track equals. + * + * @param track1 the track + */ + private Track trackEq(final Track track1) { + EasyMock.reportMatcher(new IArgumentMatcher() { + @Override + public boolean matches(Object object) { + if (object == null || track1 == null) { + return track1 == object; + } + Track track2 = (Track) object; + + return track1.getName().equals(track2.getName()); + } + + @Override + public void appendTo(StringBuffer buffer) { + buffer.append("trackEq("); + buffer.append(track1); + buffer.append(")"); + } + }); + return null; + } + /** * Fills the locations. *