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

Commit cd145f1b authored by Andrea Ambu's avatar Andrea Ambu Committed by Android (Google) Code Review
Browse files

Merge "speech: Add #isOnDeviceRecognitionAvailable" into sc-dev

parents 17af0292 40ef504b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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();
+40 −1
Original line number Diff line number Diff line
@@ -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,13 +240,45 @@ 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);
        return list != null && list.size() != 0;
    }

    /**
     * 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
@@ -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);
    }