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

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

Merge "speech: Add api to check request is supported"

parents d6b531e6 d15616a5
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -40053,8 +40053,10 @@ package android.speech {
    ctor public RecognitionService();
    method public final android.os.IBinder onBind(android.content.Intent);
    method protected abstract void onCancel(android.speech.RecognitionService.Callback);
    method public void onCheckRecognitionSupport(@NonNull android.content.Intent, @NonNull android.speech.RecognitionService.SupportCallback);
    method protected abstract void onStartListening(android.content.Intent, android.speech.RecognitionService.Callback);
    method protected abstract void onStopListening(android.speech.RecognitionService.Callback);
    method public void triggerModelDownload(@NonNull android.content.Intent);
    field public static final String SERVICE_INTERFACE = "android.speech.RecognitionService";
    field public static final String SERVICE_META_DATA = "android.speech";
  }
@@ -40072,6 +40074,36 @@ package android.speech {
    method public void rmsChanged(float) throws android.os.RemoteException;
  }
  public static class RecognitionService.SupportCallback {
    method public void onError(int);
    method public void onSupportResult(@NonNull android.speech.RecognitionSupport);
  }
  public final class RecognitionSupport implements android.os.Parcelable {
    method public int describeContents();
    method @NonNull public java.util.List<java.lang.String> getInstalledLanguages();
    method @NonNull public java.util.List<java.lang.String> getPendingLanguages();
    method @NonNull public java.util.List<java.lang.String> getSupportedLanguages();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.speech.RecognitionSupport> CREATOR;
  }
  public static final class RecognitionSupport.Builder {
    ctor public RecognitionSupport.Builder();
    method @NonNull public android.speech.RecognitionSupport.Builder addInstalledLanguages(@NonNull String);
    method @NonNull public android.speech.RecognitionSupport.Builder addPendingLanguages(@NonNull String);
    method @NonNull public android.speech.RecognitionSupport.Builder addSupportedLanguages(@NonNull String);
    method @NonNull public android.speech.RecognitionSupport build();
    method @NonNull public android.speech.RecognitionSupport.Builder setInstalledLanguages(@NonNull java.util.List<java.lang.String>);
    method @NonNull public android.speech.RecognitionSupport.Builder setPendingLanguages(@NonNull java.util.List<java.lang.String>);
    method @NonNull public android.speech.RecognitionSupport.Builder setSupportedLanguages(@NonNull java.util.List<java.lang.String>);
  }
  public interface RecognitionSupportCallback {
    method public void onError(int);
    method public void onSupportResult(@NonNull android.speech.RecognitionSupport);
  }
  public class RecognizerIntent {
    method public static final android.content.Intent getVoiceDetailsIntent(android.content.Context);
    field public static final String ACTION_GET_LANGUAGE_DETAILS = "android.speech.action.GET_LANGUAGE_DETAILS";
@@ -40121,6 +40153,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 @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);
@@ -40130,8 +40163,10 @@ package android.speech {
    method @MainThread public void setRecognitionListener(android.speech.RecognitionListener);
    method @MainThread public void startListening(android.content.Intent);
    method @MainThread public void stopListening();
    method public void triggerModelDownload(@NonNull android.content.Intent);
    field public static final String CONFIDENCE_SCORES = "confidence_scores";
    field public static final int ERROR_AUDIO = 3; // 0x3
    field public static final int ERROR_CANNOT_CHECK_SUPPORT = 14; // 0xe
    field public static final int ERROR_CLIENT = 5; // 0x5
    field public static final int ERROR_INSUFFICIENT_PERMISSIONS = 9; // 0x9
    field public static final int ERROR_LANGUAGE_NOT_SUPPORTED = 12; // 0xc
+15 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.os.Bundle;
import android.content.AttributionSource;
import android.content.Intent;
import android.speech.IRecognitionListener;
import android.speech.IRecognitionSupportCallback;

/**
* A Service interface to speech recognition. Call startListening when
@@ -60,4 +61,18 @@ oneway interface IRecognitionService {
     * @param listener to receive callbacks, note that this must be non-null
     */
    void cancel(in IRecognitionListener listener, boolean isShutdown);

    /**
     * Checks whether this RecognitionService could {@link #startListening} successfully on the
     * given recognizerIntent. For more information see {@link #startListening} and
     * {@link RecognizerIntent}.
     */
    void checkRecognitionSupport(in Intent recognizerIntent, in IRecognitionSupportCallback listener);

    /**
     * Requests RecognitionService to download the support for the given recognizerIntent. For more
     * information see {@link #checkRecognitionSupport},  {@link #startListening} and
     * {@link RecognizerIntent}.
     */
    void triggerModelDownload(in Intent recognizerIntent);
}
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.speech;

import android.os.Bundle;
import android.speech.RecognitionSupport;

/**
 *  Callback for speech recognition support checks, used with RecognitionService.
 *  This provides the {@link RecognitionSupport} for a given recognition request, callers can use
 *  it to check whether RecognitionService can fulfill a given recognition request.
 *  {@hide}
 */
oneway interface IRecognitionSupportCallback {
    void onSupportResult(in RecognitionSupport recognitionSupport);

    /**
     * A network or recognition error occurred.
     *
     * @param error code is defined in {@link SpeechRecognizer}
     */
    void onError(in int error);
}
+111 −1
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;

