1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.assist.client.tts;
18 
19 import android.content.Context;
20 import android.media.AudioAttributes;
21 import android.os.Bundle;
22 import android.speech.tts.TextToSpeech;
23 import android.speech.tts.UtteranceProgressListener;
24 
25 /**
26  * Implementation of {@link TextToSpeechEngine} by delegating to Android's {@link TextToSpeech} API.
27  * <p>
28  * NOTE: {@link #initialize(Context, TextToSpeech.OnInitListener)} must be called to use this
29  * engine. After {@link #shutdown()}, {@link #initialize(Context, TextToSpeech.OnInitListener)} may
30  * be called again to re-use it.
31  */
32 class AndroidTextToSpeechEngine implements TextToSpeechEngine {
33     private TextToSpeech mTextToSpeech;
34 
35     @Override
initialize(Context context, TextToSpeech.OnInitListener initListener)36     public void initialize(Context context, TextToSpeech.OnInitListener initListener) {
37         if (mTextToSpeech == null) {
38             mTextToSpeech = new TextToSpeech(context, initListener);
39         }
40     }
41 
42     @Override
isInitialized()43     public boolean isInitialized() {
44         return mTextToSpeech != null;
45     }
46 
47     @Override
setOnUtteranceProgressListener(UtteranceProgressListener progressListener)48     public void setOnUtteranceProgressListener(UtteranceProgressListener progressListener)
49             throws IllegalStateException {
50         assertInit();
51         mTextToSpeech.setOnUtteranceProgressListener(progressListener);
52     }
53 
54     @Override
setAudioAttributes(AudioAttributes audioAttributes)55     public void setAudioAttributes(AudioAttributes audioAttributes) {
56         assertInit();
57         mTextToSpeech.setAudioAttributes(audioAttributes);
58     }
59 
60     @Override
speak(CharSequence text, int queueMode, Bundle params, String utteranceId)61     public int speak(CharSequence text, int queueMode, Bundle params, String utteranceId)
62             throws IllegalStateException {
63         assertInit();
64         return mTextToSpeech.speak(text, queueMode, params, utteranceId);
65     }
66 
67     @Override
stop()68     public void stop() throws IllegalStateException {
69         assertInit();
70         mTextToSpeech.stop();
71     }
72 
73     @Override
isSpeaking()74     public boolean isSpeaking() {
75         return mTextToSpeech != null && mTextToSpeech.isSpeaking();
76     }
77 
78     @Override
shutdown()79     public void shutdown() throws IllegalStateException {
80         assertInit();
81         mTextToSpeech.shutdown();
82         mTextToSpeech = null;
83     }
84 
85     @Override
getStream()86     public int getStream() {
87         return TextToSpeech.Engine.DEFAULT_STREAM;
88     }
89 
90     /**
91      * Asserts that the TTS engine has been initialized.
92      *
93      * @throws IllegalStateException if the TTS has not been initialized.
94      */
assertInit()95     private void assertInit() throws IllegalStateException {
96         if (!isInitialized()) {
97             throw new IllegalStateException("TTS Engine must be initialized before use.");
98         }
99     }
100 }
101