Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b0ae1fd1 authored by Joshua Imbriani's avatar Joshua Imbriani Committed by Android (Google) Code Review
Browse files

Merge "Adding a new overloaded API for synthesizeToFile taking a...

Merge "Adding a new overloaded API for synthesizeToFile taking a FileDescriptor instead of a File object"
parents bb021e3b 21ddc7a6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -42241,6 +42241,7 @@ package android.speech.tts {
    method public int speak(CharSequence, int, android.os.Bundle, String);
    method @Deprecated public int speak(String, int, java.util.HashMap<java.lang.String,java.lang.String>);
    method public int stop();
    method public int synthesizeToFile(@NonNull CharSequence, @NonNull android.os.Bundle, @NonNull android.os.ParcelFileDescriptor, @NonNull String);
    method public int synthesizeToFile(CharSequence, android.os.Bundle, java.io.File, String);
    method @Deprecated public int synthesizeToFile(String, java.util.HashMap<java.lang.String,java.lang.String>, String);
    field public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED = "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
+257 −284
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package android.speech.tts;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RawRes;
import android.annotation.SdkConstant;
@@ -858,9 +859,7 @@ public class TextToSpeech {
        }

        // Post connection case
        runActionNoReconnect(new Action<Void>() {
            @Override
            public Void run(ITextToSpeechService service) throws RemoteException {
        runActionNoReconnect((ITextToSpeechService service) -> {
            service.setCallback(getCallerIdentity(), null);
            service.stop(getCallerIdentity());
            mServiceConnection.disconnect();
@@ -874,7 +873,6 @@ public class TextToSpeech {
            mServiceConnection = null;
            mCurrentEngine = null;
            return null;
            }
        }, null, "shutdown", false);
    }

@@ -1105,9 +1103,7 @@ public class TextToSpeech {
                     final int queueMode,
                     final Bundle params,
                     final String utteranceId) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            Uri utteranceUri = mUtterances.get(text);
            if (utteranceUri != null) {
                return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
@@ -1116,7 +1112,6 @@ public class TextToSpeech {
                return service.speak(getCallerIdentity(), text, queueMode, getParams(params),
                        utteranceId);
            }
            }
        }, ERROR, "speak");
    }

@@ -1178,16 +1173,13 @@ public class TextToSpeech {
     */
    public int playEarcon(final String earcon, final int queueMode,
            final Bundle params, final String utteranceId) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            Uri earconUri = mEarcons.get(earcon);
            if (earconUri == null) {
                return ERROR;
            }
            return service.playAudio(getCallerIdentity(), earconUri, queueMode,
                    getParams(params), utteranceId);
            }
        }, ERROR, "playEarcon");
    }

@@ -1242,12 +1234,9 @@ public class TextToSpeech {
     */
    public int playSilentUtterance(final long durationInMs, final int queueMode,
            final String utteranceId) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            return service.playSilence(getCallerIdentity(), durationInMs,
                                        queueMode, utteranceId);
            }
        }, ERROR, "playSilentUtterance");
    }

@@ -1302,16 +1291,14 @@ public class TextToSpeech {
     */
    @Deprecated
    public Set<String> getFeatures(final Locale locale) {
        return runAction(new Action<Set<String>>() {
            @Override
            public Set<String> run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            String[] features = null;
            try {
                features = service.getFeaturesForLanguage(
                    locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
            } catch (MissingResourceException e) {
                    Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 " +
                            "country code for locale: " + locale, e);
                Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 "
                        + "country code for locale: " + locale, e);
                return null;
            }

@@ -1321,7 +1308,6 @@ public class TextToSpeech {
                return featureSet;
            }
            return null;
            }
        }, null, "getFeatures");
    }

@@ -1334,11 +1320,8 @@ public class TextToSpeech {
     * @return {@code true} if the TTS engine is speaking.
     */
    public boolean isSpeaking() {
        return runAction(new Action<Boolean>() {
            @Override
            public Boolean run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            return service.isSpeaking();
            }
        }, false, "isSpeaking");
    }

@@ -1349,11 +1332,8 @@ public class TextToSpeech {
     * @return {@link #ERROR} or {@link #SUCCESS}.
     */
    public int stop() {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            return service.stop(getCallerIdentity());
            }
        }, ERROR, "stop");
    }

