diff --git a/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/MapboxMetricsReporter.kt b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/MapboxMetricsReporter.kt index 6e5dbb857f0..e2ef33a7934 100644 --- a/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/MapboxMetricsReporter.kt +++ b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/MapboxMetricsReporter.kt @@ -17,6 +17,8 @@ import com.mapbox.navigation.base.internal.metric.extractEventsNames import com.mapbox.navigation.base.metrics.MetricEvent import com.mapbox.navigation.base.metrics.MetricsObserver import com.mapbox.navigation.base.metrics.MetricsReporter +import com.mapbox.navigation.metrics.events.EventsServiceInterfacesManager +import com.mapbox.navigation.metrics.events.TelemetryEventsProvider import com.mapbox.navigation.metrics.internal.EventsServiceProvider import com.mapbox.navigation.metrics.internal.TelemetryServiceProvider import com.mapbox.navigation.metrics.internal.TelemetryUtilsDelegate @@ -34,7 +36,7 @@ object MapboxMetricsReporter : MetricsReporter { private const val LOG_CATEGORY = "MapboxMetricsReporter" private val gson = Gson() - private lateinit var eventsService: EventsServiceInterface + private lateinit var eventsManager: EventsServiceInterfacesManager private lateinit var telemetryService: TelemetryService @Volatile @@ -84,10 +86,10 @@ object MapboxMetricsReporter : MetricsReporter { userAgent: String ) { isTelemetryInitialized = true + eventsManager = TelemetryEventsProvider.getOrCreateTelemetryEventsManager(accessToken) val eventsServerOptions = EventsServerOptions(accessToken, userAgent, null) - eventsService = EventsServiceProvider.provideEventsService(eventsServerOptions) telemetryService = TelemetryServiceProvider.provideTelemetryService(eventsServerOptions) - eventsService.registerObserver(eventsServiceObserver) + eventsManager.nativeEventsServiceInterface.registerObserver(eventsServiceObserver) } /** @@ -109,7 +111,7 @@ object MapboxMetricsReporter : MetricsReporter { fun disable() { isTelemetryInitialized = false removeObserver() - eventsService.unregisterObserver(eventsServiceObserver) + eventsManager.nativeEventsServiceInterface.unregisterObserver(eventsServiceObserver) ioJobController.job.cancelChildren() } @@ -125,7 +127,7 @@ object MapboxMetricsReporter : MetricsReporter { ) return } - eventsService.sendEvent( + eventsManager.nativeEventsServiceInterface.sendEvent( Event(eventsPriority, metricEvent.toValue(), null) ) { if (it != null) { @@ -144,7 +146,7 @@ object MapboxMetricsReporter : MetricsReporter { */ override fun sendTurnstileEvent(turnstileEvent: TurnstileEvent) { ifTelemetryIsRunning { - eventsService.sendTurnstileEvent(turnstileEvent) { + eventsManager.nativeEventsServiceInterface.sendTurnstileEvent(turnstileEvent) { if (it != null) { logE("Failed to send Turnstile event: $it", LOG_CATEGORY) } @@ -170,14 +172,14 @@ object MapboxMetricsReporter : MetricsReporter { * Register [EventsServiceObserver] */ fun registerEventsServiceObserver(observer: EventsServiceObserver) { - eventsService.registerObserver(observer) + eventsManager.nativeEventsServiceInterface.registerObserver(observer) } /** * Unregister [EventsServiceObserver] */ fun unregisterEventsServiceObserver(observer: EventsServiceObserver) { - eventsService.unregisterObserver(observer) + eventsManager.nativeEventsServiceInterface.unregisterObserver(observer) } private inline fun ifTelemetryIsRunning(func: () -> Unit) { diff --git a/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/EventsObserver.kt b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/EventsObserver.kt new file mode 100644 index 00000000000..f006de47cb8 --- /dev/null +++ b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/EventsObserver.kt @@ -0,0 +1,7 @@ +package com.mapbox.navigation.metrics.events + +import com.mapbox.bindgen.Value + +fun interface EventsObserver { + fun onEvents(events: Value) +} diff --git a/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/EventsServiceInterfacesManager.kt b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/EventsServiceInterfacesManager.kt new file mode 100644 index 00000000000..32bfec64033 --- /dev/null +++ b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/EventsServiceInterfacesManager.kt @@ -0,0 +1,52 @@ +package com.mapbox.navigation.metrics.events + +import com.mapbox.bindgen.Value +import com.mapbox.common.EventsServiceError +import com.mapbox.common.EventsServiceInterface +import com.mapbox.common.EventsServiceObserver + +class EventsServiceInterfacesManager( +// platformEventsServiceInterface: EventsServiceInterface, + internal val nativeEventsServiceInterface: EventsServiceInterface, +) { + private val eventServices = listOf( +// platformEventsServiceInterface, + nativeEventsServiceInterface, + ) + + private val observers = mutableListOf() + + private val eventsServiceObserver = object : EventsServiceObserver { + override fun didEncounterError(error: EventsServiceError, events: Value) { + // do nothing + } + + override fun didSendEvents(events: Value) { + observers.forEach { it.onEvents(events) } + } + + } + + fun registerEventsObserver(eventsObserver: EventsObserver) { + checkAndRegisterInternal() + observers.add(eventsObserver) + } + + fun unregisterEventsObserver(eventsObserver: EventsObserver) { + observers.remove(eventsObserver) + checkAndUnregisterInternal() + } + + private fun checkAndRegisterInternal() { + if (observers.isEmpty()) { + eventServices.forEach { it.registerObserver(eventsServiceObserver) } + } + } + + private fun checkAndUnregisterInternal() { + if (observers.isEmpty()) { + eventServices.forEach { it.unregisterObserver(eventsServiceObserver) } + } + } +} + diff --git a/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/TelemetryEventsProvider.kt b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/TelemetryEventsProvider.kt new file mode 100644 index 00000000000..b9645feb86d --- /dev/null +++ b/libnavigation-metrics/src/main/java/com/mapbox/navigation/metrics/events/TelemetryEventsProvider.kt @@ -0,0 +1,13 @@ +package com.mapbox.navigation.metrics.events + +import com.mapbox.common.EventsServerOptions +import com.mapbox.common.EventsService + +object TelemetryEventsProvider { + fun getOrCreateTelemetryEventsManager(accessToken: String): EventsServiceInterfacesManager = + EventsServiceInterfacesManager( + EventsService.getOrCreate( + EventsServerOptions(accessToken, "MapboxNavigationNative", null) + ) + ) +}