diff options
Diffstat (limited to 'core/java/android/widget/TextView.java')
| -rw-r--r-- | core/java/android/widget/TextView.java | 66 |
1 files changed, 24 insertions, 42 deletions
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; } @@ -13933,30 +13937,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } /** - * 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 * the TranslationService. The default implementation will replace the current @@ -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); + } } } |
