The logic for not populating text to some  accessibility events is scattered.

1. Some accessibility evenents should not and were not dispatched for
   text population but there was no centralized location for enforcing
   this - rather the system was firing them in a specific way or there
   were conditions in a few places enforcing that. Now this is centralized
   and clean.

2. Updated the documentation with some new event types the were lacking.

3. Explicitly stated in the documentaition which events are dispatched to
   the sub-tree of the source for text populatation.

bug:5394527

Change-Id: I86e383807d777019ac98b970c7d9d02a2f7afac6
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index d193d6e..67a3776 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -1487,6 +1487,18 @@
     }
 
     /**
+     * Accessibility event types that are dispatched for text population.
+     */
+    private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
+            AccessibilityEvent.TYPE_VIEW_CLICKED
+            | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
+            | AccessibilityEvent.TYPE_VIEW_SELECTED
+            | AccessibilityEvent.TYPE_VIEW_FOCUSED
+            | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
+            | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
+            | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT;
+
+    /**
      * Temporary Rect currently for use in setBackground().  This will probably
      * be extended in the future to hold our own class with more than just
      * a Rect. :)
@@ -3855,7 +3867,10 @@
             return;
         }
         onInitializeAccessibilityEvent(event);
-        dispatchPopulateAccessibilityEvent(event);
+        // Only a subset of accessibility events populates text content.
+        if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
+            dispatchPopulateAccessibilityEvent(event);
+        }
         // In the beginning we called #isShown(), so we know that getParent() is not null.
         getParent().requestSendAccessibilityEvent(this, event);
     }
@@ -3876,6 +3891,10 @@
      * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
      * is responsible for handling this call.
      * </p>
+     * <p>
+     * <em>Note:</em> Accessibility events of certain types are not dispatched for
+     * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
+     * </p>
      *
      * @param event The event.
      *
@@ -3895,12 +3914,6 @@
      * Note: Called from the default {@link AccessibilityDelegate}.
      */
     boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
-        // Do not populate text to scroll events. They describe position change
-        // and usually come from container with a lot of text which is not very
-        // informative for accessibility purposes. Also they are fired frequently.
-        if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
-            return true;
-        }
         onPopulateAccessibilityEvent(event);
         return false;
     }