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

Commit d493f451 authored by Joanne Chung's avatar Joanne Chung Committed by Automerger Merge Worker
Browse files

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

Merge "Fix the Translator references are gone when receiving response." into sc-dev am: 9d17b85c am: 48e583b0

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15444547

Change-Id: I886f22dc9ca1e4d99b99fed272ff3c1689c4e639
parents 6c0789a5 48e583b0
Loading
Loading
Loading
Loading
+14 −15
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.view.translation;

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.UiTranslationController.DEBUG;

import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
@@ -38,7 +39,6 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.os.IResultReceiver;

import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
@@ -395,28 +395,27 @@ public class Translator {

    private static class TranslationResponseCallbackImpl extends ITranslationCallback.Stub {

        private final WeakReference<Consumer<TranslationResponse>> mCallback;
        private final WeakReference<Executor> mExecutor;
        private final Consumer<TranslationResponse> mCallback;
        private final Executor mExecutor;

        TranslationResponseCallbackImpl(Consumer<TranslationResponse> callback, Executor executor) {
            mCallback = new WeakReference<>(callback);
            mExecutor = new WeakReference<>(executor);
            mCallback = callback;
            mExecutor = executor;
        }

        @Override
        public void onTranslationResponse(TranslationResponse response) throws RemoteException {
            final Consumer<TranslationResponse> callback = mCallback.get();
            if (DEBUG) {
                Log.i(TAG, "onTranslationResponse called.");
            }
            final Runnable runnable =
                    () -> callback.accept(response);
            if (callback != null) {
                final Executor executor = mExecutor.get();
                    () -> mCallback.accept(response);
            final long token = Binder.clearCallingIdentity();
            try {
                    executor.execute(runnable);
                mExecutor.execute(runnable);
            } finally {
                restoreCallingIdentity(token);
            }
        }
    }
}
}