Not visible view should not be announced or interacted with.

1. Some invisible views' text was reported by accessibility events.

2. Accessibility actions could have been perfromed on invisible views.

bug:5264355

Change-Id: I68184fb436a3e10e947ec6f1eae02aa3d0d1cb7f
diff --git a/core/java/android/widget/AdapterView.java b/core/java/android/widget/AdapterView.java
index 4ba604d..565c859 100644
--- a/core/java/android/widget/AdapterView.java
+++ b/core/java/android/widget/AdapterView.java
@@ -886,9 +886,11 @@
             event.setEventType(AccessibilityEvent.TYPE_VIEW_SELECTED);
         }
 
-        // We first get a chance to populate the event.
-        onPopulateAccessibilityEvent(event);
-
+        View selectedView = getSelectedView();
+        if (selectedView != null && selectedView.getVisibility() == VISIBLE) {
+            // We first get a chance to populate the event.
+            onPopulateAccessibilityEvent(event);
+        }
         return false;
     }
 
@@ -896,10 +898,7 @@
     public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
         // We send selection events only from AdapterView to avoid
         // generation of such event for each child.
-        View selectedView = getSelectedView();
-        if (selectedView != null) {
-            selectedView.dispatchPopulateAccessibilityEvent(event);
-        }
+        getSelectedView().dispatchPopulateAccessibilityEvent(event);
     }
 
     @Override