@@ -1447,13 +1427,10 @@ public class TextToSpeech {
     */
    @Deprecated
    public Locale getDefaultLanguage() {
        return runAction(new Action<Locale>() {
            @Override
            public Locale run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            String[] defaultLanguage = service.getClientDefaultLanguage();

            return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
            }
        }, null, "getDefaultLanguage");
    }

@@ -1474,9 +1451,7 @@ public class TextToSpeech {
     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
     */
    public int setLanguage(final Locale loc) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            if (loc == null) {
                return LANG_NOT_SUPPORTED;
            }
@@ -1507,8 +1482,8 @@ public class TextToSpeech {
                // Get the default voice for the locale.
                String voiceName = service.getDefaultVoiceNameFor(language, country, variant);
                if (TextUtils.isEmpty(voiceName)) {
                        Log.w(TAG, "Couldn't find the default voice for " + language + "-" +
                                country + "-" + variant);
                    Log.w(TAG, "Couldn't find the default voice for " + language + "-"
                            + country + "-" + variant);
                    return LANG_NOT_SUPPORTED;
                }

@@ -1533,16 +1508,16 @@ public class TextToSpeech {
                try {
                    voiceLanguage = voice.getLocale().getISO3Language();
                } catch (MissingResourceException e) {
                        Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " +
                                voice.getLocale(), e);
                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: "
                            + voice.getLocale(), e);
                }

                String voiceCountry = "";
                try {
                    voiceCountry = voice.getLocale().getISO3Country();
                } catch (MissingResourceException e) {
                        Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " +
                                voice.getLocale(), e);
                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: "
                            + voice.getLocale(), e);
                }
                mParams.putString(Engine.KEY_PARAM_VOICE_NAME, voiceName);
                mParams.putString(Engine.KEY_PARAM_LANGUAGE, voiceLanguage);
@@ -1550,7 +1525,6 @@ public class TextToSpeech {
                mParams.putString(Engine.KEY_PARAM_VARIANT, voice.getLocale().getVariant());
            }
            return result;
            }
        }, LANG_NOT_SUPPORTED, "setLanguage");
    }

@@ -1582,16 +1556,13 @@ public class TextToSpeech {
     */
    @Deprecated
    public Locale getLanguage() {
        return runAction(new Action<Locale>() {
            @Override
            public Locale run(ITextToSpeechService service) {
        return runAction((ITextToSpeechService service) -> {
            /* No service call, but we're accessing mParams, hence need for
               wrapping it as an Action instance */
            String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, "");
            String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, "");
            String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, "");
            return new Locale(lang, country, variant);
            }
        }, null, "getLanguage");
    }

@@ -1599,9 +1570,7 @@ public class TextToSpeech {
     * Query the engine about the set of available languages.
     */
    public Set<Locale> getAvailableLanguages() {
        return runAction(new Action<Set<Locale>>() {
            @Override
            public Set<Locale> run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            List<Voice> voices = service.getVoices();
            if (voices == null) {
                return new HashSet<Locale>();
@@ -1611,7 +1580,6 @@ public class TextToSpeech {
                locales.add(voice.getLocale());
            }
            return locales;
            }
        }, null, "getAvailableLanguages");
    }

@@ -1625,12 +1593,9 @@ public class TextToSpeech {
     * @see Voice
     */
    public Set<Voice> getVoices() {
        return runAction(new Action<Set<Voice>>() {
            @Override
            public Set<Voice> run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            List<Voice> voices = service.getVoices();
            return (voices != null)  ? new HashSet<Voice>(voices) : new HashSet<Voice>();
            }
        }, null, "getVoices");
    }

@@ -1645,9 +1610,7 @@ public class TextToSpeech {
     * @see Voice
     */
    public int setVoice(final Voice voice) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            int result = service.loadVoice(getCallerIdentity(), voice.getName());
            if (result == SUCCESS) {
                mParams.putString(Engine.KEY_PARAM_VOICE_NAME, voice.getName());
@@ -1658,23 +1621,22 @@ public class TextToSpeech {
                try {
                    language = voice.getLocale().getISO3Language();
                } catch (MissingResourceException e) {
                        Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " +
                                voice.getLocale(), e);
                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: "
                            + voice.getLocale(), e);
                }

                String country = "";
                try {
                    country = voice.getLocale().getISO3Country();
                } catch (MissingResourceException e) {
                        Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " +
                                voice.getLocale(), e);
                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: "
                            + voice.getLocale(), e);
                }
                mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
                mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
                mParams.putString(Engine.KEY_PARAM_VARIANT, voice.getLocale().getVariant());
            }
            return result;
            }
        }, LANG_NOT_SUPPORTED, "setVoice");
    }

