/* * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.google.android.apps.mytracks.io; import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.io.TrackWriterFactory.TrackFileFormat; import com.google.android.apps.mytracks.util.StringUtils; import android.content.Context; import android.location.Location; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Vector; /** * Write track as KML to a file. * * @author Leif Hendrik Wilden */ public class KmlTrackWriter implements TrackFormatWriter { private final Vector distances = new Vector(); private final Vector elevations = new Vector(); private final StringUtils stringUtils; private PrintWriter pw = null; private Track track; public KmlTrackWriter(Context context) { stringUtils = new StringUtils(context); } /** * Testing constructor. */ KmlTrackWriter(StringUtils stringUtils) { this.stringUtils = stringUtils; } @Override public void prepare(Track track, OutputStream out) { this.track = track; this.pw = new PrintWriter(out); } @Override public String getExtension() { return TrackFileFormat.KML.getExtension(); } @Override public void writeHeader() { if (pw != null) { pw.println(""); pw.print(""); pw.println(""); pw.println("My Tracks running on Android" + ""); pw.println("" + StringUtils.stringAsCData(track.getName()) + ""); pw.println("" + StringUtils.stringAsCData(track.getDescription()) + ""); writeStyles(); } } @Override public void writeFooter() { if (pw != null) { pw.println(""); pw.println(""); } } @Override public void writeBeginTrack(Location firstPoint) { if (pw != null) { writePlacemark("(Start)", track.getDescription(), "#sh_green-circle", firstPoint); pw.println(""); pw.println("" + StringUtils.stringAsCData(track.getName()) + ""); pw.println("" + StringUtils.stringAsCData(track.getDescription()) + ""); pw.println("#track"); pw.println(""); } } @Override public void writeEndTrack(Location lastPoint) { if (pw != null) { pw.println(""); pw.println(""); String description = stringUtils.generateTrackDescription( track, distances, elevations); writePlacemark("(End)", description, "#sh_red-circle", lastPoint); } } @Override public void writeOpenSegment() { if (pw != null) { pw.print(""); } } @Override public void writeCloseSegment() { if (pw != null) { pw.println(""); } } @Override public void writeLocation(Location l) { if (pw != null) { pw.print(l.getLongitude() + "," + l.getLatitude() + "," + l.getAltitude() + " "); } } private String getPinStyle(Waypoint waypoint) { if (waypoint.getType() == Waypoint.TYPE_STATISTICS) { return "#sh_ylw-pushpin"; } // Try to find the icon color. // The string should be of the form: // "http://maps.google.com/mapfiles/ms/micons/XXX.png" int slash = waypoint.getIcon().lastIndexOf('/'); int png = waypoint.getIcon().lastIndexOf('.'); if ((slash != -1) && (slash < png)) { String color = waypoint.getIcon().substring(slash + 1, png); return "#sh_" + color + "-pushpin"; } return "#sh_blue-pushpin"; } @Override public void writeWaypoint(Waypoint waypoint) { if (pw != null) { writePlacemark( waypoint.getName(), waypoint.getDescription(), getPinStyle(waypoint), waypoint.getLocation()); } } @Override public void close() { if (pw != null) { pw.close(); pw = null; } } private void writeStyles() { pw.println(""); pw.print(""); pw.print(""); pw.print(""); pw.print(""); pw.print(""); pw.print(""); } private void writePlacemark(String name, String description, String style, Location location) { if (location != null) { pw.println(""); pw.println(" " + StringUtils.stringAsCData(name) + ""); pw.println(" " + StringUtils.stringAsCData(description) + ""); pw.println(" " + style + ""); pw.println(" "); pw.println(" " + location.getLongitude() + "," + location.getLatitude() + ""); pw.println(" "); pw.println(""); } } }