From 7b42c6ea1b27701fa26134775afa031ba755e0b1 Mon Sep 17 00:00:00 2001 From: Josh Imbriani Date: Thu, 3 Oct 2019 14:42:42 -0700 Subject: [PATCH] Adding new overloads for addEarcon and addSpeech that takes Uri Bug: 139179420 Bug: 139180464 Test: Ran CTS test Test: "run cts -m CtsSpeechTestCases" Change-Id: Ie4cbd8aa91805804e099027b2404162e651323ca --- api/current.txt | 2 + .../java/android/speech/tts/TextToSpeech.java | 67 ++++++++++++------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/api/current.txt b/api/current.txt index 26c90dff3c6b0..add8149763428 100644 --- a/api/current.txt +++ b/api/current.txt @@ -42260,10 +42260,12 @@ package android.speech.tts { method public int addEarcon(String, String, @RawRes int); method @Deprecated public int addEarcon(String, String); method public int addEarcon(String, java.io.File); + method public int addEarcon(@NonNull String, @NonNull android.net.Uri); method public int addSpeech(String, String, @RawRes int); method public int addSpeech(CharSequence, String, @RawRes int); method public int addSpeech(String, String); method public int addSpeech(CharSequence, java.io.File); + method public int addSpeech(@NonNull CharSequence, @NonNull android.net.Uri); method @Deprecated public boolean areDefaultsEnforced(); method public java.util.Set getAvailableLanguages(); method public String getDefaultEngine(); diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java index 44446ad04edda..84cc79dbe9a68 100644 --- a/core/java/android/speech/tts/TextToSpeech.java +++ b/core/java/android/speech/tts/TextToSpeech.java @@ -904,10 +904,7 @@ public class TextToSpeech { * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. */ public int addSpeech(String text, String packagename, @RawRes int resourceId) { - synchronized (mStartLock) { - mUtterances.put(text, makeResourceUri(packagename, resourceId)); - return SUCCESS; - } + return addSpeech(text, makeResourceUri(packagename, resourceId)); } /** @@ -938,10 +935,7 @@ public class TextToSpeech { * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. */ public int addSpeech(CharSequence text, String packagename, @RawRes int resourceId) { - synchronized (mStartLock) { - mUtterances.put(text, makeResourceUri(packagename, resourceId)); - return SUCCESS; - } + return addSpeech(text, makeResourceUri(packagename, resourceId)); } /** @@ -959,14 +953,11 @@ public class TextToSpeech { * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. */ public int addSpeech(String text, String filename) { - synchronized (mStartLock) { - mUtterances.put(text, Uri.parse(filename)); - return SUCCESS; - } + return addSpeech(text, Uri.parse(filename)); } /** - * Adds a mapping between a CharSequence (may be spanned with TtsSpans and a sound file. + * Adds a mapping between a CharSequence (may be spanned with TtsSpans) and a sound file. * Using this, it is possible to add custom pronounciations for a string of text. After a call * to this method, subsequent calls to {@link #speak(CharSequence, int, Bundle, String)} * will play the specified sound resource if it is available, or synthesize the text it is @@ -980,8 +971,26 @@ public class TextToSpeech { * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. */ public int addSpeech(CharSequence text, File file) { + return addSpeech(text, Uri.fromFile(file)); + } + + /** + * Adds a mapping between a CharSequence (may be spanned with TtsSpans) and a sound file. + * Using this, it is possible to add custom pronounciations for a string of text. After a call + * to this method, subsequent calls to {@link #speak(CharSequence, int, Bundle, String)} + * will play the specified sound resource if it is available, or synthesize the text it is + * missing. + * + * @param text + * The string of text. Example: "south_south_east" + * @param uri + * Uri object pointing to the sound file. + * + * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. + */ + public int addSpeech(@NonNull CharSequence text, @NonNull Uri uri) { synchronized (mStartLock) { - mUtterances.put(text, Uri.fromFile(file)); + mUtterances.put(text, uri); return SUCCESS; } } @@ -1012,10 +1021,7 @@ public class TextToSpeech { * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. */ public int addEarcon(String earcon, String packagename, @RawRes int resourceId) { - synchronized(mStartLock) { - mEarcons.put(earcon, makeResourceUri(packagename, resourceId)); - return SUCCESS; - } + return addEarcon(earcon, makeResourceUri(packagename, resourceId)); } /** @@ -1038,10 +1044,7 @@ public class TextToSpeech { */ @Deprecated public int addEarcon(String earcon, String filename) { - synchronized(mStartLock) { - mEarcons.put(earcon, Uri.parse(filename)); - return SUCCESS; - } + return addEarcon(earcon, Uri.parse(filename)); } /** @@ -1059,8 +1062,26 @@ public class TextToSpeech { * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. */ public int addEarcon(String earcon, File file) { + return addEarcon(earcon, Uri.fromFile(file)); + } + + /** + * Adds a mapping between a string of text and a sound file. + * Use this to add custom earcons. + * + * @see #playEarcon(String, int, HashMap) + * + * @param earcon + * The name of the earcon. + * Example: "[tick]" + * @param uri + * Uri object pointing to the sound file. + * + * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. + */ + public int addEarcon(@NonNull String earcon, @NonNull Uri uri) { synchronized(mStartLock) { - mEarcons.put(earcon, Uri.fromFile(file)); + mEarcons.put(earcon, uri); return SUCCESS; } }