1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package android.speech.tts;
17 
18 import org.xmlpull.v1.XmlPullParserException;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.content.pm.ResolveInfo;
26 import android.content.pm.ServiceInfo;
27 import android.content.res.Resources;
28 import android.content.res.TypedArray;
29 import android.content.res.XmlResourceParser;
30 
31 import static android.provider.Settings.Secure.getString;
32 
33 import android.provider.Settings;
34 import android.speech.tts.TextToSpeech.Engine;
35 import android.speech.tts.TextToSpeech.EngineInfo;
36 import android.text.TextUtils;
37 import android.util.AttributeSet;
38 import android.util.Log;
39 import android.util.Xml;
40 
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.Comparator;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Locale;
48 import java.util.Map;
49 import java.util.MissingResourceException;
50 
51 /**
52  * Support class for querying the list of available engines
53  * on the device and deciding which one to use etc.
54  *
55  * Comments in this class the use the shorthand "system engines" for engines that
56  * are a part of the system image.
57  *
58  * This class is thread-safe/
59  *
60  * @hide
61  */
62 public class TtsEngines {
63     private static final String TAG = "TtsEngines";
64     private static final boolean DBG = false;
65 
66     /** Locale delimiter used by the old-style 3 char locale string format (like "eng-usa") */
67     private static final String LOCALE_DELIMITER_OLD = "-";
68 
69     /** Locale delimiter used by the new-style locale string format (Locale.toString() results,
70      * like "en_US") */
71     private static final String LOCALE_DELIMITER_NEW = "_";
72 
73     private final Context mContext;
74 
75     /** Mapping of various language strings to the normalized Locale form */
76     private static final Map<String, String> sNormalizeLanguage;
77 
78     /** Mapping of various country strings to the normalized Locale form */
79     private static final Map<String, String> sNormalizeCountry;
80 
81     // Populate the sNormalize* maps
82     static {
83         HashMap<String, String> normalizeLanguage = new HashMap<String, String>();
84         for (String language : Locale.getISOLanguages()) {
85             try {
normalizeLanguage.put(new Locale(language).getISO3Language(), language)86                 normalizeLanguage.put(new Locale(language).getISO3Language(), language);
87             } catch (MissingResourceException e) {
88                 continue;
89             }
90         }
91         sNormalizeLanguage = Collections.unmodifiableMap(normalizeLanguage);
92 
93         HashMap<String, String> normalizeCountry = new HashMap<String, String>();
94         for (String country : Locale.getISOCountries()) {
95             try {
normalizeCountry.put(new Locale("", country).getISO3Country(), country)96                 normalizeCountry.put(new Locale("", country).getISO3Country(), country);
97             } catch (MissingResourceException e) {
98                 continue;
99             }
100         }
101         sNormalizeCountry = Collections.unmodifiableMap(normalizeCountry);
102     }
103 
TtsEngines(Context ctx)104     public TtsEngines(Context ctx) {
105         mContext = ctx;
106     }
107 
108     /**
109      * @return the default TTS engine. If the user has set a default, and the engine
110      *         is available on the device, the default is returned. Otherwise,
111      *         the highest ranked engine is returned as per {@link EngineInfoComparator}.
112      */
getDefaultEngine()113     public String getDefaultEngine() {
114         String engine = getString(mContext.getContentResolver(),
115                 Settings.Secure.TTS_DEFAULT_SYNTH);
116         return isEngineInstalled(engine) ? engine : getHighestRankedEngineName();
117     }
118 
119     /**
120      * @return the package name of the highest ranked system engine, {@code null}
121      *         if no TTS engines were present in the system image.
122      */
getHighestRankedEngineName()123     public String getHighestRankedEngineName() {
124         final List<EngineInfo> engines = getEngines();
125 
126         if (engines.size() > 0 && engines.get(0).system) {
127             return engines.get(0).name;
128         }
129 
130         return null;
131     }
132 
133     /**
134      * Returns the engine info for a given engine name. Note that engines are
135      * identified by their package name.
136      */
getEngineInfo(String packageName)137     public EngineInfo getEngineInfo(String packageName) {
138         PackageManager pm = mContext.getPackageManager();
139         Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
140         intent.setPackage(packageName);
141         List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent,
142                 PackageManager.MATCH_DEFAULT_ONLY);
143         // Note that the current API allows only one engine per
144         // package name. Since the "engine name" is the same as
145         // the package name.
146         if (resolveInfos != null && resolveInfos.size() == 1) {
147             return getEngineInfo(resolveInfos.get(0), pm);
148         }
149 
150         return null;
151     }
152 
153     /**
154      * Gets a list of all installed TTS engines.
155      *
156      * @return A list of engine info objects. The list can be empty, but never {@code null}.
157      */
getEngines()158     public List<EngineInfo> getEngines() {
159         PackageManager pm = mContext.getPackageManager();
160         Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
161         List<ResolveInfo> resolveInfos =
162                 pm.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY);
163         if (resolveInfos == null) return Collections.emptyList();
164 
165         List<EngineInfo> engines = new ArrayList<EngineInfo>(resolveInfos.size());
166 
167         for (ResolveInfo resolveInfo : resolveInfos) {
168             EngineInfo engine = getEngineInfo(resolveInfo, pm);
169             if (engine != null) {
170                 engines.add(engine);
171             }
172         }
173         Collections.sort(engines, EngineInfoComparator.INSTANCE);
174 
175         return engines;
176     }
177 
isSystemEngine(ServiceInfo info)178     private boolean isSystemEngine(ServiceInfo info) {
179         final ApplicationInfo appInfo = info.applicationInfo;
180         return appInfo != null && (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
181     }
182 
183     /**
184      * @return true if a given engine is installed on the system.
185      */
isEngineInstalled(String engine)186     public boolean isEngineInstalled(String engine) {
187         if (engine == null) {
188             return false;
189         }
190 
191         return getEngineInfo(engine) != null;
192     }
193 
194     /**
195      * @return an intent that can launch the settings activity for a given tts engine.
196      */
getSettingsIntent(String engine)197     public Intent getSettingsIntent(String engine) {
198         PackageManager pm = mContext.getPackageManager();
199         Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
200         intent.setPackage(engine);
201         List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent,
202                 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
203         // Note that the current API allows only one engine per
204         // package name. Since the "engine name" is the same as
205         // the package name.
206         if (resolveInfos != null && resolveInfos.size() == 1) {
207             ServiceInfo service = resolveInfos.get(0).serviceInfo;
208             if (service != null) {
209                 final String settings = settingsActivityFromServiceInfo(service, pm);
210                 if (settings != null) {
211                     Intent i = new Intent();
212                     i.setClassName(engine, settings);
213                     return i;
214                 }
215             }
216         }
217 
218         return null;
219     }
220 
221     /**
222      * The name of the XML tag that text to speech engines must use to
223      * declare their meta data.
224      *
225      * {@link com.android.internal.R.styleable#TextToSpeechEngine}
226      */
227     private static final String XML_TAG_NAME = "tts-engine";
228 
settingsActivityFromServiceInfo(ServiceInfo si, PackageManager pm)229     private String settingsActivityFromServiceInfo(ServiceInfo si, PackageManager pm) {
230         XmlResourceParser parser = null;
231         try {
232             parser = si.loadXmlMetaData(pm, TextToSpeech.Engine.SERVICE_META_DATA);
233             if (parser == null) {
234                 Log.w(TAG, "No meta-data found for :" + si);
235                 return null;
236             }
237 
238             final Resources res = pm.getResourcesForApplication(si.applicationInfo);
239 
240             int type;
241             while ((type = parser.next()) != XmlResourceParser.END_DOCUMENT) {
242                 if (type == XmlResourceParser.START_TAG) {
243                     if (!XML_TAG_NAME.equals(parser.getName())) {
244                         Log.w(TAG, "Package " + si + " uses unknown tag :"
245                                 + parser.getName());
246                         return null;
247                     }
248 
249                     final AttributeSet attrs = Xml.asAttributeSet(parser);
250                     final TypedArray array = res.obtainAttributes(attrs,
251                             com.android.internal.R.styleable.TextToSpeechEngine);
252                     final String settings = array.getString(
253                             com.android.internal.R.styleable.TextToSpeechEngine_settingsActivity);
254                     array.recycle();
255 
256                     return settings;
257                 }
258             }
259 
260             return null;
261         } catch (NameNotFoundException e) {
262             Log.w(TAG, "Could not load resources for : " + si);
263             return null;
264         } catch (XmlPullParserException e) {
265             Log.w(TAG, "Error parsing metadata for " + si + ":" + e);
266             return null;
267         } catch (IOException e) {
268             Log.w(TAG, "Error parsing metadata for " + si + ":" + e);
269             return null;
270         } finally {
271             if (parser != null) {
272                 parser.close();
273             }
274         }
275     }
276 
getEngineInfo(ResolveInfo resolve, PackageManager pm)277     private EngineInfo getEngineInfo(ResolveInfo resolve, PackageManager pm) {
278         ServiceInfo service = resolve.serviceInfo;
279         if (service != null) {
280             EngineInfo engine = new EngineInfo();
281             // Using just the package name isn't great, since it disallows having
282             // multiple engines in the same package, but that's what the existing API does.
283             engine.name = service.packageName;
284             CharSequence label = service.loadLabel(pm);
285             engine.label = TextUtils.isEmpty(label) ? engine.name : label.toString();
286             engine.icon = service.getIconResource();
287             engine.priority = resolve.priority;
288             engine.system = isSystemEngine(service);
289             return engine;
290         }
291 
292         return null;
293     }
294 
295     private static class EngineInfoComparator implements Comparator<EngineInfo> {
EngineInfoComparator()296         private EngineInfoComparator() { }
297 
298         static EngineInfoComparator INSTANCE = new EngineInfoComparator();
299 
300         /**
301          * Engines that are a part of the system image are always lesser
302          * than those that are not. Within system engines / non system engines
303          * the engines are sorted in order of their declared priority.
304          */
305         @Override
compare(EngineInfo lhs, EngineInfo rhs)306         public int compare(EngineInfo lhs, EngineInfo rhs) {
307             if (lhs.system && !rhs.system) {
308                 return -1;
309             } else if (rhs.system && !lhs.system) {
310                 return 1;
311             } else {
312                 // Either both system engines, or both non system
313                 // engines.
314                 //
315                 // Note, this isn't a typo. Higher priority numbers imply
316                 // higher priority, but are "lower" in the sort order.
317                 return rhs.priority - lhs.priority;
318             }
319         }
320     }
321 
322     /**
323      * Returns the default locale for a given TTS engine. Attempts to read the
324      * value from {@link Settings.Secure#TTS_DEFAULT_LOCALE}, failing which the
325      * default phone locale is returned.
326      *
327      * @param engineName the engine to return the locale for.
328      * @return the locale preference for this engine. Will be non null.
329      */
getLocalePrefForEngine(String engineName)330     public Locale getLocalePrefForEngine(String engineName) {
331         return getLocalePrefForEngine(engineName,
332                 getString(mContext.getContentResolver(), Settings.Secure.TTS_DEFAULT_LOCALE));
333     }
334 
335     /**
336      * Returns the default locale for a given TTS engine from given settings string. */
getLocalePrefForEngine(String engineName, String prefValue)337     public Locale getLocalePrefForEngine(String engineName, String prefValue) {
338         String localeString = parseEnginePrefFromList(
339                 prefValue,
340                 engineName);
341 
342         if (TextUtils.isEmpty(localeString)) {
343             // The new style setting is unset, attempt to return the old style setting.
344             return Locale.getDefault();
345         }
346 
347         Locale result = parseLocaleString(localeString);
348         if (result == null) {
349             Log.w(TAG, "Failed to parse locale " + localeString + ", returning en_US instead");
350             result = Locale.US;
351         }
352 
353         if (DBG) Log.d(TAG, "getLocalePrefForEngine(" + engineName + ")= " + result);
354 
355         return result;
356     }
357 
358 
359     /**
360      * True if a given TTS engine uses the default phone locale as a default locale. Attempts to
361      * read the value from {@link Settings.Secure#TTS_DEFAULT_LOCALE}. If
362      * its  value is empty, this methods returns true.
363      *
364      * @param engineName the engine to return the locale for.
365      */
isLocaleSetToDefaultForEngine(String engineName)366     public boolean isLocaleSetToDefaultForEngine(String engineName) {
367         return TextUtils.isEmpty(parseEnginePrefFromList(
368                     getString(mContext.getContentResolver(), Settings.Secure.TTS_DEFAULT_LOCALE),
369                     engineName));
370     }
371 
372     /**
373      * Parses a locale encoded as a string, and tries its best to return a valid {@link Locale}
374      * object, even if the input string is encoded using the old-style 3 character format e.g.
375      * "deu-deu". At the end, we test if the resulting locale can return ISO3 language and
376      * country codes ({@link Locale#getISO3Language()} and {@link Locale#getISO3Country()}),
377      * if it fails to do so, we return null.
378      */
parseLocaleString(String localeString)379     public Locale parseLocaleString(String localeString) {
380         String language = "", country = "", variant = "";
381         if (!TextUtils.isEmpty(localeString)) {
382             String[] split = localeString.split(
383                     "[" + LOCALE_DELIMITER_OLD + LOCALE_DELIMITER_NEW + "]");
384             language = split[0].toLowerCase();
385             if (split.length == 0) {
386                 Log.w(TAG, "Failed to convert " + localeString + " to a valid Locale object. Only" +
387                             " separators");
388                 return null;
389             }
390             if (split.length > 3) {
391                 Log.w(TAG, "Failed to convert " + localeString + " to a valid Locale object. Too" +
392                         " many separators");
393                 return null;
394             }
395             if (split.length >= 2) {
396                 country = split[1].toUpperCase();
397             }
398             if (split.length >= 3) {
399                 variant = split[2];
400             }
401 
402         }
403 
404         String normalizedLanguage = sNormalizeLanguage.get(language);
405         if (normalizedLanguage != null) {
406             language = normalizedLanguage;
407         }
408 
409         String normalizedCountry= sNormalizeCountry.get(country);
410         if (normalizedCountry != null) {
411             country = normalizedCountry;
412         }
413 
414         if (DBG) Log.d(TAG, "parseLocalePref(" + language + "," + country +
415                 "," + variant +")");
416 
417         Locale result = new Locale(language, country, variant);
418         try {
419             result.getISO3Language();
420             result.getISO3Country();
421             return result;
422         } catch(MissingResourceException e) {
423             Log.w(TAG, "Failed to convert " + localeString + " to a valid Locale object.");
424             return null;
425         }
426     }
427 
428     /**
429      * This method tries its best to return a valid {@link Locale} object from the TTS-specific
430      * Locale input (returned by {@link TextToSpeech#getLanguage}
431      * and {@link TextToSpeech#getDefaultLanguage}). A TTS Locale language field contains
432      * a three-letter ISO 639-2/T code (where a proper Locale would use a two-letter ISO 639-1
433      * code), and the country field contains a three-letter ISO 3166 country code (where a proper
434      * Locale would use a two-letter ISO 3166-1 code).
435      *
436      * This method tries to convert three-letter language and country codes into their two-letter
437      * equivalents. If it fails to do so, it keeps the value from the TTS locale.
438      */
normalizeTTSLocale(Locale ttsLocale)439     public static Locale normalizeTTSLocale(Locale ttsLocale) {
440         String language = ttsLocale.getLanguage();
441         if (!TextUtils.isEmpty(language)) {
442             String normalizedLanguage = sNormalizeLanguage.get(language);
443             if (normalizedLanguage != null) {
444                 language = normalizedLanguage;
445             }
446         }
447 
448         String country = ttsLocale.getCountry();
449         if (!TextUtils.isEmpty(country)) {
450             String normalizedCountry= sNormalizeCountry.get(country);
451             if (normalizedCountry != null) {
452                 country = normalizedCountry;
453             }
454         }
455         return new Locale(language, country, ttsLocale.getVariant());
456     }
457 
458     /**
459      * Return the old-style string form of the locale. It consists of 3 letter codes:
460      * <ul>
461      *   <li>"ISO 639-2/T language code" if the locale has no country entry</li>
462      *   <li> "ISO 639-2/T language code{@link #LOCALE_DELIMITER}ISO 3166 country code"
463      *     if the locale has no variant entry</li>
464      *   <li> "ISO 639-2/T language code{@link #LOCALE_DELIMITER}ISO 3166 country
465      *     code{@link #LOCALE_DELIMITER}variant" if the locale has a variant entry</li>
466      * </ul>
467      * If we fail to generate those codes using {@link Locale#getISO3Country()} and
468      * {@link Locale#getISO3Language()}, then we return new String[]{"eng","USA",""};
469      */
toOldLocaleStringFormat(Locale locale)470     static public String[] toOldLocaleStringFormat(Locale locale) {
471         String[] ret = new String[]{"","",""};
472         try {
473             // Note that the default locale might have an empty variant
474             // or language, and we take care that the construction is
475             // the same as {@link #getV1Locale} i.e no trailing delimiters
476             // or spaces.
477             ret[0] = locale.getISO3Language();
478             ret[1] = locale.getISO3Country();
479             ret[2] = locale.getVariant();
480 
481             return ret;
482         } catch (MissingResourceException e) {
483             // Default locale does not have a ISO 3166 and/or ISO 639-2/T codes. Return the
484             // default "eng-usa" (that would be the result of Locale.getDefault() == Locale.US).
485             return new String[]{"eng","USA",""};
486         }
487     }
488 
489     /**
490      * Parses a comma separated list of engine locale preferences. The list is of the
491      * form {@code "engine_name_1:locale_1,engine_name_2:locale2"} and so on and
492      * so forth. Returns null if the list is empty, malformed or if there is no engine
493      * specific preference in the list.
494      */
parseEnginePrefFromList(String prefValue, String engineName)495     private static String parseEnginePrefFromList(String prefValue, String engineName) {
496         if (TextUtils.isEmpty(prefValue)) {
497             return null;
498         }
499 
500         String[] prefValues = prefValue.split(",");
501 
502         for (String value : prefValues) {
503             final int delimiter = value.indexOf(':');
504             if (delimiter > 0) {
505                 if (engineName.equals(value.substring(0, delimiter))) {
506                     return value.substring(delimiter + 1);
507                 }
508             }
509         }
510 
511         return null;
512     }
513 
514     /**
515      * Serialize the locale to a string and store it as a default locale for the given engine. If
516      * the passed locale is null, an empty string will be serialized; that empty string, when
517      * read back, will evaluate to {@link Locale#getDefault()}.
518      */
updateLocalePrefForEngine(String engineName, Locale newLocale)519     public synchronized void updateLocalePrefForEngine(String engineName, Locale newLocale) {
520         final String prefList = Settings.Secure.getString(mContext.getContentResolver(),
521                 Settings.Secure.TTS_DEFAULT_LOCALE);
522         if (DBG) {
523             Log.d(TAG, "updateLocalePrefForEngine(" + engineName + ", " + newLocale +
524                     "), originally: " + prefList);
525         }
526 
527         final String newPrefList = updateValueInCommaSeparatedList(prefList,
528                 engineName, (newLocale != null) ? newLocale.toString() : "");
529 
530         if (DBG) Log.d(TAG, "updateLocalePrefForEngine(), writing: " + newPrefList.toString());
531 
532         Settings.Secure.putString(mContext.getContentResolver(),
533                 Settings.Secure.TTS_DEFAULT_LOCALE, newPrefList.toString());
534     }
535 
536     /**
537      * Updates the value for a given key in a comma separated list of key value pairs,
538      * each of which are delimited by a colon. If no value exists for the given key,
539      * the kay value pair are appended to the end of the list.
540      */
updateValueInCommaSeparatedList(String list, String key, String newValue)541     private String updateValueInCommaSeparatedList(String list, String key,
542             String newValue) {
543         StringBuilder newPrefList = new StringBuilder();
544         if (TextUtils.isEmpty(list)) {
545             // If empty, create a new list with a single entry.
546             newPrefList.append(key).append(':').append(newValue);
547         } else {
548             String[] prefValues = list.split(",");
549             // Whether this is the first iteration in the loop.
550             boolean first = true;
551             // Whether we found the given key.
552             boolean found = false;
553             for (String value : prefValues) {
554                 final int delimiter = value.indexOf(':');
555                 if (delimiter > 0) {
556                     if (key.equals(value.substring(0, delimiter))) {
557                         if (first) {
558                             first = false;
559                         } else {
560                             newPrefList.append(',');
561                         }
562                         found = true;
563                         newPrefList.append(key).append(':').append(newValue);
564                     } else {
565                         if (first) {
566                             first = false;
567                         } else {
568                             newPrefList.append(',');
569                         }
570                         // Copy across the entire key + value as is.
571                         newPrefList.append(value);
572                     }
573                 }
574             }
575 
576             if (!found) {
577                 // Not found, but the rest of the keys would have been copied
578                 // over already, so just append it to the end.
579                 newPrefList.append(',');
580                 newPrefList.append(key).append(':').append(newValue);
581             }
582         }
583 
584         return newPrefList.toString();
585     }
586 }
587