speech: Run support check in Executor

Bug: 216475054
Test: atest
Change-Id: I9f1f2f6657b4c268546e68c1c2d23b6ad06b9c46
This commit is contained in:
Andrea Ambu
2022-02-11 16:00:43 +00:00
parent 529ee8aa30
commit 604cfdf873
2 changed files with 32 additions and 32 deletions

View File

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

View File

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