From b8372c041e9e4fc6271d6db3efab1e7e5ec05814 Mon Sep 17 00:00:00 2001 From: Evan Rosky Date: Wed, 5 Apr 2017 15:07:31 -0700 Subject: [PATCH] Make AdapterView auto-focusable-aware AdapterView already does its own sort-of auto-focusable thing. This change makes it compatible with FOCUSABLE_AUTO. Bug: 36984131 Test: AdapterViewTest#testchangeFocusable Change-Id: Iff57caff0f59fb79a28ed36c78530cff41533b62 --- core/java/android/widget/AdapterView.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/core/java/android/widget/AdapterView.java b/core/java/android/widget/AdapterView.java index 5725b496f1507..be548692ca088 100644 --- a/core/java/android/widget/AdapterView.java +++ b/core/java/android/widget/AdapterView.java @@ -216,7 +216,7 @@ public abstract class AdapterView extends ViewGroup { * @see #setFocusable(boolean) * @see #checkFocus() */ - private boolean mDesiredFocusableState; + private int mDesiredFocusableState = FOCUSABLE_AUTO; private boolean mDesiredFocusableInTouchModeState; /** Lazily-constructed runnable for dispatching selection events. */ @@ -250,6 +250,12 @@ public abstract class AdapterView extends ViewGroup { if (getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) { setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); } + + mDesiredFocusableState = getFocusable(); + if (mDesiredFocusableState == FOCUSABLE_AUTO) { + // Starts off without an adapter, so NOT_FOCUSABLE by default. + super.setFocusable(NOT_FOCUSABLE); + } } /** @@ -710,16 +716,16 @@ public abstract class AdapterView extends ViewGroup { } @Override - public void setFocusable(boolean focusable) { + public void setFocusable(@Focusable int focusable) { final T adapter = getAdapter(); final boolean empty = adapter == null || adapter.getCount() == 0; mDesiredFocusableState = focusable; - if (!focusable) { + if ((focusable & (FOCUSABLE_AUTO | FOCUSABLE)) == 0) { mDesiredFocusableInTouchModeState = false; } - super.setFocusable(focusable && (!empty || isInFilterMode())); + super.setFocusable((!empty || isInFilterMode()) ? focusable : NOT_FOCUSABLE); } @Override @@ -729,7 +735,7 @@ public abstract class AdapterView extends ViewGroup { mDesiredFocusableInTouchModeState = focusable; if (focusable) { - mDesiredFocusableState = true; + mDesiredFocusableState = FOCUSABLE; } super.setFocusableInTouchMode(focusable && (!empty || isInFilterMode())); @@ -743,7 +749,7 @@ public abstract class AdapterView extends ViewGroup { // for the client, see View.setFocusableInTouchMode() comments for more // details super.setFocusableInTouchMode(focusable && mDesiredFocusableInTouchModeState); - super.setFocusable(focusable && mDesiredFocusableState); + super.setFocusable(focusable ? mDesiredFocusableState : NOT_FOCUSABLE); if (mEmptyView != null) { updateEmptyStatus((adapter == null) || adapter.isEmpty()); }