Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration from MapboxNavigationProvider to MapboxNavigationApp #129

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ android {

dependencies {
// Mapbox Navigation SDK
implementation "com.mapbox.navigation:android:2.5.1"
implementation "com.mapbox.navigation:android:2.8.0-alpha.3"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31"
implementation "androidx.core:core-ktx:1.7.0"
implementation "com.google.android.material:material:1.5.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.mapbox.android.core.permissions.PermissionsListener
import com.mapbox.android.core.permissions.PermissionsManager
import com.mapbox.android.core.permissions.PermissionsManager.areLocationPermissionsGranted
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.examples.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), PermissionsListener {
Expand Down Expand Up @@ -40,6 +41,12 @@ class MainActivity : AppCompatActivity(), PermissionsListener {
bindExamples()
}

override fun onStart() {
super.onStart()
// Each example is responsible for setting up their NavigationOptions.
MapboxNavigationApp.disable()
}

override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) {
Toast.makeText(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import com.mapbox.navigation.base.route.RouterFailure
import com.mapbox.navigation.base.route.RouterOrigin
import com.mapbox.navigation.base.trip.model.RouteProgress
import com.mapbox.navigation.core.MapboxNavigation
import com.mapbox.navigation.core.MapboxNavigationProvider
import com.mapbox.navigation.core.directions.session.RoutesObserver
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
import com.mapbox.navigation.core.replay.MapboxReplayer
import com.mapbox.navigation.core.replay.ReplayLocationEngine
import com.mapbox.navigation.core.replay.route.ReplayProgressObserver
Expand Down Expand Up @@ -240,13 +241,38 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
}

@SuppressLint("MissingPermission")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityShowAlternativeRoutesBinding.inflate(layoutInflater)
setContentView(binding.root)
val navigationCreatedObserver = object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
[email protected] = mapboxNavigation
mapboxNavigation.startTripSession()
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
// mapboxNavigation is invalid
}
}

private val navigationResumedObserver = object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerLocationObserver(locationObserver)
mapboxNavigation.registerRouteProgressObserver(replayProgressObserver)
mapboxNavigation.registerRoutesObserver(routesObserver)
mapboxNavigation.registerRouteAlternativesObserver(alternativesObserver)
binding.mapView.gestures.addOnMapClickListener(mapClickListener)
}

// initialize Mapbox Navigation
mapboxNavigation = MapboxNavigationProvider.create(
override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.unregisterRouteProgressObserver(replayProgressObserver)
mapboxNavigation.unregisterLocationObserver(locationObserver)
mapboxNavigation.unregisterRoutesObserver(routesObserver)
mapboxNavigation.unregisterRouteAlternativesObserver(alternativesObserver)
binding.mapView.gestures.removeOnMapClickListener(mapClickListener)
}
}

init {
// You can setup MapboxNavigation at any part of the app lifecycle.
MapboxNavigationApp.setup {
NavigationOptions.Builder(this)
.accessToken(getString(R.string.mapbox_access_token))
.locationEngine(ReplayLocationEngine(mapboxReplayer))
Expand All @@ -256,7 +282,13 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
.build()
)
.build()
)
}.attach(this)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityShowAlternativeRoutesBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.mapView.getMapboxMap().loadStyleUri(
NavigationStyles.NAVIGATION_DAY_STYLE
Expand All @@ -270,33 +302,26 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
findRoute(originPoint, destinationPoint)
}

binding.mapView.gestures.addOnMapClickListener(mapClickListener)
MapboxNavigationApp.registerObserver(navigationCreatedObserver)
replayOriginLocation()
mapboxNavigation.startTripSession()
}

