TextToSpeech.java revision 9c4012b31b0c09cb14689bd96a71aae42c8a00cd
1e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi/*
250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert * Copyright (C) 2009 The Android Open Source Project
3e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
4e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * use this file except in compliance with the License. You may obtain a copy of
6e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * the License at
7e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
8e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0
9e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
10e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * License for the specific language governing permissions and limitations under
14e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi * the License.
15e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi */
1621a6a6d26baf32593d63226f9728ddd66f0bd571Jean-Michel Trivipackage android.speech.tts;
1721a6a6d26baf32593d63226f9728ddd66f0bd571Jean-Michel Trivi
18ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Triviimport android.annotation.SdkConstant;
19ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Triviimport android.annotation.SdkConstant.SdkConstantType;
20e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.ComponentName;
2150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.content.ContentResolver;
22e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.Context;
23e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.Intent;
24e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.content.ServiceConnection;
25a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Triviimport android.media.AudioManager;
2650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.net.Uri;
278f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniakimport android.os.AsyncTask;
2850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.os.Bundle;
29e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.IBinder;
30e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.RemoteException;
3150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.provider.Settings;
3250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.text.TextUtils;
33e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.util.Log;
34e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
35748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Collections;
36a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Triviimport java.util.HashMap;
37748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.HashSet;
3850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.List;
39679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Triviimport java.util.Locale;
4050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.Map;
41748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Set;
42a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi
43e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi/**
44e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
4562788e9b48f884a35b89c88911b581daa6a14e08Jean-Michel Trivi * Synthesizes speech from text for immediate playback or to create a sound file.
46a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
47a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
48a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * notified of the completion of the initialization.<br>
49a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
50a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * to release the native resources used by the TextToSpeech engine.
51e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
52e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi */
53a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivipublic class TextToSpeech {
54e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
552cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    private static final String TAG = "TextToSpeech";
562cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
5791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
5891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a successful operation.
5991bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int SUCCESS = 0;
6191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
6291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a generic operation failure.
6391bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int ERROR = -1;
6591bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi
66679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
67679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where all entries in the playback queue (media to be played
68679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * and text to be synthesized) are dropped and replaced by the new entry.
69abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queues are flushed with respect to a given calling app. Entries in the queue
70abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from other callees are not discarded.
71679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
72ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_FLUSH = 0;
73679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
74679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where the new entry is added at the end of the playback queue.
75679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
76ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_ADD = 1;
77abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    /**
78abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queue mode where the entire playback queue is purged. This is different
79abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries
80abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from a given caller.
81abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     *
82abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * @hide
83abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     */
84abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    static final int QUEUE_DESTROY = 2;
85e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
86aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
879f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi     * Denotes the language is available exactly as specified by the locale.
88aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
89ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
90aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
91aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
92b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language and country specified
93aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * by the locale, but not the variant.
94aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
95ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_AVAILABLE = 1;
96aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
97aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
98b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language by the locale,
99aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * but not the country and variant.
100aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
101ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_AVAILABLE = 0;
102aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
103aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
104aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * Denotes the language data is missing.
105aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
106ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_MISSING_DATA = -1;
107aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
108aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
109a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Denotes the language is not supported.
110aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
111ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_NOT_SUPPORTED = -2;
112ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
113ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    /**
114ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * Broadcast Action: The TextToSpeech synthesizer has completed processing
115ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * of all the text in the speech queue.
116c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     *
117c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Note that this notifies callers when the <b>engine</b> has finished has
118c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * processing text data. Audio playback might not have completed (or even started)
119c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * at this point. If you wish to be notified when this happens, see
120c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * {@link OnUtteranceCompletedListener}.
121ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     */
122ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
123ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
124ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi            "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
125aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
126e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
127a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interface definition of a callback to be invoked indicating the completion of the
128a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * TextToSpeech engine initialization.
129e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
130e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public interface OnInitListener {
131a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
132a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Called to signal the completion of the TextToSpeech engine initialization.
13350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
134a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
135a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
13691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        public void onInit(int status);
137e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
138e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
139e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
14050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Listener that will be called when the TTS service has
14150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * completed synthesizing an utterance. This is only called if the utterance
14250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}).
14378c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
144a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi    public interface OnUtteranceCompletedListener {
145a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
14650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Called when an utterance has been synthesized.
14750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
148a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param utteranceId the identifier of the utterance.
14960dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
15060dd360640a400d9b4a602160733281d284aaee5Charles Chen        public void onUtteranceCompleted(String utteranceId);
15178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
15278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
15378c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
154748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Constants and parameter names for controlling text-to-speech. These include:
155748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
156748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * <ul>
157748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
158748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Intents to ask engine to install data or check its data and
159748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         extras for a TTS engine's check data activity.
160748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
161748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
162748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Keys for the parameters passed with speak commands, e.g.
163748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}.
164748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
165748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
166748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         A list of feature strings that engines might support, e.g
167748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to
168748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify
169748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         engine behaviour. The engine can be queried for the set of features it supports
170748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         through {@link TextToSpeech#getFeatures(java.util.Locale)}.
171748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
172748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * </ul>
173d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi     */
174d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    public class Engine {
17550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
17662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
17750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default speech rate.
17850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
17962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
18050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_RATE = 100;
18150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pitch.
18450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
18562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
18650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_PITCH = 100;
18750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default volume.
19050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
19162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
1929d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_VOLUME = 1.0f;
19350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1949d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
19550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pan (centered).
19650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
1979d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
1989d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_PAN = 0.0f;
1999d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
2009d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
20150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}.
20250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
2039d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
204ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int USE_DEFAULTS = 0; // false
20550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
20662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
20750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Package name of the default TTS engine.
20850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
20950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
21022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * @deprecated No longer in use, the default engine is determined by
211b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath         *         the sort order defined in {@link TtsEngines}. Note that
21222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         this doesn't "break" anything because there is no guarantee that
21322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         the engine specified below is installed on a given build, let
21422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         alone be the default.
21562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
21622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        @Deprecated
21750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String DEFAULT_ENGINE = "com.svox.pico";
218d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
219a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
220a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Default audio stream used when playing synthesized speech.
221a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
222ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
223a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
22462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
22562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates success when checking the installation status of the resources used by the
226a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
22762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
228d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_PASS = 1;
22950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
23062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates failure when checking the installation status of the resources used by the
232a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
234d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_FAIL = 0;
23550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
23662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates erroneous data when checking the installation status of the resources used by
238a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
240d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
24150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing resources when checking the installation status of the resources used
244a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
24562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
246d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
24750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing storage volume when checking the installation status of the resources
250a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
25162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
25262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
25399a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
25450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
25550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Intent for starting a TTS service. Services that handle this intent must
25650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * extend {@link TextToSpeechService}. Normal applications should not use this intent
25750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * directly, instead they should talk to the TTS service using the the methods in this
25850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * class.
25950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
26050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.SERVICE_ACTION)
26150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String INTENT_ACTION_TTS_SERVICE =
26250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.intent.action.TTS_SERVICE";
26350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
2644d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        /**
2654d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * Name under which a text to speech engine publishes information about itself.
2664d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * This meta-data should reference an XML resource containing a
2674d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * <code>&lt;{@link android.R.styleable#TextToSpeechEngine tts-engine}&gt;</code>
2684d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * tag.
2694d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         */
2704d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        public static final String SERVICE_META_DATA = "android.speech.tts";
2714d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath
272ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        // intents to ask engine to install data or check its data
273ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
274a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Triggers the platform TextToSpeech engine to
275ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * start the activity that installs the resource files on the device
276ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * that are required for TTS to be operational. Since the installation
277ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * of the data can be interrupted or declined by the user, the application
278ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * shouldn't expect successful installation upon return from that intent,
279ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * and if need be, should check installation status with
280ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #ACTION_CHECK_TTS_DATA}.
281ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
2829f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
283ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_INSTALL_TTS_DATA =
284ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.INSTALL_TTS_DATA";
285ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
286ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
287176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Broadcast Action: broadcast to signal the completion of the installation of
288176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the data files used by the synthesis engine. Success or failure is indicated in the
289176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #EXTRA_TTS_DATA_INSTALLED} extra.
29077a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
29177a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
29277a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String ACTION_TTS_DATA_INSTALLED =
29377a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi                "android.speech.tts.engine.TTS_DATA_INSTALLED";
29450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
29577a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
296a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Starts the activity from the platform TextToSpeech
297ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * engine to verify the proper installation and availability of the
298ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * resource files on the system. Upon completion, the activity will
299ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * return one of the following codes:
300ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_PASS},
301ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_FAIL},
302176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #CHECK_VOICE_DATA_BAD_DATA},
303176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #CHECK_VOICE_DATA_MISSING_DATA}, or
304176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * {@link #CHECK_VOICE_DATA_MISSING_VOLUME}.
305ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <p> Moreover, the data received in the activity result will contain the following
306ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * fields:
307ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <ul>
308176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_VOICE_DATA_ROOT_DIRECTORY} which
309176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       indicates the path to the location of the resource files,</li>
310176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_VOICE_DATA_FILES} which contains
311176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       the list of all the resource files,</li>
312176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *   <li>and {@link #EXTRA_VOICE_DATA_FILES_INFO} which
313176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       contains, for each resource file, the description of the language covered by
314176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       the file in the xxx-YYY format, where xxx is the 3-letter ISO language code,
315176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         *       and YYY is the 3-letter ISO country code.</li>
316ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * </ul>
317ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
3189f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
319ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_CHECK_TTS_DATA =
320ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.CHECK_TTS_DATA";
321ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
32250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
323176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Activity intent for getting some sample text to use for demonstrating TTS.
32450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
325176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * @hide This intent was used by engines written against the old API.
326176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Not sure if it should be exposed.
32750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
32850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
32950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String ACTION_GET_SAMPLE_TEXT =
33050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.speech.tts.engine.GET_SAMPLE_TEXT";
33150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
332176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        // extras for a TTS engine's check data activity
333176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        /**
334176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
335176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the path to its resources.
336176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         */
337176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
338176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak
33962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
340176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
341176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the file names of its resources under the
342176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * resource path.
34362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
344176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
34550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
346176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        /**
347176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
348176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the locale associated with each resource file.
349176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         */
350176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
35150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
35276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
353176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
35476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the available voices.
35576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
35676d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
35776d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
35876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_AVAILABLE_VOICES = "availableVoices";
35950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
36076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
361176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
36276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
36376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
36476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
36576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
36676d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
36750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
36876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
36976d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
37076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * caller indicates to the TextToSpeech engine which specific sets of voice data to
37176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * check for by sending an ArrayList<String> of the voices that are of interest.
37276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
37376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
37476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
37576d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
37699a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
37777a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        // extras for a TTS engine's data installation
37877a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
379176baa7de11be910c36b7b4dfa7826b55ec97963Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent.
3809f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * It indicates whether the data files for the synthesis engine were successfully
3819f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * installed. The installation was initiated with the  {@link #ACTION_INSTALL_TTS_DATA}
3829f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * intent. The possible values for this extra are
3839f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
38477a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
38577a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
38677a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi
38762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // keys for the parameters passed with speak commands. Hidden keys are used internally
38862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // to maintain engine state for each TextToSpeech instance.
38962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
39050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
39162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
392ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_RATE = "rate";
39350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
39462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
39550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
39662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
397ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_LANGUAGE = "language";
39850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
39962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
40050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
40162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
402ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_COUNTRY = "country";
40350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
40462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
40550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
40662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
407ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_VARIANT = "variant";
40850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
40962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
41050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
41160dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
41260dd360640a400d9b4a602160733281d284aaee5Charles Chen        public static final String KEY_PARAM_ENGINE = "engine";
41350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
41460dd360640a400d9b4a602160733281d284aaee5Charles Chen        /**
41550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
4161a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen         */
4171a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        public static final String KEY_PARAM_PITCH = "pitch";
41850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4191a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        /**
42062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Parameter key to specify the audio stream type to be used when speaking text
42150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * or playing back a file. The value should be one of the STREAM_ constants
42250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * defined in {@link AudioManager}.
42350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
424a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
425a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
42662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
427ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_STREAM = "streamType";
42850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
42962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
430a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Parameter key to identify an utterance in the
431a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
43262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * spoken, a file has been played back or a silence duration has elapsed.
43350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
434a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
435a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
436a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
43762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
438ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
43950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4409d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4419d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify the speech volume relative to the current stream type
4429d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
4439011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * where 0 is silence, and 1 is the maximum volume (the default behavior).
44450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4459d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4469d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4479d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4489d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_VOLUME = "volume";
44950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4509d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4519d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify how the speech is panned from left to right when speaking text.
4529d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
4539011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * 0 to center (the default behavior), and +1 to hard-right.
45450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4559d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4569d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4579d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4589d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_PAN = "pan";
45962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi
460748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
461748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)}
462748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set (and supported by the engine
463748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must
464748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * use network based synthesis.
465748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         *
466748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#speak(String, int, java.util.HashMap)
467748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
468748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#getFeatures(java.util.Locale)
469748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
470748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
471748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
472748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
473748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)}
474748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set and supported by the engine
475748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize
476748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * text on-device (without making network requests).
477748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
478748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
479d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    }
480d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
48150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Context mContext;
4829c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak    private Connection mConnectingServiceConnection;
48350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Connection mServiceConnection;
48450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private OnInitListener mInitListener;
485a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // Written from an unspecified application thread, read from
486a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // a binder thread.
487754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    private volatile UtteranceProgressListener mUtteranceProgressListener;
48891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    private final Object mStartLock = new Object();
48950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
49050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private String mRequestedEngine;
491c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // Whether to initialize this TTS object with the default engine,
492c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // if the requested engine is not available. Valid only if mRequestedEngine
493c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // is not null. Used only for testing, though potentially useful API wise
494c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // too.
495c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    private final boolean mUseFallback;
49650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mEarcons;
49750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mUtterances;
49850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Bundle mParams = new Bundle();
499d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath    private final TtsEngines mEnginesHelper;
50068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    private final String mPackageName;
501bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    private volatile String mCurrentEngine = null;
502e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
503e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
5044bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the default TTS engine.
505a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * This will also initialize the associated TextToSpeech engine if it isn't already running.
506e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
507e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param context
508a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The context this instance is running in.
50991bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * @param listener
510a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The {@link TextToSpeech.OnInitListener} that will be called when the
511a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            TextToSpeech engine has initialized.
512e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
51391bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    public TextToSpeech(Context context, OnInitListener listener) {
51450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        this(context, listener, null);
51550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
51650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
51750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
5184bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the given TTS engine.
5194bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * This will also initialize the associated TextToSpeech engine if it isn't already running.
5204bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *
5214bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param context
5224bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The context this instance is running in.
5234bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param listener
5244bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The {@link TextToSpeech.OnInitListener} that will be called when the
5254bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            TextToSpeech engine has initialized.
5264bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param engine Package name of the TTS engine to use.
52750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
52850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public TextToSpeech(Context context, OnInitListener listener, String engine) {
529c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        this(context, listener, engine, null, true);
53068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    }
53168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath
53268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    /**
53368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * Used by the framework to instantiate TextToSpeech objects with a supplied
53468e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * package name, instead of using {@link android.content.Context#getPackageName()}
53568e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     *
53668e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * @hide
53768e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     */
53868e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    public TextToSpeech(Context context, OnInitListener listener, String engine,
539c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            String packageName, boolean useFallback) {
54091bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mContext = context;
54191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mInitListener = listener;
54250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = engine;
543c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        mUseFallback = useFallback;
54487c9684fd0fa31fd6ad7f7e9f4cfedddc4fdc4b0Jean-Michel Trivi
54550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mEarcons = new HashMap<String, Uri>();
54650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mUtterances = new HashMap<String, Uri>();
54768e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        mUtteranceProgressListener = null;
548a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
549d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        mEnginesHelper = new TtsEngines(mContext);
55068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        if (packageName != null) {
55168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = packageName;
55268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        } else {
55368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = mContext.getPackageName();
55468e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        }
555da7681e7b61dd450be72f2b4a80e6d1c86342e05Jean-Michel Trivi        initTts();
556e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
557e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
5589c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak    private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method,
5599c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            boolean onlyEstablishedConnection) {
5609c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        return runAction(action, errorResult, method, false, onlyEstablishedConnection);
56150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
56250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
56350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runAction(Action<R> action, R errorResult, String method) {
5649c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        return runAction(action, errorResult, method, true, true);
56550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
56650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
5679c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak    private <R> R runAction(Action<R> action, R errorResult, String method,
5689c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            boolean reconnect, boolean onlyEstablishedConnection) {
56950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
57050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mServiceConnection == null) {
57150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Log.w(TAG, method + " failed: not bound to TTS engine");
57250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return errorResult;
573e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
5749c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            return mServiceConnection.runAction(action, errorResult, method, reconnect,
5759c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    onlyEstablishedConnection);
57650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
57750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
578e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
57950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private int initTts() {
580b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 1: Try connecting to the engine that was requested.
581c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        if (mRequestedEngine != null) {
582c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
583c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                if (connectToEngine(mRequestedEngine)) {
584c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = mRequestedEngine;
585c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return SUCCESS;
586c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                } else if (!mUseFallback) {
587c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = null;
588c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    dispatchOnInit(ERROR);
589c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return ERROR;
590c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                }
591c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            } else if (!mUseFallback) {
592c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                Log.i(TAG, "Requested engine not installed: " + mRequestedEngine);
593c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                mCurrentEngine = null;
594c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                dispatchOnInit(ERROR);
595c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                return ERROR;
596b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath            }
59750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
59850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
599b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 2: Try connecting to the user's default engine.
600b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        final String defaultEngine = getDefaultEngine();
601b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) {
60250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (connectToEngine(defaultEngine)) {
603b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = defaultEngine;
60450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
605e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
60650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
607e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
608b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 3: Try connecting to the highest ranked engine in the
609b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // system.
610d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        final String highestRanked = mEnginesHelper.getHighestRankedEngineName();
611b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (highestRanked != null && !highestRanked.equals(mRequestedEngine) &&
612b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                !highestRanked.equals(defaultEngine)) {
61322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath            if (connectToEngine(highestRanked)) {
614b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = highestRanked;
61550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
6161e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi            }
61750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
61850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
619b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // NOTE: The API currently does not allow the caller to query whether
620b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // they are actually connected to any engine. This might fail for various
621b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // reasons like if the user disables all her TTS engines.
622b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath
623b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        mCurrentEngine = null;
6240e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath        dispatchOnInit(ERROR);
62550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
62650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
62750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
62850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private boolean connectToEngine(String engine) {
62950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Connection connection = new Connection();
63050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
63150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        intent.setPackage(engine);
63250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
63350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!bound) {
63450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Log.e(TAG, "Failed to bind to " + engine);
63550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return false;
6361e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        } else {
6370e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath            Log.i(TAG, "Sucessfully bound to " + engine);
6389c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            mConnectingServiceConnection = connection;
63950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return true;
6401e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        }
641e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
642e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
64350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void dispatchOnInit(int result) {
64450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
64550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mInitListener != null) {
64650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener.onInit(result);
64750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener = null;
64850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
64950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
65050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
651e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
652492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    private IBinder getCallerIdentity() {
653492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        return mServiceConnection.getCallerIdentity();
654492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    }
655492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
656e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
657a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Releases the resources used by the TextToSpeech engine.
658a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * It is good practice for instance to call this method in the onDestroy() method of an Activity
659a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * so the TextToSpeech engine can be cleanly stopped.
660e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
661e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public void shutdown() {
6629c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        // Special case, we are asked to shutdown connection that did finalize its connection.
6639c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        synchronized (mStartLock) {
6649c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            if (mConnectingServiceConnection != null) {
6659c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mContext.unbindService(mConnectingServiceConnection);
6669c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mConnectingServiceConnection = null;
6679c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                return;
6689c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            }
6699c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }
6709c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
6719c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        // Post connection case
67250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        runActionNoReconnect(new Action<Void>() {
67350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
67450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Void run(ITextToSpeechService service) throws RemoteException {
675492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.setCallback(getCallerIdentity(), null);
676492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.stop(getCallerIdentity());
67750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mServiceConnection.disconnect();
67890e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // Context#unbindService does not result in a call to
67990e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection#onServiceDisconnected. As a result, the
68090e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // service ends up being destroyed (if there are no other open
68190e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // connections to it) but the process lives on and the
68290e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection continues to refer to the destroyed service.
68390e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                //
68490e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // This leads to tons of log spam about SynthThread being dead.
68590e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                mServiceConnection = null;
686bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath                mCurrentEngine = null;
68750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return null;
68850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
6899c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }, null, "shutdown", false);
690e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
691e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
692e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
693e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound resource in a
694a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. After a call to this method, subsequent calls to
695a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * {@link #speak(String, int, HashMap)} will play the specified sound resource
696a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * if it is available, or synthesize the text it is missing.
697e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
698e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
699a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
700e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
701e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param packagename
702e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Pass the packagename of the application that contains the
703e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            resource. If the resource is in your own application (this is
704e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            the most common case), then put the packagename of your
705e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            application here.<br/>
706e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Example: <b>"com.google.marvin.compass"</b><br/>
707e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The packagename can be found in the AndroidManifest.xml of
708e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            your application.
709e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <p>
710e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
711e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
712e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            </p>
713e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
714e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param resourceId
715a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.south_south_east</code>
7165c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
717ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
718e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7195c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String packagename, int resourceId) {
72050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
72150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, makeResourceUri(packagename, resourceId));
72250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
723e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
724e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
725e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
726e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
727e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound file. Using this, it
728a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * is possible to add custom pronounciations for a string of text.
729a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
730a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will play the specified sound resource if it is available, or synthesize the text it is
731a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * missing.
732e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
733e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
734a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
735e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param filename
736e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The full path to the sound file (for example:
737e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            "/sdcard/mysounds/hello.wav")
7385c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
739ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
740e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7415c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String filename) {
74291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        synchronized (mStartLock) {
74350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, Uri.parse(filename));
74450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
745e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
746e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
747e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
748e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
749e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
750904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * Adds a mapping between a string of text and a sound resource in a
751a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. Use this to add custom earcons.
752904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
753ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
754904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
755a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @param earcon The name of the earcon.
756a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code><br/>
757904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
758904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param packagename
759a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the package name of the application that contains the
760a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            resource. This can for instance be the package name of your own application.
761904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            Example: <b>"com.google.marvin.compass"</b><br/>
762a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The package name can be found in the AndroidManifest.xml of
763a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the application containing the resource.
764904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <p>
765904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
766904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
767904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            </p>
768904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
769904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param resourceId
770a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.tick_snd</code>
771904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
772ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
773904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
774904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String packagename, int resourceId) {
775904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        synchronized(mStartLock) {
77650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, makeResourceUri(packagename, resourceId));
77750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
778904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
779904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
780904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
781904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
782a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Adds a mapping between a string of text and a sound file.
783a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Use this to add custom earcons.
784a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *
785a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
786904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
787904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param earcon
788a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The name of the earcon.
789a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code>
790904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param filename
791904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            The full path to the sound file (for example:
792904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            "/sdcard/mysounds/tick.wav")
793904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
794ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
795904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
796904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String filename) {
79750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized(mStartLock) {
79850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, Uri.parse(filename));
79950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
800904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
801904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
802904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
80350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Uri makeResourceUri(String packageName, int resourceId) {
80450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return new Uri.Builder()
80550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
80650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .encodedAuthority(packageName)
80750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .appendEncodedPath(String.valueOf(resourceId))
80850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .build();
80950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
810904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
811904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
812442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * Speaks the string using the specified queuing strategy and speech parameters.
813442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
814442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
815442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
816442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
817442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
818442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
819e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
8202d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The string of text to be spoken. No longer than
8212d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
82250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
82350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
82450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
82550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
82650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_UTTERANCE_ID},
82750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_VOLUME},
82850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_PAN}.
829b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
830b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
831b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
832b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8335c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
834442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation.
835e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
83650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int speak(final String text, final int queueMode, final HashMap<String, String> params) {
83750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
83850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
83950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
84050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri utteranceUri = mUtterances.get(text);
84150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (utteranceUri != null) {
842492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
84350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            getParams(params));
84450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                } else {
845492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.speak(getCallerIdentity(), text, queueMode, getParams(params));
846a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
847e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
84850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "speak");
849e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
850e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
851e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
852e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Plays the earcon using the specified queueing mode and parameters.
85350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The earcon must already have been added with {@link #addEarcon(String, String)} or
85450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * {@link #addEarcon(String, String, int)}.
855442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
856442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
857442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
858442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
859442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
860442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
861e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
86250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param earcon The earcon that should be played
86350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
86450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
86550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
86650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
867a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
868b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
869b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
870b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
871b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8725c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
873442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation.
874e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
87550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playEarcon(final String earcon, final int queueMode,
87650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
87750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
87850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
87950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
88050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri earconUri = mEarcons.get(earcon);
88150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (earconUri == null) {
88250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return ERROR;
883a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
884492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playAudio(getCallerIdentity(), earconUri, queueMode,
88550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
886e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
88750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playEarcon");
888e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
889679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi
8905c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    /**
8915c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * Plays silence for the specified amount of time using the specified
8925c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * queue mode.
893442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
894442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
895442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
896442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
897442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
898442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
8995c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
90050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param durationInMs The duration of the silence.
90150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
90250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
90350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
904a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
905b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
906b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
907b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
908b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
9095c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
910442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilence operation.
9115c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     */
91250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playSilence(final long durationInMs, final int queueMode,
91350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
91450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
91550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
91650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
917492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playSilence(getCallerIdentity(), durationInMs, queueMode,
91850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
919f032bc7da536774a0b6a1c77632c65b935eee6faCharles Chen            }
92050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playSilence");
921a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi    }
922e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
923e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
924748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Queries the engine for the set of features it supports for a given locale.
925748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features can either be framework defined, e.g.
926748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
927748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Engine specific keys must be prefixed by the name of the engine they
928748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * are intended for. These keys can be used as parameters to
929748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
930748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
931748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
932748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features are boolean flags, and their values in the synthesis parameters
933748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * must be behave as per {@link Boolean#parseBoolean(String)}.
934748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
935748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * @param locale The locale to query features for.
936748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     */
937748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    public Set<String> getFeatures(final Locale locale) {
938748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        return runAction(new Action<Set<String>>() {
939748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            @Override
940748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            public Set<String> run(ITextToSpeechService service) throws RemoteException {
941748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                String[] features = service.getFeaturesForLanguage(
942748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                        locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
943748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                if (features != null) {
944748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    final Set<String> featureSet = new HashSet<String>();
945748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    Collections.addAll(featureSet, features);
946748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    return featureSet;
947748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                }
948748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                return null;
949748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            }
950748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        }, null, "getFeatures");
951748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    }
952748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
953748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    /**
954c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Checks whether the TTS engine is busy speaking. Note that a speech item is
955c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * considered complete once it's audio data has been sent to the audio mixer, or
956c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * written to a file. There might be a finite lag between this point, and when
957c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * the audio hardware completes playback.
958e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
95950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@code true} if the TTS engine is speaking.
960e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
961e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public boolean isSpeaking() {
96250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Boolean>() {
96350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
96450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Boolean run(ITextToSpeechService service) throws RemoteException {
96550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isSpeaking();
966e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
96750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, false, "isSpeaking");
968e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
969e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
970e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
971a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interrupts the current utterance (whether played or rendered to file) and discards other
972a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * utterances in the queue.
9735c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
97450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
975e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
9765c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int stop() {
97750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
97850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
97950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
980492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.stop(getCallerIdentity());
981e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
98250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "stop");
983e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
984e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
985e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
98650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the speech rate.
987e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
988679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
989e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
99050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
99150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
99250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
9935c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
99450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
995e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
9965c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setSpeechRate(float speechRate) {
99750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (speechRate > 0.0f) {
99850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intRate = (int)(speechRate * 100);
99950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intRate > 0) {
100050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
100150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
1002679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi                }
100350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
1004e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
1005e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
100650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
1007e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1008e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1009e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1010a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Sets the speech pitch for the TextToSpeech engine.
10112ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
10122ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
10132ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
101450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
10152ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            lower values lower the tone of the synthesized voice,
10162ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            greater values increase it.
10175c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
101850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
10192ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     */
10205c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setPitch(float pitch) {
102150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (pitch > 0.0f) {
102250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intPitch = (int)(pitch * 100);
102350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intPitch > 0) {
102450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
102550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
10262ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi                }
102750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
10282ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi            }
10292ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi        }
103050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
10312ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    }
10322ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi
10332ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    /**
1034bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @return the engine currently in use by this TextToSpeech instance.
1035bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @hide
1036bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     */
1037bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    public String getCurrentEngine() {
1038bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath        return mCurrentEngine;
1039bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    }
1040bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath
1041bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    /**
1042b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used as the default
1043b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Text-to-speech language.
1044b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *
1045b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1046b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1047b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     */
1048b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    public Locale getDefaultLanguage() {
1049b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        return runAction(new Action<Locale>() {
1050b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            @Override
1051b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) throws RemoteException {
1052b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                String[] defaultLanguage = service.getClientDefaultLanguage();
1053b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1054b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
1055b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            }
1056b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        }, null, "getDefaultLanguage");
1057b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    }
1058b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1059b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    /**
106050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the text-to-speech language.
106150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The TTS engine will try to use the closest match to the specified
1062a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * language as represented by the Locale, but there is no guarantee that the exact same Locale
1063a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1064a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * before choosing the language to use for the next utterances.
1065e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
106650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The locale describing the language to be used.
10675c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
106850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1069ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1070ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1071e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
107250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setLanguage(final Locale loc) {
107350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
107450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
107550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
107650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (loc == null) {
107750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return LANG_NOT_SUPPORTED;
107850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
10791a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String language = loc.getISO3Language();
10801a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String country = loc.getISO3Country();
10811a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String variant = loc.getVariant();
10821a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Check if the language, country, variant are available, and cache
10831a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // the available parts.
10841a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Note that the language is not actually set here, instead it is cached so it
10851a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // will be associated with all upcoming utterances.
108613896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak
108713896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak                int result = service.loadLanguage(getCallerIdentity(), language, country, variant);
10881a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                if (result >= LANG_AVAILABLE){
108950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
109050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        variant = "";
109150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        if (result < LANG_COUNTRY_AVAILABLE) {
109250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            country = "";
109350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        }
10941a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                    }
109550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
109650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
109750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
10981a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                }
109950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return result;
1100e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
110150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "setLanguage");
1102e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1103e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1104aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
1105228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used for synthesis
1106228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * requests sent to the TextToSpeech engine.
110750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
1108228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android 4.2 and before (API <= 17) this function returns the language that is currently
1109228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * being used by the TTS engine. That is the last language set by this or any other
1110228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * client by a {@link TextToSpeech#setLanguage} call to the same engine.
1111228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1112228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android versions after 4.2 this function returns the language that is currently being
1113228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * used for the synthesis requests sent from this client. That is the last language set
1114228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * by a {@link TextToSpeech#setLanguage} call on this instance.
1115228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1116228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1117228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1118ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi     */
1119ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    public Locale getLanguage() {
112050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Locale>() {
112150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
1122228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) {
1123228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                /* No service call, but we're accessing mParams, hence need for
1124228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                   wrapping it as an Action instance */
1125228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, "");
1126228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, "");
1127228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, "");
1128228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                return new Locale(lang, country, variant);
1129ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
113050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "getLanguage");
1131ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    }
1132ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi
1133ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    /**
1134a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Checks if the specified language as represented by the Locale is available and supported.
1135aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     *
113650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The Locale describing the language to be used.
11375c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
113850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1139ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1140ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1141aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
114250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int isLanguageAvailable(final Locale loc) {
114350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
114450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
114550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
114650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isLanguageAvailable(loc.getISO3Language(),
114750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        loc.getISO3Country(), loc.getVariant());
1148ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
114950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
1150aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    }
1151aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
1152e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1153d4989093ed708ddf9c799655ea0af7afda726426Charles Chen     * Synthesizes the given text to a file using the specified parameters.
1154442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1155442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
1156442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
1157442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
1158442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
1159442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1160e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
11612d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The text that should be synthesized. No longer than
11622d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
116350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
116450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
1165a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1166b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
1167b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
1168b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1169b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
117050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param filename Absolute file filename to write the generated audio data to.It should be
1171e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            something like "/sdcard/myappsounds/mysound.wav".
11725c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
1173442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
1174e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
117550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int synthesizeToFile(final String text, final HashMap<String, String> params,
117650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final String filename) {
117750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
117850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
117950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1180492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.synthesizeToFile(getCallerIdentity(), text, filename,
118150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
1182e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
118350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "synthesizeToFile");
118450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
118550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
118650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Bundle getParams(HashMap<String, String> params) {
118750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (params != null && !params.isEmpty()) {
118850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Bundle bundle = new Bundle(mParams);
118950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
119050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
119150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
119250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
1193b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
1194748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            // Copy feature strings defined by the framework.
1195748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
11966c07a2028505e28abd601870f05d4752e92a4b7bNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1197748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1198b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // Copy over all parameters that start with the name of the
1199b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // engine that we are currently connected to. The engine is
1200b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // free to interpret them as it chooses.
1201b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            if (!TextUtils.isEmpty(mCurrentEngine)) {
1202b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                for (Map.Entry<String, String> entry : params.entrySet()) {
1203b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    final String key = entry.getKey();
1204b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    if (key != null && key.startsWith(mCurrentEngine)) {
1205b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                        bundle.putString(key, entry.getValue());
1206b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    }
1207b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                }
1208b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            }
1209b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
121050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return bundle;
121150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        } else {
121250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mParams;
1213e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
1214e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1215e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
121650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
121750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String value = params.get(key);
121850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (value != null) {
121950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            bundle.putString(key, value);
122050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
122150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
1222a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
122350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
122450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
122550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
122650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
122750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                int value = Integer.parseInt(valueString);
122850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putInt(key, value);
122950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
123050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
123150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
123250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
12339d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi    }
12349d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
123550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
123650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
123750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
123850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
123950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                float value = Float.parseFloat(valueString);
124050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putFloat(key, value);
124150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
124250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
124350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
12449d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        }
1245a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi    }
1246a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
124778c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
124850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the listener that will be notified when synthesis of an utterance completes.
124978c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
125050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param listener The listener to use.
125178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
125250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1253754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1254754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)}
1255754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *        instead.
125678c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
1257754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    @Deprecated
125850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1259754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = UtteranceProgressListener.from(listener);
1260754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        return TextToSpeech.SUCCESS;
1261754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    }
1262754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1263754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    /**
1264754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * Sets the listener that will be notified of various events related to the
1265754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * synthesis of a given utterance.
1266754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1267754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * See {@link UtteranceProgressListener} and
1268754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}.
1269754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1270754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @param listener the listener to use.
1271754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @return {@link #ERROR} or {@link #SUCCESS}
1272754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     */
1273754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    public int setOnUtteranceProgressListener(UtteranceProgressListener listener) {
1274754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = listener;
1275a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        return TextToSpeech.SUCCESS;
127678c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
127778c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
1278b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    /**
127950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the TTS engine to use.
1280b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
12813f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     * @deprecated This doesn't inform callers when the TTS engine has been
12823f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
1283b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        can be used with the appropriate engine name. Also, there is no
1284b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        guarantee that the engine specified will be loaded. If it isn't
1285b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        installed or disabled, the user / system wide defaults will apply.
12863f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *
128750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
1288b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
128950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1290b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     */
12913f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath    @Deprecated
129260dd360640a400d9b4a602160733281d284aaee5Charles Chen    public int setEngineByPackageName(String enginePackageName) {
129350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = enginePackageName;
129450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return initTts();
1295b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    }
1296b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen
1297def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    /**
129850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets the package name of the default speech synthesis engine.
1299def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     *
130022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return Package name of the TTS engine that the user has chosen
130122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     *        as their default.
1302def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     */
1303def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    public String getDefaultEngine() {
1304d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getDefaultEngine();
130550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
130650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
130750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
1308c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * Checks whether the user's settings should override settings requested
1309c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * by the calling application. As of the Ice cream sandwich release,
1310c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * user settings never forcibly override the app's settings.
131150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
131250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public boolean areDefaultsEnforced() {
1313c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath        return false;
131450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
131550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
131642229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    /**
131750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets a list of all installed TTS engines.
131842229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     *
131922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return A list of engine info objects. The list can be empty, but never {@code null}.
132042229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     */
132150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public List<EngineInfo> getEngines() {
1322d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getEngines();
132322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath    }
132422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
132550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private class Connection implements ServiceConnection {
132650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        private ITextToSpeechService mService;
13278f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13289c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private SetupConnectionAsyncTask mOnSetupConnectionAsyncTask;
13299c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
13309c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private boolean mEstablished;
13318f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1332a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() {
1333a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            @Override
1334754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onDone(String utteranceId) {
1335754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1336754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1337754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onDone(utteranceId);
1338754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1339754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1340754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1341754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1342754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onError(String utteranceId) {
1343754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1344754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1345754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onError(utteranceId);
1346754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1347754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1348754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1349754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1350754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onStart(String utteranceId) {
1351754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1352a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                if (listener != null) {
1353754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onStart(utteranceId);
1354a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
1355a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            }
1356a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        };
135750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
13589c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private class SetupConnectionAsyncTask extends AsyncTask<Void, Void, Integer> {
13598f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            private final ComponentName mName;
13608f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13619c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            public SetupConnectionAsyncTask(ComponentName name) {
13628f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mName = name;
13638f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
13648f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13658f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
13668f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected Integer doInBackground(Void... params) {
13678f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
13688f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    if (isCancelled()) {
13698f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return null;
13708f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
13718f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13728f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    try {
13739c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        mService.setCallback(getCallerIdentity(), mCallback);
13749c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        String[] defaultLanguage = mService.getClientDefaultLanguage();
1375b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1376b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]);
1377b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]);
1378b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]);
1379b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1380b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        Log.i(TAG, "Set up connection to " + mName);
13818f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return SUCCESS;
13828f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    } catch (RemoteException re) {
13838f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        Log.e(TAG, "Error connecting to service, setCallback() failed");
13848f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return ERROR;
13858f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
13868f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
13878f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
13888f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
13898f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
13908f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected void onPostExecute(Integer result) {
13918f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
13929c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    if (mOnSetupConnectionAsyncTask == this) {
13939c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        mOnSetupConnectionAsyncTask = null;
13948f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
13959c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mEstablished = true;
13968f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    dispatchOnInit(result);
13978f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
13988f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
13998f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
14008f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1401091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak        @Override
140250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void onServiceConnected(ComponentName name, IBinder service) {
140350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
14049c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mConnectingServiceConnection = null;
14059c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
14068f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                Log.i(TAG, "Connected to " + name);
14078f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14089c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                if (mOnSetupConnectionAsyncTask != null) {
14099c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mOnSetupConnectionAsyncTask.cancel(false);
1410a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
14118f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14129c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mService = ITextToSpeechService.Stub.asInterface(service);
14139c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mServiceConnection = Connection.this;
14149c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
14159c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mEstablished = false;
14169c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mOnSetupConnectionAsyncTask = new SetupConnectionAsyncTask(name);
14179c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mOnSetupConnectionAsyncTask.execute();
141850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
141950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
142050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1421492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        public IBinder getCallerIdentity() {
1422492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath            return mCallback;
1423492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        }
1424492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
14258f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        /**
14268f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set.
14278f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         *
14289c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak         * @return true if we cancel mOnSetupConnectionAsyncTask in progress.
14298f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         */
14308f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        private boolean clearServiceConnection() {
143150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
14328f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                boolean result = false;
14339c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                if (mOnSetupConnectionAsyncTask != null) {
14349c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    result = mOnSetupConnectionAsyncTask.cancel(false);
14359c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mOnSetupConnectionAsyncTask = null;
14368f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
14378f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
143850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mService = null;
143950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // If this is the active connection, clear it
144050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (mServiceConnection == this) {
144150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mServiceConnection = null;
144250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
14438f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                return result;
14448f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
14458f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
14468f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14478f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        @Override
14488f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        public void onServiceDisconnected(ComponentName name) {
14498f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            Log.i(TAG, "Asked to disconnect from " + name);
14508f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            if (clearServiceConnection()) {
14518f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                /* We need to protect against a rare case where engine
14528f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * dies just after successful connection - and we process onServiceDisconnected
14538f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels
14548f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit
14558f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * with ERROR as argument.
14568f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 */
14578f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                dispatchOnInit(ERROR);
145850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
145950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
146050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
146150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void disconnect() {
146250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mContext.unbindService(this);
14638f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            clearServiceConnection();
146450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
146550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
14669c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        public boolean isEstablished() {
14679c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            return mService != null && mEstablished;
14689c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }
14699c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
14709c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        public <R> R runAction(Action<R> action, R errorResult, String method,
14719c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                boolean reconnect, boolean onlyEstablishedConnection) {
1472091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak            synchronized (mStartLock) {
1473091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                try {
147450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (mService == null) {
147550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        Log.w(TAG, method + " failed: not connected to TTS engine");
147650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        return errorResult;
147750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    }
14789c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    if (onlyEstablishedConnection && isEstablished()) {
14799c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        Log.w(TAG, method + " failed: TTS engine connection not fully setuped");
14809c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        return errorResult;
14819c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    }
148250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return action.run(mService);
1483091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                } catch (RemoteException ex) {
1484091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    Log.e(TAG, method + " failed", ex);
1485091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    if (reconnect) {
1486091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        disconnect();
1487091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        initTts();
1488091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    }
1489091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    return errorResult;
149050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
149142229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen            }
149242229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen        }
149342229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    }
14942cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
149550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private interface Action<R> {
149650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        R run(ITextToSpeechService service) throws RemoteException;
149750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
149850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
14992cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    /**
150050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Information about an installed text-to-speech engine.
150150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
150250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @see TextToSpeech#getEngines
15032cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert     */
150450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static class EngineInfo {
150550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
150650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Engine package name..
150750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
150850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String name;
150950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
151050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Localized label for the engine.
151150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
151250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String label;
151350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
151450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Icon for the engine.
151550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
151650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public int icon;
151722302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
151822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * Whether this engine is a part of the system
151922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * image.
1520d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1521d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
152222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1523d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public boolean system;
152422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
152522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * The priority the engine declares for the the intent filter
152622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * {@code android.intent.action.TTS_SERVICE}
1527d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1528d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
152922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1530d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public int priority;
153150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
153250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @Override
153350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String toString() {
153450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return "EngineInfo{name=" + name + "}";
153550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
153650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
15372cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    }
153822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
15392d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    /**
15402d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * Limit of length of input string passed to speak and synthesizeToFile.
15412d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *
15422d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #speak
15432d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #synthesizeToFile
15442d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     */
15452d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    public static int getMaxSpeechInputLength() {
15462d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak        return 4000;
15472d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    }
1548e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi}
1549