1 /* 2 * Copyright 2019 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.ui.preference; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.util.AttributeSet; 22 23 import androidx.preference.Preference; 24 25 import com.android.car.ui.R; 26 27 /** 28 * This class extends the base {@link Preference} class. Adds the support to add a drawable icon to 29 * the preference if there is one of fragment, intent or onPreferenceClickListener set. 30 */ 31 public class CarUiPreference extends Preference { 32 33 private Context mContext; 34 private boolean mShowChevron; 35 CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)36 public CarUiPreference(Context context, AttributeSet attrs, 37 int defStyleAttr, int defStyleRes) { 38 super(context, attrs, defStyleAttr, defStyleRes); 39 init(context, attrs, defStyleAttr, defStyleRes); 40 } 41 CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr)42 public CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr) { 43 this(context, attrs, defStyleAttr, R.style.Preference_CarUi_Preference); 44 } 45 CarUiPreference(Context context, AttributeSet attrs)46 public CarUiPreference(Context context, AttributeSet attrs) { 47 this(context, attrs, R.attr.carUiPreferenceStyle); 48 } 49 CarUiPreference(Context context)50 public CarUiPreference(Context context) { 51 this(context, null); 52 } 53 init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54 public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 55 mContext = context; 56 57 TypedArray a = getContext().obtainStyledAttributes( 58 attrs, 59 R.styleable.CarUiPreference, 60 defStyleAttr, 61 defStyleRes); 62 63 mShowChevron = a.getBoolean(R.styleable.CarUiPreference_showChevron, true); 64 } 65 66 @Override onAttached()67 public void onAttached() { 68 super.onAttached(); 69 70 boolean allowChevron = mContext.getResources().getBoolean( 71 R.bool.car_ui_preference_show_chevron); 72 73 if (!allowChevron || !mShowChevron) { 74 return; 75 } 76 77 if (getOnPreferenceClickListener() != null || getIntent() != null 78 || getFragment() != null) { 79 setWidgetLayoutResource(R.layout.car_ui_preference_chevron); 80 } 81 } 82 setShowChevron(boolean showChevron)83 public void setShowChevron(boolean showChevron) { 84 mShowChevron = showChevron; 85 } 86 } 87