[go: nahoru, domu]

TextToSpeech.java revision 7b9c912f536925ac6ec43935d6e97506851b33d6
1/*
2 * Copyright (C) 2009 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 */
16package android.speech.tts;
17
18import android.annotation.RawRes;
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.content.ComponentName;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.ServiceConnection;
26import android.media.AudioAttributes;
27import android.media.AudioManager;
28import android.net.Uri;
29import android.os.AsyncTask;
30import android.os.Bundle;
31import android.os.IBinder;
32import android.os.ParcelFileDescriptor;
33import android.os.RemoteException;
34import android.provider.Settings;
35import android.text.TextUtils;
36import android.util.Log;
37
38import java.io.File;
39import java.io.FileNotFoundException;
40import java.io.IOException;
41import java.util.ArrayList;
42import java.util.Collections;
43import java.util.HashMap;
44import java.util.HashSet;
45import java.util.List;
46import java.util.Locale;
47import java.util.Map;
48import java.util.MissingResourceException;
49import java.util.Set;
50
51/**
52 *
53 * Synthesizes speech from text for immediate playback or to create a sound file.
54 * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
55 * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
56 * notified of the completion of the initialization.<br>
57 * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
58 * to release the native resources used by the TextToSpeech engine.
59 */
60public class TextToSpeech {
61
62    private static final String TAG = "TextToSpeech";
63
64    /**
65     * Denotes a successful operation.
66     */
67    public static final int SUCCESS = 0;
68    /**
69     * Denotes a generic operation failure.
70     */
71    public static final int ERROR = -1;
72
73    /**
74     * Denotes a stop requested by a client. It's used only on the service side of the API,
75     * client should never expect to see this result code.
76     */
77    public static final int STOPPED = -2;
78
79    /**
80     * Denotes a failure of a TTS engine to synthesize the given input.
81     */
82    public static final int ERROR_SYNTHESIS = -3;
83
84    /**
85     * Denotes a failure of a TTS service.
86     */
87    public static final int ERROR_SERVICE = -4;
88
89    /**
90     * Denotes a failure related to the output (audio device or a file).
91     */
92    public static final int ERROR_OUTPUT = -5;
93
94    /**
95     * Denotes a failure caused by a network connectivity problems.
96     */
97    public static final int ERROR_NETWORK = -6;
98
99    /**
100     * Denotes a failure caused by network timeout.
101     */
102    public static final int ERROR_NETWORK_TIMEOUT = -7;
103
104    /**
105     * Denotes a failure caused by an invalid request.
106     */
107    public static final int ERROR_INVALID_REQUEST = -8;
108
109    /**
110     * Denotes a failure caused by an unfinished download of the voice data.
111     * @see Engine#KEY_FEATURE_NOT_INSTALLED
112     */
113    public static final int ERROR_NOT_INSTALLED_YET = -9;
114
115    /**
116     * Queue mode where all entries in the playback queue (media to be played
117     * and text to be synthesized) are dropped and replaced by the new entry.
118     * Queues are flushed with respect to a given calling app. Entries in the queue
119     * from other callees are not discarded.
120     */
121    public static final int QUEUE_FLUSH = 0;
122    /**
123     * Queue mode where the new entry is added at the end of the playback queue.
124     */
125    public static final int QUEUE_ADD = 1;
126    /**
127     * Queue mode where the entire playback queue is purged. This is different
128     * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries
129     * from a given caller.
130     *
131     * @hide
132     */
133    static final int QUEUE_DESTROY = 2;
134
135    /**
136     * Denotes the language is available exactly as specified by the locale.
137     */
138    public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
139
140    /**
141     * Denotes the language is available for the language and country specified
142     * by the locale, but not the variant.
143     */
144    public static final int LANG_COUNTRY_AVAILABLE = 1;
145
146    /**
147     * Denotes the language is available for the language by the locale,
148     * but not the country and variant.
149     */
150    public static final int LANG_AVAILABLE = 0;
151
152    /**
153     * Denotes the language data is missing.
154     */
155    public static final int LANG_MISSING_DATA = -1;
156
157    /**
158     * Denotes the language is not supported.
159     */
160    public static final int LANG_NOT_SUPPORTED = -2;
161
162    /**
163     * Broadcast Action: The TextToSpeech synthesizer has completed processing
164     * of all the text in the speech queue.
165     *
166     * Note that this notifies callers when the <b>engine</b> has finished has
167     * processing text data. Audio playback might not have completed (or even started)
168     * at this point. If you wish to be notified when this happens, see
169     * {@link OnUtteranceCompletedListener}.
170     */
171    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
172    public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
173            "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
174
175    /**
176     * Interface definition of a callback to be invoked indicating the completion of the
177     * TextToSpeech engine initialization.
178     */
179    public interface OnInitListener {
180        /**
181         * Called to signal the completion of the TextToSpeech engine initialization.
182         *
183         * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
184         */
185        public void onInit(int status);
186    }
187
188    /**
189     * Listener that will be called when the TTS service has
190     * completed synthesizing an utterance. This is only called if the utterance
191     * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}).
192     *
193     * @deprecated Use {@link UtteranceProgressListener} instead.
194     */
195    @Deprecated
196    public interface OnUtteranceCompletedListener {
197        /**
198         * Called when an utterance has been synthesized.
199         *
200         * @param utteranceId the identifier of the utterance.
201         */
202        public void onUtteranceCompleted(String utteranceId);
203    }
204
205    /**
206     * Constants and parameter names for controlling text-to-speech. These include:
207     *
208     * <ul>
209     *     <li>
210     *         Intents to ask engine to install data or check its data and
211     *         extras for a TTS engine's check data activity.
212     *     </li>
213     *     <li>
214     *         Keys for the parameters passed with speak commands, e.g.
215     *         {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}.
216     *     </li>
217     *     <li>
218     *         A list of feature strings that engines might support, e.g
219     *         {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to
220     *         {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify
221     *         engine behaviour. The engine can be queried for the set of features it supports
222     *         through {@link TextToSpeech#getFeatures(java.util.Locale)}.
223     *     </li>
224     * </ul>
225     */
226    public class Engine {
227
228        /**
229         * Default speech rate.
230         * @hide
231         */
232        public static final int DEFAULT_RATE = 100;
233
234        /**
235         * Default pitch.
236         * @hide
237         */
238        public static final int DEFAULT_PITCH = 100;
239
240        /**
241         * Default volume.
242         * @hide
243         */
244        public static final float DEFAULT_VOLUME = 1.0f;
245
246        /**
247         * Default pan (centered).
248         * @hide
249         */
250        public static final float DEFAULT_PAN = 0.0f;
251
252        /**
253         * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}.
254         * @hide
255         */
256        public static final int USE_DEFAULTS = 0; // false
257
258        /**
259         * Package name of the default TTS engine.
260         *
261         * @hide
262         * @deprecated No longer in use, the default engine is determined by
263         *         the sort order defined in {@link TtsEngines}. Note that
264         *         this doesn't "break" anything because there is no guarantee that
265         *         the engine specified below is installed on a given build, let
266         *         alone be the default.
267         */
268        @Deprecated
269        public static final String DEFAULT_ENGINE = "com.svox.pico";
270
271        /**
272         * Default audio stream used when playing synthesized speech.
273         */
274        public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
275
276        /**
277         * Indicates success when checking the installation status of the resources used by the
278         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
279         */
280        public static final int CHECK_VOICE_DATA_PASS = 1;
281
282        /**
283         * Indicates failure when checking the installation status of the resources used by the
284         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
285         */
286        public static final int CHECK_VOICE_DATA_FAIL = 0;
287
288        /**
289         * Indicates erroneous data when checking the installation status of the resources used by
290         * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
291         *
292         * @deprecated Use CHECK_VOICE_DATA_FAIL instead.
293         */
294        @Deprecated
295        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
296
297        /**
298         * Indicates missing resources when checking the installation status of the resources used
299         * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
300         *
301         * @deprecated Use CHECK_VOICE_DATA_FAIL instead.
302         */
303        @Deprecated
304        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
305
306        /**
307         * Indicates missing storage volume when checking the installation status of the resources
308         * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
309         *
310         * @deprecated Use CHECK_VOICE_DATA_FAIL instead.
311         */
312        @Deprecated
313        public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
314
315        /**
316         * Intent for starting a TTS service. Services that handle this intent must
317         * extend {@link TextToSpeechService}. Normal applications should not use this intent
318         * directly, instead they should talk to the TTS service using the the methods in this
319         * class.
320         */
321        @SdkConstant(SdkConstantType.SERVICE_ACTION)
322        public static final String INTENT_ACTION_TTS_SERVICE =
323                "android.intent.action.TTS_SERVICE";
324
325        /**
326         * Name under which a text to speech engine publishes information about itself.
327         * This meta-data should reference an XML resource containing a
328         * <code>&lt;{@link android.R.styleable#TextToSpeechEngine tts-engine}&gt;</code>
329         * tag.
330         */
331        public static final String SERVICE_META_DATA = "android.speech.tts";
332
333        // intents to ask engine to install data or check its data
334        /**
335         * Activity Action: Triggers the platform TextToSpeech engine to
336         * start the activity that installs the resource files on the device
337         * that are required for TTS to be operational. Since the installation
338         * of the data can be interrupted or declined by the user, the application
339         * shouldn't expect successful installation upon return from that intent,
340         * and if need be, should check installation status with
341         * {@link #ACTION_CHECK_TTS_DATA}.
342         */
343        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
344        public static final String ACTION_INSTALL_TTS_DATA =
345                "android.speech.tts.engine.INSTALL_TTS_DATA";
346
347        /**
348         * Broadcast Action: broadcast to signal the change in the list of available
349         * languages or/and their features.
350         */
351        @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
352        public static final String ACTION_TTS_DATA_INSTALLED =
353                "android.speech.tts.engine.TTS_DATA_INSTALLED";
354
355        /**
356         * Activity Action: Starts the activity from the platform TextToSpeech
357         * engine to verify the proper installation and availability of the
358         * resource files on the system. Upon completion, the activity will
359         * return one of the following codes:
360         * {@link #CHECK_VOICE_DATA_PASS},
361         * {@link #CHECK_VOICE_DATA_FAIL},
362         * <p> Moreover, the data received in the activity result will contain the following
363         * fields:
364         * <ul>
365         *   <li>{@link #EXTRA_AVAILABLE_VOICES} which contains an ArrayList<String> of all the
366         *   available voices. The format of each voice is: lang-COUNTRY-variant where COUNTRY and
367         *   variant are optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").</li>
368         *   <li>{@link #EXTRA_UNAVAILABLE_VOICES} which contains an ArrayList<String> of all the
369         *   unavailable voices (ones that user can install). The format of each voice is:
370         *   lang-COUNTRY-variant where COUNTRY and variant are optional (ie, "eng" or
371         *   "eng-USA" or "eng-USA-FEMALE").</li>
372         * </ul>
373         */
374        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
375        public static final String ACTION_CHECK_TTS_DATA =
376                "android.speech.tts.engine.CHECK_TTS_DATA";
377
378        /**
379         * Activity intent for getting some sample text to use for demonstrating TTS. Specific
380         * locale have to be requested by passing following extra parameters:
381         * <ul>
382         *   <li>language</li>
383         *   <li>country</li>
384         *   <li>variant</li>
385         * </ul>
386         *
387         * Upon completion, the activity result may contain the following fields:
388         * <ul>
389         *   <li>{@link #EXTRA_SAMPLE_TEXT} which contains an String with sample text.</li>
390         * </ul>
391         */
392        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
393        public static final String ACTION_GET_SAMPLE_TEXT =
394                "android.speech.tts.engine.GET_SAMPLE_TEXT";
395
396        /**
397         * Extra information received with the {@link #ACTION_GET_SAMPLE_TEXT} intent result where
398         * the TextToSpeech engine returns an String with sample text for requested voice
399         */
400        public static final String EXTRA_SAMPLE_TEXT = "sampleText";
401
402
403        // extras for a TTS engine's check data activity
404        /**
405         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
406         * the TextToSpeech engine returns an ArrayList<String> of all the available voices.
407         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
408         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
409         */
410        public static final String EXTRA_AVAILABLE_VOICES = "availableVoices";
411
412        /**
413         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
414         * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
415         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
416         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
417         */
418        public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
419
420        /**
421         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
422         * the TextToSpeech engine specifies the path to its resources.
423         *
424         * It may be used by language packages to find out where to put their data.
425         *
426         * @deprecated TTS engine implementation detail, this information has no use for
427         * text-to-speech API client.
428         */
429        @Deprecated
430        public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
431
432        /**
433         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
434         * the TextToSpeech engine specifies the file names of its resources under the
435         * resource path.
436         *
437         * @deprecated TTS engine implementation detail, this information has no use for
438         * text-to-speech API client.
439         */
440        @Deprecated
441        public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
442
443        /**
444         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
445         * the TextToSpeech engine specifies the locale associated with each resource file.
446         *
447         * @deprecated TTS engine implementation detail, this information has no use for
448         * text-to-speech API client.
449         */
450        @Deprecated
451        public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
452
453        /**
454         * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
455         * caller indicates to the TextToSpeech engine which specific sets of voice data to
456         * check for by sending an ArrayList<String> of the voices that are of interest.
457         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
458         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
459         *
460         * @deprecated Redundant functionality, checking for existence of specific sets of voice
461         * data can be done on client side.
462         */
463        @Deprecated
464        public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
465
466        // extras for a TTS engine's data installation
467        /**
468         * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent result.
469         * It indicates whether the data files for the synthesis engine were successfully
470         * installed. The installation was initiated with the  {@link #ACTION_INSTALL_TTS_DATA}
471         * intent. The possible values for this extra are
472         * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
473         *
474         * @deprecated No longer in use. If client ise interested in information about what
475         * changed, is should send ACTION_CHECK_TTS_DATA intent to discover available voices.
476         */
477        @Deprecated
478        public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
479
480        // keys for the parameters passed with speak commands. Hidden keys are used internally
481        // to maintain engine state for each TextToSpeech instance.
482        /**
483         * @hide
484         */
485        public static final String KEY_PARAM_RATE = "rate";
486
487        /**
488         * @hide
489         */
490        public static final String KEY_PARAM_VOICE_NAME = "voiceName";
491
492        /**
493         * @hide
494         */
495        public static final String KEY_PARAM_LANGUAGE = "language";
496
497        /**
498         * @hide
499         */
500        public static final String KEY_PARAM_COUNTRY = "country";
501
502        /**
503         * @hide
504         */
505        public static final String KEY_PARAM_VARIANT = "variant";
506
507        /**
508         * @hide
509         */
510        public static final String KEY_PARAM_ENGINE = "engine";
511
512        /**
513         * @hide
514         */
515        public static final String KEY_PARAM_PITCH = "pitch";
516
517        /**
518         * Parameter key to specify the audio stream type to be used when speaking text
519         * or playing back a file. The value should be one of the STREAM_ constants
520         * defined in {@link AudioManager}.
521         *
522         * @see TextToSpeech#speak(String, int, HashMap)
523         * @see TextToSpeech#playEarcon(String, int, HashMap)
524         */
525        public static final String KEY_PARAM_STREAM = "streamType";
526
527        /**
528         * Parameter key to specify the audio attributes to be used when
529         * speaking text or playing back a file. The value should be set
530         * using {@link TextToSpeech#setAudioAttributes(AudioAttributes)}.
531         *
532         * @see TextToSpeech#speak(String, int, HashMap)
533         * @see TextToSpeech#playEarcon(String, int, HashMap)
534         * @hide
535         */
536        public static final String KEY_PARAM_AUDIO_ATTRIBUTES = "audioAttributes";
537
538        /**
539         * Parameter key to identify an utterance in the
540         * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
541         * spoken, a file has been played back or a silence duration has elapsed.
542         *
543         * @see TextToSpeech#speak(String, int, HashMap)
544         * @see TextToSpeech#playEarcon(String, int, HashMap)
545         * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
546         */
547        public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
548
549        /**
550         * Parameter key to specify the speech volume relative to the current stream type
551         * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
552         * where 0 is silence, and 1 is the maximum volume (the default behavior).
553         *
554         * @see TextToSpeech#speak(String, int, HashMap)
555         * @see TextToSpeech#playEarcon(String, int, HashMap)
556         */
557        public static final String KEY_PARAM_VOLUME = "volume";
558
559        /**
560         * Parameter key to specify how the speech is panned from left to right when speaking text.
561         * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
562         * 0 to center (the default behavior), and +1 to hard-right.
563         *
564         * @see TextToSpeech#speak(String, int, HashMap)
565         * @see TextToSpeech#playEarcon(String, int, HashMap)
566         */
567        public static final String KEY_PARAM_PAN = "pan";
568
569        /**
570         * Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)}
571         * for a description of how feature keys work. If set (and supported by the engine
572         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must
573         * use network based synthesis.
574         *
575         * @see TextToSpeech#speak(String, int, java.util.HashMap)
576         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
577         * @see TextToSpeech#getFeatures(java.util.Locale)
578         *
579         * @deprecated Starting from API level 21, to select network synthesis, call
580         * ({@link TextToSpeech#getVoices()}, find a suitable network voice
581         * ({@link Voice#isNetworkConnectionRequired()}) and pass it
582         * to {@link TextToSpeech#setVoice(Voice)}).
583         */
584        @Deprecated
585        public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
586
587        /**
588         * Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)}
589         * for a description of how feature keys work. If set and supported by the engine
590         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize
591         * text on-device (without making network requests).
592         *
593         * @see TextToSpeech#speak(String, int, java.util.HashMap)
594         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
595         * @see TextToSpeech#getFeatures(java.util.Locale)
596
597         * @deprecated Starting from API level 21, to select embedded synthesis, call
598         * ({@link TextToSpeech#getVoices()}, find a suitable embedded voice
599         * ({@link Voice#isNetworkConnectionRequired()}) and pass it
600         * to {@link TextToSpeech#setVoice(Voice)}).
601         */
602        @Deprecated
603        public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
604
605        /**
606         * Parameter key to specify an audio session identifier (obtained from
607         * {@link AudioManager#generateAudioSessionId()}) that will be used by the request audio
608         * output. It can be used to associate one of the {@link android.media.audiofx.AudioEffect}
609         * objects with the synthesis (or earcon) output.
610         *
611         * @see TextToSpeech#speak(String, int, HashMap)
612         * @see TextToSpeech#playEarcon(String, int, HashMap)
613         */
614        public static final String KEY_PARAM_SESSION_ID = "sessionId";
615
616        /**
617         * Feature key that indicates that the voice may need to download additional data to be fully
618         * functional. The download will be triggered by calling
619         * {@link TextToSpeech#setVoice(Voice)} or {@link TextToSpeech#setLanguage(Locale)}.
620         * Until download is complete, each synthesis request will either report
621         * {@link TextToSpeech#ERROR_NOT_INSTALLED_YET} error, or use a different voice to synthesize
622         * the request. This feature should NOT be used as a key of a request parameter.
623         *
624         * @see TextToSpeech#getFeatures(java.util.Locale)
625         * @see Voice#getFeatures()
626         */
627        public static final String KEY_FEATURE_NOT_INSTALLED = "notInstalled";
628
629        /**
630         * Feature key that indicate that a network timeout can be set for the request. If set and
631         * supported as per {@link TextToSpeech#getFeatures(Locale)} or {@link Voice#getFeatures()},
632         * it can be used as request parameter to set the maximum allowed time for a single
633         * request attempt, in milliseconds, before synthesis fails. When used as a key of
634         * a request parameter, its value should be a string with an integer value.
635         *
636         * @see TextToSpeech#getFeatures(java.util.Locale)
637         * @see Voice#getFeatures()
638         */
639        public static final String KEY_FEATURE_NETWORK_TIMEOUT_MS = "networkTimeoutMs";
640
641        /**
642         * Feature key that indicates that network request retries count can be set for the request.
643         * If set and supported as per {@link TextToSpeech#getFeatures(Locale)} or
644         * {@link Voice#getFeatures()}, it can be used as a request parameter to set the
645         * number of network request retries that are attempted in case of failure. When used as
646         * a key of a request parameter, its value should be a string with an integer value.
647         *
648         * @see TextToSpeech#getFeatures(java.util.Locale)
649         * @see Voice#getFeatures()
650         */
651        public static final String KEY_FEATURE_NETWORK_RETRIES_COUNT = "networkRetriesCount";
652    }
653
654    private final Context mContext;
655    private Connection mConnectingServiceConnection;
656    private Connection mServiceConnection;
657    private OnInitListener mInitListener;
658    // Written from an unspecified application thread, read from
659    // a binder thread.
660    private volatile UtteranceProgressListener mUtteranceProgressListener;
661    private final Object mStartLock = new Object();
662
663    private String mRequestedEngine;
664    // Whether to initialize this TTS object with the default engine,
665    // if the requested engine is not available. Valid only if mRequestedEngine
666    // is not null. Used only for testing, though potentially useful API wise
667    // too.
668    private final boolean mUseFallback;
669    private final Map<String, Uri> mEarcons;
670    private final Map<CharSequence, Uri> mUtterances;
671    private final Bundle mParams = new Bundle();
672    private final TtsEngines mEnginesHelper;
673    private volatile String mCurrentEngine = null;
674
675    /**
676     * The constructor for the TextToSpeech class, using the default TTS engine.
677     * This will also initialize the associated TextToSpeech engine if it isn't already running.
678     *
679     * @param context
680     *            The context this instance is running in.
681     * @param listener
682     *            The {@link TextToSpeech.OnInitListener} that will be called when the
683     *            TextToSpeech engine has initialized. In a case of a failure the listener
684     *            may be called immediately, before TextToSpeech instance is fully constructed.
685     */
686    public TextToSpeech(Context context, OnInitListener listener) {
687        this(context, listener, null);
688    }
689
690    /**
691     * The constructor for the TextToSpeech class, using the given TTS engine.
692     * This will also initialize the associated TextToSpeech engine if it isn't already running.
693     *
694     * @param context
695     *            The context this instance is running in.
696     * @param listener
697     *            The {@link TextToSpeech.OnInitListener} that will be called when the
698     *            TextToSpeech engine has initialized. In a case of a failure the listener
699     *            may be called immediately, before TextToSpeech instance is fully constructed.
700     * @param engine Package name of the TTS engine to use.
701     */
702    public TextToSpeech(Context context, OnInitListener listener, String engine) {
703        this(context, listener, engine, null, true);
704    }
705
706    /**
707     * Used by the framework to instantiate TextToSpeech objects with a supplied
708     * package name, instead of using {@link android.content.Context#getPackageName()}
709     *
710     * @hide
711     */
712    public TextToSpeech(Context context, OnInitListener listener, String engine,
713            String packageName, boolean useFallback) {
714        mContext = context;
715        mInitListener = listener;
716        mRequestedEngine = engine;
717        mUseFallback = useFallback;
718
719        mEarcons = new HashMap<String, Uri>();
720        mUtterances = new HashMap<CharSequence, Uri>();
721        mUtteranceProgressListener = null;
722
723        mEnginesHelper = new TtsEngines(mContext);
724        initTts();
725    }
726
727    private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method,
728            boolean onlyEstablishedConnection) {
729        return runAction(action, errorResult, method, false, onlyEstablishedConnection);
730    }
731
732    private <R> R runAction(Action<R> action, R errorResult, String method) {
733        return runAction(action, errorResult, method, true, true);
734    }
735
736    private <R> R runAction(Action<R> action, R errorResult, String method,
737            boolean reconnect, boolean onlyEstablishedConnection) {
738        synchronized (mStartLock) {
739            if (mServiceConnection == null) {
740                Log.w(TAG, method + " failed: not bound to TTS engine");
741                return errorResult;
742            }
743            return mServiceConnection.runAction(action, errorResult, method, reconnect,
744                    onlyEstablishedConnection);
745        }
746    }
747
748    private int initTts() {
749        // Step 1: Try connecting to the engine that was requested.
750        if (mRequestedEngine != null) {
751            if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
752                if (connectToEngine(mRequestedEngine)) {
753                    mCurrentEngine = mRequestedEngine;
754                    return SUCCESS;
755                } else if (!mUseFallback) {
756                    mCurrentEngine = null;
757                    dispatchOnInit(ERROR);
758                    return ERROR;
759                }
760            } else if (!mUseFallback) {
761                Log.i(TAG, "Requested engine not installed: " + mRequestedEngine);
762                mCurrentEngine = null;
763                dispatchOnInit(ERROR);
764                return ERROR;
765            }
766        }
767
768        // Step 2: Try connecting to the user's default engine.
769        final String defaultEngine = getDefaultEngine();
770        if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) {
771            if (connectToEngine(defaultEngine)) {
772                mCurrentEngine = defaultEngine;
773                return SUCCESS;
774            }
775        }
776
777        // Step 3: Try connecting to the highest ranked engine in the
778        // system.
779        final String highestRanked = mEnginesHelper.getHighestRankedEngineName();
780        if (highestRanked != null && !highestRanked.equals(mRequestedEngine) &&
781                !highestRanked.equals(defaultEngine)) {
782            if (connectToEngine(highestRanked)) {
783                mCurrentEngine = highestRanked;
784                return SUCCESS;
785            }
786        }
787
788        // NOTE: The API currently does not allow the caller to query whether
789        // they are actually connected to any engine. This might fail for various
790        // reasons like if the user disables all her TTS engines.
791
792        mCurrentEngine = null;
793        dispatchOnInit(ERROR);
794        return ERROR;
795    }
796
797    private boolean connectToEngine(String engine) {
798        Connection connection = new Connection();
799        Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
800        intent.setPackage(engine);
801        boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
802        if (!bound) {
803            Log.e(TAG, "Failed to bind to " + engine);
804            return false;
805        } else {
806            Log.i(TAG, "Sucessfully bound to " + engine);
807            mConnectingServiceConnection = connection;
808            return true;
809        }
810    }
811
812    private void dispatchOnInit(int result) {
813        synchronized (mStartLock) {
814            if (mInitListener != null) {
815                mInitListener.onInit(result);
816                mInitListener = null;
817            }
818        }
819    }
820
821    private IBinder getCallerIdentity() {
822        return mServiceConnection.getCallerIdentity();
823    }
824
825    /**
826     * Releases the resources used by the TextToSpeech engine.
827     * It is good practice for instance to call this method in the onDestroy() method of an Activity
828     * so the TextToSpeech engine can be cleanly stopped.
829     */
830    public void shutdown() {
831        // Special case, we are asked to shutdown connection that did finalize its connection.
832        synchronized (mStartLock) {
833            if (mConnectingServiceConnection != null) {
834                mContext.unbindService(mConnectingServiceConnection);
835                mConnectingServiceConnection = null;
836                return;
837            }
838        }
839
840        // Post connection case
841        runActionNoReconnect(new Action<Void>() {
842            @Override
843            public Void run(ITextToSpeechService service) throws RemoteException {
844                service.setCallback(getCallerIdentity(), null);
845                service.stop(getCallerIdentity());
846                mServiceConnection.disconnect();
847                // Context#unbindService does not result in a call to
848                // ServiceConnection#onServiceDisconnected. As a result, the
849                // service ends up being destroyed (if there are no other open
850                // connections to it) but the process lives on and the
851                // ServiceConnection continues to refer to the destroyed service.
852                //
853                // This leads to tons of log spam about SynthThread being dead.
854                mServiceConnection = null;
855                mCurrentEngine = null;
856                return null;
857            }
858        }, null, "shutdown", false);
859    }
860
861    /**
862     * Adds a mapping between a string of text and a sound resource in a
863     * package. After a call to this method, subsequent calls to
864     * {@link #speak(String, int, HashMap)} will play the specified sound resource
865     * if it is available, or synthesize the text it is missing.
866     *
867     * @param text
868     *            The string of text. Example: <code>"south_south_east"</code>
869     *
870     * @param packagename
871     *            Pass the packagename of the application that contains the
872     *            resource. If the resource is in your own application (this is
873     *            the most common case), then put the packagename of your
874     *            application here.<br/>
875     *            Example: <b>"com.google.marvin.compass"</b><br/>
876     *            The packagename can be found in the AndroidManifest.xml of
877     *            your application.
878     *            <p>
879     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
880     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
881     *            </p>
882     *
883     * @param resourceId
884     *            Example: <code>R.raw.south_south_east</code>
885     *
886     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
887     */
888    public int addSpeech(String text, String packagename, @RawRes int resourceId) {
889        synchronized (mStartLock) {
890            mUtterances.put(text, makeResourceUri(packagename, resourceId));
891            return SUCCESS;
892        }
893    }
894
895    /**
896     * Adds a mapping between a CharSequence (may be spanned with TtsSpans) of text
897     * and a sound resource in a package. After a call to this method, subsequent calls to
898     * {@link #speak(String, int, HashMap)} will play the specified sound resource
899     * if it is available, or synthesize the text it is missing.
900     *
901     * @param text
902     *            The string of text. Example: <code>"south_south_east"</code>
903     *
904     * @param packagename
905     *            Pass the packagename of the application that contains the
906     *            resource. If the resource is in your own application (this is
907     *            the most common case), then put the packagename of your
908     *            application here.<br/>
909     *            Example: <b>"com.google.marvin.compass"</b><br/>
910     *            The packagename can be found in the AndroidManifest.xml of
911     *            your application.
912     *            <p>
913     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
914     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
915     *            </p>
916     *
917     * @param resourceId
918     *            Example: <code>R.raw.south_south_east</code>
919     *
920     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
921     */
922    public int addSpeech(CharSequence text, String packagename, int resourceId) {
923        synchronized (mStartLock) {
924            mUtterances.put(text, makeResourceUri(packagename, resourceId));
925            return SUCCESS;
926        }
927    }
928
929    /**
930     * Adds a mapping between a string of text and a sound file. Using this, it
931     * is possible to add custom pronounciations for a string of text.
932     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
933     * will play the specified sound resource if it is available, or synthesize the text it is
934     * missing.
935     *
936     * @param text
937     *            The string of text. Example: <code>"south_south_east"</code>
938     * @param filename
939     *            The full path to the sound file (for example:
940     *            "/sdcard/mysounds/hello.wav")
941     *
942     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
943     */
944    public int addSpeech(String text, String filename) {
945        synchronized (mStartLock) {
946            mUtterances.put(text, Uri.parse(filename));
947            return SUCCESS;
948        }
949    }
950
951    /**
952     * Adds a mapping between a CharSequence (may be spanned with TtsSpans and a sound file.
953     * Using this, it is possible to add custom pronounciations for a string of text.
954     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
955     * will play the specified sound resource if it is available, or synthesize the text it is
956     * missing.
957     *
958     * @param text
959     *            The string of text. Example: <code>"south_south_east"</code>
960     * @param file
961     *            File object pointing to the sound file.
962     *
963     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
964     */
965    public int addSpeech(CharSequence text, File file) {
966        synchronized (mStartLock) {
967            mUtterances.put(text, Uri.fromFile(file));
968            return SUCCESS;
969        }
970    }
971
972    /**
973     * Adds a mapping between a string of text and a sound resource in a
974     * package. Use this to add custom earcons.
975     *
976     * @see #playEarcon(String, int, HashMap)
977     *
978     * @param earcon The name of the earcon.
979     *            Example: <code>"[tick]"</code><br/>
980     *
981     * @param packagename
982     *            the package name of the application that contains the
983     *            resource. This can for instance be the package name of your own application.
984     *            Example: <b>"com.google.marvin.compass"</b><br/>
985     *            The package name can be found in the AndroidManifest.xml of
986     *            the application containing the resource.
987     *            <p>
988     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
989     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
990     *            </p>
991     *
992     * @param resourceId
993     *            Example: <code>R.raw.tick_snd</code>
994     *
995     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
996     */
997    public int addEarcon(String earcon, String packagename, @RawRes int resourceId) {
998        synchronized(mStartLock) {
999            mEarcons.put(earcon, makeResourceUri(packagename, resourceId));
1000            return SUCCESS;
1001        }
1002    }
1003
1004    /**
1005     * Adds a mapping between a string of text and a sound file.
1006     * Use this to add custom earcons.
1007     *
1008     * @see #playEarcon(String, int, HashMap)
1009     *
1010     * @param earcon
1011     *            The name of the earcon.
1012     *            Example: <code>"[tick]"</code>
1013     * @param filename
1014     *            The full path to the sound file (for example:
1015     *            "/sdcard/mysounds/tick.wav")
1016     *
1017     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
1018     *
1019     * @deprecated As of API level 21, replaced by
1020     *         {@link #addEarcon(String, File)}.
1021     */
1022    @Deprecated
1023    public int addEarcon(String earcon, String filename) {
1024        synchronized(mStartLock) {
1025            mEarcons.put(earcon, Uri.parse(filename));
1026            return SUCCESS;
1027        }
1028    }
1029
1030    /**
1031     * Adds a mapping between a string of text and a sound file.
1032     * Use this to add custom earcons.
1033     *
1034     * @see #playEarcon(String, int, HashMap)
1035     *
1036     * @param earcon
1037     *            The name of the earcon.
1038     *            Example: <code>"[tick]"</code>
1039     * @param file
1040     *            File object pointing to the sound file.
1041     *
1042     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
1043     */
1044    public int addEarcon(String earcon, File file) {
1045        synchronized(mStartLock) {
1046            mEarcons.put(earcon, Uri.fromFile(file));
1047            return SUCCESS;
1048        }
1049    }
1050
1051    private Uri makeResourceUri(String packageName, int resourceId) {
1052        return new Uri.Builder()
1053                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
1054                .encodedAuthority(packageName)
1055                .appendEncodedPath(String.valueOf(resourceId))
1056                .build();
1057    }
1058
1059    /**
1060     * Speaks the text using the specified queuing strategy and speech parameters, the text may
1061     * be spanned with TtsSpans.
1062     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1063     * requests and then returns. The synthesis might not have finished (or even started!) at the
1064     * time when this method returns. In order to reliably detect errors during synthesis,
1065     * we recommend setting an utterance progress listener (see
1066     * {@link #setOnUtteranceProgressListener}) and using the
1067     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1068     *
1069     * @param text The string of text to be spoken. No longer than
1070     *            {@link #getMaxSpeechInputLength()} characters.
1071     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
1072     * @param params Parameters for the request. Can be null.
1073     *            Supported parameter names:
1074     *            {@link Engine#KEY_PARAM_STREAM},
1075     *            {@link Engine#KEY_PARAM_VOLUME},
1076     *            {@link Engine#KEY_PARAM_PAN}.
1077     *            Engine specific parameters may be passed in but the parameter keys
1078     *            must be prefixed by the name of the engine they are intended for. For example
1079     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1080     *            engine named "com.svox.pico" if it is being used.
1081     * @param utteranceId An unique identifier for this request.
1082     *
1083     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation.
1084     */
1085    public int speak(final CharSequence text,
1086                     final int queueMode,
1087                     final Bundle params,
1088                     final String utteranceId) {
1089        return runAction(new Action<Integer>() {
1090            @Override
1091            public Integer run(ITextToSpeechService service) throws RemoteException {
1092                Uri utteranceUri = mUtterances.get(text);
1093                if (utteranceUri != null) {
1094                    return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
1095                            getParams(params), utteranceId);
1096                } else {
1097                    return service.speak(getCallerIdentity(), text, queueMode, getParams(params),
1098                            utteranceId);
1099                }
1100            }
1101        }, ERROR, "speak");
1102    }
1103
1104    /**
1105     * Speaks the string using the specified queuing strategy and speech parameters.
1106     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1107     * requests and then returns. The synthesis might not have finished (or even started!) at the
1108     * time when this method returns. In order to reliably detect errors during synthesis,
1109     * we recommend setting an utterance progress listener (see
1110     * {@link #setOnUtteranceProgressListener}) and using the
1111     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1112     *
1113     * @param text The string of text to be spoken. No longer than
1114     *            {@link #getMaxSpeechInputLength()} characters.
1115     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
1116     * @param params Parameters for the request. Can be null.
1117     *            Supported parameter names:
1118     *            {@link Engine#KEY_PARAM_STREAM},
1119     *            {@link Engine#KEY_PARAM_UTTERANCE_ID},
1120     *            {@link Engine#KEY_PARAM_VOLUME},
1121     *            {@link Engine#KEY_PARAM_PAN}.
1122     *            Engine specific parameters may be passed in but the parameter keys
1123     *            must be prefixed by the name of the engine they are intended for. For example
1124     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1125     *            engine named "com.svox.pico" if it is being used.
1126     *
1127     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation.
1128     * @deprecated As of API level 21, replaced by
1129     *         {@link #speak(CharSequence, int, Bundle, String)}.
1130     */
1131    @Deprecated
1132    public int speak(final String text, final int queueMode, final HashMap<String, String> params) {
1133        return speak(text, queueMode, convertParamsHashMaptoBundle(params),
1134                     params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID));
1135    }
1136
1137    /**
1138     * Plays the earcon using the specified queueing mode and parameters.
1139     * The earcon must already have been added with {@link #addEarcon(String, String)} or
1140     * {@link #addEarcon(String, String, int)}.
1141     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1142     * requests and then returns. The synthesis might not have finished (or even started!) at the
1143     * time when this method returns. In order to reliably detect errors during synthesis,
1144     * we recommend setting an utterance progress listener (see
1145     * {@link #setOnUtteranceProgressListener}) and using the
1146     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1147     *
1148     * @param earcon The earcon that should be played
1149     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
1150     * @param params Parameters for the request. Can be null.
1151     *            Supported parameter names:
1152     *            {@link Engine#KEY_PARAM_STREAM},
1153     *            Engine specific parameters may be passed in but the parameter keys
1154     *            must be prefixed by the name of the engine they are intended for. For example
1155     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1156     *            engine named "com.svox.pico" if it is being used.
1157     *
1158     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation.
1159     */
1160    public int playEarcon(final String earcon, final int queueMode,
1161            final Bundle params, final String utteranceId) {
1162        return runAction(new Action<Integer>() {
1163            @Override
1164            public Integer run(ITextToSpeechService service) throws RemoteException {
1165                Uri earconUri = mEarcons.get(earcon);
1166                if (earconUri == null) {
1167                    return ERROR;
1168                }
1169                return service.playAudio(getCallerIdentity(), earconUri, queueMode,
1170                        getParams(params), utteranceId);
1171            }
1172        }, ERROR, "playEarcon");
1173    }
1174
1175    /**
1176     * Plays the earcon using the specified queueing mode and parameters.
1177     * The earcon must already have been added with {@link #addEarcon(String, String)} or
1178     * {@link #addEarcon(String, String, int)}.
1179     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1180     * requests and then returns. The synthesis might not have finished (or even started!) at the
1181     * time when this method returns. In order to reliably detect errors during synthesis,
1182     * we recommend setting an utterance progress listener (see
1183     * {@link #setOnUtteranceProgressListener}) and using the
1184     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1185     *
1186     * @param earcon The earcon that should be played
1187     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
1188     * @param params Parameters for the request. Can be null.
1189     *            Supported parameter names:
1190     *            {@link Engine#KEY_PARAM_STREAM},
1191     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1192     *            Engine specific parameters may be passed in but the parameter keys
1193     *            must be prefixed by the name of the engine they are intended for. For example
1194     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1195     *            engine named "com.svox.pico" if it is being used.
1196     *
1197     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation.
1198     * @deprecated As of API level 21, replaced by
1199     *         {@link #playEarcon(String, int, Bundle, String)}.
1200     */
1201    @Deprecated
1202    public int playEarcon(final String earcon, final int queueMode,
1203            final HashMap<String, String> params) {
1204        return playEarcon(earcon, queueMode, convertParamsHashMaptoBundle(params),
1205                          params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID));
1206    }
1207
1208    /**
1209     * Plays silence for the specified amount of time using the specified
1210     * queue mode.
1211     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1212     * requests and then returns. The synthesis might not have finished (or even started!) at the
1213     * time when this method returns. In order to reliably detect errors during synthesis,
1214     * we recommend setting an utterance progress listener (see
1215     * {@link #setOnUtteranceProgressListener}) and using the
1216     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1217     *
1218     * @param durationInMs The duration of the silence.
1219     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
1220     * @param utteranceId An unique identifier for this request.
1221     *
1222     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilentUtterance operation.
1223     */
1224    public int playSilentUtterance(final long durationInMs, final int queueMode,
1225            final String utteranceId) {
1226        return runAction(new Action<Integer>() {
1227            @Override
1228            public Integer run(ITextToSpeechService service) throws RemoteException {
1229                return service.playSilence(getCallerIdentity(), durationInMs,
1230                                           queueMode, utteranceId);
1231            }
1232        }, ERROR, "playSilentUtterance");
1233    }
1234
1235    /**
1236     * Plays silence for the specified amount of time using the specified
1237     * queue mode.
1238     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1239     * requests and then returns. The synthesis might not have finished (or even started!) at the
1240     * time when this method returns. In order to reliably detect errors during synthesis,
1241     * we recommend setting an utterance progress listener (see
1242     * {@link #setOnUtteranceProgressListener}) and using the
1243     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1244     *
1245     * @param durationInMs The duration of the silence.
1246     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
1247     * @param params Parameters for the request. Can be null.
1248     *            Supported parameter names:
1249     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1250     *            Engine specific parameters may be passed in but the parameter keys
1251     *            must be prefixed by the name of the engine they are intended for. For example
1252     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1253     *            engine named "com.svox.pico" if it is being used.
1254     *
1255     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilence operation.
1256     * @deprecated As of API level 21, replaced by
1257     *         {@link #playSilentUtterance(long, int, String)}.
1258     */
1259    @Deprecated
1260    public int playSilence(final long durationInMs, final int queueMode,
1261            final HashMap<String, String> params) {
1262        return playSilentUtterance(durationInMs, queueMode,
1263                           params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID));
1264    }
1265
1266    /**
1267     * Queries the engine for the set of features it supports for a given locale.
1268     * Features can either be framework defined, e.g.
1269     * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
1270     * Engine specific keys must be prefixed by the name of the engine they
1271     * are intended for. These keys can be used as parameters to
1272     * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
1273     * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
1274     *
1275     * Features values are strings and their values must meet restrictions described in their
1276     * documentation.
1277     *
1278     * @param locale The locale to query features for.
1279     * @return Set instance. May return {@code null} on error.
1280     * @deprecated As of API level 21, please use voices. In order to query features of the voice,
1281     * call {@link #getVoices()} to retrieve the list of available voices and
1282     * {@link Voice#getFeatures()} to retrieve the set of features.
1283     */
1284    @Deprecated
1285    public Set<String> getFeatures(final Locale locale) {
1286        return runAction(new Action<Set<String>>() {
1287            @Override
1288            public Set<String> run(ITextToSpeechService service) throws RemoteException {
1289                String[] features = null;
1290                try {
1291                    features = service.getFeaturesForLanguage(
1292                        locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
1293                } catch(MissingResourceException e) {
1294                    Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 " +
1295                            "country code for locale: " + locale, e);
1296                    return null;
1297                }
1298
1299                if (features != null) {
1300                    final Set<String> featureSet = new HashSet<String>();
1301                    Collections.addAll(featureSet, features);
1302                    return featureSet;
1303                }
1304                return null;
1305            }
1306        }, null, "getFeatures");
1307    }
1308
1309    /**
1310     * Checks whether the TTS engine is busy speaking. Note that a speech item is
1311     * considered complete once it's audio data has been sent to the audio mixer, or
1312     * written to a file. There might be a finite lag between this point, and when
1313     * the audio hardware completes playback.
1314     *
1315     * @return {@code true} if the TTS engine is speaking.
1316     */
1317    public boolean isSpeaking() {
1318        return runAction(new Action<Boolean>() {
1319            @Override
1320            public Boolean run(ITextToSpeechService service) throws RemoteException {
1321                return service.isSpeaking();
1322            }
1323        }, false, "isSpeaking");
1324    }
1325
1326    /**
1327     * Interrupts the current utterance (whether played or rendered to file) and discards other
1328     * utterances in the queue.
1329     *
1330     * @return {@link #ERROR} or {@link #SUCCESS}.
1331     */
1332    public int stop() {
1333        return runAction(new Action<Integer>() {
1334            @Override
1335            public Integer run(ITextToSpeechService service) throws RemoteException {
1336                return service.stop(getCallerIdentity());
1337            }
1338        }, ERROR, "stop");
1339    }
1340
1341    /**
1342     * Sets the speech rate.
1343     *
1344     * This has no effect on any pre-recorded speech.
1345     *
1346     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
1347     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
1348     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
1349     *
1350     * @return {@link #ERROR} or {@link #SUCCESS}.
1351     */
1352    public int setSpeechRate(float speechRate) {
1353        if (speechRate > 0.0f) {
1354            int intRate = (int)(speechRate * 100);
1355            if (intRate > 0) {
1356                synchronized (mStartLock) {
1357                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
1358                }
1359                return SUCCESS;
1360            }
1361        }
1362        return ERROR;
1363    }
1364
1365    /**
1366     * Sets the speech pitch for the TextToSpeech engine.
1367     *
1368     * This has no effect on any pre-recorded speech.
1369     *
1370     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
1371     *            lower values lower the tone of the synthesized voice,
1372     *            greater values increase it.
1373     *
1374     * @return {@link #ERROR} or {@link #SUCCESS}.
1375     */
1376    public int setPitch(float pitch) {
1377        if (pitch > 0.0f) {
1378            int intPitch = (int)(pitch * 100);
1379            if (intPitch > 0) {
1380                synchronized (mStartLock) {
1381                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
1382                }
1383                return SUCCESS;
1384            }
1385        }
1386        return ERROR;
1387    }
1388
1389    /**
1390     * Sets the audio attributes to be used when speaking text or playing
1391     * back a file.
1392     *
1393     * @param audioAttributes Valid AudioAttributes instance.
1394     *
1395     * @return {@link #ERROR} or {@link #SUCCESS}.
1396     */
1397    public int setAudioAttributes(AudioAttributes audioAttributes) {
1398        if (audioAttributes != null) {
1399            synchronized (mStartLock) {
1400                mParams.putParcelable(Engine.KEY_PARAM_AUDIO_ATTRIBUTES,
1401                    audioAttributes);
1402            }
1403            return SUCCESS;
1404        }
1405        return ERROR;
1406    }
1407
1408    /**
1409     * @return the engine currently in use by this TextToSpeech instance.
1410     * @hide
1411     */
1412    public String getCurrentEngine() {
1413        return mCurrentEngine;
1414    }
1415
1416    /**
1417     * Returns a Locale instance describing the language currently being used as the default
1418     * Text-to-speech language.
1419     *
1420     * The locale object returned by this method is NOT a valid one. It has identical form to the
1421     * one in {@link #getLanguage()}. Please refer to {@link #getLanguage()} for more information.
1422     *
1423     * @return language, country (if any) and variant (if any) used by the client stored in a
1424     *     Locale instance, or {@code null} on error.
1425     * @deprecated As of API level 21, use <code>getDefaultVoice().getLocale()</code> ({@link
1426     *   #getDefaultVoice()})
1427     */
1428    @Deprecated
1429    public Locale getDefaultLanguage() {
1430        return runAction(new Action<Locale>() {
1431            @Override
1432            public Locale run(ITextToSpeechService service) throws RemoteException {
1433                String[] defaultLanguage = service.getClientDefaultLanguage();
1434
1435                return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
1436            }
1437        }, null, "getDefaultLanguage");
1438    }
1439
1440    /**
1441     * Sets the text-to-speech language.
1442     * The TTS engine will try to use the closest match to the specified
1443     * language as represented by the Locale, but there is no guarantee that the exact same Locale
1444     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1445     * before choosing the language to use for the next utterances.
1446     *
1447     * This method sets the current voice to the default one for the given Locale;
1448     * {@link #getVoice()} can be used to retrieve it.
1449     *
1450     * @param loc The locale describing the language to be used.
1451     *
1452     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1453     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1454     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1455     */
1456    public int setLanguage(final Locale loc) {
1457        return runAction(new Action<Integer>() {
1458            @Override
1459            public Integer run(ITextToSpeechService service) throws RemoteException {
1460                if (loc == null) {
1461                    return LANG_NOT_SUPPORTED;
1462                }
1463                String language = null, country = null;
1464                try {
1465                    language = loc.getISO3Language();
1466                } catch (MissingResourceException e) {
1467                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e);
1468                    return LANG_NOT_SUPPORTED;
1469                }
1470
1471                try {
1472                    country = loc.getISO3Country();
1473                } catch (MissingResourceException e) {
1474                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e);
1475                    return LANG_NOT_SUPPORTED;
1476                }
1477
1478                String variant = loc.getVariant();
1479
1480                // As of API level 21, setLanguage is implemented using setVoice.
1481                // (which, in the default implementation, will call loadLanguage on the service
1482                // interface).
1483
1484                // Sanitize locale using isLanguageAvailable.
1485                int result = service.isLanguageAvailable( language, country, variant);
1486                if (result >= LANG_AVAILABLE){
1487                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
1488                        variant = "";
1489                        if (result < LANG_COUNTRY_AVAILABLE) {
1490                            country = "";
1491                        }
1492                    }
1493                    // Get the default voice for the locale.
1494                    String voiceName = service.getDefaultVoiceNameFor(language, country, variant);
1495                    if (TextUtils.isEmpty(voiceName)) {
1496                        Log.w(TAG, "Couldn't find the default voice for " + language + "/" +
1497                                country + "/" + variant);
1498                        return LANG_NOT_SUPPORTED;
1499                    }
1500
1501                    // Load it.
1502                    if (service.loadVoice(getCallerIdentity(), voiceName) == TextToSpeech.ERROR) {
1503                        return LANG_NOT_SUPPORTED;
1504                    }
1505
1506                    mParams.putString(Engine.KEY_PARAM_VOICE_NAME, voiceName);
1507                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
1508                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
1509                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
1510                }
1511                return result;
1512            }
1513        }, LANG_NOT_SUPPORTED, "setLanguage");
1514    }
1515
1516    /**
1517     * Returns a Locale instance describing the language currently being used for synthesis
1518     * requests sent to the TextToSpeech engine.
1519     *
1520     * In Android 4.2 and before (API <= 17) this function returns the language that is currently
1521     * being used by the TTS engine. That is the last language set by this or any other
1522     * client by a {@link TextToSpeech#setLanguage} call to the same engine.
1523     *
1524     * In Android versions after 4.2 this function returns the language that is currently being
1525     * used for the synthesis requests sent from this client. That is the last language set
1526     * by a {@link TextToSpeech#setLanguage} call on this instance.
1527     *
1528     * If a voice is set (by {@link #setVoice(Voice)}), getLanguage will return the language of
1529     * the currently set voice.
1530     *
1531     * Please note that the Locale object returned by this method is NOT a valid Locale object. Its
1532     * language field contains a three-letter ISO 639-2/T code (where a proper Locale would use
1533     * a two-letter ISO 639-1 code), and the country field contains a three-letter ISO 3166 country
1534     * code (where a proper Locale would use a two-letter ISO 3166-1 code).
1535     *
1536     * @return language, country (if any) and variant (if any) used by the client stored in a
1537     *     Locale instance, or {@code null} on error.
1538     *
1539     * @deprecated As of API level 21, please use <code>getVoice().getLocale()</code>
1540     * ({@link #getVoice()}).
1541     */
1542    @Deprecated
1543    public Locale getLanguage() {
1544        return runAction(new Action<Locale>() {
1545            @Override
1546            public Locale run(ITextToSpeechService service) {
1547                /* No service call, but we're accessing mParams, hence need for
1548                   wrapping it as an Action instance */
1549                String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, "");
1550                String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, "");
1551                String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, "");
1552                return new Locale(lang, country, variant);
1553            }
1554        }, null, "getLanguage");
1555    }
1556
1557    /**
1558     * Query the engine about the set of available languages.
1559     */
1560    public Set<Locale> getAvailableLanguages() {
1561        return runAction(new Action<Set<Locale>>() {
1562            @Override
1563            public Set<Locale> run(ITextToSpeechService service) throws RemoteException {
1564                List<Voice> voices = service.getVoices();
1565                if (voices == null) {
1566                    return new HashSet<Locale>();
1567                }
1568                HashSet<Locale> locales = new HashSet<Locale>();
1569                for (Voice voice : voices) {
1570                    locales.add(voice.getLocale());
1571                }
1572                return locales;
1573            }
1574        }, null, "getAvailableLanguages");
1575    }
1576
1577    /**
1578     * Query the engine about the set of available voices.
1579     *
1580     * Each TTS Engine can expose multiple voices for each locale, each with a different set of
1581     * features.
1582     *
1583     * @see #setVoice(Voice)
1584     * @see Voice
1585     */
1586    public Set<Voice> getVoices() {
1587        return runAction(new Action<Set<Voice>>() {
1588            @Override
1589            public Set<Voice> run(ITextToSpeechService service) throws RemoteException {
1590                List<Voice> voices = service.getVoices();
1591                return (voices != null)  ? new HashSet<Voice>(voices) : new HashSet<Voice>();
1592            }
1593        }, null, "getVoices");
1594    }
1595
1596    /**
1597     * Sets the text-to-speech voice.
1598     *
1599     * @param voice One of objects returned by {@link #getVoices()}.
1600     *
1601     * @return {@link #ERROR} or {@link #SUCCESS}.
1602     *
1603     * @see #getVoices
1604     * @see Voice
1605     */
1606    public int setVoice(final Voice voice) {
1607        return runAction(new Action<Integer>() {
1608            @Override
1609            public Integer run(ITextToSpeechService service) throws RemoteException {
1610                int result = service.loadVoice(getCallerIdentity(), voice.getName());
1611                if (result == SUCCESS) {
1612                    mParams.putString(Engine.KEY_PARAM_VOICE_NAME, voice.getName());
1613
1614                    // Set the language/country/variant, so #getLanguage will return the voice
1615                    // locale when called.
1616                    String language = "";
1617                    try {
1618                        language = voice.getLocale().getISO3Language();
1619                    } catch (MissingResourceException e) {
1620                        Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " +
1621                                voice.getLocale(), e);
1622                    }
1623
1624                    String country = "";
1625                    try {
1626                        country = voice.getLocale().getISO3Country();
1627                    } catch (MissingResourceException e) {
1628                        Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " +
1629                                voice.getLocale(), e);
1630                    }
1631                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
1632                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
1633                    mParams.putString(Engine.KEY_PARAM_VARIANT, voice.getLocale().getVariant());
1634                }
1635                return result;
1636            }
1637        }, LANG_NOT_SUPPORTED, "setVoice");
1638    }
1639
1640    /**
1641     * Returns a Voice instance describing the voice currently being used for synthesis
1642     * requests sent to the TextToSpeech engine.
1643     *
1644     * @return Voice instance used by the client, or {@code null} if not set or on error.
1645     *
1646     * @see #getVoices
1647     * @see #setVoice
1648     * @see Voice
1649     */
1650    public Voice getVoice() {
1651        return runAction(new Action<Voice>() {
1652            @Override
1653            public Voice run(ITextToSpeechService service) throws RemoteException {
1654                String voiceName = mParams.getString(Engine.KEY_PARAM_VOICE_NAME, "");
1655                if (TextUtils.isEmpty(voiceName)) {
1656                    return null;
1657                }
1658                List<Voice> voices = service.getVoices();
1659                if (voices == null) {
1660                    return null;
1661                }
1662                for (Voice voice : voices) {
1663                    if (voice.getName().equals(voiceName)) {
1664                        return voice;
1665                    }
1666                }
1667                return null;
1668            }
1669        }, null, "getVoice");
1670    }
1671
1672    /**
1673     * Returns a Voice instance that's the default voice for the default Text-to-speech language.
1674     * @return The default voice instance for the default language, or {@code null} if not set or
1675     *     on error.
1676     */
1677    public Voice getDefaultVoice() {
1678        return runAction(new Action<Voice>() {
1679            @Override
1680            public Voice run(ITextToSpeechService service) throws RemoteException {
1681
1682                String[] defaultLanguage = service.getClientDefaultLanguage();
1683
1684                if (defaultLanguage == null || defaultLanguage.length == 0) {
1685                    Log.e(TAG, "service.getClientDefaultLanguage() returned empty array");
1686                    return null;
1687                }
1688                String language = defaultLanguage[0];
1689                String country = (defaultLanguage.length > 1) ? defaultLanguage[1] : "";
1690                String variant = (defaultLanguage.length > 2) ? defaultLanguage[2] : "";
1691
1692                // Sanitize the locale using isLanguageAvailable.
1693                int result = service.isLanguageAvailable(language, country, variant);
1694                if (result >= LANG_AVAILABLE){
1695                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
1696                        variant = "";
1697                        if (result < LANG_COUNTRY_AVAILABLE) {
1698                            country = "";
1699                        }
1700                    }
1701                } else {
1702                    // The default language is not supported.
1703                    return null;
1704                }
1705
1706                // Get the default voice name
1707                String voiceName = service.getDefaultVoiceNameFor(language, country, variant);
1708                if (TextUtils.isEmpty(voiceName)) {
1709                    return null;
1710                }
1711
1712                // Find it
1713                List<Voice> voices = service.getVoices();
1714                if (voices == null) {
1715                    return null;
1716                }
1717                for (Voice voice : voices) {
1718                    if (voice.getName().equals(voiceName)) {
1719                        return voice;
1720                    }
1721                }
1722                return null;
1723            }
1724        }, null, "getDefaultVoice");
1725    }
1726
1727
1728
1729    /**
1730     * Checks if the specified language as represented by the Locale is available and supported.
1731     *
1732     * @param loc The Locale describing the language to be used.
1733     *
1734     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1735     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1736     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1737     */
1738    public int isLanguageAvailable(final Locale loc) {
1739        return runAction(new Action<Integer>() {
1740            @Override
1741            public Integer run(ITextToSpeechService service) throws RemoteException {
1742                String language = null, country = null;
1743
1744                try {
1745                    language = loc.getISO3Language();
1746                } catch (MissingResourceException e) {
1747                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e);
1748                    return LANG_NOT_SUPPORTED;
1749                }
1750
1751                try {
1752                    country = loc.getISO3Country();
1753                } catch (MissingResourceException e) {
1754                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e);
1755                    return LANG_NOT_SUPPORTED;
1756                }
1757
1758                return service.isLanguageAvailable(language, country, loc.getVariant());
1759            }
1760        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
1761    }
1762
1763    /**
1764     * Synthesizes the given text to a file using the specified parameters.
1765     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1766     * requests and then returns. The synthesis might not have finished (or even started!) at the
1767     * time when this method returns. In order to reliably detect errors during synthesis,
1768     * we recommend setting an utterance progress listener (see
1769     * {@link #setOnUtteranceProgressListener}).
1770     *
1771     * @param text The text that should be synthesized. No longer than
1772     *            {@link #getMaxSpeechInputLength()} characters.
1773     * @param params Parameters for the request. Can be null.
1774     *            Engine specific parameters may be passed in but the parameter keys
1775     *            must be prefixed by the name of the engine they are intended for. For example
1776     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1777     *            engine named "com.svox.pico" if it is being used.
1778     * @param file File to write the generated audio data to.
1779     * @param utteranceId An unique identifier for this request.
1780     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
1781     */
1782    public int synthesizeToFile(final CharSequence text, final Bundle params,
1783            final File file, final String utteranceId) {
1784        return runAction(new Action<Integer>() {
1785            @Override
1786            public Integer run(ITextToSpeechService service) throws RemoteException {
1787                ParcelFileDescriptor fileDescriptor;
1788                int returnValue;
1789                try {
1790                    if(file.exists() && !file.canWrite()) {
1791                        Log.e(TAG, "Can't write to " + file);
1792                        return ERROR;
1793                    }
1794                    fileDescriptor = ParcelFileDescriptor.open(file,
1795                            ParcelFileDescriptor.MODE_WRITE_ONLY |
1796                            ParcelFileDescriptor.MODE_CREATE |
1797                            ParcelFileDescriptor.MODE_TRUNCATE);
1798                    returnValue = service.synthesizeToFileDescriptor(getCallerIdentity(), text,
1799                            fileDescriptor, getParams(params), utteranceId);
1800                    fileDescriptor.close();
1801                    return returnValue;
1802                } catch (FileNotFoundException e) {
1803                    Log.e(TAG, "Opening file " + file + " failed", e);
1804                    return ERROR;
1805                } catch (IOException e) {
1806                    Log.e(TAG, "Closing file " + file + " failed", e);
1807                    return ERROR;
1808                }
1809            }
1810        }, ERROR, "synthesizeToFile");
1811    }
1812
1813    /**
1814     * Synthesizes the given text to a file using the specified parameters.
1815     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1816     * requests and then returns. The synthesis might not have finished (or even started!) at the
1817     * time when this method returns. In order to reliably detect errors during synthesis,
1818     * we recommend setting an utterance progress listener (see
1819     * {@link #setOnUtteranceProgressListener}) and using the
1820     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1821     *
1822     * @param text The text that should be synthesized. No longer than
1823     *            {@link #getMaxSpeechInputLength()} characters.
1824     * @param params Parameters for the request. Can be null.
1825     *            Supported parameter names:
1826     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1827     *            Engine specific parameters may be passed in but the parameter keys
1828     *            must be prefixed by the name of the engine they are intended for. For example
1829     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1830     *            engine named "com.svox.pico" if it is being used.
1831     * @param filename Absolute file filename to write the generated audio data to.It should be
1832     *            something like "/sdcard/myappsounds/mysound.wav".
1833     *
1834     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
1835     * @deprecated As of API level 21, replaced by
1836     *         {@link #synthesizeToFile(CharSequence, Bundle, File, String)}.
1837     */
1838    @Deprecated
1839    public int synthesizeToFile(final String text, final HashMap<String, String> params,
1840            final String filename) {
1841        return synthesizeToFile(text, convertParamsHashMaptoBundle(params),
1842                new File(filename), params.get(Engine.KEY_PARAM_UTTERANCE_ID));
1843    }
1844
1845    private Bundle convertParamsHashMaptoBundle(HashMap<String, String> params) {
1846        if (params != null && !params.isEmpty()) {
1847            Bundle bundle = new Bundle();
1848            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
1849            copyIntParam(bundle, params, Engine.KEY_PARAM_SESSION_ID);
1850            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
1851            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
1852            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
1853
1854            // Copy feature strings defined by the framework.
1855            copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
1856            copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1857            copyIntParam(bundle, params, Engine.KEY_FEATURE_NETWORK_TIMEOUT_MS);
1858            copyIntParam(bundle, params, Engine.KEY_FEATURE_NETWORK_RETRIES_COUNT);
1859
1860            // Copy over all parameters that start with the name of the
1861            // engine that we are currently connected to. The engine is
1862            // free to interpret them as it chooses.
1863            if (!TextUtils.isEmpty(mCurrentEngine)) {
1864                for (Map.Entry<String, String> entry : params.entrySet()) {
1865                    final String key = entry.getKey();
1866                    if (key != null && key.startsWith(mCurrentEngine)) {
1867                        bundle.putString(key, entry.getValue());
1868                    }
1869                }
1870            }
1871
1872            return bundle;
1873        }
1874        return null;
1875    }
1876
1877    private Bundle getParams(Bundle params) {
1878        if (params != null && !params.isEmpty()) {
1879            Bundle bundle = new Bundle(mParams);
1880            bundle.putAll(params);
1881
1882            verifyIntegerBundleParam(bundle, Engine.KEY_PARAM_STREAM);
1883            verifyIntegerBundleParam(bundle, Engine.KEY_PARAM_SESSION_ID);
1884            verifyStringBundleParam(bundle, Engine.KEY_PARAM_UTTERANCE_ID);
1885            verifyFloatBundleParam(bundle, Engine.KEY_PARAM_VOLUME);
1886            verifyFloatBundleParam(bundle, Engine.KEY_PARAM_PAN);
1887
1888            // Copy feature strings defined by the framework.
1889            verifyBooleanBundleParam(bundle, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
1890            verifyBooleanBundleParam(bundle, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1891            verifyIntegerBundleParam(bundle, Engine.KEY_FEATURE_NETWORK_TIMEOUT_MS);
1892            verifyIntegerBundleParam(bundle, Engine.KEY_FEATURE_NETWORK_RETRIES_COUNT);
1893
1894            return bundle;
1895        } else {
1896            return mParams;
1897        }
1898    }
1899
1900    private static boolean verifyIntegerBundleParam(Bundle bundle, String key) {
1901        if (bundle.containsKey(key)) {
1902            if (!(bundle.get(key) instanceof Integer ||
1903                    bundle.get(key) instanceof Long)) {
1904                bundle.remove(key);
1905                Log.w(TAG, "Synthesis request paramter " + key + " containst value "
1906                        + " with invalid type. Should be an Integer or a Long");
1907                return false;
1908            }
1909        }
1910        return true;
1911    }
1912
1913    private static boolean verifyStringBundleParam(Bundle bundle, String key) {
1914        if (bundle.containsKey(key)) {
1915            if (!(bundle.get(key) instanceof String)) {
1916                bundle.remove(key);
1917                Log.w(TAG, "Synthesis request paramter " + key + " containst value "
1918                        + " with invalid type. Should be a String");
1919                return false;
1920            }
1921        }
1922        return true;
1923    }
1924
1925    private static boolean verifyBooleanBundleParam(Bundle bundle, String key) {
1926        if (bundle.containsKey(key)) {
1927            if (!(bundle.get(key) instanceof Boolean ||
1928                    bundle.get(key) instanceof String)) {
1929                bundle.remove(key);
1930                Log.w(TAG, "Synthesis request paramter " + key + " containst value "
1931                        + " with invalid type. Should be a Boolean or String");
1932                return false;
1933            }
1934        }
1935        return true;
1936    }
1937
1938
1939    private static boolean verifyFloatBundleParam(Bundle bundle, String key) {
1940        if (bundle.containsKey(key)) {
1941            if (!(bundle.get(key) instanceof Float ||
1942                    bundle.get(key) instanceof Double)) {
1943                bundle.remove(key);
1944                Log.w(TAG, "Synthesis request paramter " + key + " containst value "
1945                        + " with invalid type. Should be a Float or a Double");
1946                return false;
1947            }
1948        }
1949        return true;
1950    }
1951
1952    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
1953        String value = params.get(key);
1954        if (value != null) {
1955            bundle.putString(key, value);
1956        }
1957    }
1958
1959    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
1960        String valueString = params.get(key);
1961        if (!TextUtils.isEmpty(valueString)) {
1962            try {
1963                int value = Integer.parseInt(valueString);
1964                bundle.putInt(key, value);
1965            } catch (NumberFormatException ex) {
1966                // don't set the value in the bundle
1967            }
1968        }
1969    }
1970
1971    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
1972        String valueString = params.get(key);
1973        if (!TextUtils.isEmpty(valueString)) {
1974            try {
1975                float value = Float.parseFloat(valueString);
1976                bundle.putFloat(key, value);
1977            } catch (NumberFormatException ex) {
1978                // don't set the value in the bundle
1979            }
1980        }
1981    }
1982
1983    /**
1984     * Sets the listener that will be notified when synthesis of an utterance completes.
1985     *
1986     * @param listener The listener to use.
1987     *
1988     * @return {@link #ERROR} or {@link #SUCCESS}.
1989     *
1990     * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)}
1991     *        instead.
1992     */
1993    @Deprecated
1994    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1995        mUtteranceProgressListener = UtteranceProgressListener.from(listener);
1996        return TextToSpeech.SUCCESS;
1997    }
1998
1999    /**
2000     * Sets the listener that will be notified of various events related to the
2001     * synthesis of a given utterance.
2002     *
2003     * See {@link UtteranceProgressListener} and
2004     * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}.
2005     *
2006     * @param listener the listener to use.
2007     * @return {@link #ERROR} or {@link #SUCCESS}
2008     */
2009    public int setOnUtteranceProgressListener(UtteranceProgressListener listener) {
2010        mUtteranceProgressListener = listener;
2011        return TextToSpeech.SUCCESS;
2012    }
2013
2014    /**
2015     * Sets the TTS engine to use.
2016     *
2017     * @deprecated This doesn't inform callers when the TTS engine has been
2018     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
2019     *        can be used with the appropriate engine name. Also, there is no
2020     *        guarantee that the engine specified will be loaded. If it isn't
2021     *        installed or disabled, the user / system wide defaults will apply.
2022     *
2023     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
2024     *
2025     * @return {@link #ERROR} or {@link #SUCCESS}.
2026     */
2027    @Deprecated
2028    public int setEngineByPackageName(String enginePackageName) {
2029        mRequestedEngine = enginePackageName;
2030        return initTts();
2031    }
2032
2033    /**
2034     * Gets the package name of the default speech synthesis engine.
2035     *
2036     * @return Package name of the TTS engine that the user has chosen
2037     *        as their default.
2038     */
2039    public String getDefaultEngine() {
2040        return mEnginesHelper.getDefaultEngine();
2041    }
2042
2043    /**
2044     * Checks whether the user's settings should override settings requested
2045     * by the calling application. As of the Ice cream sandwich release,
2046     * user settings never forcibly override the app's settings.
2047     */
2048    @Deprecated
2049    public boolean areDefaultsEnforced() {
2050        return false;
2051    }
2052
2053    /**
2054     * Gets a list of all installed TTS engines.
2055     *
2056     * @return A list of engine info objects. The list can be empty, but never {@code null}.
2057     */
2058    public List<EngineInfo> getEngines() {
2059        return mEnginesHelper.getEngines();
2060    }
2061
2062    private class Connection implements ServiceConnection {
2063        private ITextToSpeechService mService;
2064
2065        private SetupConnectionAsyncTask mOnSetupConnectionAsyncTask;
2066
2067        private boolean mEstablished;
2068
2069        private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() {
2070            public void onStop(String utteranceId, boolean isStarted) throws RemoteException {
2071                UtteranceProgressListener listener = mUtteranceProgressListener;
2072                if (listener != null) {
2073                    listener.onStop(utteranceId, isStarted);
2074                }
2075            };
2076
2077            @Override
2078            public void onSuccess(String utteranceId) {
2079                UtteranceProgressListener listener = mUtteranceProgressListener;
2080                if (listener != null) {
2081                    listener.onDone(utteranceId);
2082                }
2083            }
2084
2085            @Override
2086            public void onError(String utteranceId, int errorCode) {
2087                UtteranceProgressListener listener = mUtteranceProgressListener;
2088                if (listener != null) {
2089                    listener.onError(utteranceId);
2090                }
2091            }
2092
2093            @Override
2094            public void onStart(String utteranceId) {
2095                UtteranceProgressListener listener = mUtteranceProgressListener;
2096                if (listener != null) {
2097                    listener.onStart(utteranceId);
2098                }
2099            }
2100        };
2101
2102        private class SetupConnectionAsyncTask extends AsyncTask<Void, Void, Integer> {
2103            private final ComponentName mName;
2104
2105            public SetupConnectionAsyncTask(ComponentName name) {
2106                mName = name;
2107            }
2108
2109            @Override
2110            protected Integer doInBackground(Void... params) {
2111                synchronized(mStartLock) {
2112                    if (isCancelled()) {
2113                        return null;
2114                    }
2115
2116                    try {
2117                        mService.setCallback(getCallerIdentity(), mCallback);
2118
2119                        if (mParams.getString(Engine.KEY_PARAM_LANGUAGE) == null) {
2120                            String[] defaultLanguage = mService.getClientDefaultLanguage();
2121                            mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]);
2122                            mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]);
2123                            mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]);
2124
2125                            // Get the default voice for the locale.
2126                            String defaultVoiceName = mService.getDefaultVoiceNameFor(
2127                                defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
2128                            mParams.putString(Engine.KEY_PARAM_VOICE_NAME, defaultVoiceName);
2129                        }
2130
2131                        Log.i(TAG, "Set up connection to " + mName);
2132                        return SUCCESS;
2133                    } catch (RemoteException re) {
2134                        Log.e(TAG, "Error connecting to service, setCallback() failed");
2135                        return ERROR;
2136                    }
2137                }
2138            }
2139
2140            @Override
2141            protected void onPostExecute(Integer result) {
2142                synchronized(mStartLock) {
2143                    if (mOnSetupConnectionAsyncTask == this) {
2144                        mOnSetupConnectionAsyncTask = null;
2145                    }
2146                    mEstablished = true;
2147                    dispatchOnInit(result);
2148                }
2149            }
2150        }
2151
2152        @Override
2153        public void onServiceConnected(ComponentName name, IBinder service) {
2154            synchronized(mStartLock) {
2155                mConnectingServiceConnection = null;
2156
2157                Log.i(TAG, "Connected to " + name);
2158
2159                if (mOnSetupConnectionAsyncTask != null) {
2160                    mOnSetupConnectionAsyncTask.cancel(false);
2161                }
2162
2163                mService = ITextToSpeechService.Stub.asInterface(service);
2164                mServiceConnection = Connection.this;
2165
2166                mEstablished = false;
2167                mOnSetupConnectionAsyncTask = new SetupConnectionAsyncTask(name);
2168                mOnSetupConnectionAsyncTask.execute();
2169            }
2170        }
2171
2172        public IBinder getCallerIdentity() {
2173            return mCallback;
2174        }
2175
2176        /**
2177         * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set.
2178         *
2179         * @return true if we cancel mOnSetupConnectionAsyncTask in progress.
2180         */
2181        private boolean clearServiceConnection() {
2182            synchronized(mStartLock) {
2183                boolean result = false;
2184                if (mOnSetupConnectionAsyncTask != null) {
2185                    result = mOnSetupConnectionAsyncTask.cancel(false);
2186                    mOnSetupConnectionAsyncTask = null;
2187                }
2188
2189                mService = null;
2190                // If this is the active connection, clear it
2191                if (mServiceConnection == this) {
2192                    mServiceConnection = null;
2193                }
2194                return result;
2195            }
2196        }
2197
2198        @Override
2199        public void onServiceDisconnected(ComponentName name) {
2200            Log.i(TAG, "Asked to disconnect from " + name);
2201            if (clearServiceConnection()) {
2202                /* We need to protect against a rare case where engine
2203                 * dies just after successful connection - and we process onServiceDisconnected
2204                 * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels
2205                 * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit
2206                 * with ERROR as argument.
2207                 */
2208                dispatchOnInit(ERROR);
2209            }
2210        }
2211
2212        public void disconnect() {
2213            mContext.unbindService(this);
2214            clearServiceConnection();
2215        }
2216
2217        public boolean isEstablished() {
2218            return mService != null && mEstablished;
2219        }
2220
2221        public <R> R runAction(Action<R> action, R errorResult, String method,
2222                boolean reconnect, boolean onlyEstablishedConnection) {
2223            synchronized (mStartLock) {
2224                try {
2225                    if (mService == null) {
2226                        Log.w(TAG, method + " failed: not connected to TTS engine");
2227                        return errorResult;
2228                    }
2229                    if (onlyEstablishedConnection && !isEstablished()) {
2230                        Log.w(TAG, method + " failed: TTS engine connection not fully set up");
2231                        return errorResult;
2232                    }
2233                    return action.run(mService);
2234                } catch (RemoteException ex) {
2235                    Log.e(TAG, method + " failed", ex);
2236                    if (reconnect) {
2237                        disconnect();
2238                        initTts();
2239                    }
2240                    return errorResult;
2241                }
2242            }
2243        }
2244    }
2245
2246    private interface Action<R> {
2247        R run(ITextToSpeechService service) throws RemoteException;
2248    }
2249
2250    /**
2251     * Information about an installed text-to-speech engine.
2252     *
2253     * @see TextToSpeech#getEngines
2254     */
2255    public static class EngineInfo {
2256        /**
2257         * Engine package name..
2258         */
2259        public String name;
2260        /**
2261         * Localized label for the engine.
2262         */
2263        public String label;
2264        /**
2265         * Icon for the engine.
2266         */
2267        public int icon;
2268        /**
2269         * Whether this engine is a part of the system
2270         * image.
2271         *
2272         * @hide
2273         */
2274        public boolean system;
2275        /**
2276         * The priority the engine declares for the the intent filter
2277         * {@code android.intent.action.TTS_SERVICE}
2278         *
2279         * @hide
2280         */
2281        public int priority;
2282
2283        @Override
2284        public String toString() {
2285            return "EngineInfo{name=" + name + "}";
2286        }
2287
2288    }
2289
2290    /**
2291     * Limit of length of input string passed to speak and synthesizeToFile.
2292     *
2293     * @see #speak
2294     * @see #synthesizeToFile
2295     */
2296    public static int getMaxSpeechInputLength() {
2297        return 4000;
2298    }
2299}
2300