Skip to content

Commit

Permalink
Support date format with millis
Browse files Browse the repository at this point in the history
Handle empty optional tags
  • Loading branch information
dpa99c committed Feb 13, 2024
1 parent 70e1d93 commit f3720e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ An application for easy mocking of locations via a GPX file.
After installation set this app in device developer options to be the mock location app.

The app mocks locations via a track file in [GPX format](https://wiki.openstreetmap.org/wiki/GPX).
Each `<trkpt>` must have `lat` and `lon` attributes and a `<time>` element with the value in format `yyyy-MM-ddTHH:mm:ssZ`,
and optionally `<ele>` elements for elevation.

Each `<trkpt>` must have:
- `lat` and `lon` attributes
- a `<time>` element with the value in format `yyyy-MM-ddTHH:mm:ssZ` or `yyyy-MM-ddTHH:mm:ss.SSSZ`,

and optionally:
- `<ele>` elements for elevation. e.g. `<ele>105.32</ele>`
- `<speed>` elements for speed in m/s. e.g. `<speed>2.5</speed>`
- `<accuracy>` elements for accuracy in meters. e.g. `<accuracy>7.41</accuracy>`

An example GPX track file can be found here: [`mock_track.gpx`](/mock_track.gpx)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import androidx.fragment.app.DialogFragment
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.text.SimpleDateFormat
import java.util.Date

private const val TAG = "ParseGPX"

class ParseGPX {
val items = ArrayList<TrackingPoint>()
private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'")
private val dateFormatWithMillis = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'")

var previousPointTimeStamp: Long = 0

/* class ErrorDialogFragment(message: String) : DialogFragment() {
Expand Down Expand Up @@ -57,7 +60,7 @@ class ParseGPX {
currentRecord.lon = xpp.getAttributeValue(null, "lon").toDouble()
}
}
XmlPullParser.TEXT -> textValue = xpp.text
XmlPullParser.TEXT -> textValue = xpp.text.trim()
XmlPullParser.END_TAG -> {
if (inItem) {
when (tagName) {
Expand All @@ -66,11 +69,17 @@ class ParseGPX {
inItem = false
currentRecord = TrackingPoint()
}
"ele" -> currentRecord.altitude = textValue.toDouble()
"accuracy" -> currentRecord.accuracy = textValue.toFloat()
"speed" -> currentRecord.speed = textValue.toFloat()
"ele" -> currentRecord.altitude = if (textValue.isEmpty()) 0.0 else textValue.toDouble()
"accuracy" -> currentRecord.accuracy = if (textValue.isEmpty()) 0.0f else textValue.toFloat()
"speed" -> currentRecord.speed = if (textValue.isEmpty()) 0.0f else textValue.toFloat()
"time" -> {
val time = dateFormat.parse(textValue)
val time = if(textValue.isEmpty())
Date();
else if(textValue.length == 20)
dateFormat.parse(textValue)
else
dateFormatWithMillis.parse(textValue)

// Log.i(TAG, time.toString())
currentRecord.timestamp = time.time
if (items.size == 0) {
Expand Down

0 comments on commit f3720e4

Please sign in to comment.