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

Commit c5df4e14 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "speech: Run support check in Executor"

parents 5128d9d8 604cfdf8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -39604,7 +39604,7 @@ package android.speech {
  public class SpeechRecognizer {
    method @MainThread public void cancel();
    method public void checkRecognitionSupport(@NonNull android.content.Intent, @NonNull android.speech.RecognitionSupportCallback);
    method public void checkRecognitionSupport(@NonNull android.content.Intent, @NonNull java.util.concurrent.Executor, @NonNull android.speech.RecognitionSupportCallback);
    method @MainThread @NonNull public static android.speech.SpeechRecognizer createOnDeviceSpeechRecognizer(@NonNull android.content.Context);
    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);
+31 −31
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.speech;

import android.Manifest;
import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.MainThread;
import android.annotation.NonNull;
@@ -38,7 +39,6 @@ import android.os.ServiceManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;

import com.android.internal.R;
@@ -49,6 +49,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;

/**
@@ -206,10 +207,9 @@ public class SpeechRecognizer {
                    handleSetTemporaryComponent((ComponentName) msg.obj);
                    break;
                case MSG_CHECK_RECOGNITION_SUPPORT:
                    Pair<Intent, RecognitionSupportCallback> intentAndListener =
                            (Pair<Intent, RecognitionSupportCallback>) msg.obj;
                    CheckRecognitionSupportArgs args = (CheckRecognitionSupportArgs) msg.obj;
                    handleCheckRecognitionSupport(
                            intentAndListener.first, intentAndListener.second);
                            args.mIntent, args.mCallbackExecutor, args.mCallback);
                    break;
                case MSG_TRIGGER_MODEL_DOWNLOAD:
                    handleTriggerModelDownload((Intent) msg.obj);
@@ -492,6 +492,7 @@ public class SpeechRecognizer {
     */
    public void checkRecognitionSupport(
            @NonNull Intent recognizerIntent,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull RecognitionSupportCallback supportListener) {
        Objects.requireNonNull(recognizerIntent, "intent must not be null");
        Objects.requireNonNull(supportListener, "listener must not be null");
@@ -508,7 +509,7 @@ public class SpeechRecognizer {
            connectToSystemService();
        }
        putMessage(Message.obtain(mHandler, MSG_CHECK_RECOGNITION_SUPPORT,
                Pair.create(recognizerIntent, supportListener)));
                new CheckRecognitionSupportArgs(recognizerIntent, executor, supportListener)));
    }

    /**
@@ -625,18 +626,20 @@ public class SpeechRecognizer {
    }

    private void handleCheckRecognitionSupport(
            Intent recognizerIntent, RecognitionSupportCallback recognitionSupportCallback) {
            Intent recognizerIntent,
            Executor callbackExecutor,
            RecognitionSupportCallback recognitionSupportCallback) {
        if (!maybeInitializeManagerService()) {
            return;
        }
        try {
            mService.checkRecognitionSupport(
                    recognizerIntent,
                    new InternalSupportCallback(recognitionSupportCallback));
                    new InternalSupportCallback(callbackExecutor, recognitionSupportCallback));
            if (DBG) Log.d(TAG, "service support command succeeded");
        } catch (final RemoteException e) {
            Log.e(TAG, "checkRecognitionSupport() failed", e);
            mListener.onError(ERROR_CLIENT);
            callbackExecutor.execute(() -> recognitionSupportCallback.onError(ERROR_CLIENT));
        }
    }

@@ -780,6 +783,21 @@ public class SpeechRecognizer {
        return ComponentName.unflattenFromString(serviceComponent);
    }

    private static class CheckRecognitionSupportArgs {
        final Intent mIntent;
        final Executor mCallbackExecutor;
        final RecognitionSupportCallback mCallback;

        private CheckRecognitionSupportArgs(
                Intent intent,
                Executor callbackExecutor,
                RecognitionSupportCallback callback) {
            mIntent = intent;
            mCallbackExecutor = callbackExecutor;
            mCallback = callback;
        }
    }

    /**
     * Internal wrapper of IRecognitionListener which will propagate the results to
     * RecognitionListener
@@ -890,40 +908,22 @@ public class SpeechRecognizer {
    }

    private static class InternalSupportCallback extends IRecognitionSupportCallback.Stub {
        private final Executor mExecutor;
        private final RecognitionSupportCallback mCallback;

        private static final int MSG_SUPPORT_RESULT = 1;
        private static final int MSG_ERROR = 2;

        private final Handler mInternalHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                if (mCallback == null) {
                    return;
                }
                switch (msg.what) {
                    case MSG_SUPPORT_RESULT:
                        mCallback.onSupportResult((RecognitionSupport) msg.obj);
                        break;
                    case MSG_ERROR:
                        mCallback.onError((Integer) msg.obj);
                        break;
                }
            }
        };

        private InternalSupportCallback(RecognitionSupportCallback callback) {
        private InternalSupportCallback(Executor executor, RecognitionSupportCallback callback) {
            this.mExecutor = executor;
            this.mCallback = callback;
        }

        @Override
        public void onSupportResult(RecognitionSupport recognitionSupport) throws RemoteException {
            Message.obtain(mInternalHandler, MSG_SUPPORT_RESULT, recognitionSupport).sendToTarget();
            mExecutor.execute(() -> mCallback.onSupportResult(recognitionSupport));
        }

        @Override
        public void onError(int errorCode) throws RemoteException {
            Message.obtain(mInternalHandler, MSG_ERROR, errorCode).sendToTarget();
            mExecutor.execute(() -> mCallback.onError(errorCode));
        }
    }
}