ExploreByTouchHelper.java revision cc35612e283564c245408da4bb9264f38a02487a
16eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette/*
26eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Copyright (C) 2013 The Android Open Source Project
36eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette *
46eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Licensed under the Apache License, Version 2.0 (the "License");
56eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * you may not use this file except in compliance with the License.
66eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * You may obtain a copy of the License at
76eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette *
86eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette *      http://www.apache.org/licenses/LICENSE-2.0
96eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette *
106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Unless required by applicable law or agreed to in writing, software
116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * distributed under the License is distributed on an "AS IS" BASIS,
126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * See the License for the specific language governing permissions and
146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * limitations under the License.
156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */
166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverettepackage android.support.v4.widget;
186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.content.Context;
206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.graphics.Rect;
216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.os.Bundle;
22049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.annotation.NonNull;
23049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.annotation.Nullable;
24e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viveretteimport android.support.v4.util.SparseArrayCompat;
256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.AccessibilityDelegateCompat;
26049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.v4.view.KeyEventCompat;
276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.MotionEventCompat;
286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.ViewCompat;
297f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viveretteimport android.support.v4.view.ViewCompat.FocusDirection;
307f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viveretteimport android.support.v4.view.ViewCompat.FocusRealDirection;
316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.ViewParentCompat;
326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityEventCompat;
336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityManagerCompat;
346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityNodeProviderCompat;
366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityRecordCompat;
37049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.view.KeyEvent;
386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.MotionEvent;
396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.View;
406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.ViewParent;
416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.accessibility.AccessibilityEvent;
426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.accessibility.AccessibilityManager;
436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
44049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport java.util.ArrayList;
456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport java.util.List;
466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette/**
486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ExploreByTouchHelper is a utility class for implementing accessibility
496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * support in custom {@link View}s that represent a collection of View-like
506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * logical items. It extends {@link AccessibilityNodeProviderCompat} and
516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * simplifies many aspects of providing information to accessibility services
526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * and managing accessibility focus. This class does not currently support
536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * hierarchies of logical items.
546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p>
5512d8b3f36c7b503221185b44791822e59e690966Alan Viverette * Clients should override abstract methods on this class and attach it to the
5612d8b3f36c7b503221185b44791822e59e690966Alan Viverette * host view using {@link ViewCompat#setAccessibilityDelegate}:
57049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p>
586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <pre>
59049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * class MyCustomView extends View {
60049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     private MyVirtualViewHelper mVirtualViewHelper;
61049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
62049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public MyCustomView(Context context, ...) {
63049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         ...
64049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         mVirtualViewHelper = new MyVirtualViewHelper(this);
65049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         ViewCompat.setAccessibilityDelegate(this, mVirtualViewHelper);
66049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
67049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
68049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     &#64;Override
69049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public boolean dispatchHoverEvent(MotionEvent event) {
70049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       return mHelper.dispatchHoverEvent(this, event)
71049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *           || super.dispatchHoverEvent(event);
72049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
73049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
74049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     &#64;Override
75049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public boolean dispatchKeyEvent(KeyEvent event) {
76049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       return mHelper.dispatchKeyEvent(event)
77049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *           || super.dispatchKeyEvent(event);
78049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
79049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
80049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     &#64;Override
81049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public boolean onFocusChanged(boolean gainFocus, int direction,
82049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         Rect previouslyFocusedRect) {
83049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
84049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
85049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
86049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * }
8712d8b3f36c7b503221185b44791822e59e690966Alan Viverette * mAccessHelper = new MyExploreByTouchHelper(someView);
886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ViewCompat.setAccessibilityDelegate(someView, mAccessHelper);
896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </pre>
906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */
916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverettepublic abstract class ExploreByTouchHelper extends AccessibilityDelegateCompat {
926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** Virtual node identifier value for invalid nodes. */
936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    public static final int INVALID_ID = Integer.MIN_VALUE;
946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9512d8b3f36c7b503221185b44791822e59e690966Alan Viverette    /** Virtual node identifier value for the host view's node. */
9612d8b3f36c7b503221185b44791822e59e690966Alan Viverette    public static final int HOST_ID = View.NO_ID;
9712d8b3f36c7b503221185b44791822e59e690966Alan Viverette
986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** Default class name used for virtual views. */
99049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static final String DEFAULT_CLASS_NAME = "android.view.View";
1006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
101cc35612e283564c245408da4bb9264f38a02487aAlan Viverette    /** Default bounds used to determine if the client didn't set any. */
102cc35612e283564c245408da4bb9264f38a02487aAlan Viverette    private static final Rect INVALID_PARENT_BOUNDS = new Rect(
103cc35612e283564c245408da4bb9264f38a02487aAlan Viverette            Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
104cc35612e283564c245408da4bb9264f38a02487aAlan Viverette
1056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    // Temporary, reusable data structures.
1066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private final Rect mTempScreenRect = new Rect();
1076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private final Rect mTempParentRect = new Rect();
1086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private final Rect mTempVisibleRect = new Rect();
1096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private final int[] mTempGlobalRect = new int[2];
1106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
111049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Cache of accessibility nodes. This is populated on-demand. */
112e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette    private final SparseArrayCompat<AccessibilityNodeInfoCompat> mCachedNodes =
113e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette            new SparseArrayCompat<>();
114049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** System accessibility manager, used to check state and send events. */
1166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private final AccessibilityManager mManager;
1176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** View whose internal structure is exposed through this helper. */
119049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private final View mHost;
1206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
121049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Virtual node provider used to expose logical structure to services. */
122049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private MyNodeProvider mNodeProvider;
1236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
124049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Identifier for the virtual view that holds accessibility focus. */
125049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private int mAccessibilityFocusedVirtualViewId = INVALID_ID;
1266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
127049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Identifier for the virtual view that holds keyboard focus. */
128049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private int mKeyboardFocusedVirtualViewId = INVALID_ID;
129049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
130049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Identifier for the virtual view that is currently hovered. */
1316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private int mHoveredVirtualViewId = INVALID_ID;
1326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
134049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Constructs a new helper that can expose a virtual view hierarchy for the
135049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * specified host view.
1366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
137049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param host view whose virtual view hierarchy is exposed by this helper
1386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
139049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public ExploreByTouchHelper(View host) {
140049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (host == null) {
1416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new IllegalArgumentException("View may not be null");
1426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
144049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mHost = host;
145049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
146049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final Context context = host.getContext();
1476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
148049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
149049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Host view must be focusable so that we can delegate to virtual
150049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // views.
151049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        host.setFocusable(true);
1527f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        if (ViewCompat.getImportantForAccessibility(host)
1537f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette                == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
1547f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette            ViewCompat.setImportantForAccessibility(
1557f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette                    host, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
156049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
1586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    @Override
1606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    public AccessibilityNodeProviderCompat getAccessibilityNodeProvider(View host) {
1616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (mNodeProvider == null) {
162049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mNodeProvider = new MyNodeProvider();
1636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return mNodeProvider;
1656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
1666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
168049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Delegates hover events from the host view.
169049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
1706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Dispatches hover {@link MotionEvent}s to the virtual view hierarchy when
1716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * the Explore by Touch feature is enabled.
1726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
173049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method should be called by overriding the host view's
174049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link View#dispatchHoverEvent(MotionEvent)} method:
1756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <pre>&#64;Override
1766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * public boolean dispatchHoverEvent(MotionEvent event) {
177049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   return mHelper.dispatchHoverEvent(this, event)
178049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *       || super.dispatchHoverEvent(event);
1796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * }
1806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </pre>
1816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
1826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param event The hover event to dispatch to the virtual view hierarchy.
1836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @return Whether the hover event was handled.
1846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
185049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean dispatchHoverEvent(@NonNull MotionEvent event) {
1866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (!mManager.isEnabled()
1876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                || !AccessibilityManagerCompat.isTouchExplorationEnabled(mManager)) {
1886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
1896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (event.getAction()) {
1926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case MotionEventCompat.ACTION_HOVER_MOVE:
1936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case MotionEventCompat.ACTION_HOVER_ENTER:
1946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                final int virtualViewId = getVirtualViewAt(event.getX(), event.getY());
1956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                updateHoveredVirtualView(virtualViewId);
1966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return (virtualViewId != INVALID_ID);
1976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case MotionEventCompat.ACTION_HOVER_EXIT:
198049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                if (mAccessibilityFocusedVirtualViewId != INVALID_ID) {
1996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    updateHoveredVirtualView(INVALID_ID);
2006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    return true;
2016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                }
2026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return false;
2036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
2046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return false;
2056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
2066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
2076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
2086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
209049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Delegates key events from the host view.
210049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
211049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method should be called by overriding the host view's
212049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link View#dispatchKeyEvent(KeyEvent)} method:
213049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <pre>&#64;Override
214049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * public boolean dispatchKeyEvent(KeyEvent event) {
215049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   return mHelper.dispatchKeyEvent(event)
216049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *       || super.dispatchKeyEvent(event);
217049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * }
218049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * </pre>
219049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
220049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean dispatchKeyEvent(@NonNull KeyEvent event) {
221049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        boolean handled = false;
222049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
223049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int action = event.getAction();
224049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (action != KeyEvent.ACTION_UP) {
225049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            final int keyCode = event.getKeyCode();
226049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            switch (keyCode) {
227049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_LEFT:
228049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_UP:
229049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_RIGHT:
230049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_DOWN:
231049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    if (KeyEventCompat.hasNoModifiers(event)) {
232049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        final int direction = keyToDirection(keyCode);
233049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        final int count = 1 + event.getRepeatCount();
234049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        for (int i = 0; i < count; i++) {
235049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            if (moveFocus(direction, null)) {
236049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                                handled = true;
237049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            } else {
238049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                                break;
239049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            }
240049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        }
241049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    }
242049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    break;
243049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_CENTER:
244049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_ENTER:
245049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    if (KeyEventCompat.hasNoModifiers(event)) {
246049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        if (event.getRepeatCount() == 0) {
247049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            clickKeyboardFocusedVirtualView();
248049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            handled = true;
249049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        }
250049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    }
251049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    break;
252049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_TAB:
253049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    if (KeyEventCompat.hasNoModifiers(event)) {
254049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        handled = moveFocus(View.FOCUS_FORWARD, null);
255049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
256049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        handled = moveFocus(View.FOCUS_BACKWARD, null);
257049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    }
258049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    break;
259049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            }
260049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
261049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
262049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return handled;
263049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
264049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
265049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
266049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Delegates focus changes from the host view.
267049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
268049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method should be called by overriding the host view's
269049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link View#onFocusChanged(boolean, int, Rect)} method:
270049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <pre>&#64;Override
271049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * public boolean onFocusChanged(boolean gainFocus, int direction,
272049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *     Rect previouslyFocusedRect) {
273049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
274049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
275049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * }
276049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * </pre>
277049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
278049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void onFocusChanged(boolean gainFocus, int direction,
279049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            @Nullable Rect previouslyFocusedRect) {
280049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId != INVALID_ID) {
281049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            clearKeyboardFocusForVirtualView(mKeyboardFocusedVirtualViewId);
282049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
283049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
284049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (gainFocus) {
285049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            moveFocus(direction, previouslyFocusedRect);
286049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
287049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
288049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
289049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
290049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the identifier of the virtual view that has accessibility focus
291049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         or {@link #INVALID_ID} if no virtual view has accessibility
292049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         focus
293049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
294049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final int getAccessibilityFocusedVirtualViewId() {
295049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return mAccessibilityFocusedVirtualViewId;
296049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
297049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
298049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
299049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the identifier of the virtual view that has keyboard focus
300049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         or {@link #INVALID_ID} if no virtual view has keyboard focus
301049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
302049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final int getKeyboardFocusedVirtualViewId() {
303049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return mKeyboardFocusedVirtualViewId;
304049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
305049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
306049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
307049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Maps key event codes to focus directions.
308049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
309049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param keyCode the key event code
310049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the corresponding focus direction
311049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
312049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    @FocusRealDirection
313049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static int keyToDirection(int keyCode) {
314049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        switch (keyCode) {
315049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case KeyEvent.KEYCODE_DPAD_LEFT:
316049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_LEFT;
317049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case KeyEvent.KEYCODE_DPAD_UP:
318049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_UP;
319049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case KeyEvent.KEYCODE_DPAD_RIGHT:
320049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_RIGHT;
321049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            default:
322049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_DOWN;
323049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
324049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
325049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
326049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
327049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Obtains the bounds for the specified virtual view.
328049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
329049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view
330049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param outBounds the rect to populate with virtual view bounds
331049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
332049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private void getBoundsInParent(int virtualViewId, Rect outBounds) {
333049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node = obtainAccessibilityNodeInfo(virtualViewId);
334049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.getBoundsInParent(outBounds);
335049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
336049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
337049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
338049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Adapts AccessibilityNodeInfoCompat for obtaining bounds.
339049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
340049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static final FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat> NODE_ADAPTER =
341049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            new FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat>() {
342049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                @Override
343049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                public void obtainBounds(AccessibilityNodeInfoCompat node, Rect outBounds) {
344049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    node.getBoundsInParent(outBounds);
345049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
346049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            };
347049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
348049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
349e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette     * Adapts SparseArrayCompat for iterating through values.
350049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
351e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette    private static final FocusStrategy.CollectionAdapter<SparseArrayCompat<
352e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette            AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat> SPARSE_VALUES_ADAPTER =
353e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette            new FocusStrategy.CollectionAdapter<SparseArrayCompat<
354e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette                    AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat>() {
355049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                @Override
356049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                public AccessibilityNodeInfoCompat get(
357e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette                        SparseArrayCompat<AccessibilityNodeInfoCompat> collection, int index) {
358049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    return collection.valueAt(index);
359049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
360049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
361049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                @Override
362e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette                public int size(SparseArrayCompat<AccessibilityNodeInfoCompat> collection) {
363049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    return collection.size();
364049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
365049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            };
366049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
367049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
368049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Attempts to move keyboard focus in the specified direction.
369049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
370049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param direction the direction in which to move keyboard focus
371049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param previouslyFocusedRect the bounds of the previously focused item,
372049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                              or {@code null} if not available
373049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return {@code true} if keyboard focus moved to a virtual view managed
374049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         by this helper, or {@code false} otherwise
375049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
376049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private boolean moveFocus(@FocusDirection int direction, @Nullable Rect previouslyFocusedRect) {
377049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int focusedNodeId = mKeyboardFocusedVirtualViewId;
378049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat focusedNode =
379049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                focusedNodeId == INVALID_ID ? null : mCachedNodes.get(focusedNodeId);
380049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
381049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat nextFocusedNode;
382049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        switch (direction) {
383049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_FORWARD:
384049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_BACKWARD:
385049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                final boolean isLayoutRtl =
386049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        ViewCompat.getLayoutDirection(mHost) == ViewCompat.LAYOUT_DIRECTION_RTL;
387049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                nextFocusedNode = FocusStrategy.findNextFocusInRelativeDirection(mCachedNodes,
388049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        SPARSE_VALUES_ADAPTER, NODE_ADAPTER, focusedNode, direction, isLayoutRtl,
389049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        false);
390049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
391049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_LEFT:
392049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_UP:
393049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_RIGHT:
394049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_DOWN:
395049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                final Rect selectedRect = new Rect();
396049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                if (mKeyboardFocusedVirtualViewId != INVALID_ID) {
397049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Focus is moving from a virtual view within the host.
398049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    getBoundsInParent(mKeyboardFocusedVirtualViewId, selectedRect);
399049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                } else if (previouslyFocusedRect != null) {
400049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Focus is moving from a real view outside the host.
401049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    selectedRect.set(previouslyFocusedRect);
402049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                } else {
403049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Focus is moving from... somewhere? Make a guess.
404049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Usually this happens when another view was too lazy
405049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // to pass the previously focused rect (ex. ScrollView
406049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // when moving UP or DOWN).
407049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    guessPreviouslyFocusedRect(mHost, direction, selectedRect);
408049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
409049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                nextFocusedNode = FocusStrategy.findNextFocusInAbsoluteDirection(mCachedNodes,
410049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        SPARSE_VALUES_ADAPTER, NODE_ADAPTER, focusedNode, selectedRect, direction);
411049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
412049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            default:
413049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                throw new IllegalArgumentException("direction must be one of "
414049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        + "{FOCUS_FORWARD, FOCUS_BACKWARD, FOCUS_UP, FOCUS_DOWN, "
415049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        + "FOCUS_LEFT, FOCUS_RIGHT}.");
416049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
417049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
418049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int nextFocusedNodeId;
419049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (nextFocusedNode == null) {
420049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            nextFocusedNodeId = INVALID_ID;
421049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else {
422049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            final int index = mCachedNodes.indexOfValue(nextFocusedNode);
423049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            nextFocusedNodeId = mCachedNodes.keyAt(index);
424049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
425049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
426049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return requestKeyboardFocusForVirtualView(nextFocusedNodeId);
427049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
428049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
429049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
430049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Obtains a best guess for the previously focused rect for keyboard focus
431049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * moving in the specified direction.
432049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
433049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param host the view into which focus is moving
434049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param direction the absolute direction in which focus is moving
435049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param outBounds the rect to populate with the best-guess bounds for the
436049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                  previous focus rect
437049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
438049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static Rect guessPreviouslyFocusedRect(@NonNull View host,
439049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            @FocusRealDirection int direction, @NonNull Rect outBounds) {
440049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int w = host.getWidth();
441049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int h = host.getHeight();
442049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
443049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        switch (direction) {
444049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_LEFT:
445049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(w, 0, w, h);
446049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
447049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_UP:
448049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(0, h, w, h);
449049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
450049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_RIGHT:
451049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(-1, 0, -1, h);
452049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
453049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_DOWN:
454049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(0, -1, w, -1);
455049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
456049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            default:
457049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                throw new IllegalArgumentException("direction must be one of "
458049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
459049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
460049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
461049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return outBounds;
462049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
463049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
464049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
465049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Performs a click action on the keyboard focused virtual view, if any.
466049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
467049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return {@code true} if the click action was performed successfully or
468049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         {@code false} otherwise
469049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
470049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private boolean clickKeyboardFocusedVirtualView() {
471049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return mKeyboardFocusedVirtualViewId != INVALID_ID && onPerformActionForVirtualView(
472049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                mKeyboardFocusedVirtualViewId, AccessibilityNodeInfoCompat.ACTION_CLICK, null);
473049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
474049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
475049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
4766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates an event of the specified type with information about an item
4776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * and attempts to send it up through the view hierarchy.
4786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
4796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * You should call this method after performing a user action that normally
4806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * fires an accessibility event, such as clicking on an item.
481049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
4826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <pre>public void performItemClick(T item) {
4836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *   ...
4846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *   sendEventForVirtualViewId(item.id, AccessibilityEvent.TYPE_VIEW_CLICKED);
4856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * }
4866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </pre>
4876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
488049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view for which to
489049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      send an event
490049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to send
491049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return {@code true} if the event was sent successfully, {@code false}
492049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         otherwise
4936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
494049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean sendEventForVirtualView(int virtualViewId, int eventType) {
4956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((virtualViewId == INVALID_ID) || !mManager.isEnabled()) {
4966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
4976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
4986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
499049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final ViewParent parent = mHost.getParent();
5006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (parent == null) {
5016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
5026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
5036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityEvent event = createEvent(virtualViewId, eventType);
505049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return ViewParentCompat.requestSendAccessibilityEvent(parent, mHost, event);
5066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
5076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
5096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Notifies the accessibility framework that the properties of the parent
5106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * view have changed.
5116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
512049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * You <strong>must</strong> call this method after adding or removing
513049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * items from the parent view.
5146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
515049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void invalidateRoot() {
516722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        invalidateVirtualView(HOST_ID, AccessibilityEventCompat.CONTENT_CHANGE_TYPE_SUBTREE);
5176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
5186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
5206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Notifies the accessibility framework that the properties of a particular
5216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * item have changed.
5226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
523049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * You <strong>must</strong> call this method after changing any of the
524049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * properties set in
525049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}.
5266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
527049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id to invalidate, or
528049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      {@link #HOST_ID} to invalidate the root view
529722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * @see #invalidateVirtualView(int, int)
5306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
531049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void invalidateVirtualView(int virtualViewId) {
532722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        invalidateVirtualView(virtualViewId,
533722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED);
534722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette    }
535722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette
536722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette    /**
537722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * Notifies the accessibility framework that the properties of a particular
538722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * item have changed.
539722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * <p>
540049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * You <strong>must</strong> call this method after changing any of the
541049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * properties set in
542049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}.
543722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *
544049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id to invalidate, or
545049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      {@link #HOST_ID} to invalidate the root view
546049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param changeTypes the bit mask of change types. May be {@code 0} for the
547722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *                    default (undefined) change type or one or more of:
548722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <ul>
549722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION}
550722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_SUBTREE}
551722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_TEXT}
552722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_UNDEFINED}
553722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         </ul>
554722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     */
555049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void invalidateVirtualView(int virtualViewId, int changeTypes) {
556e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette        final SparseArrayCompat<AccessibilityNodeInfoCompat> cachedNodes = mCachedNodes;
557049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (virtualViewId == HOST_ID
558049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                && (changeTypes & AccessibilityEventCompat.CONTENT_CHANGE_TYPE_SUBTREE) != 0) {
5597c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            for (int i = 0, count = cachedNodes.size(); i < count; i++) {
5601fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette                cachedNodes.valueAt(i).recycle();
5617c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            }
5627c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            cachedNodes.clear();
563049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else {
5641fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette            final int index = cachedNodes.indexOfKey(virtualViewId);
5651fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette            if (index >= 0) {
5661fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette                cachedNodes.valueAt(index).recycle();
5671fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette                cachedNodes.removeAt(index);
5687c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            }
569049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
570049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
571722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        if (virtualViewId != INVALID_ID && mManager.isEnabled()) {
572049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            final ViewParent parent = mHost.getParent();
573722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette            if (parent != null) {
574049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                // Send events up the hierarchy so they can be coalesced.
575722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                final AccessibilityEvent event = createEvent(virtualViewId,
576722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                        AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED);
577722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                AccessibilityEventCompat.setContentChangeTypes(event, changeTypes);
578049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                ViewParentCompat.requestSendAccessibilityEvent(parent, mHost, event);
579722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette            }
580722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        }
5816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
5826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
584049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Returns the virtual view ID for the currently accessibility focused
585049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * item.
586148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette     *
587049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the identifier of the virtual view that has accessibility focus
588049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         or {@link #INVALID_ID} if no virtual view has accessibility
589049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         focus
590049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @deprecated Use {@link #getAccessibilityFocusedVirtualViewId()}.
591148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette     */
592049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    @Deprecated
593148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    public int getFocusedVirtualView() {
594049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return getAccessibilityFocusedVirtualViewId();
595049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
596049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
597049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
598049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Called when the focus state of a virtual view changes.
599049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
600049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view identifier
601049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param hasFocus      {@code true} if the view has focus, {@code false}
602049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      otherwise
603049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
604049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    protected void onVirtualViewKeyboardFocusChanged(int virtualViewId, boolean hasFocus) {
605049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Stub method.
606148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    }
607148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette
608148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    /**
6096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Sets the currently hovered item, sending hover accessibility events as
6106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * necessary to maintain the correct state.
6116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
612049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for the item currently being
613049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      hovered, or {@link #INVALID_ID} if no item is
614049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      hovered within the parent view
6156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private void updateHoveredVirtualView(int virtualViewId) {
6176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (mHoveredVirtualViewId == virtualViewId) {
6186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return;
6196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
6206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int previousVirtualViewId = mHoveredVirtualViewId;
6226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mHoveredVirtualViewId = virtualViewId;
6236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Stay consistent with framework behavior by sending ENTER/EXIT pairs
6256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // in reverse order. This is accurate as of API 18.
6266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        sendEventForVirtualView(virtualViewId, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER);
6276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        sendEventForVirtualView(
6286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                previousVirtualViewId, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT);
6296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
6326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityEvent} for the specified
63312d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * virtual view id, which includes the host view ({@link #HOST_ID}).
6346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
635049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for the item for which to
636049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      construct an event
637049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to construct
638049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityEvent} populated with information about
639049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         the specified item
6406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityEvent createEvent(int virtualViewId, int eventType) {
6426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (virtualViewId) {
64312d8b3f36c7b503221185b44791822e59e690966Alan Viverette            case HOST_ID:
6446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return createEventForHost(eventType);
6456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
6466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return createEventForChild(virtualViewId, eventType);
6476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
6486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
6516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityEvent} for the host node.
6526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
653049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to construct
654049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityEvent} populated with information about
655049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         the specified item
6566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityEvent createEventForHost(int eventType) {
6586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
659049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        ViewCompat.onInitializeAccessibilityEvent(mHost, event);
6607f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        return event;
6617f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette    }
6627f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette
6637f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette    @Override
6647f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette    public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
6657f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        super.onInitializeAccessibilityEvent(host, event);
666fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette
667fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        // Allow the client to populate the event.
668fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        onPopulateEventForHost(event);
6696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
6726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityEvent} populated with
6736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * information about the specified item.
6746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
675049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for the item for which to
676049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      construct an event
677049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to construct
678049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityEvent} populated with information about
679049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         the specified item
6806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityEvent createEventForChild(int virtualViewId, int eventType) {
6826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
683049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
684049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node = obtainAccessibilityNodeInfo(virtualViewId);
685049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
686049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Allow the client to override these properties,
687049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.getText().add(node.getText());
688049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setContentDescription(node.getContentDescription());
689049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setScrollable(node.isScrollable());
690049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setPassword(node.isPassword());
691049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setEnabled(node.isEnabled());
692049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setChecked(node.isChecked());
6936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Allow the client to populate the event.
6956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        onPopulateEventForVirtualView(virtualViewId, event);
6966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Make sure the developer is following the rules.
6986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (event.getText().isEmpty() && (event.getContentDescription() == null)) {
6996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must add text or a content description in "
7006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateEventForVirtualViewId()");
7016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
7026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Don't allow the client to override these properties.
704049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setClassName(node.getClassName());
705049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setSource(mHost, virtualViewId);
706049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        event.setPackageName(mHost.getContext().getPackageName());
7076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return event;
7096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
7106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
712049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Obtains a populated {@link AccessibilityNodeInfoCompat} for the
713049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * virtual view with the specified identifier.
714049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
715049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method may be called with identifier {@link #HOST_ID} to obtain a
716049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * node for the host view.
7176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
718049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view for which to
719049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      construct a node
720049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityNodeInfoCompat} populated with information
721049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         about the specified item
7226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
723049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    @NonNull
724049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private AccessibilityNodeInfoCompat obtainAccessibilityNodeInfo(int virtualViewId) {
725049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node;
726049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int cacheIndex = mCachedNodes.indexOfKey(virtualViewId);
727049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (cacheIndex >= 0) {
728049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node = mCachedNodes.valueAt(cacheIndex);
729049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else if (virtualViewId == HOST_ID) {
730049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node = createNodeForHost();
731049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else {
732049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node = createNodeForChild(virtualViewId);
7336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
734049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
735049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mCachedNodes.put(virtualViewId, node);
736049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
737049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return node;
7386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
7396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
7416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityNodeInfoCompat} for the
7426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * host view populated with its virtual descendants.
7436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
744049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityNodeInfoCompat} for the parent node
7456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
7461fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette    @NonNull
7476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityNodeInfoCompat createNodeForHost() {
7487f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        final AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain(mHost);
7497f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        ViewCompat.onInitializeAccessibilityNodeInfo(mHost, info);
75012d8b3f36c7b503221185b44791822e59e690966Alan Viverette
7516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Add the virtual descendants.
752049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final ArrayList<Integer> virtualViewIds = new ArrayList<>();
7536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        getVisibleVirtualViews(virtualViewIds);
7547f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette
7557f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        final int realNodeCount = info.getChildCount();
756fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        if (realNodeCount > 0 && virtualViewIds.size() > 0) {
757fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette            throw new RuntimeException("Views cannot have both real and virtual children");
758fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        }
7596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
760049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        for (int i = 0, count = virtualViewIds.size(); i < count; i++) {
7617f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette            info.addChild(mHost, virtualViewIds.get(i));
7626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
7636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7647f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        return info;
7657f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette    }
7667f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette
7677f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette    @Override
7687f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette    public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
7697f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        super.onInitializeAccessibilityNodeInfo(host, info);
7707f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette
7717f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        // Allow the client to populate the host node.
7727f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette        onPopulateNodeForHost(info);
7736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
7746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
7766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityNodeInfoCompat} for the
7776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * specified item. Automatically manages accessibility focus actions.
7786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
7796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Allows the implementing class to specify most node properties, but
7806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * overrides the following:
7816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
7826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setPackageName}
7836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setClassName}
7846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setParent(View)}
7856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setSource(View, int)}
7866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser}
7876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)}
7886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
7896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
7906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Uses the bounds of the parent view and the parent-relative bounding
7916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * rectangle specified by
7926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#getBoundsInParent} to automatically
7936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * update the following properties:
7946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
7956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser}
7966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setBoundsInParent}
7976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
7986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
799049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for item for which to construct
800049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      a node
801049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityNodeInfoCompat} for the specified item
8026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
8031fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette    @NonNull
8046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityNodeInfoCompat createNodeForChild(int virtualViewId) {
8056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
8066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Ensure the client has good defaults.
8086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.setEnabled(true);
809049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setFocusable(true);
8106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.setClassName(DEFAULT_CLASS_NAME);
811cc35612e283564c245408da4bb9264f38a02487aAlan Viverette        node.setBoundsInParent(INVALID_PARENT_BOUNDS);
8126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Allow the client to populate the node.
8146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        onPopulateNodeForVirtualView(virtualViewId, node);
8156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Make sure the developer is following the rules.
8176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((node.getText() == null) && (node.getContentDescription() == null)) {
8186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must add text or a content description in "
8196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.getBoundsInParent(mTempParentRect);
823cc35612e283564c245408da4bb9264f38a02487aAlan Viverette        if (mTempParentRect.equals(INVALID_PARENT_BOUNDS)) {
8246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must set parent bounds in "
8256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int actions = node.getActions();
8296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((actions & AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS) != 0) {
8306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must not add ACTION_ACCESSIBILITY_FOCUS in "
8316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((actions & AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS) != 0) {
8346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must not add ACTION_CLEAR_ACCESSIBILITY_FOCUS in "
8356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Don't allow the client to override these properties.
839049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setPackageName(mHost.getContext().getPackageName());
840049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setSource(mHost, virtualViewId);
841049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setParent(mHost);
8426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Manage internal accessibility focus state.
844049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mAccessibilityFocusedVirtualViewId == virtualViewId) {
8456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setAccessibilityFocused(true);
8466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
8476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        } else {
8486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setAccessibilityFocused(false);
8496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
8506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
852049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Manage internal keyboard focus state.
853049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final boolean isFocused = mKeyboardFocusedVirtualViewId == virtualViewId;
854049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (isFocused) {
855049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_FOCUS);
856049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else if (node.isFocusable()) {
857049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_FOCUS);
858049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
859049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setFocused(isFocused);
860049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
8616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Set the visibility based on the parent bound.
8626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (intersectVisibleToUser(mTempParentRect)) {
8636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setVisibleToUser(true);
8646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setBoundsInParent(mTempParentRect);
8656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Calculate screen-relative bound.
868049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mHost.getLocationOnScreen(mTempGlobalRect);
8696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int offsetX = mTempGlobalRect[0];
8706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int offsetY = mTempGlobalRect[1];
8716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mTempScreenRect.set(mTempParentRect);
8726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mTempScreenRect.offset(offsetX, offsetY);
8736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.setBoundsInScreen(mTempScreenRect);
8746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return node;
8766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean performAction(int virtualViewId, int action, Bundle arguments) {
8796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (virtualViewId) {
88012d8b3f36c7b503221185b44791822e59e690966Alan Viverette            case HOST_ID:
8816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return performActionForHost(action, arguments);
8826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
8836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return performActionForChild(virtualViewId, action, arguments);
8846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean performActionForHost(int action, Bundle arguments) {
888049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return ViewCompat.performAccessibilityAction(mHost, action, arguments);
8896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean performActionForChild(int virtualViewId, int action, Bundle arguments) {
8926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (action) {
8936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS:
894148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette                return requestAccessibilityFocus(virtualViewId);
8956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
896148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette                return clearAccessibilityFocus(virtualViewId);
897049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_FOCUS:
898049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return requestKeyboardFocusForVirtualView(virtualViewId);
899049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_CLEAR_FOCUS:
900049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return clearKeyboardFocusForVirtualView(virtualViewId);
9016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
902049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return onPerformActionForVirtualView(virtualViewId, action, arguments);
9036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
9056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
9076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Computes whether the specified {@link Rect} intersects with the visible
9086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * portion of its parent {@link View}. Modifies {@code localRect} to contain
9096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * only the visible portion.
9106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
911049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param localRect a rectangle in local (parent) coordinates
912049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether the specified {@link Rect} is visible on the screen
9136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
9146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean intersectVisibleToUser(Rect localRect) {
9156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Missing or empty bounds mean this view is not visible.
9166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((localRect == null) || localRect.isEmpty()) {
9176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Attached to invisible window means this view is not visible.
921049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mHost.getWindowVisibility() != View.VISIBLE) {
9226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // An invisible predecessor means that this view is not visible.
926049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        ViewParent viewParent = mHost.getParent();
9276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        while (viewParent instanceof View) {
9286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            final View view = (View) viewParent;
9296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            if ((ViewCompat.getAlpha(view) <= 0) || (view.getVisibility() != View.VISIBLE)) {
9306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return false;
9316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            }
9326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            viewParent = view.getParent();
9336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // A null parent implies the view is not visible.
9366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (viewParent == null) {
9376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // If no portion of the parent is visible, this view is not visible.
941049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (!mHost.getLocalVisibleRect(mTempVisibleRect)) {
9426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Check if the view intersects the visible portion of the parent.
9466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return localRect.intersect(mTempVisibleRect);
9476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
9486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
9506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Attempts to give accessibility focus to a virtual view.
9516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
9526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * A virtual view will not actually take focus if
9536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityManager#isEnabled()} returns false,
9546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityManager#isTouchExplorationEnabled()} returns false,
9556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * or the view already has accessibility focus.
9566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
957049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view on which to
958049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      place accessibility focus
959049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually took accessibility focus
9606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
9616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean requestAccessibilityFocus(int virtualViewId) {
9626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (!mManager.isEnabled()
9636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                || !AccessibilityManagerCompat.isTouchExplorationEnabled(mManager)) {
9646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // TODO: Check virtual view visibility.
967049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mAccessibilityFocusedVirtualViewId != virtualViewId) {
968f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette            // Clear focus from the previously focused view, if applicable.
969049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            if (mAccessibilityFocusedVirtualViewId != INVALID_ID) {
970049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                clearAccessibilityFocus(mAccessibilityFocusedVirtualViewId);
971f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette            }
972f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette
973f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette            // Set focus on the new view.
974049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mAccessibilityFocusedVirtualViewId = virtualViewId;
975f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette
9766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            // TODO: Only invalidate virtual view bounds.
977049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mHost.invalidate();
9786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            sendEventForVirtualView(virtualViewId,
9796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
9806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return true;
9816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return false;
9836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
9846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
9866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Attempts to clear accessibility focus from a virtual view.
987148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette     *
988049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view from which to
989049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      clear accessibility focus
990049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually cleared accessibility focus
9916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
992148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    private boolean clearAccessibilityFocus(int virtualViewId) {
993049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mAccessibilityFocusedVirtualViewId == virtualViewId) {
994049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mAccessibilityFocusedVirtualViewId = INVALID_ID;
995049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mHost.invalidate();
9966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            sendEventForVirtualView(virtualViewId,
9976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
998148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette            return true;
9996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1000148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette        return false;
10016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
10026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
10036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
1004049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Attempts to give keyboard focus to a virtual view.
1005049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
1006049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view on which to
1007049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      place keyboard focus
1008049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually took keyboard focus
1009049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
1010049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean requestKeyboardFocusForVirtualView(int virtualViewId) {
1011049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (!mHost.isFocused() && !mHost.requestFocus()) {
1012049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            // Host must have real keyboard focus.
1013049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return false;
1014049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1015049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1016049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId == virtualViewId) {
1017049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            // The virtual view already has focus.
1018049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return false;
1019049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1020049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1021049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId != INVALID_ID) {
1022049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            clearKeyboardFocusForVirtualView(mKeyboardFocusedVirtualViewId);
1023049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1024049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1025049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mKeyboardFocusedVirtualViewId = virtualViewId;
1026049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1027049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        onVirtualViewKeyboardFocusChanged(virtualViewId, true);
1028049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_FOCUSED);
1029049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1030049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return true;
1031049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
1032049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1033049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
1034049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Attempts to clear keyboard focus from a virtual view.
1035049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
1036049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view from which to
1037049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      clear keyboard focus
1038049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually cleared keyboard focus
1039049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
1040049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean clearKeyboardFocusForVirtualView(int virtualViewId) {
1041049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId != virtualViewId) {
1042049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            // The virtual view is not focused.
1043049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return false;
1044049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1045049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1046049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mKeyboardFocusedVirtualViewId = INVALID_ID;
1047049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1048049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        onVirtualViewKeyboardFocusChanged(virtualViewId, false);
1049049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_FOCUSED);
1050049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1051049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return true;
1052049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
1053049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1054049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
10556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Provides a mapping between view-relative coordinates and logical
10566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * items.
10576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
10586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param x The view-relative x coordinate
10596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param y The view-relative y coordinate
10606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @return virtual view identifier for the logical item under
106112d8b3f36c7b503221185b44791822e59e690966Alan Viverette     *         coordinates (x,y) or {@link #HOST_ID} if there is no item at
1062cd9978cdf0c3852189fb881846db6569d43f5598Alan Viverette     *         the given coordinates
10636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
10646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract int getVirtualViewAt(float x, float y);
10656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
10666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
10676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates a list with the view's visible items. The ordering of items
10686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * within {@code virtualViewIds} specifies order of accessibility focus
10696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * traversal.
10706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
10716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewIds The list to populate with visible items
10726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
10736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract void getVisibleVirtualViews(List<Integer> virtualViewIds);
10746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
10756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
10766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates an {@link AccessibilityEvent} with information about the
10776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * specified item.
10786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1079049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * The helper class automatically populates the following fields based on
1080049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * the values set by
1081049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)},
1082049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * but implementations may optionally override them:
10836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1084049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>event text, see {@link AccessibilityEvent#getText()}
1085049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>content description, see
1086049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityEvent#setContentDescription(CharSequence)}
1087049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>scrollability, see {@link AccessibilityEvent#setScrollable(boolean)}
1088049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>password state, see {@link AccessibilityEvent#setPassword(boolean)}
1089049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>enabled state, see {@link AccessibilityEvent#setEnabled(boolean)}
1090049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>checked state, see {@link AccessibilityEvent#setChecked(boolean)}
10916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
10926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
10936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The following required fields are automatically populated by the
10946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * helper class and may not be overridden:
10956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1096049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>item class name, set to the value used in
1097049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}
10986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>package name, set to the package of the host view's
10996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link Context}, see {@link AccessibilityEvent#setPackageName}
11006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>event source, set to the host view and virtual view identifier,
11016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * see {@link AccessibilityRecordCompat#setSource(View, int)}
11026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
11046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewId The virtual view id for the item for which to
11056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            populate the event
11066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param event The event to populate
11076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
1108049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
1109049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Default implementation is no-op.
1110049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
11116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
11126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
1113fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * Populates an {@link AccessibilityEvent} with information about the host
1114fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * view.
1115fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * <p>
1116fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * The default implementation is a no-op.
1117fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     *
1118fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * @param event the event to populate with information about the host view
1119fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     */
1120fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    protected void onPopulateEventForHost(AccessibilityEvent event) {
1121fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        // Default implementation is no-op.
1122fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    }
1123fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette
1124fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    /**
11256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates an {@link AccessibilityNodeInfoCompat} with information
11266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * about the specified item.
11276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1128049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Implementations <strong>must</strong> populate the following required
1129049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * fields:
11306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1131049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>event text, see
1132049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setText(CharSequence)} or
1133049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setContentDescription(CharSequence)}
11346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>bounds in parent coordinates, see
1135049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setBoundsInParent(Rect)}
11366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The helper class automatically populates the following fields with
11396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * default values, but implementations may optionally override them:
11406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1141049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>enabled state, set to {@code true}, see
1142049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setEnabled(boolean)}
1143049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>keyboard focusability, set to {@code true}, see
1144049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setFocusable(boolean)}
1145049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>item class name, set to {@code android.view.View}, see
1146049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setClassName(CharSequence)}
11476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The following required fields are automatically populated by the
11506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * helper class and may not be overridden:
11516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
11526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>package name, identical to the package name set by
1153049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateEventForVirtualView(int, AccessibilityEvent)}, see
11546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#setPackageName}
11556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>node source, identical to the event source set in
1156049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateEventForVirtualView(int, AccessibilityEvent)}, see
11576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#setSource(View, int)}
11586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>parent view, set to the host view, see
11596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#setParent(View)}
11606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>visibility, computed based on parent-relative bounds, see
1161049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setVisibleToUser(boolean)}
11626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>accessibility focus, computed based on internal helper state, see
1163049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setAccessibilityFocused(boolean)}
1164049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>keyboard focus, computed based on internal helper state, see
1165049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setFocused(boolean)}
11666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>bounds in screen coordinates, computed based on host view bounds,
1167049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * see {@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)}
11686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1170049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Additionally, the helper class automatically handles keyboard focus and
1171049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * accessibility focus management by adding the appropriate
1172049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_FOCUS},
1173049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_FOCUS},
1174049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS}, or
11756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS}
1176049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * actions. Implementations must <strong>never</strong> manually add these
1177049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * actions.
11786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The helper class also automatically modifies parent- and
11806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * screen-relative bounds to reflect the portion of the item visible
11816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * within its parent.
11826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
11836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewId The virtual view identifier of the item for
11846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            which to populate the node
11856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param node The node to populate
11866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
11876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract void onPopulateNodeForVirtualView(
11886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            int virtualViewId, AccessibilityNodeInfoCompat node);
11896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
11906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
119112d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * Populates an {@link AccessibilityNodeInfoCompat} with information
119212d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * about the host view.
119312d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * <p>
1194fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * The default implementation is a no-op.
1195fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     *
1196fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * @param node the node to populate with information about the host view
119712d8b3f36c7b503221185b44791822e59e690966Alan Viverette     */
1198fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    protected void onPopulateNodeForHost(AccessibilityNodeInfoCompat node) {
119912d8b3f36c7b503221185b44791822e59e690966Alan Viverette        // Default implementation is no-op.
120012d8b3f36c7b503221185b44791822e59e690966Alan Viverette    }
120112d8b3f36c7b503221185b44791822e59e690966Alan Viverette
120212d8b3f36c7b503221185b44791822e59e690966Alan Viverette    /**
12036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Performs the specified accessibility action on the item associated
12046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * with the virtual view identifier. See
12056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#performAction(int, Bundle)} for
12066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * more information.
12076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1208049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Implementations <strong>must</strong> handle any actions added manually
1209049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * in
1210049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}.
12116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
12126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The helper class automatically handles focus management resulting
12136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * from {@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS}
12146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * and
12156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS}
12166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * actions.
12176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
12186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewId The virtual view identifier of the item on which
12196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            to perform the action
12206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param action The accessibility action to perform
12216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param arguments (Optional) A bundle with additional arguments, or
12226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            null
12236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @return true if the action was performed
12246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
12256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract boolean onPerformActionForVirtualView(
12266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            int virtualViewId, int action, Bundle arguments);
12276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
12286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
1229049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Exposes a virtual view hierarchy to the accessibility framework.
12306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
1231049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private class MyNodeProvider extends AccessibilityNodeProviderCompat {
12326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        @Override
12336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) {
12347c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            // The caller takes ownership of the node and is expected to
12357c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            // recycle it when done, so always return a copy.
12367c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            final AccessibilityNodeInfoCompat node =
12377c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette                    ExploreByTouchHelper.this.obtainAccessibilityNodeInfo(virtualViewId);
12387c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette            return AccessibilityNodeInfoCompat.obtain(node);
12396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
12406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
12416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        @Override
12426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        public boolean performAction(int virtualViewId, int action, Bundle arguments) {
12436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return ExploreByTouchHelper.this.performAction(virtualViewId, action, arguments);
12446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
12456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
12466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette}
1247