move tracks array from class state to method argument

This commit is contained in:
thrillfall
2020-08-11 13:53:03 +02:00
committed by Dennis Guse
parent 80208e3188
commit 47c259fa9a
7 changed files with 25 additions and 33 deletions
@@ -111,7 +111,7 @@ public class ExportImportTest {
// when // when
// 1. export // 1. export
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
trackExporter.writeTrack(outputStream); trackExporter.writeTrack(new Track[]{track}, outputStream);
contentProviderUtils.deleteTrack(context, trackId); contentProviderUtils.deleteTrack(context, trackId);
// 2. import // 2. import
@@ -147,7 +147,7 @@ public class ExportImportTest {
// when // when
// 1. export // 1. export
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
trackExporter.writeTrack(outputStream); trackExporter.writeTrack(new Track[]{track}, outputStream);
contentProviderUtils.deleteTrack(context, trackId); contentProviderUtils.deleteTrack(context, trackId);
// 2. import // 2. import
@@ -183,7 +183,7 @@ public class ExportImportTest {
// when // when
// 1. export // 1. export
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
trackExporter.writeTrack(outputStream); trackExporter.writeTrack(new Track[]{track}, outputStream);
// 2. import // 2. import
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
@@ -234,7 +234,7 @@ public class ExportImportTest {
// when // when
// 1. export // 1. export
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
trackExporter.writeTrack(outputStream); trackExporter.writeTrack(new Track[]{track}, outputStream);
contentProviderUtils.deleteTrack(context, trackId); contentProviderUtils.deleteTrack(context, trackId);
// 2. import // 2. import
@@ -272,7 +272,7 @@ public class ExportImportTest {
// when // when
// 1. export // 1. export
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
trackExporter.writeTrack(outputStream); trackExporter.writeTrack(new Track[]{track}, outputStream);
// 2. import // 2. import
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
@@ -212,7 +212,7 @@ public class ShareContentProvider extends CustomContentProvider implements ICont
PipeDataWriter<String> pipeDataWriter = (output, uri1, mimeType, opts, args) -> { PipeDataWriter<String> pipeDataWriter = (output, uri1, mimeType, opts, args) -> {
try (FileOutputStream fileOutputStream = new FileOutputStream(output.getFileDescriptor())) { try (FileOutputStream fileOutputStream = new FileOutputStream(output.getFileDescriptor())) {
trackExporter.writeTrack(fileOutputStream); trackExporter.writeTrack(tracks, fileOutputStream);
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "there occurred an error while sharing a file: " + e); Log.w(TAG, "there occurred an error while sharing a file: " + e);
} }
@@ -218,7 +218,7 @@ public class ExportProgressDialogFragment extends DialogFragment {
} }
try (OutputStream outputStream = context.getContentResolver().openOutputStream(file.getUri())) { try (OutputStream outputStream = context.getContentResolver().openOutputStream(file.getUri())) {
if (trackExporter.writeTrack(outputStream)) { if (trackExporter.writeTrack(new Track[]{track}, outputStream)) {
return true; return true;
} else { } else {
if (!file.delete()) { if (!file.delete()) {
@@ -74,7 +74,7 @@ public enum TrackFileFormat {
} }
public TrackExporter newTrackExporter(Context context, Track[] tracks) { public TrackExporter newTrackExporter(Context context, Track[] tracks) {
return newKmzTrackExporter(context, this.newTrackWriter(context, tracks.length > 1), tracks, exportPhotos); return newKmzTrackExporter(context, this.newTrackWriter(context, tracks.length > 1), exportPhotos);
} }
@Override @Override
@@ -101,7 +101,7 @@ public enum TrackFileFormat {
} }
public TrackExporter newTrackExporter(Context context, Track[] tracks) { public TrackExporter newTrackExporter(Context context, Track[] tracks) {
return newKmzTrackExporter(context, this.newTrackWriter(context, tracks.length > 1), tracks, exportPhotos); return newKmzTrackExporter(context, this.newTrackWriter(context, tracks.length > 1), exportPhotos);
} }
@Override @Override
@@ -134,7 +134,7 @@ public enum TrackFileFormat {
} }
public TrackExporter newTrackExporter(Context context, Track[] tracks) { public TrackExporter newTrackExporter(Context context, Track[] tracks) {
return newKmzTrackExporter(context, this.newTrackWriter(context, tracks.length > 1), tracks, exportPhotos); return newKmzTrackExporter(context, this.newTrackWriter(context, tracks.length > 1), exportPhotos);
} }
public String getExtension() { public String getExtension() {
@@ -163,7 +163,7 @@ public enum TrackFileFormat {
} }
public TrackExporter newTrackExporter(Context context, Track[] tracks) { public TrackExporter newTrackExporter(Context context, Track[] tracks) {
return newKmzTrackExporter(context, newTrackWriter(context, tracks.length > 1), tracks, exportPhotos); return newKmzTrackExporter(context, newTrackWriter(context, tracks.length > 1), exportPhotos);
} }
public String getExtension() { public String getExtension() {
@@ -196,10 +196,10 @@ public enum TrackFileFormat {
private static final String MIME_KML = "application/vnd.google-earth.kml+xml"; private static final String MIME_KML = "application/vnd.google-earth.kml+xml";
private static TrackExporter newKmzTrackExporter(Context context, TrackWriter trackWriter, Track[] tracks, boolean exportPhotos) { private static TrackExporter newKmzTrackExporter(Context context, TrackWriter trackWriter, boolean exportPhotos) {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
FileTrackExporter fileTrackExporter = new FileTrackExporter(contentProviderUtils, trackWriter, tracks); FileTrackExporter fileTrackExporter = new FileTrackExporter(contentProviderUtils, trackWriter);
return new KmzTrackExporter(context, contentProviderUtils, fileTrackExporter, exportPhotos); return new KmzTrackExporter(context, contentProviderUtils, fileTrackExporter, exportPhotos);
} }
@@ -212,7 +212,7 @@ public enum TrackFileFormat {
public TrackExporter newTrackExporter(Context context, Track[] tracks) { public TrackExporter newTrackExporter(Context context, Track[] tracks) {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
TrackWriter trackWriter = newTrackWriter(context, tracks.length > 1); TrackWriter trackWriter = newTrackWriter(context, tracks.length > 1);
return new FileTrackExporter(contentProviderUtils, trackWriter, tracks); return new FileTrackExporter(contentProviderUtils, trackWriter);
} }
/** /**
@@ -16,7 +16,6 @@
package de.dennisguse.opentracks.io.file.exporter; package de.dennisguse.opentracks.io.file.exporter;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.util.Log; import android.util.Log;
@@ -42,7 +41,6 @@ public class FileTrackExporter implements TrackExporter {
private static final String TAG = FileTrackExporter.class.getSimpleName(); private static final String TAG = FileTrackExporter.class.getSimpleName();
private final ContentProviderUtils contentProviderUtils; private final ContentProviderUtils contentProviderUtils;
private final Track[] tracks;
private final TrackWriter trackWriter; private final TrackWriter trackWriter;
/** /**
@@ -50,16 +48,14 @@ public class FileTrackExporter implements TrackExporter {
* *
* @param contentProviderUtils the content provider utils * @param contentProviderUtils the content provider utils
* @param trackWriter the track writer * @param trackWriter the track writer
* @param tracks the tracks
*/ */
public FileTrackExporter(ContentProviderUtils contentProviderUtils, TrackWriter trackWriter, Track[] tracks) { public FileTrackExporter(ContentProviderUtils contentProviderUtils, TrackWriter trackWriter) {
this.contentProviderUtils = contentProviderUtils; this.contentProviderUtils = contentProviderUtils;
this.tracks = tracks;
this.trackWriter = trackWriter; this.trackWriter = trackWriter;
} }
@Override @Override
public boolean writeTrack(@NonNull OutputStream outputStream) { public boolean writeTrack(Track[] tracks, @NonNull OutputStream outputStream) {
try { try {
trackWriter.prepare(outputStream); trackWriter.prepare(outputStream);
trackWriter.writeHeader(tracks); trackWriter.writeHeader(tracks);
@@ -126,7 +122,6 @@ public class FileTrackExporter implements TrackExporter {
boolean wroteTrack = false; boolean wroteTrack = false;
boolean wroteSegment = false; boolean wroteSegment = false;
boolean isLastLocationValid = false; boolean isLastLocationValid = false;
int locationNumber = 0;
TrackPoint lastTrackPoint = null; TrackPoint lastTrackPoint = null;
try (TrackPointIterator trackPointIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false)) { try (TrackPointIterator trackPointIterator = contentProviderUtils.getTrackPointLocationIterator(track.getId(), -1L, false)) {
@@ -138,7 +133,6 @@ public class FileTrackExporter implements TrackExporter {
TrackPoint trackPoint = trackPointIterator.next(); TrackPoint trackPoint = trackPointIterator.next();
setLocationTime(trackPoint, offset); setLocationTime(trackPoint, offset);
locationNumber++;
boolean isLocationValid = LocationUtils.isValidLocation(trackPoint.getLocation()); boolean isLocationValid = LocationUtils.isValidLocation(trackPoint.getLocation());
boolean isSegmentValid = isLocationValid && isLastLocationValid; boolean isSegmentValid = isLocationValid && isLastLocationValid;
@@ -51,7 +51,6 @@ public class KmzTrackExporter implements TrackExporter {
private final ContentProviderUtils contentProviderUtils; private final ContentProviderUtils contentProviderUtils;
private final FileTrackExporter fileTrackExporter; private final FileTrackExporter fileTrackExporter;
private final Track[] tracks;
private final boolean exportPhotos; private final boolean exportPhotos;
private Context context; private Context context;
@@ -62,18 +61,16 @@ public class KmzTrackExporter implements TrackExporter {
* @param context the context * @param context the context
* @param contentProviderUtils the content provider utils * @param contentProviderUtils the content provider utils
* @param trackExporter the file track exporter * @param trackExporter the file track exporter
* @param tracks the tracks to export
*/ */
public KmzTrackExporter(Context context, ContentProviderUtils contentProviderUtils, FileTrackExporter trackExporter, Track[] tracks, boolean exportPhotos) { public KmzTrackExporter(Context context, ContentProviderUtils contentProviderUtils, FileTrackExporter trackExporter, boolean exportPhotos) {
this.context = context; this.context = context;
this.contentProviderUtils = contentProviderUtils; this.contentProviderUtils = contentProviderUtils;
this.fileTrackExporter = trackExporter; this.fileTrackExporter = trackExporter;
this.tracks = tracks;
this.exportPhotos = exportPhotos; this.exportPhotos = exportPhotos;
} }
@Override @Override
public boolean writeTrack(@NonNull OutputStream outputStream) { public boolean writeTrack(Track[] tracks, @NonNull OutputStream outputStream) {
ZipOutputStream zipOutputStream = null; ZipOutputStream zipOutputStream = null;
try { try {
zipOutputStream = new ZipOutputStream(outputStream); zipOutputStream = new ZipOutputStream(outputStream);
@@ -82,7 +79,7 @@ public class KmzTrackExporter implements TrackExporter {
ZipEntry zipEntry = new ZipEntry(KMZ_KML_FILE); ZipEntry zipEntry = new ZipEntry(KMZ_KML_FILE);
zipOutputStream.putNextEntry(zipEntry); zipOutputStream.putNextEntry(zipEntry);
boolean success = fileTrackExporter.writeTrack(zipOutputStream); boolean success = fileTrackExporter.writeTrack(tracks, zipOutputStream);
zipOutputStream.closeEntry(); zipOutputStream.closeEntry();
if (!success) { if (!success) {
Log.e(TAG, "Unable to write kml in kmz"); Log.e(TAG, "Unable to write kml in kmz");
@@ -90,7 +87,7 @@ public class KmzTrackExporter implements TrackExporter {
} }
// Add photos // Add photos
if (exportPhotos) addImages(context, zipOutputStream); if (exportPhotos) addImages(context, tracks ,zipOutputStream);
return true; return true;
} catch (InterruptedException | IOException e) { } catch (InterruptedException | IOException e) {
Log.e(TAG, "Unable to write track", e); Log.e(TAG, "Unable to write track", e);
@@ -106,7 +103,7 @@ public class KmzTrackExporter implements TrackExporter {
} }
} }
private void addImages(Context context, ZipOutputStream zipOutputStream) throws InterruptedException, IOException { private void addImages(Context context, Track[] tracks, ZipOutputStream zipOutputStream) throws InterruptedException, IOException {
for (Track track : tracks) { for (Track track : tracks) {
try (Cursor cursor = contentProviderUtils.getWaypointCursor(track.getId(), -1L, -1)) { try (Cursor cursor = contentProviderUtils.getWaypointCursor(track.getId(), -1L, -1)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
@@ -16,12 +16,12 @@
package de.dennisguse.opentracks.io.file.exporter; package de.dennisguse.opentracks.io.file.exporter;
import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.io.OutputStream; import java.io.OutputStream;
import de.dennisguse.opentracks.content.data.Track;
/** /**
* Track exporting for exporting track to an {@link OutputStream}. * Track exporting for exporting track to an {@link OutputStream}.
* *
@@ -33,7 +33,8 @@ public interface TrackExporter {
* Write track to an output stream. * Write track to an output stream.
* Depending on the implementation a context might be required. * Depending on the implementation a context might be required.
* *
* @param tracks
* @param outputStream the output stream * @param outputStream the output stream
*/ */
boolean writeTrack(@NonNull OutputStream outputStream); boolean writeTrack(Track[] tracks, @NonNull OutputStream outputStream);
} }