From 6c8a043d7f896e212525b9b0c77c11c2322e6340 Mon Sep 17 00:00:00 2001 From: Sally Date: Wed, 17 May 2023 00:44:47 +0000 Subject: [PATCH] Discourage dispatchAccessibilityEvent and onPopulate for non-text modifications Developers use these methods in non-text ways, which has to confusing announcements and behavior in accessibility services. This provides alternatives that have better semantics and which lead to more consistent user experiences. Test: n/a (builds) Bug: 78791516 Change-Id: I84cfc9dd1b7f46fd6af2a5112f526b0d5e327f5a --- core/java/android/view/View.java | 49 ++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 71e9052627b93..5b2ce97bb3e07 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -8662,15 +8662,43 @@ public class View implements Drawable.Callback, KeyEvent.Callback, } /** - * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then - * to its children for adding their text content to the event. Note that the - * event text is populated in a separate dispatch path since we add to the + * Dispatches an {@link AccessibilityEvent} to the {@link View} to add the text content of the + * view and its children. + *

+ * Note: This method should only be used with event.setText(). + * Avoid mutating other event state in this method. In general, put UI metadata in the node for + * services to easily query. + *

+ *

+ * Note that the event text is populated in a separate dispatch path + * ({@link #onPopulateAccessibilityEvent(AccessibilityEvent)}) since we add to the * event not only the text of the source but also the text of all its descendants. + *

* A typical implementation will call - * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view + * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on this view * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} - * on each child. Override this method if custom population of the event text - * content is required. + * on each child or the first child that is visible. Override this method if custom population + * of the event text content is required. + * *

* If an {@link AccessibilityDelegate} has been specified via calling * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its @@ -8714,9 +8742,12 @@ public class View implements Drawable.Callback, KeyEvent.Callback, /** * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} * giving a chance to this View to populate the accessibility event with its - * text content. While this method is free to modify event - * attributes other than text content, doing so should normally be performed in - * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}. + * text content. + *

+ * Note: This method should only be used with event.setText(). + * Avoid mutating other event state in this method. Instead, follow the practices described in + * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}. In general, put UI + * metadata in the node for services to easily query, than in transient events. *

* Example: Adding formatted date string to an accessibility event in addition * to the text added by the super implementation: