ExploreByTouchHelper.java revision 852749684a10e7d104eed8fb2dcc68b64d079340
16eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette/* 26eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Copyright (C) 2013 The Android Open Source Project 36eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 46eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Licensed under the Apache License, Version 2.0 (the "License"); 56eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * you may not use this file except in compliance with the License. 66eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * You may obtain a copy of the License at 76eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 86eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * http://www.apache.org/licenses/LICENSE-2.0 96eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Unless required by applicable law or agreed to in writing, software 116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * distributed under the License is distributed on an "AS IS" BASIS, 126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * See the License for the specific language governing permissions and 146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * limitations under the License. 156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverettepackage android.support.v4.widget; 186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.content.Context; 206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.graphics.Rect; 216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.os.Bundle; 22049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.annotation.NonNull; 23049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport android.support.annotation.Nullable; 24e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viveretteimport android.support.v4.util.SparseArrayCompat; 256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.AccessibilityDelegateCompat; 266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.ViewCompat; 277f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viveretteimport android.support.v4.view.ViewCompat.FocusDirection; 287f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viveretteimport android.support.v4.view.ViewCompat.FocusRealDirection; 296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.ViewParentCompat; 306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.support.v4.view.accessibility.AccessibilityEventCompat; 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.view.KeyEvent; 356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.MotionEvent; 366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.View; 376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.ViewParent; 386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.accessibility.AccessibilityEvent; 396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport android.view.accessibility.AccessibilityManager; 4014d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikasimport android.view.accessibility.AccessibilityRecord; 416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 42049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viveretteimport java.util.ArrayList; 436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viveretteimport java.util.List; 446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette/** 466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ExploreByTouchHelper is a utility class for implementing accessibility 476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * support in custom {@link View}s that represent a collection of View-like 486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * logical items. It extends {@link AccessibilityNodeProviderCompat} and 496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * simplifies many aspects of providing information to accessibility services 5067f879143b4b6d4bb07a90437df66174791781e8Zach Kuznia * and managing accessibility focus. 516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 5212d8b3f36c7b503221185b44791822e59e690966Alan Viverette * Clients should override abstract methods on this class and attach it to the 5312d8b3f36c7b503221185b44791822e59e690966Alan Viverette * host view using {@link ViewCompat#setAccessibilityDelegate}: 54049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p> 556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <pre> 56049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * class MyCustomView extends View { 57049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * private MyVirtualViewHelper mVirtualViewHelper; 58049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 59049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * public MyCustomView(Context context, ...) { 60049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * ... 61049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * mVirtualViewHelper = new MyVirtualViewHelper(this); 62049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * ViewCompat.setAccessibilityDelegate(this, mVirtualViewHelper); 63049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 64049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 65049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @Override 66049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * public boolean dispatchHoverEvent(MotionEvent event) { 67049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * return mHelper.dispatchHoverEvent(this, event) 68049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * || super.dispatchHoverEvent(event); 69049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 70049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 71049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @Override 72049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * public boolean dispatchKeyEvent(KeyEvent event) { 73049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * return mHelper.dispatchKeyEvent(event) 74049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * || super.dispatchKeyEvent(event); 75049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 76049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 77049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @Override 78049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * public boolean onFocusChanged(boolean gainFocus, int direction, 79049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Rect previouslyFocusedRect) { 80049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 81049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 82049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 83049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 8412d8b3f36c7b503221185b44791822e59e690966Alan Viverette * mAccessHelper = new MyExploreByTouchHelper(someView); 856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ViewCompat.setAccessibilityDelegate(someView, mAccessHelper); 866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </pre> 876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverettepublic abstract class ExploreByTouchHelper extends AccessibilityDelegateCompat { 896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** Virtual node identifier value for invalid nodes. */ 906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette public static final int INVALID_ID = Integer.MIN_VALUE; 916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9212d8b3f36c7b503221185b44791822e59e690966Alan Viverette /** Virtual node identifier value for the host view's node. */ 9312d8b3f36c7b503221185b44791822e59e690966Alan Viverette public static final int HOST_ID = View.NO_ID; 9412d8b3f36c7b503221185b44791822e59e690966Alan Viverette 956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** Default class name used for virtual views. */ 96049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private static final String DEFAULT_CLASS_NAME = "android.view.View"; 976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 98cc35612e283564c245408da4bb9264f38a02487aAlan Viverette /** Default bounds used to determine if the client didn't set any. */ 99cc35612e283564c245408da4bb9264f38a02487aAlan Viverette private static final Rect INVALID_PARENT_BOUNDS = new Rect( 100cc35612e283564c245408da4bb9264f38a02487aAlan Viverette Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE); 101cc35612e283564c245408da4bb9264f38a02487aAlan Viverette 1026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Temporary, reusable data structures. 1036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private final Rect mTempScreenRect = new Rect(); 1046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private final Rect mTempParentRect = new Rect(); 1056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private final Rect mTempVisibleRect = new Rect(); 1066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private final int[] mTempGlobalRect = new int[2]; 1076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 1086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** System accessibility manager, used to check state and send events. */ 1096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private final AccessibilityManager mManager; 1106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 1116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** View whose internal structure is exposed through this helper. */ 112049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private final View mHost; 1136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 114049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** Virtual node provider used to expose logical structure to services. */ 115049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private MyNodeProvider mNodeProvider; 1166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 117049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** Identifier for the virtual view that holds accessibility focus. */ 118049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private int mAccessibilityFocusedVirtualViewId = INVALID_ID; 1196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 120049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** Identifier for the virtual view that holds keyboard focus. */ 121049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private int mKeyboardFocusedVirtualViewId = INVALID_ID; 122049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 123049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** Identifier for the virtual view that is currently hovered. */ 1246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private int mHoveredVirtualViewId = INVALID_ID; 1256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 1266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 127049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Constructs a new helper that can expose a virtual view hierarchy for the 128049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * specified host view. 1296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 130049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param host view whose virtual view hierarchy is exposed by this helper 1316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 132852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton public ExploreByTouchHelper(@NonNull View host) { 133049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (host == null) { 1346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette throw new IllegalArgumentException("View may not be null"); 1356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 137049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mHost = host; 138049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 139049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final Context context = host.getContext(); 1406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette mManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); 141049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 142049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Host view must be focusable so that we can delegate to virtual 143049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // views. 144049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette host.setFocusable(true); 1457f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette if (ViewCompat.getImportantForAccessibility(host) 1467f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { 1477f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette ViewCompat.setImportantForAccessibility( 1487f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette host, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES); 149049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 1526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette @Override 1536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette public AccessibilityNodeProviderCompat getAccessibilityNodeProvider(View host) { 1546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if (mNodeProvider == null) { 155049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mNodeProvider = new MyNodeProvider(); 1566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return mNodeProvider; 1586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 1606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 161049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Delegates hover events from the host view. 162049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p> 1636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Dispatches hover {@link MotionEvent}s to the virtual view hierarchy when 1646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * the Explore by Touch feature is enabled. 1656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 166049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * This method should be called by overriding the host view's 167049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link View#dispatchHoverEvent(MotionEvent)} method: 1686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <pre>@Override 1696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * public boolean dispatchHoverEvent(MotionEvent event) { 170049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * return mHelper.dispatchHoverEvent(this, event) 171049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * || super.dispatchHoverEvent(event); 1726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * } 1736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </pre> 1746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 1756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param event The hover event to dispatch to the virtual view hierarchy. 1766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @return Whether the hover event was handled. 1776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 178049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final boolean dispatchHoverEvent(@NonNull MotionEvent event) { 1795bdd71cc24714c1687c2a5e2d5f3d55e5e1497e4Aurimas Liutikas if (!mManager.isEnabled() || !mManager.isTouchExplorationEnabled()) { 1806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 1816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 1836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette switch (event.getAction()) { 1846ed40c1f86bcb172a1f0f069cde1c571a7781aeeAurimas Liutikas case MotionEvent.ACTION_HOVER_MOVE: 1856ed40c1f86bcb172a1f0f069cde1c571a7781aeeAurimas Liutikas case MotionEvent.ACTION_HOVER_ENTER: 1866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final int virtualViewId = getVirtualViewAt(event.getX(), event.getY()); 1876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette updateHoveredVirtualView(virtualViewId); 1886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return (virtualViewId != INVALID_ID); 1896ed40c1f86bcb172a1f0f069cde1c571a7781aeeAurimas Liutikas case MotionEvent.ACTION_HOVER_EXIT: 190049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mAccessibilityFocusedVirtualViewId != INVALID_ID) { 1916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette updateHoveredVirtualView(INVALID_ID); 1926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return true; 1936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 1956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette default: 1966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 1976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 2006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 201049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Delegates key events from the host view. 202049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p> 203049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * This method should be called by overriding the host view's 204049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link View#dispatchKeyEvent(KeyEvent)} method: 205049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <pre>@Override 206049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * public boolean dispatchKeyEvent(KeyEvent event) { 207049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * return mHelper.dispatchKeyEvent(event) 208049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * || super.dispatchKeyEvent(event); 209049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 210049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * </pre> 211049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 212049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final boolean dispatchKeyEvent(@NonNull KeyEvent event) { 213049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette boolean handled = false; 214049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 215049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int action = event.getAction(); 216049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (action != KeyEvent.ACTION_UP) { 217049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int keyCode = event.getKeyCode(); 218049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette switch (keyCode) { 219049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_LEFT: 220049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_UP: 221049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_RIGHT: 222049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_DOWN: 2239c3a43f2608c2e7fceea46e4b500d68ce1c619ffAurimas Liutikas if (event.hasNoModifiers()) { 224049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int direction = keyToDirection(keyCode); 225049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int count = 1 + event.getRepeatCount(); 226049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette for (int i = 0; i < count; i++) { 227049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (moveFocus(direction, null)) { 228049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette handled = true; 229049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } else { 230049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 231049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 232049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 233049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 234049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 235049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_CENTER: 236049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_ENTER: 2379c3a43f2608c2e7fceea46e4b500d68ce1c619ffAurimas Liutikas if (event.hasNoModifiers()) { 238049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (event.getRepeatCount() == 0) { 239049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette clickKeyboardFocusedVirtualView(); 240049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette handled = true; 241049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 242049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 243049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 244049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_TAB: 2459c3a43f2608c2e7fceea46e4b500d68ce1c619ffAurimas Liutikas if (event.hasNoModifiers()) { 246049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette handled = moveFocus(View.FOCUS_FORWARD, null); 2479c3a43f2608c2e7fceea46e4b500d68ce1c619ffAurimas Liutikas } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) { 248049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette handled = moveFocus(View.FOCUS_BACKWARD, null); 249049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 250049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 251049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 252049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 253049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 254049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return handled; 255049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 256049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 257049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 258049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Delegates focus changes from the host view. 259049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p> 260049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * This method should be called by overriding the host view's 261049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link View#onFocusChanged(boolean, int, Rect)} method: 262049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <pre>@Override 263049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * public boolean onFocusChanged(boolean gainFocus, int direction, 264049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Rect previouslyFocusedRect) { 265049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 266049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect); 267049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * } 268049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * </pre> 269049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 270049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final void onFocusChanged(boolean gainFocus, int direction, 271049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @Nullable Rect previouslyFocusedRect) { 272049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mKeyboardFocusedVirtualViewId != INVALID_ID) { 273049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette clearKeyboardFocusForVirtualView(mKeyboardFocusedVirtualViewId); 274049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 275049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 276049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (gainFocus) { 277049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette moveFocus(direction, previouslyFocusedRect); 278049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 279049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 280049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 281049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 282049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return the identifier of the virtual view that has accessibility focus 283049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * or {@link #INVALID_ID} if no virtual view has accessibility 284049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * focus 285049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 286049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final int getAccessibilityFocusedVirtualViewId() { 287049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return mAccessibilityFocusedVirtualViewId; 288049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 289049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 290049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 291049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return the identifier of the virtual view that has keyboard focus 292049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * or {@link #INVALID_ID} if no virtual view has keyboard focus 293049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 294049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final int getKeyboardFocusedVirtualViewId() { 295049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return mKeyboardFocusedVirtualViewId; 296049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 297049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 298049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 299049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Maps key event codes to focus directions. 300049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 301049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param keyCode the key event code 302049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return the corresponding focus direction 303049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 304049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @FocusRealDirection 305049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private static int keyToDirection(int keyCode) { 306049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette switch (keyCode) { 307049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_LEFT: 308049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return View.FOCUS_LEFT; 309049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_UP: 310049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return View.FOCUS_UP; 311049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case KeyEvent.KEYCODE_DPAD_RIGHT: 312049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return View.FOCUS_RIGHT; 313049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette default: 314049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return View.FOCUS_DOWN; 315049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 316049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 317049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 318049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 319049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Obtains the bounds for the specified virtual view. 320049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 321049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view 322049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param outBounds the rect to populate with virtual view bounds 323049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 324049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private void getBoundsInParent(int virtualViewId, Rect outBounds) { 325049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final AccessibilityNodeInfoCompat node = obtainAccessibilityNodeInfo(virtualViewId); 326049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.getBoundsInParent(outBounds); 327049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 328049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 329049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 330049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Adapts AccessibilityNodeInfoCompat for obtaining bounds. 331049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 332049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private static final FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat> NODE_ADAPTER = 333049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette new FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat>() { 334049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @Override 335049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public void obtainBounds(AccessibilityNodeInfoCompat node, Rect outBounds) { 336049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.getBoundsInParent(outBounds); 337049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 338049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette }; 339049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 340049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 341e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette * Adapts SparseArrayCompat for iterating through values. 342049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 343e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette private static final FocusStrategy.CollectionAdapter<SparseArrayCompat< 344e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat> SPARSE_VALUES_ADAPTER = 345e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette new FocusStrategy.CollectionAdapter<SparseArrayCompat< 346e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat>() { 347049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @Override 348049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public AccessibilityNodeInfoCompat get( 349e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette SparseArrayCompat<AccessibilityNodeInfoCompat> collection, int index) { 350049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return collection.valueAt(index); 351049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 352049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 353049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @Override 354e69072d0197b6360882acd76d4f0271727cfe5bcAlan Viverette public int size(SparseArrayCompat<AccessibilityNodeInfoCompat> collection) { 355049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return collection.size(); 356049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 357049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette }; 358049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 359049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 360049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Attempts to move keyboard focus in the specified direction. 361049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 362049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param direction the direction in which to move keyboard focus 363049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param previouslyFocusedRect the bounds of the previously focused item, 364049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * or {@code null} if not available 365049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return {@code true} if keyboard focus moved to a virtual view managed 366049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * by this helper, or {@code false} otherwise 367049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 368049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private boolean moveFocus(@FocusDirection int direction, @Nullable Rect previouslyFocusedRect) { 3691b327048091c92b5f46981792930aba4ab122c30Alan Viverette final SparseArrayCompat<AccessibilityNodeInfoCompat> allNodes = getAllNodes(); 3701b327048091c92b5f46981792930aba4ab122c30Alan Viverette 371049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int focusedNodeId = mKeyboardFocusedVirtualViewId; 372049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final AccessibilityNodeInfoCompat focusedNode = 3731b327048091c92b5f46981792930aba4ab122c30Alan Viverette focusedNodeId == INVALID_ID ? null : allNodes.get(focusedNodeId); 374049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 375049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final AccessibilityNodeInfoCompat nextFocusedNode; 376049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette switch (direction) { 377049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_FORWARD: 378049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_BACKWARD: 379049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final boolean isLayoutRtl = 380049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette ViewCompat.getLayoutDirection(mHost) == ViewCompat.LAYOUT_DIRECTION_RTL; 3811b327048091c92b5f46981792930aba4ab122c30Alan Viverette nextFocusedNode = FocusStrategy.findNextFocusInRelativeDirection(allNodes, 382049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette SPARSE_VALUES_ADAPTER, NODE_ADAPTER, focusedNode, direction, isLayoutRtl, 383049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette false); 384049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 385049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_LEFT: 386049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_UP: 387049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_RIGHT: 388049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_DOWN: 389049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final Rect selectedRect = new Rect(); 390049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mKeyboardFocusedVirtualViewId != INVALID_ID) { 391049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Focus is moving from a virtual view within the host. 392049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette getBoundsInParent(mKeyboardFocusedVirtualViewId, selectedRect); 393049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } else if (previouslyFocusedRect != null) { 394049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Focus is moving from a real view outside the host. 395049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette selectedRect.set(previouslyFocusedRect); 396049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } else { 397049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Focus is moving from... somewhere? Make a guess. 398049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Usually this happens when another view was too lazy 399049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // to pass the previously focused rect (ex. ScrollView 400049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // when moving UP or DOWN). 401049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette guessPreviouslyFocusedRect(mHost, direction, selectedRect); 402049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 4031b327048091c92b5f46981792930aba4ab122c30Alan Viverette nextFocusedNode = FocusStrategy.findNextFocusInAbsoluteDirection(allNodes, 404049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette SPARSE_VALUES_ADAPTER, NODE_ADAPTER, focusedNode, selectedRect, direction); 405049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 406049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette default: 407049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette throw new IllegalArgumentException("direction must be one of " 408049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette + "{FOCUS_FORWARD, FOCUS_BACKWARD, FOCUS_UP, FOCUS_DOWN, " 409049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette + "FOCUS_LEFT, FOCUS_RIGHT}."); 410049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 411049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 412049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int nextFocusedNodeId; 413049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (nextFocusedNode == null) { 414049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette nextFocusedNodeId = INVALID_ID; 415049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } else { 4161b327048091c92b5f46981792930aba4ab122c30Alan Viverette final int index = allNodes.indexOfValue(nextFocusedNode); 4171b327048091c92b5f46981792930aba4ab122c30Alan Viverette nextFocusedNodeId = allNodes.keyAt(index); 418049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 419049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 420049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return requestKeyboardFocusForVirtualView(nextFocusedNodeId); 421049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 422049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 4231b327048091c92b5f46981792930aba4ab122c30Alan Viverette private SparseArrayCompat<AccessibilityNodeInfoCompat> getAllNodes() { 4241b327048091c92b5f46981792930aba4ab122c30Alan Viverette final List<Integer> virtualViewIds = new ArrayList<>(); 4251b327048091c92b5f46981792930aba4ab122c30Alan Viverette getVisibleVirtualViews(virtualViewIds); 4261b327048091c92b5f46981792930aba4ab122c30Alan Viverette 4271b327048091c92b5f46981792930aba4ab122c30Alan Viverette final SparseArrayCompat<AccessibilityNodeInfoCompat> allNodes = new SparseArrayCompat<>(); 4281b327048091c92b5f46981792930aba4ab122c30Alan Viverette for (int virtualViewId = 0; virtualViewId < virtualViewIds.size(); virtualViewId++) { 4291b327048091c92b5f46981792930aba4ab122c30Alan Viverette final AccessibilityNodeInfoCompat virtualView = createNodeForChild(virtualViewId); 4301b327048091c92b5f46981792930aba4ab122c30Alan Viverette allNodes.put(virtualViewId, virtualView); 4311b327048091c92b5f46981792930aba4ab122c30Alan Viverette } 4321b327048091c92b5f46981792930aba4ab122c30Alan Viverette 4331b327048091c92b5f46981792930aba4ab122c30Alan Viverette return allNodes; 4341b327048091c92b5f46981792930aba4ab122c30Alan Viverette } 4351b327048091c92b5f46981792930aba4ab122c30Alan Viverette 436049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 437049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Obtains a best guess for the previously focused rect for keyboard focus 438049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * moving in the specified direction. 439049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 440049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param host the view into which focus is moving 441049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param direction the absolute direction in which focus is moving 442049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param outBounds the rect to populate with the best-guess bounds for the 443049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * previous focus rect 444049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 445049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private static Rect guessPreviouslyFocusedRect(@NonNull View host, 446049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @FocusRealDirection int direction, @NonNull Rect outBounds) { 447049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int w = host.getWidth(); 448049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final int h = host.getHeight(); 449049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 450049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette switch (direction) { 451049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_LEFT: 452049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette outBounds.set(w, 0, w, h); 453049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 454049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_UP: 455049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette outBounds.set(0, h, w, h); 456049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 457049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_RIGHT: 458049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette outBounds.set(-1, 0, -1, h); 459049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 460049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case View.FOCUS_DOWN: 461049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette outBounds.set(0, -1, w, -1); 462049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette break; 463049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette default: 464049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette throw new IllegalArgumentException("direction must be one of " 465049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}."); 466049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 467049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 468049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return outBounds; 469049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 470049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 471049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 472049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Performs a click action on the keyboard focused virtual view, if any. 473049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 474049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return {@code true} if the click action was performed successfully or 475049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@code false} otherwise 476049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 477049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private boolean clickKeyboardFocusedVirtualView() { 478049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return mKeyboardFocusedVirtualViewId != INVALID_ID && onPerformActionForVirtualView( 479049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mKeyboardFocusedVirtualViewId, AccessibilityNodeInfoCompat.ACTION_CLICK, null); 480049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 481049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 482049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 4836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Populates an event of the specified type with information about an item 4846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * and attempts to send it up through the view hierarchy. 4856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 4866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * You should call this method after performing a user action that normally 4876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * fires an accessibility event, such as clicking on an item. 488049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p> 4896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <pre>public void performItemClick(T item) { 4906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * ... 4916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * sendEventForVirtualViewId(item.id, AccessibilityEvent.TYPE_VIEW_CLICKED); 4926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * } 4936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </pre> 4946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 495049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view for which to 496049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * send an event 497049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param eventType the type of event to send 498049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return {@code true} if the event was sent successfully, {@code false} 499049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * otherwise 5006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 501049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final boolean sendEventForVirtualView(int virtualViewId, int eventType) { 5026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if ((virtualViewId == INVALID_ID) || !mManager.isEnabled()) { 5036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 5046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 5056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 506049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final ViewParent parent = mHost.getParent(); 5076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if (parent == null) { 5086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 5096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 5106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 5116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final AccessibilityEvent event = createEvent(virtualViewId, eventType); 512049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return ViewParentCompat.requestSendAccessibilityEvent(parent, mHost, event); 5136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 5146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 5156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 5166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Notifies the accessibility framework that the properties of the parent 5176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * view have changed. 5186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 519049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * You <strong>must</strong> call this method after adding or removing 520049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * items from the parent view. 5216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 522049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final void invalidateRoot() { 523722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette invalidateVirtualView(HOST_ID, AccessibilityEventCompat.CONTENT_CHANGE_TYPE_SUBTREE); 5246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 5256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 5266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 5276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Notifies the accessibility framework that the properties of a particular 5286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * item have changed. 5296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 530049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * You <strong>must</strong> call this method after changing any of the 531049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * properties set in 532049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}. 5336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 534049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view id to invalidate, or 535049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #HOST_ID} to invalidate the root view 536722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * @see #invalidateVirtualView(int, int) 5376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 538049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final void invalidateVirtualView(int virtualViewId) { 539722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette invalidateVirtualView(virtualViewId, 540722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED); 541722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette } 542722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette 543722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette /** 544722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * Notifies the accessibility framework that the properties of a particular 545722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * item have changed. 546722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * <p> 547049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * You <strong>must</strong> call this method after changing any of the 548049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * properties set in 549049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}. 550722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * 551049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view id to invalidate, or 552049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #HOST_ID} to invalidate the root view 553049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param changeTypes the bit mask of change types. May be {@code 0} for the 554722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * default (undefined) change type or one or more of: 555722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * <ul> 556722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION} 557722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_SUBTREE} 558722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_TEXT} 559722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * <li>{@link AccessibilityEventCompat#CONTENT_CHANGE_TYPE_UNDEFINED} 560722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette * </ul> 561722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette */ 562049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final void invalidateVirtualView(int virtualViewId, int changeTypes) { 563722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette if (virtualViewId != INVALID_ID && mManager.isEnabled()) { 564049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final ViewParent parent = mHost.getParent(); 565722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette if (parent != null) { 566049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Send events up the hierarchy so they can be coalesced. 567722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette final AccessibilityEvent event = createEvent(virtualViewId, 568722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED); 569722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette AccessibilityEventCompat.setContentChangeTypes(event, changeTypes); 570049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette ViewParentCompat.requestSendAccessibilityEvent(parent, mHost, event); 571722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette } 572722514df962c62eee53f82df927b8969bc5ceb95Alan Viverette } 5736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 5746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 5756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 576049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Returns the virtual view ID for the currently accessibility focused 577049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * item. 578148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette * 579049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return the identifier of the virtual view that has accessibility focus 580049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * or {@link #INVALID_ID} if no virtual view has accessibility 581049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * focus 582049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @deprecated Use {@link #getAccessibilityFocusedVirtualViewId()}. 583148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette */ 584049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @Deprecated 585148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette public int getFocusedVirtualView() { 586049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return getAccessibilityFocusedVirtualViewId(); 587049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 588049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 589049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 590049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Called when the focus state of a virtual view changes. 591049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 592049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view identifier 593049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param hasFocus {@code true} if the view has focus, {@code false} 594049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * otherwise 595049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 596049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette protected void onVirtualViewKeyboardFocusChanged(int virtualViewId, boolean hasFocus) { 597049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Stub method. 598148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette } 599148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette 600148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette /** 6016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Sets the currently hovered item, sending hover accessibility events as 6026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * necessary to maintain the correct state. 6036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 604049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view id for the item currently being 605049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * hovered, or {@link #INVALID_ID} if no item is 606049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * hovered within the parent view 6076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 6086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private void updateHoveredVirtualView(int virtualViewId) { 6096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if (mHoveredVirtualViewId == virtualViewId) { 6106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return; 6116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 6126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final int previousVirtualViewId = mHoveredVirtualViewId; 6146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette mHoveredVirtualViewId = virtualViewId; 6156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Stay consistent with framework behavior by sending ENTER/EXIT pairs 6176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // in reverse order. This is accurate as of API 18. 61814d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_HOVER_ENTER); 6196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette sendEventForVirtualView( 62014d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas previousVirtualViewId, AccessibilityEvent.TYPE_VIEW_HOVER_EXIT); 6216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 6226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 6246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Constructs and returns an {@link AccessibilityEvent} for the specified 62512d8b3f36c7b503221185b44791822e59e690966Alan Viverette * virtual view id, which includes the host view ({@link #HOST_ID}). 6266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 627049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view id for the item for which to 628049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * construct an event 629049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param eventType the type of event to construct 630049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return an {@link AccessibilityEvent} populated with information about 631049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * the specified item 6326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 6336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private AccessibilityEvent createEvent(int virtualViewId, int eventType) { 6346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette switch (virtualViewId) { 63512d8b3f36c7b503221185b44791822e59e690966Alan Viverette case HOST_ID: 6366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return createEventForHost(eventType); 6376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette default: 6386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return createEventForChild(virtualViewId, eventType); 6396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 6406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 6416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 6436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Constructs and returns an {@link AccessibilityEvent} for the host node. 6446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 645049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param eventType the type of event to construct 646049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return an {@link AccessibilityEvent} populated with information about 647049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * the specified item 6486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 6496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private AccessibilityEvent createEventForHost(int eventType) { 6506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final AccessibilityEvent event = AccessibilityEvent.obtain(eventType); 6515bdd71cc24714c1687c2a5e2d5f3d55e5e1497e4Aurimas Liutikas mHost.onInitializeAccessibilityEvent(event); 6527f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette return event; 6537f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette } 6547f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette 6557f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette @Override 6567f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { 6577f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette super.onInitializeAccessibilityEvent(host, event); 658fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette 659fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette // Allow the client to populate the event. 660fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette onPopulateEventForHost(event); 6616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 6626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 6646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Constructs and returns an {@link AccessibilityEvent} populated with 6656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * information about the specified item. 6666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 667049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view id for the item for which to 668049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * construct an event 669049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param eventType the type of event to construct 670049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return an {@link AccessibilityEvent} populated with information about 671049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * the specified item 6726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 6736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private AccessibilityEvent createEventForChild(int virtualViewId, int eventType) { 6746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final AccessibilityEvent event = AccessibilityEvent.obtain(eventType); 675049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final AccessibilityNodeInfoCompat node = obtainAccessibilityNodeInfo(virtualViewId); 676049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 677049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Allow the client to override these properties, 67814d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.getText().add(node.getText()); 67914d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.setContentDescription(node.getContentDescription()); 68014d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.setScrollable(node.isScrollable()); 68114d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.setPassword(node.isPassword()); 68214d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.setEnabled(node.isEnabled()); 68314d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.setChecked(node.isChecked()); 6846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Allow the client to populate the event. 6866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette onPopulateEventForVirtualView(virtualViewId, event); 6876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Make sure the developer is following the rules. 6896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if (event.getText().isEmpty() && (event.getContentDescription() == null)) { 6906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette throw new RuntimeException("Callbacks must add text or a content description in " 6916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette + "populateEventForVirtualViewId()"); 6926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 6936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Don't allow the client to override these properties. 69514d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas event.setClassName(node.getClassName()); 69614d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas AccessibilityRecordCompat.setSource(event, mHost, virtualViewId); 697049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette event.setPackageName(mHost.getContext().getPackageName()); 6986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 6996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return event; 7006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 7016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 7026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 703049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Obtains a populated {@link AccessibilityNodeInfoCompat} for the 704049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * virtual view with the specified identifier. 705049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <p> 706049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * This method may be called with identifier {@link #HOST_ID} to obtain a 707049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * node for the host view. 7086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 709049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view for which to 710049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * construct a node 711049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return an {@link AccessibilityNodeInfoCompat} populated with information 712049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * about the specified item 7136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 714049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette @NonNull 715540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas AccessibilityNodeInfoCompat obtainAccessibilityNodeInfo(int virtualViewId) { 7161b327048091c92b5f46981792930aba4ab122c30Alan Viverette if (virtualViewId == HOST_ID) { 7171b327048091c92b5f46981792930aba4ab122c30Alan Viverette return createNodeForHost(); 7186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 719049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 7201b327048091c92b5f46981792930aba4ab122c30Alan Viverette return createNodeForChild(virtualViewId); 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 */ 7291fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette @NonNull 7306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private AccessibilityNodeInfoCompat createNodeForHost() { 7317f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette final AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain(mHost); 7327f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette ViewCompat.onInitializeAccessibilityNodeInfo(mHost, info); 73312d8b3f36c7b503221185b44791822e59e690966Alan Viverette 7346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Add the virtual descendants. 735049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final ArrayList<Integer> virtualViewIds = new ArrayList<>(); 7366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette getVisibleVirtualViews(virtualViewIds); 7377f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette 7387f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette final int realNodeCount = info.getChildCount(); 739fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette if (realNodeCount > 0 && virtualViewIds.size() > 0) { 740fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette throw new RuntimeException("Views cannot have both real and virtual children"); 741fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette } 7426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 743049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette for (int i = 0, count = virtualViewIds.size(); i < count; i++) { 7447f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette info.addChild(mHost, virtualViewIds.get(i)); 7456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 7466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 7477f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette return info; 7487f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette } 7497f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette 7507f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette @Override 7517f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { 7527f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette super.onInitializeAccessibilityNodeInfo(host, info); 7537f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette 7547f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette // Allow the client to populate the host node. 7557f62b32cd8fe966a5b1eaa850fb6595347a2564bAlan Viverette onPopulateNodeForHost(info); 7566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 7576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 7586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 7596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Constructs and returns an {@link AccessibilityNodeInfoCompat} for the 7606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * specified item. Automatically manages accessibility focus actions. 7616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 7626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Allows the implementing class to specify most node properties, but 7636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * overrides the following: 7646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 7656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setPackageName} 7666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setClassName} 7676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setParent(View)} 7686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setSource(View, int)} 7696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser} 7706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)} 7716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 7726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 7736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Uses the bounds of the parent view and the parent-relative bounding 7746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * rectangle specified by 7756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#getBoundsInParent} to automatically 7766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * update the following properties: 7776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 7786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser} 7796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>{@link AccessibilityNodeInfoCompat#setBoundsInParent} 7806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 7816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 782049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the virtual view id for item for which to construct 783049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * a node 784049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return an {@link AccessibilityNodeInfoCompat} for the specified item 7856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 7861fe6cc2f16121bc57f6a89aaa5502e47ab3e8fe9Alan Viverette @NonNull 7876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private AccessibilityNodeInfoCompat createNodeForChild(int virtualViewId) { 7886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain(); 7896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 7906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Ensure the client has good defaults. 7916eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.setEnabled(true); 792049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.setFocusable(true); 7936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.setClassName(DEFAULT_CLASS_NAME); 794cc35612e283564c245408da4bb9264f38a02487aAlan Viverette node.setBoundsInParent(INVALID_PARENT_BOUNDS); 7950906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette node.setBoundsInScreen(INVALID_PARENT_BOUNDS); 79646f49c1900fc65a3e3bc0057e28161be6230e1daZach Kuznia node.setParent(mHost); 7976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 7986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Allow the client to populate the node. 7996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette onPopulateNodeForVirtualView(virtualViewId, node); 8006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Make sure the developer is following the rules. 8026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if ((node.getText() == null) && (node.getContentDescription() == null)) { 8036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette throw new RuntimeException("Callbacks must add text or a content description in " 8046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette + "populateNodeForVirtualViewId()"); 8056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.getBoundsInParent(mTempParentRect); 808cc35612e283564c245408da4bb9264f38a02487aAlan Viverette if (mTempParentRect.equals(INVALID_PARENT_BOUNDS)) { 8096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette throw new RuntimeException("Callbacks must set parent bounds in " 8106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette + "populateNodeForVirtualViewId()"); 8116eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8126eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8136eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final int actions = node.getActions(); 8146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if ((actions & AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS) != 0) { 8156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette throw new RuntimeException("Callbacks must not add ACTION_ACCESSIBILITY_FOCUS in " 8166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette + "populateNodeForVirtualViewId()"); 8176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if ((actions & AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS) != 0) { 8196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette throw new RuntimeException("Callbacks must not add ACTION_CLEAR_ACCESSIBILITY_FOCUS in " 8206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette + "populateNodeForVirtualViewId()"); 8216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Don't allow the client to override these properties. 824049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.setPackageName(mHost.getContext().getPackageName()); 825049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.setSource(mHost, virtualViewId); 8266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Manage internal accessibility focus state. 828049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mAccessibilityFocusedVirtualViewId == virtualViewId) { 8296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.setAccessibilityFocused(true); 8306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS); 8316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } else { 8326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.setAccessibilityFocused(false); 8336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette node.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS); 8346eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 836049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Manage internal keyboard focus state. 837049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette final boolean isFocused = mKeyboardFocusedVirtualViewId == virtualViewId; 838049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (isFocused) { 839049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_FOCUS); 840049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } else if (node.isFocusable()) { 841049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.addAction(AccessibilityNodeInfoCompat.ACTION_FOCUS); 842049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 843049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette node.setFocused(isFocused); 844049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 8453b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia mHost.getLocationOnScreen(mTempGlobalRect); 8466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8470906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette // If not explicitly specified, calculate screen-relative bounds and 8480906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette // offset for scroll position based on bounds in parent. 8490906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette node.getBoundsInScreen(mTempScreenRect); 8500906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette if (mTempScreenRect.equals(INVALID_PARENT_BOUNDS)) { 8510906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette node.getBoundsInParent(mTempScreenRect); 8523b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia 8533b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia // If there is a parent node, adjust bounds based on the parent node. 8543b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia if (node.mParentVirtualDescendantId != HOST_ID) { 8553b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia AccessibilityNodeInfoCompat parentNode = AccessibilityNodeInfoCompat.obtain(); 8563b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia // Walk up the node tree to adjust the screen rect. 8573b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia for (int virtualDescendantId = node.mParentVirtualDescendantId; 8583b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia virtualDescendantId != HOST_ID; 8593b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia virtualDescendantId = parentNode.mParentVirtualDescendantId) { 8603b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia // Reset the values in the parent node we'll be using. 8613b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia parentNode.setParent(mHost, HOST_ID); 8623b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia parentNode.setBoundsInParent(INVALID_PARENT_BOUNDS); 8633b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia // Adjust the bounds for the parent node. 8643b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia onPopulateNodeForVirtualView(virtualDescendantId, parentNode); 8653b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia parentNode.getBoundsInParent(mTempParentRect); 8663b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia mTempScreenRect.offset(mTempParentRect.left, mTempParentRect.top); 8673b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia } 8683b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia parentNode.recycle(); 8693b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia } 8703b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia // Adjust the rect for the host view's location. 8710906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette mTempScreenRect.offset(mTempGlobalRect[0] - mHost.getScrollX(), 8720906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette mTempGlobalRect[1] - mHost.getScrollY()); 8733b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia } 8743b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia 8753b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia if (mHost.getLocalVisibleRect(mTempVisibleRect)) { 8763b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia mTempVisibleRect.offset(mTempGlobalRect[0] - mHost.getScrollX(), 8773b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia mTempGlobalRect[1] - mHost.getScrollY()); 8783236f136c34c3db9b05c69cdbaccdc0fa6203a1bAurimas Liutikas final boolean intersects = mTempScreenRect.intersect(mTempVisibleRect); 8793236f136c34c3db9b05c69cdbaccdc0fa6203a1bAurimas Liutikas if (intersects) { 8803236f136c34c3db9b05c69cdbaccdc0fa6203a1bAurimas Liutikas node.setBoundsInScreen(mTempScreenRect); 8813b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia 8823236f136c34c3db9b05c69cdbaccdc0fa6203a1bAurimas Liutikas if (isVisibleToUser(mTempScreenRect)) { 8833236f136c34c3db9b05c69cdbaccdc0fa6203a1bAurimas Liutikas node.setVisibleToUser(true); 8843236f136c34c3db9b05c69cdbaccdc0fa6203a1bAurimas Liutikas } 8853b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia } 8860906dafe25770e872a72f8abd2c044b0faef86d7Alan Viverette } 8876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 8886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return node; 8896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 891540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas boolean performAction(int virtualViewId, int action, Bundle arguments) { 8926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette switch (virtualViewId) { 89312d8b3f36c7b503221185b44791822e59e690966Alan Viverette case HOST_ID: 8946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return performActionForHost(action, arguments); 8956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette default: 8966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return performActionForChild(virtualViewId, action, arguments); 8976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 8996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private boolean performActionForHost(int action, Bundle arguments) { 901049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return ViewCompat.performAccessibilityAction(mHost, action, arguments); 9026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private boolean performActionForChild(int virtualViewId, int action, Bundle arguments) { 9056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette switch (action) { 9066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette case AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS: 907148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette return requestAccessibilityFocus(virtualViewId); 9086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette case AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS: 909148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette return clearAccessibilityFocus(virtualViewId); 910049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case AccessibilityNodeInfoCompat.ACTION_FOCUS: 911049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return requestKeyboardFocusForVirtualView(virtualViewId); 912049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette case AccessibilityNodeInfoCompat.ACTION_CLEAR_FOCUS: 913049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return clearKeyboardFocusForVirtualView(virtualViewId); 9146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette default: 915049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return onPerformActionForVirtualView(virtualViewId, action, arguments); 9166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 9206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Computes whether the specified {@link Rect} intersects with the visible 9216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * portion of its parent {@link View}. Modifies {@code localRect} to contain 9226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * only the visible portion. 9236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 924049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param localRect a rectangle in local (parent) coordinates 925049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return whether the specified {@link Rect} is visible on the screen 9266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 9273b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia private boolean isVisibleToUser(Rect localRect) { 9286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Missing or empty bounds mean this view is not visible. 9296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette if ((localRect == null) || localRect.isEmpty()) { 9306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 9316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9326eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // Attached to invisible window means this view is not visible. 934049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mHost.getWindowVisibility() != View.VISIBLE) { 9356eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 9366eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // An invisible predecessor means that this view is not visible. 939049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette ViewParent viewParent = mHost.getParent(); 9406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette while (viewParent instanceof View) { 9416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette final View view = (View) viewParent; 9425bdd71cc24714c1687c2a5e2d5f3d55e5e1497e4Aurimas Liutikas if ((view.getAlpha() <= 0) || (view.getVisibility() != View.VISIBLE)) { 9436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 9446eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette viewParent = view.getParent(); 9466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // A null parent implies the view is not visible. 9493b591130c68223aa613660aaff79e65e26b5fc06Zach Kuznia return viewParent != null; 9506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 9536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Attempts to give accessibility focus to a virtual view. 9546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 9556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * A virtual view will not actually take focus if 9566eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityManager#isEnabled()} returns false, 9576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityManager#isTouchExplorationEnabled()} returns false, 9586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * or the view already has accessibility focus. 9596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 960049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view on which to 961049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * place accessibility focus 962049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return whether this virtual view actually took accessibility focus 9636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 9646eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette private boolean requestAccessibilityFocus(int virtualViewId) { 9655bdd71cc24714c1687c2a5e2d5f3d55e5e1497e4Aurimas Liutikas if (!mManager.isEnabled() || !mManager.isTouchExplorationEnabled()) { 9666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 9676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // TODO: Check virtual view visibility. 969049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mAccessibilityFocusedVirtualViewId != virtualViewId) { 970f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette // Clear focus from the previously focused view, if applicable. 971049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mAccessibilityFocusedVirtualViewId != INVALID_ID) { 972049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette clearAccessibilityFocus(mAccessibilityFocusedVirtualViewId); 973f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette } 974f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette 975f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette // Set focus on the new view. 976049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mAccessibilityFocusedVirtualViewId = virtualViewId; 977f6a201aaad218a0ee66a9479b16b012a3fd108eaAlan Viverette 9786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette // TODO: Only invalidate virtual view bounds. 979049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mHost.invalidate(); 9806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette sendEventForVirtualView(virtualViewId, 9816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED); 9826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return true; 9836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return false; 9856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 9866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 9876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 9886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Attempts to clear accessibility focus from a virtual view. 989148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette * 990049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view from which to 991049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * clear accessibility focus 992049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return whether this virtual view actually cleared accessibility focus 9936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 994148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette private boolean clearAccessibilityFocus(int virtualViewId) { 995049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mAccessibilityFocusedVirtualViewId == virtualViewId) { 996049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mAccessibilityFocusedVirtualViewId = INVALID_ID; 997049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mHost.invalidate(); 9986eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette sendEventForVirtualView(virtualViewId, 9996eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); 1000148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette return true; 10016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 1002148d85f65c0f01be809032d2140c7df6d7275504Alan Viverette return false; 10036eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 10046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 10056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 1006049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Attempts to give keyboard focus to a virtual view. 1007049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 1008049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view on which to 1009049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * place keyboard focus 1010049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return whether this virtual view actually took keyboard focus 1011049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 1012049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final boolean requestKeyboardFocusForVirtualView(int virtualViewId) { 1013049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (!mHost.isFocused() && !mHost.requestFocus()) { 1014049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Host must have real keyboard focus. 1015049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return false; 1016049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1017049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1018049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mKeyboardFocusedVirtualViewId == virtualViewId) { 1019049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // The virtual view already has focus. 1020049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return false; 1021049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1022049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1023049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mKeyboardFocusedVirtualViewId != INVALID_ID) { 1024049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette clearKeyboardFocusForVirtualView(mKeyboardFocusedVirtualViewId); 1025049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1026049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1027049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mKeyboardFocusedVirtualViewId = virtualViewId; 1028049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1029049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette onVirtualViewKeyboardFocusChanged(virtualViewId, true); 1030049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_FOCUSED); 1031049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1032049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return true; 1033049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1034049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1035049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 1036049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Attempts to clear keyboard focus from a virtual view. 1037049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * 1038049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @param virtualViewId the identifier of the virtual view from which to 1039049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * clear keyboard focus 1040049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * @return whether this virtual view actually cleared keyboard focus 1041049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette */ 1042049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette public final boolean clearKeyboardFocusForVirtualView(int virtualViewId) { 1043049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette if (mKeyboardFocusedVirtualViewId != virtualViewId) { 1044049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // The virtual view is not focused. 1045049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return false; 1046049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1047049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1048049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette mKeyboardFocusedVirtualViewId = INVALID_ID; 1049049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1050049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette onVirtualViewKeyboardFocusChanged(virtualViewId, false); 1051049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette sendEventForVirtualView(virtualViewId, AccessibilityEvent.TYPE_VIEW_FOCUSED); 1052049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1053049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette return true; 1054049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 1055049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette 1056049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette /** 10576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Provides a mapping between view-relative coordinates and logical 10586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * items. 10596eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 10606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param x The view-relative x coordinate 10616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param y The view-relative y coordinate 10626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @return virtual view identifier for the logical item under 106312d8b3f36c7b503221185b44791822e59e690966Alan Viverette * coordinates (x,y) or {@link #HOST_ID} if there is no item at 1064cd9978cdf0c3852189fb881846db6569d43f5598Alan Viverette * the given coordinates 10656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 10666eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette protected abstract int getVirtualViewAt(float x, float y); 10676eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 10686eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 10696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Populates a list with the view's visible items. The ordering of items 10706eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * within {@code virtualViewIds} specifies order of accessibility focus 10716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * traversal. 10726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 10736eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param virtualViewIds The list to populate with visible items 10746eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 10756eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette protected abstract void getVisibleVirtualViews(List<Integer> virtualViewIds); 10766eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 10776eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 10786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Populates an {@link AccessibilityEvent} with information about the 10796eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * specified item. 10806eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 1081049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * The helper class automatically populates the following fields based on 1082049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * the values set by 1083049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}, 1084049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * but implementations may optionally override them: 10856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 1086049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>event text, see {@link AccessibilityEvent#getText()} 1087049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>content description, see 1088049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityEvent#setContentDescription(CharSequence)} 1089049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>scrollability, see {@link AccessibilityEvent#setScrollable(boolean)} 1090049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>password state, see {@link AccessibilityEvent#setPassword(boolean)} 1091049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>enabled state, see {@link AccessibilityEvent#setEnabled(boolean)} 1092049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>checked state, see {@link AccessibilityEvent#setChecked(boolean)} 10936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 10946eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 10956eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * The following required fields are automatically populated by the 10966eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * helper class and may not be overridden: 10976eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 1098049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>item class name, set to the value used in 1099049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)} 11006eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>package name, set to the package of the host view's 11016eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link Context}, see {@link AccessibilityEvent#setPackageName} 11026eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>event source, set to the host view and virtual view identifier, 110314d02ef06479168249fdfeea47bc105d05e88749Aurimas Liutikas * see {@link AccessibilityRecordCompat#setSource(AccessibilityRecord, View, int)} 11046eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 11056eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 11066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param virtualViewId The virtual view id for the item for which to 11076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * populate the event 11086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param event The event to populate 11096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 1110852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton protected void onPopulateEventForVirtualView(int virtualViewId, 1111852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton @NonNull AccessibilityEvent event) { 1112049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette // Default implementation is no-op. 1113049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette } 11146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 11156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 1116fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * Populates an {@link AccessibilityEvent} with information about the host 1117fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * view. 1118fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * <p> 1119fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * The default implementation is a no-op. 1120fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * 1121fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * @param event the event to populate with information about the host view 1122fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette */ 1123852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton protected void onPopulateEventForHost(@NonNull AccessibilityEvent event) { 1124fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette // Default implementation is no-op. 1125fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette } 1126fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette 1127fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette /** 11286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Populates an {@link AccessibilityNodeInfoCompat} with information 11296eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * about the specified item. 11306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 1131049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Implementations <strong>must</strong> populate the following required 1132049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * fields: 11336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 1134049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>event text, see 1135049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setText(CharSequence)} or 1136049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setContentDescription(CharSequence)} 11376eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>bounds in parent coordinates, see 1138049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setBoundsInParent(Rect)} 11396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 11406eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 11416eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * The helper class automatically populates the following fields with 11426eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * default values, but implementations may optionally override them: 11436eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 1144049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>enabled state, set to {@code true}, see 1145049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setEnabled(boolean)} 1146049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>keyboard focusability, set to {@code true}, see 1147049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setFocusable(boolean)} 1148049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>item class name, set to {@code android.view.View}, see 1149049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setClassName(CharSequence)} 11506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 11516eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 11526eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * The following required fields are automatically populated by the 11536eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * helper class and may not be overridden: 11546eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <ul> 11556eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>package name, identical to the package name set by 1156049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateEventForVirtualView(int, AccessibilityEvent)}, see 11576eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#setPackageName} 11586eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>node source, identical to the event source set in 1159049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateEventForVirtualView(int, AccessibilityEvent)}, see 11606eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#setSource(View, int)} 11616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>parent view, set to the host view, see 11626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#setParent(View)} 11636eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>visibility, computed based on parent-relative bounds, see 1164049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setVisibleToUser(boolean)} 11656eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>accessibility focus, computed based on internal helper state, see 1166049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setAccessibilityFocused(boolean)} 1167049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * <li>keyboard focus, computed based on internal helper state, see 1168049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#setFocused(boolean)} 11696eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <li>bounds in screen coordinates, computed based on host view bounds, 1170049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * see {@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)} 11716eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * </ul> 11726eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 1173049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Additionally, the helper class automatically handles keyboard focus and 1174049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * accessibility focus management by adding the appropriate 1175049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#ACTION_FOCUS}, 1176049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_FOCUS}, 1177049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS}, or 11786eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS} 1179049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * actions. Implementations must <strong>never</strong> manually add these 1180049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * actions. 11816eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 11826eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * The helper class also automatically modifies parent- and 11836eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * screen-relative bounds to reflect the portion of the item visible 11846eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * within its parent. 11856eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 11866eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param virtualViewId The virtual view identifier of the item for 11876eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * which to populate the node 11886eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param node The node to populate 11896eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 11906eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette protected abstract void onPopulateNodeForVirtualView( 1191852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton int virtualViewId, @NonNull AccessibilityNodeInfoCompat node); 11926eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 11936eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 119412d8b3f36c7b503221185b44791822e59e690966Alan Viverette * Populates an {@link AccessibilityNodeInfoCompat} with information 119512d8b3f36c7b503221185b44791822e59e690966Alan Viverette * about the host view. 119612d8b3f36c7b503221185b44791822e59e690966Alan Viverette * <p> 1197fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * The default implementation is a no-op. 1198fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * 1199fe107ccc9e0ee09fbf7bcb9d17cee970b10ee0cfAlan Viverette * @param node the node to populate with information about the host view 120012d8b3f36c7b503221185b44791822e59e690966Alan Viverette */ 1201852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton protected void onPopulateNodeForHost(@NonNull AccessibilityNodeInfoCompat node) { 120212d8b3f36c7b503221185b44791822e59e690966Alan Viverette // Default implementation is no-op. 120312d8b3f36c7b503221185b44791822e59e690966Alan Viverette } 120412d8b3f36c7b503221185b44791822e59e690966Alan Viverette 120512d8b3f36c7b503221185b44791822e59e690966Alan Viverette /** 12066eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * Performs the specified accessibility action on the item associated 12076eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * with the virtual view identifier. See 12086eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#performAction(int, Bundle)} for 12096eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * more information. 12106eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 1211049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Implementations <strong>must</strong> handle any actions added manually 1212049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * in 1213049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * {@link #onPopulateNodeForVirtualView(int, AccessibilityNodeInfoCompat)}. 12146eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * <p> 12156eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * The helper class automatically handles focus management resulting 12166eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * from {@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS} 12176eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * and 12186eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * {@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS} 12196eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * actions. 12206eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * 12216eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param virtualViewId The virtual view identifier of the item on which 12226eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * to perform the action 12236eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param action The accessibility action to perform 12246eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @param arguments (Optional) A bundle with additional arguments, or 12256eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * null 12266eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette * @return true if the action was performed 12276eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 12286eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette protected abstract boolean onPerformActionForVirtualView( 1229852749684a10e7d104eed8fb2dcc68b64d079340Jake Wharton int virtualViewId, int action, @Nullable Bundle arguments); 12306eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 12316eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette /** 1232049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette * Exposes a virtual view hierarchy to the accessibility framework. 12336eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette */ 1234049a692ac952be43d5fcd975ec6d394ca058c9d8Alan Viverette private class MyNodeProvider extends AccessibilityNodeProviderCompat { 1235540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas MyNodeProvider() { 1236540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas } 1237540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas 12386eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette @Override 12396eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) { 12407c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette // The caller takes ownership of the node and is expected to 12417c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette // recycle it when done, so always return a copy. 12427c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette final AccessibilityNodeInfoCompat node = 12437c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette ExploreByTouchHelper.this.obtainAccessibilityNodeInfo(virtualViewId); 12447c8264ea497e54aa1536c76e553b52d2207a50fbAlan Viverette return AccessibilityNodeInfoCompat.obtain(node); 12456eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 12466eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette 12476eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette @Override 12486eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette public boolean performAction(int virtualViewId, int action, Bundle arguments) { 12496eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette return ExploreByTouchHelper.this.performAction(virtualViewId, action, arguments); 12506eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 12515c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver 12525c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver @Override 12535c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver public AccessibilityNodeInfoCompat findFocus(int focusType) { 12545c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver int focusedId = (focusType == AccessibilityNodeInfoCompat.FOCUS_ACCESSIBILITY) ? 12555c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver mAccessibilityFocusedVirtualViewId : mKeyboardFocusedVirtualViewId; 12565c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver if (focusedId == INVALID_ID) { 12575c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver return null; 12585c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver } 12595c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver return createAccessibilityNodeInfo(focusedId); 12605c0b1da91a9f27f85113990f99f6844030e8c9d8Phil Weaver } 12616eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette } 12626eb3cdf42d5382aef6b6a6afd7c305dbc27885b9Alan Viverette} 1263