diff --git a/app/src/main/java/com/termux/api/apis/SpeechToTextAPI.java b/app/src/main/java/com/termux/api/apis/SpeechToTextAPI.java index c33d7a31a..06bd8ecc1 100644 --- a/app/src/main/java/com/termux/api/apis/SpeechToTextAPI.java +++ b/app/src/main/java/com/termux/api/apis/SpeechToTextAPI.java @@ -19,6 +19,8 @@ import java.io.PrintWriter; import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.LinkedBlockingQueue; public class SpeechToTextAPI { @@ -38,6 +40,8 @@ public SpeechToTextService(String name) { } protected SpeechRecognizer mSpeechRecognizer; + final CountDownLatch latch = new CountDownLatch(1); + final Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); final LinkedBlockingQueue queueu = new LinkedBlockingQueue<>(); private static final String LOG_TAG = "SpeechToTextService"; @@ -47,6 +51,7 @@ public void onCreate() { Logger.logDebug(LOG_TAG, "onCreate"); super.onCreate(); + final Context context = this; mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); @@ -62,6 +67,10 @@ public void onResults(Bundle results) { List recognitions = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); Logger.logError(LOG_TAG, "RecognitionListener#onResults(" + recognitions + ")"); queueu.addAll(recognitions); + // On full mode, stop only when recognition is achieved + if (!recognizerIntent.getBooleanExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)) { + queueu.add(STOP_ELEMENT); + } } @Override @@ -108,7 +117,11 @@ public void onError(int error) { @Override public void onEndOfSpeech() { Logger.logError(LOG_TAG, "RecognitionListener#onEndOfSpeech()"); - queueu.add(STOP_ELEMENT); + + // On partial mode, stop recognition when speech ends + if (recognizerIntent.getBooleanExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)) { + queueu.add(STOP_ELEMENT); + } } @Override @@ -141,13 +154,12 @@ public void onBeginningOfSpeech() { .create().show(); } - Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); - recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Enter shell command"); - recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); - recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10); - recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US"); - recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); + try { + latch.await(1, TimeUnit.SECONDS); + } catch (InterruptedException e) { + } mSpeechRecognizer.startListening(recognizerIntent); + } @Override @@ -162,9 +174,20 @@ public void onDestroy() { protected void onHandleIntent(final Intent intent) { Logger.logDebug(LOG_TAG, "onHandleIntent:\n" + IntentUtils.getIntentString(intent)); + + recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Enter shell command"); + recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10); + recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US"); + recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); + + recognizerIntent.putExtras(intent.getExtras()); + latch.countDown(); + ResultReturner.returnData(this, intent, new ResultReturner.WithInput() { @Override public void writeResult(PrintWriter out) throws Exception { + Logger.logError(LOG_TAG, "Start listening"); while (true) { String s = queueu.take(); if (s == STOP_ELEMENT) {