override fun onStart() {
super.onStart()
mapboxNavigation.registerLocationObserver(locationObserver)
mapboxNavigation.registerRouteProgressObserver(replayProgressObserver)
mapboxNavigation.registerRoutesObserver(routesObserver)
mapboxNavigation.registerRouteAlternativesObserver(alternativesObserver)
override fun onResume() {
super.onResume()
MapboxNavigationApp.registerObserver(navigationResumedObserver)
}

override fun onStop() {
super.onStop()
mapboxNavigation.unregisterRouteProgressObserver(replayProgressObserver)
mapboxNavigation.unregisterLocationObserver(locationObserver)
mapboxNavigation.unregisterRoutesObserver(routesObserver)
mapboxNavigation.unregisterRouteAlternativesObserver(alternativesObserver)
override fun onPause() {
super.onPause()
MapboxNavigationApp.unregisterObserver(navigationResumedObserver)
}

override fun onDestroy() {
super.onDestroy()
MapboxNavigationApp.unregisterObserver(navigationCreatedObserver)
routeLineApi.cancel()
routeLineView.cancel()
mapboxReplayer.finish()
mapboxNavigation.onDestroy()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.location.Location
import android.os.Bundle
import android.view.View.GONE
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.bindgen.Expected
Expand All @@ -21,15 +23,18 @@ import com.mapbox.navigation.base.options.NavigationOptions
import com.mapbox.navigation.base.route.NavigationRoute
import com.mapbox.navigation.base.route.toNavigationRoute
import com.mapbox.navigation.core.MapboxNavigation
import com.mapbox.navigation.core.MapboxNavigationProvider
import com.mapbox.navigation.core.directions.session.RoutesObserver
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
import com.mapbox.navigation.core.lifecycle.NavigationOptionsProvider
import com.mapbox.navigation.core.replay.MapboxReplayer
import com.mapbox.navigation.core.replay.ReplayLocationEngine
import com.mapbox.navigation.core.replay.route.ReplayProgressObserver
import com.mapbox.navigation.core.replay.route.ReplayRouteMapper
import com.mapbox.navigation.core.trip.session.BannerInstructionsObserver
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.core.trip.session.LocationObserver
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
import com.mapbox.navigation.examples.R
import com.mapbox.navigation.examples.databinding.MapboxActivityShowJunctionsBinding
import com.mapbox.navigation.ui.base.util.MapboxNavigationConsumer
Expand Down Expand Up @@ -59,7 +64,6 @@ import kotlinx.coroutines.launch
class ShowJunctionsActivity : AppCompatActivity() {

private lateinit var mapboxMap: MapboxMap
private lateinit var mapboxNavigation: MapboxNavigation
private lateinit var binding: MapboxActivityShowJunctionsBinding
private lateinit var locationComponent: LocationComponentPlugin

Expand Down Expand Up @@ -139,28 +143,39 @@ class ShowJunctionsActivity : AppCompatActivity() {
junctionApi.generateJunction(bannerInstructions, junctionCallback)
}

private fun init() {
initNavigation()
initStyle()
@SuppressLint("MissingPermission")
private val sessionStarter = object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.startTripSession()
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.stopTripSession()
}
}

@SuppressLint("MissingPermission")
private fun initNavigation() {
mapboxNavigation = MapboxNavigationProvider.create(
NavigationOptions.Builder(this)
.accessToken(getString(R.string.mapbox_access_token))
.locationEngine(ReplayLocationEngine(mapboxReplayer))
.build()
)
mapboxNavigation.startTripSession()
init {
mapboxNavigationInstaller()
.onCreated(sessionStarter)
.onStarted(
mapboxLocationObserver(locationObserver),
mapboxRoutesObserver(routesObserver),
mapboxRouteProgressObserver(replayProgressObserver),
mapboxBannerInstructionsObserver(bannerInstructionsObserver),
)
.install {
NavigationOptions.Builder(this)
.accessToken(getString(R.string.mapbox_access_token))
.locationEngine(ReplayLocationEngine(mapboxReplayer))
.build()
}
}

@SuppressLint("MissingPermission")
private fun initStyle() {
private fun init() {
mapboxMap.loadStyleUri(Style.MAPBOX_STREETS) { style ->
routeLineView.initializeLayers(style)
binding.actionButton.setOnClickListener {
mapboxNavigation.setNavigationRoutes(listOf(route))
MapboxNavigationApp.current()?.setNavigationRoutes(listOf(route))
binding.actionButton.visibility = GONE
}
}
Expand Down Expand Up @@ -204,28 +219,114 @@ class ShowJunctionsActivity : AppCompatActivity() {
init()
}

override fun onStart() {
super.onStart()
mapboxNavigation.registerRoutesObserver(routesObserver)
mapboxNavigation.registerLocationObserver(locationObserver)
mapboxNavigation.registerRouteProgressObserver(replayProgressObserver)
mapboxNavigation.registerBannerInstructionsObserver(bannerInstructionsObserver)
}

override fun onStop() {
super.onStop()
mapboxNavigation.unregisterRoutesObserver(routesObserver)
mapboxNavigation.unregisterLocationObserver(locationObserver)
mapboxNavigation.unregisterRouteProgressObserver(replayProgressObserver)
mapboxNavigation.unregisterBannerInstructionsObserver(bannerInstructionsObserver)
}

override fun onDestroy() {
super.onDestroy()
routeLineApi.cancel()
routeLineView.cancel()
junctionApi.cancelAll()
mapboxReplayer.finish()
mapboxNavigation.onDestroy()
}
}

fun AppCompatActivity.mapboxNavigationInstaller() = MapboxNavigationActivityInstaller(this)

fun mapboxLocationObserver(locationObserver: LocationObserver): MapboxNavigationObserver {
return object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerLocationObserver(locationObserver)
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.unregisterLocationObserver(locationObserver)
}
}
}

fun mapboxRoutesObserver(routesObserver: RoutesObserver): MapboxNavigationObserver {
return object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerRoutesObserver(routesObserver)
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.unregisterRoutesObserver(routesObserver)
}
}
}

fun mapboxBannerInstructionsObserver(bannerInstructionsObserver: BannerInstructionsObserver): MapboxNavigationObserver {
return object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerBannerInstructionsObserver(bannerInstructionsObserver)
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.unregisterBannerInstructionsObserver(bannerInstructionsObserver)
}
}
}

