1package android.support.v4.speech.tts;
2
3import android.speech.tts.TextToSpeech;
4import android.speech.tts.UtteranceProgressListener;
5import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
6
7import java.util.Locale;
8import java.util.Set;
9
10/** Helper class for TTS functionality introduced in ICS MR1 */
11class TextToSpeechICSMR1 {
12    /**
13     * Call {@link TextToSpeech#getFeatures} if available.
14     *
15     * @return {@link TextToSpeech#getFeatures} or null on older devices.
16     */
17    static Set<String> getFeatures(TextToSpeech tts, Locale locale) {
18        if (android.os.Build.VERSION.SDK_INT >=
19                android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
20            return tts.getFeatures(locale);
21        }
22        return null;
23    }
24
25    public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
26    public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
27
28    static interface UtteranceProgressListenerICSMR1 {
29        void onDone(String utteranceId);
30        void onError(String utteranceId);
31        void onStart(String utteranceId);
32    }
33
34    /**
35     * Call {@link TextToSpeech#setOnUtteranceProgressListener} if ICS-MR1 or newer.
36     *
37     * On pre ICS-MR1 devices,{@link TextToSpeech#setOnUtteranceCompletedListener} is
38     * used to emulate its behavior - at the end of synthesis we call
39     * {@link UtteranceProgressListenerICSMR1#onStart(String)} and
40     * {@link UtteranceProgressListenerICSMR1#onDone(String)} one after the other.
41     * Errors can't be detected.
42     */
43    static void setUtteranceProgressListener(TextToSpeech tts,
44            final UtteranceProgressListenerICSMR1 listener) {
45        if (android.os.Build.VERSION.SDK_INT >=
46                android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
47            tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
48                @Override
49                public void onStart(String utteranceId) {
50                    listener.onStart(utteranceId);
51                }
52
53                @Override
54                public void onError(String utteranceId) {
55                    listener.onError(utteranceId);
56                }
57
58                @Override
59                public void onDone(String utteranceId) {
60                    listener.onDone(utteranceId);
61                }
62            });
63        } else {
64            tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
65                @Override
66                public void onUtteranceCompleted(String utteranceId) {
67                    // Emulate onStart. Clients are expecting it will happen.
68                    listener.onStart(utteranceId);
69                    listener.onDone(utteranceId);
70                }
71            });
72        }
73    }
74}
75