Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b45451f6 authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 6583 into donut

* changes:
  Adding a lock to stop so that stop will stop utterances that are just starting up.
parents 3bb69233 0a3d8744
Loading
Loading
Loading
Loading
+36 −11
Original line number Original line Diff line number Diff line
@@ -38,6 +38,8 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Locale;
import java.util.Locale;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.TimeUnit;



/**
/**
 * @hide Synthesizes speech from text. This is implemented as a service so that
 * @hide Synthesizes speech from text. This is implemented as a service so that
@@ -358,10 +360,17 @@ public class TtsService extends Service implements OnCompletionListener {
     * Stops all speech output and removes any utterances still in the queue.
     * Stops all speech output and removes any utterances still in the queue.
     */
     */
    private int stop() {
    private int stop() {
        int result = TextToSpeech.TTS_ERROR;
        boolean speechQueueAvailable = false;
        try{
            // If the queue is locked for more than 1 second,
            // something has gone very wrong with processSpeechQueue.
            speechQueueAvailable = speechQueueLock.tryLock(1000, TimeUnit.MILLISECONDS);
            if (speechQueueAvailable) {
                Log.i("TTS", "Stopping");
                Log.i("TTS", "Stopping");
                mSpeechQueue.clear();
                mSpeechQueue.clear();


        int result = nativeSynth.stop();
                result = nativeSynth.stop();
                mIsSpeaking = false;
                mIsSpeaking = false;
                if (mPlayer != null) {
                if (mPlayer != null) {
                    try {
                    try {
@@ -371,8 +380,19 @@ public class TtsService extends Service implements OnCompletionListener {
                    }
                    }
                }
                }
                Log.i("TTS", "Stopped");
                Log.i("TTS", "Stopped");
            }
        } catch (InterruptedException e) {
          Log.e("TTS stop", "tryLock interrupted");
          e.printStackTrace();
        } finally {
            // This check is needed because finally will always run; even if the
            // method returns somewhere in the try block.
            if (speechQueueAvailable) {
                speechQueueLock.unlock();
            }
            return result;
            return result;
        }
        }
    }


    public void onCompletion(MediaPlayer arg0) {
    public void onCompletion(MediaPlayer arg0) {
        processSpeechQueue();
        processSpeechQueue();
@@ -443,6 +463,7 @@ public class TtsService extends Service implements OnCompletionListener {
                    }
                    }
                    nativeSynth.speak(text);
                    nativeSynth.speak(text);
                } catch (InterruptedException e) {
                } catch (InterruptedException e) {
                    Log.e("TTS speakInternalOnly", "tryLock interrupted");
                    e.printStackTrace();
                    e.printStackTrace();
                } finally {
                } finally {
                    // This check is needed because finally will always run;
                    // This check is needed because finally will always run;
@@ -497,6 +518,7 @@ public class TtsService extends Service implements OnCompletionListener {
                    }
                    }
                    nativeSynth.synthesizeToFile(text, filename);
                    nativeSynth.synthesizeToFile(text, filename);
                } catch (InterruptedException e) {
                } catch (InterruptedException e) {
                    Log.e("TTS synthToFileInternalOnly", "tryLock interrupted");
                    e.printStackTrace();
                    e.printStackTrace();
                } finally {
                } finally {
                    // This check is needed because finally will always run;
                    // This check is needed because finally will always run;
@@ -650,6 +672,9 @@ public class TtsService extends Service implements OnCompletionListener {
            if (mSpeechQueue.size() > 0) {
            if (mSpeechQueue.size() > 0) {
                mSpeechQueue.remove(0);
                mSpeechQueue.remove(0);
            }
            }
        } catch (InterruptedException e) {
            Log.e("TTS processSpeechQueue", "tryLock interrupted");
            e.printStackTrace();
        } finally {
        } finally {
            // This check is needed because finally will always run; even if the
            // This check is needed because finally will always run; even if the
            // method returns somewhere in the try block.
            // method returns somewhere in the try block.