Merge change 7094 into donut

* changes:
  Cleaning up TtsService
This commit is contained in:
Android (Google) Code Review
2009-07-14 10:55:57 -07:00

View File

@@ -76,7 +76,8 @@ public class TtsService extends Service implements OnCompletionListener {
mCallingApp = source; mCallingApp = source;
} }
public SpeechItem(String source, String text, ArrayList<String> params, int itemType, String filename) { public SpeechItem(String source, String text, ArrayList<String> params,
int itemType, String filename) {
mText = text; mText = text;
mParams = params; mParams = params;
mType = itemType; mType = itemType;
@@ -118,8 +119,10 @@ public class TtsService extends Service implements OnCompletionListener {
private static final String CATEGORY = "android.intent.category.TTS"; private static final String CATEGORY = "android.intent.category.TTS";
private static final String PKGNAME = "android.tts"; private static final String PKGNAME = "android.tts";
private final RemoteCallbackList<android.speech.tts.ITtsCallback> mCallbacks = new RemoteCallbackList<ITtsCallback>(); private final RemoteCallbackList<ITtsCallback> mCallbacks
private HashMap<String, android.speech.tts.ITtsCallback> mCallbacksMap; = new RemoteCallbackList<ITtsCallback>();
private HashMap<String, ITtsCallback> mCallbacksMap;
private Boolean mIsSpeaking; private Boolean mIsSpeaking;
private ArrayList<SpeechItem> mSpeechQueue; private ArrayList<SpeechItem> mSpeechQueue;
@@ -446,25 +449,22 @@ public class TtsService extends Service implements OnCompletionListener {
private void silence(final SpeechItem speechItem) { private void silence(final SpeechItem speechItem) {
class SilenceThread implements Runnable { class SilenceThread implements Runnable {
public void run() { public void run() {
long duration = speechItem.mDuration;
String callingApp = speechItem.mCallingApp;
ArrayList<String> params = speechItem.mParams;
String utteranceId = ""; String utteranceId = "";
if (params != null){ if (speechItem.mParams != null){
for (int i = 0; i < params.size() - 1; i = i + 2){ for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
String param = params.get(i); String param = speechItem.mParams.get(i);
if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_UTTERANCE_ID)){ if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_UTTERANCE_ID)){
utteranceId = params.get(i+1); utteranceId = speechItem.mParams.get(i+1);
} }
} }
} }
try { try {
Thread.sleep(duration); Thread.sleep(speechItem.mDuration);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (utteranceId.length() > 0){ if (utteranceId.length() > 0){
dispatchUtteranceCompletedCallback(utteranceId, callingApp); dispatchUtteranceCompletedCallback(utteranceId, speechItem.mCallingApp);
} }
mCurrentSpeechItem = null; mCurrentSpeechItem = null;
processSpeechQueue(); processSpeechQueue();
@@ -479,9 +479,6 @@ public class TtsService extends Service implements OnCompletionListener {
private void speakInternalOnly(final SpeechItem speechItem) { private void speakInternalOnly(final SpeechItem speechItem) {
class SynthThread implements Runnable { class SynthThread implements Runnable {
public void run() { public void run() {
String text = speechItem.mText;
ArrayList<String> params = speechItem.mParams;
String callingApp = speechItem.mCallingApp;
boolean synthAvailable = false; boolean synthAvailable = false;
String utteranceId = ""; String utteranceId = "";
try { try {
@@ -494,26 +491,28 @@ public class TtsService extends Service implements OnCompletionListener {
return; return;
} }
int streamType = DEFAULT_STREAM_TYPE; int streamType = DEFAULT_STREAM_TYPE;
if (params != null){ if (speechItem.mParams != null){
String language = ""; String language = "";
String country = ""; String country = "";
String variant = ""; String variant = "";
for (int i = 0; i < params.size() - 1; i = i + 2){ for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
String param = params.get(i); String param = speechItem.mParams.get(i);
if (param != null) { if (param != null) {
if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_RATE)) { if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_RATE)) {
setSpeechRate("", Integer.parseInt(params.get(i+1))); setSpeechRate("",
Integer.parseInt(speechItem.mParams.get(i+1)));
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_LANGUAGE)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_LANGUAGE)){
language = params.get(i+1); language = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_COUNTRY)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_COUNTRY)){
country = params.get(i+1); country = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_VARIANT)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_VARIANT)){
variant = params.get(i+1); variant = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_UTTERANCE_ID)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_UTTERANCE_ID)){
utteranceId = params.get(i+1); utteranceId = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_STREAM)) { } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_STREAM)) {
try { try {
streamType = Integer.parseInt(params.get(i + 1)); streamType
= Integer.parseInt(speechItem.mParams.get(i + 1));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
streamType = DEFAULT_STREAM_TYPE; streamType = DEFAULT_STREAM_TYPE;
} }
@@ -524,7 +523,7 @@ public class TtsService extends Service implements OnCompletionListener {
setLanguage("", language, country, variant); setLanguage("", language, country, variant);
} }
} }
nativeSynth.speak(text, streamType); nativeSynth.speak(speechItem.mText, streamType);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.e("TTS speakInternalOnly", "tryLock interrupted"); Log.e("TTS speakInternalOnly", "tryLock interrupted");
e.printStackTrace(); e.printStackTrace();
@@ -536,7 +535,7 @@ public class TtsService extends Service implements OnCompletionListener {
synthesizerLock.unlock(); synthesizerLock.unlock();
} }
if (utteranceId.length() > 0){ if (utteranceId.length() > 0){
dispatchUtteranceCompletedCallback(utteranceId, callingApp); dispatchUtteranceCompletedCallback(utteranceId, speechItem.mCallingApp);
} }
mCurrentSpeechItem = null; mCurrentSpeechItem = null;
processSpeechQueue(); processSpeechQueue();
@@ -551,13 +550,9 @@ public class TtsService extends Service implements OnCompletionListener {
private void synthToFileInternalOnly(final SpeechItem speechItem) { private void synthToFileInternalOnly(final SpeechItem speechItem) {
class SynthThread implements Runnable { class SynthThread implements Runnable {
public void run() { public void run() {
String text = speechItem.mText;
ArrayList<String> params = speechItem.mParams;
String filename = speechItem.mFilename;
String callingApp = speechItem.mCallingApp;
boolean synthAvailable = false; boolean synthAvailable = false;
String utteranceId = ""; String utteranceId = "";
Log.i("TTS", "Synthesizing to " + filename); Log.i("TTS", "Synthesizing to " + speechItem.mFilename);
try { try {
synthAvailable = synthesizerLock.tryLock(); synthAvailable = synthesizerLock.tryLock();
if (!synthAvailable) { if (!synthAvailable) {
@@ -567,23 +562,24 @@ public class TtsService extends Service implements OnCompletionListener {
synth.start(); synth.start();
return; return;
} }
if (params != null){ if (speechItem.mParams != null){
String language = ""; String language = "";
String country = ""; String country = "";
String variant = ""; String variant = "";
for (int i = 0; i < params.size() - 1; i = i + 2){ for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
String param = params.get(i); String param = speechItem.mParams.get(i);
if (param != null){ if (param != null){
if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_RATE)){ if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_RATE)){
setSpeechRate("", Integer.parseInt(params.get(i+1))); setSpeechRate("",
Integer.parseInt(speechItem.mParams.get(i+1)));
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_LANGUAGE)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_LANGUAGE)){
language = params.get(i+1); language = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_COUNTRY)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_COUNTRY)){
country = params.get(i+1); country = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_VARIANT)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_VARIANT)){
variant = params.get(i+1); variant = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_UTTERANCE_ID)){ } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_UTTERANCE_ID)){
utteranceId = params.get(i+1); utteranceId = speechItem.mParams.get(i+1);
} }
} }
} }
@@ -591,7 +587,7 @@ public class TtsService extends Service implements OnCompletionListener {
setLanguage("", language, country, variant); setLanguage("", language, country, variant);
} }
} }
nativeSynth.synthesizeToFile(text, filename); nativeSynth.synthesizeToFile(speechItem.mText, speechItem.mFilename);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.e("TTS synthToFileInternalOnly", "tryLock interrupted"); Log.e("TTS synthToFileInternalOnly", "tryLock interrupted");
e.printStackTrace(); e.printStackTrace();
@@ -603,7 +599,7 @@ public class TtsService extends Service implements OnCompletionListener {
synthesizerLock.unlock(); synthesizerLock.unlock();
} }
if (utteranceId.length() > 0){ if (utteranceId.length() > 0){
dispatchUtteranceCompletedCallback(utteranceId, callingApp); dispatchUtteranceCompletedCallback(utteranceId, speechItem.mCallingApp);
} }
mCurrentSpeechItem = null; mCurrentSpeechItem = null;
processSpeechQueue(); processSpeechQueue();