ExploreByTouchHelper.java revision 049a692ac952be43d5fcd975ec6d394ca058c9d8
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;
246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.AccessibilityDelegateCompat;
25049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.v4.view.KeyEventCompat;
266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.MotionEventCompat;
276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.ViewCompat;
286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.ViewParentCompat;
296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityEventCompat;
306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityManagerCompat;
316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityNodeProviderCompat;
336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityRecordCompat;
34049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.util.SparseArray;
35049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.view.KeyEvent;
366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.MotionEvent;
376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.View;
386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.ViewParent;
396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.accessibility.AccessibilityEvent;
406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.accessibility.AccessibilityManager;
416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
42049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport java.util.ArrayList;
436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport java.util.List;
446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
45049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.v4.view.ViewCompat.FocusDirection;
46049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.v4.view.ViewCompat.FocusRealDirection;
47049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
48049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport static android.support.v4.view.ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
49049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport static android.support.v4.view.ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES;
50049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette/**
526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ExploreByTouchHelper is a utility class for implementing accessibility
536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * support in custom {@link View}s that represent a collection of View-like
546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * logical items. It extends {@link AccessibilityNodeProviderCompat} and
556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * simplifies many aspects of providing information to accessibility services
566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * and managing accessibility focus. This class does not currently support
576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * hierarchies of logical items.
586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p>
5912d8b3f36c7b503221185b44791822e59e690966Alan Viverette * Clients should override abstract methods on this class and attach it to the
6012d8b3f36c7b503221185b44791822e59e690966Alan Viverette * host view using {@link ViewCompat#setAccessibilityDelegate}:
61049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p>
626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <pre>
63049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * class MyCustomView extends View {
64049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     private MyVirtualViewHelper mVirtualViewHelper;
65049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
66049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public MyCustomView(Context context, ...) {
67049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         ...
68049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         mVirtualViewHelper = new MyVirtualViewHelper(this);
69049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         ViewCompat.setAccessibilityDelegate(this, mVirtualViewHelper);
70049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
71049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
72049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     &#64;Override
73049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public boolean dispatchHoverEvent(MotionEvent event) {
74049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       return mHelper.dispatchHoverEvent(this, event)
75049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *           || super.dispatchHoverEvent(event);
76049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
77049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
78049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     &#64;Override
79049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public boolean dispatchKeyEvent(KeyEvent event) {
80049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       return mHelper.dispatchKeyEvent(event)
81049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *           || super.dispatchKeyEvent(event);
82049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
83049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *
84049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     &#64;Override
85049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     public boolean onFocusChanged(boolean gainFocus, int direction,
86049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *         Rect previouslyFocusedRect) {
87049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
88049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *       mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
89049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette *     }
90049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * }
9112d8b3f36c7b503221185b44791822e59e690966Alan Viverette * mAccessHelper = new MyExploreByTouchHelper(someView);
926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ViewCompat.setAccessibilityDelegate(someView, mAccessHelper);
936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </pre>
946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */
956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverettepublic abstract class ExploreByTouchHelper extends AccessibilityDelegateCompat {
966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** Virtual node identifier value for invalid nodes. */
976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    public static final int INVALID_ID = Integer.MIN_VALUE;
986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9912d8b3f36c7b503221185b44791822e59e690966Alan Viverette    /** Virtual node identifier value for the host view's node. */
10012d8b3f36c7b503221185b44791822e59e690966Alan Viverette    public static final int HOST_ID = View.NO_ID;
10112d8b3f36c7b503221185b44791822e59e690966Alan Viverette
1026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** Default class name used for virtual views. */
103049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static final String DEFAULT_CLASS_NAME = "android.view.View";
1046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan 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. */
112049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private final SparseArray<AccessibilityNodeInfoCompat> mCachedNodes = new SparseArray<>();
113049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** System accessibility manager, used to check state and send events. */
1156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private final AccessibilityManager mManager;
1166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /** View whose internal structure is exposed through this helper. */
118049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private final View mHost;
1196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
120049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Virtual node provider used to expose logical structure to services. */
121049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private MyNodeProvider mNodeProvider;
1226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
123049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Identifier for the virtual view that holds accessibility focus. */
124049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private int mAccessibilityFocusedVirtualViewId = INVALID_ID;
1256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
126049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Identifier for the virtual view that holds keyboard focus. */
127049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private int mKeyboardFocusedVirtualViewId = INVALID_ID;
128049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
129049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /** Identifier for the virtual view that is currently hovered. */
1306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private int mHoveredVirtualViewId = INVALID_ID;
1316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
133049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Constructs a new helper that can expose a virtual view hierarchy for the
134049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * specified host view.
1356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
136049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param host view whose virtual view hierarchy is exposed by this helper
1376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
138049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public ExploreByTouchHelper(View host) {
139049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (host == null) {
1406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new IllegalArgumentException("View may not be null");
1416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
143049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mHost = host;
144049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
145049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final Context context = host.getContext();
1466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
147049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
148049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Host view must be focusable so that we can delegate to virtual
149049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // views.
150049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        host.setFocusable(true);
151049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (ViewCompat.getImportantForAccessibility(host) == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
152049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            ViewCompat.setImportantForAccessibility(host, IMPORTANT_FOR_ACCESSIBILITY_YES);
153049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
1556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    @Override
1576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    public AccessibilityNodeProviderCompat getAccessibilityNodeProvider(View host) {
1586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (mNodeProvider == null) {
159049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mNodeProvider = new MyNodeProvider();
1606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return mNodeProvider;
1626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
1636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
165049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Delegates hover events from the host view.
166049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
1676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Dispatches hover {@link MotionEvent}s to the virtual view hierarchy when
1686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * the Explore by Touch feature is enabled.
1696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
170049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method should be called by overriding the host view's
171049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link View#dispatchHoverEvent(MotionEvent)} method:
1726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <pre>&#64;Override
1736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * public boolean dispatchHoverEvent(MotionEvent event) {
174049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   return mHelper.dispatchHoverEvent(this, event)
175049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *       || super.dispatchHoverEvent(event);
1766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * }
1776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </pre>
1786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
1796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param event The hover event to dispatch to the virtual view hierarchy.
1806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @return Whether the hover event was handled.
1816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
182049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean dispatchHoverEvent(@NonNull MotionEvent event) {
1836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (!mManager.isEnabled()
1846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                || !AccessibilityManagerCompat.isTouchExplorationEnabled(mManager)) {
1856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
1866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
1876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
1886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (event.getAction()) {
1896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case MotionEventCompat.ACTION_HOVER_MOVE:
1906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case MotionEventCompat.ACTION_HOVER_ENTER:
1916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                final int virtualViewId = getVirtualViewAt(event.getX(), event.getY());
1926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                updateHoveredVirtualView(virtualViewId);
1936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return (virtualViewId != INVALID_ID);
1946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case MotionEventCompat.ACTION_HOVER_EXIT:
195049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                if (mAccessibilityFocusedVirtualViewId != INVALID_ID) {
1966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    updateHoveredVirtualView(INVALID_ID);
1976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    return true;
1986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                }
1996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return false;
2006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
2016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return false;
2026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
2036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
2046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
2056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
206049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Delegates key events from the host view.
207049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
208049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method should be called by overriding the host view's
209049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link View#dispatchKeyEvent(KeyEvent)} method:
210049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <pre>&#64;Override
211049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * public boolean dispatchKeyEvent(KeyEvent event) {
212049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   return mHelper.dispatchKeyEvent(event)
213049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *       || super.dispatchKeyEvent(event);
214049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * }
215049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * </pre>
216049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
217049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean dispatchKeyEvent(@NonNull KeyEvent event) {
218049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        boolean handled = false;
219049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
220049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int action = event.getAction();
221049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (action != KeyEvent.ACTION_UP) {
222049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            final int keyCode = event.getKeyCode();
223049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            switch (keyCode) {
224049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_LEFT:
225049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_UP:
226049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_RIGHT:
227049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_DOWN:
228049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    if (KeyEventCompat.hasNoModifiers(event)) {
229049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        final int direction = keyToDirection(keyCode);
230049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        final int count = 1 + event.getRepeatCount();
231049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        for (int i = 0; i < count; i++) {
232049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            if (moveFocus(direction, null)) {
233049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                                handled = true;
234049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            } else {
235049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                                break;
236049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            }
237049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        }
238049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    }
239049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    break;
240049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_DPAD_CENTER:
241049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_ENTER:
242049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    if (KeyEventCompat.hasNoModifiers(event)) {
243049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        if (event.getRepeatCount() == 0) {
244049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            clickKeyboardFocusedVirtualView();
245049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                            handled = true;
246049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        }
247049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    }
248049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    break;
249049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                case KeyEvent.KEYCODE_TAB:
250049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    if (KeyEventCompat.hasNoModifiers(event)) {
251049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        handled = moveFocus(View.FOCUS_FORWARD, null);
252049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
253049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        handled = moveFocus(View.FOCUS_BACKWARD, null);
254049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    }
255049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    break;
256049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            }
257049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
258049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
259049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return handled;
260049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
261049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
262049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
263049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Delegates focus changes from the host view.
264049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
265049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method should be called by overriding the host view's
266049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link View#onFocusChanged(boolean, int, Rect)} method:
267049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <pre>&#64;Override
268049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * public boolean onFocusChanged(boolean gainFocus, int direction,
269049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *     Rect previouslyFocusedRect) {
270049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
271049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *   mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
272049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * }
273049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * </pre>
274049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
275049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void onFocusChanged(boolean gainFocus, int direction,
276049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            @Nullable Rect previouslyFocusedRect) {
277049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId != INVALID_ID) {
278049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            clearKeyboardFocusForVirtualView(mKeyboardFocusedVirtualViewId);
279049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
280049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
281049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (gainFocus) {
282049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            moveFocus(direction, previouslyFocusedRect);
283049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
284049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
285049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
286049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
287049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the identifier of the virtual view that has accessibility focus
288049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         or {@link #INVALID_ID} if no virtual view has accessibility
289049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         focus
290049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
291049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final int getAccessibilityFocusedVirtualViewId() {
292049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return mAccessibilityFocusedVirtualViewId;
293049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
294049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
295049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
296049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the identifier of the virtual view that has keyboard focus
297049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         or {@link #INVALID_ID} if no virtual view has keyboard focus
298049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
299049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final int getKeyboardFocusedVirtualViewId() {
300049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return mKeyboardFocusedVirtualViewId;
301049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
302049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
303049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
304049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Maps key event codes to focus directions.
305049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
306049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param keyCode the key event code
307049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the corresponding focus direction
308049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
309049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    @FocusRealDirection
310049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static int keyToDirection(int keyCode) {
311049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        switch (keyCode) {
312049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case KeyEvent.KEYCODE_DPAD_LEFT:
313049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_LEFT;
314049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case KeyEvent.KEYCODE_DPAD_UP:
315049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_UP;
316049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case KeyEvent.KEYCODE_DPAD_RIGHT:
317049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_RIGHT;
318049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            default:
319049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return View.FOCUS_DOWN;
320049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
321049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
322049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
323049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
324049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Obtains the bounds for the specified virtual view.
325049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
326049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view
327049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param outBounds the rect to populate with virtual view bounds
328049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
329049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private void getBoundsInParent(int virtualViewId, Rect outBounds) {
330049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node = obtainAccessibilityNodeInfo(virtualViewId);
331049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.getBoundsInParent(outBounds);
332049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
333049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
334049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
335049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Adapts AccessibilityNodeInfoCompat for obtaining bounds.
336049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
337049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static final FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat> NODE_ADAPTER =
338049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            new FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat>() {
339049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                @Override
340049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                public void obtainBounds(AccessibilityNodeInfoCompat node, Rect outBounds) {
341049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    node.getBoundsInParent(outBounds);
342049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
343049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            };
344049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
345049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
346049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Adapts SparseArray for iterating through values.
347049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
348049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static final FocusStrategy.CollectionAdapter<SparseArray<AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat> SPARSE_VALUES_ADAPTER =
349049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            new FocusStrategy.CollectionAdapter<SparseArray<AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat>() {
350049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                @Override
351049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                public AccessibilityNodeInfoCompat get(
352049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        SparseArray<AccessibilityNodeInfoCompat> collection, int index) {
353049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    return collection.valueAt(index);
354049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
355049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
356049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                @Override
357049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                public int size(SparseArray<AccessibilityNodeInfoCompat> collection) {
358049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    return collection.size();
359049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
360049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            };
361049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
362049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
363049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Attempts to move keyboard focus in the specified direction.
364049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
365049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param direction the direction in which to move keyboard focus
366049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param previouslyFocusedRect the bounds of the previously focused item,
367049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                              or {@code null} if not available
368049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return {@code true} if keyboard focus moved to a virtual view managed
369049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         by this helper, or {@code false} otherwise
370049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
371049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private boolean moveFocus(@FocusDirection int direction, @Nullable Rect previouslyFocusedRect) {
372049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int focusedNodeId = mKeyboardFocusedVirtualViewId;
373049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat focusedNode =
374049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                focusedNodeId == INVALID_ID ? null : mCachedNodes.get(focusedNodeId);
375049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
376049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat nextFocusedNode;
377049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        switch (direction) {
378049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_FORWARD:
379049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_BACKWARD:
380049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                final boolean isLayoutRtl =
381049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        ViewCompat.getLayoutDirection(mHost) == ViewCompat.LAYOUT_DIRECTION_RTL;
382049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                nextFocusedNode = FocusStrategy.findNextFocusInRelativeDirection(mCachedNodes,
383049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        SPARSE_VALUES_ADAPTER, NODE_ADAPTER, focusedNode, direction, isLayoutRtl,
384049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        false);
385049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
386049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_LEFT:
387049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_UP:
388049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_RIGHT:
389049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_DOWN:
390049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                final Rect selectedRect = new Rect();
391049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                if (mKeyboardFocusedVirtualViewId != INVALID_ID) {
392049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Focus is moving from a virtual view within the host.
393049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    getBoundsInParent(mKeyboardFocusedVirtualViewId, selectedRect);
394049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                } else if (previouslyFocusedRect != null) {
395049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Focus is moving from a real view outside the host.
396049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    selectedRect.set(previouslyFocusedRect);
397049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                } else {
398049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Focus is moving from... somewhere? Make a guess.
399049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // Usually this happens when another view was too lazy
400049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // to pass the previously focused rect (ex. ScrollView
401049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    // when moving UP or DOWN).
402049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                    guessPreviouslyFocusedRect(mHost, direction, selectedRect);
403049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                }
404049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                nextFocusedNode = FocusStrategy.findNextFocusInAbsoluteDirection(mCachedNodes,
405049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        SPARSE_VALUES_ADAPTER, NODE_ADAPTER, focusedNode, selectedRect, direction);
406049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
407049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            default:
408049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                throw new IllegalArgumentException("direction must be one of "
409049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        + "{FOCUS_FORWARD, FOCUS_BACKWARD, FOCUS_UP, FOCUS_DOWN, "
410049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        + "FOCUS_LEFT, FOCUS_RIGHT}.");
411049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
412049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
413049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int nextFocusedNodeId;
414049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (nextFocusedNode == null) {
415049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            nextFocusedNodeId = INVALID_ID;
416049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else {
417049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            final int index = mCachedNodes.indexOfValue(nextFocusedNode);
418049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            nextFocusedNodeId = mCachedNodes.keyAt(index);
419049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
420049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
421049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return requestKeyboardFocusForVirtualView(nextFocusedNodeId);
422049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
423049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
424049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
425049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Obtains a best guess for the previously focused rect for keyboard focus
426049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * moving in the specified direction.
427049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
428049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param host the view into which focus is moving
429049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param direction the absolute direction in which focus is moving
430049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param outBounds the rect to populate with the best-guess bounds for the
431049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                  previous focus rect
432049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
433049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private static Rect guessPreviouslyFocusedRect(@NonNull View host,
434049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            @FocusRealDirection int direction, @NonNull Rect outBounds) {
435049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int w = host.getWidth();
436049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int h = host.getHeight();
437049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
438049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        switch (direction) {
439049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_LEFT:
440049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(w, 0, w, h);
441049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
442049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_UP:
443049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(0, h, w, h);
444049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
445049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_RIGHT:
446049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(-1, 0, -1, h);
447049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
448049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case View.FOCUS_DOWN:
449049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                outBounds.set(0, -1, w, -1);
450049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                break;
451049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            default:
452049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                throw new IllegalArgumentException("direction must be one of "
453049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                        + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
454049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
455049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
456049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return outBounds;
457049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
458049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
459049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
460049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Performs a click action on the keyboard focused virtual view, if any.
461049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
462049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return {@code true} if the click action was performed successfully or
463049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         {@code false} otherwise
464049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
465049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private boolean clickKeyboardFocusedVirtualView() {
466049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return mKeyboardFocusedVirtualViewId != INVALID_ID && onPerformActionForVirtualView(
467049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                mKeyboardFocusedVirtualViewId, AccessibilityNodeInfoCompat.ACTION_CLICK, null);
468049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
469049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
470049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
4716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates an event of the specified type with information about an item
4726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * and attempts to send it up through the view hierarchy.
4736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
4746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * You should call this method after performing a user action that normally
4756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * fires an accessibility event, such as clicking on an item.
476049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
4776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <pre>public void performItemClick(T item) {
4786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *   ...
4796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *   sendEventForVirtualViewId(item.id, AccessibilityEvent.TYPE_VIEW_CLICKED);
4806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * }
4816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </pre>
4826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
483049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view for which to
484049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      send an event
485049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to send
486049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return {@code true} if the event was sent successfully, {@code false}
487049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         otherwise
4886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
489049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean sendEventForVirtualView(int virtualViewId, int eventType) {
4906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((virtualViewId == INVALID_ID) || !mManager.isEnabled()) {
4916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
4926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
4936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
494049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final ViewParent parent = mHost.getParent();
4956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (parent == null) {
4966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
4976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
4986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
4996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityEvent event = createEvent(virtualViewId, eventType);
500049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return ViewParentCompat.requestSendAccessibilityEvent(parent, mHost, event);
5016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
5026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
5046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Notifies the accessibility framework that the properties of the parent
5056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * view have changed.
5066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
507049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * You <strong>must</strong> call this method after adding or removing
508049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * items from the parent view.
5096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
510049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void invalidateRoot() {
511722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        invalidateVirtualView(HOST_ID, AccessibilityEventCompat.CONTENT_CHANGE_TYPE_SUBTREE);
5126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
5136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
5156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Notifies the accessibility framework that the properties of a particular
5166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * item have changed.
5176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
518049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * You <strong>must</strong> call this method after changing any of the
519049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * properties set in
520049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}.
5216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
522049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id to invalidate, or
523049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      {@link #HOST_ID} to invalidate the root view
524722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * @see #invalidateVirtualView(int, int)
5256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
526049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void invalidateVirtualView(int virtualViewId) {
527722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        invalidateVirtualView(virtualViewId,
528722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED);
529722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette    }
530722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette
531722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette    /**
532722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * Notifies the accessibility framework that the properties of a particular
533722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * item have changed.
534722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     * <p>
535049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * You <strong>must</strong> call this method after changing any of the
536049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * properties set in
537049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}.
538722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *
539049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id to invalidate, or
540049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      {@link #HOST_ID} to invalidate the root view
541049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param changeTypes the bit mask of change types. May be {@code 0} for the
542722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *                    default (undefined) change type or one or more of:
543722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <ul>
544722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION}
545722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_SUBTREE}
546722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_TEXT}
547722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_UNDEFINED}
548722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     *         </ul>
549722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette     */
550049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final void invalidateVirtualView(int virtualViewId, int changeTypes) {
551049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (virtualViewId == HOST_ID
552049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                && (changeTypes & AccessibilityEventCompat.CONTENT_CHANGE_TYPE_SUBTREE) != 0) {
553049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mCachedNodes.clear();
554049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else {
555049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mCachedNodes.remove(virtualViewId);
556049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
557049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
558722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        if (virtualViewId != INVALID_ID && mManager.isEnabled()) {
559049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            final ViewParent parent = mHost.getParent();
560722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette            if (parent != null) {
561049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                // Send events up the hierarchy so they can be coalesced.
562722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                final AccessibilityEvent event = createEvent(virtualViewId,
563722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                        AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED);
564722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette                AccessibilityEventCompat.setContentChangeTypes(event, changeTypes);
565049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                ViewParentCompat.requestSendAccessibilityEvent(parent, mHost, event);
566722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette            }
567722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette        }
5686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
5696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
5706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
571049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Returns the virtual view ID for the currently accessibility focused
572049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * item.
573148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette     *
574049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return the identifier of the virtual view that has accessibility focus
575049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         or {@link #INVALID_ID} if no virtual view has accessibility
576049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         focus
577049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @deprecated Use {@link #getAccessibilityFocusedVirtualViewId()}.
578148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette     */
579049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    @Deprecated
580148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    public int getFocusedVirtualView() {
581049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return getAccessibilityFocusedVirtualViewId();
582049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
583049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
584049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
585049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Called when the focus state of a virtual view changes.
586049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
587049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view identifier
588049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param hasFocus      {@code true} if the view has focus, {@code false}
589049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      otherwise
590049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
591049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    protected void onVirtualViewKeyboardFocusChanged(int virtualViewId, boolean hasFocus) {
592049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Stub method.
593148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    }
594148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette
595148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    /**
5966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Sets the currently hovered item, sending hover accessibility events as
5976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * necessary to maintain the correct state.
5986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
599049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for the item currently being
600049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      hovered, or {@link #INVALID_ID} if no item is
601049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      hovered within the parent view
6026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private void updateHoveredVirtualView(int virtualViewId) {
6046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (mHoveredVirtualViewId == virtualViewId) {
6056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return;
6066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
6076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int previousVirtualViewId = mHoveredVirtualViewId;
6096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mHoveredVirtualViewId = virtualViewId;
6106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Stay consistent with framework behavior by sending ENTER/EXIT pairs
6126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // in reverse order. This is accurate as of API 18.
6136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        sendEventForVirtualView(virtualViewId, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER);
6146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        sendEventForVirtualView(
6156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                previousVirtualViewId, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT);
6166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
6196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityEvent} for the specified
62012d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * virtual view id, which includes the host view ({@link #HOST_ID}).
6216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
622049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for the item for which to
623049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      construct an event
624049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to construct
625049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityEvent} populated with information about
626049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         the specified item
6276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityEvent createEvent(int virtualViewId, int eventType) {
6296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (virtualViewId) {
63012d8b3f36c7b503221185b44791822e59e690966Alan Viverette            case HOST_ID:
6316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return createEventForHost(eventType);
6326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
6336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return createEventForChild(virtualViewId, eventType);
6346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
6356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
6386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityEvent} for the host node.
6396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
640049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to construct
641049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityEvent} populated with information about
642049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         the specified item
6436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityEvent createEventForHost(int eventType) {
6456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
646049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        ViewCompat.onInitializeAccessibilityEvent(mHost, event);
647fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette
648fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        // Allow the client to populate the event.
649fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        onPopulateEventForHost(event);
650fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette
6516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return event;
6526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
6556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityEvent} populated with
6566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * information about the specified item.
6576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
658049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for the item for which to
659049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      construct an event
660049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param eventType the type of event to construct
661049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityEvent} populated with information about
662049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         the specified item
6636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
6646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityEvent createEventForChild(int virtualViewId, int eventType) {
6656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
666049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
667049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node = obtainAccessibilityNodeInfo(virtualViewId);
668049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
669049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Allow the client to override these properties,
670049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.getText().add(node.getText());
671049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setContentDescription(node.getContentDescription());
672049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setScrollable(node.isScrollable());
673049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setPassword(node.isPassword());
674049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setEnabled(node.isEnabled());
675049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setChecked(node.isChecked());
6766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Allow the client to populate the event.
6786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        onPopulateEventForVirtualView(virtualViewId, event);
6796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Make sure the developer is following the rules.
6816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (event.getText().isEmpty() && (event.getContentDescription() == null)) {
6826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must add text or a content description in "
6836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateEventForVirtualViewId()");
6846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
6856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Don't allow the client to override these properties.
687049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setClassName(node.getClassName());
688049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        record.setSource(mHost, virtualViewId);
689049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        event.setPackageName(mHost.getContext().getPackageName());
6906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return event;
6926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
6936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
6946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
695049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Obtains a populated {@link AccessibilityNodeInfoCompat} for the
696049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * virtual view with the specified identifier.
697049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <p>
698049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * This method may be called with identifier {@link #HOST_ID} to obtain a
699049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * node for the host view.
7006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
701049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view for which to
702049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      construct a node
703049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityNodeInfoCompat} populated with information
704049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *         about the specified item
7056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
706049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    @NonNull
707049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private AccessibilityNodeInfoCompat obtainAccessibilityNodeInfo(int virtualViewId) {
708049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node;
709049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final int cacheIndex = mCachedNodes.indexOfKey(virtualViewId);
710049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (cacheIndex >= 0) {
711049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node = mCachedNodes.valueAt(cacheIndex);
712049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else if (virtualViewId == HOST_ID) {
713049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node = createNodeForHost();
714049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else {
715049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node = createNodeForChild(virtualViewId);
7166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
717049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
718049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mCachedNodes.put(virtualViewId, node);
719049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
720049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return node;
7216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
7226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
7246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityNodeInfoCompat} for the
7256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * host view populated with its virtual descendants.
7266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
727049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityNodeInfoCompat} for the parent node
7286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
7296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityNodeInfoCompat createNodeForHost() {
730049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain(mHost);
731049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        ViewCompat.onInitializeAccessibilityNodeInfo(mHost, node);
732fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        final int realNodeCount = node.getChildCount();
7336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
73412d8b3f36c7b503221185b44791822e59e690966Alan Viverette        // Allow the client to populate the host node.
73512d8b3f36c7b503221185b44791822e59e690966Alan Viverette        onPopulateNodeForHost(node);
73612d8b3f36c7b503221185b44791822e59e690966Alan Viverette
7376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Add the virtual descendants.
738049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final ArrayList<Integer> virtualViewIds = new ArrayList<>();
7396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        getVisibleVirtualViews(virtualViewIds);
740fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        if (realNodeCount > 0 && virtualViewIds.size() > 0) {
741fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette            throw new RuntimeException("Views cannot have both real and virtual children");
742fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        }
7436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
744049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        for (int i = 0, count = virtualViewIds.size(); i < count; i++) {
745049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node.addChild(mHost, virtualViewIds.get(i));
7466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
7476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return node;
7496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
7506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
7526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Constructs and returns an {@link AccessibilityNodeInfoCompat} for the
7536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * specified item. Automatically manages accessibility focus actions.
7546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
7556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Allows the implementing class to specify most node properties, but
7566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * overrides the following:
7576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
7586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setPackageName}
7596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setClassName}
7606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setParent(View)}
7616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setSource(View, int)}
7626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser}
7636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)}
7646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
7656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
7666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Uses the bounds of the parent view and the parent-relative bounding
7676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * rectangle specified by
7686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#getBoundsInParent} to automatically
7696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * update the following properties:
7706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
7716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser}
7726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>{@link AccessibilityNodeInfoCompat#setBoundsInParent}
7736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
7746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
775049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the virtual view id for item for which to construct
776049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      a node
777049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return an {@link AccessibilityNodeInfoCompat} for the specified item
7786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
7796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private AccessibilityNodeInfoCompat createNodeForChild(int virtualViewId) {
7806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
7816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Ensure the client has good defaults.
7836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.setEnabled(true);
784049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setFocusable(true);
7856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.setClassName(DEFAULT_CLASS_NAME);
7866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Allow the client to populate the node.
7886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        onPopulateNodeForVirtualView(virtualViewId, node);
7896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Make sure the developer is following the rules.
7916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((node.getText() == null) && (node.getContentDescription() == null)) {
7926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must add text or a content description in "
7936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
7946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
7956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
7966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.getBoundsInParent(mTempParentRect);
7976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (mTempParentRect.isEmpty()) {
7986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must set parent bounds in "
7996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int actions = node.getActions();
8036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((actions & AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS) != 0) {
8046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must not add ACTION_ACCESSIBILITY_FOCUS in "
8056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((actions & AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS) != 0) {
8086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            throw new RuntimeException("Callbacks must not add ACTION_CLEAR_ACCESSIBILITY_FOCUS in "
8096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    + "populateNodeForVirtualViewId()");
8106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Don't allow the client to override these properties.
813049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setPackageName(mHost.getContext().getPackageName());
814049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setSource(mHost, virtualViewId);
815049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setParent(mHost);
8166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Manage internal accessibility focus state.
818049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mAccessibilityFocusedVirtualViewId == virtualViewId) {
8196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setAccessibilityFocused(true);
8206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
8216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        } else {
8226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setAccessibilityFocused(false);
8236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
8246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
826049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Manage internal keyboard focus state.
827049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        final boolean isFocused = mKeyboardFocusedVirtualViewId == virtualViewId;
828049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (isFocused) {
829049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_FOCUS);
830049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        } else if (node.isFocusable()) {
831049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            node.addAction(AccessibilityNodeInfoCompat.ACTION_FOCUS);
832049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
833049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        node.setFocused(isFocused);
834049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
8356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Set the visibility based on the parent bound.
8366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (intersectVisibleToUser(mTempParentRect)) {
8376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setVisibleToUser(true);
8386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            node.setBoundsInParent(mTempParentRect);
8396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Calculate screen-relative bound.
842049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mHost.getLocationOnScreen(mTempGlobalRect);
8436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int offsetX = mTempGlobalRect[0];
8446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        final int offsetY = mTempGlobalRect[1];
8456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mTempScreenRect.set(mTempParentRect);
8466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        mTempScreenRect.offset(offsetX, offsetY);
8476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        node.setBoundsInScreen(mTempScreenRect);
8486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return node;
8506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean performAction(int virtualViewId, int action, Bundle arguments) {
8536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (virtualViewId) {
85412d8b3f36c7b503221185b44791822e59e690966Alan Viverette            case HOST_ID:
8556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return performActionForHost(action, arguments);
8566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
8576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return performActionForChild(virtualViewId, action, arguments);
8586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean performActionForHost(int action, Bundle arguments) {
862049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return ViewCompat.performAccessibilityAction(mHost, action, arguments);
8636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean performActionForChild(int virtualViewId, int action, Bundle arguments) {
8666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        switch (action) {
8676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS:
868148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette                return requestAccessibilityFocus(virtualViewId);
8696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
870148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette                return clearAccessibilityFocus(virtualViewId);
871049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_FOCUS:
872049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return requestKeyboardFocusForVirtualView(virtualViewId);
873049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            case AccessibilityNodeInfoCompat.ACTION_CLEAR_FOCUS:
874049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return clearKeyboardFocusForVirtualView(virtualViewId);
8756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            default:
876049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                return onPerformActionForVirtualView(virtualViewId, action, arguments);
8776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
8796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
8816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Computes whether the specified {@link Rect} intersects with the visible
8826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * portion of its parent {@link View}. Modifies {@code localRect} to contain
8836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * only the visible portion.
8846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
885049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param localRect a rectangle in local (parent) coordinates
886049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether the specified {@link Rect} is visible on the screen
8876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
8886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean intersectVisibleToUser(Rect localRect) {
8896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Missing or empty bounds mean this view is not visible.
8906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if ((localRect == null) || localRect.isEmpty()) {
8916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
8926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Attached to invisible window means this view is not visible.
895049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mHost.getWindowVisibility() != View.VISIBLE) {
8966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
8976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
8986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
8996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // An invisible predecessor means that this view is not visible.
900049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        ViewParent viewParent = mHost.getParent();
9016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        while (viewParent instanceof View) {
9026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            final View view = (View) viewParent;
9036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            if ((ViewCompat.getAlpha(view) <= 0) || (view.getVisibility() != View.VISIBLE)) {
9046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                return false;
9056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            }
9066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            viewParent = view.getParent();
9076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // A null parent implies the view is not visible.
9106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (viewParent == null) {
9116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // If no portion of the parent is visible, this view is not visible.
915049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (!mHost.getLocalVisibleRect(mTempVisibleRect)) {
9166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // Check if the view intersects the visible portion of the parent.
9206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return localRect.intersect(mTempVisibleRect);
9216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
9226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
9246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Attempts to give accessibility focus to a virtual view.
9256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
9266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * A virtual view will not actually take focus if
9276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityManager#isEnabled()} returns false,
9286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityManager#isTouchExplorationEnabled()} returns false,
9296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * or the view already has accessibility focus.
9306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
931049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view on which to
932049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      place accessibility focus
933049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually took accessibility focus
9346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
9356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    private boolean requestAccessibilityFocus(int virtualViewId) {
9366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        if (!mManager.isEnabled()
9376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                || !AccessibilityManagerCompat.isTouchExplorationEnabled(mManager)) {
9386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return false;
9396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        // TODO: Check virtual view visibility.
941049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mAccessibilityFocusedVirtualViewId != virtualViewId) {
942f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette            // Clear focus from the previously focused view, if applicable.
943049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            if (mAccessibilityFocusedVirtualViewId != INVALID_ID) {
944049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette                clearAccessibilityFocus(mAccessibilityFocusedVirtualViewId);
945f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette            }
946f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette
947f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette            // Set focus on the new view.
948049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mAccessibilityFocusedVirtualViewId = virtualViewId;
949f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette
9506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            // TODO: Only invalidate virtual view bounds.
951049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mHost.invalidate();
9526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            sendEventForVirtualView(virtualViewId,
9536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
9546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return true;
9556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
9566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        return false;
9576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
9586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
9606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Attempts to clear accessibility focus from a virtual view.
961148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette     *
962049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view from which to
963049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      clear accessibility focus
964049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually cleared accessibility focus
9656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
966148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette    private boolean clearAccessibilityFocus(int virtualViewId) {
967049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mAccessibilityFocusedVirtualViewId == virtualViewId) {
968049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mAccessibilityFocusedVirtualViewId = INVALID_ID;
969049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            mHost.invalidate();
9706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            sendEventForVirtualView(virtualViewId,
9716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette                    AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
972148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette            return true;
9736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
974148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette        return false;
9756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
9766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
9776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
978049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Attempts to give keyboard focus to a virtual view.
979049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
980049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view on which to
981049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      place keyboard focus
982049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually took keyboard focus
983049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
984049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean requestKeyboardFocusForVirtualView(int virtualViewId) {
985049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (!mHost.isFocused() && !mHost.requestFocus()) {
986049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            // Host must have real keyboard focus.
987049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return false;
988049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
989049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
990049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId == virtualViewId) {
991049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            // The virtual view already has focus.
992049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return false;
993049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
994049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
995049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId != INVALID_ID) {
996049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            clearKeyboardFocusForVirtualView(mKeyboardFocusedVirtualViewId);
997049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
998049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
999049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mKeyboardFocusedVirtualViewId = virtualViewId;
1000049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1001049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        onVirtualViewKeyboardFocusChanged(virtualViewId, true);
1002049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_FOCUSED);
1003049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1004049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return true;
1005049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
1006049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1007049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
1008049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Attempts to clear keyboard focus from a virtual view.
1009049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *
1010049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @param virtualViewId the identifier of the virtual view from which to
1011049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     *                      clear keyboard focus
1012049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * @return whether this virtual view actually cleared keyboard focus
1013049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     */
1014049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    public final boolean clearKeyboardFocusForVirtualView(int virtualViewId) {
1015049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        if (mKeyboardFocusedVirtualViewId != virtualViewId) {
1016049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            // The virtual view is not focused.
1017049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return false;
1018049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        }
1019049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1020049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        mKeyboardFocusedVirtualViewId = INVALID_ID;
1021049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1022049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        onVirtualViewKeyboardFocusChanged(virtualViewId, false);
1023049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_FOCUSED);
1024049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1025049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        return true;
1026049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
1027049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette
1028049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    /**
10296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Provides a mapping between view-relative coordinates and logical
10306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * items.
10316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
10326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param x The view-relative x coordinate
10336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param y The view-relative y coordinate
10346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @return virtual view identifier for the logical item under
103512d8b3f36c7b503221185b44791822e59e690966Alan Viverette     *         coordinates (x,y) or {@link #HOST_ID} if there is no item at
1036cd9978cdf0c3852189fb881846db6569d43f5598Alan Viverette     *         the given coordinates
10376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
10386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract int getVirtualViewAt(float x, float y);
10396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
10406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
10416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates a list with the view's visible items. The ordering of items
10426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * within {@code virtualViewIds} specifies order of accessibility focus
10436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * traversal.
10446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
10456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewIds The list to populate with visible items
10466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
10476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract void getVisibleVirtualViews(List<Integer> virtualViewIds);
10486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
10496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
10506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates an {@link AccessibilityEvent} with information about the
10516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * specified item.
10526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1053049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * The helper class automatically populates the following fields based on
1054049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * the values set by
1055049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)},
1056049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * but implementations may optionally override them:
10576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1058049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>event text, see {@link AccessibilityEvent#getText()}
1059049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>content description, see
1060049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityEvent#setContentDescription(CharSequence)}
1061049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>scrollability, see {@link AccessibilityEvent#setScrollable(boolean)}
1062049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>password state, see {@link AccessibilityEvent#setPassword(boolean)}
1063049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>enabled state, see {@link AccessibilityEvent#setEnabled(boolean)}
1064049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>checked state, see {@link AccessibilityEvent#setChecked(boolean)}
10656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
10666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
10676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The following required fields are automatically populated by the
10686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * helper class and may not be overridden:
10696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1070049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>item class name, set to the value used in
1071049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}
10726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>package name, set to the package of the host view's
10736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link Context}, see {@link AccessibilityEvent#setPackageName}
10746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>event source, set to the host view and virtual view identifier,
10756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * see {@link AccessibilityRecordCompat#setSource(View, int)}
10766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
10776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
10786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewId The virtual view id for the item for which to
10796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            populate the event
10806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param event The event to populate
10816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
1082049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
1083049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette        // Default implementation is no-op.
1084049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    }
10856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
10866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
1087fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * Populates an {@link AccessibilityEvent} with information about the host
1088fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * view.
1089fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * <p>
1090fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * The default implementation is a no-op.
1091fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     *
1092fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * @param event the event to populate with information about the host view
1093fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     */
1094fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    protected void onPopulateEventForHost(AccessibilityEvent event) {
1095fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette        // Default implementation is no-op.
1096fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    }
1097fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette
1098fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    /**
10996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Populates an {@link AccessibilityNodeInfoCompat} with information
11006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * about the specified item.
11016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1102049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Implementations <strong>must</strong> populate the following required
1103049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * fields:
11046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1105049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>event text, see
1106049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setText(CharSequence)} or
1107049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setContentDescription(CharSequence)}
11086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>bounds in parent coordinates, see
1109049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setBoundsInParent(Rect)}
11106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The helper class automatically populates the following fields with
11136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * default values, but implementations may optionally override them:
11146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
1115049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>enabled state, set to {@code true}, see
1116049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setEnabled(boolean)}
1117049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>keyboard focusability, set to {@code true}, see
1118049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setFocusable(boolean)}
1119049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>item class name, set to {@code android.view.View}, see
1120049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setClassName(CharSequence)}
11216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The following required fields are automatically populated by the
11246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * helper class and may not be overridden:
11256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <ul>
11266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>package name, identical to the package name set by
1127049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateEventForVirtualView(int, AccessibilityEvent)}, see
11286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#setPackageName}
11296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>node source, identical to the event source set in
1130049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateEventForVirtualView(int, AccessibilityEvent)}, see
11316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#setSource(View, int)}
11326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>parent view, set to the host view, see
11336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#setParent(View)}
11346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>visibility, computed based on parent-relative bounds, see
1135049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setVisibleToUser(boolean)}
11366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>accessibility focus, computed based on internal helper state, see
1137049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setAccessibilityFocused(boolean)}
1138049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * <li>keyboard focus, computed based on internal helper state, see
1139049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#setFocused(boolean)}
11406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <li>bounds in screen coordinates, computed based on host view bounds,
1141049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * see {@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)}
11426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * </ul>
11436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1144049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Additionally, the helper class automatically handles keyboard focus and
1145049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * accessibility focus management by adding the appropriate
1146049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_FOCUS},
1147049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_FOCUS},
1148049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS}, or
11496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS}
1150049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * actions. Implementations must <strong>never</strong> manually add these
1151049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * actions.
11526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The helper class also automatically modifies parent- and
11546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * screen-relative bounds to reflect the portion of the item visible
11556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * within its parent.
11566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
11576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewId The virtual view identifier of the item for
11586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            which to populate the node
11596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param node The node to populate
11606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
11616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract void onPopulateNodeForVirtualView(
11626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            int virtualViewId, AccessibilityNodeInfoCompat node);
11636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
11646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
116512d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * Populates an {@link AccessibilityNodeInfoCompat} with information
116612d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * about the host view.
116712d8b3f36c7b503221185b44791822e59e690966Alan Viverette     * <p>
1168fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * The default implementation is a no-op.
1169fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     *
1170fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette     * @param node the node to populate with information about the host view
117112d8b3f36c7b503221185b44791822e59e690966Alan Viverette     */
1172fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette    protected void onPopulateNodeForHost(AccessibilityNodeInfoCompat node) {
117312d8b3f36c7b503221185b44791822e59e690966Alan Viverette        // Default implementation is no-op.
117412d8b3f36c7b503221185b44791822e59e690966Alan Viverette    }
117512d8b3f36c7b503221185b44791822e59e690966Alan Viverette
117612d8b3f36c7b503221185b44791822e59e690966Alan Viverette    /**
11776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * Performs the specified accessibility action on the item associated
11786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * with the virtual view identifier. See
11796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#performAction(int, Bundle)} for
11806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * more information.
11816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
1182049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Implementations <strong>must</strong> handle any actions added manually
1183049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * in
1184049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}.
11856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * <p>
11866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * The helper class automatically handles focus management resulting
11876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * from {@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS}
11886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * and
11896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS}
11906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * actions.
11916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *
11926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param virtualViewId The virtual view identifier of the item on which
11936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            to perform the action
11946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param action The accessibility action to perform
11956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @param arguments (Optional) A bundle with additional arguments, or
11966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     *            null
11976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     * @return true if the action was performed
11986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
11996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    protected abstract boolean onPerformActionForVirtualView(
12006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            int virtualViewId, int action, Bundle arguments);
12016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
12026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    /**
1203049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette     * Exposes a virtual view hierarchy to the accessibility framework.
12046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette     */
1205049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette    private class MyNodeProvider extends AccessibilityNodeProviderCompat {
12066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        @Override
12076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) {
1208049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette            return ExploreByTouchHelper.this.obtainAccessibilityNodeInfo(virtualViewId);
12096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
12106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette
12116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        @Override
12126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        public boolean performAction(int virtualViewId, int action, Bundle arguments) {
12136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette            return ExploreByTouchHelper.this.performAction(virtualViewId, action, arguments);
12146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette        }
12156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette    }
12166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette}
1217