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/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 0d160a9..c11a735 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -4529,7 +4529,7 @@
                 predicate.init(accessibilityId);
                 View root = ViewRootImpl.this.mView;
                 View target = root.findViewByPredicate(predicate);
-                if (target != null && target.isShown()) {
+                if (target != null && target.getVisibility() == View.VISIBLE) {
                     info = target.createAccessibilityNodeInfo();
                 }
             } finally {
@@ -4572,7 +4572,7 @@
             try {
                 View root = ViewRootImpl.this.mView;
                 View target = root.findViewById(viewId);
-                if (target != null && target.isShown()) {
+                if (target != null && target.getVisibility() == View.VISIBLE) {
                     info = target.createAccessibilityNodeInfo();
                 }
             } finally {
@@ -4623,14 +4623,14 @@
                 ArrayList<View> foundViews = mAttachInfo.mFocusablesTempList;
                 foundViews.clear();
 
-                View root;
+                View root = null;
                 if (accessibilityViewId != View.NO_ID) {
                     root = findViewByAccessibilityId(accessibilityViewId);
                 } else {
                     root = ViewRootImpl.this.mView;
                 }
 
-                if (root == null || !root.isShown()) {
+                if (root == null || root.getVisibility() != View.VISIBLE) {
                     return;
                 }
 
@@ -4645,7 +4645,7 @@
                 final int viewCount = foundViews.size();
                 for (int i = 0; i < viewCount; i++) {
                     View foundView = foundViews.get(i);
-                    if (foundView.isShown()) {
+                    if (foundView.getVisibility() == View.VISIBLE) {
                         infos.add(foundView.createAccessibilityNodeInfo());
                     }
                  }
@@ -4718,7 +4718,7 @@
 
         private boolean performActionFocus(int accessibilityId) {
             View target = findViewByAccessibilityId(accessibilityId);
-            if (target == null) {
+            if (target == null || target.getVisibility() != View.VISIBLE) {
                 return false;
             }
             // Get out of touch mode since accessibility wants to move focus around.
@@ -4728,7 +4728,7 @@
 
         private boolean performActionClearFocus(int accessibilityId) {
             View target = findViewByAccessibilityId(accessibilityId);
-            if (target == null) {
+            if (target == null || target.getVisibility() != View.VISIBLE) {
                 return false;
             }
             if (!target.isFocused()) {
@@ -4740,7 +4740,7 @@
 
         private boolean performActionSelect(int accessibilityId) {
             View target = findViewByAccessibilityId(accessibilityId);
-            if (target == null) {
+            if (target == null || target.getVisibility() != View.VISIBLE) {
                 return false;
             }
             if (target.isSelected()) {
@@ -4752,7 +4752,7 @@
 
         private boolean performActionClearSelection(int accessibilityId) {
             View target = findViewByAccessibilityId(accessibilityId);
-            if (target == null) {
+            if (target == null || target.getVisibility() != View.VISIBLE) {
                 return false;
             }
             if (!target.isSelected()) {
@@ -4769,18 +4769,21 @@
             }
             mFindByAccessibilityIdPredicate.init(accessibilityId);
             View foundView = root.findViewByPredicate(mFindByAccessibilityIdPredicate);
-            return (foundView != null && foundView.isShown()) ? foundView : null;
+            if (foundView == null || foundView.getVisibility() != View.VISIBLE) {
+                return null;
+            }
+            return foundView;
         }
 
         private final class FindByAccessibilitytIdPredicate implements Predicate<View> {
-            public int mSerchedId;
+            public int mSearchedId;
 
             public void init(int searchedId) {
-                mSerchedId = searchedId;
+                mSearchedId = searchedId;
             }
 
             public boolean apply(View view) {
-                return (view.getAccessibilityViewId() == mSerchedId);
+                return (view.getAccessibilityViewId() == mSearchedId);
             }
         }
     }
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
diff --git a/core/java/android/widget/RelativeLayout.java b/core/java/android/widget/RelativeLayout.java
index a5cf62e..6edfd59 100644
--- a/core/java/android/widget/RelativeLayout.java
+++ b/core/java/android/widget/RelativeLayout.java
@@ -961,7 +961,8 @@
         }
 
         for (View view : mTopToBottomLeftToRightSet) {
-            if (view.dispatchPopulateAccessibilityEvent(event)) {
+            if (view.getVisibility() == View.VISIBLE
+                    && view.dispatchPopulateAccessibilityEvent(event)) {
                 mTopToBottomLeftToRightSet.clear();
                 return true;
             }
diff --git a/core/java/android/widget/TabWidget.java b/core/java/android/widget/TabWidget.java
index 9afb625..191c4ca 100644
--- a/core/java/android/widget/TabWidget.java
+++ b/core/java/android/widget/TabWidget.java
@@ -405,7 +405,10 @@
         onPopulateAccessibilityEvent(event);
         // Dispatch only to the selected tab.
         if (mSelectedTab != -1) {
-            return getChildTabViewAt(mSelectedTab).dispatchPopulateAccessibilityEvent(event);
+            View tabView = getChildTabViewAt(mSelectedTab);
+            if (tabView != null && tabView.getVisibility() == VISIBLE) {
+                return tabView.dispatchPopulateAccessibilityEvent(event);
+            }
         }
         return false;
     }