From 18b1d3bde4b2437cdeeacd3544274a8f8e80ed81 Mon Sep 17 00:00:00 2001 From: Joanne Chung Date: Wed, 14 Apr 2021 23:24:42 +0800 Subject: Refine the ViewTranslationCallback usage. Currently, TextView uses its default implementation even developers uses setViewTranslationCallback() to set their customized ViewTranslationCallback, we should only set default TextView implementation if developers don't set it. The onViewTranslationResponse() will call getViewTranslationCallback instead of getting TextView default implementation directly. This can make sure we can get the expected ViewTranslationCallback. Bug: 183467275 Test: manual Test: atest CtsTranslationTestCases Change-Id: I41417140f8985aec6c80f1bca3cfba804727d5df --- core/java/android/widget/TextView.java | 66 +++++++++++++--------------------- 1 file changed, 24 insertions(+), 42 deletions(-) (limited to 'core/java/android/widget/TextView.java') diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index e0238b9bce13..9959510d5ac0 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -740,7 +740,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private MovementMethod mMovement; private TransformationMethod mTransformation; - private TextViewTranslationCallback mDefaultTranslationCallback; @UnsupportedAppUsage private boolean mAllowTransformationLengthChange; @UnsupportedAppUsage @@ -2376,11 +2375,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener @ViewDebug.CapturedViewProperty @InspectableProperty public CharSequence getText() { - if (mUseTextPaddingForUiTranslation - && mDefaultTranslationCallback != null - && mDefaultTranslationCallback.isTextPaddingEnabled() - && mDefaultTranslationCallback.isShowingTranslation()) { - return mDefaultTranslationCallback.getPaddedText(mText, mTransformed); + if (mUseTextPaddingForUiTranslation) { + ViewTranslationCallback callback = getViewTranslationCallback(); + if (callback != null && callback instanceof TextViewTranslationCallback) { + TextViewTranslationCallback defaultCallback = + (TextViewTranslationCallback) callback; + if (defaultCallback.isTextPaddingEnabled() + && defaultCallback.isShowingTranslation()) { + return defaultCallback.getPaddedText(mText, mTransformed); + } + } } return mText; } @@ -13932,30 +13936,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener requestsCollector.accept(requestBuilder.build()); } - /** - * Returns a {@link ViewTranslationCallback} that is used to display the translated information. - * The default implementation will use a {@link TransformationMethod} that allow to replace the - * current {@link TransformationMethod} to transform the original text to the translated text - * display. - * - * @return a {@link ViewTranslationCallback} that is used to control how to display the - * translated information or {@code null} if this View doesn't support translation. - * - * @hide - */ - @Nullable - @Override - public ViewTranslationCallback getViewTranslationCallback() { - return getDefaultViewTranslationCallback(); - } - - private ViewTranslationCallback getDefaultViewTranslationCallback() { - if (mDefaultTranslationCallback == null) { - mDefaultTranslationCallback = new TextViewTranslationCallback(); - } - return mDefaultTranslationCallback; - } - /** * * Called when the content from {@link #onCreateViewTranslationRequest} had been translated by @@ -13969,17 +13949,19 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener public void onViewTranslationResponse(@NonNull ViewTranslationResponse response) { // set ViewTranslationResponse super.onViewTranslationResponse(response); - // TODO(b/183467275): Use the overridden ViewTranslationCallback instead of our default - // implementation if the view has overridden getViewTranslationCallback. - TextViewTranslationCallback callback = - (TextViewTranslationCallback) getDefaultViewTranslationCallback(); - TranslationTransformationMethod oldTranslationMethod = - callback.getTranslationTransformation(); - TransformationMethod originalTranslationMethod = oldTranslationMethod != null - ? oldTranslationMethod.getOriginalTransformationMethod() : mTransformation; - TranslationTransformationMethod newTranslationMethod = - new TranslationTransformationMethod(response, originalTranslationMethod); - // TODO(b/178353965): well-handle setTransformationMethod. - callback.setTranslationTransformation(newTranslationMethod); + // TODO(b/178353965): move to ViewTranslationCallback.onShow() + ViewTranslationCallback callback = getViewTranslationCallback(); + if (callback instanceof TextViewTranslationCallback) { + TextViewTranslationCallback textViewDefaultCallback = + (TextViewTranslationCallback) callback; + TranslationTransformationMethod oldTranslationMethod = + textViewDefaultCallback.getTranslationTransformation(); + TransformationMethod originalTranslationMethod = oldTranslationMethod != null + ? oldTranslationMethod.getOriginalTransformationMethod() : mTransformation; + TranslationTransformationMethod newTranslationMethod = + new TranslationTransformationMethod(response, originalTranslationMethod); + // TODO(b/178353965): well-handle setTransformationMethod. + textViewDefaultCallback.setTranslationTransformation(newTranslationMethod); + } } } -- cgit v1.2.3