diff options
| author | Joanne Chung <joannechung@google.com> | 2021-04-14 23:24:42 +0800 |
|---|---|---|
| committer | Joanne Chung <joannechung@google.com> | 2021-05-21 19:11:14 +0800 |
| commit | 18b1d3bde4b2437cdeeacd3544274a8f8e80ed81 (patch) | |
| tree | 0132607a112cc952279df77fa9131596ff67f325 /core/java/android/widget/TextView.java | |
| parent | cc0987b12a4b8e3731f39614324a5c28bd4ec52b (diff) | |
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
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); + } } } |
