Skip to content

Commit e0beaea

Browse files
committed
Demonstrate the appropriate time to call setAudioGuidance.
Calling it before navigation has begun will not have an impact when using a mode that isn't mute/unmute.
1 parent 609973f commit e0beaea

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

navigation-sample/app/src/main/java/com/example/navigationapidemo/NavFragmentActivity.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class NavFragmentActivity : AppCompatActivity() {
4949
private var pendingNavActions = mutableListOf<InitializedNavRunnable>()
5050
private var arrivalListener: Navigator.ArrivalListener? = null
5151
private var routeChangedListener: Navigator.RouteChangedListener? = null
52+
private var navigationSessionListener: Navigator.NavigationSessionListener? = null
5253

5354
private lateinit var navFragment: SupportNavigationFragment
5455
private var navInfoDisplayFragment: Fragment? = null
@@ -187,6 +188,14 @@ class NavFragmentActivity : AppCompatActivity() {
187188
showToast("onRouteChanged: the driver's route changed")
188189
}
189190
navigator.addRouteChangedListener(routeChangedListener)
191+
192+
navigationSessionListener =
193+
Navigator.NavigationSessionListener {
194+
// Enable voice audio guidance (through the device speaker)
195+
navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
196+
}
197+
198+
navigator.addNavigationSessionListener(navigationSessionListener)
190199
}
191200
}
192201

@@ -221,9 +230,6 @@ class NavFragmentActivity : AppCompatActivity() {
221230
// Hide the toolbar to maximize the navigation UI
222231
actionBar?.hide()
223232

224-
// Enable voice audio guidance (through the device speaker)
225-
navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
226-
227233
// Simulate vehicle progress along the route (for demo/debug builds)
228234
if (BuildConfig.DEBUG) {
229235
navigator.simulator.simulateLocationsAlongExistingRoute(
@@ -357,6 +363,10 @@ class NavFragmentActivity : AppCompatActivity() {
357363
navigator.removeRouteChangedListener(routeChangedListener)
358364
}
359365

366+
if (navigationSessionListener != null) {
367+
navigator.removeNavigationSessionListener(navigationSessionListener)
368+
}
369+
360370
navigator.simulator.unsetUserLocation()
361371
navigator.cleanup()
362372
}

navigation-sample/app/src/main/java/com/example/navigationapidemo/NavViewActivity.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class NavViewActivity : AppCompatActivity() {
5353
var pendingNavActions = mutableListOf<InitializedNavRunnable>()
5454
private var arrivalListener: Navigator.ArrivalListener? = null
5555
private var routeChangedListener: Navigator.RouteChangedListener? = null
56+
private var navigationSessionListener: Navigator.NavigationSessionListener? = null
5657

5758
// Only used to demo the turn-by-turn nav forwarding feature.
5859
var navInfoDisplayFragment: Fragment? = null
@@ -192,6 +193,14 @@ class NavViewActivity : AppCompatActivity() {
192193
showToast("onRouteChanged: the driver's route changed")
193194
}
194195
navigator.addRouteChangedListener(routeChangedListener)
196+
197+
navigationSessionListener =
198+
Navigator.NavigationSessionListener {
199+
// Enable voice audio guidance (through the device speaker)
200+
navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
201+
}
202+
203+
navigator.addNavigationSessionListener(navigationSessionListener)
195204
}
196205
}
197206

@@ -226,9 +235,6 @@ class NavViewActivity : AppCompatActivity() {
226235
// Hide the toolbar to maximize the navigation UI
227236
actionBar?.hide()
228237

229-
// Enable voice audio guidance (through the device speaker)
230-
navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
231-
232238
// Simulate vehicle progress along the route (for demo/debug builds)
233239
if (BuildConfig.DEBUG) {
234240
navigator.simulator.simulateLocationsAlongExistingRoute(
@@ -297,6 +303,10 @@ class NavViewActivity : AppCompatActivity() {
297303
navigator.removeRouteChangedListener(routeChangedListener)
298304
}
299305

306+
if (navigationSessionListener != null) {
307+
navigator.removeNavigationSessionListener(navigationSessionListener)
308+
}
309+
300310
navigator.simulator?.unsetUserLocation()
301311
navigator.cleanup()
302312
}

navigation-sample/app/src/main/java/com/example/navigationapidemo/SwappingMapAndNavActivity.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
5656
private lateinit var mapFragment: SupportMapFragment
5757
private lateinit var navigationFragment: SupportNavigationFragment
5858
private var arrivalListener: Navigator.ArrivalListener? = null
59+
private var navigationSessionListener: Navigator.NavigationSessionListener? = null
5960

6061
override fun onCreate(savedInstanceState: Bundle?) {
6162
super.onCreate(savedInstanceState)
@@ -148,9 +149,6 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
148149
// Hide the toolbar to maximize the navigation UI
149150
actionBar?.hide()
150151

151-
// Enable voice audio guidance (through the device speaker)
152-
navigator?.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
153-
154152
// Simulate vehicle progress along the route (for demo/debug builds)
155153
if (BuildConfig.DEBUG) {
156154
navigator
@@ -197,7 +195,7 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
197195
}
198196
}
199197

200-
private fun registerArrivalListener() {
198+
private fun registerListeners() {
201199
arrivalListener =
202200
Navigator.ArrivalListener {
203201
showToast("User has arrived at the destination!")
@@ -212,6 +210,14 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
212210
stopTripAndShowMapFragment(/* unused= */ null)
213211
}
214212
navigator?.addArrivalListener(arrivalListener)
213+
214+
navigationSessionListener =
215+
Navigator.NavigationSessionListener {
216+
// Enable voice audio guidance (through the device speaker)
217+
navigator?.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
218+
}
219+
220+
navigator?.addNavigationSessionListener(navigationSessionListener)
215221
}
216222

217223
// Detaches old fragment and adds a new fragment to the activity if it's not added otherwise
@@ -240,6 +246,10 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
240246
if (arrivalListener != null) {
241247
navigator?.removeArrivalListener(arrivalListener)
242248
}
249+
if (navigationSessionListener != null) {
250+
navigator?.removeNavigationSessionListener(navigationSessionListener)
251+
}
252+
243253
navigator?.simulator?.unsetUserLocation()
244254
navigator?.cleanup()
245255
super.onDestroy()
@@ -267,7 +277,7 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
267277
if (swappingMapAndNavActivity != null) {
268278
swappingMapAndNavActivity.navigator = navigator
269279
// Register an arrival listener that returns back to a top-down map once the trip is over.
270-
swappingMapAndNavActivity.registerArrivalListener()
280+
swappingMapAndNavActivity.registerListeners()
271281
}
272282
}
273283

0 commit comments

Comments
 (0)