diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java index a00df8f380f..a78569b54d5 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java @@ -168,13 +168,13 @@ public void onInstructionListVisibilityChanged(boolean shown) { } @Override - public SpeechAnnouncement willVoice(SpeechAnnouncement announcement) { - return SpeechAnnouncement.builder().announcement("All announcements will be the same.").build(); + public String willVoice(SpeechAnnouncement announcement) { + return "All announcements will be the same."; } @Override - public BannerInstructions willDisplay(BannerInstructions instructions) { - return instructions; + public String willDisplay(BannerInstructions instructions) { + return "All banners will be the same."; } private void startNavigation(DirectionsRoute directionsRoute) { diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java index b0b1dc07e1c..4f2f26e3217 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java @@ -5,6 +5,7 @@ import android.support.design.widget.BottomSheetBehavior.BottomSheetCallback; import android.view.View; +import com.mapbox.api.directions.v5.models.BannerComponents; import com.mapbox.api.directions.v5.models.BannerInstructions; import com.mapbox.api.directions.v5.models.DirectionsRoute; import com.mapbox.geojson.Point; @@ -12,14 +13,17 @@ import com.mapbox.services.android.navigation.ui.v5.listeners.BannerInstructionsListener; import com.mapbox.services.android.navigation.ui.v5.listeners.FeedbackListener; import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener; -import com.mapbox.services.android.navigation.ui.v5.listeners.SpeechAnnouncementListener; import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener; import com.mapbox.services.android.navigation.ui.v5.listeners.RouteListener; +import com.mapbox.services.android.navigation.ui.v5.listeners.SpeechAnnouncementListener; import com.mapbox.services.android.navigation.ui.v5.voice.SpeechAnnouncement; import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener; import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation; import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener; +import java.util.ArrayList; +import java.util.List; + /** * In charge of holding any {@link NavigationView} related listeners {@link NavigationListener}, * {@link RouteListener}, or {@link FeedbackListener} and firing listener events when @@ -27,6 +31,8 @@ */ class NavigationViewEventDispatcher { + private static final int ONE = 1; + private static final int FIRST = 0; private ProgressChangeListener progressChangeListener; private MilestoneEventListener milestoneEventListener; private FeedbackListener feedbackListener; @@ -167,14 +173,22 @@ void onInstructionListVisibilityChanged(boolean shown) { SpeechAnnouncement onAnnouncement(SpeechAnnouncement announcement) { if (speechAnnouncementListener != null) { - return speechAnnouncementListener.willVoice(announcement); + String textAnnouncement = speechAnnouncementListener.willVoice(announcement); + SpeechAnnouncement announcementToBeVoiced = SpeechAnnouncement.builder().announcement(textAnnouncement).build(); + return announcementToBeVoiced; } return announcement; } BannerInstructions onBannerDisplay(BannerInstructions instructions) { if (bannerInstructionsListener != null) { - return bannerInstructionsListener.willDisplay(instructions); + String textBanner = bannerInstructionsListener.willDisplay(instructions); + List bannerComponents = new ArrayList<>(ONE); + bannerComponents.add(instructions.primary().components().get(FIRST).toBuilder().text(textBanner) + .abbreviation(textBanner).build()); + BannerInstructions bannerToBeDisplayed = instructions.toBuilder().primary(instructions.primary().toBuilder() + .components(bannerComponents).build()).build(); + return bannerToBeDisplayed; } return instructions; } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/BannerInstructionsListener.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/BannerInstructionsListener.java index 66311ba7151..b8294b4e5aa 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/BannerInstructionsListener.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/BannerInstructionsListener.java @@ -19,8 +19,8 @@ public interface BannerInstructionsListener { * and it will be ignored. * * @param instructions about to be displayed - * @return instructions to be displayed; null if should be ignored - * @since 0.16.0 + * @return instruction text to be displayed; null if should be ignored + * @since 0.18.0 */ - BannerInstructions willDisplay(BannerInstructions instructions); + String willDisplay(BannerInstructions instructions); } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/SpeechAnnouncementListener.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/SpeechAnnouncementListener.java index 432b9278887..840ed4f90ce 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/SpeechAnnouncementListener.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/SpeechAnnouncementListener.java @@ -19,8 +19,8 @@ public interface SpeechAnnouncementListener { * and it will be ignored. * * @param announcement about to be announced - * @return announcement to be played; null if should be ignored - * @since 0.16.0 + * @return text announcement to be played; null if should be ignored + * @since 0.18.0 */ - SpeechAnnouncement willVoice(SpeechAnnouncement announcement); + String willVoice(SpeechAnnouncement announcement); } diff --git a/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcherTest.java b/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcherTest.java index f4cd7a9eb42..db4ecf725b8 100644 --- a/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcherTest.java +++ b/libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcherTest.java @@ -2,7 +2,9 @@ import android.support.annotation.NonNull; +import com.mapbox.api.directions.v5.models.BannerComponents; import com.mapbox.api.directions.v5.models.BannerInstructions; +import com.mapbox.api.directions.v5.models.BannerText; import com.mapbox.api.directions.v5.models.DirectionsRoute; import com.mapbox.geojson.Point; import com.mapbox.services.android.navigation.ui.v5.feedback.FeedbackItem; @@ -19,8 +21,13 @@ import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -362,10 +369,8 @@ public void onMilestoneEventListenerAddedInOptions_isRemovedInOnDestroy() { @Test public void onNewBannerInstruction_instructionListenerIsCalled() { - BannerInstructions modifiedInstructions = mock(BannerInstructions.class); - BannerInstructions originalInstructions = mock(BannerInstructions.class); + BannerInstructions originalInstructions = mock(BannerInstructions.class, RETURNS_DEEP_STUBS); BannerInstructionsListener bannerInstructionsListener = mock(BannerInstructionsListener.class); - when(bannerInstructionsListener.willDisplay(originalInstructions)).thenReturn(modifiedInstructions); NavigationViewEventDispatcher eventDispatcher = buildViewEventDispatcher(bannerInstructionsListener); eventDispatcher.onBannerDisplay(originalInstructions); @@ -375,10 +380,10 @@ public void onNewBannerInstruction_instructionListenerIsCalled() { @Test public void onNewVoiceAnnouncement_instructionListenerIsCalled() { - SpeechAnnouncement modifiedAnnouncement = mock(SpeechAnnouncement.class); SpeechAnnouncement originalAnnouncement = mock(SpeechAnnouncement.class); SpeechAnnouncementListener speechAnnouncementListener = mock(SpeechAnnouncementListener.class); - when(speechAnnouncementListener.willVoice(originalAnnouncement)).thenReturn(modifiedAnnouncement); + String textAnnouncement = "announcement to be voiced"; + when(speechAnnouncementListener.willVoice(originalAnnouncement)).thenReturn(textAnnouncement); NavigationViewEventDispatcher eventDispatcher = buildViewEventDispatcher(speechAnnouncementListener); eventDispatcher.onAnnouncement(originalAnnouncement); @@ -386,6 +391,38 @@ public void onNewVoiceAnnouncement_instructionListenerIsCalled() { verify(speechAnnouncementListener).willVoice(originalAnnouncement); } + @Test + public void onNewBannerInstruction_bannerToBeDisplayedIsReturned() { + List originalBannerComponents = new ArrayList<>(1); + BannerComponents originalBannerComponent = BannerComponents.builder().text("text").type("type").build(); + originalBannerComponents.add(originalBannerComponent); + BannerText originalBannerText = BannerText.builder().components(originalBannerComponents).build(); + BannerInstructions originalInstructions = BannerInstructions.builder().primary(originalBannerText) + .distanceAlongGeometry(25.0).build(); + BannerInstructionsListener bannerInstructionsListener = mock(BannerInstructionsListener.class); + String textBanner = "banner to be displayed"; + when(bannerInstructionsListener.willDisplay(originalInstructions)).thenReturn(textBanner); + NavigationViewEventDispatcher eventDispatcher = buildViewEventDispatcher(bannerInstructionsListener); + + BannerInstructions modifiedInstruction = eventDispatcher.onBannerDisplay(originalInstructions); + + assertEquals("banner to be displayed", modifiedInstruction.primary().components().get(0).text()); + assertEquals("banner to be displayed", modifiedInstruction.primary().components().get(0).abbreviation()); + } + + @Test + public void onNewVoiceAnnouncement_announcementToBeVoicedIsReturned() { + SpeechAnnouncement originalAnnouncement = SpeechAnnouncement.builder().announcement("announcement").build(); + SpeechAnnouncementListener speechAnnouncementListener = mock(SpeechAnnouncementListener.class); + String textAnnouncement = "announcement to be voiced"; + when(speechAnnouncementListener.willVoice(originalAnnouncement)).thenReturn(textAnnouncement); + NavigationViewEventDispatcher eventDispatcher = buildViewEventDispatcher(speechAnnouncementListener); + + SpeechAnnouncement modifiedAnnouncement = eventDispatcher.onAnnouncement(originalAnnouncement); + + assertEquals("announcement to be voiced", modifiedAnnouncement.announcement()); + } + @NonNull private NavigationViewEventDispatcher buildViewEventDispatcher(SpeechAnnouncementListener speechAnnouncementListener) { NavigationViewEventDispatcher eventDispatcher = new NavigationViewEventDispatcher();