summaryrefslogtreecommitdiff
path: root/core/java/android/widget/TextViewTranslationCallback.java
diff options
context:
space:
mode:
authorJoanne Chung <joannechung@google.com>2021-06-10 18:34:19 +0800
committerJoanne Chung <joannechung@google.com>2021-06-17 22:12:27 +0800
commita9da1722cc6994d32b5eb2ca5618dba7013d447b (patch)
treee657b0595068a545de4026b20911ac6afd3df1dd /core/java/android/widget/TextViewTranslationCallback.java
parent44e18a7ab7b67ffd82de27d567a676d4749b67fd (diff)
Bug fix: Move the implementation out from View.onTranslationResponse.
Keep onTranslationResponse() only to save TranslationResponse. We can move set TransformationMethod logic to TextViewTranslationCallback. With the current implementation, if the developers overrides onTranslationResponse(), they don't have a chance to use the TextView default ViewTranslationCallback implementation because they don't set the TranslationTransformation, they must implement their solution. If we move logic to TextViewViewTranslationCallback and the developers only overrides onTranslationResponse(), they still have a chance to use the default TextViewViewTranslationCallback if the developers set the TranslationResponse for View. Bug: 178353965 Test: manual to make sure translation still works. Test: atest CtsTranslationTestCases Change-Id: Iada7f3efbbc7705ecf962c78e275ed942816707f
Diffstat (limited to 'core/java/android/widget/TextViewTranslationCallback.java')
-rw-r--r--core/java/android/widget/TextViewTranslationCallback.java69
1 files changed, 24 insertions, 45 deletions
diff --git a/core/java/android/widget/TextViewTranslationCallback.java b/core/java/android/widget/TextViewTranslationCallback.java
index a7d5ee465299..e1b04f8957e5 100644
--- a/core/java/android/widget/TextViewTranslationCallback.java
+++ b/core/java/android/widget/TextViewTranslationCallback.java
@@ -56,26 +56,6 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
private CharSequence mContentDescription;
- /**
- * Invoked by the platform when receiving the successful {@link ViewTranslationResponse} for the
- * view that provides the translatable information by {@link View#createTranslationRequest} and
- * sent by the platform.
- */
- void setTranslationTransformation(TranslationTransformationMethod method) {
- if (method == null) {
- if (DEBUG) {
- Log.w(TAG, "setTranslationTransformation: should not set null "
- + "TranslationTransformationMethod");
- }
- return;
- }
- mTranslationTransformation = method;
- }
-
- TranslationTransformationMethod getTranslationTransformation() {
- return mTranslationTransformation;
- }
-
private void clearTranslationTransformation() {
if (DEBUG) {
Log.v(TAG, "clearTranslationTransformation: " + mTranslationTransformation);
@@ -88,34 +68,33 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
*/
@Override
public boolean onShowTranslation(@NonNull View view) {
- if (view.getViewTranslationResponse() == null) {
- Log.wtf(TAG, "onShowTranslation() shouldn't be called before "
+ ViewTranslationResponse response = view.getViewTranslationResponse();
+ if (response == null) {
+ Log.e(TAG, "onShowTranslation() shouldn't be called before "
+ "onViewTranslationResponse().");
return false;
}
- if (mTranslationTransformation != null) {
- final TransformationMethod transformation = mTranslationTransformation;
- runWithAnimation(
- (TextView) view,
- () -> {
- mIsShowingTranslation = true;
- ((TextView) view).setTransformationMethod(transformation);
- });
- ViewTranslationResponse response = view.getViewTranslationResponse();
- if (response.getKeys().contains(ViewTranslationRequest.ID_CONTENT_DESCRIPTION)) {
- CharSequence translatedContentDescription =
- response.getValue(ViewTranslationRequest.ID_CONTENT_DESCRIPTION).getText();
- if (!TextUtils.isEmpty(translatedContentDescription)) {
- mContentDescription = view.getContentDescription();
- view.setContentDescription(translatedContentDescription);
- }
- }
- } else {
- if (DEBUG) {
- // TODO(b/182433547): remove before S release
- Log.w(TAG, "onShowTranslation(): no translated text.");
+ if (mTranslationTransformation == null) {
+ TransformationMethod originalTranslationMethod =
+ ((TextView) view).getTransformationMethod();
+ mTranslationTransformation = new TranslationTransformationMethod(response,
+ originalTranslationMethod);
+ }
+ final TransformationMethod transformation = mTranslationTransformation;
+ runWithAnimation(
+ (TextView) view,
+ () -> {
+ mIsShowingTranslation = true;
+ // TODO(b/178353965): well-handle setTransformationMethod.
+ ((TextView) view).setTransformationMethod(transformation);
+ });
+ if (response.getKeys().contains(ViewTranslationRequest.ID_CONTENT_DESCRIPTION)) {
+ CharSequence translatedContentDescription =
+ response.getValue(ViewTranslationRequest.ID_CONTENT_DESCRIPTION).getText();
+ if (!TextUtils.isEmpty(translatedContentDescription)) {
+ mContentDescription = view.getContentDescription();
+ view.setContentDescription(translatedContentDescription);
}
- return false;
}
return true;
}
@@ -126,7 +105,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
@Override
public boolean onHideTranslation(@NonNull View view) {
if (view.getViewTranslationResponse() == null) {
- Log.wtf(TAG, "onHideTranslation() shouldn't be called before "
+ Log.e(TAG, "onHideTranslation() shouldn't be called before "
+ "onViewTranslationResponse().");
return false;
}