1 /*
2  * Copyright (C) 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.settings.common;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.ProgressBar;
25 import android.widget.TextView;
26 
27 import androidx.preference.PreferenceViewHolder;
28 
29 import com.android.car.settings.R;
30 import com.android.car.ui.preference.CarUiPreference;
31 
32 /**
33  * Preference which shows a progress bar. The progress bar layout shown can be changed by setting
34  * the xml layout attribute. The canonical example can be seen in
35  * {@link R.layout#progress_bar_preference}.
36  */
37 public class ProgressBarPreference extends CarUiPreference {
38 
39     private CharSequence mMinLabel;
40     private CharSequence mMaxLabel;
41 
42     private int mMin;
43     private int mMax;
44     private int mProgress;
45 
ProgressBarPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)46     public ProgressBarPreference(
47             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
48         super(context, attrs, defStyleAttr, defStyleRes);
49         init(attrs);
50     }
51 
ProgressBarPreference(Context context, AttributeSet attrs, int defStyleAttr)52     public ProgressBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
53         super(context, attrs, defStyleAttr);
54         init(attrs);
55     }
56 
ProgressBarPreference(Context context, AttributeSet attrs)57     public ProgressBarPreference(Context context, AttributeSet attrs) {
58         super(context, attrs);
59         init(attrs);
60     }
61 
ProgressBarPreference(Context context)62     public ProgressBarPreference(Context context) {
63         super(context);
64         init(/* attrs= */ null);
65     }
66 
init(AttributeSet attrs)67     private void init(AttributeSet attrs) {
68         TypedArray a = getContext().obtainStyledAttributes(attrs,
69                 R.styleable.ProgressBarPreference);
70 
71         mMin = a.getInteger(R.styleable.ProgressBarPreference_min, 0);
72         mMax = a.getInteger(R.styleable.ProgressBarPreference_max, 100);
73         mProgress = a.getInteger(R.styleable.ProgressBarPreference_progress, 0);
74         mMinLabel = a.getString(R.styleable.ProgressBarPreference_minLabel);
75         mMaxLabel = a.getString(R.styleable.ProgressBarPreference_maxLabel);
76 
77         a.recycle();
78     }
79 
80     /** Sets the min label of the progress bar. */
setMinLabel(CharSequence startLabel)81     public void setMinLabel(CharSequence startLabel) {
82         if (mMinLabel != startLabel) {
83             mMinLabel = startLabel;
84             notifyChanged();
85         }
86     }
87 
88     /** Sets the max label of the progress bar. */
setMaxLabel(CharSequence endLabel)89     public void setMaxLabel(CharSequence endLabel) {
90         if (mMaxLabel != endLabel) {
91             mMaxLabel = endLabel;
92             notifyChanged();
93         }
94     }
95 
96     /** Sets the minimum possible value of the progress bar. */
setMin(int min)97     public void setMin(int min) {
98         if (mMin != min) {
99             mMin = min;
100             notifyChanged();
101         }
102     }
103 
104     /** Sets the maximum possible value of the progress bar. */
setMax(int max)105     public void setMax(int max) {
106         if (mMax != max) {
107             mMax = max;
108             notifyChanged();
109         }
110     }
111 
112     /** Sets the current progress value of the progress bar. */
setProgress(int progress)113     public void setProgress(int progress) {
114         if (mProgress != progress) {
115             mProgress = progress;
116             notifyChanged();
117         }
118     }
119 
120     /** Returns the current progress value of the progress bar. */
getProgress()121     public int getProgress() {
122         return mProgress;
123     }
124 
125     @Override
onBindViewHolder(PreferenceViewHolder view)126     public void onBindViewHolder(PreferenceViewHolder view) {
127         super.onBindViewHolder(view);
128 
129         ProgressBar progressBar = (ProgressBar) view.findViewById(android.R.id.progress);
130         progressBar.setMin(mMin);
131         progressBar.setMax(mMax);
132         progressBar.setProgress(mProgress);
133 
134         View progressBarLabels = view.findViewById(R.id.progress_bar_labels);
135         if (TextUtils.isEmpty(mMinLabel) && TextUtils.isEmpty(mMaxLabel)) {
136             progressBarLabels.setVisibility(View.GONE);
137         } else {
138             progressBarLabels.setVisibility(View.VISIBLE);
139 
140             TextView minLabel = (TextView) view.findViewById(android.R.id.text1);
141             if (minLabel != null) {
142                 minLabel.setText(mMinLabel);
143             }
144             TextView maxLabel = (TextView) view.findViewById(android.R.id.text2);
145             if (maxLabel != null) {
146                 maxLabel.setText(mMaxLabel);
147             }
148         }
149     }
150 }
151