summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Andrea Ambu <andreaambu@google.com> 2021-05-13 18:21:30 +0100
committer Andrea Ambu <andreaambu@google.com> 2021-05-18 19:04:46 +0000
commit40ef504bc8abdabc641770b3670f850a42bbaf3b (patch)
tree52343ece91b665147db599a932fdc58c9681ddb3
parent13bd292dd503a97a7e51bb31f4b7831afd400f36 (diff)
speech: Add #isOnDeviceRecognitionAvailable
Bug: 188055926 Test: CtsVoiceRecognitionTestCases Change-Id: I6ba1d083005782beedb28313077af041af2ae975
-rw-r--r--core/api/current.txt3
-rw-r--r--core/java/android/speech/SpeechRecognizer.java41
2 files changed, 42 insertions, 2 deletions
diff --git a/core/api/current.txt b/core/api/current.txt
index bdc2c0ca5c93..f1241df85e41 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -39223,7 +39223,8 @@ package android.speech {
method @MainThread public static android.speech.SpeechRecognizer createSpeechRecognizer(android.content.Context);
method @MainThread public static android.speech.SpeechRecognizer createSpeechRecognizer(android.content.Context, android.content.ComponentName);
method public void destroy();
- method public static boolean isRecognitionAvailable(android.content.Context);
+ method public static boolean isOnDeviceRecognitionAvailable(@NonNull android.content.Context);
+ method public static boolean isRecognitionAvailable(@NonNull android.content.Context);
method @MainThread public void setRecognitionListener(android.speech.RecognitionListener);
method @MainThread public void startListening(android.content.Intent);
method @MainThread public void stopListening();
diff --git a/core/java/android/speech/SpeechRecognizer.java b/core/java/android/speech/SpeechRecognizer.java
index b9920072271f..37a97ca719b1 100644
--- a/core/java/android/speech/SpeechRecognizer.java
+++ b/core/java/android/speech/SpeechRecognizer.java
@@ -40,6 +40,8 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.Slog;
+import com.android.internal.R;
+
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -238,7 +240,7 @@ public class SpeechRecognizer {
* @param context with which {@code SpeechRecognizer} will be created
* @return {@code true} if recognition is available, {@code false} otherwise
*/
- public static boolean isRecognitionAvailable(final Context context) {
+ public static boolean isRecognitionAvailable(@NonNull final Context context) {
// TODO(b/176578753): make sure this works well with system speech recognizers.
final List<ResolveInfo> list = context.getPackageManager().queryIntentServices(
new Intent(RecognitionService.SERVICE_INTERFACE), 0);
@@ -246,6 +248,38 @@ public class SpeechRecognizer {
}
/**
+ * Checks whether an on-device speech recognition service is available on the system. If this
+ * method returns {@code false},
+ * {@link SpeechRecognizer#createOnDeviceSpeechRecognizer(Context)} will
+ * fail.
+ *
+ * @param context with which on-device {@code SpeechRecognizer} will be created
+ * @return {@code true} if on-device recognition is available, {@code false} otherwise
+ */
+ public static boolean isOnDeviceRecognitionAvailable(@NonNull final Context context) {
+ ComponentName componentName =
+ ComponentName.unflattenFromString(
+ context.getString(R.string.config_defaultOnDeviceSpeechRecognitionService));
+ if (componentName == null) {
+ return false;
+ }
+
+ List<ResolveInfo> resolveInfos =
+ context.getPackageManager().queryIntentServices(
+ new Intent(RecognitionService.SERVICE_INTERFACE), 0);
+ if (resolveInfos == null) {
+ return false;
+ }
+
+ for (ResolveInfo ri : resolveInfos) {
+ if (ri.serviceInfo != null && componentName.equals(ri.serviceInfo.getComponentName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
* Factory method to create a new {@code SpeechRecognizer}. Please note that
* {@link #setRecognitionListener(RecognitionListener)} should be called before dispatching any
* command to the created {@code SpeechRecognizer}, otherwise no notifications will be
@@ -315,6 +349,8 @@ public class SpeechRecognizer {
* notifications will be received.
*
* @param context in which to create {@code SpeechRecognizer}
+ * @throws UnsupportedOperationException iff {@link #isOnDeviceRecognitionAvailable(Context)}
+ * is false
* @return a new on-device {@code SpeechRecognizer}.
*/
@NonNull
@@ -324,6 +360,9 @@ public class SpeechRecognizer {
throw new IllegalArgumentException("Context cannot be null");
}
checkIsCalledFromMainThread();
+ if (!isOnDeviceRecognitionAvailable(context)) {
+ throw new UnsupportedOperationException("On-device recognition is not available");
+ }
return new SpeechRecognizer(context, /* onDevice */ true);
}