1 /* 2 * Copyright (C) 2021 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.content.res.TypedArray; 21 import android.util.AttributeSet; 22 23 import androidx.core.content.res.TypedArrayUtils; 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceCategory; 26 27 import com.android.car.ui.R; 28 import com.android.car.ui.preference.CarUiEditTextPreference; 29 import com.android.car.ui.preference.CarUiPreference; 30 31 /** 32 * {@link PreferenceCategory} which allows for CarUiPreference chevrons to be hidden for the entire 33 * category with {@link R.styleable.PreferenceGroup_showChevron} 34 */ 35 public class SettingsPreferenceCategory extends PreferenceCategory { 36 37 private final boolean mShouldShowChevron; 38 SettingsPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)39 public SettingsPreferenceCategory(Context context, AttributeSet attrs, 40 int defStyleAttr, int defStyleRes) { 41 super(context, attrs, defStyleAttr, defStyleRes); 42 43 TypedArray a = context.obtainStyledAttributes( 44 attrs, 45 R.styleable.PreferenceGroup, 46 defStyleAttr, 47 defStyleRes); 48 49 mShouldShowChevron = a.getBoolean(R.styleable.PreferenceGroup_showChevron, true); 50 a.recycle(); 51 } 52 SettingsPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr)53 public SettingsPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) { 54 this(context, attrs, defStyleAttr, 0); 55 } 56 SettingsPreferenceCategory(Context context, AttributeSet attrs)57 public SettingsPreferenceCategory(Context context, AttributeSet attrs) { 58 this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceCategoryStyle, 59 android.R.attr.preferenceCategoryStyle)); 60 } 61 SettingsPreferenceCategory(Context context)62 public SettingsPreferenceCategory(Context context) { 63 this(context, null); 64 } 65 66 @Override addPreference(Preference preference)67 public boolean addPreference(Preference preference) { 68 if (!mShouldShowChevron && (preference instanceof CarUiPreference 69 || preference instanceof CarUiEditTextPreference)) { 70 ((CarUiPreference) preference).setShowChevron(false); 71 } 72 return super.addPreference(preference); 73 } 74 } 75