1 /* 2 * Copyright (C) 2020 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.graphics.drawable.Drawable; 22 import android.text.TextUtils; 23 import android.util.AttributeSet; 24 import android.view.View; 25 import android.widget.ImageView; 26 import android.widget.TextView; 27 28 import androidx.annotation.IdRes; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceViewHolder; 31 32 import com.android.car.settings.R; 33 34 /** 35 * {@link Preference} for displaying information in the header of a PreferenceScreen. 36 * Supports displaying an icon, title, and summary (description). 37 */ 38 public class EntityHeaderPreference extends Preference { 39 EntityHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40 public EntityHeaderPreference(Context context, AttributeSet attrs, 41 int defStyleAttr, int defStyleRes) { 42 super(context, attrs, defStyleAttr, defStyleRes); 43 init(context, attrs); 44 } 45 EntityHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr)46 public EntityHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr) { 47 super(context, attrs, defStyleAttr); 48 init(context, attrs); 49 } 50 EntityHeaderPreference(Context context, AttributeSet attrs)51 public EntityHeaderPreference(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 init(context, attrs); 54 } 55 EntityHeaderPreference(Context context)56 public EntityHeaderPreference(Context context) { 57 super(context); 58 init(context, /* attrs= */ null); 59 } 60 init(Context context, AttributeSet attrs)61 private void init(Context context, AttributeSet attrs) { 62 setLayoutResource(R.layout.entity_header_preference); 63 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Preference); 64 setSelectable(a.getBoolean(R.styleable.Preference_selectable, /* defValue= */ false)); 65 } 66 67 @Override onBindViewHolder(PreferenceViewHolder holder)68 public void onBindViewHolder(PreferenceViewHolder holder) { 69 super.onBindViewHolder(holder); 70 View itemView = holder.itemView; 71 setIconView(itemView, R.id.entity_header_icon, getIcon()); 72 setTextView(itemView, R.id.entity_header_title, getTitle()); 73 setTextView(itemView, R.id.entity_header_summary, getSummary()); 74 } 75 setTextView(View view, @IdRes int id, CharSequence text)76 private void setTextView(View view, @IdRes int id, CharSequence text) { 77 TextView textView = view.findViewById(id); 78 if (textView != null) { 79 textView.setText(text); 80 textView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE); 81 } 82 } 83 setIconView(View view, @IdRes int id, Drawable icon)84 private void setIconView(View view, @IdRes int id, Drawable icon) { 85 ImageView iconView = view.findViewById(id); 86 if (iconView != null) { 87 iconView.setImageDrawable(icon); 88 iconView.setVisibility(icon == null ? View.GONE : View.VISIBLE); 89 } 90 } 91 } 92