Don't strip country and variant from locale.

When the synthesizer returns LANG_AVAILABLE or LANG_COUNTRY_AVAILABLE
for a certain locale, we strip the country and variant from the locale
before asking the synthesizer for the default voice name for this
locale.

This can prevent the synthesizer from picking the right locale. For
instance it prevents the synthesizer from picking en-gb if en-au is
requested, now it can only have a single preferred voice for all English
countries.

The solution is not to strip the country and variant from the locale.
This is not likely to break anything, because the input to
onGetDefaultVoiceNameFor was never expected to be a subset of all
supported language-country-variant combinations. The default
implementation handles this correctly.

Change-Id: I017f69d321039c7aa7a3ef5a09ba138e35b66670
(cherry picked from commit 4758e99783493fc546e6819b5fe9d44e1ef8c318)
diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java
index cf29310..3eeb04a 100644
--- a/core/java/android/speech/tts/TextToSpeech.java
+++ b/core/java/android/speech/tts/TextToSpeech.java
@@ -1481,14 +1481,8 @@
                 // interface).
 
                 // Sanitize locale using isLanguageAvailable.
-                int result = service.isLanguageAvailable( language, country, variant);
-                if (result >= LANG_AVAILABLE){
-                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
-                        variant = "";
-                        if (result < LANG_COUNTRY_AVAILABLE) {
-                            country = "";
-                        }
-                    }
+                int result = service.isLanguageAvailable(language, country, variant);
+                if (result >= LANG_AVAILABLE) {
                     // Get the default voice for the locale.
                     String voiceName = service.getDefaultVoiceNameFor(language, country, variant);
                     if (TextUtils.isEmpty(voiceName)) {
@@ -1502,10 +1496,28 @@
                         return LANG_NOT_SUPPORTED;
                     }
 
+                    // Set the language/country/variant of the voice, so #getLanguage will return
+                    // the currently set voice locale when called.
+                    Voice voice = getVoice(service, voiceName);
+                    String voiceLanguage = "";
+                    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);
+                    }
+
+                    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);
+                    }
                     mParams.putString(Engine.KEY_PARAM_VOICE_NAME, voiceName);
-                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
-                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
-                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
+                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, voiceLanguage);
+                    mParams.putString(Engine.KEY_PARAM_COUNTRY, voiceCountry);
+                    mParams.putString(Engine.KEY_PARAM_VARIANT, voice.getLocale().getVariant());
                 }
                 return result;
             }
@@ -1654,20 +1666,32 @@
                 if (TextUtils.isEmpty(voiceName)) {
                     return null;
                 }
-                List<Voice> voices = service.getVoices();
-                if (voices == null) {
-                    return null;
-                }
-                for (Voice voice : voices) {
-                    if (voice.getName().equals(voiceName)) {
-                        return voice;
-                    }
-                }
-                return null;
+                return getVoice(service, voiceName);
             }
         }, null, "getVoice");
     }
 
+
+    /**
+     * Returns a Voice instance of the voice with the given voice name.
+     *
+     * @return Voice instance with the given voice name, or {@code null} if not set or on error.
+     *
+     * @see Voice
+     */
+    private Voice getVoice(ITextToSpeechService service, String voiceName) throws RemoteException {
+        List<Voice> voices = service.getVoices();
+        if (voices == null) {
+            return null;
+        }
+        for (Voice voice : voices) {
+            if (voice.getName().equals(voiceName)) {
+                return voice;
+            }
+        }
+        return null;
+    }
+
     /**
      * Returns a Voice instance that's the default voice for the default Text-to-speech language.
      * @return The default voice instance for the default language, or {@code null} if not set or
@@ -1690,14 +1714,7 @@
 
                 // Sanitize the locale using isLanguageAvailable.
                 int result = service.isLanguageAvailable(language, country, variant);
-                if (result >= LANG_AVAILABLE){
-                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
-                        variant = "";
-                        if (result < LANG_COUNTRY_AVAILABLE) {
-                            country = "";
-                        }
-                    }
-                } else {
+                if (result < LANG_AVAILABLE) {
                     // The default language is not supported.
                     return null;
                 }