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