Merge "Fixing some minor issues in accessibility focus." into jb-dev
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 6adf656..b69450c 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -4325,7 +4325,6 @@
         if (gainFocus) {
             if (AccessibilityManager.getInstance(mContext).isEnabled()) {
                 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
-                requestAccessibilityFocus();
             }
         }
 
@@ -6183,8 +6182,6 @@
             invalidate();
             sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
             notifyAccessibilityStateChanged();
-            // Try to give input focus to this view - not a descendant.
-            requestFocusNoSearch(View.FOCUS_DOWN, null);
             return true;
         }
         return false;
@@ -6230,11 +6227,13 @@
     private void requestAccessibilityFocusFromHover() {
         if (includeForAccessibility() && isActionableForAccessibility()) {
             requestAccessibilityFocus();
+            requestFocusNoSearch(View.FOCUS_DOWN, null);
         } else {
             if (mParent != null) {
                 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
                 if (nextFocus != null) {
                     nextFocus.requestAccessibilityFocus();
+                    nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
                 }
             }
         }
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java
index a8e1043..42c4ed6 100644
--- a/core/java/android/widget/AbsListView.java
+++ b/core/java/android/widget/AbsListView.java
@@ -1352,24 +1352,23 @@
             case ACCESSIBILITY_FOCUS_FORWARD: {
                 ViewRootImpl viewRootImpl = getViewRootImpl();
                 if (viewRootImpl == null) {
-                    break;
+                    return null;
                 }
                 View currentFocus = viewRootImpl.getAccessibilityFocusedHost();
                 if (currentFocus == null) {
-                    break;
+                    return super.focusSearch(this, direction);
                 }
                 // If we have the focus try giving it to the first child.
                 if (currentFocus == this) {
-                    final int firstVisiblePosition = getFirstVisiblePosition();
-                    if (firstVisiblePosition >= 0) {
+                    if (getChildCount() > 0) {
                         return getChildAt(0);
                     }
-                    return null;
+                    return super.focusSearch(this, direction);
                 }
                 // Find the item that has accessibility focus.
                 final int currentPosition = getPositionForView(currentFocus);
                 if (currentPosition < 0 || currentPosition >= getCount()) {
-                    break;
+                    return super.focusSearch(this, direction);
                 }
                 // Try to advance focus in the current item.
                 View currentItem = getChildAt(currentPosition - getFirstVisiblePosition());
@@ -1386,25 +1385,31 @@
                 final int nextPosition = currentPosition - getFirstVisiblePosition() + 1;
                 if (nextPosition < getChildCount()) {
                     return getChildAt(nextPosition);
+                } else {
+                    return super.focusSearch(this, direction);
                 }
-            } break;
+            }
             case ACCESSIBILITY_FOCUS_BACKWARD: {
                 ViewRootImpl viewRootImpl = getViewRootImpl();
                 if (viewRootImpl == null) {
-                    break;
+                    return null;
                 }
                 View currentFocus = viewRootImpl.getAccessibilityFocusedHost();
                 if (currentFocus == null) {
-                    break;
+                    return super.focusSearch(this, direction);
                 }
                 // If we have the focus do a generic search.
                 if (currentFocus == this) {
+                    final int lastChildIndex = getChildCount() - 1;
+                    if (lastChildIndex >= 0) {
+                        return getChildAt(lastChildIndex);
+                    }
                     return super.focusSearch(this, direction);
                 }
                 // Find the item that has accessibility focus.
                 final int currentPosition = getPositionForView(currentFocus);
                 if (currentPosition < 0 || currentPosition >= getCount()) {
-                    break;
+                    return super.focusSearch(this, direction);
                 }
                 // Try to advance focus in the current item.
                 View currentItem = getChildAt(currentPosition - getFirstVisiblePosition());
@@ -1422,7 +1427,7 @@
                 if (nextPosition >= 0) {
                     return getChildAt(nextPosition);
                 } else {
-                    return this;
+                    return super.focusSearch(this, direction);
                 }
             }
         }