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