summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorAndrea Ambu <andreaambu@google.com>2022-01-21 16:45:47 +0000
committerAndrea Ambu <andreaambu@google.com>2022-02-04 11:15:50 +0000
commit950a0f3e7845a81991e70736ea21b375c96dd864 (patch)
treee5a79d0f60c2280a83f0022d11bcb7b209e0ce54 /core/java/android
parent499454759689e38bce250762d64f51ad20d525c3 (diff)
speech: Allow long speech sessions
See go/one-speech-long-form Bug: 204189031 Cts-Coverage-Bug: 204189031 Test: atest Change-Id: I62adfa7aca5e8df32253c141bd4dd7bc7e4fad3c
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/speech/IRecognitionListener.aidl18
-rw-r--r--core/java/android/speech/RecognitionListener.java27
-rw-r--r--core/java/android/speech/RecognitionService.java20
-rw-r--r--core/java/android/speech/RecognizerIntent.java12
-rw-r--r--core/java/android/speech/SpeechRecognizer.java16
5 files changed, 92 insertions, 1 deletions
diff --git a/core/java/android/speech/IRecognitionListener.aidl b/core/java/android/speech/IRecognitionListener.aidl
index 7c79b1ae15de..986a41c64053 100644
--- a/core/java/android/speech/IRecognitionListener.aidl
+++ b/core/java/android/speech/IRecognitionListener.aidl
@@ -78,6 +78,24 @@ oneway interface IRecognitionListener {
void onPartialResults(in Bundle results);
/**
+ * Called for each ready segment of a recognition request. To request segmented speech results
+ * use {@link RecognizerIntent#EXTRA_SEGMENT_SESSION}. The callback might be called
+ * any number of times between {@link #onBeginningOfSpeech()} and
+ * {@link #onEndOfSegmentedSession()}.
+ *
+ * @param segmentResults the returned results. To retrieve the results in
+ * ArrayList&lt;String&gt; format use {@link Bundle#getStringArrayList(String)} with
+ * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter
+ */
+ void onSegmentResults(in Bundle results);
+
+ /**
+ * Called at the end of a segmented recognition request. To request segmented speech results
+ * use {@link RecognizerIntent#EXTRA_SEGMENT_SESSION}.
+ */
+ void onEndOfSegmentedSession();
+
+ /**
* Reserved for adding future events.
*
* @param eventType the type of the occurred event
diff --git a/core/java/android/speech/RecognitionListener.java b/core/java/android/speech/RecognitionListener.java
index c94b60f847f4..64fd09ff7faf 100644
--- a/core/java/android/speech/RecognitionListener.java
+++ b/core/java/android/speech/RecognitionListener.java
@@ -15,6 +15,7 @@
*/
package android.speech;
+import android.annotation.NonNull;
import android.content.Intent;
import android.os.Bundle;
@@ -69,7 +70,13 @@ public interface RecognitionListener {
/**
* Called when recognition results are ready.
- *
+ *
+ * <p>
+ * Called with the results for the full speech since {@link #onReadyForSpeech(Bundle)}.
+ * To get recognition results in segments rather than for the full session see
+ * {@link RecognizerIntent#EXTRA_SEGMENT_SESSION}.
+ * </p>
+ *
* @param results the recognition results. To retrieve the results in {@code
* ArrayList<String>} format use {@link Bundle#getStringArrayList(String)} with
* {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter. A float array of
@@ -92,6 +99,24 @@ public interface RecognitionListener {
void onPartialResults(Bundle partialResults);
/**
+ * Called for each ready segment of a recognition request. To request segmented speech results
+ * use {@link RecognizerIntent#EXTRA_SEGMENT_SESSION}. The callback might be called
+ * any number of times between {@link #onReadyForSpeech(Bundle)} and
+ * {@link #onEndOfSegmentedSession()}.
+ *
+ * @param segmentResults the returned results. To retrieve the results in
+ * ArrayList&lt;String&gt; format use {@link Bundle#getStringArrayList(String)} with
+ * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter
+ */
+ default void onSegmentResults(@NonNull Bundle segmentResults) {}
+
+ /**
+ * Called at the end of a segmented recognition request. To request segmented speech results
+ * use {@link RecognizerIntent#EXTRA_SEGMENT_SESSION}.
+ */
+ default void onEndOfSegmentedSession() {}
+
+ /**
* Reserved for adding future events.
*
* @param eventType the type of the occurred event
diff --git a/core/java/android/speech/RecognitionService.java b/core/java/android/speech/RecognitionService.java
index 5dbbc045077e..08136761f75c 100644
--- a/core/java/android/speech/RecognitionService.java
+++ b/core/java/android/speech/RecognitionService.java
@@ -427,6 +427,26 @@ public abstract class RecognitionService extends Service {
}
/**
+ * The service should call this method for each ready segment of a long recognition session.
+ *
+ * @param results the recognition results. To retrieve the results in {@code
+ * ArrayList<String>} format use {@link Bundle#getStringArrayList(String)} with
+ * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter
+ */
+ @SuppressLint({"CallbackMethodName", "RethrowRemoteException"})
+ public void segmentResults(@NonNull Bundle results) throws RemoteException {
+ mListener.onSegmentResults(results);
+ }
+
+ /**
+ * The service should call this method to end a segmented session.
+ */
+ @SuppressLint({"CallbackMethodName", "RethrowRemoteException"})
+ public void endOfSegmentedSession() throws RemoteException {
+ mListener.onEndOfSegmentedSession();
+ }
+
+ /**
* Return the Linux uid assigned to the process that sent you the current transaction that
* is being processed. This is obtained from {@link Binder#getCallingUid()}.
*/
diff --git a/core/java/android/speech/RecognizerIntent.java b/core/java/android/speech/RecognizerIntent.java
index 3183f15fe16c..271e3072c4d9 100644
--- a/core/java/android/speech/RecognizerIntent.java
+++ b/core/java/android/speech/RecognizerIntent.java
@@ -426,4 +426,16 @@ public class RecognizerIntent {
*
*/
public static final String EXTRA_PREFER_OFFLINE = "android.speech.extra.PREFER_OFFLINE";
+
+ /**
+ * Optional boolean, when true and supported by the recognizer implementation it will split
+ * the recognition results in segments, returned via
+ * {@link RecognitionListener#onSegmentResults(Bundle)} and terminate the session with
+ * {@link RecognitionListener#onEndOfSegmentedSession()}. There will be no call to
+ * {@link RecognitionListener#onResults(Bundle)}. Callers can use
+ * {@link #EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS} and
+ * {@link #EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS} to tune how long the segments
+ * will be. Defaults to false.
+ */
+ public static final String EXTRA_SEGMENT_SESSION = "android.speech.extra.SEGMENT_SESSION";
}
diff --git a/core/java/android/speech/SpeechRecognizer.java b/core/java/android/speech/SpeechRecognizer.java
index 71c1e882a1f6..502558e355e6 100644
--- a/core/java/android/speech/SpeechRecognizer.java
+++ b/core/java/android/speech/SpeechRecognizer.java
@@ -768,6 +768,8 @@ public class SpeechRecognizer {
private static final int MSG_PARTIAL_RESULTS = 7;
private static final int MSG_RMS_CHANGED = 8;
private static final int MSG_ON_EVENT = 9;
+ private static final int MSG_SEGMENT_RESULTS = 10;
+ private static final int MSG_SEGMENT_END_SESSION = 11;
private final Handler mInternalHandler = new Handler(Looper.getMainLooper()) {
@Override
@@ -803,6 +805,12 @@ public class SpeechRecognizer {
case MSG_ON_EVENT:
mInternalListener.onEvent(msg.arg1, (Bundle) msg.obj);
break;
+ case MSG_SEGMENT_RESULTS:
+ mInternalListener.onSegmentResults((Bundle) msg.obj);
+ break;
+ case MSG_SEGMENT_END_SESSION:
+ mInternalListener.onEndOfSegmentedSession();
+ break;
}
}
};
@@ -839,6 +847,14 @@ public class SpeechRecognizer {
Message.obtain(mInternalHandler, MSG_RMS_CHANGED, rmsdB).sendToTarget();
}
+ public void onSegmentResults(final Bundle bundle) {
+ Message.obtain(mInternalHandler, MSG_SEGMENT_RESULTS, bundle).sendToTarget();
+ }
+
+ public void onEndOfSegmentedSession() {
+ Message.obtain(mInternalHandler, MSG_SEGMENT_END_SESSION).sendToTarget();
+ }
+
public void onEvent(final int eventType, final Bundle params) {
Message.obtain(mInternalHandler, MSG_ON_EVENT, eventType, eventType, params)
.sendToTarget();