diff --git a/core/api/current.txt b/core/api/current.txt index fb0a95a104128..410825cd76340 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -41450,9 +41450,11 @@ package android.speech { 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 public void onCheckRecognitionSupport(@NonNull android.content.Intent, @NonNull android.content.AttributionSource, @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 onTriggerModelDownload(@NonNull android.content.Intent); + method public void onTriggerModelDownload(@NonNull android.content.Intent, @NonNull android.content.AttributionSource); field public static final String SERVICE_INTERFACE = "android.speech.RecognitionService"; field public static final String SERVICE_META_DATA = "android.speech"; } diff --git a/core/java/android/speech/IRecognitionService.aidl b/core/java/android/speech/IRecognitionService.aidl index ad3ad7ad1e053..cc64c45c7606b 100644 --- a/core/java/android/speech/IRecognitionService.aidl +++ b/core/java/android/speech/IRecognitionService.aidl @@ -67,12 +67,15 @@ oneway interface IRecognitionService { * given recognizerIntent. For more information see {@link #startListening} and * {@link RecognizerIntent}. */ - void checkRecognitionSupport(in Intent recognizerIntent, in IRecognitionSupportCallback listener); + void checkRecognitionSupport( + in Intent recognizerIntent, + in AttributionSource attributionSource, + 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); + void triggerModelDownload(in Intent recognizerIntent, in AttributionSource attributionSource); } diff --git a/core/java/android/speech/RecognitionService.java b/core/java/android/speech/RecognitionService.java index a5dbdd7ce89a9..0b0b3b56acd35 100644 --- a/core/java/android/speech/RecognitionService.java +++ b/core/java/android/speech/RecognitionService.java @@ -110,13 +110,14 @@ public abstract class RecognitionService extends Service { dispatchClearCallback((IRecognitionListener) msg.obj); break; case MSG_CHECK_RECOGNITION_SUPPORT: - Pair intentAndListener = - (Pair) msg.obj; + CheckRecognitionSupportArgs checkArgs = (CheckRecognitionSupportArgs) msg.obj; dispatchCheckRecognitionSupport( - intentAndListener.first, intentAndListener.second); + checkArgs.mIntent, checkArgs.callback, checkArgs.mAttributionSource); break; case MSG_TRIGGER_MODEL_DOWNLOAD: - dispatchTriggerModelDownload((Intent) msg.obj); + Pair params = + (Pair) msg.obj; + dispatchTriggerModelDownload(params.first, params.second); break; } } @@ -211,12 +212,18 @@ public abstract class RecognitionService extends Service { } private void dispatchCheckRecognitionSupport( - Intent intent, IRecognitionSupportCallback callback) { - RecognitionService.this.onCheckRecognitionSupport(intent, new SupportCallback(callback)); + Intent intent, IRecognitionSupportCallback callback, + AttributionSource attributionSource) { + RecognitionService.this.onCheckRecognitionSupport( + intent, + attributionSource, + new SupportCallback(callback)); } - private void dispatchTriggerModelDownload(Intent intent) { - RecognitionService.this.onTriggerModelDownload(intent); + private void dispatchTriggerModelDownload( + Intent intent, + AttributionSource attributionSource) { + RecognitionService.this.onTriggerModelDownload(intent, attributionSource); } private static class StartListeningArgs { @@ -233,6 +240,21 @@ public abstract class RecognitionService extends Service { } } + private static class CheckRecognitionSupportArgs { + public final Intent mIntent; + public final IRecognitionSupportCallback callback; + public final AttributionSource mAttributionSource; + + private CheckRecognitionSupportArgs( + Intent intent, + IRecognitionSupportCallback callback, + AttributionSource attributionSource) { + this.mIntent = intent; + this.callback = callback; + this.mAttributionSource = attributionSource; + } + } + /** * Notifies the service that it should start listening for speech. * @@ -297,6 +319,26 @@ public abstract class RecognitionService extends Service { supportCallback.onError(SpeechRecognizer.ERROR_CANNOT_CHECK_SUPPORT); } + /** + * Queries the service on whether it would support a {@link #onStartListening(Intent, Callback)} + * for the same {@code recognizerIntent}. + * + *

The service will notify the caller about the level of support or error via + * {@link SupportCallback}. + * + *

If the service does not offer the support check it will notify the caller with + * {@link SpeechRecognizer#ERROR_CANNOT_CHECK_SUPPORT}. + * + *

Provides the calling AttributionSource to the service implementation so that permissions + * and bandwidth could be correctly blamed.

+ */ + public void onCheckRecognitionSupport( + @NonNull Intent recognizerIntent, + @NonNull AttributionSource attributionSource, + @NonNull SupportCallback supportCallback) { + onCheckRecognitionSupport(recognizerIntent, supportCallback); + } + /** * Requests the download of the recognizer support for {@code recognizerIntent}. */ @@ -306,6 +348,18 @@ public abstract class RecognitionService extends Service { } } + /** + * Requests the download of the recognizer support for {@code recognizerIntent}. + * + *

Provides the calling AttributionSource to the service implementation so that permissions + * and bandwidth could be correctly blamed.

+ */ + public void onTriggerModelDownload( + @NonNull Intent recognizerIntent, + @NonNull AttributionSource attributionSource) { + onTriggerModelDownload(recognizerIntent); + } + @Override @SuppressLint("MissingNullability") public Context createContext(@NonNull ContextParams contextParams) { @@ -524,7 +578,8 @@ public abstract class RecognitionService extends Service { public static class SupportCallback { private final IRecognitionSupportCallback mCallback; - private SupportCallback(IRecognitionSupportCallback callback) { + private SupportCallback( + IRecognitionSupportCallback callback) { this.mCallback = callback; } @@ -596,22 +651,27 @@ public abstract class RecognitionService extends Service { @Override public void checkRecognitionSupport( - Intent recognizerIntent, IRecognitionSupportCallback callback) { + Intent recognizerIntent, + @NonNull AttributionSource attributionSource, + 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))); + new CheckRecognitionSupportArgs( + recognizerIntent, callback, attributionSource))); } } @Override - public void triggerModelDownload(Intent recognizerIntent) { + public void triggerModelDownload( + Intent recognizerIntent, @NonNull AttributionSource attributionSource) { final RecognitionService service = mServiceRef.get(); if (service != null) { service.mHandler.sendMessage( Message.obtain( - service.mHandler, MSG_TRIGGER_MODEL_DOWNLOAD, recognizerIntent)); + service.mHandler, MSG_TRIGGER_MODEL_DOWNLOAD, + Pair.create(recognizerIntent, attributionSource))); } } diff --git a/core/java/android/speech/SpeechRecognizer.java b/core/java/android/speech/SpeechRecognizer.java index 33c5b5a27fab7..9c46e55d19968 100644 --- a/core/java/android/speech/SpeechRecognizer.java +++ b/core/java/android/speech/SpeechRecognizer.java @@ -663,6 +663,7 @@ public class SpeechRecognizer { try { mService.checkRecognitionSupport( recognizerIntent, + mContext.getAttributionSource(), new InternalSupportCallback(callbackExecutor, recognitionSupportCallback)); if (DBG) Log.d(TAG, "service support command succeeded"); } catch (final RemoteException e) { @@ -676,7 +677,7 @@ public class SpeechRecognizer { return; } try { - mService.triggerModelDownload(recognizerIntent); + mService.triggerModelDownload(recognizerIntent, mContext.getAttributionSource()); } catch (final RemoteException e) { Log.e(TAG, "downloadModel() failed", e); mListener.onError(ERROR_CLIENT); diff --git a/services/core/java/com/android/server/speech/RemoteSpeechRecognitionService.java b/services/core/java/com/android/server/speech/RemoteSpeechRecognitionService.java index e148a48f80d05..4839c96a1b056 100644 --- a/services/core/java/com/android/server/speech/RemoteSpeechRecognitionService.java +++ b/services/core/java/com/android/server/speech/RemoteSpeechRecognitionService.java @@ -233,7 +233,9 @@ final class RemoteSpeechRecognitionService extends ServiceConnector.Impl service.checkRecognitionSupport(recognizerIntent, callback)); + run(service -> + service.checkRecognitionSupport(recognizerIntent, attributionSource, callback)); } - void triggerModelDownload(Intent recognizerIntent) { + void triggerModelDownload(Intent recognizerIntent, AttributionSource attributionSource) { if (!mConnected) { Slog.e(TAG, "#downloadModel failed due to connection."); return; } - run(service -> service.triggerModelDownload(recognizerIntent)); + run(service -> service.triggerModelDownload(recognizerIntent, attributionSource)); } void shutdown(IBinder clientToken) { diff --git a/services/core/java/com/android/server/speech/SpeechRecognitionManagerServiceImpl.java b/services/core/java/com/android/server/speech/SpeechRecognitionManagerServiceImpl.java index e245c08329204..6aa600ad63cdc 100644 --- a/services/core/java/com/android/server/speech/SpeechRecognitionManagerServiceImpl.java +++ b/services/core/java/com/android/server/speech/SpeechRecognitionManagerServiceImpl.java @@ -183,13 +183,17 @@ final class SpeechRecognitionManagerServiceImpl extends @Override public void checkRecognitionSupport( Intent recognizerIntent, + AttributionSource attributionSource, IRecognitionSupportCallback callback) { - service.checkRecognitionSupport(recognizerIntent, callback); + service.checkRecognitionSupport( + recognizerIntent, attributionSource, callback); } @Override - public void triggerModelDownload(Intent recognizerIntent) { - service.triggerModelDownload(recognizerIntent); + public void triggerModelDownload( + Intent recognizerIntent, + AttributionSource attributionSource) { + service.triggerModelDownload(recognizerIntent, attributionSource); } }); } catch (RemoteException e) {