1/*
2 * Copyright (C) 2016 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.preference;
18
19import android.os.Bundle;
20import android.support.annotation.RestrictTo;
21import android.support.v4.view.AccessibilityDelegateCompat;
22import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
23import android.support.v7.widget.RecyclerView;
24import android.support.v7.widget.RecyclerViewAccessibilityDelegate;
25import android.view.View;
26import android.view.accessibility.AccessibilityEvent;
27
28import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
29
30/**
31 * The AccessibilityDelegate used by the RecyclerView that displays Views for Preferences.
32 *
33 * @hide
34 */
35@RestrictTo(GROUP_ID)
36public class PreferenceRecyclerViewAccessibilityDelegate
37        extends RecyclerViewAccessibilityDelegate {
38    final RecyclerView mRecyclerView;
39    final AccessibilityDelegateCompat mDefaultItemDelegate = super.getItemDelegate();
40
41    public PreferenceRecyclerViewAccessibilityDelegate(RecyclerView recyclerView) {
42        super(recyclerView);
43        mRecyclerView = recyclerView;
44    }
45
46    @Override
47    public AccessibilityDelegateCompat getItemDelegate() {
48        return mItemDelegate;
49    }
50
51    final AccessibilityDelegateCompat mItemDelegate = new AccessibilityDelegateCompat() {
52        @Override
53        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
54            mDefaultItemDelegate.onInitializeAccessibilityNodeInfo(host, info);
55            int position = mRecyclerView.getChildAdapterPosition(host);
56
57            RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
58            if (!(adapter instanceof PreferenceGroupAdapter)) {
59                return;
60            }
61
62            PreferenceGroupAdapter preferenceGroupAdapter = (PreferenceGroupAdapter) adapter;
63            Preference preference = preferenceGroupAdapter.getItem(position);
64            if (preference == null) {
65                return;
66            }
67
68            preference.onInitializeAccessibilityNodeInfo(info);
69        }
70
71        @Override
72        public boolean performAccessibilityAction(View host, int action, Bundle args) {
73            // Must forward actions since the default delegate will handle actions.
74            return mDefaultItemDelegate.performAccessibilityAction(host, action, args);
75        }
76    };
77}
78