Set default language in new TTS clients.

A recent change altered semantics of getLanguage call to return client
language instead of service language. This solved problems
with interferences between two clients using different lanaguages.

This change created a bug - new TTS client instance have no language set.
Since reading user preferences requires additional permissions I've
added new tts service method - getClientDefaultLanguage that will return
user preferences.

I've also added new client method, getDefaultLanguage, that allow easy
access to this data.

Bug: 7666482
Change-Id: Ieb7d2ba3a99d20c513add97f054874720a1cd82e
diff --git a/core/java/android/speech/tts/ITextToSpeechService.aidl b/core/java/android/speech/tts/ITextToSpeechService.aidl
index 580d52c..6982029 100644
--- a/core/java/android/speech/tts/ITextToSpeechService.aidl
+++ b/core/java/android/speech/tts/ITextToSpeechService.aidl
@@ -97,7 +97,19 @@
      *         be empty too.
      */
     String[] getLanguage();
-
+    
+    /**
+     * Returns a default TTS language, country and variant as set by the user.
+     *
+     * Can be called from multiple threads.
+     *
+     * @return A 3-element array, containing language (ISO 3-letter code),
+     *         country (ISO 3-letter code) and variant used by the engine.
+     *         The country and variant may be {@code ""}. If country is empty, then variant must
+     *         be empty too.
+     */
+    String[] getClientDefaultLanguage();
+    
     /**
      * Checks whether the engine supports a given language.
      *
diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java
index cb5e0cd..30a8626 100644
--- a/core/java/android/speech/tts/TextToSpeech.java
+++ b/core/java/android/speech/tts/TextToSpeech.java
@@ -1024,6 +1024,24 @@
     }
 
     /**
+     * Returns a Locale instance describing the language currently being used as the default
+     * Text-to-speech language.
+     *
+     * @return language, country (if any) and variant (if any) used by the client stored in a
+     *     Locale instance, or {@code null} on error.
+     */
+    public Locale getDefaultLanguage() {
+        return runAction(new Action<Locale>() {
+            @Override
+            public Locale run(ITextToSpeechService service) throws RemoteException {
+                String[] defaultLanguage = service.getClientDefaultLanguage();
+
+                return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
+            }
+        }, null, "getDefaultLanguage");
+    }
+
+    /**
      * Sets the text-to-speech language.
      * The TTS engine will try to use the closest match to the specified
      * language as represented by the Locale, but there is no guarantee that the exact same Locale
@@ -1338,7 +1356,13 @@
 
                     try {
                         mConnectedService.setCallback(getCallerIdentity(), mCallback);
-                        Log.i(TAG, "Setuped connection to " + mName);
+                        String[] defaultLanguage = mConnectedService.getClientDefaultLanguage();
+
+                        mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]);
+                        mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]);
+                        mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]);
+
+                        Log.i(TAG, "Set up connection to " + mName);
                         return SUCCESS;
                     } catch (RemoteException re) {
                         Log.e(TAG, "Error connecting to service, setCallback() failed");
diff --git a/core/java/android/speech/tts/TextToSpeechService.java b/core/java/android/speech/tts/TextToSpeechService.java
index 458350d9..4054740 100644
--- a/core/java/android/speech/tts/TextToSpeechService.java
+++ b/core/java/android/speech/tts/TextToSpeechService.java
@@ -850,6 +850,11 @@
             return onGetLanguage();
         }
 
+        @Override
+        public String[] getClientDefaultLanguage() {
+            return getSettingsLocale();
+        }
+
         /*
          * If defaults are enforced, then no language is "available" except
          * perhaps the default language selected by the user.