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.text.TextUtils; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.widget.TextView; 24 25 import androidx.annotation.IdRes; 26 import androidx.preference.PreferenceViewHolder; 27 28 import com.android.car.settings.R; 29 import com.android.car.ui.preference.CarUiPreference; 30 31 /** 32 * A Preference that allows a summary text to be on the different side of the title. 33 */ 34 public class SummaryPreference extends CarUiPreference { 35 SummaryPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)36 public SummaryPreference(Context context, AttributeSet attrs, 37 int defStyleAttr, int defStyleRes) { 38 super(context, attrs, defStyleAttr, defStyleRes); 39 init(context, attrs); 40 } 41 SummaryPreference(Context context, AttributeSet attrs, int defStyleAttr)42 public SummaryPreference(Context context, AttributeSet attrs, int defStyleAttr) { 43 super(context, attrs, defStyleAttr); 44 init(context, attrs); 45 } 46 SummaryPreference(Context context, AttributeSet attrs)47 public SummaryPreference(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 init(context, attrs); 50 } 51 SummaryPreference(Context context)52 public SummaryPreference(Context context) { 53 super(context); 54 init(context, /* attrs= */ null); 55 } 56 init(Context context, AttributeSet attrs)57 private void init(Context context, AttributeSet attrs) { 58 setLayoutResource(R.layout.summary_preference); 59 } 60 61 @Override onBindViewHolder(PreferenceViewHolder holder)62 public void onBindViewHolder(PreferenceViewHolder holder) { 63 super.onBindViewHolder(holder); 64 View itemView = holder.itemView; 65 setTextView(itemView, R.id.title, getTitle()); 66 setTextView(itemView, R.id.summary, getSummary()); 67 } 68 setTextView(View view, @IdRes int id, CharSequence text)69 private void setTextView(View view, @IdRes int id, CharSequence text) { 70 TextView textView = view.findViewById(id); 71 if (textView != null) { 72 textView.setText(text); 73 textView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE); 74 } 75 } 76 } 77