From 8cc43ea5957cc924d6296a60deff599a2675abeb Mon Sep 17 00:00:00 2001 From: Chuck Greb Date: Tue, 30 Sep 2014 13:46:46 -0400 Subject: [PATCH 1/3] Adds speed to mock locations broadcast from a GPX trace --- .../com/mapzen/android/lost/LocationClient.java | 11 +++++++++-- .../mapzen/android/lost/LocationClientTest.java | 16 ++++++++++++++++ lost/src/test/resources/lost.gpx | 3 +++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lost/src/main/java/com/mapzen/android/lost/LocationClient.java b/lost/src/main/java/com/mapzen/android/lost/LocationClient.java index 77f3ec1..424b8a1 100644 --- a/lost/src/main/java/com/mapzen/android/lost/LocationClient.java +++ b/lost/src/main/java/com/mapzen/android/lost/LocationClient.java @@ -33,6 +33,7 @@ public class LocationClient { // GPX tags public static final String TAG_TRACK_POINT = "trkpt"; + public static final String TAG_SPEED = "speed"; public static final String TAG_LAT = "lat"; public static final String TAG_LNG = "lon"; @@ -269,13 +270,17 @@ public void run() { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final XPath xPath = XPathFactory.newInstance().newXPath(); final String expression = "//" + TAG_TRACK_POINT; + final String speedExpression = "//" + TAG_SPEED; NodeList nodeList = null; + NodeList speedList = null; try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file); nodeList = (NodeList) xPath.compile(expression) .evaluate(document, XPathConstants.NODESET); + speedList = (NodeList) xPath.compile(speedExpression) + .evaluate(document, XPathConstants.NODESET); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { @@ -286,12 +291,12 @@ public void run() { e.printStackTrace(); } - parse(nodeList); + parse(nodeList, speedList); } }).start(); } - private void parse(NodeList nodeList) { + private void parse(NodeList nodeList, NodeList speedList) { if (nodeList != null) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); @@ -302,6 +307,8 @@ private void parse(NodeList nodeList) { location.setLatitude(Double.parseDouble(lat)); location.setLongitude(Double.parseDouble(lng)); location.setTime(System.currentTimeMillis()); + location.setSpeed(Float.parseFloat(speedList.item(i).getFirstChild() + .getNodeValue())); new Handler(context.getMainLooper()).post(new Runnable() { @Override diff --git a/lost/src/test/java/com/mapzen/android/lost/LocationClientTest.java b/lost/src/test/java/com/mapzen/android/lost/LocationClientTest.java index 2085762..4aa4baf 100644 --- a/lost/src/test/java/com/mapzen/android/lost/LocationClientTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/LocationClientTest.java @@ -394,6 +394,22 @@ public void setMockTrace_shouldInvokeListenerForEachLocation() throws Exception assertThat(listener.getAllLocations().get(2).getLongitude()).isEqualTo(2.1); } + @Test + public void setMockTrace_shouldBroadcastSpeedWithLocation() throws Exception { + loadTestGpxTrace(); + locationClient.setMockMode(true); + TestLocationListener listener = new TestLocationListener(); + LocationRequest request = LocationRequest.create(); + request.setFastestInterval(0); + locationClient.requestLocationUpdates(request, listener); + locationClient.setMockTrace("lost.gpx"); + Thread.sleep(1000); + Robolectric.runUiThreadTasks(); + assertThat(listener.getAllLocations().get(0).getSpeed()).isEqualTo(10f); + assertThat(listener.getAllLocations().get(1).getSpeed()).isEqualTo(20f); + assertThat(listener.getAllLocations().get(2).getSpeed()).isEqualTo(30f); + } + @Test public void setMockTrace_shouldRespectFastestInterval() throws Exception { loadTestGpxTrace(); diff --git a/lost/src/test/resources/lost.gpx b/lost/src/test/resources/lost.gpx index 03fcdf5..361fe4d 100644 --- a/lost/src/test/resources/lost.gpx +++ b/lost/src/test/resources/lost.gpx @@ -8,14 +8,17 @@ -0.0 + 10.0 1.0 + 20.0 2.0 + 30.0 From 2781183a81ec7a2228a364348166d5dd9c91e611 Mon Sep 17 00:00:00 2001 From: Chuck Greb Date: Tue, 30 Sep 2014 13:57:02 -0400 Subject: [PATCH 2/3] Updates sample app to display speed --- lost-sample/res/layout/list_item.xml | 5 +++++ lost-sample/src/main/java/com/example/lost/LostActivity.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lost-sample/res/layout/list_item.xml b/lost-sample/res/layout/list_item.xml index c1673b2..6e02a7e 100644 --- a/lost-sample/res/layout/list_item.xml +++ b/lost-sample/res/layout/list_item.xml @@ -22,6 +22,11 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> + + Date: Tue, 30 Sep 2014 15:41:46 -0400 Subject: [PATCH 3/3] Makes a subnode of tag. --- lost/src/test/resources/lost.gpx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lost/src/test/resources/lost.gpx b/lost/src/test/resources/lost.gpx index 361fe4d..1cc0f00 100644 --- a/lost/src/test/resources/lost.gpx +++ b/lost/src/test/resources/lost.gpx @@ -7,18 +7,18 @@ -0.0 + 10.0 - 10.0 1.0 + 20.0 - 20.0 2.0 + 30.0 - 30.0