1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.locationtracker.data;
18
19import com.android.locationtracker.data.TrackerEntry.EntryType;
20
21/**
22 * Formats tracker data as CSV output
23 */
24class CSVFormatter implements IFormatter {
25
26    private static final String DELIMITER = ", ";
27
28    public String getHeader() {
29        StringBuilder csvBuilder = new StringBuilder();
30        for (String col : TrackerEntry.ATTRIBUTES) {
31            // skip type and id column
32            if (!TrackerEntry.ENTRY_TYPE.equals(col) &&
33                !TrackerEntry.ID_COL.equals(col)) {
34                csvBuilder.append(col);
35                csvBuilder.append(DELIMITER);
36            }
37        }
38        csvBuilder.append("\n");
39        return csvBuilder.toString();
40    }
41
42    public String getOutput(TrackerEntry entry) {
43        StringBuilder rowOutput = new StringBuilder();
44        // these must match order of columns added in getHeader
45        rowOutput.append(entry.getTimestamp());
46        rowOutput.append(DELIMITER);
47        rowOutput.append(entry.getTag());
48        rowOutput.append(DELIMITER);
49        //rowOutput.append(entry.getType());
50        //rowOutput.append(DELIMITER);
51        if (entry.getType() == EntryType.LOCATION_TYPE) {
52            if (entry.getLocation().hasAccuracy()) {
53                rowOutput.append(entry.getLocation().getAccuracy());
54            }
55            rowOutput.append(DELIMITER);
56            rowOutput.append(entry.getLocation().getLatitude());
57            rowOutput.append(DELIMITER);
58            rowOutput.append(entry.getLocation().getLongitude());
59            rowOutput.append(DELIMITER);
60            if (entry.getLocation().hasAltitude()) {
61                rowOutput.append(entry.getLocation().getAltitude());
62            }
63            rowOutput.append(DELIMITER);
64            if (entry.getLocation().hasSpeed()) {
65                rowOutput.append(entry.getLocation().getSpeed());
66            }
67            rowOutput.append(DELIMITER);
68            if (entry.getLocation().hasBearing()) {
69                rowOutput.append(entry.getLocation().getBearing());
70            }
71            rowOutput.append(DELIMITER);
72            rowOutput.append(entry.getDistFromNetLocation());
73            rowOutput.append(DELIMITER);
74            rowOutput.append(DateUtils.getKMLTimestamp(entry.getLocation()
75                    .getTime()));
76            rowOutput.append(DELIMITER);
77        }
78        rowOutput.append(entry.getLogMsg());
79        rowOutput.append("\n");
80        return rowOutput.toString();
81    }
82
83    public String getFooter() {
84        // not needed, return empty string
85        return "";
86    }
87}
88