Skip to content

Commit

Permalink
Explore extenstion ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
kmadsen committed Aug 26, 2022
1 parent 551f9b1 commit 0863b04
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 94 deletions.
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.8.0-alpha.2"
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 All @@ -28,6 +29,9 @@ class MainActivity : AppCompatActivity(), PermissionsListener {
return
}

// Each example needs to call MapboxNavigationApp.setup.
MapboxNavigationApp.disable()

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
}
}

val navigationResumedObserver = object : MapboxNavigationObserver {
private val navigationResumedObserver = object : MapboxNavigationObserver {
override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerLocationObserver(locationObserver)
mapboxNavigation.registerRouteProgressObserver(replayProgressObserver)
Expand All @@ -274,7 +274,7 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {

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

lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
MapboxNavigationApp.registerObserver(navigationCreatedObserver)
}

override fun onResume(owner: LifecycleOwner) {
MapboxNavigationApp.registerObserver(navigationResumedObserver)
}

override fun onPause(owner: LifecycleOwner) {
MapboxNavigationApp.unregisterObserver(navigationResumedObserver)
}

override fun onDestroy(owner: LifecycleOwner) {
MapboxNavigationApp.unregisterObserver(navigationCreatedObserver)
}
})
}.attach(this)
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -322,11 +304,23 @@ class ShowAlternativeRoutesActivity : AppCompatActivity() {
findRoute(originPoint, destinationPoint)
}

MapboxNavigationApp.registerObserver(navigationCreatedObserver)
replayOriginLocation()
}

override fun onResume() {
super.onResume()
MapboxNavigationApp.registerObserver(navigationResumedObserver)
}

override fun onPause() {
super.onPause()
MapboxNavigationApp.unregisterObserver(navigationResumedObserver)
}

override fun onDestroy() {
super.onDestroy()
MapboxNavigationApp.unregisterObserver(navigationCreatedObserver)
routeLineApi.cancel()
routeLineView.cancel()
mapboxReplayer.finish()
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

0 comments on commit 0863b04

Please sign in to comment.