mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
Change TrackFormatWriter to be able to write multiple tracks to a file
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||
printWriter.println("<gpx");
|
||||
@@ -109,7 +107,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBeginTrack(Location firstLocation) {
|
||||
public void writeBeginTrack(Track track, Location firstLocation) {
|
||||
if (printWriter != null) {
|
||||
printWriter.println("<trk>");
|
||||
printWriter.println("<name>" + StringUtils.formatCData(track.getName()) + "</name>");
|
||||
@@ -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("</trk>");
|
||||
}
|
||||
|
||||
@@ -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<Integer> powerList = new ArrayList<Integer>();
|
||||
private ArrayList<Integer> cadenceList = new ArrayList<Integer>();
|
||||
@@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||
printWriter.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\"");
|
||||
@@ -170,7 +168,7 @@ public class KmlTrackWriter implements TrackFormatWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBeginTrack(Location firstLocation) {
|
||||
public void writeBeginTrack(Track track, Location firstLocation) {
|
||||
if (printWriter != null) {
|
||||
String name = context.getString(R.string.marker_label_start, track.getName());
|
||||
writePlacemark(name, "", "", START_STYLE, firstLocation);
|
||||
@@ -187,7 +185,7 @@ public class KmlTrackWriter implements TrackFormatWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeEndTrack(Location lastLocation) {
|
||||
public void writeEndTrack(Track track, Location lastLocation) {
|
||||
if (printWriter != null) {
|
||||
printWriter.println("</gx:MultiTrack>");
|
||||
printWriter.println("</Placemark>");
|
||||
|
||||
@@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||
printWriter.println("<TrainingCenterDatabase"
|
||||
@@ -144,7 +141,8 @@ public class TcxTrackWriter implements TrackFormatWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBeginTrack(Location firstPoint) {
|
||||
public void writeBeginTrack(Track track, Location firstPoint) {
|
||||
sportType = getSportType(track.getCategory());
|
||||
if (printWriter != null) {
|
||||
String startTime = StringUtils.formatDateTimeIso8601(
|
||||
track.getTripStatistics().getStartTime());
|
||||
@@ -165,7 +163,7 @@ public class TcxTrackWriter implements TrackFormatWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeEndTrack(Location lastPoint) {
|
||||
public void writeEndTrack(Track track, Location lastPoint) {
|
||||
if (printWriter != null) {
|
||||
printWriter.println("</Lap>");
|
||||
printWriter.println("<Notes>" + StringUtils.formatCData(track.getDescription()) + "</Notes>");
|
||||
|
||||
@@ -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:
|
||||
* <ol>
|
||||
* <li>{@link #prepare}
|
||||
* <li>{@link #writeHeader}
|
||||
* <li>{@link #writeBeginWaypoints}
|
||||
* <li>For each waypoint: {@link #writeWaypoint}
|
||||
* <li>{@link #writeEndWaypoints}
|
||||
* <li>{@link #writeBeginTrack}
|
||||
* <li>For each segment:
|
||||
* <ol>
|
||||
* <li>{@link #writeOpenSegment}
|
||||
* <li>For each location in the segment: {@link #writeLocation}
|
||||
* <li>{@link #writeCloseSegment}
|
||||
* </ol>
|
||||
* <li>{@link #writeEndTrack}
|
||||
* <li>{@link #writeFooter}
|
||||
* <li>{@link #close}
|
||||
* </ol>
|
||||
* <ul>
|
||||
* <li>{@link #prepare}</li>
|
||||
* <li>{@link #writeHeader}</li>
|
||||
* <li>For each track:</li>
|
||||
* <ul>
|
||||
* <li>{@link #writeBeginWaypoints}
|
||||
* <li>For each waypoint: {@link #writeWaypoint}
|
||||
* <li>{@link #writeEndWaypoints}
|
||||
* <li>{@link #writeBeginTrack}
|
||||
* <li>For each segment:
|
||||
* <ul>
|
||||
* <li>{@link #writeOpenSegment}</li>
|
||||
* <li>For each location in the segment: {@link #writeLocation}</li>
|
||||
* <li>{@link #writeCloseSegment}</li>
|
||||
* </ul>
|
||||
* </li>
|
||||
* <li>{@link #writeEndTrack}</li>
|
||||
* </ul>
|
||||
* <li>{@link #writeFooter}</li>
|
||||
* <li>{@link #close}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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.
|
||||
|
||||
@@ -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();
|
||||
|
||||
+4
-4
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user