From b656705faf8937e1b5ec0f4c0dfc058d0028723d Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Mon, 7 Sep 2015 17:55:05 +0300 Subject: [PATCH] Android. Passing correct names of Chinese languages to CPP part. Chinese TTS turn notification start working on Android now. --- .../com/mapswithme/maps/sound/TtsPlayer.java | 21 ++++++++++--- android/src/com/mapswithme/util/Language.java | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/android/src/com/mapswithme/maps/sound/TtsPlayer.java b/android/src/com/mapswithme/maps/sound/TtsPlayer.java index 4a45269c25..1f3449f797 100644 --- a/android/src/com/mapswithme/maps/sound/TtsPlayer.java +++ b/android/src/com/mapswithme/maps/sound/TtsPlayer.java @@ -1,12 +1,13 @@ package com.mapswithme.maps.sound; -import android.content.Context; import android.speech.tts.TextToSpeech; +import android.text.TextUtils; import android.util.Log; import android.widget.Toast; import com.mapswithme.maps.Framework; import com.mapswithme.maps.MwmApplication; +import com.mapswithme.util.Language; import java.util.Locale; @@ -105,11 +106,18 @@ public enum TtsPlayer return; } + String localeTwine = Language.localeToTwineLanguage(mTtsLocale); + if (TextUtils.isEmpty(localeTwine)) + { + Log.w(TAG, "Cann't get a twine language name for the locale " + locale.getLanguage() + " " + locale.getCountry() + + ". TTS will be switched off."); + mTtsLocale = null; + return; + } + + nativeSetTurnNotificationsLocale(localeTwine); mTts.setLanguage(mTtsLocale); - // @TODO(vbykoianko) In case of mTtsLocale.getLanguage() returns zh. But the core is needed zh-Hant or zh-Hans. - // It should be fixed. - nativeSetTurnNotificationsLocale(mTtsLocale.getLanguage()); - Log.i(TAG, "setLocaleIfAvailable() onInit nativeSetTurnNotificationsLocale(" + mTtsLocale.getLanguage() + ")"); + Log.i(TAG, "setLocaleIfAvailable() onInit nativeSetTurnNotificationsLocale(" + localeTwine + ")"); } }); } @@ -152,6 +160,9 @@ public enum TtsPlayer return nativeAreTurnNotificationsEnabled(); } + // Note. After a call of enable(true) the flag enabled in cpp core will be set. + // But later in onInit callback the initialization could fail. + // In that case isValid() returns false and every call of playTurnNotifications returns in the beginning. public void enable(boolean enabled) { if (enabled && !isValid()) diff --git a/android/src/com/mapswithme/util/Language.java b/android/src/com/mapswithme/util/Language.java index cc78f9a983..1d2a2bdc9f 100644 --- a/android/src/com/mapswithme/util/Language.java +++ b/android/src/com/mapswithme/util/Language.java @@ -2,6 +2,8 @@ package com.mapswithme.util; import android.content.Context; import android.os.Build; +import android.text.TextUtils; +import android.util.Log; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; @@ -11,6 +13,7 @@ import java.util.Locale; public class Language { + private final static String TAG = "Language"; // Locale.getLanguage() returns even 3-letter codes, not that we need in the C++ core, // so we use locale itself, like zh_CN static public String getDefaultLocale() @@ -35,4 +38,32 @@ public class Language return getDefaultLocale(); } + + // Converts Locale to twine language name. + // If locale can be converted returns a twine language name. For example zh-Hans, ru, en and so on. + // If not returns an empty string. + static public String localeToTwineLanguage(Locale locale) + { + if (locale == null) + { + Log.e(TAG, "localeToTwineLanguage was called with null Locale."); + return ""; + } + + final String chinese = Locale.CHINESE.getLanguage(); + final String language = locale.getLanguage(); + + if (chinese.equals(language)) + { + if (Locale.SIMPLIFIED_CHINESE.equals(locale)) + return "zh-Hans"; // Chinese simplified + return "zh-Hant"; // Chinese traditional + } + if (TextUtils.isEmpty(language)) + { + Log.e(TAG, "locale.getLanguage() returns null or empty string."); + return ""; + } + return language; + } }