1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.locationtracker.data;
18
19import com.android.locationtracker.data.TrackerEntry.EntryType;
20
21import android.location.Location;
22
23/**
24 * Formats tracker data as KML output
25 */
26class KMLFormatter implements IFormatter {
27
28    public String getHeader() {
29        LineBuilder builder = new LineBuilder();
30        builder.addLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
31        builder.addLine("<kml xmlns=\"http://earth.google.com/kml/2.2\">");
32        builder.addLine("<Document>");
33        return builder.toString();
34    }
35
36    public String getFooter() {
37        LineBuilder builder = new LineBuilder();
38        builder.addLine("</Document>");
39        builder.addLine("</kml>");
40        return builder.toString();
41    }
42
43    public String getOutput(TrackerEntry entry) {
44        LineBuilder builder = new LineBuilder();
45
46        if (entry.getType() == EntryType.LOCATION_TYPE) {
47
48            Location loc = entry.getLocation();
49            builder.addLine("<Placemark>");
50            builder.addLine("<description>");
51            builder.addLine("accuracy = " + loc.getAccuracy());
52            builder.addLine("distance from last network location  = "
53                    + entry.getDistFromNetLocation());
54            builder.addLine("</description>");
55            builder.addLine("<TimeStamp>");
56            builder.addLine("<when>" + entry.getTimestamp() + "</when>");
57            builder.addLine("</TimeStamp>");
58            builder.addLine("<Point>");
59            builder.addLine("<coordinates>");
60            builder.addLine(loc.getLongitude() + "," + loc.getLatitude() + ","
61                    + loc.getAltitude());
62            builder.addLine("</coordinates>");
63            builder.addLine("</Point>");
64            builder.addLine("</Placemark>");
65        }
66        return builder.toString();
67    }
68
69    private static class LineBuilder {
70        private StringBuilder mBuilder;
71
72        public LineBuilder() {
73            mBuilder = new StringBuilder();
74        }
75
76        public void addLine(String line) {
77            mBuilder.append(line);
78            mBuilder.append("\n");
79        }
80
81        @Override
82        public String toString() {
83            return mBuilder.toString();
84        }
85
86    }
87
88}
89