1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.car.developeroptions.widget;
16 
17 import android.content.Context;
18 import android.content.res.TypedArray;
19 import android.text.TextUtils.TruncateAt;
20 import android.util.AttributeSet;
21 import android.widget.TextView;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceViewHolder;
25 
26 import com.android.car.developeroptions.R;
27 
28 /**
29  * A preference whose summary text will only span one single line.
30  */
31 public class FixedLineSummaryPreference extends Preference {
32 
33     private int mSummaryLineCount;
34 
FixedLineSummaryPreference(Context context, AttributeSet attrs)35     public FixedLineSummaryPreference(Context context, AttributeSet attrs) {
36         super(context, attrs);
37         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedLineSummaryPreference,
38                 0, 0);
39         if (a.hasValue(R.styleable.FixedLineSummaryPreference_summaryLineCount)) {
40             mSummaryLineCount = a.getInteger(
41                     R.styleable.FixedLineSummaryPreference_summaryLineCount, 1);
42         } else {
43             mSummaryLineCount = 1;
44         }
45         a.recycle();
46     }
47 
setSummaryLineCount(int count)48     public void setSummaryLineCount(int count) {
49         mSummaryLineCount = count;
50     }
51 
52     @Override
onBindViewHolder(PreferenceViewHolder holder)53     public void onBindViewHolder(PreferenceViewHolder holder) {
54         super.onBindViewHolder(holder);
55         TextView summary = (TextView) holder.findViewById(android.R.id.summary);
56         if (summary != null) {
57             summary.setMinLines(mSummaryLineCount);
58             summary.setMaxLines(mSummaryLineCount);
59             summary.setEllipsize(TruncateAt.END);
60         }
61     }
62 }
63