1/*
2 * Copyright (C) 2015 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.content.Context;
20import android.support.v4.content.res.TypedArrayUtils;
21import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
22import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat.CollectionItemInfoCompat;
23import android.util.AttributeSet;
24
25/**
26 * Used to group {@link Preference} objects and provide a disabled title above
27 * the group.
28 *
29 * <div class="special reference">
30 * <h3>Developer Guides</h3>
31 * <p>For information about building a settings UI with Preferences,
32 * read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
33 * guide.</p>
34 * </div>
35 */
36public class PreferenceCategory extends PreferenceGroup {
37    private static final String TAG = "PreferenceCategory";
38
39    public PreferenceCategory(
40            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
41        super(context, attrs, defStyleAttr, defStyleRes);
42    }
43
44    public PreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
45        this(context, attrs, defStyleAttr, 0);
46    }
47
48    public PreferenceCategory(Context context, AttributeSet attrs) {
49        this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceCategoryStyle,
50                android.R.attr.preferenceCategoryStyle));
51    }
52
53    public PreferenceCategory(Context context) {
54        this(context, null);
55    }
56
57    @Override
58    protected boolean onPrepareAddPreference(Preference preference) {
59        if (preference instanceof PreferenceCategory) {
60            throw new IllegalArgumentException(
61                    "Cannot add a " + TAG + " directly to a " + TAG);
62        }
63
64        return super.onPrepareAddPreference(preference);
65    }
66
67    @Override
68    public boolean isEnabled() {
69        return false;
70    }
71
72    @Override
73    public boolean shouldDisableDependents() {
74        return !super.isEnabled();
75    }
76
77    @Override
78    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfoCompat info) {
79        super.onInitializeAccessibilityNodeInfo(info);
80
81        CollectionItemInfoCompat existingItemInfo = info.getCollectionItemInfo();
82        if (existingItemInfo == null) {
83            return;
84        }
85
86        final CollectionItemInfoCompat newItemInfo = CollectionItemInfoCompat.obtain(
87                existingItemInfo.getRowIndex(),
88                existingItemInfo.getRowSpan(),
89                existingItemInfo.getColumnIndex(),
90                existingItemInfo.getColumnSpan(),
91                true /* heading */,
92                existingItemInfo.isSelected());
93        info.setCollectionItemInfo(newItemInfo);
94    }
95}
96