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