forked from upstream-mirrors/OpenTracks
Refactored TrackWriter instantiation.
This commit is contained in:
@@ -22,8 +22,7 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
import de.dennisguse.opentracks.android.IContentResolver;
|
import de.dennisguse.opentracks.android.IContentResolver;
|
||||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.FileTrackExporter;
|
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.TrackWriter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A content provider that mimics the behavior of {@link androidx.core.content.FileProvider}, which shares virtual (non-existing) KML-files.
|
* A content provider that mimics the behavior of {@link androidx.core.content.FileProvider}, which shares virtual (non-existing) KML-files.
|
||||||
@@ -176,14 +175,13 @@ public class ShareContentProvider extends CustomContentProvider implements ICont
|
|||||||
tracks[i] = contentProviderUtils.getTrack(trackIds[i]);
|
tracks[i] = contentProviderUtils.getTrack(trackIds[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackWriter kmlTrackWriter = TrackFileFormat.KML.newTrackWriter(getContext(), false);
|
final TrackExporter trackExporter = getTrackFileFormat(uri).newTrackExporter(getContext(), tracks, null);
|
||||||
final FileTrackExporter fileTrackExporter = new FileTrackExporter(contentProviderUtils, tracks, kmlTrackWriter, null);
|
|
||||||
|
|
||||||
PipeDataWriter pipeDataWriter = new PipeDataWriter<String>() {
|
PipeDataWriter pipeDataWriter = new PipeDataWriter<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void writeDataToPipe(@NonNull ParcelFileDescriptor output, @NonNull Uri uri, @NonNull String mimeType, @Nullable Bundle opts, @Nullable String args) {
|
public void writeDataToPipe(@NonNull ParcelFileDescriptor output, @NonNull Uri uri, @NonNull String mimeType, @Nullable Bundle opts, @Nullable String args) {
|
||||||
try (FileOutputStream fileOutputStream = new FileOutputStream(output.getFileDescriptor())) {
|
try (FileOutputStream fileOutputStream = new FileOutputStream(output.getFileDescriptor())) {
|
||||||
fileTrackExporter.writeTrack(fileOutputStream);
|
trackExporter.writeTrack(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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,14 @@ import android.os.Parcelable;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
|
import de.dennisguse.opentracks.content.ContentProviderUtils;
|
||||||
|
import de.dennisguse.opentracks.content.Track;
|
||||||
|
import de.dennisguse.opentracks.io.file.exporter.FileTrackExporter;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.GpxTrackWriter;
|
import de.dennisguse.opentracks.io.file.exporter.GpxTrackWriter;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.KmlTrackWriter;
|
import de.dennisguse.opentracks.io.file.exporter.KmlTrackWriter;
|
||||||
|
import de.dennisguse.opentracks.io.file.exporter.KmzTrackExporter;
|
||||||
|
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
||||||
|
import de.dennisguse.opentracks.io.file.exporter.TrackExporterListener;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.TrackWriter;
|
import de.dennisguse.opentracks.io.file.exporter.TrackWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,6 +42,15 @@ public enum TrackFileFormat implements Parcelable {
|
|||||||
public String getMimeType() {
|
public String getMimeType() {
|
||||||
return "application/vnd.google-earth.kmz";
|
return "application/vnd.google-earth.kmz";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TrackExporter newTrackExporter(Context context, Track[] tracks, TrackExporterListener trackExporterListener) {
|
||||||
|
ContentProviderUtils contentProviderUtils = ContentProviderUtils.Factory.get(context);
|
||||||
|
TrackWriter trackWriter = this.newTrackWriter(context, tracks.length > 1);
|
||||||
|
|
||||||
|
FileTrackExporter fileTrackExporter = new FileTrackExporter(contentProviderUtils, trackWriter, tracks, trackExporterListener);
|
||||||
|
|
||||||
|
return new KmzTrackExporter(contentProviderUtils, fileTrackExporter, tracks);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
GPX {
|
GPX {
|
||||||
@Override
|
@Override
|
||||||
@@ -79,6 +94,12 @@ public enum TrackFileFormat implements Parcelable {
|
|||||||
*/
|
*/
|
||||||
public abstract TrackWriter newTrackWriter(Context context, boolean multiple);
|
public abstract TrackWriter newTrackWriter(Context context, boolean multiple);
|
||||||
|
|
||||||
|
public TrackExporter newTrackExporter(Context context, Track[] tracks, TrackExporterListener trackExporterListener) {
|
||||||
|
ContentProviderUtils contentProviderUtils = ContentProviderUtils.Factory.get(context);
|
||||||
|
TrackWriter trackWriter = this.newTrackWriter(context, tracks.length > 1);
|
||||||
|
return new FileTrackExporter(contentProviderUtils, trackWriter, tracks, trackExporterListener);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the mime type for each format.
|
* Returns the mime type for each format.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ import android.database.Cursor;
|
|||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.Constants;
|
import de.dennisguse.opentracks.Constants;
|
||||||
import de.dennisguse.opentracks.content.ContentProviderUtils;
|
import de.dennisguse.opentracks.content.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.content.ContentProviderUtils.LocationIterator;
|
import de.dennisguse.opentracks.content.ContentProviderUtils.LocationIterator;
|
||||||
@@ -28,8 +30,6 @@ import de.dennisguse.opentracks.content.Track;
|
|||||||
import de.dennisguse.opentracks.content.Waypoint;
|
import de.dennisguse.opentracks.content.Waypoint;
|
||||||
import de.dennisguse.opentracks.util.LocationUtils;
|
import de.dennisguse.opentracks.util.LocationUtils;
|
||||||
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Track Writer for writing tracks to an {@link OutputStream}.
|
* Track Writer for writing tracks to an {@link OutputStream}.
|
||||||
*
|
*
|
||||||
@@ -49,11 +49,11 @@ public class FileTrackExporter implements TrackExporter {
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param contentProviderUtils the content provider utils
|
* @param contentProviderUtils the content provider utils
|
||||||
* @param tracks the tracks
|
|
||||||
* @param trackWriter the track writer
|
* @param trackWriter the track writer
|
||||||
|
* @param tracks the tracks
|
||||||
* @param trackExporterListener the track export listener
|
* @param trackExporterListener the track export listener
|
||||||
*/
|
*/
|
||||||
public FileTrackExporter(ContentProviderUtils contentProviderUtils, Track[] tracks, TrackWriter trackWriter, TrackExporterListener trackExporterListener) {
|
public FileTrackExporter(ContentProviderUtils contentProviderUtils, TrackWriter trackWriter, Track[] tracks, TrackExporterListener trackExporterListener) {
|
||||||
this.contentProviderUtils = contentProviderUtils;
|
this.contentProviderUtils = contentProviderUtils;
|
||||||
this.tracks = tracks;
|
this.tracks = tracks;
|
||||||
this.trackWriter = trackWriter;
|
this.trackWriter = trackWriter;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
/**
|
/**
|
||||||
* Creates an AsyncTask.
|
* Creates an AsyncTask.
|
||||||
*
|
*
|
||||||
* @param exportActivity the activity currently associated with this task
|
* @param exportActivity the activity currently associated with this task
|
||||||
* @param trackFileFormat the track file format
|
* @param trackFileFormat the track file format
|
||||||
* @param directory the directory to write the file
|
* @param directory the directory to write the file
|
||||||
*/
|
*/
|
||||||
@@ -154,23 +154,20 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileTrackExporter fileTrackExporter = new FileTrackExporter(contentProviderUtils, tracks,
|
TrackExporterListener trackExporterListener = new TrackExporterListener() {
|
||||||
trackFileFormat.newTrackWriter(context, tracks.length > 1),
|
@Override
|
||||||
new TrackExporterListener() {
|
public void onProgressUpdate(int number, int max) {
|
||||||
@Override
|
//Update the progress dialog once every 500 points.
|
||||||
public void onProgressUpdate(int number, int max) {
|
if (number % 500 == 0) {
|
||||||
//Update the progress dialog once every 500 points.
|
publishProgress(number, max);
|
||||||
if (number % 500 == 0) {
|
}
|
||||||
publishProgress(number, max);
|
}
|
||||||
}
|
};
|
||||||
}
|
|
||||||
});
|
TrackExporter trackExporter = trackFileFormat.newTrackExporter(context, tracks, trackExporterListener);
|
||||||
boolean useKmz = trackFileFormat == TrackFileFormat.KMZ;
|
|
||||||
String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : trackFileFormat.getExtension();
|
|
||||||
TrackExporter trackExporter = useKmz ? new KmzTrackExporter(contentProviderUtils, fileTrackExporter, tracks) : fileTrackExporter;
|
|
||||||
|
|
||||||
Track track = tracks[0];
|
Track track = tracks[0];
|
||||||
String fileName = FileUtils.buildUniqueFileName(directory, track.getName(), extension);
|
String fileName = FileUtils.buildUniqueFileName(directory, track.getName(), trackFileFormat.getExtension());
|
||||||
File file = new File(directory, fileName);
|
File file = new File(directory, fileName);
|
||||||
|
|
||||||
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
|
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user