From 3d2d277a1094a0d686a9035b98750873ed7d3978 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Sat, 27 Jul 2013 18:30:34 +0100 Subject: [PATCH] Avoid MissingResourceException on calls to getISO3CountryCode. --- .../com/reecedunn/espeak/SpeechSynthesis.java | 9 +++++++-- android/src/com/reecedunn/espeak/Voice.java | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/android/src/com/reecedunn/espeak/SpeechSynthesis.java b/android/src/com/reecedunn/espeak/SpeechSynthesis.java index 8d3223f8..46b1a703 100644 --- a/android/src/com/reecedunn/espeak/SpeechSynthesis.java +++ b/android/src/com/reecedunn/espeak/SpeechSynthesis.java @@ -166,13 +166,18 @@ public class SpeechSynthesis { try { if (locale != null && !locale.getISO3Language().equals("")) { + // This will throw a MissingResourceException on Android 4.3, + // so check here to avoid the exception in the Android + // TextToSpeech class. + String country = locale.getISO3Country(); + final Voice voice = new Voice(name, identifier, gender, age, locale); voices.add(voice); } } catch (MissingResourceException e) { // Android 4.3 throws this exception if the 3-letter language - // code is missing for a locale (e.g. nci). Earlier versions - // of Android return an empty string. + // (e.g. nci) or country (e.g. 021) code is missing for a locale. + // Earlier versions of Android return an empty string. } } diff --git a/android/src/com/reecedunn/espeak/Voice.java b/android/src/com/reecedunn/espeak/Voice.java index 19907436..8d2da28a 100644 --- a/android/src/com/reecedunn/espeak/Voice.java +++ b/android/src/com/reecedunn/espeak/Voice.java @@ -18,6 +18,7 @@ package com.reecedunn.espeak; import java.util.Locale; +import java.util.MissingResourceException; import android.speech.tts.TextToSpeech; @@ -51,13 +52,18 @@ public class Voice { public int match(Locale query) { if (!locale.getISO3Language().equals(query.getISO3Language())) { return TextToSpeech.LANG_NOT_SUPPORTED; - } else if (!locale.getISO3Country().equals(query.getISO3Country())) { - return TextToSpeech.LANG_AVAILABLE; - } else if (!locale.getVariant().equals(query.getVariant())) { - return TextToSpeech.LANG_COUNTRY_AVAILABLE; - } else { - return TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE; } + try { + if (!locale.getISO3Country().equals(query.getISO3Country())) { + return TextToSpeech.LANG_AVAILABLE; + } + } catch (MissingResourceException e) { + return TextToSpeech.LANG_AVAILABLE; + } + if (!locale.getVariant().equals(query.getVariant())) { + return TextToSpeech.LANG_COUNTRY_AVAILABLE; + } + return TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE; } @Override