summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorJoanne Chung <joannechung@google.com>2021-07-30 21:19:14 +0800
committerJoanne Chung <joannechung@google.com>2021-08-05 07:07:18 +0000
commitfd19460694f050487f4f4400409cb9cd98fdfddf (patch)
tree943e918f1d49ef59ed29d8511980e3c09ff23615 /core/java
parent5528d08a4f77aeebdebacc55b6c3a2eec685d323 (diff)
Fix the Translator references are gone when receiving response.
There are some issues TranslationService already sent the response but the View onResponse doesn't be called. The reference seems to be gone. If we don't fix the problem, we will missing the ui translation even the service already give us the response. To fix the problem, we change it to strong reference. The change is low risk, the only risk is if TranslationService doesn not release the reference to the callback properly causing a memory leak for apps but this can recovered by app update. Bug: 194973014 Test: atest CtsTranslationTestCases Test: The translation for chat apps work fine. The memory is decreased after receiving the responses for a while. Change-Id: Ib3956b2250c54a29acf9f7d51f51e8191fe8aba2
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/view/translation/Translator.java29
1 files changed, 14 insertions, 15 deletions
diff --git a/core/java/android/view/translation/Translator.java b/core/java/android/view/translation/Translator.java
index edd0d16a5ccb..606f39d6e25e 100644
--- a/core/java/android/view/translation/Translator.java
+++ b/core/java/android/view/translation/Translator.java
@@ -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,27 +395,26 @@ 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();
- final long token = Binder.clearCallingIdentity();
- try {
- executor.execute(runnable);
- } finally {
- restoreCallingIdentity(token);
- }
+ () -> mCallback.accept(response);
+ final long token = Binder.clearCallingIdentity();
+ try {
+ mExecutor.execute(runnable);
+ } finally {
+ restoreCallingIdentity(token);
}
}
}