import com.android.internal.util.function.pooled.PooledLambda;

@@ -90,6 +91,10 @@ public abstract class RecognitionService extends Service {

    private static final int MSG_RESET = 4;

    private static final int MSG_CHECK_RECOGNITION_SUPPORT = 5;

    private static final int MSG_TRIGGER_MODEL_DOWNLOAD = 6;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
@@ -107,6 +112,15 @@ public abstract class RecognitionService extends Service {
                case MSG_RESET:
                    dispatchClearCallback();
                    break;
                case MSG_CHECK_RECOGNITION_SUPPORT:
                    Pair<Intent, IRecognitionSupportCallback> intentAndListener =
                            (Pair<Intent, IRecognitionSupportCallback>) msg.obj;
                    dispatchCheckRecognitionSupport(
                            intentAndListener.first, intentAndListener.second);
                    break;
                case MSG_TRIGGER_MODEL_DOWNLOAD:
                    dispatchTriggerModelDownload((Intent) msg.obj);
                    break;
            }
        }
    };
@@ -179,6 +193,15 @@ public abstract class RecognitionService extends Service {
        mStartedDataDelivery = false;
    }

    private void dispatchCheckRecognitionSupport(
            Intent intent, IRecognitionSupportCallback callback) {
        RecognitionService.this.onCheckRecognitionSupport(intent, new SupportCallback(callback));
    }

    private void dispatchTriggerModelDownload(Intent intent) {
        RecognitionService.this.triggerModelDownload(intent);
    }

    private class StartListeningArgs {
        public final Intent mIntent;

@@ -238,6 +261,34 @@ public abstract class RecognitionService extends Service {
     */
    protected abstract void onStopListening(Callback listener);

    /**
     * Queries the service on whether it would support a {@link #onStartListening(Intent, Callback)}
     * for the same {@code recognizerIntent}.
     *
     * <p>The service will notify the caller about the level of support or error via
     * {@link SupportCallback}.
     *
     * <p>If the service does not offer the support check it will notify the caller with
     * {@link SpeechRecognizer#ERROR_CANNOT_CHECK_SUPPORT}.
     */
    public void onCheckRecognitionSupport(
            @NonNull Intent recognizerIntent,
            @NonNull SupportCallback supportCallback) {
        if (DBG) {
            Log.i(TAG, String.format("#onSupports [%s]", recognizerIntent));
        }
        supportCallback.onError(SpeechRecognizer.ERROR_CANNOT_CHECK_SUPPORT);
    }

    /**
     * Requests the download of the recognizer support for {@code recognizerIntent}.
     */
    public void triggerModelDownload(@NonNull Intent recognizerIntent) {
        if (DBG) {
            Log.i(TAG, String.format("#downloadModel [%s]", recognizerIntent));
        }
    }

    @Override
    @SuppressLint("MissingNullability")
    public Context createContext(@NonNull ContextParams contextParams) {
@@ -410,6 +461,44 @@ public abstract class RecognitionService extends Service {
        }
    }

    /**
     * This class receives callbacks from the speech recognition service and forwards them to the
     * user. An instance of this class is passed to the
     * {@link RecognitionService#onCheckRecognitionSupport(Intent, SupportCallback)} method. Recognizers may call
     * these methods on any thread.
     */
    public static class SupportCallback {

        private final IRecognitionSupportCallback mCallback;

        private SupportCallback(IRecognitionSupportCallback callback) {
            this.mCallback = callback;
        }

        /** The service should call this method to notify the caller about the level of support. */
        public void onSupportResult(@NonNull RecognitionSupport recognitionSupport) {
            try {
                mCallback.onSupportResult(recognitionSupport);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }

        /**
         * The service should call this method when an error occurred and can't satisfy the support
         * request.
         *
         * @param errorCode code is defined in {@link SpeechRecognizer}
         */
        public void onError(@SpeechRecognizer.RecognitionError int errorCode) {
            try {
                mCallback.onError(errorCode);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
    }

/** Binder of the recognition service */
    private static final class RecognitionServiceBinder extends IRecognitionService.Stub {
        private final WeakReference<RecognitionService> mServiceRef;
@@ -452,6 +541,27 @@ public abstract class RecognitionService extends Service {
            }
        }

        @Override
        public void checkRecognitionSupport(
                Intent recognizerIntent, IRecognitionSupportCallback callback) {
            final RecognitionService service = mServiceRef.get();
            if (service != null) {
                service.mHandler.sendMessage(
                        Message.obtain(service.mHandler, MSG_CHECK_RECOGNITION_SUPPORT,
                                Pair.create(recognizerIntent, callback)));
            }
        }

        @Override
        public void triggerModelDownload(Intent recognizerIntent) {
            final RecognitionService service = mServiceRef.get();
            if (service != null) {
                service.mHandler.sendMessage(
                        Message.obtain(
                                service.mHandler, MSG_TRIGGER_MODEL_DOWNLOAD, recognizerIntent));
            }
        }

        public void clearReference() {
            mServiceRef.clear();
        }
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.speech;

parcelable RecognitionSupport;
Loading