From a5d4f094e82ccc4d803322072ab79d04f6c743a9 Mon Sep 17 00:00:00 2001 From: Joanne Chung Date: Fri, 10 Dec 2021 12:45:19 +0800 Subject: [PATCH] Fix translation animation flash for the same translation The system received the same multiple translation responses in a very short time. We use a isShowingTranslation flag to determine if the duplicated responses to call onShowTranslation. However the isShowingTransation flag is set in a post runnable, this may cause the system allow the duplicate responses can call onShowTranslation because the isShowingTransaltion isn't set true yet. Use multiple flags isShowingTranslation and a new isRunningAnimation to check if the same translation response should skip to call onShowTranslation. Bug: 207457172 Test: manual Test: atest CtsTranslationTestCases Change-Id: I7003b7f49fc0a8ce2e909228bcb89acedee6d3d0 --- .../view/translation/UiTranslationController.java | 5 ++++- .../android/widget/TextViewTranslationCallback.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/java/android/view/translation/UiTranslationController.java b/core/java/android/view/translation/UiTranslationController.java index 9d1bf171128eb..fb534c7885e42 100644 --- a/core/java/android/view/translation/UiTranslationController.java +++ b/core/java/android/view/translation/UiTranslationController.java @@ -435,7 +435,10 @@ public class UiTranslationController { if (view.getViewTranslationResponse() != null && view.getViewTranslationResponse().equals(response)) { if (callback instanceof TextViewTranslationCallback) { - if (((TextViewTranslationCallback) callback).isShowingTranslation()) { + TextViewTranslationCallback textViewCallback = + (TextViewTranslationCallback) callback; + if (textViewCallback.isShowingTranslation() + || textViewCallback.isAnimationRunning()) { if (DEBUG) { Log.d(TAG, "Duplicate ViewTranslationResponse for " + autofillId + ". Ignoring."); diff --git a/core/java/android/widget/TextViewTranslationCallback.java b/core/java/android/widget/TextViewTranslationCallback.java index 4a78f3ee6faca..942be21b1ade9 100644 --- a/core/java/android/widget/TextViewTranslationCallback.java +++ b/core/java/android/widget/TextViewTranslationCallback.java @@ -46,6 +46,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { private TranslationTransformationMethod mTranslationTransformation; private boolean mIsShowingTranslation = false; + private boolean mAnimationRunning = false; private boolean mIsTextPaddingEnabled = false; private CharSequence mPaddedText; private int mAnimationDurationMillis = 250; // default value @@ -92,6 +93,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { (TextView) view, () -> { mIsShowingTranslation = true; + mAnimationRunning = false; // TODO(b/178353965): well-handle setTransformationMethod. ((TextView) view).setTransformationMethod(transformation); }); @@ -124,6 +126,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { (TextView) view, () -> { mIsShowingTranslation = false; + mAnimationRunning = false; ((TextView) view).setTransformationMethod(transformation); }); if (!TextUtils.isEmpty(mContentDescription)) { @@ -162,6 +165,13 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { return mIsShowingTranslation; } + /** + * Returns whether the view is running animation to show or hide the translation. + */ + public boolean isAnimationRunning() { + return mAnimationRunning; + } + @Override public void enableContentPadding() { mIsTextPaddingEnabled = true; @@ -230,6 +240,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback { mAnimator.end(); // Note: mAnimator is now null; do not use again here. } + mAnimationRunning = true; int fadedOutColor = colorWithAlpha(view.getCurrentTextColor(), 0); mAnimator = ValueAnimator.ofArgb(view.getCurrentTextColor(), fadedOutColor); mAnimator.addUpdateListener(