fun mapboxRouteProgressObserver(routeProgressObserver: RouteProgressObserver): MapboxNavigationObserver {
return object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerRouteProgressObserver(routeProgressObserver)
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)
}
}
}

class MapboxNavigationActivityInstaller(
val activity: AppCompatActivity
) {
private val onCreated = mutableSetOf<MapboxNavigationObserver>()
private val onStarted = mutableSetOf<MapboxNavigationObserver>()
private val onResumed = mutableSetOf<MapboxNavigationObserver>()

fun onCreated(vararg observers: MapboxNavigationObserver) = apply {
onCreated.addAll(observers)
}

fun onStarted(vararg observers: MapboxNavigationObserver) = apply {
onStarted.addAll(observers)
}

fun onResumed(vararg observers: MapboxNavigationObserver) = apply {
onResumed.addAll(observers)
}

fun install(
navigationOptionsProvider: NavigationOptionsProvider
) {
MapboxNavigationApp.attach(activity)
activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
val navigationOptions = navigationOptionsProvider.createNavigationOptions()
MapboxNavigationApp.setup(navigationOptions)
onCreated.forEach { MapboxNavigationApp.registerObserver(it) }
}

override fun onStart(owner: LifecycleOwner) {
onStarted.forEach { MapboxNavigationApp.registerObserver(it) }
}

override fun onResume(owner: LifecycleOwner) {
onResumed.forEach { MapboxNavigationApp.registerObserver(it) }
}

override fun onPause(owner: LifecycleOwner) {
onResumed.reversed().forEach { MapboxNavigationApp.unregisterObserver(it) }
}

override fun onStop(owner: LifecycleOwner) {
onStarted.reversed().forEach { MapboxNavigationApp.unregisterObserver(it) }
}

override fun onDestroy(owner: LifecycleOwner) {
onCreated.reversed().forEach { MapboxNavigationApp.unregisterObserver(it) }
}
})
}
}
Loading