diff options
| -rw-r--r-- | core/api/current.txt | 3 | ||||
| -rw-r--r-- | core/java/android/speech/SpeechRecognizer.java | 41 |
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); } |