summaryrefslogtreecommitdiffstats
path: root/core/java/android/speech
diff options
context:
space:
mode:
authorPrzemyslaw Szczepaniak <pszczepaniak@google.com>2013-09-20 15:29:25 +0100
committerPrzemyslaw Szczepaniak <pszczepaniak@google.com>2013-10-30 10:21:34 +0000
commitca4f1fc57c86fb7e6094ba4117a027fffd79d227 (patch)
tree8fb2b1b6dba40a6010232671578213336cd2e249 /core/java/android/speech
parent0e29faacecdb728e2878c799da8532e33539f046 (diff)
downloadframeworks_base-ca4f1fc57c86fb7e6094ba4117a027fffd79d227.zip
frameworks_base-ca4f1fc57c86fb7e6094ba4117a027fffd79d227.tar.gz
frameworks_base-ca4f1fc57c86fb7e6094ba4117a027fffd79d227.tar.bz2
Catch MissingResourceException caused by bad locale in TTS.
Malformed locale objects will throw MissingResourceException from getISO3Country() and getISO3Language() methods. This change guard against the case where device default locale is malformed and user tries to get TTS default locale (b/10814409) and case where user tries to pass bad locale as argument to TextToSpeech.getFeatures method (b/10494193). Bug:11429552 Bug:10494193 Bug:10814409 Change-Id: Ie54d5d30bb042324da0c0d19b77c3a932c5642f7 (cherry picked from commit 58f16653728417100c61b5cb63298e0e3bb528dc)
Diffstat (limited to 'core/java/android/speech')
-rw-r--r--core/java/android/speech/tts/TextToSpeech.java10
-rw-r--r--core/java/android/speech/tts/TtsEngines.java45
2 files changed, 35 insertions, 20 deletions
diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java
index b808363..2752085 100644
--- a/core/java/android/speech/tts/TextToSpeech.java
+++ b/core/java/android/speech/tts/TextToSpeech.java
@@ -993,8 +993,16 @@ public class TextToSpeech {
return runAction(new Action<Set<String>>() {
@Override
public Set<String> run(ITextToSpeechService service) throws RemoteException {
- String[] features = service.getFeaturesForLanguage(
+ 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);
+ return null;
+ }
+
if (features != null) {
final Set<String> featureSet = new HashSet<String>();
Collections.addAll(featureSet, features);
diff --git a/core/java/android/speech/tts/TtsEngines.java b/core/java/android/speech/tts/TtsEngines.java
index 5fbd22e..4f996cd 100644
--- a/core/java/android/speech/tts/TtsEngines.java
+++ b/core/java/android/speech/tts/TtsEngines.java
@@ -44,6 +44,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
+import java.util.MissingResourceException;
/**
* Support class for querying the list of available engines
@@ -369,28 +370,34 @@ public class TtsEngines {
public String getDefaultLocale() {
final Locale locale = Locale.getDefault();
- // Note that the default locale might have an empty variant
- // or language, and we take care that the construction is
- // the same as {@link #getV1Locale} i.e no trailing delimiters
- // or spaces.
- String defaultLocale = locale.getISO3Language();
- if (TextUtils.isEmpty(defaultLocale)) {
- Log.w(TAG, "Default locale is empty.");
- return "";
- }
+ try {
+ // Note that the default locale might have an empty variant
+ // or language, and we take care that the construction is
+ // the same as {@link #getV1Locale} i.e no trailing delimiters
+ // or spaces.
+ String defaultLocale = locale.getISO3Language();
+ if (TextUtils.isEmpty(defaultLocale)) {
+ Log.w(TAG, "Default locale is empty.");
+ return "";
+ }
+
+ if (!TextUtils.isEmpty(locale.getISO3Country())) {
+ defaultLocale += LOCALE_DELIMITER + locale.getISO3Country();
+ } else {
+ // Do not allow locales of the form lang--variant with
+ // an empty country.
+ return defaultLocale;
+ }
+ if (!TextUtils.isEmpty(locale.getVariant())) {
+ defaultLocale += LOCALE_DELIMITER + locale.getVariant();
+ }
- if (!TextUtils.isEmpty(locale.getISO3Country())) {
- defaultLocale += LOCALE_DELIMITER + locale.getISO3Country();
- } else {
- // Do not allow locales of the form lang--variant with
- // an empty country.
return defaultLocale;
+ } catch (MissingResourceException e) {
+ // Default locale does not have a ISO 3166 and/or ISO 639-2/T codes. Return the
+ // default "eng-usa" (that would be the result of Locale.getDefault() == Locale.US).
+ return "eng-usa";
}
- if (!TextUtils.isEmpty(locale.getVariant())) {
- defaultLocale += LOCALE_DELIMITER + locale.getVariant();
- }
-
- return defaultLocale;
}
/**