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
authorDorel Luca <dluca@mozilla.com>
Thu, 16 May 2019 03:18:35 +0300
changeset 535907 317b7da2d8053c65bb905ab79f8226563b6538ca
parent 535906 a187487af38a4caa5f125ab660c4d1d09d69aa9d
child 535908 b8de4598780a6e000e3cd3b8fbde5ba3b972c492
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1544584
milestone68.0a1
backs outb60e7b3407b3aa6a2110f7e09a41e1ea10a04213
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
mobile/android/geckoview/src/main/java/org/mozilla/gecko/SpeechSynthesisService.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/SpeechSynthesisService.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/SpeechSynthesisService.java
@@ -15,30 +15,23 @@ import android.speech.tts.TextToSpeech;
 import android.speech.tts.UtteranceProgressListener;
 import android.util.Log;
 
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Locale;
 import java.util.Set;
 import java.util.UUID;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 public class SpeechSynthesisService  {
     private static final String LOGTAG = "GeckoSpeechSynthesis";
-    // Object type is used to make it easier to remove android.speech dependencies using Proguard.
-    private static Object sTTS;
+    private static TextToSpeech sTTS;
 
     @WrapForJNI(calledFrom = "gecko")
     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) {
             return;
         }
 
         final Context ctx = GeckoAppShell.getApplicationContext();
 
         sTTS = new TextToSpeech(ctx, new TextToSpeech.OnInitListener() {
             @Override
@@ -49,96 +42,86 @@ public class SpeechSynthesisService  {
                 }
 
                 setUtteranceListener();
                 registerVoicesByLocale();
             }
         });
     }
 
-    private static TextToSpeech getTTS() {
-        return (TextToSpeech) sTTS;
-    }
-
     private static void registerVoicesByLocale() {
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
-                TextToSpeech tss = getTTS();
                 Locale defaultLocale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
-                        ? tss.getDefaultLanguage()
-                        : tss.getLanguage();
+                        ? sTTS.getDefaultLanguage()
+                        : sTTS.getLanguage();
                 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);
                     String localeStr = locale.toString();
                     registerVoice("moz-tts:android:" + localeStr, locale.getDisplayName(), localeStr.replace("_", "-"), !isLocal, defaultLocale == locale);
                 }
                 doneRegisteringVoices();
             }
         });
     }
 
     private static Set<Locale> getAvailableLanguages() {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             // While this method was introduced in 21, it seems that it
             // has not been implemented in the speech service side until 23.
-            return getTTS().getAvailableLanguages();
+            return sTTS.getAvailableLanguages();
         }
         Set<Locale> locales = new HashSet<Locale>();
         for (Locale locale : Locale.getAvailableLocales()) {
-            if (locale.getVariant().isEmpty() && getTTS().isLanguageAvailable(locale) > 0) {
+            if (locale.getVariant().isEmpty() && sTTS.isLanguageAvailable(locale) > 0) {
                 locales.add(locale);
             }
         }
 
         return locales;
     }
 
     @WrapForJNI(dispatchTo = "gecko")
     private static native void registerVoice(String uri, String name, String locale, boolean isNetwork, boolean isDefault);
 
     @WrapForJNI(dispatchTo = "gecko")
     private static native void doneRegisteringVoices();
 
     @WrapForJNI(calledFrom = "gecko")
     public static String speak(final String uri, final String text, final float rate,
                                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) {
             Log.w(LOGTAG, "TextToSpeech is not initialized");
-            return;
+            return null;
         }
 
         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_UTTERANCE_ID, utteranceId);
-        TextToSpeech tss = (TextToSpeech) sTTS;
-        tss.setLanguage(new Locale(uri.substring("moz-tts:android:".length())));
-        tss.setSpeechRate(rate);
-        tss.setPitch(pitch);
-        int speakRes = tss.speak(text, TextToSpeech.QUEUE_FLUSH, params);
-        result.set(speakRes == TextToSpeech.SUCCESS);
+        sTTS.setLanguage(new Locale(uri.substring("moz-tts:android:".length())));
+        sTTS.setSpeechRate(rate);
+        sTTS.setPitch(pitch);
+        int result = sTTS.speak(text, TextToSpeech.QUEUE_FLUSH, params);
+        if (result != TextToSpeech.SUCCESS) {
+            return null;
+        }
+
+        return utteranceId;
     }
 
     private static void setUtteranceListener() {
         if (sTTS == null) {
             Log.w(LOGTAG, "TextToSpeech is not initialized");
             return;
         }
 
-        getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
+        sTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
             @Override
             public void onDone(final String utteranceId) {
                 dispatchEnd(utteranceId);
             }
 
             @Override
             public void onError(final String utteranceId) {
                 dispatchError(utteranceId);
@@ -175,26 +158,21 @@ public class SpeechSynthesisService  {
     @WrapForJNI(dispatchTo = "gecko")
     private static native void dispatchError(String utteranceId);
 
     @WrapForJNI(dispatchTo = "gecko")
     private static native void dispatchBoundary(String utteranceId, int start, int end);
 
     @WrapForJNI(calledFrom = "gecko")
     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) {
             Log.w(LOGTAG, "TextToSpeech is not initialized");
             return;
         }
 
-        getTTS().stop();
+        sTTS.stop();
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
             // Android M has onStop method.  If Android L or above, dispatch
             // event
             dispatchEnd(null);
         }
     }
 }