TextToSpeech.java revision 176baa7de11be910c36b7b4dfa7826b55ec97963
1e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi/*
250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert * Copyright (C) 2009 The Android Open Source Project
3e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
4e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * use this file except in compliance with the License. You may obtain a copy of
6e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * the License at
7e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
8e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0
9e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
10e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * License for the specific language governing permissions and limitations under
14e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * the License.
15e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi */
1621a6a6d26baf32593d63226f9728ddd66f0bd571Jean-Michel Trivipackage android.speech.tts;
1721a6a6d26baf32593d63226f9728ddd66f0bd571Jean-Michel Trivi
18ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Triviimport android.annotation.SdkConstant;
19ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Triviimport android.annotation.SdkConstant.SdkConstantType;
20e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.ComponentName;
2150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.content.ContentResolver;
22e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.Context;
23e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.Intent;
24e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.ServiceConnection;
25a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Triviimport android.media.AudioManager;
2650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.net.Uri;
278f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniakimport android.os.AsyncTask;
2850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.os.Bundle;
29e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.IBinder;
30e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.RemoteException;
3150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.provider.Settings;
3250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.text.TextUtils;
33e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.util.Log;
34e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
35748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Collections;
36a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Triviimport java.util.HashMap;
37748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.HashSet;
3850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.List;
39679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Triviimport java.util.Locale;
4050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.Map;
41748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Set;
42a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi
43e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi/**
44e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
4562788e9b48f884a35b89c88911b581daa6a14e08Jean-Michel Trivi * Synthesizes speech from text for immediate playback or to create a sound file.
46a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
47a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
48a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * notified of the completion of the initialization.<br>
49a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
50a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * to release the native resources used by the TextToSpeech engine.
51e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
52e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi */
53a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivipublic class TextToSpeech {
54e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
552cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    private static final String TAG = "TextToSpeech";
562cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
5791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
5891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a successful operation.
5991bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int SUCCESS = 0;
6191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
6291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a generic operation failure.
6391bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int ERROR = -1;
6591bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi
66679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
67679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where all entries in the playback queue (media to be played
68679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * and text to be synthesized) are dropped and replaced by the new entry.
69abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queues are flushed with respect to a given calling app. Entries in the queue
70abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from other callees are not discarded.
71679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
72ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_FLUSH = 0;
73679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
74679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where the new entry is added at the end of the playback queue.
75679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
76ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_ADD = 1;
77abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    /**
78abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queue mode where the entire playback queue is purged. This is different
79abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries
80abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from a given caller.
81abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     *
82abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * @hide
83abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     */
84abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    static final int QUEUE_DESTROY = 2;
85e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
86aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
879f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi     * Denotes the language is available exactly as specified by the locale.
88aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
89ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
90aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
91aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
92b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language and country specified
93aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * by the locale, but not the variant.
94aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
95ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_AVAILABLE = 1;
96aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
97aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
98b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language by the locale,
99aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * but not the country and variant.
100aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
101ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_AVAILABLE = 0;
102aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
103aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
104aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * Denotes the language data is missing.
105aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
106ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_MISSING_DATA = -1;
107aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
108aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
109a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Denotes the language is not supported.
110aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
111ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_NOT_SUPPORTED = -2;
112ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
113ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    /**
114ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * Broadcast Action: The TextToSpeech synthesizer has completed processing
115ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * of all the text in the speech queue.
116c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     *
117c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Note that this notifies callers when the <b>engine</b> has finished has
118c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * processing text data. Audio playback might not have completed (or even started)
119c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * at this point. If you wish to be notified when this happens, see
120c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * {@link OnUtteranceCompletedListener}.
121ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     */
122ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
123ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
124ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi            "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
125aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
126e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
127a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interface definition of a callback to be invoked indicating the completion of the
128a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * TextToSpeech engine initialization.
129e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
130e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public interface OnInitListener {
131a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
132a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Called to signal the completion of the TextToSpeech engine initialization.
13350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
134a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
135a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
13691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        public void onInit(int status);
137e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
138e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
139e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
14050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Listener that will be called when the TTS service has
14150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * completed synthesizing an utterance. This is only called if the utterance
14250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}).
14378c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
144a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi    public interface OnUtteranceCompletedListener {
145a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
14650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Called when an utterance has been synthesized.
14750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
148a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param utteranceId the identifier of the utterance.
14960dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
15060dd360640a400d9b4a602160733281d284aaee5Charles Chen        public void onUtteranceCompleted(String utteranceId);
15178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
15278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
15378c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
154748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Constants and parameter names for controlling text-to-speech. These include:
155748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
156748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * <ul>
157748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
158748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Intents to ask engine to install data or check its data and
159748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         extras for a TTS engine's check data activity.
160748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
161748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
162748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Keys for the parameters passed with speak commands, e.g.
163748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}.
164748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
165748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
166748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         A list of feature strings that engines might support, e.g
167748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to
168748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify
169748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         engine behaviour. The engine can be queried for the set of features it supports
170748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         through {@link TextToSpeech#getFeatures(java.util.Locale)}.
171748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
172748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * </ul>
173d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi     */
174d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    public class Engine {
17550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
17662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
17750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default speech rate.
17850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
17962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
18050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_RATE = 100;
18150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pitch.
18450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
18562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
18650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_PITCH = 100;
18750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default volume.
19050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
19162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
1929d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_VOLUME = 1.0f;
19350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1949d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
19550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pan (centered).
19650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
1979d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
1989d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_PAN = 0.0f;
1999d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
2009d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
20150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}.
20250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
2039d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
204ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int USE_DEFAULTS = 0; // false
20550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
20662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
20750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Package name of the default TTS engine.
20850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
20950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
21022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * @deprecated No longer in use, the default engine is determined by
211b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath         *         the sort order defined in {@link TtsEngines}. Note that
21222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         this doesn't "break" anything because there is no guarantee that
21322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         the engine specified below is installed on a given build, let
21422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         alone be the default.
21562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
21622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        @Deprecated
21750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String DEFAULT_ENGINE = "com.svox.pico";
218d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
219a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
220a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Default audio stream used when playing synthesized speech.
221a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
222ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
223a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
22462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
22562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates success when checking the installation status of the resources used by the
226a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
22762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
228d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_PASS = 1;
22950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
23062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates failure when checking the installation status of the resources used by the
232a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
234d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_FAIL = 0;
23550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
23662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates erroneous data when checking the installation status of the resources used by
238a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
240d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
24150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing resources when checking the installation status of the resources used
244a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
24562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
246d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
24750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing storage volume when checking the installation status of the resources
250a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
25162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
25262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
25399a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
25450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
25550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Intent for starting a TTS service. Services that handle this intent must
25650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * extend {@link TextToSpeechService}. Normal applications should not use this intent
25750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * directly, instead they should talk to the TTS service using the the methods in this
25850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * class.
25950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
26050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.SERVICE_ACTION)
26150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String INTENT_ACTION_TTS_SERVICE =
26250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.intent.action.TTS_SERVICE";
26350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
2644d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        /**
2654d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * Name under which a text to speech engine publishes information about itself.
2664d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * This meta-data should reference an XML resource containing a
2674d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * <code>&lt;{@link android.R.styleable#TextToSpeechEngine tts-engine}&gt;</code>
2684d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * tag.
2694d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         */
2704d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        public static final String SERVICE_META_DATA = "android.speech.tts";
2714d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath
272ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        // intents to ask engine to install data or check its data
273ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
274a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Triggers the platform TextToSpeech engine to
275ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * start the activity that installs the resource files on the device
276ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * that are required for TTS to be operational. Since the installation
277ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * of the data can be interrupted or declined by the user, the application
278ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * shouldn't expect successful installation upon return from that intent,
279ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * and if need be, should check installation status with
280ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #ACTION_CHECK_TTS_DATA}.
281ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
2829f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
283ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_INSTALL_TTS_DATA =
284ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.INSTALL_TTS_DATA";
285ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
286ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
287176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Broadcast Action: broadcast to signal the completion of the installation of
288176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the data files used by the synthesis engine. Success or failure is indicated in the
289176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #EXTRA_TTS_DATA_INSTALLED} extra.
29077a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
29177a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
29277a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String ACTION_TTS_DATA_INSTALLED =
29377a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi                "android.speech.tts.engine.TTS_DATA_INSTALLED";
29450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
29577a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
296a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Starts the activity from the platform TextToSpeech
297ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * engine to verify the proper installation and availability of the
298ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * resource files on the system. Upon completion, the activity will
299ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * return one of the following codes:
300ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_PASS},
301ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_FAIL},
302176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #CHECK_VOICE_DATA_BAD_DATA},
303176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #CHECK_VOICE_DATA_MISSING_DATA}, or
304176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #CHECK_VOICE_DATA_MISSING_VOLUME}.
305ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <p> Moreover, the data received in the activity result will contain the following
306ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * fields:
307ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <ul>
308176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_VOICE_DATA_ROOT_DIRECTORY} which
309176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       indicates the path to the location of the resource files,</li>
310176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_VOICE_DATA_FILES} which contains
311176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       the list of all the resource files,</li>
312176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *   <li>and {@link #EXTRA_VOICE_DATA_FILES_INFO} which
313176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       contains, for each resource file, the description of the language covered by
314176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       the file in the xxx-YYY format, where xxx is the 3-letter ISO language code,
315176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       and YYY is the 3-letter ISO country code.</li>
316ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * </ul>
317ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
3189f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
319ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_CHECK_TTS_DATA =
320ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.CHECK_TTS_DATA";
321ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
32250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
323176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Activity intent for getting some sample text to use for demonstrating TTS.
32450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
325176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * @hide This intent was used by engines written against the old API.
326176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Not sure if it should be exposed.
32750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
32850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
32950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String ACTION_GET_SAMPLE_TEXT =
33050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.speech.tts.engine.GET_SAMPLE_TEXT";
33150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
332176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        // extras for a TTS engine's check data activity
333176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        /**
334176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
335176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the path to its resources.
336176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         */
337176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
338176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak
33962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
340176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
341176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the file names of its resources under the
342176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * resource path.
34362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
344176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
34550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
346176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        /**
347176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
348176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the locale associated with each resource file.
349176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         */
350176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
35150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
35276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
353176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
35476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the available voices.
35576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
35676d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
35776d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
35876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_AVAILABLE_VOICES = "availableVoices";
35950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
36076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
361176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
36276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
36376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
36476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
36576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
36676d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
36750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
36876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
36976d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
37076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * caller indicates to the TextToSpeech engine which specific sets of voice data to
37176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * check for by sending an ArrayList<String> of the voices that are of interest.
37276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
37376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
37476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
37576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
37699a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
37777a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        // extras for a TTS engine's data installation
37877a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
379176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent.
3809f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * It indicates whether the data files for the synthesis engine were successfully
3819f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * installed. The installation was initiated with the  {@link #ACTION_INSTALL_TTS_DATA}
3829f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * intent. The possible values for this extra are
3839f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
38477a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
38577a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
38677a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi
38762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // keys for the parameters passed with speak commands. Hidden keys are used internally
38862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // to maintain engine state for each TextToSpeech instance.
38962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
39050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
39162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
392ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_RATE = "rate";
39350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
39462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
39550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
39662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
397ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_LANGUAGE = "language";
39850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
39962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
40050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
40162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
402ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_COUNTRY = "country";
40350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
40462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
40550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
40662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
407ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_VARIANT = "variant";
40850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
40962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
41050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
41160dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
41260dd360640a400d9b4a602160733281d284aaee5Charles Chen        public static final String KEY_PARAM_ENGINE = "engine";
41350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
41460dd360640a400d9b4a602160733281d284aaee5Charles Chen        /**
41550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
4161a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen         */
4171a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        public static final String KEY_PARAM_PITCH = "pitch";
41850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4191a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        /**
42062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Parameter key to specify the audio stream type to be used when speaking text
42150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * or playing back a file. The value should be one of the STREAM_ constants
42250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * defined in {@link AudioManager}.
42350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
424a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
425a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
42662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
427ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_STREAM = "streamType";
42850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
42962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
430a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Parameter key to identify an utterance in the
431a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
43262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * spoken, a file has been played back or a silence duration has elapsed.
43350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
434a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
435a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
436a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
43762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
438ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
43950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4409d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4419d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify the speech volume relative to the current stream type
4429d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
4439011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * where 0 is silence, and 1 is the maximum volume (the default behavior).
44450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4459d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4469d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4479d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4489d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_VOLUME = "volume";
44950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4509d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4519d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify how the speech is panned from left to right when speaking text.
4529d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
4539011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * 0 to center (the default behavior), and +1 to hard-right.
45450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4559d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4569d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4579d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4589d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_PAN = "pan";
45962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi
460748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
461748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)}
462748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set (and supported by the engine
463748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must
464748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * use network based synthesis.
465748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         *
466748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#speak(String, int, java.util.HashMap)
467748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
468748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#getFeatures(java.util.Locale)
469748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
470748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
471748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
472748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
473748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)}
474748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set and supported by the engine
475748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize
476748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * text on-device (without making network requests).
477748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
478748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
479d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    }
480d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
48150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Context mContext;
48250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Connection mServiceConnection;
48350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private OnInitListener mInitListener;
484a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // Written from an unspecified application thread, read from
485a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // a binder thread.
486754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    private volatile UtteranceProgressListener mUtteranceProgressListener;
48791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    private final Object mStartLock = new Object();
48850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
48950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private String mRequestedEngine;
490c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // Whether to initialize this TTS object with the default engine,
491c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // if the requested engine is not available. Valid only if mRequestedEngine
492c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // is not null. Used only for testing, though potentially useful API wise
493c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // too.
494c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    private final boolean mUseFallback;
49550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mEarcons;
49650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mUtterances;
49750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Bundle mParams = new Bundle();
498d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath    private final TtsEngines mEnginesHelper;
49968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    private final String mPackageName;
500bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    private volatile String mCurrentEngine = null;
501e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
502e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
5034bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the default TTS engine.
504a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * This will also initialize the associated TextToSpeech engine if it isn't already running.
505e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
506e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param context
507a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The context this instance is running in.
50891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * @param listener
509a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The {@link TextToSpeech.OnInitListener} that will be called when the
510a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            TextToSpeech engine has initialized.
511e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
51291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    public TextToSpeech(Context context, OnInitListener listener) {
51350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        this(context, listener, null);
51450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
51550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
51650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
5174bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the given TTS engine.
5184bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * This will also initialize the associated TextToSpeech engine if it isn't already running.
5194bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *
5204bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param context
5214bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The context this instance is running in.
5224bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param listener
5234bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The {@link TextToSpeech.OnInitListener} that will be called when the
5244bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            TextToSpeech engine has initialized.
5254bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param engine Package name of the TTS engine to use.
52650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
52750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public TextToSpeech(Context context, OnInitListener listener, String engine) {
528c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        this(context, listener, engine, null, true);
52968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    }
53068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath
53168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    /**
53268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * Used by the framework to instantiate TextToSpeech objects with a supplied
53368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * package name, instead of using {@link android.content.Context#getPackageName()}
53468e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     *
53568e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * @hide
53668e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     */
53768e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    public TextToSpeech(Context context, OnInitListener listener, String engine,
538c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            String packageName, boolean useFallback) {
53991bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mContext = context;
54091bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mInitListener = listener;
54150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = engine;
542c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        mUseFallback = useFallback;
54387c9684fd0fa31fd6ad7f7e9f4cfedddc4fdc4b0Jean-Michel Trivi
54450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mEarcons = new HashMap<String, Uri>();
54550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mUtterances = new HashMap<String, Uri>();
54668e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        mUtteranceProgressListener = null;
547a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
548d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        mEnginesHelper = new TtsEngines(mContext);
54968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        if (packageName != null) {
55068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = packageName;
55168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        } else {
55268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = mContext.getPackageName();
55368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        }
554da7681e7b61dd450be72f2b4a80e6d1c86342e05Jean-Michel Trivi        initTts();
555e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
556e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
55750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method) {
55850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(action, errorResult, method, false);
55950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
56050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
56150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runAction(Action<R> action, R errorResult, String method) {
56250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(action, errorResult, method, true);
56350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
56450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
56550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
56650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
56750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mServiceConnection == null) {
56850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Log.w(TAG, method + " failed: not bound to TTS engine");
56950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return errorResult;
570e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
57150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mServiceConnection.runAction(action, errorResult, method, reconnect);
57250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
57350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
574e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
57550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private int initTts() {
576b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 1: Try connecting to the engine that was requested.
577c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        if (mRequestedEngine != null) {
578c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
579c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                if (connectToEngine(mRequestedEngine)) {
580c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = mRequestedEngine;
581c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return SUCCESS;
582c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                } else if (!mUseFallback) {
583c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = null;
584c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    dispatchOnInit(ERROR);
585c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return ERROR;
586c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                }
587c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            } else if (!mUseFallback) {
588c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                Log.i(TAG, "Requested engine not installed: " + mRequestedEngine);
589c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                mCurrentEngine = null;
590c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                dispatchOnInit(ERROR);
591c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                return ERROR;
592b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath            }
59350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
59450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
595b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 2: Try connecting to the user's default engine.
596b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        final String defaultEngine = getDefaultEngine();
597b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) {
59850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (connectToEngine(defaultEngine)) {
599b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = defaultEngine;
60050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
601e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
60250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
603e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
604b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 3: Try connecting to the highest ranked engine in the
605b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // system.
606d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        final String highestRanked = mEnginesHelper.getHighestRankedEngineName();
607b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (highestRanked != null && !highestRanked.equals(mRequestedEngine) &&
608b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                !highestRanked.equals(defaultEngine)) {
60922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath            if (connectToEngine(highestRanked)) {
610b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = highestRanked;
61150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
6121e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi            }
61350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
61450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
615b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // NOTE: The API currently does not allow the caller to query whether
616b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // they are actually connected to any engine. This might fail for various
617b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // reasons like if the user disables all her TTS engines.
618b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath
619b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        mCurrentEngine = null;
6200e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath        dispatchOnInit(ERROR);
62150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
62250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
62350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
62450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private boolean connectToEngine(String engine) {
62550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Connection connection = new Connection();
62650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
62750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        intent.setPackage(engine);
62850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
62950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!bound) {
63050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Log.e(TAG, "Failed to bind to " + engine);
63150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return false;
6321e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        } else {
6330e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath            Log.i(TAG, "Sucessfully bound to " + engine);
63450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return true;
6351e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        }
636e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
637e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
63850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void dispatchOnInit(int result) {
63950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
64050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mInitListener != null) {
64150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener.onInit(result);
64250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener = null;
64350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
64450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
64550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
646e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
647492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    private IBinder getCallerIdentity() {
648492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        return mServiceConnection.getCallerIdentity();
649492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    }
650492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
651e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
652a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Releases the resources used by the TextToSpeech engine.
653a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * It is good practice for instance to call this method in the onDestroy() method of an Activity
654a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * so the TextToSpeech engine can be cleanly stopped.
655e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
656e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public void shutdown() {
65750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        runActionNoReconnect(new Action<Void>() {
65850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
65950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Void run(ITextToSpeechService service) throws RemoteException {
660492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.setCallback(getCallerIdentity(), null);
661492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.stop(getCallerIdentity());
66250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mServiceConnection.disconnect();
66390e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // Context#unbindService does not result in a call to
66490e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection#onServiceDisconnected. As a result, the
66590e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // service ends up being destroyed (if there are no other open
66690e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // connections to it) but the process lives on and the
66790e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection continues to refer to the destroyed service.
66890e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                //
66990e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // This leads to tons of log spam about SynthThread being dead.
67090e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                mServiceConnection = null;
671bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath                mCurrentEngine = null;
67250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return null;
67350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
67450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "shutdown");
675e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
676e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
677e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
678e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound resource in a
679a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. After a call to this method, subsequent calls to
680a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * {@link #speak(String, int, HashMap)} will play the specified sound resource
681a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * if it is available, or synthesize the text it is missing.
682e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
683e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
684a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
685e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
686e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param packagename
687e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Pass the packagename of the application that contains the
688e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            resource. If the resource is in your own application (this is
689e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            the most common case), then put the packagename of your
690e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            application here.<br/>
691e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Example: <b>"com.google.marvin.compass"</b><br/>
692e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The packagename can be found in the AndroidManifest.xml of
693e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            your application.
694e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <p>
695e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
696e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
697e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            </p>
698e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
699e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param resourceId
700a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.south_south_east</code>
7015c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
702ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
703e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7045c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String packagename, int resourceId) {
70550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
70650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, makeResourceUri(packagename, resourceId));
70750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
708e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
709e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
710e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
711e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
712e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound file. Using this, it
713a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * is possible to add custom pronounciations for a string of text.
714a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
715a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will play the specified sound resource if it is available, or synthesize the text it is
716a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * missing.
717e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
718e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
719a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
720e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param filename
721e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The full path to the sound file (for example:
722e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            "/sdcard/mysounds/hello.wav")
7235c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
724ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
725e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7265c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String filename) {
72791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        synchronized (mStartLock) {
72850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, Uri.parse(filename));
72950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
730e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
731e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
732e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
733e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
734e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
735904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * Adds a mapping between a string of text and a sound resource in a
736a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. Use this to add custom earcons.
737904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
738ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
739904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
740a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @param earcon The name of the earcon.
741a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code><br/>
742904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
743904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param packagename
744a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the package name of the application that contains the
745a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            resource. This can for instance be the package name of your own application.
746904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            Example: <b>"com.google.marvin.compass"</b><br/>
747a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The package name can be found in the AndroidManifest.xml of
748a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the application containing the resource.
749904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <p>
750904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
751904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
752904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            </p>
753904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
754904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param resourceId
755a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.tick_snd</code>
756904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
757ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
758904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
759904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String packagename, int resourceId) {
760904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        synchronized(mStartLock) {
76150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, makeResourceUri(packagename, resourceId));
76250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
763904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
764904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
765904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
766904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
767a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Adds a mapping between a string of text and a sound file.
768a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Use this to add custom earcons.
769a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *
770a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
771904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
772904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param earcon
773a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The name of the earcon.
774a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code>
775904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param filename
776904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            The full path to the sound file (for example:
777904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            "/sdcard/mysounds/tick.wav")
778904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
779ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
780904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
781904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String filename) {
78250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized(mStartLock) {
78350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, Uri.parse(filename));
78450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
785904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
786904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
787904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
78850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Uri makeResourceUri(String packageName, int resourceId) {
78950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return new Uri.Builder()
79050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
79150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .encodedAuthority(packageName)
79250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .appendEncodedPath(String.valueOf(resourceId))
79350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .build();
79450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
795904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
796904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
797442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * Speaks the string using the specified queuing strategy and speech parameters.
798442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
799442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
800442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
801442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
802442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
803442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
804e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
8052d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The string of text to be spoken. No longer than
8062d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
80750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
80850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
80950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
81050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
81150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_UTTERANCE_ID},
81250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_VOLUME},
81350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_PAN}.
814b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
815b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
816b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
817b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8185c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
819442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation.
820e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
82150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int speak(final String text, final int queueMode, final HashMap<String, String> params) {
82250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
82350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
82450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
82550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri utteranceUri = mUtterances.get(text);
82650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (utteranceUri != null) {
827492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
82850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            getParams(params));
82950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                } else {
830492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.speak(getCallerIdentity(), text, queueMode, getParams(params));
831a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
832e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
83350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "speak");
834e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
835e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
836e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
837e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Plays the earcon using the specified queueing mode and parameters.
83850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The earcon must already have been added with {@link #addEarcon(String, String)} or
83950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * {@link #addEarcon(String, String, int)}.
840442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
841442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
842442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
843442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
844442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
845442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
846e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
84750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param earcon The earcon that should be played
84850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
84950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
85050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
85150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
852a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
853b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
854b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
855b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
856b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8575c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
858442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation.
859e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
86050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playEarcon(final String earcon, final int queueMode,
86150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
86250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
86350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
86450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
86550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri earconUri = mEarcons.get(earcon);
86650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (earconUri == null) {
86750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return ERROR;
868a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
869492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playAudio(getCallerIdentity(), earconUri, queueMode,
87050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
871e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
87250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playEarcon");
873e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
874679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi
8755c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    /**
8765c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * Plays silence for the specified amount of time using the specified
8775c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * queue mode.
878442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
879442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
880442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
881442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
882442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
883442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
8845c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
88550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param durationInMs The duration of the silence.
88650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
88750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
88850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
889a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
890b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
891b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
892b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
893b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8945c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
895442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilence operation.
8965c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     */
89750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playSilence(final long durationInMs, final int queueMode,
89850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
89950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
90050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
90150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
902492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playSilence(getCallerIdentity(), durationInMs, queueMode,
90350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
904f032bc7da536774a0b6a1c77632c65b935eee6faCharles Chen            }
90550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playSilence");
906a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi    }
907e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
908e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
909748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Queries the engine for the set of features it supports for a given locale.
910748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features can either be framework defined, e.g.
911748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
912748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Engine specific keys must be prefixed by the name of the engine they
913748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * are intended for. These keys can be used as parameters to
914748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
915748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
916748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
917748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features are boolean flags, and their values in the synthesis parameters
918748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * must be behave as per {@link Boolean#parseBoolean(String)}.
919748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
920748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * @param locale The locale to query features for.
921748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     */
922748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    public Set<String> getFeatures(final Locale locale) {
923748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        return runAction(new Action<Set<String>>() {
924748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            @Override
925748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            public Set<String> run(ITextToSpeechService service) throws RemoteException {
926748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                String[] features = service.getFeaturesForLanguage(
927748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                        locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
928748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                if (features != null) {
929748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    final Set<String> featureSet = new HashSet<String>();
930748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    Collections.addAll(featureSet, features);
931748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    return featureSet;
932748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                }
933748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                return null;
934748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            }
935748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        }, null, "getFeatures");
936748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    }
937748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
938748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    /**
939c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Checks whether the TTS engine is busy speaking. Note that a speech item is
940c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * considered complete once it's audio data has been sent to the audio mixer, or
941c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * written to a file. There might be a finite lag between this point, and when
942c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * the audio hardware completes playback.
943e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
94450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@code true} if the TTS engine is speaking.
945e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
946e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public boolean isSpeaking() {
94750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Boolean>() {
94850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
94950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Boolean run(ITextToSpeechService service) throws RemoteException {
95050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isSpeaking();
951e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
95250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, false, "isSpeaking");
953e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
954e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
955e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
956a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interrupts the current utterance (whether played or rendered to file) and discards other
957a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * utterances in the queue.
9585c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
95950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
960e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
9615c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int stop() {
96250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
96350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
96450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
965492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.stop(getCallerIdentity());
966e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
96750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "stop");
968e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
969e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
970e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
97150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the speech rate.
972e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
973679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
974e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
97550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
97650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
97750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
9785c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
97950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
980e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
9815c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setSpeechRate(float speechRate) {
98250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (speechRate > 0.0f) {
98350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intRate = (int)(speechRate * 100);
98450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intRate > 0) {
98550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
98650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
987679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi                }
98850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
989e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
990e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
99150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
992e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
993e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
994e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
995a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Sets the speech pitch for the TextToSpeech engine.
9962ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
9972ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
9982ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
99950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
10002ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            lower values lower the tone of the synthesized voice,
10012ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            greater values increase it.
10025c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
100350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
10042ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     */
10055c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setPitch(float pitch) {
100650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (pitch > 0.0f) {
100750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intPitch = (int)(pitch * 100);
100850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intPitch > 0) {
100950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
101050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
10112ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi                }
101250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
10132ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi            }
10142ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi        }
101550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
10162ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    }
10172ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi
10182ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    /**
1019bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @return the engine currently in use by this TextToSpeech instance.
1020bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @hide
1021bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     */
1022bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    public String getCurrentEngine() {
1023bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath        return mCurrentEngine;
1024bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    }
1025bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath
1026bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    /**
1027b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used as the default
1028b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Text-to-speech language.
1029b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *
1030b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1031b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1032b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     */
1033b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    public Locale getDefaultLanguage() {
1034b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        return runAction(new Action<Locale>() {
1035b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            @Override
1036b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) throws RemoteException {
1037b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                String[] defaultLanguage = service.getClientDefaultLanguage();
1038b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1039b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
1040b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            }
1041b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        }, null, "getDefaultLanguage");
1042b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    }
1043b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1044b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    /**
104550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the text-to-speech language.
104650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The TTS engine will try to use the closest match to the specified
1047a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * language as represented by the Locale, but there is no guarantee that the exact same Locale
1048a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1049a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * before choosing the language to use for the next utterances.
1050e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
105150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The locale describing the language to be used.
10525c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
105350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1054ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1055ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1056e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
105750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setLanguage(final Locale loc) {
105850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
105950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
106050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
106150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (loc == null) {
106250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return LANG_NOT_SUPPORTED;
106350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
10641a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String language = loc.getISO3Language();
10651a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String country = loc.getISO3Country();
10661a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String variant = loc.getVariant();
10671a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Check if the language, country, variant are available, and cache
10681a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // the available parts.
10691a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Note that the language is not actually set here, instead it is cached so it
10701a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // will be associated with all upcoming utterances.
107113896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak
107213896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak                int result = service.loadLanguage(getCallerIdentity(), language, country, variant);
10731a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                if (result >= LANG_AVAILABLE){
107450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
107550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        variant = "";
107650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        if (result < LANG_COUNTRY_AVAILABLE) {
107750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            country = "";
107850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        }
10791a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                    }
108050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
108150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
108250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
10831a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                }
108450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return result;
1085e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
108650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "setLanguage");
1087e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1088e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1089aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
1090228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used for synthesis
1091228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * requests sent to the TextToSpeech engine.
109250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
1093228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android 4.2 and before (API <= 17) this function returns the language that is currently
1094228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * being used by the TTS engine. That is the last language set by this or any other
1095228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * client by a {@link TextToSpeech#setLanguage} call to the same engine.
1096228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1097228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android versions after 4.2 this function returns the language that is currently being
1098228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * used for the synthesis requests sent from this client. That is the last language set
1099228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * by a {@link TextToSpeech#setLanguage} call on this instance.
1100228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1101228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1102228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1103ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi     */
1104ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    public Locale getLanguage() {
110550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Locale>() {
110650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
1107228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) {
1108228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                /* No service call, but we're accessing mParams, hence need for
1109228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                   wrapping it as an Action instance */
1110228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, "");
1111228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, "");
1112228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, "");
1113228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                return new Locale(lang, country, variant);
1114ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
111550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "getLanguage");
1116ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    }
1117ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi
1118ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    /**
1119a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Checks if the specified language as represented by the Locale is available and supported.
1120aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     *
112150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The Locale describing the language to be used.
11225c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
112350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1124ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1125ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1126aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
112750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int isLanguageAvailable(final Locale loc) {
112850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
112950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
113050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
113150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isLanguageAvailable(loc.getISO3Language(),
113250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        loc.getISO3Country(), loc.getVariant());
1133ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
113450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
1135aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    }
1136aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
1137e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1138d4989093ed708ddf9c799655ea0af7afda726426Charles Chen     * Synthesizes the given text to a file using the specified parameters.
1139442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1140442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
1141442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
1142442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
1143442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
1144442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1145e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
11462d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The text that should be synthesized. No longer than
11472d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
114850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
114950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
1150a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1151b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
1152b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
1153b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1154b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
115550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param filename Absolute file filename to write the generated audio data to.It should be
1156e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            something like "/sdcard/myappsounds/mysound.wav".
11575c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
1158442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
1159e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
116050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int synthesizeToFile(final String text, final HashMap<String, String> params,
116150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final String filename) {
116250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
116350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
116450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1165492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.synthesizeToFile(getCallerIdentity(), text, filename,
116650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
1167e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
116850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "synthesizeToFile");
116950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
117050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
117150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Bundle getParams(HashMap<String, String> params) {
117250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (params != null && !params.isEmpty()) {
117350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Bundle bundle = new Bundle(mParams);
117450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
117550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
117650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
117750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
1178b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
1179748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            // Copy feature strings defined by the framework.
1180748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
11816c07a2028505e28abd601870f05d4752e92a4b7bNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1182748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1183b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // Copy over all parameters that start with the name of the
1184b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // engine that we are currently connected to. The engine is
1185b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // free to interpret them as it chooses.
1186b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            if (!TextUtils.isEmpty(mCurrentEngine)) {
1187b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                for (Map.Entry<String, String> entry : params.entrySet()) {
1188b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    final String key = entry.getKey();
1189b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    if (key != null && key.startsWith(mCurrentEngine)) {
1190b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                        bundle.putString(key, entry.getValue());
1191b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    }
1192b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                }
1193b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            }
1194b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
119550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return bundle;
119650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        } else {
119750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mParams;
1198e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
1199e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1200e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
120150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
120250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String value = params.get(key);
120350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (value != null) {
120450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            bundle.putString(key, value);
120550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
120650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
1207a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
120850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
120950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
121050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
121150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
121250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                int value = Integer.parseInt(valueString);
121350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putInt(key, value);
121450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
121550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
121650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
121750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
12189d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi    }
12199d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
122050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
122150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
122250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
122350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
122450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                float value = Float.parseFloat(valueString);
122550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putFloat(key, value);
122650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
122750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
122850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
12299d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        }
1230a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi    }
1231a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
123278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
123350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the listener that will be notified when synthesis of an utterance completes.
123478c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
123550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param listener The listener to use.
123678c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
123750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1238754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1239754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)}
1240754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *        instead.
124178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
1242754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    @Deprecated
124350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1244754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = UtteranceProgressListener.from(listener);
1245754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        return TextToSpeech.SUCCESS;
1246754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    }
1247754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1248754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    /**
1249754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * Sets the listener that will be notified of various events related to the
1250754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * synthesis of a given utterance.
1251754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1252754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * See {@link UtteranceProgressListener} and
1253754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}.
1254754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1255754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @param listener the listener to use.
1256754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @return {@link #ERROR} or {@link #SUCCESS}
1257754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     */
1258754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    public int setOnUtteranceProgressListener(UtteranceProgressListener listener) {
1259754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = listener;
1260a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        return TextToSpeech.SUCCESS;
126178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
126278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
1263b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    /**
126450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the TTS engine to use.
1265b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
12663f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     * @deprecated This doesn't inform callers when the TTS engine has been
12673f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
1268b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        can be used with the appropriate engine name. Also, there is no
1269b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        guarantee that the engine specified will be loaded. If it isn't
1270b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        installed or disabled, the user / system wide defaults will apply.
12713f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *
127250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
1273b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
127450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1275b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     */
12763f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath    @Deprecated
127760dd360640a400d9b4a602160733281d284aaee5Charles Chen    public int setEngineByPackageName(String enginePackageName) {
127850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = enginePackageName;
127950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return initTts();
1280b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    }
1281b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen
1282def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    /**
128350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets the package name of the default speech synthesis engine.
1284def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     *
128522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return Package name of the TTS engine that the user has chosen
128622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     *        as their default.
1287def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     */
1288def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    public String getDefaultEngine() {
1289d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getDefaultEngine();
129050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
129150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
129250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
1293c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * Checks whether the user's settings should override settings requested
1294c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * by the calling application. As of the Ice cream sandwich release,
1295c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * user settings never forcibly override the app's settings.
129650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
129750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public boolean areDefaultsEnforced() {
1298c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath        return false;
129950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
130050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
130142229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    /**
130250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets a list of all installed TTS engines.
130342229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     *
130422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return A list of engine info objects. The list can be empty, but never {@code null}.
130542229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     */
130650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public List<EngineInfo> getEngines() {
1307d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getEngines();
130822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath    }
130922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
131050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private class Connection implements ServiceConnection {
131150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        private ITextToSpeechService mService;
13128f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13138f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        private OnServiceConnectedAsyncTask mOnServiceConnectedAsyncTask;
13148f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1315a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() {
1316a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            @Override
1317754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onDone(String utteranceId) {
1318754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1319754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1320754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onDone(utteranceId);
1321754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1322754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1323754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1324754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1325754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onError(String utteranceId) {
1326754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1327754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1328754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onError(utteranceId);
1329754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1330754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1331754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1332754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1333754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onStart(String utteranceId) {
1334754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1335a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                if (listener != null) {
1336754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onStart(utteranceId);
1337a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
1338a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            }
1339a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        };
134050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
13418f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        private class OnServiceConnectedAsyncTask extends AsyncTask<Void, Void, Integer> {
13428f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            private final ComponentName mName;
13438f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            private final ITextToSpeechService mConnectedService;
13448f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13458f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            public OnServiceConnectedAsyncTask(ComponentName name, IBinder service) {
13468f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mName = name;
13478f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mConnectedService = ITextToSpeechService.Stub.asInterface(service);
13488f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
13498f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13508f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
13518f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected Integer doInBackground(Void... params) {
13528f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
13538f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    if (isCancelled()) {
13548f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return null;
13558f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
13568f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13578f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    try {
13588f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        mConnectedService.setCallback(getCallerIdentity(), mCallback);
1359b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        String[] defaultLanguage = mConnectedService.getClientDefaultLanguage();
1360b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1361b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]);
1362b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]);
1363b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]);
1364b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1365b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        Log.i(TAG, "Set up connection to " + mName);
13668f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return SUCCESS;
13678f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    } catch (RemoteException re) {
13688f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        Log.e(TAG, "Error connecting to service, setCallback() failed");
13698f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return ERROR;
13708f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
13718f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
13728f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
13738f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13748f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
13758f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected void onPostExecute(Integer result) {
13768f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
13778f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    if (mOnServiceConnectedAsyncTask == this) {
13788f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        mOnServiceConnectedAsyncTask = null;
13798f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
13808f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13818f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    mServiceConnection = Connection.this;
13828f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    mService = mConnectedService;
13838f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13848f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    dispatchOnInit(result);
13858f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
13868f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
13878f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
13888f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1389091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak        @Override
139050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void onServiceConnected(ComponentName name, IBinder service) {
139150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
13928f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                Log.i(TAG, "Connected to " + name);
13938f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13948f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                if (mOnServiceConnectedAsyncTask != null) {
13958f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    mOnServiceConnectedAsyncTask.cancel(false);
1396a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
13978f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13988f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mOnServiceConnectedAsyncTask = new OnServiceConnectedAsyncTask(name, service);
13998f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mOnServiceConnectedAsyncTask.execute();
140050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
140150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
140250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1403492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        public IBinder getCallerIdentity() {
1404492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath            return mCallback;
1405492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        }
1406492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
14078f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        /**
14088f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set.
14098f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         *
14108f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         * @return true if we cancel mOnServiceConnectedAsyncTask in progress.
14118f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         */
14128f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        private boolean clearServiceConnection() {
141350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
14148f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                boolean result = false;
14158f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                if (mOnServiceConnectedAsyncTask != null) {
14168f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    result = mOnServiceConnectedAsyncTask.cancel(false);
14178f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    mOnServiceConnectedAsyncTask = null;
14188f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
14198f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
142050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mService = null;
142150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // If this is the active connection, clear it
142250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (mServiceConnection == this) {
142350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mServiceConnection = null;
142450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
14258f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                return result;
14268f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
14278f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
14288f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14298f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        @Override
14308f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        public void onServiceDisconnected(ComponentName name) {
14318f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            Log.i(TAG, "Asked to disconnect from " + name);
14328f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            if (clearServiceConnection()) {
14338f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                /* We need to protect against a rare case where engine
14348f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * dies just after successful connection - and we process onServiceDisconnected
14358f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels
14368f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit
14378f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * with ERROR as argument.
14388f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 */
14398f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                dispatchOnInit(ERROR);
144050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
144150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
144250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
144350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void disconnect() {
144450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mContext.unbindService(this);
14458f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            clearServiceConnection();
144650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
144750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
144850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
1449091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak            synchronized (mStartLock) {
1450091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                try {
145150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (mService == null) {
145250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        Log.w(TAG, method + " failed: not connected to TTS engine");
145350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        return errorResult;
145450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    }
145550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return action.run(mService);
1456091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                } catch (RemoteException ex) {
1457091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    Log.e(TAG, method + " failed", ex);
1458091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    if (reconnect) {
1459091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        disconnect();
1460091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        initTts();
1461091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    }
1462091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    return errorResult;
146350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
146442229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen            }
146542229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen        }
146642229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    }
14672cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
146850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private interface Action<R> {
146950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        R run(ITextToSpeechService service) throws RemoteException;
147050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
147150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
14722cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    /**
147350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Information about an installed text-to-speech engine.
147450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
147550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @see TextToSpeech#getEngines
14762cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert     */
147750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static class EngineInfo {
147850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
147950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Engine package name..
148050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
148150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String name;
148250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
148350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Localized label for the engine.
148450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
148550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String label;
148650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
148750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Icon for the engine.
148850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
148950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public int icon;
149022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
149122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * Whether this engine is a part of the system
149222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * image.
1493d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1494d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
149522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1496d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public boolean system;
149722302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
149822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * The priority the engine declares for the the intent filter
149922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * {@code android.intent.action.TTS_SERVICE}
1500d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1501d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
150222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1503d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public int priority;
150450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
150550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @Override
150650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String toString() {
150750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return "EngineInfo{name=" + name + "}";
150850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
150950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
15102cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    }
151122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
15122d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    /**
15132d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * Limit of length of input string passed to speak and synthesizeToFile.
15142d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *
15152d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #speak
15162d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #synthesizeToFile
15172d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     */
15182d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    public static int getMaxSpeechInputLength() {
15192d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak        return 4000;
15202d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    }
1521e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi}
1522