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;
2750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.os.Bundle;
28e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.IBinder;
29e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.RemoteException;
3050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.provider.Settings;
3150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.text.TextUtils;
32e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.util.Log;
33e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
34748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Collections;
35a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Triviimport java.util.HashMap;
36748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.HashSet;
3750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.List;
38679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Triviimport java.util.Locale;
3950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.Map;
40748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Set;
41a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi
42e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi/**
43e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
4462788e9b48f884a35b89c88911b581daa6a14e08Jean-Michel Trivi * Synthesizes speech from text for immediate playback or to create a sound file.
45a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
46a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
47a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * notified of the completion of the initialization.<br>
48a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
49a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * to release the native resources used by the TextToSpeech engine.
50e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
51e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi */
52a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivipublic class TextToSpeech {
53e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
542cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    private static final String TAG = "TextToSpeech";
552cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
5691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
5791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a successful operation.
5891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
5950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int SUCCESS = 0;
6091bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
6191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a generic operation failure.
6291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int ERROR = -1;
6491bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi
65679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
66679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where all entries in the playback queue (media to be played
67679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * and text to be synthesized) are dropped and replaced by the new entry.
68abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queues are flushed with respect to a given calling app. Entries in the queue
69abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from other callees are not discarded.
70679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
71ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_FLUSH = 0;
72679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
73679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where the new entry is added at the end of the playback queue.
74679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
75ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_ADD = 1;
76abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    /**
77abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queue mode where the entire playback queue is purged. This is different
78abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries
79abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from a given caller.
80abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     *
81abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * @hide
82abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     */
83abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    static final int QUEUE_DESTROY = 2;
84e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
85aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
869f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi     * Denotes the language is available exactly as specified by the locale.
87aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
88ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
89aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
90aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
91b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language and country specified
92aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * by the locale, but not the variant.
93aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
94ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_AVAILABLE = 1;
95aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
96aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
97b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language by the locale,
98aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * but not the country and variant.
99aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
100ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_AVAILABLE = 0;
101aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
102aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
103aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * Denotes the language data is missing.
104aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
105ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_MISSING_DATA = -1;
106aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
107aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
108a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Denotes the language is not supported.
109aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
110ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_NOT_SUPPORTED = -2;
111ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
112ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    /**
113ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * Broadcast Action: The TextToSpeech synthesizer has completed processing
114ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * of all the text in the speech queue.
115c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     *
116c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Note that this notifies callers when the <b>engine</b> has finished has
117c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * processing text data. Audio playback might not have completed (or even started)
118c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * at this point. If you wish to be notified when this happens, see
119c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * {@link OnUtteranceCompletedListener}.
120ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     */
121ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
122ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
123ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi            "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
124aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
125e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
126a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interface definition of a callback to be invoked indicating the completion of the
127a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * TextToSpeech engine initialization.
128e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
129e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public interface OnInitListener {
130a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
131a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Called to signal the completion of the TextToSpeech engine initialization.
13250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
133a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
134a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
13591bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        public void onInit(int status);
136e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
137e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
138e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
13950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Listener that will be called when the TTS service has
14050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * completed synthesizing an utterance. This is only called if the utterance
14150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}).
14278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
143a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi    public interface OnUtteranceCompletedListener {
144a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
14550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Called when an utterance has been synthesized.
14650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
147a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param utteranceId the identifier of the utterance.
14860dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
14960dd360640a400d9b4a602160733281d284aaee5Charles Chen        public void onUtteranceCompleted(String utteranceId);
15078c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
15178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
15278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
153748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Constants and parameter names for controlling text-to-speech. These include:
154748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
155748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * <ul>
156748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
157748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Intents to ask engine to install data or check its data and
158748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         extras for a TTS engine's check data activity.
159748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
160748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
161748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Keys for the parameters passed with speak commands, e.g.
162748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}.
163748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
164748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
165748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         A list of feature strings that engines might support, e.g
166748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to
167748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify
168748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         engine behaviour. The engine can be queried for the set of features it supports
169748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         through {@link TextToSpeech#getFeatures(java.util.Locale)}.
170748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
171748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * </ul>
172d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi     */
173d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    public class Engine {
17450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
17562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
17650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default speech rate.
17750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
17862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
17950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_RATE = 100;
18050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pitch.
18350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
18462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
18550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_PITCH = 100;
18650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default volume.
18950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
19062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
1919d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_VOLUME = 1.0f;
19250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1939d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
19450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pan (centered).
19550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
1969d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
1979d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_PAN = 0.0f;
1989d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
1999d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
20050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}.
20150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
2029d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
203ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int USE_DEFAULTS = 0; // false
20450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
20562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
20650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Package name of the default TTS engine.
20750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
20850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
20922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * @deprecated No longer in use, the default engine is determined by
210b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath         *         the sort order defined in {@link TtsEngines}. Note that
21122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         this doesn't "break" anything because there is no guarantee that
21222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         the engine specified below is installed on a given build, let
21322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         alone be the default.
21462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
21522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        @Deprecated
21650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String DEFAULT_ENGINE = "com.svox.pico";
217d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
218a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
219a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Default audio stream used when playing synthesized speech.
220a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
221ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
222a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
22362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
22462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates success when checking the installation status of the resources used by the
225a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
22662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
227d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_PASS = 1;
22850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
22962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates failure when checking the installation status of the resources used by the
231a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
233d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_FAIL = 0;
23450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
23562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates erroneous data when checking the installation status of the resources used by
237a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
239d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
24050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing resources when checking the installation status of the resources used
243a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
24462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
245d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
24650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing storage volume when checking the installation status of the resources
249a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
25062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
25162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
25299a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
25350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
25450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Intent for starting a TTS service. Services that handle this intent must
25550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * extend {@link TextToSpeechService}. Normal applications should not use this intent
25650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * directly, instead they should talk to the TTS service using the the methods in this
25750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * class.
25850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
25950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.SERVICE_ACTION)
26050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String INTENT_ACTION_TTS_SERVICE =
26150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.intent.action.TTS_SERVICE";
26250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
2634d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        /**
2644d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * Name under which a text to speech engine publishes information about itself.
2654d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * This meta-data should reference an XML resource containing a
2664d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * <code>&lt;{@link android.R.styleable#TextToSpeechEngine tts-engine}&gt;</code>
2674d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * tag.
2684d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         */
2694d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        public static final String SERVICE_META_DATA = "android.speech.tts";
2704d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath
271ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        // intents to ask engine to install data or check its data
272ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
273a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Triggers the platform TextToSpeech engine to
274ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * start the activity that installs the resource files on the device
275ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * that are required for TTS to be operational. Since the installation
276ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * of the data can be interrupted or declined by the user, the application
277ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * shouldn't expect successful installation upon return from that intent,
278ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * and if need be, should check installation status with
279ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #ACTION_CHECK_TTS_DATA}.
280ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
2819f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
282ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_INSTALL_TTS_DATA =
283ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.INSTALL_TTS_DATA";
284ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
285ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
2869f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * Broadcast Action: broadcast to signal the completion of the installation of
2879f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * the data files used by the synthesis engine. Success or failure is indicated in the
2889f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * {@link #EXTRA_TTS_DATA_INSTALLED} extra.
28977a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
29077a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
29177a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String ACTION_TTS_DATA_INSTALLED =
29277a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi                "android.speech.tts.engine.TTS_DATA_INSTALLED";
29350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
29477a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
295a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Starts the activity from the platform TextToSpeech
296ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * engine to verify the proper installation and availability of the
297ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * resource files on the system. Upon completion, the activity will
298ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * return one of the following codes:
299ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_PASS},
300ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_FAIL},
301ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_BAD_DATA},
302ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_MISSING_DATA}, or
303ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_MISSING_VOLUME}.
304ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <p> Moreover, the data received in the activity result will contain the following
305ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * fields:
306ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <ul>
307ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         *   <li>{@link #EXTRA_VOICE_DATA_ROOT_DIRECTORY} which
308a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         *       indicates the path to the location of the resource files,</li>
309ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         *   <li>{@link #EXTRA_VOICE_DATA_FILES} which contains
310a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         *       the list of all the resource files,</li>
311ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         *   <li>and {@link #EXTRA_VOICE_DATA_FILES_INFO} which
312ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         *       contains, for each resource file, the description of the language covered by
313ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         *       the file in the xxx-YYY format, where xxx is the 3-letter ISO language code,
314ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         *       and YYY is the 3-letter ISO country code.</li>
315ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * </ul>
316ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
3179f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
318ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_CHECK_TTS_DATA =
319ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.CHECK_TTS_DATA";
320ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
32150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
32250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Activity intent for getting some sample text to use for demonstrating TTS.
32350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
32450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide This intent was used by engines written against the old API.
32550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Not sure if it should be exposed.
32650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
32750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
32850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String ACTION_GET_SAMPLE_TEXT =
32950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.speech.tts.engine.GET_SAMPLE_TEXT";
33050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
331ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        // extras for a TTS engine's check data activity
33262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
333ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
334a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine specifies the path to its resources.
33562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
336ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
33750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
33862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
339ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
340a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine specifies the file names of its resources under the
34162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * resource path.
34262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
343ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
34450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
34562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
346ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
347a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine specifies the locale associated with each resource file.
34862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
349ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
35050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
35176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
35276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
35376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the available voices.
35476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
35576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
35676d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
35776d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_AVAILABLE_VOICES = "availableVoices";
35850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
35976d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
36076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
36176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
36276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
36376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
36476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
36576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
36650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
36776d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
36876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
36976d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * caller indicates to the TextToSpeech engine which specific sets of voice data to
37076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * check for by sending an ArrayList<String> of the voices that are of interest.
37176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
37276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
37376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
37476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
37599a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
37677a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        // extras for a TTS engine's data installation
37777a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
3789f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent.
3799f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * It indicates whether the data files for the synthesis engine were successfully
3809f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * installed. The installation was initiated with the  {@link #ACTION_INSTALL_TTS_DATA}
3819f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * intent. The possible values for this extra are
3829f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
38377a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
38477a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
38577a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi
38662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // keys for the parameters passed with speak commands. Hidden keys are used internally
38762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // to maintain engine state for each TextToSpeech instance.
38862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
38950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
39062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
391ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_RATE = "rate";
39250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
39362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
39450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
39562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
396ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_LANGUAGE = "language";
39750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
39862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
39950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
40062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
401ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_COUNTRY = "country";
40250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
40362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
40450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
40562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
406ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_VARIANT = "variant";
40750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
40862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
40950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
41060dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
41160dd360640a400d9b4a602160733281d284aaee5Charles Chen        public static final String KEY_PARAM_ENGINE = "engine";
41250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
41360dd360640a400d9b4a602160733281d284aaee5Charles Chen        /**
41450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
4151a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen         */
4161a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        public static final String KEY_PARAM_PITCH = "pitch";
41750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4181a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        /**
41962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Parameter key to specify the audio stream type to be used when speaking text
42050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * or playing back a file. The value should be one of the STREAM_ constants
42150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * defined in {@link AudioManager}.
42250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
423a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
424a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
42562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
426ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_STREAM = "streamType";
42750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
42862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
429a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Parameter key to identify an utterance in the
430a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
43162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * spoken, a file has been played back or a silence duration has elapsed.
43250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
433a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
434a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
435a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
43662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
437ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
43850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4399d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4409d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify the speech volume relative to the current stream type
4419d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
4429011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * where 0 is silence, and 1 is the maximum volume (the default behavior).
44350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4449d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4459d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4469d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4479d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_VOLUME = "volume";
44850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4499d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4509d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify how the speech is panned from left to right when speaking text.
4519d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
4529011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * 0 to center (the default behavior), and +1 to hard-right.
45350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4549d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4559d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4569d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4579d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_PAN = "pan";
45862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi
459748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
460748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)}
461748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set (and supported by the engine
462748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must
463748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * use network based synthesis.
464748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         *
465748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#speak(String, int, java.util.HashMap)
466748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
467748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#getFeatures(java.util.Locale)
468748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
469748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
470748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
471748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
472748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)}
473748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set and supported by the engine
474748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize
475748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * text on-device (without making network requests).
476748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
477748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
478d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    }
479d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
48050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Context mContext;
48150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Connection mServiceConnection;
48250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private OnInitListener mInitListener;
483a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // Written from an unspecified application thread, read from
484a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // a binder thread.
485754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    private volatile UtteranceProgressListener mUtteranceProgressListener;
48691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    private final Object mStartLock = new Object();
48750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
48850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private String mRequestedEngine;
489c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // Whether to initialize this TTS object with the default engine,
490c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // if the requested engine is not available. Valid only if mRequestedEngine
491c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // is not null. Used only for testing, though potentially useful API wise
492c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // too.
493c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    private final boolean mUseFallback;
49450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mEarcons;
49550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mUtterances;
49650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Bundle mParams = new Bundle();
497d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath    private final TtsEngines mEnginesHelper;
49868e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    private final String mPackageName;
499bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    private volatile String mCurrentEngine = null;
500e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
501e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
5024bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the default TTS engine.
503a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * This will also initialize the associated TextToSpeech engine if it isn't already running.
504e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
505e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param context
506a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The context this instance is running in.
50791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * @param listener
508a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The {@link TextToSpeech.OnInitListener} that will be called when the
509a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            TextToSpeech engine has initialized.
510e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
51191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    public TextToSpeech(Context context, OnInitListener listener) {
51250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        this(context, listener, null);
51350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
51450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
51550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
5164bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the given TTS engine.
5174bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * This will also initialize the associated TextToSpeech engine if it isn't already running.
5184bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *
5194bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param context
5204bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The context this instance is running in.
5214bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param listener
5224bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The {@link TextToSpeech.OnInitListener} that will be called when the
5234bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            TextToSpeech engine has initialized.
5244bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param engine Package name of the TTS engine to use.
52550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
52650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public TextToSpeech(Context context, OnInitListener listener, String engine) {
527c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        this(context, listener, engine, null, true);
52868e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    }
52968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath
53068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    /**
53168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * Used by the framework to instantiate TextToSpeech objects with a supplied
53268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * package name, instead of using {@link android.content.Context#getPackageName()}
53368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     *
53468e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * @hide
53568e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     */
53668e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    public TextToSpeech(Context context, OnInitListener listener, String engine,
537c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            String packageName, boolean useFallback) {
53891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mContext = context;
53991bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mInitListener = listener;
54050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = engine;
541c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        mUseFallback = useFallback;
54287c9684fd0fa31fd6ad7f7e9f4cfedddc4fdc4b0Jean-Michel Trivi
54350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mEarcons = new HashMap<String, Uri>();
54450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mUtterances = new HashMap<String, Uri>();
54568e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        mUtteranceProgressListener = null;
546a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
547d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        mEnginesHelper = new TtsEngines(mContext);
54868e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        if (packageName != null) {
54968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = packageName;
55068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        } else {
55168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = mContext.getPackageName();
55268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        }
553da7681e7b61dd450be72f2b4a80e6d1c86342e05Jean-Michel Trivi        initTts();
554e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
555e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
55650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method) {
55750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(action, errorResult, method, false);
55850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
55950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
56050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runAction(Action<R> action, R errorResult, String method) {
56150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(action, errorResult, method, true);
56250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
56350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
56450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
56550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
56650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mServiceConnection == null) {
56750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Log.w(TAG, method + " failed: not bound to TTS engine");
56850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return errorResult;
569e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
57050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mServiceConnection.runAction(action, errorResult, method, reconnect);
57150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
57250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
573e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
57450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private int initTts() {
575b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 1: Try connecting to the engine that was requested.
576c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        if (mRequestedEngine != null) {
577c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
578c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                if (connectToEngine(mRequestedEngine)) {
579c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = mRequestedEngine;
580c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return SUCCESS;
581c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                } else if (!mUseFallback) {
582c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = null;
583c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    dispatchOnInit(ERROR);
584c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return ERROR;
585c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                }
586c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            } else if (!mUseFallback) {
587c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                Log.i(TAG, "Requested engine not installed: " + mRequestedEngine);
588c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                mCurrentEngine = null;
589c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                dispatchOnInit(ERROR);
590c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                return ERROR;
591b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath            }
59250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
59350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
594b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 2: Try connecting to the user's default engine.
595b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        final String defaultEngine = getDefaultEngine();
596b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) {
59750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (connectToEngine(defaultEngine)) {
598b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = defaultEngine;
59950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
600e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
60150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
602e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
603b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 3: Try connecting to the highest ranked engine in the
604b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // system.
605d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        final String highestRanked = mEnginesHelper.getHighestRankedEngineName();
606b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (highestRanked != null && !highestRanked.equals(mRequestedEngine) &&
607b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                !highestRanked.equals(defaultEngine)) {
60822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath            if (connectToEngine(highestRanked)) {
609b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = highestRanked;
61050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
6111e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi            }
61250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
61350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
614b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // NOTE: The API currently does not allow the caller to query whether
615b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // they are actually connected to any engine. This might fail for various
616b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // reasons like if the user disables all her TTS engines.
617b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath
618b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        mCurrentEngine = null;
6190e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath        dispatchOnInit(ERROR);
62050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
62150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
62250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
62350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private boolean connectToEngine(String engine) {
62450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Connection connection = new Connection();
62550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
62650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        intent.setPackage(engine);
62750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
62850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!bound) {
62950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Log.e(TAG, "Failed to bind to " + engine);
63050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return false;
6311e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        } else {
6320e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath            Log.i(TAG, "Sucessfully bound to " + engine);
63350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return true;
6341e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        }
635e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
636e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
63750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void dispatchOnInit(int result) {
63850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
63950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mInitListener != null) {
64050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener.onInit(result);
64150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener = null;
64250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
64350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
64450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
645e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
646492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    private IBinder getCallerIdentity() {
647492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        return mServiceConnection.getCallerIdentity();
648492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    }
649492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
650e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
651a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Releases the resources used by the TextToSpeech engine.
652a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * It is good practice for instance to call this method in the onDestroy() method of an Activity
653a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * so the TextToSpeech engine can be cleanly stopped.
654e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
655e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public void shutdown() {
65650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        runActionNoReconnect(new Action<Void>() {
65750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
65850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Void run(ITextToSpeechService service) throws RemoteException {
659492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.setCallback(getCallerIdentity(), null);
660492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.stop(getCallerIdentity());
66150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mServiceConnection.disconnect();
66290e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // Context#unbindService does not result in a call to
66390e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection#onServiceDisconnected. As a result, the
66490e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // service ends up being destroyed (if there are no other open
66590e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // connections to it) but the process lives on and the
66690e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection continues to refer to the destroyed service.
66790e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                //
66890e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // This leads to tons of log spam about SynthThread being dead.
66990e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                mServiceConnection = null;
670bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath                mCurrentEngine = null;
67150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return null;
67250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
67350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "shutdown");
674e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
675e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
676e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
677e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound resource in a
678a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. After a call to this method, subsequent calls to
679a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * {@link #speak(String, int, HashMap)} will play the specified sound resource
680a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * if it is available, or synthesize the text it is missing.
681e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
682e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
683a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
684e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
685e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param packagename
686e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Pass the packagename of the application that contains the
687e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            resource. If the resource is in your own application (this is
688e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            the most common case), then put the packagename of your
689e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            application here.<br/>
690e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Example: <b>"com.google.marvin.compass"</b><br/>
691e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The packagename can be found in the AndroidManifest.xml of
692e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            your application.
693e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <p>
694e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
695e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
696e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            </p>
697e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
698e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param resourceId
699a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.south_south_east</code>
7005c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
701ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
702e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7035c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String packagename, int resourceId) {
70450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
70550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, makeResourceUri(packagename, resourceId));
70650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
707e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
708e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
709e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
710e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
711e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound file. Using this, it
712a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * is possible to add custom pronounciations for a string of text.
713a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
714a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will play the specified sound resource if it is available, or synthesize the text it is
715a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * missing.
716e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
717e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
718a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
719e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param filename
720e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The full path to the sound file (for example:
721e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            "/sdcard/mysounds/hello.wav")
7225c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
723ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
724e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7255c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String filename) {
72691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        synchronized (mStartLock) {
72750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, Uri.parse(filename));
72850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
729e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
730e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
731e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
732e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
733e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
734904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * Adds a mapping between a string of text and a sound resource in a
735a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. Use this to add custom earcons.
736904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
737ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
738904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
739a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @param earcon The name of the earcon.
740a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code><br/>
741904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
742904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param packagename
743a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the package name of the application that contains the
744a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            resource. This can for instance be the package name of your own application.
745904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            Example: <b>"com.google.marvin.compass"</b><br/>
746a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The package name can be found in the AndroidManifest.xml of
747a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the application containing the resource.
748904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <p>
749904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
750904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
751904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            </p>
752904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
753904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param resourceId
754a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.tick_snd</code>
755904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
756ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
757904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
758904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String packagename, int resourceId) {
759904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        synchronized(mStartLock) {
76050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, makeResourceUri(packagename, resourceId));
76150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
762904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
763904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
764904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
765904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
766a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Adds a mapping between a string of text and a sound file.
767a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Use this to add custom earcons.
768a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *
769a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
770904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
771904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param earcon
772a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The name of the earcon.
773a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code>
774904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param filename
775904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            The full path to the sound file (for example:
776904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            "/sdcard/mysounds/tick.wav")
777904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
778ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
779904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
780904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String filename) {
78150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized(mStartLock) {
78250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, Uri.parse(filename));
78350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
784904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
785904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
786904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
78750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Uri makeResourceUri(String packageName, int resourceId) {
78850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return new Uri.Builder()
78950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
79050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .encodedAuthority(packageName)
79150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .appendEncodedPath(String.valueOf(resourceId))
79250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .build();
79350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
794904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
795904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
796e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Speaks the string using the specified queuing strategy and speech
797a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * parameters.
798e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
79950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param text The string of text to be spoken.
80050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
80150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
80250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
80350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
80450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_UTTERANCE_ID},
80550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_VOLUME},
80650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_PAN}.
807b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
808b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
809b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
810b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8115c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
81250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
813e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
81450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int speak(final String text, final int queueMode, final HashMap<String, String> params) {
81550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
81650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
81750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
81850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri utteranceUri = mUtterances.get(text);
81950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (utteranceUri != null) {
820492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
82150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            getParams(params));
82250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                } else {
823492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.speak(getCallerIdentity(), text, queueMode, getParams(params));
824a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
825e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
82650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "speak");
827e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
828e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
829e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
830e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Plays the earcon using the specified queueing mode and parameters.
83150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The earcon must already have been added with {@link #addEarcon(String, String)} or
83250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * {@link #addEarcon(String, String, int)}.
833e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
83450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param earcon The earcon that should be played
83550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
83650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
83750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
83850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
839a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
840b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
841b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
842b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
843b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8445c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
84550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
846e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
84750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playEarcon(final String earcon, final int queueMode,
84850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
84950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
85050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
85150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
85250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri earconUri = mEarcons.get(earcon);
85350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (earconUri == null) {
85450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return ERROR;
855a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
856492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playAudio(getCallerIdentity(), earconUri, queueMode,
85750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
858e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
85950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playEarcon");
860e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
861679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi
8625c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    /**
8635c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * Plays silence for the specified amount of time using the specified
8645c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * queue mode.
8655c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
86650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param durationInMs The duration of the silence.
86750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
86850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
86950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
870a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
871b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
872b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
873b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
874b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8755c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
87650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
8775c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     */
87850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playSilence(final long durationInMs, final int queueMode,
87950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
88050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
88150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
88250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
883492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playSilence(getCallerIdentity(), durationInMs, queueMode,
88450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
885f032bc7da536774a0b6a1c77632c65b935eee6faCharles Chen            }
88650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playSilence");
887a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi    }
888e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
889e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
890748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Queries the engine for the set of features it supports for a given locale.
891748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features can either be framework defined, e.g.
892748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
893748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Engine specific keys must be prefixed by the name of the engine they
894748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * are intended for. These keys can be used as parameters to
895748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
896748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
897748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
898748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features are boolean flags, and their values in the synthesis parameters
899748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * must be behave as per {@link Boolean#parseBoolean(String)}.
900748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
901748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * @param locale The locale to query features for.
902748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     */
903748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    public Set<String> getFeatures(final Locale locale) {
904748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        return runAction(new Action<Set<String>>() {
905748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            @Override
906748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            public Set<String> run(ITextToSpeechService service) throws RemoteException {
907748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                String[] features = service.getFeaturesForLanguage(
908748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                        locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
909748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                if (features != null) {
910748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    final Set<String> featureSet = new HashSet<String>();
911748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    Collections.addAll(featureSet, features);
912748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    return featureSet;
913748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                }
914748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                return null;
915748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            }
916748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        }, null, "getFeatures");
917748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    }
918748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
919748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    /**
920c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Checks whether the TTS engine is busy speaking. Note that a speech item is
921c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * considered complete once it's audio data has been sent to the audio mixer, or
922c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * written to a file. There might be a finite lag between this point, and when
923c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * the audio hardware completes playback.
924e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
92550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@code true} if the TTS engine is speaking.
926e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
927e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public boolean isSpeaking() {
92850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Boolean>() {
92950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
93050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Boolean run(ITextToSpeechService service) throws RemoteException {
93150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isSpeaking();
932e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
93350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, false, "isSpeaking");
934e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
935e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
936e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
937a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interrupts the current utterance (whether played or rendered to file) and discards other
938a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * utterances in the queue.
9395c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
94050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
941e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
9425c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int stop() {
94350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
94450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
94550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
946492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.stop(getCallerIdentity());
947e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
94850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "stop");
949e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
950e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
951e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
95250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the speech rate.
953e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
954679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
955e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
95650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
95750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
95850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
9595c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
96050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
961e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
9625c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setSpeechRate(float speechRate) {
96350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (speechRate > 0.0f) {
96450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intRate = (int)(speechRate * 100);
96550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intRate > 0) {
96650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
96750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
968679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi                }
96950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
970e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
971e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
97250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
973e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
974e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
975e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
976a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Sets the speech pitch for the TextToSpeech engine.
9772ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
9782ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
9792ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
98050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
9812ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            lower values lower the tone of the synthesized voice,
9822ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            greater values increase it.
9835c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
98450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
9852ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     */
9865c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setPitch(float pitch) {
98750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (pitch > 0.0f) {
98850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intPitch = (int)(pitch * 100);
98950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intPitch > 0) {
99050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
99150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
9922ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi                }
99350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
9942ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi            }
9952ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi        }
99650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
9972ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    }
9982ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi
9992ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    /**
1000bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @return the engine currently in use by this TextToSpeech instance.
1001bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @hide
1002bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     */
1003bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    public String getCurrentEngine() {
1004bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath        return mCurrentEngine;
1005bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    }
1006bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath
1007bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    /**
100850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the text-to-speech language.
100950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The TTS engine will try to use the closest match to the specified
1010a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * language as represented by the Locale, but there is no guarantee that the exact same Locale
1011a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1012a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * before choosing the language to use for the next utterances.
1013e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
101450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The locale describing the language to be used.
10155c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
101650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1017ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1018ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1019e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
102050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setLanguage(final Locale loc) {
102150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
102250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
102350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
102450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (loc == null) {
102550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return LANG_NOT_SUPPORTED;
102650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
10271a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String language = loc.getISO3Language();
10281a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String country = loc.getISO3Country();
10291a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String variant = loc.getVariant();
10301a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Check if the language, country, variant are available, and cache
10311a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // the available parts.
10321a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Note that the language is not actually set here, instead it is cached so it
10331a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // will be associated with all upcoming utterances.
103450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                int result = service.loadLanguage(language, country, variant);
10351a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                if (result >= LANG_AVAILABLE){
103650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
103750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        variant = "";
103850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        if (result < LANG_COUNTRY_AVAILABLE) {
103950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            country = "";
104050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        }
10411a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                    }
104250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
104350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
104450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
10451a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                }
104650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return result;
1047e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
104850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "setLanguage");
1049e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1050e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1051aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
1052a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Returns a Locale instance describing the language currently being used by the TextToSpeech
1053a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * engine.
105450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
1055ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi     * @return language, country (if any) and variant (if any) used by the engine stored in a Locale
105650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *     instance, or {@code null} on error.
1057ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi     */
1058ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    public Locale getLanguage() {
105950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Locale>() {
106050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
106150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Locale run(ITextToSpeechService service) throws RemoteException {
106250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                String[] locStrings = service.getLanguage();
106350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (locStrings != null && locStrings.length == 3) {
106450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return new Locale(locStrings[0], locStrings[1], locStrings[2]);
1065ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi                }
106650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return null;
1067ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
106850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "getLanguage");
1069ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    }
1070ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi
1071ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    /**
1072a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Checks if the specified language as represented by the Locale is available and supported.
1073aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     *
107450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The Locale describing the language to be used.
10755c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
107650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1077ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1078ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1079aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
108050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int isLanguageAvailable(final Locale loc) {
108150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
108250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
108350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
108450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isLanguageAvailable(loc.getISO3Language(),
108550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        loc.getISO3Country(), loc.getVariant());
1086ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
108750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
1088aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    }
1089aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
1090e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1091d4989093ed708ddf9c799655ea0af7afda726426Charles Chen     * Synthesizes the given text to a file using the specified parameters.
1092e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
1093b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     * @param text The text that should be synthesized
109450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
109550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
1096a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1097b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
1098b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
1099b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1100b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
110150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param filename Absolute file filename to write the generated audio data to.It should be
1102e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            something like "/sdcard/myappsounds/mysound.wav".
11035c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
110450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1105e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
110650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int synthesizeToFile(final String text, final HashMap<String, String> params,
110750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final String filename) {
110850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
110950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
111050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1111492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.synthesizeToFile(getCallerIdentity(), text, filename,
111250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
1113e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
111450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "synthesizeToFile");
111550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
111650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
111750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Bundle getParams(HashMap<String, String> params) {
111850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (params != null && !params.isEmpty()) {
111950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Bundle bundle = new Bundle(mParams);
112050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
112150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
112250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
112350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
1124b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
1125748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            // Copy feature strings defined by the framework.
1126748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
11276c07a2028505e28abd601870f05d4752e92a4b7bNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1128748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1129b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // Copy over all parameters that start with the name of the
1130b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // engine that we are currently connected to. The engine is
1131b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // free to interpret them as it chooses.
1132b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            if (!TextUtils.isEmpty(mCurrentEngine)) {
1133b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                for (Map.Entry<String, String> entry : params.entrySet()) {
1134b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    final String key = entry.getKey();
1135b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    if (key != null && key.startsWith(mCurrentEngine)) {
1136b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                        bundle.putString(key, entry.getValue());
1137b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    }
1138b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                }
1139b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            }
1140b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
114150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return bundle;
114250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        } else {
114350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mParams;
1144e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
1145e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1146e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
114750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
114850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String value = params.get(key);
114950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (value != null) {
115050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            bundle.putString(key, value);
115150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
115250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
1153a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
115450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
115550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
115650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
115750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
115850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                int value = Integer.parseInt(valueString);
115950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putInt(key, value);
116050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
116150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
116250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
116350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
11649d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi    }
11659d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
116650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
116750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
116850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
116950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
117050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                float value = Float.parseFloat(valueString);
117150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putFloat(key, value);
117250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
117350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
117450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
11759d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        }
1176a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi    }
1177a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
117878c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
117950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the listener that will be notified when synthesis of an utterance completes.
118078c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
118150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param listener The listener to use.
118278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
118350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1184754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1185754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)}
1186754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *        instead.
118778c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
1188754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    @Deprecated
118950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1190754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = UtteranceProgressListener.from(listener);
1191754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        return TextToSpeech.SUCCESS;
1192754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    }
1193754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1194754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    /**
1195754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * Sets the listener that will be notified of various events related to the
1196754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * synthesis of a given utterance.
1197754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1198754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * See {@link UtteranceProgressListener} and
1199754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}.
1200754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1201754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @param listener the listener to use.
1202754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @return {@link #ERROR} or {@link #SUCCESS}
1203754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     */
1204754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    public int setOnUtteranceProgressListener(UtteranceProgressListener listener) {
1205754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = listener;
1206a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        return TextToSpeech.SUCCESS;
120778c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
120878c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
1209b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    /**
121050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the TTS engine to use.
1211b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
12123f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     * @deprecated This doesn't inform callers when the TTS engine has been
12133f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
1214b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        can be used with the appropriate engine name. Also, there is no
1215b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        guarantee that the engine specified will be loaded. If it isn't
1216b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        installed or disabled, the user / system wide defaults will apply.
12173f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *
121850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
1219b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
122050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1221b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     */
12223f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath    @Deprecated
122360dd360640a400d9b4a602160733281d284aaee5Charles Chen    public int setEngineByPackageName(String enginePackageName) {
122450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = enginePackageName;
122550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return initTts();
1226b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    }
1227b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen
1228def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    /**
122950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets the package name of the default speech synthesis engine.
1230def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     *
123122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return Package name of the TTS engine that the user has chosen
123222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     *        as their default.
1233def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     */
1234def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    public String getDefaultEngine() {
1235d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getDefaultEngine();
123650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
123750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
123850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
1239c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * Checks whether the user's settings should override settings requested
1240c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * by the calling application. As of the Ice cream sandwich release,
1241c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * user settings never forcibly override the app's settings.
124250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
124350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public boolean areDefaultsEnforced() {
1244c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath        return false;
124550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
124650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
124742229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    /**
124850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets a list of all installed TTS engines.
124942229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     *
125022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return A list of engine info objects. The list can be empty, but never {@code null}.
125142229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     */
125250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public List<EngineInfo> getEngines() {
1253d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getEngines();
125422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath    }
125522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
125622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
125750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private class Connection implements ServiceConnection {
125850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        private ITextToSpeechService mService;
1259a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() {
1260a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            @Override
1261754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onDone(String utteranceId) {
1262754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1263754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1264754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onDone(utteranceId);
1265754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1266754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1267754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1268754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1269754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onError(String utteranceId) {
1270754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1271754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1272754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onError(utteranceId);
1273754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1274754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1275754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1276754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1277754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onStart(String utteranceId) {
1278754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1279a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                if (listener != null) {
1280754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onStart(utteranceId);
1281a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
1282a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            }
1283a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        };
128450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
128550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void onServiceConnected(ComponentName name, IBinder service) {
128650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Log.i(TAG, "Connected to " + name);
128750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
128850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (mServiceConnection != null) {
128950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    // Disconnect any previous service connection
129050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mServiceConnection.disconnect();
129150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
129250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mServiceConnection = this;
129350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mService = ITextToSpeechService.Stub.asInterface(service);
1294a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                try {
1295492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    mService.setCallback(getCallerIdentity(), mCallback);
1296a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                    dispatchOnInit(SUCCESS);
1297a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                } catch (RemoteException re) {
1298a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                    Log.e(TAG, "Error connecting to service, setCallback() failed");
1299a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                    dispatchOnInit(ERROR);
1300a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
130150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
130250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
130350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1304492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        public IBinder getCallerIdentity() {
1305492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath            return mCallback;
1306492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        }
1307492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
130850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void onServiceDisconnected(ComponentName name) {
130950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
131050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mService = null;
131150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // If this is the active connection, clear it
131250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (mServiceConnection == this) {
131350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mServiceConnection = null;
131450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
131550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
131650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
131750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
131850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void disconnect() {
131950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mContext.unbindService(this);
132050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
132150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
132250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
132342229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen            try {
132450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
132550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (mService == null) {
132650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        Log.w(TAG, method + " failed: not connected to TTS engine");
132750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        return errorResult;
132850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    }
132950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return action.run(mService);
133050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
133150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (RemoteException ex) {
133250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Log.e(TAG, method + " failed", ex);
133350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (reconnect) {
133450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    disconnect();
133550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    initTts();
133650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
133750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return errorResult;
133842229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen            }
133942229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen        }
134042229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    }
13412cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
134250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private interface Action<R> {
134350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        R run(ITextToSpeechService service) throws RemoteException;
134450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
134550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
13462cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    /**
134750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Information about an installed text-to-speech engine.
134850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
134950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @see TextToSpeech#getEngines
13502cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert     */
135150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static class EngineInfo {
135250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
135350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Engine package name..
135450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
135550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String name;
135650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
135750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Localized label for the engine.
135850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
135950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String label;
136050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
136150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Icon for the engine.
136250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
136350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public int icon;
136422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
136522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * Whether this engine is a part of the system
136622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * image.
1367d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1368d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
136922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1370d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public boolean system;
137122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
137222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * The priority the engine declares for the the intent filter
137322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * {@code android.intent.action.TTS_SERVICE}
1374d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1375d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
137622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1377d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public int priority;
137850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
137950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @Override
138050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String toString() {
138150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return "EngineInfo{name=" + name + "}";
138250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
138350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
13842cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    }
138522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
1386e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi}
1387