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;
305acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniakimport android.os.ParcelFileDescriptor;
31e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.os.RemoteException;
3250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.provider.Settings;
3350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport android.text.TextUtils;
34e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Triviimport android.util.Log;
35e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
365acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniakimport java.io.File;
375acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniakimport java.io.FileNotFoundException;
385acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniakimport java.io.IOException;
39748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Collections;
40a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Triviimport java.util.HashMap;
41748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.HashSet;
4250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.List;
43679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Triviimport java.util.Locale;
4450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringertimport java.util.Map;
45d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniakimport java.util.MissingResourceException;
46748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamathimport java.util.Set;
47a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi
48e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi/**
49e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
5062788e9b48f884a35b89c88911b581daa6a14e08Jean-Michel Trivi * Synthesizes speech from text for immediate playback or to create a sound file.
51a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
52a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
53a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * notified of the completion of the initialization.<br>
54a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
55a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi * to release the native resources used by the TextToSpeech engine.
56e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi *
57e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi */
58a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivipublic class TextToSpeech {
59e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
602cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    private static final String TAG = "TextToSpeech";
612cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
6291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
6391bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a successful operation.
6491bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int SUCCESS = 0;
6691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    /**
6791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * Denotes a generic operation failure.
6891bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     */
6950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static final int ERROR = -1;
7091bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi
71679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
72679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where all entries in the playback queue (media to be played
73679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * and text to be synthesized) are dropped and replaced by the new entry.
74abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queues are flushed with respect to a given calling app. Entries in the queue
75abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from other callees are not discarded.
76679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
77ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_FLUSH = 0;
78679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi    /**
79679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * Queue mode where the new entry is added at the end of the playback queue.
80679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     */
81ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int QUEUE_ADD = 1;
82abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    /**
83abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * Queue mode where the entire playback queue is purged. This is different
84abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries
85abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * from a given caller.
86abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     *
87abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     * @hide
88abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath     */
89abc63fbddab2477a2954bc804aba2826e1f11084Narayan Kamath    static final int QUEUE_DESTROY = 2;
90e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
91aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
929f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi     * Denotes the language is available exactly as specified by the locale.
93aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
94ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
95aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
96aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
97b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language and country specified
98aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * by the locale, but not the variant.
99aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
100ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_COUNTRY_AVAILABLE = 1;
101aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
102aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
103b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     * Denotes the language is available for the language by the locale,
104aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * but not the country and variant.
105aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
106ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_AVAILABLE = 0;
107aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
108aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
109aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     * Denotes the language data is missing.
110aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
111ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_MISSING_DATA = -1;
112aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
113aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
114a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Denotes the language is not supported.
115aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
116ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final int LANG_NOT_SUPPORTED = -2;
117ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
118ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    /**
119ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * Broadcast Action: The TextToSpeech synthesizer has completed processing
120ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * of all the text in the speech queue.
121c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     *
122c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Note that this notifies callers when the <b>engine</b> has finished has
123c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * processing text data. Audio playback might not have completed (or even started)
124c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * at this point. If you wish to be notified when this happens, see
125c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * {@link OnUtteranceCompletedListener}.
126ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     */
127ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
128ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi    public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
129ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi            "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
130aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
131e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
132a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interface definition of a callback to be invoked indicating the completion of the
133a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * TextToSpeech engine initialization.
134e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
135e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public interface OnInitListener {
136a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
137a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Called to signal the completion of the TextToSpeech engine initialization.
13850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
139a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
140a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
14191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        public void onInit(int status);
142e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
143e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
144e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
14550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Listener that will be called when the TTS service has
14650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * completed synthesizing an utterance. This is only called if the utterance
14750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}).
148647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak     *
149647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak     * @deprecated Use {@link UtteranceProgressListener} instead.
15078c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
151647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak    @Deprecated
152a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi    public interface OnUtteranceCompletedListener {
153a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
15450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Called when an utterance has been synthesized.
15550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
156a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @param utteranceId the identifier of the utterance.
15760dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
15860dd360640a400d9b4a602160733281d284aaee5Charles Chen        public void onUtteranceCompleted(String utteranceId);
15978c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
16078c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
16178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
162748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Constants and parameter names for controlling text-to-speech. These include:
163748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
164748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * <ul>
165748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
166748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Intents to ask engine to install data or check its data and
167748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         extras for a TTS engine's check data activity.
168748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
169748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
170748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         Keys for the parameters passed with speak commands, e.g.
171748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}.
172748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
173748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     <li>
174748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         A list of feature strings that engines might support, e.g
175748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to
176748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify
177748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         engine behaviour. The engine can be queried for the set of features it supports
178748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *         through {@link TextToSpeech#getFeatures(java.util.Locale)}.
179748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *     </li>
180748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * </ul>
181d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi     */
182d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    public class Engine {
18350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
18462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
18550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default speech rate.
18650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
18762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
18850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_RATE = 100;
18950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
19062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
19150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pitch.
19250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
19362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
19450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final int DEFAULT_PITCH = 100;
19550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
19662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
19750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default volume.
19850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
19962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
2009d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_VOLUME = 1.0f;
20150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
2029d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
20350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default pan (centered).
20450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
2059d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
2069d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final float DEFAULT_PAN = 0.0f;
2079d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
2089d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
20950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}.
21050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
2119d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
212ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int USE_DEFAULTS = 0; // false
21350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
21462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
21550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Package name of the default TTS engine.
21650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
21750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
21822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * @deprecated No longer in use, the default engine is determined by
219b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath         *         the sort order defined in {@link TtsEngines}. Note that
22022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         this doesn't "break" anything because there is no guarantee that
22122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         the engine specified below is installed on a given build, let
22222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         *         alone be the default.
22362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
22422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        @Deprecated
22550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String DEFAULT_ENGINE = "com.svox.pico";
226d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
227a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi        /**
228a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Default audio stream used when playing synthesized speech.
229a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         */
230ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
231a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
23262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates success when checking the installation status of the resources used by the
234a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
23562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
236d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_PASS = 1;
23750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
23862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
23962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates failure when checking the installation status of the resources used by the
240a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
24162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
242d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_FAIL = 0;
24350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
24462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
24562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates erroneous data when checking the installation status of the resources used by
246a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
247647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
248647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated Use CHECK_VOICE_DATA_FAIL instead.
24962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
250647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
251d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
25250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
25362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
25462253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing resources when checking the installation status of the resources used
255a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
256647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
257647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated Use CHECK_VOICE_DATA_FAIL instead.
25862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
259647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
260d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
26150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
26262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
26362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Indicates missing storage volume when checking the installation status of the resources
264a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
265647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
266647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated Use CHECK_VOICE_DATA_FAIL instead.
26762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
268647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
26962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
27099a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
27150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
27250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Intent for starting a TTS service. Services that handle this intent must
27350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * extend {@link TextToSpeechService}. Normal applications should not use this intent
27450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * directly, instead they should talk to the TTS service using the the methods in this
27550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * class.
27650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
27750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.SERVICE_ACTION)
27850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String INTENT_ACTION_TTS_SERVICE =
27950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.intent.action.TTS_SERVICE";
28050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
2814d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        /**
2824d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * Name under which a text to speech engine publishes information about itself.
2834d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * This meta-data should reference an XML resource containing a
2844d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * <code>&lt;{@link android.R.styleable#TextToSpeechEngine tts-engine}&gt;</code>
2854d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         * tag.
2864d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath         */
2874d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath        public static final String SERVICE_META_DATA = "android.speech.tts";
2884d03462b374dfc080f0c7c78d458c102a26be5c6Narayan Kamath
289ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        // intents to ask engine to install data or check its data
290ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
291a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Triggers the platform TextToSpeech engine to
292ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * start the activity that installs the resource files on the device
293ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * that are required for TTS to be operational. Since the installation
294ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * of the data can be interrupted or declined by the user, the application
295ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * shouldn't expect successful installation upon return from that intent,
296ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * and if need be, should check installation status with
297ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #ACTION_CHECK_TTS_DATA}.
298ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
2999f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
300ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_INSTALL_TTS_DATA =
301ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.INSTALL_TTS_DATA";
302ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
303ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        /**
304647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Broadcast Action: broadcast to signal the change in the list of available
305647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * languages or/and their features.
30677a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
30777a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
30877a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String ACTION_TTS_DATA_INSTALLED =
30977a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi                "android.speech.tts.engine.TTS_DATA_INSTALLED";
31050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
31177a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
312a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Activity Action: Starts the activity from the platform TextToSpeech
313ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * engine to verify the proper installation and availability of the
314ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * resource files on the system. Upon completion, the activity will
315ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * return one of the following codes:
316ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_PASS},
317ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * {@link #CHECK_VOICE_DATA_FAIL},
318ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <p> Moreover, the data received in the activity result will contain the following
319ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * fields:
320ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * <ul>
321647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_AVAILABLE_VOICES} which contains an ArrayList<String> of all the
322647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   available voices. The format of each voice is: lang-COUNTRY-variant where COUNTRY and
323647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   variant are optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").</li>
324647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_UNAVAILABLE_VOICES} which contains an ArrayList<String> of all the
325647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   unavailable voices (ones that user can install). The format of each voice is:
326647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   lang-COUNTRY-variant where COUNTRY and variant are optional (ie, "eng" or
327647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   "eng-USA" or "eng-USA-FEMALE").</li>
328ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         * </ul>
329ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi         */
3309f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
331ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String ACTION_CHECK_TTS_DATA =
332ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi                "android.speech.tts.engine.CHECK_TTS_DATA";
333ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi
33450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
335647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Activity intent for getting some sample text to use for demonstrating TTS. Specific
336647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * locale have to be requested by passing following extra parameters:
337647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * <ul>
338647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   <li>language</li>
339647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   <li>country</li>
340647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   <li>variant</li>
341647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * </ul>
34250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
343647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Upon completion, the activity result may contain the following fields:
344647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * <ul>
345647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *   <li>{@link #EXTRA_SAMPLE_TEXT} which contains an String with sample text.</li>
346647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * </ul>
34750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
34850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
34950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public static final String ACTION_GET_SAMPLE_TEXT =
35050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                "android.speech.tts.engine.GET_SAMPLE_TEXT";
35150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
35262253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
353647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_GET_SAMPLE_TEXT} intent result where
354647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * the TextToSpeech engine returns an String with sample text for requested voice
35562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
356647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        public static final String EXTRA_SAMPLE_TEXT = "sampleText";
35750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
35850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
359647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        // extras for a TTS engine's check data activity
36076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
361647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
36276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the available 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_AVAILABLE_VOICES = "availableVoices";
36750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
36876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
369647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
37076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
37176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
37276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
37376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
37476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
37550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
37676d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        /**
377647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
378647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the path to its resources.
379647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
380647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * It may be used by language packages to find out where to put their data.
381647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
382647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated TTS engine implementation detail, this information has no use for
383647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * text-to-speech API client.
384647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         */
385647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
386647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
387647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak
388647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        /**
389647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
390647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the file names of its resources under the
391647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * resource path.
392647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
393647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated TTS engine implementation detail, this information has no use for
394647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * text-to-speech API client.
395647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         */
396647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
397647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
398647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak
399647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        /**
400647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where
401647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * the TextToSpeech engine specifies the locale associated with each resource file.
402647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
403647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated TTS engine implementation detail, this information has no use for
404647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * text-to-speech API client.
405647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         */
406647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
407647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
408647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak
409647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        /**
41076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
41176d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * caller indicates to the TextToSpeech engine which specific sets of voice data to
41276d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * check for by sending an ArrayList<String> of the voices that are of interest.
41376d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
41476d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
415647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
416647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated Redundant functionality, checking for existence of specific sets of voice
417647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * data can be done on client side.
41876d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen         */
419647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
42076d9c3cb4f6f8c027959d2951e35a8b37762a1f3Charles Chen        public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
42199a0feecd0f0aad314d7a4637d329b8a9e8c1150Charles Chen
42277a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        // extras for a TTS engine's data installation
42377a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        /**
424647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent result.
4259f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * It indicates whether the data files for the synthesis engine were successfully
4269f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * installed. The installation was initiated with the  {@link #ACTION_INSTALL_TTS_DATA}
4279f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * intent. The possible values for this extra are
4289f5eadd2eed8b95c077a15d9e3e3c66fd151c215Jean-Michel Trivi         * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
429647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
430647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @deprecated No longer in use. If client ise interested in information about what
431647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * changed, is should send ACTION_CHECK_TTS_DATA intent to discover available voices.
43277a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi         */
433647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak        @Deprecated
43477a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi        public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
43577a5d39343760d9950ca15a87db0ae778afb4f2bJean-Michel Trivi
43662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // keys for the parameters passed with speak commands. Hidden keys are used internally
43762253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        // to maintain engine state for each TextToSpeech instance.
43862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
43950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
44062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
441ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_RATE = "rate";
44250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
44362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
44450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
44562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
446ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_LANGUAGE = "language";
44750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
44862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
44950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
45062253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
451ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_COUNTRY = "country";
45250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
45362253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
45450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
45562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
456ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_VARIANT = "variant";
45750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
45862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
45950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
46060dd360640a400d9b4a602160733281d284aaee5Charles Chen         */
46160dd360640a400d9b4a602160733281d284aaee5Charles Chen        public static final String KEY_PARAM_ENGINE = "engine";
46250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
46360dd360640a400d9b4a602160733281d284aaee5Charles Chen        /**
46450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * @hide
4651a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen         */
4661a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        public static final String KEY_PARAM_PITCH = "pitch";
46750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4681a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen        /**
46962253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * Parameter key to specify the audio stream type to be used when speaking text
47050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * or playing back a file. The value should be one of the STREAM_ constants
47150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * defined in {@link AudioManager}.
47250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
473a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
474a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
47562253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
476ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_STREAM = "streamType";
47750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
47862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi        /**
479a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * Parameter key to identify an utterance in the
480a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
48162253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         * spoken, a file has been played back or a silence duration has elapsed.
48250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
483a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
484a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
485a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi         * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
48662253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi         */
487ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi        public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
48850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4899d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
4909d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify the speech volume relative to the current stream type
4919d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
4929011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * where 0 is silence, and 1 is the maximum volume (the default behavior).
49350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
4949d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
4959d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
4969d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
4979d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_VOLUME = "volume";
49850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
4999d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        /**
5009d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Parameter key to specify how the speech is panned from left to right when speaking text.
5019d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
5029011ec832d78982d017b1cef55a54af44ba2a447Jean-Michel Trivi         * 0 to center (the default behavior), and +1 to hard-right.
50350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         *
5049d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#speak(String, int, HashMap)
5059d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         * @see TextToSpeech#playEarcon(String, int, HashMap)
5069d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi         */
5079d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        public static final String KEY_PARAM_PAN = "pan";
50862253a319d6359ce71c547d0b0aa36ba17789ab4Jean-Michel Trivi
509748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
510748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)}
511748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set (and supported by the engine
512748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must
513748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * use network based synthesis.
514748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         *
515748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#speak(String, int, java.util.HashMap)
516748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
517748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * @see TextToSpeech#getFeatures(java.util.Locale)
518748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
519748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
520748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
521748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        /**
522748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)}
523748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * for a description of how feature keys work. If set and supported by the engine
524748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize
525748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         * text on-device (without making network requests).
526647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         *
527647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @see TextToSpeech#speak(String, int, java.util.HashMap)
528647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
529647abce570f3afdd667e06e9ad71738efff0e8a8Przemyslaw Szczepaniak         * @see TextToSpeech#getFeatures(java.util.Locale)
530748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath         */
531748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
532d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi    }
533d146874d7341bc9602c93719582b4209e7b81f01Jean-Michel Trivi
53450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Context mContext;
5359c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak    private Connection mConnectingServiceConnection;
53650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Connection mServiceConnection;
53750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private OnInitListener mInitListener;
538a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // Written from an unspecified application thread, read from
539a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath    // a binder thread.
540754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    private volatile UtteranceProgressListener mUtteranceProgressListener;
54191bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    private final Object mStartLock = new Object();
54250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
54350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private String mRequestedEngine;
544c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // Whether to initialize this TTS object with the default engine,
545c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // if the requested engine is not available. Valid only if mRequestedEngine
546c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // is not null. Used only for testing, though potentially useful API wise
547c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    // too.
548c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath    private final boolean mUseFallback;
54950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mEarcons;
55050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Map<String, Uri> mUtterances;
55150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private final Bundle mParams = new Bundle();
552d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath    private final TtsEngines mEnginesHelper;
55368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    private final String mPackageName;
554bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    private volatile String mCurrentEngine = null;
555e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
556e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
5574bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the default TTS engine.
558a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * This will also initialize the associated TextToSpeech engine if it isn't already running.
559e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
560e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param context
561a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The context this instance is running in.
56291bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi     * @param listener
563a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The {@link TextToSpeech.OnInitListener} that will be called when the
564e86a936afddaa823d8454c09ed43c2d864ac77ebPrzemyslaw Szczepaniak     *            TextToSpeech engine has initialized. In a case of a failure the listener
565e86a936afddaa823d8454c09ed43c2d864ac77ebPrzemyslaw Szczepaniak     *            may be called immediately, before TextToSpeech instance is fully constructed.
566e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
56791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi    public TextToSpeech(Context context, OnInitListener listener) {
56850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        this(context, listener, null);
56950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
57050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
57150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
5724bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * The constructor for the TextToSpeech class, using the given TTS engine.
5734bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * This will also initialize the associated TextToSpeech engine if it isn't already running.
5744bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *
5754bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param context
5764bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The context this instance is running in.
5774bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param listener
5784bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     *            The {@link TextToSpeech.OnInitListener} that will be called when the
579e86a936afddaa823d8454c09ed43c2d864ac77ebPrzemyslaw Szczepaniak     *            TextToSpeech engine has initialized. In a case of a failure the listener
580e86a936afddaa823d8454c09ed43c2d864ac77ebPrzemyslaw Szczepaniak     *            may be called immediately, before TextToSpeech instance is fully constructed.
5814bbca889df9ca76c398f3a11e871fc6ad4a4514dBjorn Bringert     * @param engine Package name of the TTS engine to use.
58250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
58350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public TextToSpeech(Context context, OnInitListener listener, String engine) {
584c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        this(context, listener, engine, null, true);
58568e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    }
58668e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath
58768e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    /**
58868e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * Used by the framework to instantiate TextToSpeech objects with a supplied
58968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * package name, instead of using {@link android.content.Context#getPackageName()}
59068e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     *
59168e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     * @hide
59268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath     */
59368e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath    public TextToSpeech(Context context, OnInitListener listener, String engine,
594c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            String packageName, boolean useFallback) {
59591bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mContext = context;
59691bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        mInitListener = listener;
59750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = engine;
598c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        mUseFallback = useFallback;
59987c9684fd0fa31fd6ad7f7e9f4cfedddc4fdc4b0Jean-Michel Trivi
60050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mEarcons = new HashMap<String, Uri>();
60150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mUtterances = new HashMap<String, Uri>();
60268e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        mUtteranceProgressListener = null;
603a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
604d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        mEnginesHelper = new TtsEngines(mContext);
60568e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        if (packageName != null) {
60668e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = packageName;
60768e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        } else {
60868e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath            mPackageName = mContext.getPackageName();
60968e2af55d65d2e61fbf8096eccaa2e4ca02b6c5aNarayan Kamath        }
610da7681e7b61dd450be72f2b4a80e6d1c86342e05Jean-Michel Trivi        initTts();
611e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
612e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
6139c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak    private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method,
6149c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            boolean onlyEstablishedConnection) {
6159c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        return runAction(action, errorResult, method, false, onlyEstablishedConnection);
61650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
61750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
61850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private <R> R runAction(Action<R> action, R errorResult, String method) {
6199c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        return runAction(action, errorResult, method, true, true);
62050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
62150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
6229c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak    private <R> R runAction(Action<R> action, R errorResult, String method,
6239c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            boolean reconnect, boolean onlyEstablishedConnection) {
62450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
62550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mServiceConnection == null) {
62650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Log.w(TAG, method + " failed: not bound to TTS engine");
62750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return errorResult;
628e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
6299c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            return mServiceConnection.runAction(action, errorResult, method, reconnect,
6309c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    onlyEstablishedConnection);
63150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
63250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
633e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
63450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private int initTts() {
635b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 1: Try connecting to the engine that was requested.
636c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath        if (mRequestedEngine != null) {
637c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
638c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                if (connectToEngine(mRequestedEngine)) {
639c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = mRequestedEngine;
640c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return SUCCESS;
641c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                } else if (!mUseFallback) {
642c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    mCurrentEngine = null;
643c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    dispatchOnInit(ERROR);
644c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                    return ERROR;
645c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                }
646c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath            } else if (!mUseFallback) {
647c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                Log.i(TAG, "Requested engine not installed: " + mRequestedEngine);
648c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                mCurrentEngine = null;
649c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                dispatchOnInit(ERROR);
650c60aad2a8ab519a9f9ac03f4f9f929ded7ba2db5Narayan Kamath                return ERROR;
651b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath            }
65250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
65350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
654b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 2: Try connecting to the user's default engine.
655b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        final String defaultEngine = getDefaultEngine();
656b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) {
65750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (connectToEngine(defaultEngine)) {
658b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = defaultEngine;
65950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
660e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
66150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
662e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
663b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // Step 3: Try connecting to the highest ranked engine in the
664b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // system.
665d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        final String highestRanked = mEnginesHelper.getHighestRankedEngineName();
666b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        if (highestRanked != null && !highestRanked.equals(mRequestedEngine) &&
667b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                !highestRanked.equals(defaultEngine)) {
66822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath            if (connectToEngine(highestRanked)) {
669b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath                mCurrentEngine = highestRanked;
67050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
6711e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi            }
67250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
67350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
674b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // NOTE: The API currently does not allow the caller to query whether
675b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // they are actually connected to any engine. This might fail for various
676b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        // reasons like if the user disables all her TTS engines.
677b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath
678b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath        mCurrentEngine = null;
6790e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath        dispatchOnInit(ERROR);
68050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
68150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
68250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
68350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private boolean connectToEngine(String engine) {
68450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Connection connection = new Connection();
68550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
68650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        intent.setPackage(engine);
68750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
68850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!bound) {
68950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Log.e(TAG, "Failed to bind to " + engine);
69050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return false;
6911e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        } else {
6920e20fe5bab7dc3aff488d133961acfe0239f5240Narayan Kamath            Log.i(TAG, "Sucessfully bound to " + engine);
6939c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            mConnectingServiceConnection = connection;
69450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return true;
6951e13a02320aa165c22172d43b2b3c3cd8ad35cf7Jean-Michel Trivi        }
696e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
697e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
69850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void dispatchOnInit(int result) {
69950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
70050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (mInitListener != null) {
70150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener.onInit(result);
70250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mInitListener = null;
70350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
70450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
70550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
706e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
707492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    private IBinder getCallerIdentity() {
708492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        return mServiceConnection.getCallerIdentity();
709492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath    }
710492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
711e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
712a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Releases the resources used by the TextToSpeech engine.
713a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * It is good practice for instance to call this method in the onDestroy() method of an Activity
714a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * so the TextToSpeech engine can be cleanly stopped.
715e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
716e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public void shutdown() {
7179c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        // Special case, we are asked to shutdown connection that did finalize its connection.
7189c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        synchronized (mStartLock) {
7199c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            if (mConnectingServiceConnection != null) {
7209c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mContext.unbindService(mConnectingServiceConnection);
7219c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mConnectingServiceConnection = null;
7229c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                return;
7239c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            }
7249c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }
7259c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
7269c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        // Post connection case
72750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        runActionNoReconnect(new Action<Void>() {
72850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
72950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Void run(ITextToSpeechService service) throws RemoteException {
730492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.setCallback(getCallerIdentity(), null);
731492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                service.stop(getCallerIdentity());
73250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mServiceConnection.disconnect();
73390e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // Context#unbindService does not result in a call to
73490e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection#onServiceDisconnected. As a result, the
73590e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // service ends up being destroyed (if there are no other open
73690e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // connections to it) but the process lives on and the
73790e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // ServiceConnection continues to refer to the destroyed service.
73890e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                //
73990e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                // This leads to tons of log spam about SynthThread being dead.
74090e5650f96dabadaaf141beae20a646855073ae1Narayan Kamath                mServiceConnection = null;
741bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath                mCurrentEngine = null;
74250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return null;
74350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
7449c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }, null, "shutdown", false);
745e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
746e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
747e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
748e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound resource in a
749a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. After a call to this method, subsequent calls to
750a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * {@link #speak(String, int, HashMap)} will play the specified sound resource
751a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * if it is available, or synthesize the text it is missing.
752e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
753e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
754a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
755e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
756e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param packagename
757e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Pass the packagename of the application that contains the
758e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            resource. If the resource is in your own application (this is
759e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            the most common case), then put the packagename of your
760e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            application here.<br/>
761e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            Example: <b>"com.google.marvin.compass"</b><br/>
762e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The packagename can be found in the AndroidManifest.xml of
763e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            your application.
764e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <p>
765e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
766e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
767e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            </p>
768e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
769e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param resourceId
770a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.south_south_east</code>
7715c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
772ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
773e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7745c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String packagename, int resourceId) {
77550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized (mStartLock) {
77650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, makeResourceUri(packagename, resourceId));
77750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
778e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
779e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
780e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
781e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
782e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Adds a mapping between a string of text and a sound file. Using this, it
783a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * is possible to add custom pronounciations for a string of text.
784a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
785a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will play the specified sound resource if it is available, or synthesize the text it is
786a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * missing.
787e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
788e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param text
789a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The string of text. Example: <code>"south_south_east"</code>
790e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * @param filename
791e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            The full path to the sound file (for example:
792e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            "/sdcard/mysounds/hello.wav")
7935c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
794ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
795e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
7965c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int addSpeech(String text, String filename) {
79791bf30a4779146a14b2c9c5ce168d641cd31cb8eJean-Michel Trivi        synchronized (mStartLock) {
79850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mUtterances.put(text, Uri.parse(filename));
79950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
800e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
801e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
802e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
803e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
804e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
805904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * Adds a mapping between a string of text and a sound resource in a
806a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * package. Use this to add custom earcons.
807904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
808ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
809904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
810a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @param earcon The name of the earcon.
811a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code><br/>
812904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
813904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param packagename
814a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the package name of the application that contains the
815a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            resource. This can for instance be the package name of your own application.
816904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            Example: <b>"com.google.marvin.compass"</b><br/>
817a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The package name can be found in the AndroidManifest.xml of
818a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            the application containing the resource.
819904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <p>
820904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
821904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
822904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            </p>
823904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
824904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param resourceId
825a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>R.raw.tick_snd</code>
826904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
827ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
828904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
829904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String packagename, int resourceId) {
830904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        synchronized(mStartLock) {
83150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, makeResourceUri(packagename, resourceId));
83250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
833904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
834904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
835904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
836904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
837a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Adds a mapping between a string of text and a sound file.
838a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Use this to add custom earcons.
839a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *
840a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * @see #playEarcon(String, int, HashMap)
841904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
842904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param earcon
843a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            The name of the earcon.
844a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            Example: <code>"[tick]"</code>
845904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     * @param filename
846904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            The full path to the sound file (for example:
847904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *            "/sdcard/mysounds/tick.wav")
848904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     *
849ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
850904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen     */
851904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    public int addEarcon(String earcon, String filename) {
85250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        synchronized(mStartLock) {
85350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mEarcons.put(earcon, Uri.parse(filename));
85450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return SUCCESS;
855904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen        }
856904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    }
857904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
85850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Uri makeResourceUri(String packageName, int resourceId) {
85950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return new Uri.Builder()
86050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
86150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .encodedAuthority(packageName)
86250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .appendEncodedPath(String.valueOf(resourceId))
86350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                .build();
86450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
865904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen
866904dfa588431ff7c99c337d7797f5bef9ac12ce3Charles Chen    /**
867442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * Speaks the string using the specified queuing strategy and speech parameters.
868442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
869442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
870442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
871442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
872442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
873442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
874e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
8752d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The string of text to be spoken. No longer than
8762d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
87750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
87850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
87950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
88050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
88150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_UTTERANCE_ID},
88250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_VOLUME},
88350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_PAN}.
884b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
885b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
886b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
887b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
8885c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
889442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation.
890e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
89150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int speak(final String text, final int queueMode, final HashMap<String, String> params) {
89250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
89350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
89450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
89550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri utteranceUri = mUtterances.get(text);
89650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (utteranceUri != null) {
897492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
89850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            getParams(params));
89950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                } else {
900492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                    return service.speak(getCallerIdentity(), text, queueMode, getParams(params));
901a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
902e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
90350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "speak");
904e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
905e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
906e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
907e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     * Plays the earcon using the specified queueing mode and parameters.
90850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The earcon must already have been added with {@link #addEarcon(String, String)} or
90950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * {@link #addEarcon(String, String, int)}.
910442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
911442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
912442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
913442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
914442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
915442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
916e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
91750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param earcon The earcon that should be played
91850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
91950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
92050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
92150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            {@link Engine#KEY_PARAM_STREAM},
922a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
923b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
924b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
925b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
926b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
9275c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
928442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation.
929e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
93050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playEarcon(final String earcon, final int queueMode,
93150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
93250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
93350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
93450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
93550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                Uri earconUri = mEarcons.get(earcon);
93650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (earconUri == null) {
93750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return ERROR;
938a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi                }
939492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playAudio(getCallerIdentity(), earconUri, queueMode,
94050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
941e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
94250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playEarcon");
943e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
944679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi
9455c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    /**
9465c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * Plays silence for the specified amount of time using the specified
9475c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     * queue mode.
948442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
949442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
950442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
951442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
952442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
953442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
9545c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
95550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param durationInMs The duration of the silence.
95650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
95750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
95850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
959a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
960b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
961b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
962b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
963b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
9645c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
965442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilence operation.
9665c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     */
96750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int playSilence(final long durationInMs, final int queueMode,
96850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final HashMap<String, String> params) {
96950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
97050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
97150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
972492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.playSilence(getCallerIdentity(), durationInMs, queueMode,
97350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        getParams(params));
974f032bc7da536774a0b6a1c77632c65b935eee6faCharles Chen            }
97550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "playSilence");
976a8518c169bb34e540b7542ad5bd3891053d01a9fJean-Michel Trivi    }
977e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
978e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
979748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Queries the engine for the set of features it supports for a given locale.
980748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features can either be framework defined, e.g.
981748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
982748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Engine specific keys must be prefixed by the name of the engine they
983748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * are intended for. These keys can be used as parameters to
984748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
985748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
986748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
987748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * Features are boolean flags, and their values in the synthesis parameters
988748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * must be behave as per {@link Boolean#parseBoolean(String)}.
989748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     *
990748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     * @param locale The locale to query features for.
991748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath     */
992748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    public Set<String> getFeatures(final Locale locale) {
993748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        return runAction(new Action<Set<String>>() {
994748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            @Override
995748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            public Set<String> run(ITextToSpeechService service) throws RemoteException {
996ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                String[] features = null;
997ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                try {
998ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                    features = service.getFeaturesForLanguage(
999748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                        locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
1000ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                } catch(MissingResourceException e) {
1001ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 " +
1002ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                            "country code for locale: " + locale, e);
1003ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                    return null;
1004ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak                }
1005ca4f1fc57c86fb7e6094ba4117a027fffd79d227Przemyslaw Szczepaniak
1006748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                if (features != null) {
1007748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    final Set<String> featureSet = new HashSet<String>();
1008748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    Collections.addAll(featureSet, features);
1009748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    return featureSet;
1010748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                }
1011748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                return null;
1012748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            }
1013748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        }, null, "getFeatures");
1014748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    }
1015748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1016748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    /**
1017c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Checks whether the TTS engine is busy speaking. Note that a speech item is
1018c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * considered complete once it's audio data has been sent to the audio mixer, or
1019c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * written to a file. There might be a finite lag between this point, and when
1020c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * the audio hardware completes playback.
1021e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
102250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@code true} if the TTS engine is speaking.
1023e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
1024e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public boolean isSpeaking() {
102550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Boolean>() {
102650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
102750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Boolean run(ITextToSpeechService service) throws RemoteException {
102850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isSpeaking();
1029e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
103050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, false, "isSpeaking");
1031e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1032e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1033e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1034a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interrupts the current utterance (whether played or rendered to file) and discards other
1035a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * utterances in the queue.
10365c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
103750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1038e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
10395c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int stop() {
104050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
104150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
104250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1043492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.stop(getCallerIdentity());
1044e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
104550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "stop");
1046e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1047e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1048e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
104950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the speech rate.
1050e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
1051679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
1052e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
105350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
105450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
105550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
10565c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
105750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1058e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
10595c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setSpeechRate(float speechRate) {
106050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (speechRate > 0.0f) {
106150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intRate = (int)(speechRate * 100);
106250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intRate > 0) {
106350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
106450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
1065679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi                }
106650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
1067e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
1068e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
106950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
1070e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1071e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1072e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1073a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Sets the speech pitch for the TextToSpeech engine.
10742ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
10752ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
10762ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
107750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
10782ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            lower values lower the tone of the synthesized voice,
10792ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            greater values increase it.
10805c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
108150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
10822ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     */
10835c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setPitch(float pitch) {
108450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (pitch > 0.0f) {
108550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intPitch = (int)(pitch * 100);
108650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intPitch > 0) {
108750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
108850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
10892ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi                }
109050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
10912ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi            }
10922ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi        }
109350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
10942ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    }
10952ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi
10962ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    /**
1097bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @return the engine currently in use by this TextToSpeech instance.
1098bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @hide
1099bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     */
1100bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    public String getCurrentEngine() {
1101bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath        return mCurrentEngine;
1102bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    }
1103bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath
1104bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    /**
1105b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used as the default
1106b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Text-to-speech language.
1107b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *
1108b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1109b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1110b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     */
1111b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    public Locale getDefaultLanguage() {
1112b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        return runAction(new Action<Locale>() {
1113b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            @Override
1114b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) throws RemoteException {
1115b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                String[] defaultLanguage = service.getClientDefaultLanguage();
1116b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1117b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
1118b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            }
1119b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        }, null, "getDefaultLanguage");
1120b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    }
1121b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1122b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    /**
112350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the text-to-speech language.
112450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The TTS engine will try to use the closest match to the specified
1125a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * language as represented by the Locale, but there is no guarantee that the exact same Locale
1126a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1127a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * before choosing the language to use for the next utterances.
1128e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
112950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The locale describing the language to be used.
11305c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
113150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1132ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1133ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1134e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
113550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setLanguage(final Locale loc) {
113650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
113750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
113850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
113950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (loc == null) {
114050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return LANG_NOT_SUPPORTED;
114150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
1142d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                String language = null, country = null;
1143d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1144d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    language = loc.getISO3Language();
1145d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1146d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e);
1147d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1148d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1149d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1150d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1151d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    country = loc.getISO3Country();
1152d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1153d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e);
1154d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1155d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1156d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
11571a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String variant = loc.getVariant();
1158d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
11591a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Check if the language, country, variant are available, and cache
11601a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // the available parts.
11611a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Note that the language is not actually set here, instead it is cached so it
11621a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // will be associated with all upcoming utterances.
116313896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak
116413896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak                int result = service.loadLanguage(getCallerIdentity(), language, country, variant);
11651a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                if (result >= LANG_AVAILABLE){
116650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
116750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        variant = "";
116850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        if (result < LANG_COUNTRY_AVAILABLE) {
116950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            country = "";
117050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        }
11711a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                    }
117250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
117350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
117450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
11751a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                }
117650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return result;
1177e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
117850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "setLanguage");
1179e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1180e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1181aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
1182228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used for synthesis
1183228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * requests sent to the TextToSpeech engine.
118450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
1185228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android 4.2 and before (API <= 17) this function returns the language that is currently
1186228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * being used by the TTS engine. That is the last language set by this or any other
1187228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * client by a {@link TextToSpeech#setLanguage} call to the same engine.
1188228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1189228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android versions after 4.2 this function returns the language that is currently being
1190228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * used for the synthesis requests sent from this client. That is the last language set
1191228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * by a {@link TextToSpeech#setLanguage} call on this instance.
1192228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1193228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1194228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1195ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi     */
1196ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    public Locale getLanguage() {
119750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Locale>() {
119850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
1199228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) {
1200228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                /* No service call, but we're accessing mParams, hence need for
1201228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                   wrapping it as an Action instance */
1202228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, "");
1203228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, "");
1204228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, "");
1205228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                return new Locale(lang, country, variant);
1206ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
120750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "getLanguage");
1208ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    }
1209ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi
1210ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    /**
1211a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Checks if the specified language as represented by the Locale is available and supported.
1212aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     *
121350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The Locale describing the language to be used.
12145c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
121550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1216ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1217ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1218aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
121950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int isLanguageAvailable(final Locale loc) {
122050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
122150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
122250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1223d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                String language = null, country = null;
1224d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1225d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1226d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    language = loc.getISO3Language();
1227d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1228d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e);
1229d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1230d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1231d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1232d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1233d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    country = loc.getISO3Country();
1234d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1235d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e);
1236d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1237d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1238d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1239d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                return service.isLanguageAvailable(language, country, loc.getVariant());
1240ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
124150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
1242aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    }
1243aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
1244e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1245d4989093ed708ddf9c799655ea0af7afda726426Charles Chen     * Synthesizes the given text to a file using the specified parameters.
1246442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1247442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
1248442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
1249442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
1250442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
1251442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1252e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
12532d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The text that should be synthesized. No longer than
12542d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
125550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
125650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
1257a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1258b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
1259b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
1260b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1261b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
126250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param filename Absolute file filename to write the generated audio data to.It should be
1263e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            something like "/sdcard/myappsounds/mysound.wav".
12645c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
1265442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
1266e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
126750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int synthesizeToFile(final String text, final HashMap<String, String> params,
126850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final String filename) {
126950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
127050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
127150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
12725acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                ParcelFileDescriptor fileDescriptor;
12735acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                int returnValue;
12745acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                try {
12755acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    File file = new File(filename);
12765acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    if(file.exists() && !file.canWrite()) {
12775acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                        Log.e(TAG, "Can't write to " + filename);
12785acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                        return ERROR;
12795acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    }
12805acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    fileDescriptor = ParcelFileDescriptor.open(file,
12815acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            ParcelFileDescriptor.MODE_WRITE_ONLY |
12825acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            ParcelFileDescriptor.MODE_CREATE |
12835acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            ParcelFileDescriptor.MODE_TRUNCATE);
12845acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    returnValue = service.synthesizeToFileDescriptor(getCallerIdentity(), text,
12855acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            fileDescriptor, getParams(params));
12865acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    fileDescriptor.close();
12875acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    return returnValue;
12885acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                } catch (FileNotFoundException e) {
12895acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    Log.e(TAG, "Opening file " + filename + " failed", e);
12905acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    return ERROR;
12915acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                } catch (IOException e) {
12925acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    Log.e(TAG, "Closing file " + filename + " failed", e);
12935acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    return ERROR;
12945acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                }
1295e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
129650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "synthesizeToFile");
129750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
129850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
129950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Bundle getParams(HashMap<String, String> params) {
130050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (params != null && !params.isEmpty()) {
130150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Bundle bundle = new Bundle(mParams);
130250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
130350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
130450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
130550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
1306b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
1307748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            // Copy feature strings defined by the framework.
1308748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
13096c07a2028505e28abd601870f05d4752e92a4b7bNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1310748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1311b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // Copy over all parameters that start with the name of the
1312b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // engine that we are currently connected to. The engine is
1313b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // free to interpret them as it chooses.
1314b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            if (!TextUtils.isEmpty(mCurrentEngine)) {
1315b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                for (Map.Entry<String, String> entry : params.entrySet()) {
1316b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    final String key = entry.getKey();
1317b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    if (key != null && key.startsWith(mCurrentEngine)) {
1318b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                        bundle.putString(key, entry.getValue());
1319b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    }
1320b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                }
1321b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            }
1322b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
132350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return bundle;
132450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        } else {
132550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mParams;
1326e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
1327e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1328e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
132950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
133050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String value = params.get(key);
133150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (value != null) {
133250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            bundle.putString(key, value);
133350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
133450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
1335a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
133650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
133750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
133850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
133950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
134050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                int value = Integer.parseInt(valueString);
134150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putInt(key, value);
134250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
134350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
134450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
134550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
13469d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi    }
13479d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
134850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
134950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
135050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
135150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
135250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                float value = Float.parseFloat(valueString);
135350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putFloat(key, value);
135450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
135550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
135650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
13579d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        }
1358a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi    }
1359a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
136078c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
136150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the listener that will be notified when synthesis of an utterance completes.
136278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
136350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param listener The listener to use.
136478c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
136550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1366754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1367754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)}
1368754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *        instead.
136978c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
1370754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    @Deprecated
137150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1372754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = UtteranceProgressListener.from(listener);
1373754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        return TextToSpeech.SUCCESS;
1374754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    }
1375754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1376754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    /**
1377754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * Sets the listener that will be notified of various events related to the
1378754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * synthesis of a given utterance.
1379754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1380754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * See {@link UtteranceProgressListener} and
1381754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}.
1382754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1383754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @param listener the listener to use.
1384754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @return {@link #ERROR} or {@link #SUCCESS}
1385754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     */
1386754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    public int setOnUtteranceProgressListener(UtteranceProgressListener listener) {
1387754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = listener;
1388a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        return TextToSpeech.SUCCESS;
138978c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
139078c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
1391b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    /**
139250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the TTS engine to use.
1393b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
13943f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     * @deprecated This doesn't inform callers when the TTS engine has been
13953f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
1396b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        can be used with the appropriate engine name. Also, there is no
1397b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        guarantee that the engine specified will be loaded. If it isn't
1398b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        installed or disabled, the user / system wide defaults will apply.
13993f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *
140050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
1401b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
140250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1403b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     */
14043f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath    @Deprecated
140560dd360640a400d9b4a602160733281d284aaee5Charles Chen    public int setEngineByPackageName(String enginePackageName) {
140650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = enginePackageName;
140750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return initTts();
1408b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    }
1409b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen
1410def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    /**
141150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets the package name of the default speech synthesis engine.
1412def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     *
141322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return Package name of the TTS engine that the user has chosen
141422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     *        as their default.
1415def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     */
1416def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    public String getDefaultEngine() {
1417d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getDefaultEngine();
141850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
141950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
142050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
1421c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * Checks whether the user's settings should override settings requested
1422c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * by the calling application. As of the Ice cream sandwich release,
1423c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * user settings never forcibly override the app's settings.
142450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
142550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public boolean areDefaultsEnforced() {
1426c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath        return false;
142750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
142850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
142942229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    /**
143050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets a list of all installed TTS engines.
143142229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     *
143222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return A list of engine info objects. The list can be empty, but never {@code null}.
143342229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     */
143450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public List<EngineInfo> getEngines() {
1435d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getEngines();
143622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath    }
143722302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
143850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private class Connection implements ServiceConnection {
143950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        private ITextToSpeechService mService;
14408f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14419c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private SetupConnectionAsyncTask mOnSetupConnectionAsyncTask;
14429c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
14439c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private boolean mEstablished;
14448f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1445a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() {
1446a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            @Override
1447754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onDone(String utteranceId) {
1448754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1449754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1450754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onDone(utteranceId);
1451754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1452754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1453754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1454754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1455754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onError(String utteranceId) {
1456754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1457754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1458754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onError(utteranceId);
1459754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1460754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1461754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1462754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1463754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onStart(String utteranceId) {
1464754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1465a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                if (listener != null) {
1466754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onStart(utteranceId);
1467a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
1468a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            }
1469a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        };
147050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
14719c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private class SetupConnectionAsyncTask extends AsyncTask<Void, Void, Integer> {
14728f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            private final ComponentName mName;
14738f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14749c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            public SetupConnectionAsyncTask(ComponentName name) {
14758f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mName = name;
14768f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
14778f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14788f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
14798f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected Integer doInBackground(Void... params) {
14808f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
14818f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    if (isCancelled()) {
14828f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return null;
14838f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
14848f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14858f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    try {
14869c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        mService.setCallback(getCallerIdentity(), mCallback);
14879c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        String[] defaultLanguage = mService.getClientDefaultLanguage();
1488b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1489b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]);
1490b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]);
1491b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]);
1492b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1493b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        Log.i(TAG, "Set up connection to " + mName);
14948f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return SUCCESS;
14958f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    } catch (RemoteException re) {
14968f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        Log.e(TAG, "Error connecting to service, setCallback() failed");
14978f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return ERROR;
14988f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
14998f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
15008f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
15018f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15028f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
15038f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected void onPostExecute(Integer result) {
15048f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
15059c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    if (mOnSetupConnectionAsyncTask == this) {
15069c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        mOnSetupConnectionAsyncTask = null;
15078f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
15089c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mEstablished = true;
15098f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    dispatchOnInit(result);
15108f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
15118f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
15128f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
15138f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1514091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak        @Override
151550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void onServiceConnected(ComponentName name, IBinder service) {
151650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
15179c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mConnectingServiceConnection = null;
15189c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
15198f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                Log.i(TAG, "Connected to " + name);
15208f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15219c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                if (mOnSetupConnectionAsyncTask != null) {
15229c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mOnSetupConnectionAsyncTask.cancel(false);
1523a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
15248f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15259c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mService = ITextToSpeechService.Stub.asInterface(service);
15269c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mServiceConnection = Connection.this;
15279c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
15289c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mEstablished = false;
15299c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mOnSetupConnectionAsyncTask = new SetupConnectionAsyncTask(name);
15309c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mOnSetupConnectionAsyncTask.execute();
153150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
153250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
153350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1534492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        public IBinder getCallerIdentity() {
1535492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath            return mCallback;
1536492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        }
1537492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
15388f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        /**
15398f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set.
15408f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         *
15419c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak         * @return true if we cancel mOnSetupConnectionAsyncTask in progress.
15428f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         */
15438f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        private boolean clearServiceConnection() {
154450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
15458f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                boolean result = false;
15469c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                if (mOnSetupConnectionAsyncTask != null) {
15479c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    result = mOnSetupConnectionAsyncTask.cancel(false);
15489c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mOnSetupConnectionAsyncTask = null;
15498f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
15508f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
155150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mService = null;
155250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // If this is the active connection, clear it
155350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (mServiceConnection == this) {
155450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mServiceConnection = null;
155550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
15568f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                return result;
15578f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
15588f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
15598f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15608f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        @Override
15618f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        public void onServiceDisconnected(ComponentName name) {
15628f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            Log.i(TAG, "Asked to disconnect from " + name);
15638f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            if (clearServiceConnection()) {
15648f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                /* We need to protect against a rare case where engine
15658f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * dies just after successful connection - and we process onServiceDisconnected
15668f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels
15678f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit
15688f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * with ERROR as argument.
15698f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 */
15708f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                dispatchOnInit(ERROR);
157150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
157250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
157350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
157450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void disconnect() {
157550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mContext.unbindService(this);
15768f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            clearServiceConnection();
157750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
157850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
15799c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        public boolean isEstablished() {
15809c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            return mService != null && mEstablished;
15819c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }
15829c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
15839c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        public <R> R runAction(Action<R> action, R errorResult, String method,
15849c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                boolean reconnect, boolean onlyEstablishedConnection) {
1585091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak            synchronized (mStartLock) {
1586091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                try {
158750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (mService == null) {
158850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        Log.w(TAG, method + " failed: not connected to TTS engine");
158950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        return errorResult;
159050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    }
15911c2df38242c29cd7f359a6fb13dc13b387264310Fergus Henderson                    if (onlyEstablishedConnection && !isEstablished()) {
15921c2df38242c29cd7f359a6fb13dc13b387264310Fergus Henderson                        Log.w(TAG, method + " failed: TTS engine connection not fully set up");
15939c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        return errorResult;
15949c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    }
159550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return action.run(mService);
1596091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                } catch (RemoteException ex) {
1597091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    Log.e(TAG, method + " failed", ex);
1598091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    if (reconnect) {
1599091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        disconnect();
1600091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        initTts();
1601091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    }
1602091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    return errorResult;
160350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
160442229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen            }
160542229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen        }
160642229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    }
16072cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
160850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private interface Action<R> {
160950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        R run(ITextToSpeechService service) throws RemoteException;
161050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
161150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
16122cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    /**
161350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Information about an installed text-to-speech engine.
161450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
161550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @see TextToSpeech#getEngines
16162cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert     */
161750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static class EngineInfo {
161850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
161950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Engine package name..
162050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
162150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String name;
162250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
162350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Localized label for the engine.
162450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
162550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String label;
162650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
162750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Icon for the engine.
162850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
162950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public int icon;
163022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
163122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * Whether this engine is a part of the system
163222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * image.
1633d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1634d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
163522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1636d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public boolean system;
163722302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
163822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * The priority the engine declares for the the intent filter
163922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * {@code android.intent.action.TTS_SERVICE}
1640d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1641d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
164222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1643d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public int priority;
164450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
164550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @Override
164650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String toString() {
164750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return "EngineInfo{name=" + name + "}";
164850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
164950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
16502cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    }
165122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
16522d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    /**
16532d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * Limit of length of input string passed to speak and synthesizeToFile.
16542d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *
16552d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #speak
16562d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #synthesizeToFile
16572d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     */
16582d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    public static int getMaxSpeechInputLength() {
16592d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak        return 4000;
16602d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    }
1661e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi}
1662