Change TrackFormatWriter to be able to write multiple tracks to a file

This commit is contained in:
Jimmy Shih
2013-05-07 15:58:37 -07:00
parent dfa60a44f6
commit feb479d9b9
8 changed files with 99 additions and 73 deletions
@@ -58,7 +58,6 @@ public class CsvTrackWriter implements TrackFormatWriter {
private final Context context; private final Context context;
private PrintWriter printWriter; private PrintWriter printWriter;
private Track track;
private int segmentIndex; private int segmentIndex;
private int pointIndex; private int pointIndex;
@@ -72,8 +71,7 @@ public class CsvTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void prepare(Track aTrack, OutputStream out) { public void prepare(OutputStream out) {
track = aTrack;
printWriter = new PrintWriter(out); printWriter = new PrintWriter(out);
segmentIndex = 0; segmentIndex = 0;
pointIndex = 0; pointIndex = 0;
@@ -85,7 +83,7 @@ public class CsvTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeHeader() { public void writeHeader(Track track) {
writeCommaSeparatedLine(context.getString(R.string.generic_name), writeCommaSeparatedLine(context.getString(R.string.generic_name),
context.getString(R.string.track_edit_activity_type_hint), context.getString(R.string.track_edit_activity_type_hint),
context.getString(R.string.generic_description)); context.getString(R.string.generic_description));
@@ -133,7 +131,7 @@ public class CsvTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeBeginTrack(Location firstPoint) { public void writeBeginTrack(Track track, Location firstPoint) {
writeCommaSeparatedLine(context.getString(R.string.description_track_segment), writeCommaSeparatedLine(context.getString(R.string.description_track_segment),
context.getString(R.string.description_track_point), context.getString(R.string.description_track_point),
context.getString(R.string.description_location_latitude), context.getString(R.string.description_location_latitude),
@@ -149,7 +147,7 @@ public class CsvTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeEndTrack(Location lastPoint) { public void writeEndTrack(Track track, Location lastPoint) {
// Do nothing // Do nothing
} }
@@ -52,7 +52,6 @@ public class GpxTrackWriter implements TrackFormatWriter {
} }
private final Context context; private final Context context;
private Track track;
private PrintWriter printWriter; private PrintWriter printWriter;
public GpxTrackWriter(Context context) { public GpxTrackWriter(Context context) {
@@ -65,8 +64,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void prepare(Track aTrack, OutputStream outputStream) { public void prepare(OutputStream outputStream) {
this.track = aTrack;
this.printWriter = new PrintWriter(outputStream); this.printWriter = new PrintWriter(outputStream);
} }
@@ -79,7 +77,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeHeader() { public void writeHeader(Track track) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
printWriter.println("<gpx"); printWriter.println("<gpx");
@@ -109,7 +107,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeBeginTrack(Location firstLocation) { public void writeBeginTrack(Track track, Location firstLocation) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("<trk>"); printWriter.println("<trk>");
printWriter.println("<name>" + StringUtils.formatCData(track.getName()) + "</name>"); printWriter.println("<name>" + StringUtils.formatCData(track.getName()) + "</name>");
@@ -120,7 +118,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeEndTrack(Location lastLocation) { public void writeEndTrack(Track track, Location lastLocation) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("</trk>"); printWriter.println("</trk>");
} }
@@ -70,7 +70,6 @@ public class KmlTrackWriter implements TrackFormatWriter {
private final Context context; private final Context context;
private final DescriptionGenerator descriptionGenerator; private final DescriptionGenerator descriptionGenerator;
private Track track;
private PrintWriter printWriter; private PrintWriter printWriter;
private ArrayList<Integer> powerList = new ArrayList<Integer>(); private ArrayList<Integer> powerList = new ArrayList<Integer>();
private ArrayList<Integer> cadenceList = new ArrayList<Integer>(); private ArrayList<Integer> cadenceList = new ArrayList<Integer>();
@@ -95,8 +94,7 @@ public class KmlTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void prepare(Track aTrack, OutputStream outputStream) { public void prepare(OutputStream outputStream) {
this.track = aTrack;
this.printWriter = new PrintWriter(outputStream); this.printWriter = new PrintWriter(outputStream);
} }
@@ -109,7 +107,7 @@ public class KmlTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeHeader() { public void writeHeader(Track track) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
printWriter.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\""); printWriter.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\"");
@@ -170,7 +168,7 @@ public class KmlTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeBeginTrack(Location firstLocation) { public void writeBeginTrack(Track track, Location firstLocation) {
if (printWriter != null) { if (printWriter != null) {
String name = context.getString(R.string.marker_label_start, track.getName()); String name = context.getString(R.string.marker_label_start, track.getName());
writePlacemark(name, "", "", START_STYLE, firstLocation); writePlacemark(name, "", "", START_STYLE, firstLocation);
@@ -187,7 +185,7 @@ public class KmlTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeEndTrack(Location lastLocation) { public void writeEndTrack(Track track, Location lastLocation) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("</gx:MultiTrack>"); printWriter.println("</gx:MultiTrack>");
printWriter.println("</Placemark>"); printWriter.println("</Placemark>");
@@ -84,7 +84,6 @@ public class TcxTrackWriter implements TrackFormatWriter {
R.string.activity_type_walking }; R.string.activity_type_walking };
private final Context context; private final Context context;
private Track track;
private PrintWriter printWriter; private PrintWriter printWriter;
private SportType sportType; private SportType sportType;
@@ -93,10 +92,8 @@ public class TcxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void prepare(Track aTrack, OutputStream out) { public void prepare(OutputStream out) {
this.track = aTrack;
this.printWriter = new PrintWriter(out); this.printWriter = new PrintWriter(out);
this.sportType = getSportType(track.getCategory());
} }
@Override @Override
@@ -113,7 +110,7 @@ public class TcxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeHeader() { public void writeHeader(Track track) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
printWriter.println("<TrainingCenterDatabase" printWriter.println("<TrainingCenterDatabase"
@@ -144,7 +141,8 @@ public class TcxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeBeginTrack(Location firstPoint) { public void writeBeginTrack(Track track, Location firstPoint) {
sportType = getSportType(track.getCategory());
if (printWriter != null) { if (printWriter != null) {
String startTime = StringUtils.formatDateTimeIso8601( String startTime = StringUtils.formatDateTimeIso8601(
track.getTripStatistics().getStartTime()); track.getTripStatistics().getStartTime());
@@ -165,7 +163,7 @@ public class TcxTrackWriter implements TrackFormatWriter {
} }
@Override @Override
public void writeEndTrack(Location lastPoint) { public void writeEndTrack(Track track, Location lastPoint) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("</Lap>"); printWriter.println("</Lap>");
printWriter.println("<Notes>" + StringUtils.formatCData(track.getDescription()) + "</Notes>"); printWriter.println("<Notes>" + StringUtils.formatCData(track.getDescription()) + "</Notes>");
@@ -23,26 +23,30 @@ import android.location.Location;
import java.io.OutputStream; import java.io.OutputStream;
/** /**
* Interface for writing a track to file. * Interface for writing tracks to a file.
* *
* The expected sequence of calls is: * The expected sequence of calls is:
* <ol> * <ul>
* <li>{@link #prepare} * <li>{@link #prepare}</li>
* <li>{@link #writeHeader} * <li>{@link #writeHeader}</li>
* <li>{@link #writeBeginWaypoints} * <li>For each track:</li>
* <li>For each waypoint: {@link #writeWaypoint} * <ul>
* <li>{@link #writeEndWaypoints} * <li>{@link #writeBeginWaypoints}
* <li>{@link #writeBeginTrack} * <li>For each waypoint: {@link #writeWaypoint}
* <li>For each segment: * <li>{@link #writeEndWaypoints}
* <ol> * <li>{@link #writeBeginTrack}
* <li>{@link #writeOpenSegment} * <li>For each segment:
* <li>For each location in the segment: {@link #writeLocation} * <ul>
* <li>{@link #writeCloseSegment} * <li>{@link #writeOpenSegment}</li>
* </ol> * <li>For each location in the segment: {@link #writeLocation}</li>
* <li>{@link #writeEndTrack} * <li>{@link #writeCloseSegment}</li>
* <li>{@link #writeFooter} * </ul>
* <li>{@link #close} * </li>
* </ol> * <li>{@link #writeEndTrack}</li>
* </ul>
* <li>{@link #writeFooter}</li>
* <li>{@link #close}</li>
* </ul>
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
@@ -54,12 +58,11 @@ public interface TrackFormatWriter {
public String getExtension(); 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 for the file handler
* @param outputStream the output stream to write the track to
*/ */
public void prepare(Track track, OutputStream outputStream); public void prepare(OutputStream outputStream);
/** /**
* Closes the underlying file handler. * Closes the underlying file handler.
@@ -67,9 +70,11 @@ public interface TrackFormatWriter {
public void close(); 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. * Writes the footer.
@@ -96,16 +101,18 @@ public interface TrackFormatWriter {
/** /**
* Writes the beginning of the track. * Writes the beginning of the track.
* *
* @param track the track
* @param firstLocation the first location * @param firstLocation the first location
*/ */
public void writeBeginTrack(Location firstLocation); public void writeBeginTrack(Track track, Location firstLocation);
/** /**
* Writes the end of the track. * Writes the end of the track.
* *
* @param track the track
* @param lastLocation the last location * @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. * Writes the statements necessary to open a new segment.
@@ -105,8 +105,8 @@ public class TrackWriter {
@Override @Override
public void run() { public void run() {
try { try {
trackFormatWriter.prepare(track, outputStream); trackFormatWriter.prepare(outputStream);
trackFormatWriter.writeHeader(); trackFormatWriter.writeHeader(track);
writeWaypoints(); writeWaypoints();
writeLocations(); writeLocations();
trackFormatWriter.writeFooter(); trackFormatWriter.writeFooter();
@@ -206,7 +206,7 @@ public class TrackWriter {
boolean isSegmentValid = isLocationValid && isLastLocationValid; boolean isSegmentValid = isLocationValid && isLastLocationValid;
if (!wroteTrack && isSegmentValid) { if (!wroteTrack && isSegmentValid) {
// Found the first two consecutive locations that are valid // Found the first two consecutive locations that are valid
trackFormatWriter.writeBeginTrack(locationFactory.lastLocation); trackFormatWriter.writeBeginTrack(track, locationFactory.lastLocation);
wroteTrack = true; wroteTrack = true;
} }
@@ -241,11 +241,11 @@ public class TrackWriter {
} }
if (wroteTrack) { if (wroteTrack) {
Location lastValidTrackPoint = myTracksProviderUtils.getLastValidTrackPoint(track.getId()); Location lastValidTrackPoint = myTracksProviderUtils.getLastValidTrackPoint(track.getId());
trackFormatWriter.writeEndTrack(lastValidTrackPoint); trackFormatWriter.writeEndTrack(track, lastValidTrackPoint);
} else { } else {
// Write an empty track // Write an empty track
trackFormatWriter.writeBeginTrack(null); trackFormatWriter.writeBeginTrack(track, null);
trackFormatWriter.writeEndTrack(null); trackFormatWriter.writeEndTrack(track, null);
} }
} finally { } finally {
iterator.close(); iterator.close();
@@ -115,13 +115,13 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase {
*/ */
protected String writeTrack(TrackFormatWriter writer) throws Exception { protected String writeTrack(TrackFormatWriter writer) throws Exception {
OutputStream output = new ByteArrayOutputStream(BUFFER_SIZE); OutputStream output = new ByteArrayOutputStream(BUFFER_SIZE);
writer.prepare(track, output); writer.prepare(output);
writer.writeHeader(); writer.writeHeader(track);
writer.writeBeginWaypoints(); writer.writeBeginWaypoints();
writer.writeWaypoint(wp1); writer.writeWaypoint(wp1);
writer.writeWaypoint(wp2); writer.writeWaypoint(wp2);
writer.writeEndWaypoints(); writer.writeEndWaypoints();
writer.writeBeginTrack(location1); writer.writeBeginTrack(track, location1);
writer.writeOpenSegment(); writer.writeOpenSegment();
writer.writeLocation(location1); writer.writeLocation(location1);
writer.writeLocation(location2); writer.writeLocation(location2);
@@ -130,7 +130,7 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase {
writer.writeLocation(location3); writer.writeLocation(location3);
writer.writeLocation(location4); writer.writeLocation(location4);
writer.writeCloseSegment(); writer.writeCloseSegment();
writer.writeEndTrack(location4); writer.writeEndTrack(track, location4);
writer.writeFooter(); writer.writeFooter();
writer.close(); writer.close();
return output.toString(); return output.toString();
@@ -91,10 +91,10 @@ public class TrackWriterTest extends AndroidTestCase {
public void testWriteTrack_emptyTrack() throws Exception { public void testWriteTrack_emptyTrack() throws Exception {
// Set expected mock behavior // Set expected mock behavior
trackFormatWriter.prepare(track, outputStream); trackFormatWriter.prepare(outputStream);
trackFormatWriter.writeHeader(); trackFormatWriter.writeHeader(track);
trackFormatWriter.writeBeginTrack(null); trackFormatWriter.writeBeginTrack(track, null);
trackFormatWriter.writeEndTrack(null); trackFormatWriter.writeEndTrack(track, null);
trackFormatWriter.writeFooter(); trackFormatWriter.writeFooter();
trackFormatWriter.close(); trackFormatWriter.close();
@@ -123,10 +123,10 @@ public class TrackWriterTest extends AndroidTestCase {
myTracksProviderUtils.bulkInsertTrackPoint(locations, locations.length, TRACK_ID)); myTracksProviderUtils.bulkInsertTrackPoint(locations, locations.length, TRACK_ID));
// Set expected mock behavior // Set expected mock behavior
trackFormatWriter.prepare(track, outputStream); trackFormatWriter.prepare(outputStream);
trackFormatWriter.writeHeader(); trackFormatWriter.writeHeader(track);
trackFormatWriter.writeBeginTrack(null); trackFormatWriter.writeBeginTrack(track, null);
trackFormatWriter.writeEndTrack(null); trackFormatWriter.writeEndTrack(track, null);
trackFormatWriter.writeFooter(); trackFormatWriter.writeFooter();
trackFormatWriter.close(); trackFormatWriter.close();
@@ -162,8 +162,8 @@ public class TrackWriterTest extends AndroidTestCase {
waypoint.setId(i + 1); waypoint.setId(i + 1);
} }
trackFormatWriter.prepare(track, outputStream); trackFormatWriter.prepare(outputStream);
trackFormatWriter.writeHeader(); trackFormatWriter.writeHeader(track);
// Expect reading/writing of the waypoints (except the first) // Expect reading/writing of the waypoints (except the first)
trackFormatWriter.writeBeginWaypoints(); trackFormatWriter.writeBeginWaypoints();
@@ -172,7 +172,7 @@ public class TrackWriterTest extends AndroidTestCase {
trackFormatWriter.writeEndWaypoints(); trackFormatWriter.writeEndWaypoints();
// Begin the track // Begin the track
trackFormatWriter.writeBeginTrack(locationEq(locations[0])); trackFormatWriter.writeBeginTrack(trackEq(track), locationEq(locations[0]));
// Write locations 1-2 // Write locations 1-2
trackFormatWriter.writeOpenSegment(); trackFormatWriter.writeOpenSegment();
@@ -190,7 +190,7 @@ public class TrackWriterTest extends AndroidTestCase {
trackFormatWriter.writeCloseSegment(); trackFormatWriter.writeCloseSegment();
// End the track // End the track
trackFormatWriter.writeEndTrack(locationEq(locations[5])); trackFormatWriter.writeEndTrack(trackEq(track), locationEq(locations[5]));
trackFormatWriter.writeFooter(); trackFormatWriter.writeFooter();
trackFormatWriter.close(); trackFormatWriter.close();
@@ -266,6 +266,33 @@ public class TrackWriterTest extends AndroidTestCase {
return null; 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. * Fills the locations.
* *