summaryrefslogtreecommitdiffstats
path: root/tests/LocationTracker/src/com/android/locationtracker/data
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2010-02-23 07:50:18 -0500
committerMike Lockwood <lockwood@android.com>2010-02-23 07:50:18 -0500
commit191d898468d2f4910a684f429bec518320843744 (patch)
treee9a20eb9d42a48830a52ffe9116925c9b4763bbf /tests/LocationTracker/src/com/android/locationtracker/data
parente5a1d4a21eb35abe75f60cd0416ee469e8c2471b (diff)
downloadframeworks_base-191d898468d2f4910a684f429bec518320843744.zip
frameworks_base-191d898468d2f4910a684f429bec518320843744.tar.gz
frameworks_base-191d898468d2f4910a684f429bec518320843744.tar.bz2
Move LocationTracker from com.google.android to com.android package.
Change-Id: I75757373e39669955d5f753a31a2a7b78d0b68ed Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'tests/LocationTracker/src/com/android/locationtracker/data')
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/CSVFormatter.java87
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/DateUtils.java61
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/IFormatter.java27
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/KMLFormatter.java88
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/TrackerDataHelper.java173
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/TrackerEntry.java253
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/TrackerListHelper.java75
-rw-r--r--tests/LocationTracker/src/com/android/locationtracker/data/TrackerProvider.java126
8 files changed, 890 insertions, 0 deletions
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/CSVFormatter.java b/tests/LocationTracker/src/com/android/locationtracker/data/CSVFormatter.java
new file mode 100644
index 0000000..22ddf45
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/CSVFormatter.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import com.android.locationtracker.data.TrackerEntry.EntryType;
+
+/**
+ * Formats tracker data as CSV output
+ */
+class CSVFormatter implements IFormatter {
+
+ private static final String DELIMITER = ", ";
+
+ public String getHeader() {
+ StringBuilder csvBuilder = new StringBuilder();
+ for (String col : TrackerEntry.ATTRIBUTES) {
+ // skip type and id column
+ if (!TrackerEntry.ENTRY_TYPE.equals(col) &&
+ !TrackerEntry.ID_COL.equals(col)) {
+ csvBuilder.append(col);
+ csvBuilder.append(DELIMITER);
+ }
+ }
+ csvBuilder.append("\n");
+ return csvBuilder.toString();
+ }
+
+ public String getOutput(TrackerEntry entry) {
+ StringBuilder rowOutput = new StringBuilder();
+ // these must match order of columns added in getHeader
+ rowOutput.append(entry.getTimestamp());
+ rowOutput.append(DELIMITER);
+ rowOutput.append(entry.getTag());
+ rowOutput.append(DELIMITER);
+ //rowOutput.append(entry.getType());
+ //rowOutput.append(DELIMITER);
+ if (entry.getType() == EntryType.LOCATION_TYPE) {
+ if (entry.getLocation().hasAccuracy()) {
+ rowOutput.append(entry.getLocation().getAccuracy());
+ }
+ rowOutput.append(DELIMITER);
+ rowOutput.append(entry.getLocation().getLatitude());
+ rowOutput.append(DELIMITER);
+ rowOutput.append(entry.getLocation().getLongitude());
+ rowOutput.append(DELIMITER);
+ if (entry.getLocation().hasAltitude()) {
+ rowOutput.append(entry.getLocation().getAltitude());
+ }
+ rowOutput.append(DELIMITER);
+ if (entry.getLocation().hasSpeed()) {
+ rowOutput.append(entry.getLocation().getSpeed());
+ }
+ rowOutput.append(DELIMITER);
+ if (entry.getLocation().hasBearing()) {
+ rowOutput.append(entry.getLocation().getBearing());
+ }
+ rowOutput.append(DELIMITER);
+ rowOutput.append(entry.getDistFromNetLocation());
+ rowOutput.append(DELIMITER);
+ rowOutput.append(DateUtils.getKMLTimestamp(entry.getLocation()
+ .getTime()));
+ rowOutput.append(DELIMITER);
+ }
+ rowOutput.append(entry.getLogMsg());
+ rowOutput.append("\n");
+ return rowOutput.toString();
+ }
+
+ public String getFooter() {
+ // not needed, return empty string
+ return "";
+ }
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/DateUtils.java b/tests/LocationTracker/src/com/android/locationtracker/data/DateUtils.java
new file mode 100644
index 0000000..1691f27
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/DateUtils.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import java.util.Calendar;
+import java.util.TimeZone;
+
+/**
+ * Provides formatting date as string utilities
+ */
+public class DateUtils {
+
+ private DateUtils() {
+
+ }
+
+ /**
+ * Returns timestamp given by param in KML format ie yyyy-mm-ddThh:mm:ssZ,
+ * where T is the separator between the date and the time and the time zone
+ * is Z (for UTC)
+ *
+ * @return KML timestamp as String
+ */
+ public static String getKMLTimestamp(long when) {
+ TimeZone tz = TimeZone.getTimeZone("GMT");
+ Calendar c = Calendar.getInstance(tz);
+ c.setTimeInMillis(when);
+ return String.format("%tY-%tm-%tdT%tH:%tM:%tSZ", c, c, c, c, c, c);
+ }
+
+ /**
+ * Helper version of getKMLTimestamp, that returns timestamp for current
+ * time
+ */
+ public static String getCurrentKMLTimestamp() {
+ return getKMLTimestamp(System.currentTimeMillis());
+ }
+
+ /**
+ * Returns timestamp in following format: yyyy-mm-dd-hh-mm-ss
+ */
+ public static String getCurrentTimestamp() {
+ Calendar c = Calendar.getInstance();
+ c.setTimeInMillis(System.currentTimeMillis());
+ return String.format("%tY-%tm-%td-%tH-%tM-%tS", c, c, c, c, c, c);
+ }
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/IFormatter.java b/tests/LocationTracker/src/com/android/locationtracker/data/IFormatter.java
new file mode 100644
index 0000000..d413191
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/IFormatter.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+/**
+ * interface for formatting tracker data output
+ */
+interface IFormatter {
+
+ String getHeader();
+ String getOutput(TrackerEntry entry);
+ String getFooter();
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/KMLFormatter.java b/tests/LocationTracker/src/com/android/locationtracker/data/KMLFormatter.java
new file mode 100644
index 0000000..ef4bbbb
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/KMLFormatter.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import com.android.locationtracker.data.TrackerEntry.EntryType;
+
+import android.location.Location;
+
+/**
+ * Formats tracker data as KML output
+ */
+class KMLFormatter implements IFormatter {
+
+ public String getHeader() {
+ LineBuilder builder = new LineBuilder();
+ builder.addLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+ builder.addLine("<kml xmlns=\"http://earth.google.com/kml/2.2\">");
+ builder.addLine("<Document>");
+ return builder.toString();
+ }
+
+ public String getFooter() {
+ LineBuilder builder = new LineBuilder();
+ builder.addLine("</Document>");
+ builder.addLine("</kml>");
+ return builder.toString();
+ }
+
+ public String getOutput(TrackerEntry entry) {
+ LineBuilder builder = new LineBuilder();
+
+ if (entry.getType() == EntryType.LOCATION_TYPE) {
+
+ Location loc = entry.getLocation();
+ builder.addLine("<Placemark>");
+ builder.addLine("<description>");
+ builder.addLine("accuracy = " + loc.getAccuracy());
+ builder.addLine("distance from last network location = "
+ + entry.getDistFromNetLocation());
+ builder.addLine("</description>");
+ builder.addLine("<TimeStamp>");
+ builder.addLine("<when>" + entry.getTimestamp() + "</when>");
+ builder.addLine("</TimeStamp>");
+ builder.addLine("<Point>");
+ builder.addLine("<coordinates>");
+ builder.addLine(loc.getLongitude() + "," + loc.getLatitude() + ","
+ + loc.getAltitude());
+ builder.addLine("</coordinates>");
+ builder.addLine("</Point>");
+ builder.addLine("</Placemark>");
+ }
+ return builder.toString();
+ }
+
+ private static class LineBuilder {
+ private StringBuilder mBuilder;
+
+ public LineBuilder() {
+ mBuilder = new StringBuilder();
+ }
+
+ public void addLine(String line) {
+ mBuilder.append(line);
+ mBuilder.append("\n");
+ }
+
+ @Override
+ public String toString() {
+ return mBuilder.toString();
+ }
+
+ }
+
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/TrackerDataHelper.java b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerDataHelper.java
new file mode 100644
index 0000000..ad25126
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerDataHelper.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.location.Location;
+
+/**
+ * Helper class for writing and retrieving data using the TrackerProvider
+ * content provider
+ *
+ */
+public class TrackerDataHelper {
+
+ private Context mContext;
+ /** formats data output */
+ protected IFormatter mFormatter;
+
+ /** formats output as Comma separated value CSV file */
+ public static final IFormatter CSV_FORMATTER = new CSVFormatter();
+ /** formats output as KML file */
+ public static final IFormatter KML_FORMATTER = new KMLFormatter();
+ /** provides no formatting */
+ public static final IFormatter NO_FORMATTER = new IFormatter() {
+ public String getFooter() {
+ return "";
+ }
+
+ public String getHeader() {
+ return "";
+ }
+
+ public String getOutput(TrackerEntry entry) {
+ return "";
+ }
+ };
+
+ /**
+ * Creates instance
+ *
+ * @param context - content context
+ * @param formatter - formats the output from the get*Output* methods
+ */
+ public TrackerDataHelper(Context context, IFormatter formatter) {
+ mContext = context;
+ mFormatter = formatter;
+ }
+
+ /**
+ * Creates a instance with no output formatting capabilities. Useful for
+ * clients that require write-only access
+ */
+ public TrackerDataHelper(Context context) {
+ this(context, NO_FORMATTER);
+ }
+
+ /**
+ * insert given TrackerEntry into content provider
+ */
+ void writeEntry(TrackerEntry entry) {
+ mContext.getContentResolver().insert(TrackerProvider.CONTENT_URI,
+ entry.getAsContentValues());
+ }
+
+ /**
+ * insert given location into tracker data
+ */
+ public void writeEntry(Location loc, float distFromNetLoc) {
+ writeEntry(TrackerEntry.createEntry(loc, distFromNetLoc));
+ }
+
+ /**
+ * insert given log message into tracker data
+ */
+ public void writeEntry(String tag, String logMsg) {
+ writeEntry(TrackerEntry.createEntry(tag, logMsg));
+ }
+
+ /**
+ * Deletes all tracker entries
+ */
+ public void deleteAll() {
+ mContext.getContentResolver().delete(TrackerProvider.CONTENT_URI, null,
+ null);
+ }
+
+ /**
+ * Query tracker data, filtering by given tag
+ *
+ * @param tag
+ * @return Cursor to data
+ */
+ public Cursor query(String tag, int limit) {
+ String selection = (tag == null ? null : TrackerEntry.TAG + "=?");
+ String[] selectionArgs = (tag == null ? null : new String[] {tag});
+ Cursor cursor = mContext.getContentResolver().query(
+ TrackerProvider.CONTENT_URI, TrackerEntry.ATTRIBUTES,
+ selection, selectionArgs, null);
+ if (cursor == null) {
+ return cursor;
+ }
+ int pos = (cursor.getCount() < limit ? 0 : cursor.getCount() - limit);
+ cursor.moveToPosition(pos);
+ return cursor;
+ }
+
+ /**
+ * Retrieves a cursor that starts at the last limit rows
+ *
+ * @param limit
+ * @return a cursor, null if bad things happened
+ */
+ public Cursor query(int limit) {
+ return query(null, limit);
+ }
+
+ /**
+ * Query tracker data, filtering by given tag. mo limit to number of rows
+ * returned
+ *
+ * @param tag
+ * @return Cursor to data
+ */
+ public Cursor query(String tag) {
+ return query(tag, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Returns the output header particular to the associated formatter
+ */
+ public String getOutputHeader() {
+ return mFormatter.getHeader();
+ }
+
+ /**
+ * Returns the output footer particular to the associated formatter
+ */
+ public String getOutputFooter() {
+ return mFormatter.getFooter();
+ }
+
+ /**
+ * Helper method which converts row referenced by given cursor to a string
+ * output
+ *
+ * @param cursor
+ * @return CharSequence output, null if given cursor is invalid or no more
+ * data
+ */
+ public String getNextOutput(Cursor cursor) {
+ if (cursor == null || cursor.isAfterLast()) {
+ return null;
+ }
+ String output = mFormatter.getOutput(TrackerEntry.createEntry(cursor));
+ cursor.moveToNext();
+ return output;
+ }
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/TrackerEntry.java b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerEntry.java
new file mode 100644
index 0000000..8c961d1
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerEntry.java
@@ -0,0 +1,253 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.location.Location;
+
+
+/**
+ * Class that holds a tracker entry. An entry can be either a valid location, or
+ * a simple log msg
+ *
+ * It provides a concrete data structure to represent data stored in the
+ * TrackerProvider
+ */
+class TrackerEntry {
+
+ static final String TIMESTAMP = "Timestamp";
+ static final String TAG = "Tag";
+ static final String ENTRY_TYPE = "Type";
+
+ private Location mLocation;
+ private float mDistFromNetLocation;
+ private String mLogMsg;
+
+ static final String ID_COL = "_id";
+ static final String ACCURACY = "Accuracy";
+ static final String LATITUDE = "Latitude";
+ static final String LONGITUDE = "Longitude";
+ static final String ALTITUDE = "Altitude";
+ static final String SPEED = "Speed";
+ static final String BEARING = "Bearing";
+ static final String DIST_NET_LOCATION = "DistFromNetLocation";
+ static final String LOC_TIME = "LocationTime";
+ static final String DEBUG_INFO = "DebugInfo";
+
+ static final String STRING_DATA = "STRING";
+ static final String INT_DATA = "INTEGER";
+ static final String REAL_DATA = "REAL";
+ static final String BLOB_DATA = "BLOB";
+
+ static final String[] ATTRIBUTES = {
+ ID_COL, TIMESTAMP, TAG, ENTRY_TYPE, ACCURACY, LATITUDE, LONGITUDE,
+ ALTITUDE, SPEED, BEARING, DIST_NET_LOCATION, LOC_TIME, DEBUG_INFO};
+ static final String[] ATTRIBUTES_DATA_TYPE = {
+ INT_DATA + " PRIMARY KEY", STRING_DATA, STRING_DATA, STRING_DATA,
+ REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA,
+ REAL_DATA, INT_DATA, STRING_DATA};
+
+ // location extra keys used to retrieve debug info
+ private static final String NETWORK_LOCATION_SOURCE_KEY =
+ "networkLocationSource";
+ private static final String NETWORK_LOCATION_TYPE_KEY =
+ "networkLocationType";
+ private static final String[] LOCATION_DEBUG_KEYS = {
+ NETWORK_LOCATION_SOURCE_KEY, NETWORK_LOCATION_TYPE_KEY};
+
+ enum EntryType {
+ LOCATION_TYPE, LOG_TYPE
+ }
+
+ private String mTimestamp;
+ private String mTag;
+ private EntryType mType;
+
+ private TrackerEntry(String tag, EntryType type) {
+ mType = type;
+ mTag = tag;
+ mLocation = null;
+ }
+
+ private TrackerEntry(Location loc) {
+ this(loc.getProvider(), EntryType.LOCATION_TYPE);
+ mLocation = new Location(loc);
+ }
+
+ /**
+ * Creates a TrackerEntry from a Location
+ */
+ static TrackerEntry createEntry(Location loc, float distFromNetLocation) {
+ TrackerEntry entry = new TrackerEntry(loc);
+
+ String timestampVal = DateUtils.getCurrentKMLTimestamp();
+ entry.setTimestamp(timestampVal);
+ entry.setDistFromNetLocation(distFromNetLocation);
+ return entry;
+ }
+
+ /**
+ * Creates a TrackerEntry from a log msg
+ */
+ static TrackerEntry createEntry(String tag, String msg) {
+ TrackerEntry entry = new TrackerEntry(tag, EntryType.LOG_TYPE);
+ String timestampVal = DateUtils.getCurrentKMLTimestamp();
+ entry.setTimestamp(timestampVal);
+ entry.setLogMsg(msg);
+ return entry;
+ }
+
+ private void setTimestamp(String timestamp) {
+ mTimestamp = timestamp;
+ }
+
+ EntryType getType() {
+ return mType;
+ }
+
+ private void setDistFromNetLocation(float distFromNetLocation) {
+ mDistFromNetLocation = distFromNetLocation;
+ }
+
+ private void setLogMsg(String msg) {
+ mLogMsg = msg;
+ }
+
+ private void setLocation(Location location) {
+ mLocation = location;
+ }
+
+ String getTimestamp() {
+ return mTimestamp;
+ }
+
+ String getTag() {
+ return mTag;
+ }
+
+ Location getLocation() {
+ return mLocation;
+ }
+
+ String getLogMsg() {
+ return mLogMsg;
+ }
+
+ float getDistFromNetLocation() {
+ return mDistFromNetLocation;
+ }
+
+ static void buildCreationString(StringBuilder builder) {
+ if (ATTRIBUTES.length != ATTRIBUTES_DATA_TYPE.length) {
+ throw new IllegalArgumentException(
+ "Attribute length does not match data type length");
+ }
+ for (int i = 0; i < ATTRIBUTES_DATA_TYPE.length; i++) {
+ if (i != 0) {
+ builder.append(", ");
+ }
+ builder.append(String.format("%s %s", ATTRIBUTES[i],
+ ATTRIBUTES_DATA_TYPE[i]));
+ }
+ }
+
+ ContentValues getAsContentValues() {
+ ContentValues cValues = new ContentValues(ATTRIBUTES.length);
+ cValues.put(TIMESTAMP, mTimestamp);
+ cValues.put(TAG, mTag);
+ cValues.put(ENTRY_TYPE, mType.toString());
+ if (mType == EntryType.LOCATION_TYPE) {
+ cValues.put(LATITUDE, mLocation.getLatitude());
+ cValues.put(LONGITUDE, mLocation.getLongitude());
+ if (mLocation.hasAccuracy()) {
+ cValues.put(ACCURACY, mLocation.getAccuracy());
+ }
+ if (mLocation.hasAltitude()) {
+ cValues.put(ALTITUDE, mLocation.getAltitude());
+ }
+ if (mLocation.hasSpeed()) {
+ cValues.put(SPEED, mLocation.getSpeed());
+ }
+ if (mLocation.hasBearing()) {
+ cValues.put(BEARING, mLocation.getBearing());
+ }
+ cValues.put(DIST_NET_LOCATION, mDistFromNetLocation);
+ cValues.put(LOC_TIME, mLocation.getTime());
+ StringBuilder debugBuilder = new StringBuilder("");
+ if (mLocation.getExtras() != null) {
+ for (String key : LOCATION_DEBUG_KEYS) {
+ Object val = mLocation.getExtras().get(key);
+ if (val != null) {
+ debugBuilder.append(String.format("%s=%s; ", key, val
+ .toString()));
+ }
+ }
+ }
+ cValues.put(DEBUG_INFO, debugBuilder.toString());
+ } else {
+ cValues.put(DEBUG_INFO, mLogMsg);
+ }
+ return cValues;
+ }
+
+ static TrackerEntry createEntry(Cursor cursor) {
+ String timestamp = cursor.getString(cursor.getColumnIndex(TIMESTAMP));
+ String tag = cursor.getString(cursor.getColumnIndex(TAG));
+ String sType = cursor.getString(cursor.getColumnIndex(ENTRY_TYPE));
+ TrackerEntry entry = new TrackerEntry(tag, EntryType.valueOf(sType));
+ entry.setTimestamp(timestamp);
+ if (entry.getType() == EntryType.LOCATION_TYPE) {
+ Location location = new Location(tag);
+ location.setLatitude(cursor.getFloat(cursor
+ .getColumnIndexOrThrow(LATITUDE)));
+ location.setLongitude(cursor.getFloat(cursor
+ .getColumnIndexOrThrow(LONGITUDE)));
+
+ Float accuracy = getNullableFloat(cursor, ACCURACY);
+ if (accuracy != null) {
+ location.setAccuracy(accuracy);
+ }
+ Float altitude = getNullableFloat(cursor, ALTITUDE);
+ if (altitude != null) {
+ location.setAltitude(altitude);
+ }
+ Float bearing = getNullableFloat(cursor, BEARING);
+ if (bearing != null) {
+ location.setBearing(bearing);
+ }
+ Float speed = getNullableFloat(cursor, SPEED);
+ if (speed != null) {
+ location.setSpeed(speed);
+ }
+ location.setTime(cursor.getLong(cursor.getColumnIndex(LOC_TIME)));
+ entry.setLocation(location);
+ }
+ entry.setLogMsg(cursor.getString(cursor.getColumnIndex(DEBUG_INFO)));
+
+ return entry;
+ }
+
+ private static Float getNullableFloat(Cursor cursor, String colName) {
+ Float retValue = null;
+ int colIndex = cursor.getColumnIndexOrThrow(colName);
+ if (!cursor.isNull(colIndex)) {
+ retValue = cursor.getFloat(colIndex);
+ }
+ return retValue;
+ }
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/TrackerListHelper.java b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerListHelper.java
new file mode 100644
index 0000000..0247ef0
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerListHelper.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import android.app.ListActivity;
+import android.content.Context;
+import android.database.Cursor;
+import android.view.View;
+import android.widget.ResourceCursorAdapter;
+import android.widget.TextView;
+
+import com.android.locationtracker.R;
+
+/**
+ * Used to bind Tracker data to a list view UI
+ */
+public class TrackerListHelper extends TrackerDataHelper {
+
+ private ListActivity mActivity;
+
+ // sort entries by most recent first
+ private static final String SORT_ORDER = TrackerEntry.ID_COL + " DESC";
+
+ public TrackerListHelper(ListActivity activity) {
+ super(activity, TrackerDataHelper.CSV_FORMATTER);
+ mActivity = activity;
+ }
+
+ /**
+ * Helper method for binding the list activities UI to the tracker data
+ * Tracker data will be sorted in most-recent first order
+ * Will enable automatic UI changes as tracker data changes
+ *
+ * @param layout - layout to populate data
+ */
+ public void bindListUI(int layout) {
+ Cursor cursor = mActivity.managedQuery(TrackerProvider.CONTENT_URI,
+ TrackerEntry.ATTRIBUTES, null, null, SORT_ORDER);
+ // Used to map tracker entries from the database to views
+ TrackerAdapter adapter = new TrackerAdapter(mActivity, layout, cursor);
+ mActivity.setListAdapter(adapter);
+ cursor.setNotificationUri(mActivity.getContentResolver(),
+ TrackerProvider.CONTENT_URI);
+
+ }
+
+ private class TrackerAdapter extends ResourceCursorAdapter {
+
+ public TrackerAdapter(Context context, int layout, Cursor c) {
+ super(context, layout, c);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ final TextView v = (TextView) view
+ .findViewById(R.id.entrylist_item);
+ String rowText = mFormatter.getOutput(TrackerEntry
+ .createEntry(cursor));
+ v.setText(rowText);
+ }
+ }
+}
diff --git a/tests/LocationTracker/src/com/android/locationtracker/data/TrackerProvider.java b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerProvider.java
new file mode 100644
index 0000000..ea849e0
--- /dev/null
+++ b/tests/LocationTracker/src/com/android/locationtracker/data/TrackerProvider.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 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.android.locationtracker.data;
+
+import android.content.ContentProvider;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.net.Uri;
+import android.util.Log;
+
+/**
+ * Content provider for location tracking.
+ *
+ * It is recommended to use the TrackerDataHelper class to access this data
+ * rather than this class directly
+ */
+public class TrackerProvider extends ContentProvider {
+
+ public static final Uri CONTENT_URI = Uri
+ .parse("content://com.android.locationtracker");
+
+ private static final String DB_NAME = "tracking.db";
+ private static final String TABLE_NAME = "tracking";
+ private static final int DB_VERSION = 1;
+
+ private static final String LOG_TAG = "TrackerProvider";
+
+ /**
+ * This class helps open, create, and upgrade the database file.
+ */
+ private static class DatabaseHelper extends SQLiteOpenHelper {
+
+ DatabaseHelper(Context context) {
+ super(context, DB_NAME, null, DB_VERSION);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ StringBuilder queryBuilder = new StringBuilder();
+ queryBuilder.append(String.format("CREATE TABLE %s (", TABLE_NAME));
+ TrackerEntry.buildCreationString(queryBuilder);
+
+ queryBuilder.append(");");
+ db.execSQL(queryBuilder.toString());
+ db.setVersion(DB_VERSION);
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ // TODO: reimplement this when dB version changes
+ Log.w(LOG_TAG, "Upgrading database from version " + oldVersion
+ + " to " + newVersion
+ + ", which will destroy all old data");
+ db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
+ onCreate(db);
+ }
+ }
+
+ private DatabaseHelper mOpenHelper;
+
+ @Override
+ public boolean onCreate() {
+ mOpenHelper = new DatabaseHelper(getContext());
+ return true;
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+ int result = db.delete(TABLE_NAME, selection, selectionArgs);
+ getContext().getContentResolver().notifyChange(uri, null);
+ return result;
+ }
+
+ @Override
+ public String getType(Uri uri) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Uri insert(Uri uri, ContentValues values) {
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+ long rowId = db.insert(TABLE_NAME, null, values);
+ if (rowId > 0) {
+ Uri addedUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
+ getContext().getContentResolver().notifyChange(addedUri, null);
+ return addedUri;
+ }
+ return null;
+ }
+
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection,
+ String[] selectionArgs, String sortOrder) {
+ SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+ // TODO: extract limit from URI ?
+ Cursor cursor = db.query(TABLE_NAME, projection, selection,
+ selectionArgs, null, null, sortOrder);
+ getContext().getContentResolver().notifyChange(uri, null);
+ return cursor;
+ }
+
+ @Override
+ public int update(Uri uri, ContentValues values, String selection,
+ String[] selectionArgs) {
+ throw new UnsupportedOperationException();
+ }
+}