blob: b86a15c17cb2908d945fbefc419551993570ce6c [file] [log] [blame]
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +02001/*
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +02002 ** Copyright 2013, The ChameleonOS Open Source Project
3 ** Copyright 2016, The OmniROM Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
micky38798506412023-10-23 16:33:47 +020017package omnirom.preference;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020018
19import android.content.Context;
20import android.content.res.TypedArray;
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +020021import android.util.TypedValue;
Vachounet41e03b62019-09-11 10:40:12 +020022import androidx.preference.Preference;
23import androidx.preference.PreferenceViewHolder;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020024import android.util.AttributeSet;
25import android.util.Log;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.ViewParent;
30import android.widget.SeekBar;
31import android.widget.SeekBar.OnSeekBarChangeListener;
32import android.widget.TextView;
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +020033
micky38798506412023-10-23 16:33:47 +020034import omnirom.preference.R;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020035
36public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
37
38 private final String TAG = getClass().getName();
39
40 private static final String ANDROIDNS = "http://schemas.android.com/apk/res/android";
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020041 private static final int DEFAULT_VALUE = 50;
42
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +020043 private int mMaxValue = 100;
44 private int mMinValue = 0;
45 private int mInterval = 1;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020046 private int mCurrentValue;
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +020047 private String mUnitsLeft = "";
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020048 private String mUnitsRight = "";
49 private SeekBar mSeekBar;
50 private TextView mStatusText;
51
52 public SeekBarPreference(Context context, AttributeSet attrs) {
53 super(context, attrs);
54 initPreference(context, attrs);
55 }
56
57 public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59 initPreference(context, attrs);
60 }
61
62 private void initPreference(Context context, AttributeSet attrs) {
Juan Ezquerro LLanesc956ca32018-06-06 20:05:53 +020063 setValuesFromXml(context, attrs);
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020064 setLayoutResource(R.layout.preference_seek_bar);
65 }
66
Juan Ezquerro LLanesc956ca32018-06-06 20:05:53 +020067 private void setValuesFromXml(Context context, AttributeSet attrs) {
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020068 mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020069
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +020070 final TypedArray attributes = context.obtainStyledAttributes(attrs,
71 R.styleable.SeekBarPreference);
72
73 TypedValue minAttr =
74 attributes.peekValue(R.styleable.SeekBarPreference_min);
75 if (minAttr != null && minAttr.type == TypedValue.TYPE_INT_DEC) {
76 mMinValue = minAttr.data;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020077 }
Juan Ezquerro LLanesc956ca32018-06-06 20:05:53 +020078
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +020079 TypedValue unitsLeftAttr =
80 attributes.peekValue(R.styleable.SeekBarPreference_unitsLeft);
81 CharSequence data = null;
82 if (unitsLeftAttr != null && unitsLeftAttr.type == TypedValue.TYPE_STRING) {
83 if (unitsLeftAttr.resourceId != 0) {
84 data = context.getText(unitsLeftAttr.resourceId);
85 } else {
86 data = unitsLeftAttr.string;
87 }
88 }
89 mUnitsLeft = (data == null) ? "" : data.toString();
90
91 TypedValue unitsRightAttr =
92 attributes.peekValue(R.styleable.SeekBarPreference_unitsRight);
93 data = null;
94 if (unitsRightAttr != null && unitsRightAttr.type == TypedValue.TYPE_STRING) {
95 if (unitsRightAttr.resourceId != 0) {
96 data = context.getText(unitsRightAttr.resourceId);
97 } else {
98 data = unitsRightAttr.string;
99 }
100 }
101 mUnitsRight = (data == null) ? "" : data.toString();
102
103 TypedValue intervalAttr =
104 attributes.peekValue(R.styleable.SeekBarPreference_interval);
105 if (intervalAttr != null && intervalAttr.type == TypedValue.TYPE_INT_DEC) {
106 mInterval = intervalAttr.data;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200107 }
108
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200109 attributes.recycle();
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200110 }
111
112 @Override
113 public void onDependencyChanged(Preference dependency, boolean disableDependent) {
114 super.onDependencyChanged(dependency, disableDependent);
115 this.setShouldDisableView(true);
116 if (mSeekBar != null) {
117 mSeekBar.setEnabled(!disableDependent);
118 }
119 }
120
121 @Override
122 public void onBindViewHolder(PreferenceViewHolder holder) {
123 super.onBindViewHolder(holder);
124
125 mSeekBar = (SeekBar) holder.findViewById(R.id.seekbar);
126 mSeekBar.setMax(mMaxValue - mMinValue);
127 mSeekBar.setProgress(mCurrentValue - mMinValue);
128 mSeekBar.setOnSeekBarChangeListener(this);
129 mSeekBar.setEnabled(isEnabled());
130
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200131 mStatusText = (TextView) holder.findViewById(R.id.seekBarPrefValue);
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200132 mStatusText.setText(String.valueOf(mCurrentValue));
133 mStatusText.setMinimumWidth(30);
134
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200135 TextView unitsRight = (TextView) holder.findViewById(R.id.seekBarPrefUnitsRight);
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200136 unitsRight.setText(mUnitsRight);
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200137 TextView unitsLeft = (TextView) holder.findViewById(R.id.seekBarPrefUnitsLeft);
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200138 unitsLeft.setText(mUnitsLeft);
139 }
140
141 @Override
142 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
143 int newValue = progress + mMinValue;
144
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200145 if (newValue > mMaxValue) {
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200146 newValue = mMaxValue;
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200147 } else if (newValue < mMinValue) {
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200148 newValue = mMinValue;
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200149 } else if (mInterval != 1 && newValue % mInterval != 0) {
150 newValue = Math.round(((float) newValue) / mInterval) * mInterval;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200151 }
152
153 // change rejected, revert to the previous value
154 if (!callChangeListener(newValue)) {
155 seekBar.setProgress(mCurrentValue - mMinValue);
156 return;
157 }
158
159 // change accepted, store it
160 mCurrentValue = newValue;
161 mStatusText.setText(String.valueOf(newValue));
162 persistInt(newValue);
163 }
164
165 @Override
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200166 public void onStartTrackingTouch(SeekBar seekBar) {
167 }
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200168
169 @Override
170 public void onStopTrackingTouch(SeekBar seekBar) {
171 notifyChanged();
172 }
173
174 @Override
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200175 protected Object onGetDefaultValue(TypedArray ta, int index) {
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200176 int defaultValue = ta.getInt(index, DEFAULT_VALUE);
177 return defaultValue;
178 }
179
180 @Override
181 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
182 if (restoreValue) {
183 mCurrentValue = getPersistedInt(mCurrentValue);
184 } else {
185 int temp = 0;
186 try {
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200187 temp = (Integer) defaultValue;
188 } catch (Exception ex) {
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200189 Log.e(TAG, "Invalid default value: " + defaultValue.toString());
190 }
191 persistInt(temp);
192 mCurrentValue = temp;
193 }
194 }
195
196 public void setValue(int value) {
197 mCurrentValue = value;
198 }
199
200 public void setMaxValue(int value) {
201 mMaxValue = value;
202 if (mSeekBar != null) {
203 mSeekBar.setMax(mMaxValue - mMinValue);
204 }
205 }
206
207 public void setMinValue(int value) {
208 mMinValue = value;
209 if (mSeekBar != null) {
210 mSeekBar.setMax(mMaxValue - mMinValue);
211 }
212 }
213
214 @Override
Juan Ezquerro LLanes4a10d472018-06-12 20:23:37 +0200215 public void setEnabled(boolean enabled) {
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +0200216 if (mSeekBar != null) {
217 mSeekBar.setEnabled(enabled);
218 }
219 super.setEnabled(enabled);
220 }
221}