@@ -306,6 +306,7 @@ public void quit() {
306306 */
307307 public int enqueueSpeechItem (int queueMode , final SpeechItem speechItem ) {
308308 if (!speechItem .isValid ()) {
309+ speechItem .dispatchOnError ();
309310 return TextToSpeech .ERROR ;
310311 }
311312
@@ -332,6 +333,7 @@ public void run() {
332333 return TextToSpeech .SUCCESS ;
333334 } else {
334335 Log .w (TAG , "SynthThread has quit" );
336+ speechItem .dispatchOnError ();
335337 return TextToSpeech .ERROR ;
336338 }
337339 }
@@ -381,14 +383,16 @@ public int stopAll() {
381383 }
382384 }
383385
384- interface UtteranceCompletedDispatcher {
385- public void dispatchUtteranceCompleted ();
386+ interface UtteranceProgressDispatcher {
387+ public void dispatchOnDone ();
388+ public void dispatchOnStart ();
389+ public void dispatchOnError ();
386390 }
387391
388392 /**
389393 * An item in the synth thread queue.
390394 */
391- private abstract class SpeechItem implements UtteranceCompletedDispatcher {
395+ private abstract class SpeechItem implements UtteranceProgressDispatcher {
392396 private final String mCallingApp ;
393397 protected final Bundle mParams ;
394398 private boolean mStarted = false ;
@@ -443,10 +447,27 @@ public void stop() {
443447 stopImpl ();
444448 }
445449
446- public void dispatchUtteranceCompleted () {
450+ @ Override
451+ public void dispatchOnDone () {
452+ final String utteranceId = getUtteranceId ();
453+ if (!TextUtils .isEmpty (utteranceId )) {
454+ mCallbacks .dispatchOnDone (getCallingApp (), utteranceId );
455+ }
456+ }
457+
458+ @ Override
459+ public void dispatchOnStart () {
460+ final String utteranceId = getUtteranceId ();
461+ if (!TextUtils .isEmpty (utteranceId )) {
462+ mCallbacks .dispatchOnStart (getCallingApp (), utteranceId );
463+ }
464+ }
465+
466+ @ Override
467+ public void dispatchOnError () {
447468 final String utteranceId = getUtteranceId ();
448469 if (!TextUtils .isEmpty (utteranceId )) {
449- mCallbacks .dispatchUtteranceCompleted (getCallingApp (), utteranceId );
470+ mCallbacks .dispatchOnError (getCallingApp (), utteranceId );
450471 }
451472 }
452473
@@ -617,9 +638,12 @@ protected AbstractSynthesisCallback createSynthesisCallback() {
617638
618639 @ Override
619640 protected int playImpl () {
641+ dispatchOnStart ();
620642 int status = super .playImpl ();
621643 if (status == TextToSpeech .SUCCESS ) {
622- dispatchUtteranceCompleted ();
644+ dispatchOnDone ();
645+ } else {
646+ dispatchOnError ();
623647 }
624648 return status ;
625649 }
@@ -856,16 +880,34 @@ public void setCallback(String packageName, ITextToSpeechCallback cb) {
856880 }
857881 }
858882
859- public void dispatchUtteranceCompleted (String packageName , String utteranceId ) {
860- ITextToSpeechCallback cb ;
861- synchronized (mAppToCallback ) {
862- cb = mAppToCallback .get (packageName );
883+ public void dispatchOnDone (String packageName , String utteranceId ) {
884+ ITextToSpeechCallback cb = getCallbackFor (packageName );
885+ if (cb == null ) return ;
886+ try {
887+ cb .onDone (utteranceId );
888+ } catch (RemoteException e ) {
889+ Log .e (TAG , "Callback onDone failed: " + e );
863890 }
891+ }
892+
893+ public void dispatchOnStart (String packageName , String utteranceId ) {
894+ ITextToSpeechCallback cb = getCallbackFor (packageName );
895+ if (cb == null ) return ;
896+ try {
897+ cb .onStart (utteranceId );
898+ } catch (RemoteException e ) {
899+ Log .e (TAG , "Callback onStart failed: " + e );
900+ }
901+
902+ }
903+
904+ public void dispatchOnError (String packageName , String utteranceId ) {
905+ ITextToSpeechCallback cb = getCallbackFor (packageName );
864906 if (cb == null ) return ;
865907 try {
866- cb .utteranceCompleted (utteranceId );
908+ cb .onError (utteranceId );
867909 } catch (RemoteException e ) {
868- Log .e (TAG , "Callback failed: " + e );
910+ Log .e (TAG , "Callback onError failed: " + e );
869911 }
870912 }
871913
@@ -886,6 +928,15 @@ public void kill() {
886928 }
887929 }
888930
931+ private ITextToSpeechCallback getCallbackFor (String packageName ) {
932+ ITextToSpeechCallback cb ;
933+ synchronized (mAppToCallback ) {
934+ cb = mAppToCallback .get (packageName );
935+ }
936+
937+ return cb ;
938+ }
939+
889940 }
890941
891942}
0 commit comments