Bug 1544584 - Make it possible to remove android.speech dependencies using Proguard r=snorp,rbarker,eeejay
☠☠ backed out by 317b7da2d805 ☠ ☠
authorImanol Fernandez <mortimergoro@gmail.com>
Wed, 15 May 2019 19:56:43 +0000
changeset 532824 b60e7b3407b3aa6a2110f7e09a41e1ea10a04213
parent 532823 c496d42a69b48d22510821637c5494200b80aee0
child 532825 a0d592c0cd66b61393d78a81a88be2d9f133837b
push id11272
push userapavel@mozilla.com
push dateThu, 16 May 2019 15:28:22 +0000
treeherdermozilla-beta@2265bfc5920d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, rbarker, eeejay
bugs1544584
milestone68.0a1
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
Bug 1544584 - Make it possible to remove android.speech dependencies using Proguard r=snorp,rbarker,eeejay Some minor refactor to make it possible to remove android.speech dependencies using Proguard Differential Revision: https://phabricator.services.mozilla.com/D27612
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,23 +15,30 @@ 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";
-    private static TextToSpeech sTTS;
+    // Object type is used to make it easier to remove android.speech dependencies using Proguard.
+    private static Object 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
@@ -42,86 +49,96 @@ 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
-                        ? sTTS.getDefaultLanguage()
-                        : sTTS.getLanguage();
+                        ? tss.getDefaultLanguage()
+                        : tss.getLanguage();
                 for (Locale locale : getAvailableLanguages()) {
-                    final Set<String> features = sTTS.getFeatures(locale);
+                    final Set<String> features = tss.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 sTTS.getAvailableLanguages();
+            return getTTS().getAvailableLanguages();
         }
         Set<Locale> locales = new HashSet<Locale>();
         for (Locale locale : Locale.getAvailableLocales()) {
-            if (locale.getVariant().isEmpty() && sTTS.isLanguageAvailable(locale) > 0) {
+            if (locale.getVariant().isEmpty() && getTTS().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 null;
+            return;
         }
 
         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);
-        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;
+        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);
     }
 
     private static void setUtteranceListener() {
         if (sTTS == null) {
             Log.w(LOGTAG, "TextToSpeech is not initialized");
             return;
         }
 
-        sTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
+        getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
             @Override
             public void onDone(final String utteranceId) {
                 dispatchEnd(utteranceId);
             }
 
             @Override
             public void onError(final String utteranceId) {
                 dispatchError(utteranceId);
@@ -158,21 +175,26 @@ 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;
         }
 
-        sTTS.stop();
+        getTTS().stop();
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
             // Android M has onStop method.  If Android L or above, dispatch
             // event
             dispatchEnd(null);
         }
     }
 }