1 /*
2  * Copyright (C) 2022 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 
17 package com.android.car.settings.common;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceGroup;
24 
25 /**
26  * {@link PreferenceGroup} which does not display a title, icon, or summary. This allows for
27  * logical grouping of preferences without indications in the UI.
28  *
29  * <P>Items added to this group will automatically added into the parent of this group. When the
30  * parent is set to isOrderingAsAdded the order will be preserved and items will be placed after
31  * this group and before any other already added items of the parent. The order how children are
32  * added will be also preserved.
33  *
34  * <P>Example:
35  * <ul>
36  *     <li>A</li>
37  *     <li>B</li>
38  * </ul>
39  * A.addPreference(A_1);
40  * A.addPreference(A_2);
41  *
42  * will result in
43  * <ul>
44  *     <li>A
45  *         <ul>
46  *             <li>A_1</li>
47  *             <li>A_2</li>
48  *         </ul>
49  *     </li>
50  *     <li>B</li>
51  * </ul>
52  */
53 public class LogicalRootPreferenceGroup extends LogicalPreferenceGroup {
54 
55     private int mInsertedPreferences = 0;
56 
LogicalRootPreferenceGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)57     public LogicalRootPreferenceGroup(Context context, AttributeSet attrs,
58             int defStyleAttr, int defStyleRes) {
59         super(context, attrs, defStyleAttr, defStyleRes);
60     }
61 
LogicalRootPreferenceGroup(Context context, AttributeSet attrs, int defStyleAttr)62     public LogicalRootPreferenceGroup(Context context, AttributeSet attrs, int defStyleAttr) {
63         super(context, attrs, defStyleAttr);
64     }
65 
LogicalRootPreferenceGroup(Context context, AttributeSet attrs)66     public LogicalRootPreferenceGroup(Context context, AttributeSet attrs) {
67         super(context, attrs);
68     }
69 
LogicalRootPreferenceGroup(Context context)70     public LogicalRootPreferenceGroup(Context context) {
71         super(context);
72     }
73 
74     @Override
addPreference(Preference preference)75     public boolean addPreference(Preference preference) {
76         updateShowChevron(preference);
77 
78         boolean result = getParent().addPreference(preference);
79         updateOrder(preference);
80         return result;
81     }
82 
updateOrder(Preference newPreference)83     private void updateOrder(Preference newPreference) {
84         if (!getParent().isOrderingAsAdded()) {
85             return;
86         }
87 
88         int preferenceOrder = getOrder();
89         for (int index = 0; index < getParent().getPreferenceCount(); index++) {
90             Preference preference = getParent().getPreference(index);
91             if (preference != this
92                     && preference != newPreference
93                     && preference.getOrder() > preferenceOrder + mInsertedPreferences) {
94                 preference.setOrder(preference.getOrder() + 1);
95             }
96         }
97         mInsertedPreferences++;
98         newPreference.setOrder(preferenceOrder + mInsertedPreferences);
99         notifyHierarchyChanged();
100     }
101 }
102