speech: Wire calling in uid on support callback

This is to keep support call parallel to startListening

Bug: 268475583
Test: cts
Change-Id: I07882b69296acdf19f4e6a49a40c0bed38235934
This commit is contained in:
Andrea Ambu
2022-12-01 14:36:04 +00:00
parent 26ef4c4c63
commit 3833e44fb9
6 changed files with 95 additions and 22 deletions

View File

@@ -40804,9 +40804,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";
}

View File

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

View File

@@ -110,13 +110,14 @@ public abstract class RecognitionService extends Service {
dispatchClearCallback((IRecognitionListener) msg.obj);
break;
case MSG_CHECK_RECOGNITION_SUPPORT:
Pair<Intent, IRecognitionSupportCallback> intentAndListener =
(Pair<Intent, IRecognitionSupportCallback>) 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<Intent, AttributionSource> params =
(Pair<Intent, AttributionSource>) 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}.
*
* <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}.
*
* <p>Provides the calling AttributionSource to the service implementation so that permissions
* and bandwidth could be correctly blamed.</p>
*/
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}.
*
* <p>Provides the calling AttributionSource to the service implementation so that permissions
* and bandwidth could be correctly blamed.</p>
*/
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)));
}
}

View File

@@ -652,6 +652,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) {
@@ -665,7 +666,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);

View File

@@ -234,7 +234,9 @@ final class RemoteSpeechRecognitionService extends ServiceConnector.Impl<IRecogn
void checkRecognitionSupport(
Intent recognizerIntent,
AttributionSource attributionSource,
IRecognitionSupportCallback callback) {
if (!mConnected) {
try {
callback.onError(SpeechRecognizer.ERROR_SERVER_DISCONNECTED);
@@ -244,15 +246,16 @@ final class RemoteSpeechRecognitionService extends ServiceConnector.Impl<IRecogn
}
return;
}
run(service -> 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) {

View File

@@ -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) {