Merge "Fix the Translator references are gone when receiving response." into sc-dev

This commit is contained in:
Joanne Chung
2021-08-05 13:42:54 +00:00
committed by Android (Google) Code Review

View File

@@ -18,6 +18,7 @@ package android.view.translation;
import static android.view.translation.TranslationManager.STATUS_SYNC_CALL_FAIL; import static android.view.translation.TranslationManager.STATUS_SYNC_CALL_FAIL;
import static android.view.translation.TranslationManager.SYNC_CALLS_TIMEOUT_MS; import static android.view.translation.TranslationManager.SYNC_CALLS_TIMEOUT_MS;
import static android.view.translation.UiTranslationController.DEBUG;
import android.annotation.CallbackExecutor; import android.annotation.CallbackExecutor;
import android.annotation.NonNull; import android.annotation.NonNull;
@@ -38,7 +39,6 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.os.IResultReceiver; import com.android.internal.os.IResultReceiver;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@@ -395,27 +395,26 @@ public class Translator {
private static class TranslationResponseCallbackImpl extends ITranslationCallback.Stub { private static class TranslationResponseCallbackImpl extends ITranslationCallback.Stub {
private final WeakReference<Consumer<TranslationResponse>> mCallback; private final Consumer<TranslationResponse> mCallback;
private final WeakReference<Executor> mExecutor; private final Executor mExecutor;
TranslationResponseCallbackImpl(Consumer<TranslationResponse> callback, Executor executor) { TranslationResponseCallbackImpl(Consumer<TranslationResponse> callback, Executor executor) {
mCallback = new WeakReference<>(callback); mCallback = callback;
mExecutor = new WeakReference<>(executor); mExecutor = executor;
} }
@Override @Override
public void onTranslationResponse(TranslationResponse response) throws RemoteException { public void onTranslationResponse(TranslationResponse response) throws RemoteException {
final Consumer<TranslationResponse> callback = mCallback.get(); if (DEBUG) {
Log.i(TAG, "onTranslationResponse called.");
}
final Runnable runnable = final Runnable runnable =
() -> callback.accept(response); () -> mCallback.accept(response);
if (callback != null) { final long token = Binder.clearCallingIdentity();
final Executor executor = mExecutor.get(); try {
final long token = Binder.clearCallingIdentity(); mExecutor.execute(runnable);
try { } finally {
executor.execute(runnable); restoreCallingIdentity(token);
} finally {
restoreCallingIdentity(token);
}
} }
} }
} }