• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceCategory;
24 import androidx.preference.PreferenceGroup;
25 
26 /**
27  * A controller for Preference categories.
28  */
29 public class PreferenceCategoryController extends PreferenceController<PreferenceCategory> {
30 
PreferenceCategoryController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)31     public PreferenceCategoryController(Context context, String preferenceKey,
32             FragmentController fragmentController,
33             CarUxRestrictions uxRestrictions) {
34         super(context, preferenceKey, fragmentController, uxRestrictions);
35     }
36 
37     @Override
getPreferenceType()38     protected Class<PreferenceCategory> getPreferenceType() {
39         return PreferenceCategory.class;
40     }
41 
42     @Override
getDefaultAvailabilityStatus()43     protected int getDefaultAvailabilityStatus() {
44         if (getPreference().getPreferenceCount() == 0) {
45             return CONDITIONALLY_UNAVAILABLE;
46         }
47 
48         int count = getVisibleChildrenCount(getPreference());
49         if (count == 0) {
50             return CONDITIONALLY_UNAVAILABLE;
51         }
52 
53         return AVAILABLE;
54     }
55 
getVisibleChildrenCount(PreferenceGroup preferenceGroup)56     private int getVisibleChildrenCount(PreferenceGroup preferenceGroup) {
57         int preferenceCount = 0;
58         for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
59             Preference preference = preferenceGroup.getPreference(i);
60             if (preference instanceof PreferenceGroup) {
61                 preferenceCount += getVisibleChildrenCount((PreferenceGroup) preference);
62             } else if (preference.isVisible()) {
63                 preferenceCount++;
64             }
65         }
66         return preferenceCount;
67     }
68 }
69