diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index bc970b8cc8..6761586757 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -63,6 +63,7 @@ import com.mapswithme.maps.settings.SettingsActivity; import com.mapswithme.maps.settings.StoragePathManager; import com.mapswithme.maps.settings.StoragePathManager.MoveFilesListener; import com.mapswithme.maps.settings.UnitLocale; +import com.mapswithme.maps.sound.TTSPlayer; import com.mapswithme.maps.widget.BottomButtonsLayout; import com.mapswithme.maps.widget.FadeView; import com.mapswithme.maps.widget.placepage.BasePlacePageAnimationController; diff --git a/android/src/com/mapswithme/maps/sound/TTSPlayer.java b/android/src/com/mapswithme/maps/sound/TTSPlayer.java new file mode 100644 index 0000000000..50bdbc8ff0 --- /dev/null +++ b/android/src/com/mapswithme/maps/sound/TTSPlayer.java @@ -0,0 +1,110 @@ +package com.mapswithme.maps.sound; + +import android.content.Context; +import android.speech.tts.TextToSpeech; +import android.util.Log; +import android.widget.Toast; + +import com.mapswithme.maps.MWMApplication; + +import java.util.Locale; + + +public class TTSPlayer +{ + private static TTSPlayer ourInstance = null; + + private Context mContext = null; + private TextToSpeech mTts = null; + + private final static String TAG = "TTSPlayer"; + + private TTSPlayer() + { + mContext = MWMApplication.get().getApplicationContext(); + } + + @Override + protected void finalize() throws Throwable + { + if(mTts != null) + mTts.shutdown(); + super.finalize(); + } + + public static TTSPlayer get() + { + if (ourInstance == null) + ourInstance = new TTSPlayer(); + return ourInstance; + } + + public void setLocaleIfAvailable(final Locale locale) + { + if (mTts != null && mTts.getLanguage().equals(locale)) + return; + + // @TODO Consider move TextToSpeech to a service: + // http://stackoverflow.com/questions/24712639/android-texttospeech-initialization-blocks-freezes-ui-thread + mTts = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() + { + @Override + public void onInit(int status) + { + if (status == TextToSpeech.ERROR) + { + Log.w(TAG, "Can't initialize TextToSpeech for locale " + locale.getLanguage() + " " + locale.getCountry()); + return; + } + + if (mTts.setLanguage(locale) != TextToSpeech.LANG_AVAILABLE) + mTts.setLanguage(Locale.UK); // Assuming that Locale.UK is always available. + } + }); + + final Locale loc = getLocale(); + if (loc != null) + ;// Call native method to set locale for TTS (loc.getLanguage()) + } + + public Locale getLocale() + { + if (mTts == null) + return null; + return mTts.getLanguage(); + } + + public void speak(String textToSpeak) + { + if (mTts == null) + { + Log.e(TAG, "speakText is called while mTts == null"); + return; + } + + Toast.makeText(mContext, textToSpeak, Toast.LENGTH_SHORT).show(); + mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, null); + } + + public void stop() + { + if(mTts != null) + mTts.stop(); + } + + public boolean isEnabled() + { + // Call native method to check if TTS is set as enabled + return true; + } + + public void enable(boolean enabled) + { + // Call native method to enable/disable TTS + } + + public void setLengthUnits(int units) + { + // Call native method to set units for TTS + } +} diff --git a/platform/settings.hpp b/platform/settings.hpp index a6852b2a53..47dbb1a2f3 100644 --- a/platform/settings.hpp +++ b/platform/settings.hpp @@ -46,6 +46,8 @@ namespace Settings StringStorage::Instance().DeleteKeyAndValue(key); } + // @TODO(vbykoianko) For the time being two enums which are reflected length units are used. + // This enum should be replaced with enum class LengthUnits. enum Units { Metric = 0, Foot }; /// Use this function for running some stuff once according to date.