Mapbox control class
This commit is contained in:
parent
7636428d77
commit
385234bfd9
2 changed files with 74 additions and 40 deletions
|
@ -31,65 +31,30 @@ class MainActivity : AppCompatActivity() {
|
|||
lateinit var garmin: Garmin
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
private val mapboxObserver = MapboxObserver()
|
||||
|
||||
init {
|
||||
lifecycle.addObserver(object : DefaultLifecycleObserver {
|
||||
override fun onResume(owner: LifecycleOwner) {
|
||||
MapboxNavigationApp.attach(owner)
|
||||
MapboxNavigationApp.registerObserver(mapboxObserver)
|
||||
}
|
||||
|
||||
override fun onPause(owner: LifecycleOwner) {
|
||||
MapboxNavigationApp.detach(owner)
|
||||
MapboxNavigationApp.unregisterObserver(mapboxObserver)
|
||||
}
|
||||
})
|
||||
}
|
||||
private val mapboxToolbox = MapboxToolbox(lifecycle, this)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
if (!MapboxNavigationApp.isSetup()) {
|
||||
MapboxNavigationApp.setup {
|
||||
NavigationOptions.Builder(applicationContext)
|
||||
.accessToken(BuildConfig.MAPBOX_DOWNLOADS_TOKEN)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
MapboxNavigationApp.current()?.startTripSession()
|
||||
mapboxToolbox.onCreate()
|
||||
bluetoothInit()
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
||||
mapboxToolbox.onStart()
|
||||
}
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
|
||||
mapboxToolbox.onStop()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
MapboxNavigationApp.current()?.stopTripSession()
|
||||
maneuverApi.cancel()
|
||||
mapboxToolbox.onDestroy()
|
||||
}
|
||||
|
||||
|
||||
// Define distance formatter options
|
||||
private val distanceFormatter: DistanceFormatterOptions by lazy {
|
||||
DistanceFormatterOptions.Builder(this).build()
|
||||
}
|
||||
// Create an instance of the Maneuver API
|
||||
private val maneuverApi: MapboxManeuverApi by lazy {
|
||||
MapboxManeuverApi(MapboxDistanceFormatter(distanceFormatter))
|
||||
}
|
||||
|
||||
private val routeProgressObserver =
|
||||
RouteProgressObserver { routeProgress -> maneuverApi.getManeuvers(routeProgress).value?.apply { garmin.process(this[0]) } }
|
||||
|
||||
private fun bluetoothInit() {
|
||||
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
|
||||
val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
|
||||
|
|
69
app/src/main/java/eu/ztsh/garmin/MapboxToolbox.kt
Normal file
69
app/src/main/java/eu/ztsh/garmin/MapboxToolbox.kt
Normal file
|
@ -0,0 +1,69 @@
|
|||
package eu.ztsh.garmin
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.mapbox.navigation.base.formatter.DistanceFormatterOptions
|
||||
import com.mapbox.navigation.base.options.NavigationOptions
|
||||
import com.mapbox.navigation.core.formatter.MapboxDistanceFormatter
|
||||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
|
||||
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
|
||||
import com.mapbox.navigation.ui.maneuver.api.MapboxManeuverApi
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
class MapboxToolbox(lifecycle: Lifecycle, private val context: MainActivity) {
|
||||
|
||||
private val mapboxObserver = MapboxObserver()
|
||||
|
||||
init {
|
||||
lifecycle.addObserver(object : DefaultLifecycleObserver {
|
||||
override fun onResume(owner: LifecycleOwner) {
|
||||
MapboxNavigationApp.attach(owner)
|
||||
MapboxNavigationApp.registerObserver(mapboxObserver)
|
||||
}
|
||||
|
||||
override fun onPause(owner: LifecycleOwner) {
|
||||
MapboxNavigationApp.detach(owner)
|
||||
MapboxNavigationApp.unregisterObserver(mapboxObserver)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun onCreate() {
|
||||
if (!MapboxNavigationApp.isSetup()) {
|
||||
MapboxNavigationApp.setup {
|
||||
NavigationOptions.Builder(context)
|
||||
.accessToken(BuildConfig.MAPBOX_DOWNLOADS_TOKEN)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
MapboxNavigationApp.current()?.startTripSession()
|
||||
}
|
||||
|
||||
fun onStart() {
|
||||
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
||||
}
|
||||
|
||||
fun onStop() {
|
||||
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
|
||||
}
|
||||
|
||||
fun onDestroy() {
|
||||
MapboxNavigationApp.current()?.stopTripSession()
|
||||
maneuverApi.cancel()
|
||||
}
|
||||
|
||||
// Define distance formatter options
|
||||
private val distanceFormatter: DistanceFormatterOptions by lazy {
|
||||
DistanceFormatterOptions.Builder(context).build()
|
||||
}
|
||||
// Create an instance of the Maneuver API
|
||||
private val maneuverApi: MapboxManeuverApi by lazy {
|
||||
MapboxManeuverApi(MapboxDistanceFormatter(distanceFormatter))
|
||||
}
|
||||
|
||||
private val routeProgressObserver =
|
||||
RouteProgressObserver { routeProgress -> maneuverApi.getManeuvers(routeProgress).value?.apply { context.garmin.process(this[0]) } }
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue