Backed out changeset b60e7b3407b3 (bug 1544584) for Android Checkstyle in /builds/worker/workspace/build/src/mobile/android/geckoview/src/main/java/org/mozilla/gecko/SpeechSynthesisService.java

This commit is contained in:
Dorel Luca 2019-05-16 03:18:35 +03:00
Родитель 69828aa2fe
Коммит f8c68f3fd8
1 изменённых файлов: 19 добавлений и 41 удалений

Просмотреть файл

@ -20,20 +20,13 @@ import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
public class SpeechSynthesisService { public class SpeechSynthesisService {
private static final String LOGTAG = "GeckoSpeechSynthesis"; private static final String LOGTAG = "GeckoSpeechSynthesis";
// Object type is used to make it easier to remove android.speech dependencies using Proguard. private static TextToSpeech sTTS;
private static Object sTTS;
@WrapForJNI(calledFrom = "gecko") @WrapForJNI(calledFrom = "gecko")
public static void initSynth() { public static void initSynth() {
initSynthInternal();
}
// Extra internal method to make it easier to remove android.speech dependencies using Proguard.
private static void initSynthInternal() {
if (sTTS != null) { if (sTTS != null) {
return; return;
} }
@ -54,20 +47,15 @@ public class SpeechSynthesisService {
}); });
} }
private static TextToSpeech getTTS() {
return (TextToSpeech) sTTS;
}
private static void registerVoicesByLocale() { private static void registerVoicesByLocale() {
ThreadUtils.postToBackgroundThread(new Runnable() { ThreadUtils.postToBackgroundThread(new Runnable() {
@Override @Override
public void run() { public void run() {
TextToSpeech tss = getTTS();
Locale defaultLocale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 Locale defaultLocale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
? tss.getDefaultLanguage() ? sTTS.getDefaultLanguage()
: tss.getLanguage(); : sTTS.getLanguage();
for (Locale locale : getAvailableLanguages()) { for (Locale locale : getAvailableLanguages()) {
final Set<String> features = tss.getFeatures(locale); final Set<String> features = sTTS.getFeatures(locale);
boolean isLocal = features != null && features.contains(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); boolean isLocal = features != null && features.contains(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
String localeStr = locale.toString(); String localeStr = locale.toString();
registerVoice("moz-tts:android:" + localeStr, locale.getDisplayName(), localeStr.replace("_", "-"), !isLocal, defaultLocale == locale); registerVoice("moz-tts:android:" + localeStr, locale.getDisplayName(), localeStr.replace("_", "-"), !isLocal, defaultLocale == locale);
@ -81,11 +69,11 @@ public class SpeechSynthesisService {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// While this method was introduced in 21, it seems that it // While this method was introduced in 21, it seems that it
// has not been implemented in the speech service side until 23. // has not been implemented in the speech service side until 23.
return getTTS().getAvailableLanguages(); return sTTS.getAvailableLanguages();
} }
Set<Locale> locales = new HashSet<Locale>(); Set<Locale> locales = new HashSet<Locale>();
for (Locale locale : Locale.getAvailableLocales()) { for (Locale locale : Locale.getAvailableLocales()) {
if (locale.getVariant().isEmpty() && getTTS().isLanguageAvailable(locale) > 0) { if (locale.getVariant().isEmpty() && sTTS.isLanguageAvailable(locale) > 0) {
locales.add(locale); locales.add(locale);
} }
} }
@ -102,29 +90,24 @@ public class SpeechSynthesisService {
@WrapForJNI(calledFrom = "gecko") @WrapForJNI(calledFrom = "gecko")
public static String speak(final String uri, final String text, final float rate, public static String speak(final String uri, final String text, final float rate,
final float pitch, final float volume) { final float pitch, final float volume) {
AtomicBoolean result = new AtomicBoolean(false);
final String utteranceId = UUID.randomUUID().toString();
speakInternal(uri, text, rate, pitch, volume, utteranceId, result);
return result.get() ? utteranceId : null;
}
// Extra internal method to make it easier to remove android.speech dependencies using Proguard.
private static void speakInternal(final String uri, final String text, final float rate,
final float pitch, final float volume, String utteranceId, AtomicBoolean result) {
if (sTTS == null) { if (sTTS == null) {
Log.w(LOGTAG, "TextToSpeech is not initialized"); Log.w(LOGTAG, "TextToSpeech is not initialized");
return; return null;
} }
HashMap<String, String> params = new HashMap<String, String>(); HashMap<String, String> params = new HashMap<String, String>();
final String utteranceId = UUID.randomUUID().toString();
params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Float.toString(volume)); params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Float.toString(volume));
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
TextToSpeech tss = (TextToSpeech) sTTS; sTTS.setLanguage(new Locale(uri.substring("moz-tts:android:".length())));
tss.setLanguage(new Locale(uri.substring("moz-tts:android:".length()))); sTTS.setSpeechRate(rate);
tss.setSpeechRate(rate); sTTS.setPitch(pitch);
tss.setPitch(pitch); int result = sTTS.speak(text, TextToSpeech.QUEUE_FLUSH, params);
int speakRes = tss.speak(text, TextToSpeech.QUEUE_FLUSH, params); if (result != TextToSpeech.SUCCESS) {
result.set(speakRes == TextToSpeech.SUCCESS); return null;
}
return utteranceId;
} }
private static void setUtteranceListener() { private static void setUtteranceListener() {
@ -133,7 +116,7 @@ public class SpeechSynthesisService {
return; return;
} }
getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() { sTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override @Override
public void onDone(final String utteranceId) { public void onDone(final String utteranceId) {
dispatchEnd(utteranceId); dispatchEnd(utteranceId);
@ -180,17 +163,12 @@ public class SpeechSynthesisService {
@WrapForJNI(calledFrom = "gecko") @WrapForJNI(calledFrom = "gecko")
public static void stop() { public static void stop() {
stopInternal();
}
// Extra internal method to make it easier to remove android.speech dependencies using Proguard.
private static void stopInternal() {
if (sTTS == null) { if (sTTS == null) {
Log.w(LOGTAG, "TextToSpeech is not initialized"); Log.w(LOGTAG, "TextToSpeech is not initialized");
return; return;
} }
getTTS().stop(); sTTS.stop();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Android M has onStop method. If Android L or above, dispatch // Android M has onStop method. If Android L or above, dispatch
// event // event