1a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar/* 2a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * Copyright (C) 2014 The Android Open Source Project 3a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * 4a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 5a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * you may not use this file except in compliance with the License. 6a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * You may obtain a copy of the License at 7a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * 8a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 9a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * 10a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * Unless required by applicable law or agreed to in writing, software 11a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 12a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * See the License for the specific language governing permissions and 14a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * limitations under the License. 15a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar */ 16a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 17a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarpackage android.support.v7.widget; 18a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 19a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarimport android.os.Bundle; 20a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarimport android.support.v4.view.AccessibilityDelegateCompat; 21a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarimport android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; 22a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarimport android.view.View; 23a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarimport android.view.accessibility.AccessibilityEvent; 24a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 25a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar/** 26a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * The AccessibilityDelegate used by RecyclerView. 27a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * <p> 28a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar * This class handles basic accessibility actions and delegates them to LayoutManager. 29a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar */ 30a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyarpublic class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateCompat { 31a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar final RecyclerView mRecyclerView; 32a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 33a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 34a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar public RecyclerViewAccessibilityDelegate(RecyclerView recyclerView) { 35a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar mRecyclerView = recyclerView; 36a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 37a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 38da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar private boolean shouldIgnore() { 39da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar return mRecyclerView.hasPendingAdapterUpdates(); 40da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar } 41da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar 42a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar @Override 43a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar public boolean performAccessibilityAction(View host, int action, Bundle args) { 44a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar if (super.performAccessibilityAction(host, action, args)) { 45a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return true; 46a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 47da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 48a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return mRecyclerView.getLayoutManager().performAccessibilityAction(action, args); 49a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 50a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 51a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return false; 52a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 53a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 54a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar @Override 55a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { 56a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar super.onInitializeAccessibilityNodeInfo(host, info); 57a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar info.setClassName(RecyclerView.class.getName()); 58da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 59a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar mRecyclerView.getLayoutManager().onInitializeAccessibilityNodeInfo(info); 60a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 61a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 62a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 63a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar @Override 64a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { 65a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar super.onInitializeAccessibilityEvent(host, event); 66a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar event.setClassName(RecyclerView.class.getName()); 67da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar if (host instanceof RecyclerView && !shouldIgnore()) { 68a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar RecyclerView rv = (RecyclerView) host; 69a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar if (rv.getLayoutManager() != null) { 70a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar rv.getLayoutManager().onInitializeAccessibilityEvent(event); 71a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 72a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 73a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 74a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 75a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar AccessibilityDelegateCompat getItemDelegate() { 76a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return mItemDelegate; 77a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 78a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 79a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar final AccessibilityDelegateCompat mItemDelegate = new AccessibilityDelegateCompat() { 80a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar @Override 81a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { 82a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar super.onInitializeAccessibilityNodeInfo(host, info); 83da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 84a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar mRecyclerView.getLayoutManager(). 85a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar onInitializeAccessibilityNodeInfoForItem(host, info); 86a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 87a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 88a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar 89a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar @Override 90a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar public boolean performAccessibilityAction(View host, int action, Bundle args) { 91a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar if (super.performAccessibilityAction(host, action, args)) { 92a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return true; 93a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 94da50fde358bd3ef2edb273b3bf0265cf5e84ec6bYigit Boyar if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 95a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return mRecyclerView.getLayoutManager(). 96a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar performAccessibilityActionForItem(host, action, args); 97a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 98a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar return false; 99a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar } 100a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar }; 101a910619e83d0052e1d81aa5fe532821a2f99d76cYigit Boyar} 102