forked from upstream-mirrors/OpenTracks
Merge SAX GPX importer contribution
This commit is contained in:
@@ -24,7 +24,7 @@ import com.google.android.apps.mytracks.content.Track;
|
|||||||
import com.google.android.apps.mytracks.content.Waypoint;
|
import com.google.android.apps.mytracks.content.Waypoint;
|
||||||
import com.google.android.apps.mytracks.io.AuthManager;
|
import com.google.android.apps.mytracks.io.AuthManager;
|
||||||
import com.google.android.apps.mytracks.io.AuthManagerFactory;
|
import com.google.android.apps.mytracks.io.AuthManagerFactory;
|
||||||
import com.google.android.apps.mytracks.io.GpxImport;
|
import com.google.android.apps.mytracks.io.GpxImporter;
|
||||||
import com.google.android.apps.mytracks.io.SendToDocs;
|
import com.google.android.apps.mytracks.io.SendToDocs;
|
||||||
import com.google.android.apps.mytracks.io.SendToMyMaps;
|
import com.google.android.apps.mytracks.io.SendToMyMaps;
|
||||||
import com.google.android.apps.mytracks.io.TrackWriter;
|
import com.google.android.apps.mytracks.io.TrackWriter;
|
||||||
@@ -62,24 +62,25 @@ import android.view.Menu;
|
|||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.Window;
|
||||||
import android.view.View.OnTouchListener;
|
import android.view.View.OnTouchListener;
|
||||||
import android.view.ViewGroup.LayoutParams;
|
import android.view.ViewGroup.LayoutParams;
|
||||||
import android.view.Window;
|
|
||||||
import android.view.WindowManager.BadTokenException;
|
import android.view.WindowManager.BadTokenException;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
import android.widget.TabHost;
|
import android.widget.TabHost;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.io.InputStream;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The super activity that embeds our sub activities.
|
* The super activity that embeds our sub activities.
|
||||||
*
|
*
|
||||||
@@ -950,13 +951,14 @@ public class MyTracks extends TabActivity implements OnTouchListener,
|
|||||||
Thread t = new Thread() {
|
Thread t = new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
boolean success = false;
|
|
||||||
ArrayList<Track> tracks = new ArrayList<Track>();
|
|
||||||
int message = R.string.success;
|
int message = R.string.success;
|
||||||
|
|
||||||
|
long[] trackIdsImported = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
GpxImport.importGPXFile(fileName, tracks);
|
InputStream is = new FileInputStream(fileName);
|
||||||
success = true;
|
trackIdsImported = GpxImporter.importGPXFile(is, providerUtils);
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
Log.e(MyTracksConstants.TAG, "Caught an unexpected exception.", e);
|
Log.e(MyTracksConstants.TAG, "Caught an unexpected exception.", e);
|
||||||
message = R.string.error_generic;
|
message = R.string.error_generic;
|
||||||
@@ -973,15 +975,11 @@ public class MyTracks extends TabActivity implements OnTouchListener,
|
|||||||
Log.e(MyTracksConstants.TAG, "Caught an unexpected exception.", e);
|
Log.e(MyTracksConstants.TAG, "Caught an unexpected exception.", e);
|
||||||
message = R.string.error_out_of_memory;
|
message = R.string.error_out_of_memory;
|
||||||
}
|
}
|
||||||
if (success) {
|
if (trackIdsImported != null && trackIdsImported.length > 0) {
|
||||||
long trackId = -1;
|
// select last track from import file
|
||||||
for (Track track : tracks) {
|
setSelectedTrack(trackIdsImported[trackIdsImported.length - 1]);
|
||||||
Uri uri = providerUtils.insertTrackAndTrackPoints(track);
|
|
||||||
trackId = Long.parseLong(uri.getLastPathSegment());
|
|
||||||
}
|
|
||||||
setSelectedTrack(trackId);
|
|
||||||
} else {
|
} else {
|
||||||
MyTracks.this.showMessageDialog(message, false/*success*/);
|
MyTracks.this.showMessageDialog(message, false/* success */);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
|
|||||||
@@ -1,237 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.stats.TripStatisticsBuilder;
|
|
||||||
import com.google.android.apps.mytracks.util.MyTracksUtils;
|
|
||||||
|
|
||||||
import android.location.Location;
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.NamedNodeMap;
|
|
||||||
import org.w3c.dom.Node;
|
|
||||||
import org.w3c.dom.NodeList;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper class to parse a GPX file or string and convert it into a track
|
|
||||||
* object.
|
|
||||||
*
|
|
||||||
* TODO: See if we can use a SAX style parser as the DOM style
|
|
||||||
* parsing uses too much memory and will not allow import of very large GPX
|
|
||||||
* files (limit currently set to 500KB).
|
|
||||||
*
|
|
||||||
* @author Leif Hendrik Wilden
|
|
||||||
*/
|
|
||||||
public class GpxImport {
|
|
||||||
private static final SimpleDateFormat DATE_FORMAT1 =
|
|
||||||
new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
|
|
||||||
private static final SimpleDateFormat DATE_FORMAT2 =
|
|
||||||
new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
|
||||||
private static final SimpleDateFormat DATE_FORMAT3 =
|
|
||||||
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads GPS tracks from a GPX file and append tracks and their coordinates to
|
|
||||||
* the given list of tracks.
|
|
||||||
*
|
|
||||||
* @param tracks a list of tracks
|
|
||||||
* @param filename a file name
|
|
||||||
* @throws SAXException a parsing error
|
|
||||||
* @throws ParserConfigurationException internal error
|
|
||||||
* @throws IOException a file reading problem
|
|
||||||
*/
|
|
||||||
public static void importGPXFile(
|
|
||||||
final String filename, final ArrayList<Track> tracks)
|
|
||||||
throws SAXException, ParserConfigurationException, IOException,
|
|
||||||
OutOfMemoryError {
|
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
||||||
File file = new File(filename);
|
|
||||||
if (file.length() > 500 * 1024) {
|
|
||||||
// Better to throw an error now then, to let the VM do it. If the VM does
|
|
||||||
// it, it will throw another exception while throwing the OutOfMemoryError
|
|
||||||
// and that will kill out app with a nasty error message. In that way we
|
|
||||||
// can at least display a reasonable message to the user and let her know
|
|
||||||
// what the problem is.
|
|
||||||
throw new OutOfMemoryError();
|
|
||||||
}
|
|
||||||
Document doc = builder.parse(file);
|
|
||||||
importGPXDocument(tracks, doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a utility class with only static members.
|
|
||||||
*/
|
|
||||||
private GpxImport() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads GPS tracks from a GPX document and append then to the given list of
|
|
||||||
* tracks.
|
|
||||||
*
|
|
||||||
* TODO: Break this method down into smaller ones
|
|
||||||
*
|
|
||||||
* @param tracks an array list of tracks
|
|
||||||
* @param doc a document
|
|
||||||
*/
|
|
||||||
public static void importGPXDocument(
|
|
||||||
final ArrayList<Track> tracks, final Document doc) {
|
|
||||||
NodeList trackNodes = doc.getElementsByTagName("trk");
|
|
||||||
for (int n = 0; n < trackNodes.getLength(); n++) {
|
|
||||||
Track track = new Track();
|
|
||||||
tracks.add(track);
|
|
||||||
ArrayList<Location> locations = track.getLocations();
|
|
||||||
Location lastLocation = null;
|
|
||||||
|
|
||||||
Node trkNode = trackNodes.item(n);
|
|
||||||
NodeList segmentNodes = trkNode.getChildNodes();
|
|
||||||
ArrayList<Node> nodes = new ArrayList<Node>();
|
|
||||||
int nSegments = 0;
|
|
||||||
for (int k = 0; k < segmentNodes.getLength(); k++) {
|
|
||||||
Node segmentNode = segmentNodes.item(k);
|
|
||||||
if (segmentNode.getNodeName().equals("name")) {
|
|
||||||
track.setName(segmentNode.getFirstChild().getNodeValue());
|
|
||||||
} else if (segmentNode.getNodeName().equals("description")) {
|
|
||||||
track.setDescription(segmentNode.getFirstChild().getNodeValue());
|
|
||||||
} else if (segmentNode.getNodeName().equals("trkseg")) {
|
|
||||||
if (nSegments > 0) {
|
|
||||||
// Add a segment separator:
|
|
||||||
Location location = new Location("gps");
|
|
||||||
location.setLatitude(100.0);
|
|
||||||
location.setLongitude(100.0);
|
|
||||||
location.setAltitude(0);
|
|
||||||
if (locations.size() > 0) {
|
|
||||||
long pointTime = locations.get(locations.size() - 1).getTime();
|
|
||||||
location.setTime(pointTime);
|
|
||||||
}
|
|
||||||
track.addLocation(location);
|
|
||||||
lastLocation = null;
|
|
||||||
}
|
|
||||||
nSegments++;
|
|
||||||
NodeList segmentChildren = segmentNode.getChildNodes();
|
|
||||||
nodes.clear();
|
|
||||||
for (int j = 0; j < segmentChildren.getLength(); j++) {
|
|
||||||
Node child = segmentChildren.item(j);
|
|
||||||
if (child.getNodeName().equals("trkpt")) {
|
|
||||||
nodes.add(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < nodes.size(); i++) {
|
|
||||||
NamedNodeMap namedNodes = nodes.get(i).getAttributes();
|
|
||||||
double lat = Double.parseDouble(namedNodes.getNamedItem("lat")
|
|
||||||
.getNodeValue());
|
|
||||||
double lon = Double.parseDouble(namedNodes.getNamedItem("lon")
|
|
||||||
.getNodeValue());
|
|
||||||
NodeList children = nodes.get(i).getChildNodes();
|
|
||||||
Node elementNode = null;
|
|
||||||
Node timeNode = null;
|
|
||||||
for (int j = 0; j < children.getLength(); j++) {
|
|
||||||
Node child = children.item(j);
|
|
||||||
if (child.getNodeName().equals("ele")) {
|
|
||||||
elementNode = child;
|
|
||||||
} else if (child.getNodeName().equals("time")) {
|
|
||||||
timeNode = child;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String altitudeStr = null;
|
|
||||||
if (elementNode != null) {
|
|
||||||
altitudeStr = elementNode.getFirstChild().getNodeValue();
|
|
||||||
} else {
|
|
||||||
altitudeStr = "0";
|
|
||||||
}
|
|
||||||
String timeContents = null;
|
|
||||||
if (timeNode != null) {
|
|
||||||
timeContents = timeNode.getFirstChild().getNodeValue();
|
|
||||||
} else {
|
|
||||||
timeContents = "";
|
|
||||||
}
|
|
||||||
double altitude = Double.parseDouble(altitudeStr);
|
|
||||||
long t = -1;
|
|
||||||
try {
|
|
||||||
// 1st try with time zone at end a la "+0000"
|
|
||||||
t = DATE_FORMAT1.parse(timeContents).getTime();
|
|
||||||
} catch (ParseException e) {
|
|
||||||
// if that fails, try with a literal "Z" at the end (this is not
|
|
||||||
// according to xml standard, but some gpx files are like that):
|
|
||||||
try {
|
|
||||||
t = DATE_FORMAT2.parse(timeContents).getTime();
|
|
||||||
} catch (ParseException ex) {
|
|
||||||
// some gpx timestamps have 3 additional digits at the end.
|
|
||||||
try {
|
|
||||||
t = DATE_FORMAT3.parse(timeContents).getTime();
|
|
||||||
} catch (ParseException exc) {
|
|
||||||
t = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Location location = new Location("gps");
|
|
||||||
location.setLatitude(lat);
|
|
||||||
location.setLongitude(lon);
|
|
||||||
location.setAltitude(altitude);
|
|
||||||
location.setTime(t);
|
|
||||||
// We don't have a speed and bearing in GPX, make something up from
|
|
||||||
// the last two points:
|
|
||||||
if (lastLocation != null) {
|
|
||||||
final long dt = location.getTime() - lastLocation.getTime();
|
|
||||||
if (dt > 0) {
|
|
||||||
final float speed =
|
|
||||||
location.distanceTo(lastLocation) / (dt / 1000);
|
|
||||||
location.setSpeed(speed);
|
|
||||||
}
|
|
||||||
location.setBearing(lastLocation.bearingTo(location));
|
|
||||||
}
|
|
||||||
lastLocation = location;
|
|
||||||
if (MyTracksUtils.isValidLocation(location)) {
|
|
||||||
track.addLocation(location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (locations.size() > 0) {
|
|
||||||
long startTime = locations.get(0).getTime();
|
|
||||||
|
|
||||||
// Calculate statistics for the imported track
|
|
||||||
TripStatisticsBuilder statsBuilder = new TripStatisticsBuilder();
|
|
||||||
statsBuilder.resumeAt(startTime);
|
|
||||||
for (Location location : locations) {
|
|
||||||
if (MyTracksUtils.isValidLocation(location)) {
|
|
||||||
/* Any time works here. The totalTime will be set by "pauseAt" later: */
|
|
||||||
statsBuilder.addLocation(location, location.getTime());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long lastPointTime = locations.get(locations.size() - 1).getTime();
|
|
||||||
statsBuilder.pauseAt(lastPointTime);
|
|
||||||
|
|
||||||
track.setStatistics(statsBuilder.getStatistics());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,517 @@
|
|||||||
|
/*
|
||||||
|
* 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.MyTracksProviderUtils;
|
||||||
|
import com.google.android.apps.mytracks.content.Track;
|
||||||
|
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||||
|
import com.google.android.apps.mytracks.util.MyTracksUtils;
|
||||||
|
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationManager;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.SimpleTimeZone;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import javax.xml.parsers.SAXParser;
|
||||||
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
|
||||||
|
import org.xml.sax.Attributes;
|
||||||
|
import org.xml.sax.Locator;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Imports GPX XML files to the my tracks provider
|
||||||
|
*
|
||||||
|
* @author Leif Hendrik Wilden
|
||||||
|
* @author Steffen Horlacher
|
||||||
|
*/
|
||||||
|
public class GpxImporter extends DefaultHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Different date formats used in GPX files
|
||||||
|
*/
|
||||||
|
static final SimpleDateFormat DATE_FORMAT1 = new SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd'T'hh:mm:ssZ");
|
||||||
|
static final SimpleDateFormat DATE_FORMAT2 = new SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd'T'hh:mm:ss'Z'");
|
||||||
|
static final SimpleDateFormat DATE_FORMAT3 = new SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||||
|
static final SimpleTimeZone UTC_TIMEZONE = new SimpleTimeZone(0, "UTC");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GPX-XML tag names and attributes
|
||||||
|
*/
|
||||||
|
private static final String TAG_TRACK = "trk";
|
||||||
|
private static final String TAG_TRACK_POINT = "trkpt";
|
||||||
|
private static final String TAG_NAME = "name";
|
||||||
|
private static final String TAG_DESCRIPTION = "desc";
|
||||||
|
private static final String TAG_ALTITUDE = "ele";
|
||||||
|
private static final String TAG_TIME = "time";
|
||||||
|
private static final String ATT_LAT = "lat";
|
||||||
|
private static final String ATT_LON = "lon";
|
||||||
|
|
||||||
|
final private MyTracksProviderUtils providerUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of track ids written in the database. Only contains successfully
|
||||||
|
* written tracks.
|
||||||
|
*/
|
||||||
|
final private List<Long> tracksWritten;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains the current elements content
|
||||||
|
*/
|
||||||
|
private StringBuilder content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Currently reading location
|
||||||
|
*/
|
||||||
|
private Location location;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Previous location, required for calculations
|
||||||
|
*/
|
||||||
|
private Location lastLocation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Currently reading track
|
||||||
|
*/
|
||||||
|
private Track track;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Statistics object for the current track
|
||||||
|
*/
|
||||||
|
private TripStatistics stats;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of locations already processed
|
||||||
|
*/
|
||||||
|
private int numberOfLocations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to identify if a track was written to the database but not yet
|
||||||
|
* finished successfully.
|
||||||
|
*/
|
||||||
|
private boolean isCurrentTrackRollbackable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flag to indicate if we in a track xml element some sub elements like name
|
||||||
|
* may be used in other parts of the gpx file - ignore them
|
||||||
|
*/
|
||||||
|
private boolean isInTrackElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counter to find out which child level of track we are processing
|
||||||
|
*/
|
||||||
|
private int trackChildDepth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SAX-Locator to get current line information
|
||||||
|
*/
|
||||||
|
private Locator locator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads GPS tracks from a GPX file and writes tracks and their coordinates to
|
||||||
|
* the database.
|
||||||
|
*
|
||||||
|
* @param tracks
|
||||||
|
* a list of tracks
|
||||||
|
* @param is
|
||||||
|
* a input steam with gpx-xml data
|
||||||
|
* @return long[] array of track ids written in the database
|
||||||
|
* @throws SAXException
|
||||||
|
* a parsing error
|
||||||
|
* @throws ParserConfigurationException
|
||||||
|
* internal error
|
||||||
|
* @throws IOException
|
||||||
|
* a file reading problem
|
||||||
|
*/
|
||||||
|
public static long[] importGPXFile(final InputStream is,
|
||||||
|
final MyTracksProviderUtils providerUtils)
|
||||||
|
throws ParserConfigurationException, SAXException, IOException {
|
||||||
|
|
||||||
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||||
|
GpxImporter handler = new GpxImporter(providerUtils);
|
||||||
|
SAXParser parser = factory.newSAXParser();
|
||||||
|
long[] trackIds = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
parser.parse(is, handler);
|
||||||
|
trackIds = handler.getImportedTrackIds();
|
||||||
|
} finally {
|
||||||
|
// delete track if not finished
|
||||||
|
handler.rollbackUnfinishedTracks();
|
||||||
|
}
|
||||||
|
|
||||||
|
return trackIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor, requires providerUtils for writing tracks the database.
|
||||||
|
*/
|
||||||
|
public GpxImporter(MyTracksProviderUtils providerUtils) {
|
||||||
|
this.providerUtils = providerUtils;
|
||||||
|
tracksWritten = new ArrayList<Long>();
|
||||||
|
content = new StringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void characters(char[] ch, int start, int length) throws SAXException {
|
||||||
|
content.append(ch, start, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startElement(String uri, String localName, String name,
|
||||||
|
Attributes attributes) throws SAXException {
|
||||||
|
|
||||||
|
// reset element content
|
||||||
|
content.setLength(0);
|
||||||
|
|
||||||
|
if (localName.equalsIgnoreCase(TAG_TRACK)) {
|
||||||
|
|
||||||
|
// test if we are already in a track element - abort in this case
|
||||||
|
if (isInTrackElement) {
|
||||||
|
String msg = createErrorMessage("Invalid GPX-XML detected");
|
||||||
|
throw new SAXException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
isInTrackElement = true;
|
||||||
|
trackChildDepth = 0;
|
||||||
|
onTrackElementStart();
|
||||||
|
|
||||||
|
// process this element only as sub-elements of track
|
||||||
|
} else if (isInTrackElement) {
|
||||||
|
|
||||||
|
trackChildDepth++;
|
||||||
|
if (localName.equalsIgnoreCase(TAG_TRACK_POINT)) {
|
||||||
|
onTrackPointElementStart(attributes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void endElement(String uri, String localName, String name)
|
||||||
|
throws SAXException {
|
||||||
|
|
||||||
|
if (localName.equalsIgnoreCase(TAG_TRACK)) {
|
||||||
|
onTrackElementEnd();
|
||||||
|
isInTrackElement = false;
|
||||||
|
trackChildDepth = 0;
|
||||||
|
|
||||||
|
// process these elements only as sub-elements of track
|
||||||
|
} else if (isInTrackElement) {
|
||||||
|
|
||||||
|
if (localName.equalsIgnoreCase(TAG_TRACK_POINT)) {
|
||||||
|
onTrackPointElementEnd();
|
||||||
|
} else if (localName.equalsIgnoreCase(TAG_ALTITUDE)) {
|
||||||
|
onAltitudeElementEnd();
|
||||||
|
} else if (localName.equalsIgnoreCase(TAG_TIME)) {
|
||||||
|
onTimeElementEnd();
|
||||||
|
} else if (localName.equalsIgnoreCase(TAG_NAME)) {
|
||||||
|
// we are only interested in the first level name element
|
||||||
|
if (trackChildDepth == 1) {
|
||||||
|
onNameElementEnd();
|
||||||
|
}
|
||||||
|
} else if (localName.equalsIgnoreCase(TAG_DESCRIPTION)) {
|
||||||
|
// we are only interested in the first level description element
|
||||||
|
if (trackChildDepth == 1) {
|
||||||
|
onDescriptionElementEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
trackChildDepth--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset element content
|
||||||
|
content.setLength(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDocumentLocator(Locator locator) {
|
||||||
|
this.locator = locator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Track object and insert empty track in database. Track will be
|
||||||
|
* updated with missing values later.
|
||||||
|
*/
|
||||||
|
private void onTrackElementStart() {
|
||||||
|
|
||||||
|
track = new Track();
|
||||||
|
numberOfLocations = 0;
|
||||||
|
|
||||||
|
Uri trackUri = providerUtils.insertTrack(track);
|
||||||
|
long trackId = Long.parseLong(trackUri.getLastPathSegment());
|
||||||
|
track.setId(trackId);
|
||||||
|
isCurrentTrackRollbackable = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads trackpoint attributes and assigns them to the current location
|
||||||
|
*
|
||||||
|
* @param attributes
|
||||||
|
* xml attributes
|
||||||
|
*/
|
||||||
|
private void onTrackPointElementStart(Attributes attributes) {
|
||||||
|
location = createLocationFromAttributes(attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Location createLocationFromAttributes(Attributes attributes) {
|
||||||
|
String latitude = null;
|
||||||
|
String longitude = null;
|
||||||
|
|
||||||
|
for (int i = 0; i < attributes.getLength(); i++) {
|
||||||
|
if (attributes.getLocalName(i).equals(ATT_LAT)) {
|
||||||
|
latitude = attributes.getValue(i);
|
||||||
|
} else if (attributes.getLocalName(i).equals(ATT_LON)) {
|
||||||
|
longitude = attributes.getValue(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create new location and set attributes
|
||||||
|
Location loc = new Location(LocationManager.GPS_PROVIDER);
|
||||||
|
loc.setLatitude(Double.parseDouble(latitude));
|
||||||
|
loc.setLongitude(Double.parseDouble(longitude));
|
||||||
|
return loc;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onDescriptionElementEnd() {
|
||||||
|
track.setDescription(content.toString().trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onNameElementEnd() {
|
||||||
|
track.setName(content.toString().trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Track point finished, write in database
|
||||||
|
*
|
||||||
|
* @throws SAXException
|
||||||
|
* - thrown if track point is invalid
|
||||||
|
*/
|
||||||
|
private void onTrackPointElementEnd() throws SAXException {
|
||||||
|
|
||||||
|
if (MyTracksUtils.isValidLocation(location)) {
|
||||||
|
|
||||||
|
stats.addLocation(location, location.getTime());
|
||||||
|
|
||||||
|
// insert in db
|
||||||
|
Uri trackPointIdUri = providerUtils.insertTrackPoint(location, track
|
||||||
|
.getId());
|
||||||
|
|
||||||
|
// set start and stop id for track
|
||||||
|
long trackPointId = Long.parseLong(trackPointIdUri.getLastPathSegment());
|
||||||
|
|
||||||
|
// first track point?
|
||||||
|
if (lastLocation == null) {
|
||||||
|
track.setStartId(trackPointId);
|
||||||
|
}
|
||||||
|
// location has no setId method
|
||||||
|
// updating stop id on track every time...
|
||||||
|
track.setStopId(trackPointId);
|
||||||
|
|
||||||
|
lastLocation = location;
|
||||||
|
numberOfLocations++;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// invalid location - abort import
|
||||||
|
String msg = createErrorMessage("Invalid location detected: " + location);
|
||||||
|
throw new SAXException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Track finished - update in database
|
||||||
|
*/
|
||||||
|
private void onTrackElementEnd() {
|
||||||
|
|
||||||
|
if (lastLocation != null) {
|
||||||
|
|
||||||
|
// Calculate statistics for the imported track and update
|
||||||
|
stats.pauseAt(lastLocation.getTime());
|
||||||
|
track.setStopTime(lastLocation.getTime());
|
||||||
|
track.setNumberOfPoints(numberOfLocations);
|
||||||
|
stats.fillStatisticsForTrack(track);
|
||||||
|
providerUtils.updateTrack(track);
|
||||||
|
tracksWritten.add(track.getId());
|
||||||
|
isCurrentTrackRollbackable = false;
|
||||||
|
lastLocation = null;
|
||||||
|
stats = null;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// track contains no track points makes not really
|
||||||
|
// sense to import it as we have no location
|
||||||
|
// information -> roll back
|
||||||
|
rollbackUnfinishedTracks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setting time and doing additional calculations as this is the last value
|
||||||
|
* required. Also sets the start time for track and statistics as there is no
|
||||||
|
* start time in the track root element
|
||||||
|
*
|
||||||
|
* @throws SAXException
|
||||||
|
* on parsing errors
|
||||||
|
*/
|
||||||
|
private void onTimeElementEnd() throws SAXException {
|
||||||
|
|
||||||
|
long time = parseTimeForAllFormats(content.toString().trim());
|
||||||
|
|
||||||
|
if (location != null) {
|
||||||
|
|
||||||
|
// check for negative time change
|
||||||
|
if (lastLocation != null) {
|
||||||
|
long timeDifference = time - lastLocation.getTime();
|
||||||
|
if (timeDifference < 0) {
|
||||||
|
String msg = createErrorMessage("Found negative time change.");
|
||||||
|
throw new SAXException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
location.setTime(time);
|
||||||
|
// initialize start time with time of first track point
|
||||||
|
if (stats == null) {
|
||||||
|
stats = new TripStatistics(time);
|
||||||
|
track.setStartTime(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't have a speed and bearing in GPX, make something up from
|
||||||
|
// the last two points.
|
||||||
|
// TODO GPS points tend to have some inherent imprecision,
|
||||||
|
// speed and bearing will likely be off, so the statistics for things like
|
||||||
|
// max speed will also be off.
|
||||||
|
if (lastLocation != null) {
|
||||||
|
final long dt = location.getTime() - lastLocation.getTime();
|
||||||
|
if (dt > 0) {
|
||||||
|
final float speed = location.distanceTo(lastLocation) / (dt / 1000);
|
||||||
|
location.setSpeed(speed);
|
||||||
|
}
|
||||||
|
location.setBearing(lastLocation.bearingTo(location));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onAltitudeElementEnd() {
|
||||||
|
if (location != null) {
|
||||||
|
String altitude = content.toString().trim();
|
||||||
|
location.setAltitude(Double.parseDouble(altitude));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a exception is thrown during the import callers must execute this method
|
||||||
|
* in the catch (also finally is ok) clause to avoid inconsistent data.
|
||||||
|
*/
|
||||||
|
public void rollbackUnfinishedTracks() {
|
||||||
|
if (isCurrentTrackRollbackable) {
|
||||||
|
providerUtils.deleteTrack(track.getId());
|
||||||
|
isCurrentTrackRollbackable = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all track ids of the tracks created by this importer run
|
||||||
|
*
|
||||||
|
* @return array of track ids
|
||||||
|
*/
|
||||||
|
private long[] getImportedTrackIds() {
|
||||||
|
// Convert from java.lang.Long for convenience
|
||||||
|
long[] result = new long[tracksWritten.size()];
|
||||||
|
for (int i = 0; i < result.length; i++) {
|
||||||
|
result[i] = tracksWritten.get(i);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse time trying different formats used in GPX files
|
||||||
|
*
|
||||||
|
* @param timeContents
|
||||||
|
* string with time information
|
||||||
|
* @return time as long
|
||||||
|
* @throws SAXException
|
||||||
|
* on time parsing errors
|
||||||
|
*/
|
||||||
|
private long parseTimeForAllFormats(String timeContents) throws SAXException {
|
||||||
|
|
||||||
|
long time = -1;
|
||||||
|
|
||||||
|
// 1st try with time zone at end a la "+0000"
|
||||||
|
time = parseTime(timeContents, DATE_FORMAT1);
|
||||||
|
if(time > -1) {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if that fails, try with a literal "Z" at the end
|
||||||
|
// (this is not according to xml standard, but some gpx files are like
|
||||||
|
// that):
|
||||||
|
time = parseTime(timeContents, DATE_FORMAT2);
|
||||||
|
if(time > -1) {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
// some gpx timestamps have 3 additional digits at the end.
|
||||||
|
time = parseTime(timeContents, DATE_FORMAT3);
|
||||||
|
if(time > -1) {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
// everything failed - abort the import
|
||||||
|
String msg = createErrorMessage("Invalid time format: " + timeContents);
|
||||||
|
throw new SAXException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long parseTime(String timeContents, SimpleDateFormat formatParam) {
|
||||||
|
SimpleDateFormat format = formatParam;
|
||||||
|
try {
|
||||||
|
format.setTimeZone(UTC_TIMEZONE);
|
||||||
|
return format.parse(timeContents).getTime();
|
||||||
|
} catch (ParseException ex) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds an parsing error message with current line information
|
||||||
|
*
|
||||||
|
* @param details
|
||||||
|
* details about the error, will be appended
|
||||||
|
* @return error message string with current line information
|
||||||
|
*/
|
||||||
|
private String createErrorMessage(String details) {
|
||||||
|
StringBuffer msg = new StringBuffer();
|
||||||
|
msg.append("Parsing error at line: ");
|
||||||
|
msg.append(locator.getLineNumber());
|
||||||
|
msg.append(" column: ");
|
||||||
|
msg.append(locator.getColumnNumber());
|
||||||
|
msg.append(". ");
|
||||||
|
msg.append(details);
|
||||||
|
return msg.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,221 @@
|
|||||||
|
package com.google.android.apps.mytracks.io;
|
||||||
|
|
||||||
|
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||||
|
import com.google.android.apps.mytracks.content.Track;
|
||||||
|
import com.google.android.apps.mytracks.content.TrackPointsColumns;
|
||||||
|
import com.google.android.apps.mytracks.content.TracksColumns;
|
||||||
|
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.Factory;
|
||||||
|
import com.google.android.apps.mytracks.testing.TestingProviderUtilsFactory;
|
||||||
|
import com.google.android.testing.mocking.AndroidMock;
|
||||||
|
import com.google.android.testing.mocking.UsesMocks;
|
||||||
|
|
||||||
|
import android.content.ContentUris;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
|
import org.easymock.Capture;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for the GPX importer.
|
||||||
|
*
|
||||||
|
* @author Steffen Horlacher
|
||||||
|
*/
|
||||||
|
public class GpxImporterTest extends AndroidTestCase {
|
||||||
|
|
||||||
|
private static final String TRACK_NAME = "blablub";
|
||||||
|
private static final String TRACK_DESC = "s'Laebe isch koi Schlotzer";
|
||||||
|
|
||||||
|
private static final String TRACK_LAT_1 = "48.768364";
|
||||||
|
private static final String TRACK_LON_1 = "9.177886";
|
||||||
|
private static final String TRACK_ELE_1 = "324.0";
|
||||||
|
private static final String TRACK_TIME_1 = "2010-04-22T18:21:00Z";
|
||||||
|
|
||||||
|
private static final String TRACK_LAT_2 = "48.768374";
|
||||||
|
private static final String TRACK_LON_2 = "9.177816";
|
||||||
|
private static final String TRACK_ELE_2 = "333.0";
|
||||||
|
private static final String TRACK_TIME_2 = "2010-04-22T18:21:50Z";
|
||||||
|
|
||||||
|
// TODO use real files from different sources with more track points
|
||||||
|
private static final String VALID_TEST_GPX = "<gpx><trk><name><![CDATA["
|
||||||
|
+ TRACK_NAME + "]]></name><desc><![CDATA[" + TRACK_DESC
|
||||||
|
+ "]]></desc><trkseg>" + "<trkpt lat=\"" + TRACK_LAT_1 + "\" lon=\""
|
||||||
|
+ TRACK_LON_1 + "\"><ele>" + TRACK_ELE_1 + "</ele><time>"
|
||||||
|
+ TRACK_TIME_1 + "</time></trkpt> +" + "<trkpt lat=\"" + TRACK_LAT_2
|
||||||
|
+ "\" lon=\"" + TRACK_LON_2 + "\"><ele>" + TRACK_ELE_2
|
||||||
|
+ "</ele><time>" + TRACK_TIME_2 + "</time></trkpt>"
|
||||||
|
+ "</trkseg></trk></gpx>";
|
||||||
|
|
||||||
|
// invalid xml
|
||||||
|
private static final String INVALID_XML_TEST_GPX = VALID_TEST_GPX
|
||||||
|
.substring(0, VALID_TEST_GPX.length() - 50);
|
||||||
|
private static final String INVALID_LOCATION_TEST_GPX = VALID_TEST_GPX
|
||||||
|
.replaceAll(TRACK_LAT_1, "1000.0");
|
||||||
|
private static final String INVALID_TIME_TEST_GPX = VALID_TEST_GPX
|
||||||
|
.replaceAll(TRACK_TIME_1, "invalid");
|
||||||
|
|
||||||
|
private static final long TRACK_ID = 1;
|
||||||
|
private static final long TRACK_POINT_ID_1 = 1;
|
||||||
|
private static final long TRACK_POINT_ID_2 = 1;
|
||||||
|
|
||||||
|
private static final Uri TRACK_ID_URI = ContentUris.appendId(
|
||||||
|
TracksColumns.CONTENT_URI.buildUpon(), TRACK_ID).build();
|
||||||
|
private static final Uri TRACK_POINT_ID_URI_1 = ContentUris.appendId(
|
||||||
|
TrackPointsColumns.CONTENT_URI.buildUpon(), TRACK_POINT_ID_1).build();
|
||||||
|
private static final Uri TRACK_POINT_ID_URI_2 = ContentUris.appendId(
|
||||||
|
TrackPointsColumns.CONTENT_URI.buildUpon(), TRACK_POINT_ID_2).build();
|
||||||
|
|
||||||
|
private MyTracksProviderUtils providerUtils;
|
||||||
|
|
||||||
|
private Factory oldProviderUtilsFactory;
|
||||||
|
|
||||||
|
@UsesMocks(MyTracksProviderUtils.class)
|
||||||
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
providerUtils = AndroidMock.createMock(MyTracksProviderUtils.class);
|
||||||
|
oldProviderUtilsFactory = TestingProviderUtilsFactory
|
||||||
|
.installWithInstance(providerUtils);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
TestingProviderUtilsFactory.restoreOldFactory(oldProviderUtilsFactory);
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test import success
|
||||||
|
*/
|
||||||
|
public void testImportSuccess() throws Exception {
|
||||||
|
|
||||||
|
Capture<Track> trackParam = new Capture<Track>();
|
||||||
|
Capture<Location> locParam1 = new MyLocationCaptue();
|
||||||
|
Capture<Location> locParam2 = new MyLocationCaptue();
|
||||||
|
|
||||||
|
AndroidMock.expect(
|
||||||
|
providerUtils.insertTrack(AndroidMock.capture(trackParam)))
|
||||||
|
.andReturn(TRACK_ID_URI);
|
||||||
|
|
||||||
|
AndroidMock.expect(
|
||||||
|
providerUtils.insertTrackPoint(AndroidMock.capture(locParam1),
|
||||||
|
AndroidMock.anyLong())).andReturn(TRACK_POINT_ID_URI_1);
|
||||||
|
|
||||||
|
AndroidMock.expect(
|
||||||
|
providerUtils.insertTrackPoint(AndroidMock.capture(locParam2),
|
||||||
|
AndroidMock.anyLong())).andReturn(TRACK_POINT_ID_URI_2);
|
||||||
|
|
||||||
|
providerUtils.updateTrack(AndroidMock.capture(trackParam));
|
||||||
|
|
||||||
|
AndroidMock.replay(providerUtils);
|
||||||
|
|
||||||
|
InputStream is = new ByteArrayInputStream(VALID_TEST_GPX.getBytes());
|
||||||
|
GpxImporter.importGPXFile(is, providerUtils);
|
||||||
|
|
||||||
|
AndroidMock.verify();
|
||||||
|
|
||||||
|
SimpleDateFormat format = GpxImporter.DATE_FORMAT2;
|
||||||
|
|
||||||
|
// verify track parameter
|
||||||
|
Track track = trackParam.getValue();
|
||||||
|
assertEquals(TRACK_NAME, track.getName());
|
||||||
|
assertEquals(TRACK_DESC, track.getDescription());
|
||||||
|
assertEquals(format.parse(TRACK_TIME_1).getTime(), track.getStartTime());
|
||||||
|
assertNotSame(-1, track.getStartId());
|
||||||
|
assertNotSame(-1, track.getStopId());
|
||||||
|
|
||||||
|
// verify last location parameter
|
||||||
|
Location loc1 = locParam1.getValue();
|
||||||
|
assertEquals(Double.parseDouble(TRACK_LAT_1), loc1.getLatitude());
|
||||||
|
assertEquals(Double.parseDouble(TRACK_LON_1), loc1.getLongitude());
|
||||||
|
assertEquals(Double.parseDouble(TRACK_ELE_1), loc1.getAltitude());
|
||||||
|
assertEquals(format.parse(TRACK_TIME_1).getTime(), loc1.getTime());
|
||||||
|
|
||||||
|
Location loc2 = locParam2.getValue();
|
||||||
|
assertEquals(Double.parseDouble(TRACK_LAT_2), loc2.getLatitude());
|
||||||
|
assertEquals(Double.parseDouble(TRACK_LON_2), loc2.getLongitude());
|
||||||
|
assertEquals(Double.parseDouble(TRACK_ELE_2), loc2.getAltitude());
|
||||||
|
assertEquals(format.parse(TRACK_TIME_2).getTime(), loc2.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test with invalid location - track should be deleted
|
||||||
|
*/
|
||||||
|
public void testImportLocationFailure() throws ParserConfigurationException,
|
||||||
|
SAXException, IOException {
|
||||||
|
|
||||||
|
testInvalidXML(INVALID_LOCATION_TEST_GPX);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test with invalid time - track should be deleted
|
||||||
|
*/
|
||||||
|
public void testImportTimeFailure() throws ParserConfigurationException,
|
||||||
|
SAXException, IOException {
|
||||||
|
|
||||||
|
testInvalidXML(INVALID_TIME_TEST_GPX);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test with invalid xml - track should be deleted
|
||||||
|
*/
|
||||||
|
public void testImportXMLFailure() throws ParserConfigurationException,
|
||||||
|
SAXException, IOException {
|
||||||
|
|
||||||
|
testInvalidXML(INVALID_XML_TEST_GPX);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testInvalidXML(String xml) throws ParserConfigurationException,
|
||||||
|
IOException {
|
||||||
|
|
||||||
|
AndroidMock.expect(
|
||||||
|
providerUtils.insertTrack((Track) AndroidMock.anyObject()))
|
||||||
|
.andReturn(TRACK_ID_URI);
|
||||||
|
|
||||||
|
AndroidMock.expect(
|
||||||
|
providerUtils.insertTrackPoint((Location) AndroidMock.anyObject(),
|
||||||
|
AndroidMock.anyLong())).andStubReturn(TRACK_POINT_ID_URI_1);
|
||||||
|
|
||||||
|
providerUtils.deleteTrack(TRACK_ID);
|
||||||
|
|
||||||
|
AndroidMock.replay(providerUtils);
|
||||||
|
|
||||||
|
InputStream is = new ByteArrayInputStream(xml.getBytes());
|
||||||
|
|
||||||
|
try {
|
||||||
|
GpxImporter.importGPXFile(is, providerUtils);
|
||||||
|
} catch (SAXException e) {
|
||||||
|
// expected exception
|
||||||
|
}
|
||||||
|
|
||||||
|
AndroidMock.verify();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Workaround because of capture bug 2617107 in easymock:
|
||||||
|
*
|
||||||
|
* http://sourceforge.net/tracker/?func=detail&aid=2617107&group_id=82958&atid=567837
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
class MyLocationCaptue extends Capture<Location> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setValue(Location value) {
|
||||||
|
if (!hasCaptured()) {
|
||||||
|
super.setValue(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user