From 64f6b7605138711af789281aa14b77657415c29b Mon Sep 17 00:00:00 2001 From: Joanne Chung Date: Wed, 22 Sep 2021 16:36:47 +0800 Subject: [PATCH] Fix showing translated text if user switch back to original lauguage quickly. Fixes the bug if the translation service spending more time to deal with the translation but user switches back to original lauguage quickly, we will show translated text. When the response back, if the translation state is finished, we drop the result. If the state is pause, we update the response but we do not call onShowTranslation to translated text. Bug: 200251777 Test: atest CtsTranslationTestCases Test: manual. Try to send request late and switch back original language, it keeps original. Then it works fine when switch between the source and target languages. Change-Id: I780456f091884c61e5801ead3ff021531fee87b4 --- .../view/translation/UiTranslationController.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/java/android/view/translation/UiTranslationController.java b/core/java/android/view/translation/UiTranslationController.java index aa73ed7c89081..9d1bf171128eb 100644 --- a/core/java/android/view/translation/UiTranslationController.java +++ b/core/java/android/view/translation/UiTranslationController.java @@ -369,6 +369,10 @@ public class UiTranslationController { Log.v(TAG, "onVirtualViewTranslationCompleted: received response for " + "AutofillId " + autofillId); } + view.onVirtualViewTranslationResponses(virtualChildResponse); + if (mCurrentState == STATE_UI_TRANSLATION_PAUSED) { + return; + } mActivity.runOnUiThread(() -> { if (view.getViewTranslationCallback() == null) { if (DEBUG) { @@ -377,7 +381,6 @@ public class UiTranslationController { } return; } - view.onVirtualViewTranslationResponses(virtualChildResponse); if (view.getViewTranslationCallback() != null) { view.getViewTranslationCallback().onShowTranslation(view); } @@ -425,6 +428,8 @@ public class UiTranslationController { + " may be gone."); continue; } + int currentState; + currentState = mCurrentState; mActivity.runOnUiThread(() -> { ViewTranslationCallback callback = view.getViewTranslationCallback(); if (view.getViewTranslationResponse() != null @@ -458,6 +463,9 @@ public class UiTranslationController { callback.enableContentPadding(); } view.onViewTranslationResponse(response); + if (currentState == STATE_UI_TRANSLATION_PAUSED) { + return; + } callback.onShowTranslation(view); }); }