Reset translation state when views disappear.

Views may be re-used (e.g., as in ListView and RecyclerView) so we need
to ensure they don't have the translated text when they appear again.

RecyclerView seems to recycle translated views even when
`hasTransientState() == true` which is the case when views are
translated. This should be fixed separately.

Bug: 223700458
Test: atest CtsTranslationTestCases
Test: atest CtsContentCaptureServiceTestCases
Test: atest CtsAutoFillServiceTestCases
Test: manual - New messages are translated correctly even if views are
re-used. Messages are translated correctly when scrolled onto the screen
and stay translated while on-screen.

Change-Id: I97cc418a801c006c857e971110ab811ad383343e
This commit is contained in:
Mark Punzalan
2022-04-27 22:39:54 +00:00
parent 100004b0ca
commit c35960c314
2 changed files with 22 additions and 7 deletions

View File

@@ -9921,7 +9921,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
* <ol>
* <li>It should only be called when content capture is enabled for the view.
* <li>It must call viewAppeared() before viewDisappeared()
* <li>viewAppearead() can only be called when the view is visible and laidout
* <li>viewAppeared() can only be called when the view is visible and laid out
* <li>It should not call the same event twice.
* </ol>
*/
@@ -9998,6 +9998,11 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
Log.v(CONTENT_CAPTURE_LOG_TAG, "no AttachInfo on disappeared for " + this);
}
}
// We reset any translation state as views may be re-used (e.g., as in ListView and
// RecyclerView). We only need to do this for views important for content capture since
// views unimportant for content capture won't be translated anyway.
clearTranslationState();
}
}
@@ -12717,6 +12722,21 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
== PFLAG4_HAS_TRANSLATION_TRANSIENT_STATE;
}
/**
* @hide
*/
public void clearTranslationState() {
if (mViewTranslationCallback != null) {
mViewTranslationCallback.onClearTranslation(this);
}
clearViewTranslationCallback();
clearViewTranslationResponse();
if (hasTranslationTransientState()) {
setHasTransientState(false);
setHasTranslationTransientState(false);
}
}
/**
* Returns true if this view is currently attached to a window.
*/

View File

@@ -158,12 +158,7 @@ public class UiTranslationController implements Dumpable {
case STATE_UI_TRANSLATION_FINISHED:
destroyTranslators();
runForEachView((view, callback) -> {
callback.onClearTranslation(view);
view.clearViewTranslationResponse();
if (view.hasTranslationTransientState()) {
view.setHasTransientState(false);
view.setHasTranslationTransientState(false);
}
view.clearTranslationState();
});
notifyTranslationFinished(/* activityDestroyed= */ false);
synchronized (mLock) {