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 android.content.Context;
20import android.database.Cursor;
21import android.location.Location;
22
23/**
24 * Helper class for writing and retrieving data using the TrackerProvider
25 * content provider
26 *
27 */
28public class TrackerDataHelper {
29
30    private Context mContext;
31    /** formats data output */
32    protected IFormatter mFormatter;
33
34    /** formats output as Comma separated value CSV file */
35    public static final IFormatter CSV_FORMATTER = new CSVFormatter();
36    /** formats output as KML file */
37    public static final IFormatter KML_FORMATTER = new KMLFormatter();
38    /** provides no formatting */
39    public static final IFormatter NO_FORMATTER = new IFormatter() {
40        public String getFooter() {
41            return "";
42        }
43
44        public String getHeader() {
45            return "";
46        }
47
48        public String getOutput(TrackerEntry entry) {
49            return "";
50        }
51    };
52
53    /**
54     * Creates instance
55     *
56     * @param context - content context
57     * @param formatter - formats the output from the get*Output* methods
58     */
59    public TrackerDataHelper(Context context, IFormatter formatter) {
60        mContext = context;
61        mFormatter = formatter;
62    }
63
64    /**
65     * Creates a instance with no output formatting capabilities. Useful for
66     * clients that require write-only access
67     */
68    public TrackerDataHelper(Context context) {
69        this(context, NO_FORMATTER);
70    }
71
72    /**
73     * insert given TrackerEntry into content provider
74     */
75    void writeEntry(TrackerEntry entry) {
76        mContext.getContentResolver().insert(TrackerProvider.CONTENT_URI,
77                entry.getAsContentValues());
78    }
79
80    /**
81     * insert given location into tracker data
82     */
83    public void writeEntry(Location loc, float distFromNetLoc) {
84        writeEntry(TrackerEntry.createEntry(loc, distFromNetLoc));
85    }
86
87    /**
88     * insert given log message into tracker data
89     */
90    public void writeEntry(String tag, String logMsg) {
91        writeEntry(TrackerEntry.createEntry(tag, logMsg));
92    }
93
94    /**
95     * Deletes all tracker entries
96     */
97    public void deleteAll() {
98        mContext.getContentResolver().delete(TrackerProvider.CONTENT_URI, null,
99                null);
100    }
101
102    /**
103     * Query tracker data, filtering by given tag
104     *
105     * @param tag
106     * @return Cursor to data
107     */
108    public Cursor query(String tag, int limit) {
109        String selection = (tag == null ? null : TrackerEntry.TAG + "=?");
110        String[] selectionArgs = (tag == null ? null : new String[] {tag});
111        Cursor cursor = mContext.getContentResolver().query(
112                TrackerProvider.CONTENT_URI, TrackerEntry.ATTRIBUTES,
113                selection, selectionArgs, null);
114        if (cursor == null) {
115            return cursor;
116        }
117        int pos = (cursor.getCount() < limit ? 0 : cursor.getCount() - limit);
118        cursor.moveToPosition(pos);
119        return cursor;
120    }
121
122    /**
123     * Retrieves a cursor that starts at the last limit rows
124     *
125     * @param limit
126     * @return a cursor, null if bad things happened
127     */
128    public Cursor query(int limit) {
129        return query(null, limit);
130    }
131
132    /**
133     * Query tracker data, filtering by given tag. mo limit to number of rows
134     * returned
135     *
136     * @param tag
137     * @return Cursor to data
138     */
139    public Cursor query(String tag) {
140        return query(tag, Integer.MAX_VALUE);
141    }
142
143    /**
144     * Returns the output header particular to the associated formatter
145     */
146    public String getOutputHeader() {
147        return mFormatter.getHeader();
148    }
149
150    /**
151     * Returns the output footer particular to the associated formatter
152     */
153    public String getOutputFooter() {
154        return mFormatter.getFooter();
155    }
156
157    /**
158     * Helper method which converts row referenced by given cursor to a string
159     * output
160     *
161     * @param cursor
162     * @return CharSequence output, null if given cursor is invalid or no more
163     *         data
164     */
165    public String getNextOutput(Cursor cursor) {
166        if (cursor == null || cursor.isAfterLast()) {
167            return null;
168        }
169        String output = mFormatter.getOutput(TrackerEntry.createEntry(cursor));
170        cursor.moveToNext();
171        return output;
172    }
173}
174