diff --git a/android/jni/com/mapswithme/maps/sound/tts.cpp b/android/jni/com/mapswithme/maps/sound/tts.cpp index 1edcf5cf69..33330ebf9c 100644 --- a/android/jni/com/mapswithme/maps/sound/tts.cpp +++ b/android/jni/com/mapswithme/maps/sound/tts.cpp @@ -11,27 +11,26 @@ namespace extern "C" { JNIEXPORT void JNICALL - Java_com_mapswithme_maps_sound_TTSPlayer_nativeEnableTurnNotifications(JNIEnv * env, jclass thiz, jboolean enable) + Java_com_mapswithme_maps_sound_TtsPlayer_nativeEnableTurnNotifications(JNIEnv * env, jclass thiz, jboolean enable) { return frm()->EnableTurnNotifications(enable == JNI_TRUE ? true : false); } JNIEXPORT jboolean JNICALL - Java_com_mapswithme_maps_sound_TTSPlayer_nativeAreTurnNotificationsEnabled(JNIEnv * env, jclass clazz) + Java_com_mapswithme_maps_sound_TtsPlayer_nativeAreTurnNotificationsEnabled(JNIEnv * env, jclass clazz) { return frm()->AreTurnNotificationsEnabled() ? JNI_TRUE : JNI_FALSE; } JNIEXPORT void JNICALL - Java_com_mapswithme_maps_sound_TTSPlayer_nativeSetTurnNotificationsLocale(JNIEnv * env, jclass thiz, jstring jLocale) + Java_com_mapswithme_maps_sound_TtsPlayer_nativeSetTurnNotificationsLocale(JNIEnv * env, jclass thiz, jstring jLocale) { - if (frm()) - frm()->SetTurnNotificationsLocale(jni::ToNativeString(env, jLocale)); + frm()->SetTurnNotificationsLocale(jni::ToNativeString(env, jLocale)); } JNIEXPORT jstring JNICALL - Java_com_mapswithme_maps_sound_TTSPlayer_nativeGetTurnNotificationsLocale(JNIEnv * env, jclass thiz) + Java_com_mapswithme_maps_sound_TtsPlayer_nativeGetTurnNotificationsLocale(JNIEnv * env, jclass thiz) { return jni::ToJavaString(env, frm()->GetTurnNotificationsLocale().c_str()); } -} // extern "C" \ No newline at end of file +} // extern "C" diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index 9bd7a2169e..fad09ad2b6 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -63,7 +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.sound.TtsPlayer; import com.mapswithme.maps.widget.BottomButtonsLayout; import com.mapswithme.maps.widget.FadeView; import com.mapswithme.maps.widget.placepage.BasePlacePageAnimationController; @@ -445,8 +445,8 @@ public class MWMActivity extends BaseMwmFragmentActivity setContentView(R.layout.activity_map); initViews(); - // Initializing TTS player instance. - TTSPlayer.get(); + // Initializing TTS player. + TtsPlayer.INSTANCE.init(); // Do not turn off the screen while benchmarking if (MWMApplication.get().nativeIsBenchmarking()) @@ -797,11 +797,7 @@ public class MWMActivity extends BaseMwmFragmentActivity mTvTurnDistance.setText(builder); // Turn sound notifications. - if (info.mTurnNotifications != null) - { - for (String textToSpeak : info.mTurnNotifications) - TTSPlayer.get().speak(textToSpeak); - } + TtsPlayer.INSTANCE.speakNotifications(info.mTurnNotifications); } } @@ -942,7 +938,7 @@ public class MWMActivity extends BaseMwmFragmentActivity pauseLocation(); stopWatchingExternalStorage(); stopWatchingCompassStatusUpdate(); - TTSPlayer.get().stop(); + TtsPlayer.INSTANCE.stop(); super.onPause(); mLikesManager.cancelLikeDialog(); } diff --git a/android/src/com/mapswithme/maps/search/SearchFragment.java b/android/src/com/mapswithme/maps/search/SearchFragment.java index d0ade90f0c..b225bad21d 100644 --- a/android/src/com/mapswithme/maps/search/SearchFragment.java +++ b/android/src/com/mapswithme/maps/search/SearchFragment.java @@ -25,7 +25,7 @@ import com.mapswithme.maps.R; import com.mapswithme.maps.base.BaseMwmRecyclerFragment; import com.mapswithme.maps.base.OnBackPressListener; import com.mapswithme.maps.location.LocationHelper; -import com.mapswithme.maps.sound.TTSPlayer; +import com.mapswithme.maps.sound.TtsPlayer; import com.mapswithme.util.InputUtils; import com.mapswithme.util.Language; import com.mapswithme.util.StringUtils; @@ -281,7 +281,7 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl setSearchQuery(query); } - private void hideSearchPanel() + private void hideSearch() { mEtSearchQuery.setText(null); InputUtils.hideKeyboard(mEtSearchQuery); @@ -299,7 +299,7 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl return false; // close Search panel - hideSearchPanel(); + hideSearch(); // change map style for the Map activity final int mapStyle = isDark ? Framework.MAP_STYLE_DARK : Framework.MAP_STYLE_LIGHT; @@ -316,7 +316,7 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl if (!pedestrian && !vehicle) return false; - hideSearchPanel(); + hideSearch(); final int routerType = pedestrian ? Framework.ROUTER_TYPE_PEDESTRIAN : Framework.ROUTER_TYPE_VEHICLE; Framework.setRouter(routerType); @@ -326,14 +326,14 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl private boolean trySwitchOnTurnSound(String query) { - final boolean sound = query.equals("?sound"); - final boolean nosound = query.equals("?nosound"); + final boolean sound = "?sound".equals(query); + final boolean nosound = "?nosound".equals(query); if (!sound && !nosound) return false; - hideSearchPanel(); - TTSPlayer.get().enable(sound); + hideSearch(); + TtsPlayer.INSTANCE.enable(sound); return sound; } diff --git a/android/src/com/mapswithme/maps/sound/TTSPlayer.java b/android/src/com/mapswithme/maps/sound/TtsPlayer.java similarity index 62% rename from android/src/com/mapswithme/maps/sound/TTSPlayer.java rename to android/src/com/mapswithme/maps/sound/TtsPlayer.java index 84e114cc13..df721ec6e6 100644 --- a/android/src/com/mapswithme/maps/sound/TTSPlayer.java +++ b/android/src/com/mapswithme/maps/sound/TtsPlayer.java @@ -10,51 +10,45 @@ import com.mapswithme.maps.MWMApplication; import java.util.Locale; -public class TTSPlayer +public enum TtsPlayer { - private static TTSPlayer ourInstance = null; + INSTANCE; - private Context mContext = null; - private TextToSpeech mTts = null; - private Locale mTtsLocale = null; + private Context mContext; + private TextToSpeech mTts; + private Locale mTtsLocale; - private final static String TAG = "TTSPlayer"; + private final static String TAG = "TtsPlayer"; - private TTSPlayer() + TtsPlayer() { mContext = MWMApplication.get().getApplicationContext(); - setLocaleIfAvailable(Locale.getDefault()); } - @Override - protected void finalize() throws Throwable + public void init() { - if(mTts != null) - mTts.shutdown(); - super.finalize(); + Locale systemLanguage = Locale.getDefault(); + if (INSTANCE.mTtsLocale == null || !INSTANCE.isLocaleEqual(systemLanguage)) + INSTANCE.setLocaleIfAvailable(systemLanguage); } - public static TTSPlayer get() - { - if (ourInstance == null || !ourInstance.isLocaleEquals(Locale.getDefault())) - ourInstance = new TTSPlayer(); - - return ourInstance; - } - - private boolean isLocaleEquals(Locale locale) + private boolean isLocaleEqual(Locale locale) { return locale.getLanguage().equals(mTtsLocale.getLanguage()) && - locale.getCountry().equals(mTtsLocale.getCountry()); + locale.getCountry().equals(mTtsLocale.getCountry()); } private void setLocaleIfAvailable(final Locale locale) { - if (mTts != null && mTts.getLanguage().equals(locale)) + if (mTts != null && mTtsLocale.equals(locale)) return; - // @TODO Consider move TextToSpeech to a service: - // http://stackoverflow.com/questions/24712639/android-texttospeech-initialization-blocks-freezes-ui-thread + if (mTts != null) + { + mTts.stop(); + mTts.shutdown(); + } + mTts = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() { @Override @@ -82,11 +76,11 @@ public class TTSPlayer }); } - public void speak(String textToSpeak) + private void speak(String textToSpeak) { if (mTts == null) { - Log.w(TAG, "TTSPlayer.speak() is called while mTts == null."); + Log.w(TAG, "TtsPlayer.speak() is called while mTts == null."); return; } // @TODO(vbykoianko) removes these two toasts below when the test period is finished. @@ -98,6 +92,15 @@ public class TTSPlayer } } + public void speakNotifications(String[] turnNotifications) + { + if (turnNotifications == null) + return; + + for (String textToSpeak : turnNotifications) + speak(textToSpeak); + } + public void stop() { if(mTts != null) @@ -114,8 +117,8 @@ public class TTSPlayer nativeEnableTurnNotifications(enabled); } - public native static void nativeEnableTurnNotifications(boolean enable); - public native static boolean nativeAreTurnNotificationsEnabled(); - public native static void nativeSetTurnNotificationsLocale(String locale); - public native static String nativeGetTurnNotificationsLocale(); + private native static void nativeEnableTurnNotifications(boolean enable); + private native static boolean nativeAreTurnNotificationsEnabled(); + private native static void nativeSetTurnNotificationsLocale(String locale); + private native static String nativeGetTurnNotificationsLocale(); }