Search in sources :

Example 1 with GpxFileWriteEvent

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());
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Gpx(com.peterlaurence.trekadvisor.util.gpx.model.Gpx) Date(java.util.Date) TrackSegment(com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment) GpxFileWriteEvent(com.peterlaurence.trekadvisor.service.event.GpxFileWriteEvent) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Track(com.peterlaurence.trekadvisor.util.gpx.model.Track)

Aggregations

GpxFileWriteEvent (com.peterlaurence.trekadvisor.service.event.GpxFileWriteEvent)1 Gpx (com.peterlaurence.trekadvisor.util.gpx.model.Gpx)1 Track (com.peterlaurence.trekadvisor.util.gpx.model.Track)1 TrackSegment (com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1