use of com.peterlaurence.trekadvisor.service.event.GpxFileWriteEvent in project TrekAdvisor by peterLaurence.
the class LocationService method createGpx.
/**
* When we stop recording the location events, create a {@link Gpx} object for further
* serialization. <br>
* Whatever the outcome of this process, a {@link GpxFileWriteEvent} is emitted in the
* LocationServiceThread.
*/
private void createGpx() {
mServiceHandler.post(() -> {
TrackSegment.Builder trkSegBuilder = new TrackSegment.Builder();
trkSegBuilder.setTrackPoints(mTrackPoints);
ArrayList<TrackSegment> trkSegList = new ArrayList<>();
trkSegList.add(trkSegBuilder.build());
Track.Builder trackBuilder = new Track.Builder().setName(TrekAdvisorContext.APP_FOLDER_NAME + " track");
trackBuilder.setTrackSegments(trkSegList);
ArrayList<Track> trkList = new ArrayList<>();
trkList.add(trackBuilder.build());
Gpx.Builder gpxBuilder = new Gpx.Builder();
gpxBuilder.setCreator(TrekAdvisorContext.APP_FOLDER_NAME);
gpxBuilder.setVersion(GPX_VERSION);
gpxBuilder.setTracks(trkList);
Gpx gpx = gpxBuilder.build();
try {
if (!TrekAdvisorContext.DEFAULT_RECORDINGS_DIR.exists()) {
TrekAdvisorContext.DEFAULT_RECORDINGS_DIR.mkdir();
}
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd\\MM\\yyyy-HH:mm:ss", Locale.ENGLISH);
String gpxFileName = "track-" + dateFormat.format(date) + ".gpx";
File gpxFile = new File(TrekAdvisorContext.DEFAULT_RECORDINGS_DIR, gpxFileName);
FileOutputStream fos = new FileOutputStream(gpxFile);
GPXWriter.write(gpx, fos);
} catch (Exception e) {
// for instance, don't care : we want to stop the service anyway
// TODO : warn the user that the gpx file could not be saved
} finally {
EventBus.getDefault().post(new GpxFileWriteEvent());
}
});
}
Aggregations