|
| 1 | +package com.mapbox.navigation.examples.voicefeedback |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.os.Bundle |
| 5 | +import android.widget.Toast |
| 6 | +import androidx.activity.result.contract.ActivityResultContracts |
| 7 | +import androidx.appcompat.app.AppCompatActivity |
| 8 | +import com.mapbox.maps.MapboxMap |
| 9 | +import com.mapbox.maps.plugin.locationcomponent.location |
| 10 | +import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 11 | +import com.mapbox.navigation.base.options.NavigationOptions |
| 12 | +import com.mapbox.navigation.core.MapboxNavigation |
| 13 | +import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp |
| 14 | +import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver |
| 15 | +import com.mapbox.navigation.core.lifecycle.requireMapboxNavigation |
| 16 | +import com.mapbox.navigation.examples.core.databinding.LayoutActivityVoiceFeedbackBinding |
| 17 | +import com.mapbox.navigation.examples.util.Utils |
| 18 | +import com.mapbox.navigation.ui.base.installer.installComponents |
| 19 | +import com.mapbox.navigation.ui.maps.location.NavigationLocationProvider |
| 20 | +import com.mapbox.navigation.ui.voicefeedback.voiceFeedbackButton |
| 21 | + |
| 22 | +/** |
| 23 | + * Example activity demonstrating the [libnavui-voicefeedback](https://docs.mapbox.com/android/navigation/ui/latest/libnavui-voicefeedback/overview/) |
| 24 | + * SDK. This SDK provides voice feedback capabilities, allowing users to report issues, |
| 25 | + * suggest improvements, or give feedback directly through voice while navigating. |
| 26 | + * |
| 27 | + * It utilizes [MapboxVoiceFeedbackButton] and [FeedbackAgentSession] (via the ComponentInstaller) |
| 28 | + * to manage the lifecycle and UI states of the voice interaction flow. |
| 29 | + */ |
| 30 | +@OptIn(ExperimentalPreviewMapboxNavigationAPI::class) |
| 31 | +class MapboxVoiceFeedbackActivity : AppCompatActivity() { |
| 32 | + |
| 33 | + private val navigationLocationProvider = NavigationLocationProvider() |
| 34 | + private lateinit var binding: LayoutActivityVoiceFeedbackBinding |
| 35 | + private lateinit var mapboxMap: MapboxMap |
| 36 | + private val mapboxNavigation: MapboxNavigation by requireMapboxNavigation( |
| 37 | + onResumedObserver = object : MapboxNavigationObserver { |
| 38 | + override fun onAttached(mapboxNavigation: MapboxNavigation) { |
| 39 | + } |
| 40 | + |
| 41 | + override fun onDetached(mapboxNavigation: MapboxNavigation) { |
| 42 | + } |
| 43 | + }, |
| 44 | + onInitialize = this::initNavigation |
| 45 | + ) |
| 46 | + |
| 47 | + private val requestPermissionLauncher = registerForActivityResult( |
| 48 | + ActivityResultContracts.RequestMultiplePermissions() |
| 49 | + ) { permissions -> |
| 50 | + val areGranted = permissions.values.all { it } |
| 51 | + if (areGranted) { |
| 52 | + mapboxNavigation.startTripSession() |
| 53 | + } else { |
| 54 | + Toast.makeText( |
| 55 | + this, |
| 56 | + "Location and Microphone permissions are required", |
| 57 | + Toast.LENGTH_LONG |
| 58 | + ).show() |
| 59 | + finish() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 64 | + super.onCreate(savedInstanceState) |
| 65 | + binding = LayoutActivityVoiceFeedbackBinding.inflate(layoutInflater) |
| 66 | + setContentView(binding.root) |
| 67 | + |
| 68 | + mapboxMap = binding.mapView.getMapboxMap() |
| 69 | + |
| 70 | + requestPermissionLauncher.launch( |
| 71 | + arrayOf( |
| 72 | + Manifest.permission.ACCESS_FINE_LOCATION, |
| 73 | + Manifest.permission.RECORD_AUDIO |
| 74 | + ) |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + private fun initNavigation() { |
| 79 | + if (!MapboxNavigationApp.isSetup()) { |
| 80 | + MapboxNavigationApp.setup( |
| 81 | + NavigationOptions.Builder(this) |
| 82 | + .accessToken(Utils.getMapboxAccessToken(this)) |
| 83 | + .build() |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + MapboxNavigationApp.installComponents(this) { |
| 88 | + voiceFeedbackButton(binding.voiceFeedbackButton) |
| 89 | + } |
| 90 | + |
| 91 | + binding.mapView.location.apply { |
| 92 | + enabled = true |
| 93 | + setLocationProvider(navigationLocationProvider) |
| 94 | + } |
| 95 | + |
| 96 | + mapboxMap.setCamera( |
| 97 | + com.mapbox.maps.CameraOptions.Builder() |
| 98 | + .center(com.mapbox.geojson.Point.fromLngLat(-73.98513, 40.748817)) // New York City |
| 99 | + .zoom(13.0) |
| 100 | + .build() |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
0 commit comments