diff --git a/app/src/main/java/eu/ztsh/garmin/Garmin.kt b/app/src/main/java/eu/ztsh/garmin/Garmin.kt index dc70096..aaf8f0c 100644 --- a/app/src/main/java/eu/ztsh/garmin/Garmin.kt +++ b/app/src/main/java/eu/ztsh/garmin/Garmin.kt @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothSocket import android.util.Log +import com.mapbox.navigation.core.trip.session.LocationMatcherResult import com.mapbox.navigation.tripdata.maneuver.model.Maneuver import com.mapbox.navigation.tripdata.progress.model.TripProgressUpdateValue import eu.ztsh.garmin.data.Arrows @@ -27,10 +28,10 @@ class Garmin( private lateinit var connection: ConnectThread private lateinit var maneuvers: ManeuverProcessingThread private lateinit var trips: TripProgressProcessingThread + private lateinit var locations: LocationMatcherProcessingThread private val cache = DataCache() - private val maneuversPool = Executors.newFixedThreadPool(4) - private val tripPool = Executors.newFixedThreadPool(4) + private val processingPool = Executors.newFixedThreadPool(8) fun start() { connection = ConnectThread() @@ -41,6 +42,9 @@ class Garmin( trips = TripProgressProcessingThread() trips.start() + + locations = LocationMatcherProcessingThread() + locations.start() } fun close() { @@ -52,16 +56,38 @@ class Garmin( trips.interrupt() trips.join(0) - maneuversPool.shutdown() - tripPool.shutdown() + locations.interrupt() + locations.join(0) + + processingPool.shutdown() } fun process(maneuver: Maneuver) { - maneuversPool.submit{maneuvers.enqueue(maneuver)} + processingPool.submit{maneuvers.enqueue(maneuver)} } fun process(tripProgressUpdateValue: TripProgressUpdateValue) { - maneuversPool.submit{trips.enqueue(tripProgressUpdateValue)} + processingPool.submit{trips.enqueue(tripProgressUpdateValue)} + } + + fun process(locationMatcherResult: LocationMatcherResult) { + processingPool.submit{locations.enqueue(locationMatcherResult)} + } + + private inner class LocationMatcherProcessingThread: ProcessingThread() { + + override fun mapAndSend(maybeItem: LocationMatcherResult?): LocationMatcherResult? { + if (maybeItem != null && cache.hasChanged(maybeItem)) { + send(GarminMapper.map(MapboxMapper.asSpeed(maybeItem))) + return maybeItem + } + return null + } + + override fun updateCache(item: LocationMatcherResult) { + cache.update(item) + } + } private inner class TripProgressProcessingThread : ProcessingThread() { diff --git a/app/src/main/java/eu/ztsh/garmin/data/DataCache.kt b/app/src/main/java/eu/ztsh/garmin/data/DataCache.kt index 61bedfb..3e61688 100644 --- a/app/src/main/java/eu/ztsh/garmin/data/DataCache.kt +++ b/app/src/main/java/eu/ztsh/garmin/data/DataCache.kt @@ -42,7 +42,9 @@ class DataCache { // location fun hasChanged(locationMatcherResult: LocationMatcherResult): Boolean { - return locationCache == null || locationCache!! != locationMatcherResult + return locationCache.let { it == null + || it.enhancedLocation.speed != locationMatcherResult.enhancedLocation.speed + || it.speedLimitInfo.speed != locationMatcherResult.speedLimitInfo.speed } } fun update(locationMatcherResult: LocationMatcherResult) { diff --git a/app/src/main/java/eu/ztsh/garmin/mapbox/LocationObserver.kt b/app/src/main/java/eu/ztsh/garmin/mapbox/LocationObserver.kt index 998eebe..8dfefde 100644 --- a/app/src/main/java/eu/ztsh/garmin/mapbox/LocationObserver.kt +++ b/app/src/main/java/eu/ztsh/garmin/mapbox/LocationObserver.kt @@ -4,6 +4,7 @@ import com.mapbox.common.location.Location import com.mapbox.navigation.core.trip.session.LocationMatcherResult import com.mapbox.navigation.core.trip.session.LocationObserver import com.mapbox.navigation.ui.maps.camera.transition.NavigationCameraTransitionOptions +import eu.ztsh.garmin.Garmin class LocationObserver(private val mapControl: MapControl) : LocationObserver { @@ -31,6 +32,8 @@ class LocationObserver(private val mapControl: MapControl) : LocationObserver { mapControl.viewportDataSource.onLocationChanged(enhancedLocation) mapControl.viewportDataSource.evaluate() + Garmin.instance.process(locationMatcherResult) + // if this is the first location update the activity has received, // it's best to immediately move the camera to the current user location if (!firstLocationUpdateReceived) {