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 {
996748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                String[] features = service.getFeaturesForLanguage(
997748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                        locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
998748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                if (features != null) {
999748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    final Set<String> featureSet = new HashSet<String>();
1000748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    Collections.addAll(featureSet, features);
1001748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                    return featureSet;
1002748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                }
1003748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath                return null;
1004748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            }
1005748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath        }, null, "getFeatures");
1006748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    }
1007748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1008748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath    /**
1009c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * Checks whether the TTS engine is busy speaking. Note that a speech item is
1010c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * considered complete once it's audio data has been sent to the audio mixer, or
1011c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * written to a file. There might be a finite lag between this point, and when
1012c34f76fe89b5a31d01d63067c2f24b9a6a76df18Narayan Kamath     * the audio hardware completes playback.
1013e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
101450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@code true} if the TTS engine is speaking.
1015e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
1016e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    public boolean isSpeaking() {
101750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Boolean>() {
101850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
101950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Boolean run(ITextToSpeechService service) throws RemoteException {
102050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return service.isSpeaking();
1021e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
102250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, false, "isSpeaking");
1023e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1024e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1025e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1026a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Interrupts the current utterance (whether played or rendered to file) and discards other
1027a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * utterances in the queue.
10285c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
102950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1030e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
10315c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int stop() {
103250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
103350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
103450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1035492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath                return service.stop(getCallerIdentity());
1036e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
103750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "stop");
1038e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1039e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1040e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
104150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the speech rate.
1042e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
1043679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
1044e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
104550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
104650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
104750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
10485c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
104950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1050e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
10515c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setSpeechRate(float speechRate) {
105250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (speechRate > 0.0f) {
105350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intRate = (int)(speechRate * 100);
105450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intRate > 0) {
105550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
105650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
1057679d728f09eeab2f8b882e42f6e081db1ac74996Jean-Michel Trivi                }
105850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
1059e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
1060e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
106150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
1062e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1063e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1064e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1065a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Sets the speech pitch for the TextToSpeech engine.
10662ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
10672ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     * This has no effect on any pre-recorded speech.
10682ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *
106950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
10702ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            lower values lower the tone of the synthesized voice,
10712ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     *            greater values increase it.
10725c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
107350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
10742ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi     */
10755c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen    public int setPitch(float pitch) {
107650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (pitch > 0.0f) {
107750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            int intPitch = (int)(pitch * 100);
107850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            if (intPitch > 0) {
107950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                synchronized (mStartLock) {
108050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
10812ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi                }
108250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return SUCCESS;
10832ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi            }
10842ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi        }
108550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return ERROR;
10862ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    }
10872ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi
10882ea5349583de4a505501530d04133524bb6d5d38Jean-Michel Trivi    /**
1089bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @return the engine currently in use by this TextToSpeech instance.
1090bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     * @hide
1091bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath     */
1092bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    public String getCurrentEngine() {
1093bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath        return mCurrentEngine;
1094bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    }
1095bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath
1096bd2492e14ee9db9139cc0ff0bd29fc9864b0126cNarayan Kamath    /**
1097b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used as the default
1098b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * Text-to-speech language.
1099b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *
1100b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1101b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1102b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak     */
1103b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    public Locale getDefaultLanguage() {
1104b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        return runAction(new Action<Locale>() {
1105b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            @Override
1106b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) throws RemoteException {
1107b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                String[] defaultLanguage = service.getClientDefaultLanguage();
1108b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1109b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]);
1110b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak            }
1111b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak        }, null, "getDefaultLanguage");
1112b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    }
1113b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1114b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak    /**
111550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the text-to-speech language.
111650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * The TTS engine will try to use the closest match to the specified
1117a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * language as represented by the Locale, but there is no guarantee that the exact same Locale
1118a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1119a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * before choosing the language to use for the next utterances.
1120e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
112150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The locale describing the language to be used.
11225c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
112350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1124ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1125ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1126e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
112750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setLanguage(final Locale loc) {
112850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
112950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
113050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
113150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (loc == null) {
113250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return LANG_NOT_SUPPORTED;
113350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
1134d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                String language = null, country = null;
1135d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1136d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    language = loc.getISO3Language();
1137d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1138d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e);
1139d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1140d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1141d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1142d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1143d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    country = loc.getISO3Country();
1144d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1145d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e);
1146d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1147d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1148d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
11491a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                String variant = loc.getVariant();
1150d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
11511a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Check if the language, country, variant are available, and cache
11521a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // the available parts.
11531a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // Note that the language is not actually set here, instead it is cached so it
11541a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                // will be associated with all upcoming utterances.
115513896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak
115613896b74194b07c821d5d89713e4e747b9b77d73Przemyslaw Szczepaniak                int result = service.loadLanguage(getCallerIdentity(), language, country, variant);
11571a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                if (result >= LANG_AVAILABLE){
115850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
115950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        variant = "";
116050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        if (result < LANG_COUNTRY_AVAILABLE) {
116150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                            country = "";
116250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        }
11631a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                    }
116450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
116550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
116650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
11671a2712ce2a18eba6809d984d2f7443fbdccaa7edCharles Chen                }
116850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                return result;
1169e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
117050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "setLanguage");
1171e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1172e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
1173aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    /**
1174228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * Returns a Locale instance describing the language currently being used for synthesis
1175228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * requests sent to the TextToSpeech engine.
117650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
1177228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android 4.2 and before (API <= 17) this function returns the language that is currently
1178228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * being used by the TTS engine. That is the last language set by this or any other
1179228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * client by a {@link TextToSpeech#setLanguage} call to the same engine.
1180228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1181228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * In Android versions after 4.2 this function returns the language that is currently being
1182228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * used for the synthesis requests sent from this client. That is the last language set
1183228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * by a {@link TextToSpeech#setLanguage} call on this instance.
1184228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *
1185228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     * @return language, country (if any) and variant (if any) used by the client stored in a
1186228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak     *     Locale instance, or {@code null} on error.
1187ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi     */
1188ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    public Locale getLanguage() {
118950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Locale>() {
119050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
1191228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak            public Locale run(ITextToSpeechService service) {
1192228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                /* No service call, but we're accessing mParams, hence need for
1193228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                   wrapping it as an Action instance */
1194228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, "");
1195228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, "");
1196228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, "");
1197228323e759445398e2d723de9365da71099ec58bPrzemyslaw Szczepaniak                return new Locale(lang, country, variant);
1198ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
119950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, null, "getLanguage");
1200ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    }
1201ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi
1202ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi    /**
1203a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     * Checks if the specified language as represented by the Locale is available and supported.
1204aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     *
120550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param loc The Locale describing the language to be used.
12065c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
120750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1208ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1209ed06578eddde07abe325fa4c92910bb7246cd49fJean-Michel Trivi     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
1210aaf842edbfe76990413d4c002acb394f855321b5Charles Chen     */
121150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int isLanguageAvailable(final Locale loc) {
121250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
121350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
121450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
1215d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                String language = null, country = null;
1216d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1217d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1218d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    language = loc.getISO3Language();
1219d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1220d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e);
1221d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1222d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1223d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1224d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                try {
1225d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    country = loc.getISO3Country();
1226d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                } catch (MissingResourceException e) {
1227d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e);
1228d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                    return LANG_NOT_SUPPORTED;
1229d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                }
1230d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak
1231d0b927948ec06ab90da639fe0a7bf1ec862301a0Przemyslaw Szczepaniak                return service.isLanguageAvailable(language, country, loc.getVariant());
1232ddb0a803fd353fbaf0139cc8804499bc9dce7403Jean-Michel Trivi            }
123350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
1234aaf842edbfe76990413d4c002acb394f855321b5Charles Chen    }
1235aaf842edbfe76990413d4c002acb394f855321b5Charles Chen
1236e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    /**
1237d4989093ed708ddf9c799655ea0af7afda726426Charles Chen     * Synthesizes the given text to a file using the specified parameters.
1238442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
1239442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * requests and then returns. The synthesis might not have finished (or even started!) at the
1240442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * time when this method returns. In order to reliably detect errors during synthesis,
1241442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * we recommend setting an utterance progress listener (see
1242442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link #setOnUtteranceProgressListener}) and using the
1243442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
1244e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *
12452d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @param text The text that should be synthesized. No longer than
12462d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *            {@link #getMaxSpeechInputLength()} characters.
124750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param params Parameters for the request. Can be null.
124850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *            Supported parameter names:
1249a9b417e9bfea3da908884c726ffc9bf4f64691cfJean-Michel Trivi     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
1250b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            Engine specific parameters may be passed in but the parameter keys
1251b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            must be prefixed by the name of the engine they are intended for. For example
1252b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
1253b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath     *            engine named "com.svox.pico" if it is being used.
125450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param filename Absolute file filename to write the generated audio data to.It should be
1255e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     *            something like "/sdcard/myappsounds/mysound.wav".
12565c22f516be7753859f62ca3ff5327c453ee26faaCharles Chen     *
1257442f1786b243923a11d7c15a32e96c7962d4b50aPrzemyslaw Szczepaniak     * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation.
1258e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi     */
125950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int synthesizeToFile(final String text, final HashMap<String, String> params,
126050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            final String filename) {
126150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return runAction(new Action<Integer>() {
126250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            @Override
126350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            public Integer run(ITextToSpeechService service) throws RemoteException {
12645acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                ParcelFileDescriptor fileDescriptor;
12655acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                int returnValue;
12665acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                try {
12675acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    File file = new File(filename);
12685acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    if(file.exists() && !file.canWrite()) {
12695acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                        Log.e(TAG, "Can't write to " + filename);
12705acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                        return ERROR;
12715acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    }
12725acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    fileDescriptor = ParcelFileDescriptor.open(file,
12735acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            ParcelFileDescriptor.MODE_WRITE_ONLY |
12745acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            ParcelFileDescriptor.MODE_CREATE |
12755acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            ParcelFileDescriptor.MODE_TRUNCATE);
12765acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    returnValue = service.synthesizeToFileDescriptor(getCallerIdentity(), text,
12775acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                            fileDescriptor, getParams(params));
12785acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    fileDescriptor.close();
12795acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    return returnValue;
12805acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                } catch (FileNotFoundException e) {
12815acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    Log.e(TAG, "Opening file " + filename + " failed", e);
12825acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    return ERROR;
12835acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                } catch (IOException e) {
12845acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    Log.e(TAG, "Closing file " + filename + " failed", e);
12855acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                    return ERROR;
12865acb33af357b56fffb055997718b1e4aa97f53fcPrzemyslaw Szczepaniak                }
1287e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi            }
128850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }, ERROR, "synthesizeToFile");
128950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
129050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
129150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private Bundle getParams(HashMap<String, String> params) {
129250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (params != null && !params.isEmpty()) {
129350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            Bundle bundle = new Bundle(mParams);
129450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
129550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
129650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
129750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
1298b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
1299748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            // Copy feature strings defined by the framework.
1300748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
13016c07a2028505e28abd601870f05d4752e92a4b7bNarayan Kamath            copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
1302748af66ca27d3afe2e16ccc80b147d447635292aNarayan Kamath
1303b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // Copy over all parameters that start with the name of the
1304b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // engine that we are currently connected to. The engine is
1305b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            // free to interpret them as it chooses.
1306b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            if (!TextUtils.isEmpty(mCurrentEngine)) {
1307b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                for (Map.Entry<String, String> entry : params.entrySet()) {
1308b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    final String key = entry.getKey();
1309b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    if (key != null && key.startsWith(mCurrentEngine)) {
1310b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                        bundle.putString(key, entry.getValue());
1311b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                    }
1312b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath                }
1313b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath            }
1314b956f37e375bb2588208d4b5e8a40fae6fae5f86Narayan Kamath
131550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return bundle;
131650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        } else {
131750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return mParams;
1318e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi        }
1319e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi    }
1320e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi
132150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
132250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String value = params.get(key);
132350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (value != null) {
132450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            bundle.putString(key, value);
132550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
132650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
1327a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
132850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
132950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
133050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
133150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
133250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                int value = Integer.parseInt(valueString);
133350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putInt(key, value);
133450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
133550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
133650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
133750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
13389d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi    }
13399d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi
134050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
134150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        String valueString = params.get(key);
134250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        if (!TextUtils.isEmpty(valueString)) {
134350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            try {
134450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                float value = Float.parseFloat(valueString);
134550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                bundle.putFloat(key, value);
134650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            } catch (NumberFormatException ex) {
134750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // don't set the value in the bundle
134850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
13499d2d26af2e1111251f5a21213a071eb4fdc1224fJean-Michel Trivi        }
1350a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi    }
1351a981013aa7315e13c6c5f6aad489813c419031eaJean-Michel Trivi
135278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    /**
135350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the listener that will be notified when synthesis of an utterance completes.
135478c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
135550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param listener The listener to use.
135678c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     *
135750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1358754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1359754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)}
1360754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *        instead.
136178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen     */
1362754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    @Deprecated
136350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1364754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = UtteranceProgressListener.from(listener);
1365754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        return TextToSpeech.SUCCESS;
1366754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    }
1367754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1368754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    /**
1369754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * Sets the listener that will be notified of various events related to the
1370754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * synthesis of a given utterance.
1371754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1372754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * See {@link UtteranceProgressListener} and
1373754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}.
1374754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     *
1375754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @param listener the listener to use.
1376754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     * @return {@link #ERROR} or {@link #SUCCESS}
1377754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath     */
1378754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath    public int setOnUtteranceProgressListener(UtteranceProgressListener listener) {
1379754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath        mUtteranceProgressListener = listener;
1380a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        return TextToSpeech.SUCCESS;
138178c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen    }
138278c9d0d2c5eb4d5687ae7cbe41155159329ad68fCharles Chen
1383b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    /**
138450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Sets the TTS engine to use.
1385b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
13863f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     * @deprecated This doesn't inform callers when the TTS engine has been
13873f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
1388b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        can be used with the appropriate engine name. Also, there is no
1389b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        guarantee that the engine specified will be loaded. If it isn't
1390b9db1fb9de483f35f0189dae240b38e8a9cea8c9Narayan Kamath     *        installed or disabled, the user / system wide defaults will apply.
13913f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath     *
139250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
1393b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     *
139450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @return {@link #ERROR} or {@link #SUCCESS}.
1395b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen     */
13963f0363bb4b9fab9799ac308dd48baf8830e30647Narayan Kamath    @Deprecated
139760dd360640a400d9b4a602160733281d284aaee5Charles Chen    public int setEngineByPackageName(String enginePackageName) {
139850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        mRequestedEngine = enginePackageName;
139950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        return initTts();
1400b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen    }
1401b4fbe768f8bfb2550dec100e29d0edc09b8a051aCharles Chen
1402def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    /**
140350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets the package name of the default speech synthesis engine.
1404def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     *
140522302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return Package name of the TTS engine that the user has chosen
140622302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     *        as their default.
1407def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen     */
1408def7185cf19d48c95b50c2b83503d5cd21a613bfCharles Chen    public String getDefaultEngine() {
1409d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getDefaultEngine();
141050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
141150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
141250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    /**
1413c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * Checks whether the user's settings should override settings requested
1414c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * by the calling application. As of the Ice cream sandwich release,
1415c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath     * user settings never forcibly override the app's settings.
141650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     */
141750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public boolean areDefaultsEnforced() {
1418c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0Narayan Kamath        return false;
141950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
142050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
142142229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    /**
142250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Gets a list of all installed TTS engines.
142342229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     *
142422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath     * @return A list of engine info objects. The list can be empty, but never {@code null}.
142542229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen     */
142650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public List<EngineInfo> getEngines() {
1427d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        return mEnginesHelper.getEngines();
142822302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath    }
142922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
143050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private class Connection implements ServiceConnection {
143150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        private ITextToSpeechService mService;
14328f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14339c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private SetupConnectionAsyncTask mOnSetupConnectionAsyncTask;
14349c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
14359c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private boolean mEstablished;
14368f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1437a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() {
1438a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            @Override
1439754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onDone(String utteranceId) {
1440754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1441754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1442754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onDone(utteranceId);
1443754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1444754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1445754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1446754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1447754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onError(String utteranceId) {
1448754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1449754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                if (listener != null) {
1450754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onError(utteranceId);
1451754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                }
1452754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            }
1453754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath
1454754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            @Override
1455754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath            public void onStart(String utteranceId) {
1456754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                UtteranceProgressListener listener = mUtteranceProgressListener;
1457a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                if (listener != null) {
1458754c72ed9e8e83e5a913aa7552fc2e1b1b5277e0Narayan Kamath                    listener.onStart(utteranceId);
1459a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
1460a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath            }
1461a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath        };
146250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
14639c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        private class SetupConnectionAsyncTask extends AsyncTask<Void, Void, Integer> {
14648f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            private final ComponentName mName;
14658f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14669c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            public SetupConnectionAsyncTask(ComponentName name) {
14678f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                mName = name;
14688f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
14698f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14708f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
14718f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected Integer doInBackground(Void... params) {
14728f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
14738f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    if (isCancelled()) {
14748f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return null;
14758f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
14768f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14778f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    try {
14789c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        mService.setCallback(getCallerIdentity(), mCallback);
14799c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        String[] defaultLanguage = mService.getClientDefaultLanguage();
1480b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1481b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]);
1482b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]);
1483b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]);
1484b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak
1485b46533732c40c6aa4d0d7357176835a33d863234Przemyslaw Szczepaniak                        Log.i(TAG, "Set up connection to " + mName);
14868f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return SUCCESS;
14878f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    } catch (RemoteException re) {
14888f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        Log.e(TAG, "Error connecting to service, setCallback() failed");
14898f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                        return ERROR;
14908f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
14918f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
14928f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
14938f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
14948f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            @Override
14958f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            protected void onPostExecute(Integer result) {
14968f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                synchronized(mStartLock) {
14979c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    if (mOnSetupConnectionAsyncTask == this) {
14989c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        mOnSetupConnectionAsyncTask = null;
14998f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    }
15009c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mEstablished = true;
15018f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                    dispatchOnInit(result);
15028f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
15038f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
15048f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
15058f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
1506091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak        @Override
150750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void onServiceConnected(ComponentName name, IBinder service) {
150850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
15099c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mConnectingServiceConnection = null;
15109c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
15118f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                Log.i(TAG, "Connected to " + name);
15128f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15139c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                if (mOnSetupConnectionAsyncTask != null) {
15149c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mOnSetupConnectionAsyncTask.cancel(false);
1515a57f23837ad172c1b046d5e9cc8eb3d2e41a69f4Narayan Kamath                }
15168f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15179c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mService = ITextToSpeechService.Stub.asInterface(service);
15189c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mServiceConnection = Connection.this;
15199c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
15209c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mEstablished = false;
15219c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mOnSetupConnectionAsyncTask = new SetupConnectionAsyncTask(name);
15229c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                mOnSetupConnectionAsyncTask.execute();
152350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
152450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
152550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
1526492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        public IBinder getCallerIdentity() {
1527492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath            return mCallback;
1528492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath        }
1529492b7f0d51f53164aa6eb974cd7ab6a7889af677Narayan Kamath
15308f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        /**
15318f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set.
15328f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         *
15339c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak         * @return true if we cancel mOnSetupConnectionAsyncTask in progress.
15348f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak         */
15358f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        private boolean clearServiceConnection() {
153650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            synchronized(mStartLock) {
15378f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                boolean result = false;
15389c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                if (mOnSetupConnectionAsyncTask != null) {
15399c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    result = mOnSetupConnectionAsyncTask.cancel(false);
15409c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    mOnSetupConnectionAsyncTask = null;
15418f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                }
15428f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
154350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                mService = null;
154450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                // If this is the active connection, clear it
154550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                if (mServiceConnection == this) {
154650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    mServiceConnection = null;
154750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
15488f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                return result;
15498f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            }
15508f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        }
15518f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak
15528f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        @Override
15538f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak        public void onServiceDisconnected(ComponentName name) {
15548f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            Log.i(TAG, "Asked to disconnect from " + name);
15558f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            if (clearServiceConnection()) {
15568f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                /* We need to protect against a rare case where engine
15578f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * dies just after successful connection - and we process onServiceDisconnected
15588f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels
15598f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit
15608f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 * with ERROR as argument.
15618f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                 */
15628f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak                dispatchOnInit(ERROR);
156350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            }
156450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
156550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
156650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public void disconnect() {
156750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            mContext.unbindService(this);
15688f3957cb91a9e1465fa11aaf4d4286d4c5a59ba7Przemyslaw Szczepaniak            clearServiceConnection();
156950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
157050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
15719c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        public boolean isEstablished() {
15729c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak            return mService != null && mEstablished;
15739c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        }
15749c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak
15759c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak        public <R> R runAction(Action<R> action, R errorResult, String method,
15769c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                boolean reconnect, boolean onlyEstablishedConnection) {
1577091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak            synchronized (mStartLock) {
1578091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                try {
157950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    if (mService == null) {
158050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        Log.w(TAG, method + " failed: not connected to TTS engine");
158150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                        return errorResult;
158250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    }
15831c2df38242c29cd7f359a6fb13dc13b387264310Fergus Henderson                    if (onlyEstablishedConnection && !isEstablished()) {
15841c2df38242c29cd7f359a6fb13dc13b387264310Fergus Henderson                        Log.w(TAG, method + " failed: TTS engine connection not fully set up");
15859c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                        return errorResult;
15869c4012b31b0c09cb14689bd96a71aae42c8a00cdPrzemyslaw Szczepaniak                    }
158750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                    return action.run(mService);
1588091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                } catch (RemoteException ex) {
1589091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    Log.e(TAG, method + " failed", ex);
1590091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    if (reconnect) {
1591091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        disconnect();
1592091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                        initTts();
1593091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    }
1594091d56cab8f6f6a3460fbb596f99b1a262948e96Przemyslaw Szczepaniak                    return errorResult;
159550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert                }
159642229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen            }
159742229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen        }
159842229259a6fca8851db74dc1c0ecbab2d3fb788dCharles Chen    }
15992cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert
160050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    private interface Action<R> {
160150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        R run(ITextToSpeechService service) throws RemoteException;
160250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    }
160350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
16042cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    /**
160550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * Information about an installed text-to-speech engine.
160650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     *
160750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert     * @see TextToSpeech#getEngines
16082cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert     */
160950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert    public static class EngineInfo {
161050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
161150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Engine package name..
161250e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
161350e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String name;
161450e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
161550e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Localized label for the engine.
161650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
161750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String label;
161850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        /**
161950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         * Icon for the engine.
162050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert         */
162150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public int icon;
162222302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
162322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * Whether this engine is a part of the system
162422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * image.
1625d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1626d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
162722302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1628d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public boolean system;
162922302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath        /**
163022302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * The priority the engine declares for the the intent filter
163122302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         * {@code android.intent.action.TTS_SERVICE}
1632d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         *
1633d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath         * @hide
163422302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath         */
1635d3ee2fa18464fb7e4d7f6d27610fbf60b6d1ffceNarayan Kamath        public int priority;
163650e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
163750e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        @Override
163850e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        public String toString() {
163950e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert            return "EngineInfo{name=" + name + "}";
164050e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert        }
164150e657bb2d005568f5dd8bc1d904d07b0d94018fBjorn Bringert
16422cad2cc15345d8623049a17712068e813d305a25Bjorn Bringert    }
164322302fb7ba11f75234f8a268f5932973dd080bf9Narayan Kamath
16442d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    /**
16452d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * Limit of length of input string passed to speak and synthesizeToFile.
16462d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     *
16472d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #speak
16482d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     * @see #synthesizeToFile
16492d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak     */
16502d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    public static int getMaxSpeechInputLength() {
16512d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak        return 4000;
16522d940bcbd1c472f8b11ce1495354f340604b4f2cPrzemyslaw Szczepaniak    }
1653e74d507d171de60cc028e6176fe08cc9cdd8b701Jean-Michel Trivi}
1654