From ac57f8d4da92af07513664613bcc5c085bf098a0 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Fri, 4 Sep 2015 16:41:32 +0300 Subject: [PATCH] Android. Initialization of TTS on the first switching on. --- .../src/com/mapswithme/maps/MwmActivity.java | 10 +---- .../com/mapswithme/maps/sound/TtsPlayer.java | 38 ++++++++++++------- routing/turns_tts_text.cpp | 9 +++-- routing/turns_tts_text.hpp | 1 + 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 1c634f9a3b..a8e8eef77b 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -451,8 +451,6 @@ public class MwmActivity extends BaseMwmFragmentActivity setContentView(R.layout.activity_map); initViews(); - TtsPlayer.INSTANCE.init(); - if (MwmApplication.get().nativeIsBenchmarking()) Utils.keepScreenOn(true, getWindow()); @@ -880,13 +878,8 @@ public class MwmActivity extends BaseMwmFragmentActivity mLayoutRouting.updateRouteInfo(); mMainMenu.updateRoutingInfo(); - // TODO think about moving TtsPlayer logic to RoutingLayout to minimize native calls. if (state == RoutingLayout.State.TURN_INSTRUCTIONS) - { - final String[] turnNotifications = Framework.nativeGenerateTurnSound(); - if (turnNotifications != null) - TtsPlayer.INSTANCE.speak(turnNotifications); - } + TtsPlayer.INSTANCE.playTurnNotifications(); } } @@ -969,6 +962,7 @@ public class MwmActivity extends BaseMwmFragmentActivity { super.onStart(); + TtsPlayer.INSTANCE.reinitIfLocaleChanged(); if (!mIsFragmentContainer) popFragment(); } diff --git a/android/src/com/mapswithme/maps/sound/TtsPlayer.java b/android/src/com/mapswithme/maps/sound/TtsPlayer.java index 2a49eb80d8..89d78bc409 100644 --- a/android/src/com/mapswithme/maps/sound/TtsPlayer.java +++ b/android/src/com/mapswithme/maps/sound/TtsPlayer.java @@ -5,6 +5,7 @@ import android.speech.tts.TextToSpeech; import android.util.Log; import android.widget.Toast; +import com.mapswithme.maps.Framework; import com.mapswithme.maps.MwmApplication; import java.util.Locale; @@ -25,16 +26,22 @@ public enum TtsPlayer TtsPlayer() {} - public void init() + public void reinitIfLocaleChanged() { - Locale systemLanguage = Locale.getDefault(); - if (systemLanguage == null) - systemLanguage = DEFAULT_LOCALE; + if (!isValid()) + return; // TtsPlayer was not inited yet. - if (mTtsLocale == null || !isLocaleEqual(systemLanguage)) + final Locale systemLanguage = getDefaultLocale(); + if (!isLocaleEqual(systemLanguage)) initTts(systemLanguage); } + private Locale getDefaultLocale() + { + final Locale systemLanguage = Locale.getDefault(); + return systemLanguage == null ? DEFAULT_LOCALE : systemLanguage; + } + private boolean isLocaleEqual(Locale locale) { return locale.getLanguage().equals(mTtsLocale.getLanguage()) && @@ -107,13 +114,16 @@ public enum TtsPlayer }); } - private boolean readyToPlay() + private boolean isValid() { return !mIsLocaleChanging && mTts != null && mTtsLocale != null; } private void speak(String textToSpeak) { + if (textToSpeak.isEmpty()) + return; + // @TODO(vbykoianko) removes these two toasts below when the test period is finished. Toast.makeText(MwmApplication.get(), textToSpeak, Toast.LENGTH_SHORT).show(); if (mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, null) == TextToSpeech.ERROR) @@ -123,16 +133,16 @@ public enum TtsPlayer } } - public void speak(String[] turnNotifications) + public void playTurnNotifications() { - if (!readyToPlay()) + if (!isValid()) return; // speak() is called while TTS is not ready or could not be initialized. - if (turnNotifications == null) - return; - - for (String textToSpeak : turnNotifications) - speak(textToSpeak); + // TODO think about moving TtsPlayer logic to RoutingLayout to minimize native calls. + final String[] turnNotifications = Framework.nativeGenerateTurnSound(); + if (turnNotifications != null) + for (String textToSpeak : turnNotifications) + speak(textToSpeak); } public void stop() @@ -148,6 +158,8 @@ public enum TtsPlayer public void enable(boolean enabled) { + if (!isValid()) + initTts(getDefaultLocale()); nativeEnableTurnNotifications(enabled); } diff --git a/routing/turns_tts_text.cpp b/routing/turns_tts_text.cpp index e55ed3f47a..44caafbbae 100644 --- a/routing/turns_tts_text.cpp +++ b/routing/turns_tts_text.cpp @@ -48,13 +48,14 @@ void GetTtsText::SetLocaleWithJson(string const & jsonBuffer) string GetTtsText::operator()(Notification const & notification) const { - if (!m_getCurLang->IsValid()) - return string(); if (notification.m_distanceUnits == 0 && !notification.m_useThenInsteadOfDistance) return GetTextById(GetDirectionTextId(notification)); - return GetTextById(GetDistanceTextId(notification)) + " " + - GetTextById(GetDirectionTextId(notification)); + string const distStr = GetTextById(GetDistanceTextId(notification)); + string const dirStr = GetTextById(GetDirectionTextId(notification)); + if (distStr.empty() && dirStr.empty()) + return ""; + return distStr + " " + dirStr; } string GetTtsText::GetTextById(string const & textId) const diff --git a/routing/turns_tts_text.hpp b/routing/turns_tts_text.hpp index 9c54cce5e3..a160d27a05 100644 --- a/routing/turns_tts_text.hpp +++ b/routing/turns_tts_text.hpp @@ -22,6 +22,7 @@ class GetTtsText { public: string operator()(Notification const & notification) const; + /// TODO(vbykoianko) Check if locale is available. If not use default (en) locale. void SetLocale(string const & locale); inline string GetLocale() const { return m_locale; } /// SetLocaleWithJson is used for writing unit tests only.