@@ -1689,15 +1651,12 @@ public class TextToSpeech {
     * @see Voice
     */
    public Voice getVoice() {
        return runAction(new Action<Voice>() {
            @Override
            public Voice run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            String voiceName = mParams.getString(Engine.KEY_PARAM_VOICE_NAME, "");
            if (TextUtils.isEmpty(voiceName)) {
                return null;
            }
            return getVoice(service, voiceName);
            }
        }, null, "getVoice");
    }

@@ -1730,9 +1689,7 @@ public class TextToSpeech {
     *     on error.
     */
    public Voice getDefaultVoice() {
        return runAction(new Action<Voice>() {
            @Override
            public Voice run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {

            String[] defaultLanguage = service.getClientDefaultLanguage();

@@ -1768,7 +1725,6 @@ public class TextToSpeech {
                }
            }
            return null;
            }
        }, null, "getDefaultVoice");
    }

@@ -1784,9 +1740,7 @@ public class TextToSpeech {
     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
     */
    public int isLanguageAvailable(final Locale loc) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
        return runAction((ITextToSpeechService service) -> {
            String language = null, country = null;

            try {
@@ -1804,10 +1758,36 @@ public class TextToSpeech {
            }

            return service.isLanguageAvailable(language, country, loc.getVariant());
            }
        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
    }

    /**
     * Synthesizes the given text to a ParcelFileDescriptor using the specified parameters.
     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
     * requests and then returns. The synthesis might not have finished (or even started!) at the
     * time when this method returns. In order to reliably detect errors during synthesis,
     * we recommend setting an utterance progress listener (see
     * {@link #setOnUtteranceProgressListener}).
     *
     * @param text The text that should be synthesized. No longer than
     *            {@link #getMaxSpeechInputLength()} characters.
     * @param params Parameters for the request. Can be null.
     *            Engine specific parameters may be passed in but the parameter keys
     *            must be prefixed by the name of the engine they are intended for. For example
     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the engine
     *            named "com.svox.pico" if it is being used.
     * @param fileDescriptor ParcelFileDescriptor to write the generated audio data to.
     * @param utteranceId An unique identifier for this request.
     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
     */
    public int synthesizeToFile(@NonNull final CharSequence text, @NonNull final Bundle params,
            @NonNull final ParcelFileDescriptor fileDescriptor, @NonNull final String utteranceId) {
        return runAction((ITextToSpeechService service) -> {
            return service.synthesizeToFileDescriptor(getCallerIdentity(), text,
                    fileDescriptor, getParams(params), utteranceId);
        }, ERROR, "synthesizeToFile");
    }

    /**
     * Synthesizes the given text to a file using the specified parameters.
     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
@@ -1829,22 +1809,17 @@ public class TextToSpeech {
     */
    public int synthesizeToFile(final CharSequence text, final Bundle params,
            final File file, final String utteranceId) {
        return runAction(new Action<Integer>() {
            @Override
            public Integer run(ITextToSpeechService service) throws RemoteException {
                ParcelFileDescriptor fileDescriptor;
                int returnValue;
                try {
        if (file.exists() && !file.canWrite()) {
            Log.e(TAG, "Can't write to " + file);
            return ERROR;
        }
                    fileDescriptor = ParcelFileDescriptor.open(file,
                            ParcelFileDescriptor.MODE_WRITE_ONLY |
                            ParcelFileDescriptor.MODE_CREATE |
                            ParcelFileDescriptor.MODE_TRUNCATE);
                    returnValue = service.synthesizeToFileDescriptor(getCallerIdentity(), text,
                            fileDescriptor, getParams(params), utteranceId);
        try (
            ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(file,
                ParcelFileDescriptor.MODE_WRITE_ONLY
                | ParcelFileDescriptor.MODE_CREATE
                | ParcelFileDescriptor.MODE_TRUNCATE);
        ) {
            int returnValue = synthesizeToFile(text, params, fileDescriptor, utteranceId);
            fileDescriptor.close();
            return returnValue;
        } catch (FileNotFoundException e) {
@@ -1855,8 +1830,6 @@ public class TextToSpeech {
            return ERROR;
        }
    }
        }, ERROR, "synthesizeToFile");
    }

    /**
     * Synthesizes the given text to a file using the specified parameters.