From 5667540a7d373ed7d0481e976a2738b10214e898 Mon Sep 17 00:00:00 2001 From: Joanne Chung Date: Thu, 7 Oct 2021 13:10:27 +0800 Subject: [PATCH] Fix not show translation if call startTranslation after pauseTranslation It is possible the developer calls pauseTranslation() to show the original text but it calls startTranslation() to show translated text. Ideally the developer should call resumeTranslation but it also make sense to call startTranslation() to show translated text. When receiving translation response, we avoid showing transaltion if the view already has the response and it's the same. But it is good to also check if the view is showing translated text or not. If the view is not translated text, it is possible developer calls startTranslation() instead if calling pauseTranslation() to show translated again, the fixing can resolve this case. The issue case can be fixed by this change. But there is a deeper problem about it's useless the caller call finishTranslation(). This is planned to be fixed in next release. Bug: 201238016 Test: manual. The issue case is fixed. Test: manual. Test some chat apps, it still works fine. Test: atest CtsTranslationTestCases Change-Id: I699d0fa1d60ac96db094adcc6e17f4203df03214 --- .../view/translation/UiTranslationController.java | 14 +++++++++----- .../widget/TextViewTranslationCallback.java | 8 +++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/core/java/android/view/translation/UiTranslationController.java b/core/java/android/view/translation/UiTranslationController.java index f1c5a080a77b3..d078c2cfbfd13 100644 --- a/core/java/android/view/translation/UiTranslationController.java +++ b/core/java/android/view/translation/UiTranslationController.java @@ -431,15 +431,19 @@ public class UiTranslationController { continue; } mActivity.runOnUiThread(() -> { + ViewTranslationCallback callback = view.getViewTranslationCallback(); if (view.getViewTranslationResponse() != null && view.getViewTranslationResponse().equals(response)) { - if (DEBUG) { - Log.d(TAG, "Duplicate ViewTranslationResponse for " + autofillId - + ". Ignoring."); + if (callback instanceof TextViewTranslationCallback) { + if (((TextViewTranslationCallback) callback).isShowingTranslation()) { + if (DEBUG) { + Log.d(TAG, "Duplicate ViewTranslationResponse for " + autofillId + + ". Ignoring."); + } + return; + } } - return; } - ViewTranslationCallback callback = view.getViewTranslationCallback(); if (callback == null) { if (view instanceof TextView) { // developer doesn't provide their override, we set the default TextView diff --git a/core/java/android/widget/TextViewTranslationCallback.java b/core/java/android/widget/TextViewTranslationCallback.java index 152405bf4d378..4a78f3ee6faca 100644 --- a/core/java/android/widget/TextViewTranslationCallback.java +++ b/core/java/android/widget/TextViewTranslationCallback.java @@ -64,6 +64,12 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { */ @Override public boolean onShowTranslation(@NonNull View view) { + if (mIsShowingTranslation) { + if (DEBUG) { + Log.d(TAG, view + " is already showing translated text."); + } + return false; + } ViewTranslationResponse response = view.getViewTranslationResponse(); if (response == null) { Log.e(TAG, "onShowTranslation() shouldn't be called before " @@ -152,7 +158,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { return true; } - boolean isShowingTranslation() { + public boolean isShowingTranslation() { return mIsShowingTranslation; }