summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Joanne Chung <joannechung@google.com> 2021-09-19 23:06:02 +0800
committer Joanne Chung <joannechung@google.com> 2021-09-19 15:16:57 +0000
commit7672bbc91578389c46bdfbd679166698f4cf24d7 (patch)
tree92603fc18a7c6308f1c369beb716d36ebdedbde1
parent98fa7d142f9dabf78bbe3c180ecee02a0f21525b (diff)
Fix the existing translation doesn't update for new translated result.
It is possible the existing text is changed that triggers a new translation. We cache the translation response when onShowTransltion is called. We should keep the translation response is updated if this is a new translation result. Bug: 200232741 Test: atest CtsTranslationTestCases Test: manual. The issue app works fine. Change-Id: Iaf7423cd35d4484e33de84e478256b77a000f390
-rw-r--r--core/java/android/text/method/TranslationTransformationMethod.java7
-rw-r--r--core/java/android/view/translation/ViewTranslationCallback.java5
-rw-r--r--core/java/android/widget/TextViewTranslationCallback.java7
3 files changed, 18 insertions, 1 deletions
diff --git a/core/java/android/text/method/TranslationTransformationMethod.java b/core/java/android/text/method/TranslationTransformationMethod.java
index 80387aa8d66d..43d186ee9d21 100644
--- a/core/java/android/text/method/TranslationTransformationMethod.java
+++ b/core/java/android/text/method/TranslationTransformationMethod.java
@@ -62,6 +62,13 @@ public class TranslationTransformationMethod implements TransformationMethod2 {
return mOriginalTranslationMethod;
}
+ /**
+ * Returns the {@link TextView}'s {@link ViewTranslationResponse}.
+ */
+ public ViewTranslationResponse getViewTranslationResponse() {
+ return mTranslationResponse;
+ }
+
@Override
public CharSequence getTransformation(CharSequence source, View view) {
if (!mAllowLengthChanges) {
diff --git a/core/java/android/view/translation/ViewTranslationCallback.java b/core/java/android/view/translation/ViewTranslationCallback.java
index a0756622ca69..66c028b48dc6 100644
--- a/core/java/android/view/translation/ViewTranslationCallback.java
+++ b/core/java/android/view/translation/ViewTranslationCallback.java
@@ -41,6 +41,11 @@ public interface ViewTranslationCallback {
* method will not be called before {@link View#onViewTranslationResponse} or
* {@link View#onVirtualViewTranslationResponses}.
*
+ * <p> NOTE: It is possible the user changes text that causes a new
+ * {@link ViewTranslationResponse} returns to show the new translation. If you cache the
+ * {@link ViewTranslationResponse} here, you should remember to keep the cached value up
+ * to date.
+ *
* <p> NOTE: For TextView implementation, {@link ContentCaptureSession#notifyViewTextChanged}
* shouldn't be called with the translated text, simply calling setText() here will trigger the
* method. You should either override {@code View#onProvideContentCaptureStructure()} to report
diff --git a/core/java/android/widget/TextViewTranslationCallback.java b/core/java/android/widget/TextViewTranslationCallback.java
index 9d60009031f9..152405bf4d37 100644
--- a/core/java/android/widget/TextViewTranslationCallback.java
+++ b/core/java/android/widget/TextViewTranslationCallback.java
@@ -70,7 +70,12 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
+ "onViewTranslationResponse().");
return false;
}
- if (mTranslationTransformation == null) {
+ // It is possible user changes text and new translation response returns, system should
+ // update the translation response to keep the result up to date.
+ // Because TextView.setTransformationMethod() will skip the same TransformationMethod
+ // instance, we should create a new one to let new translation can work.
+ if (mTranslationTransformation == null
+ || !response.equals(mTranslationTransformation.getViewTranslationResponse())) {
TransformationMethod originalTranslationMethod =
((TextView) view).getTransformationMethod();
mTranslationTransformation = new TranslationTransformationMethod(response,