Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 1 | /* |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 2 | ** 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 | */ |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame^] | 17 | package omnirom.preference; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 21 | import android.util.TypedValue; |
Vachounet | 41e03b6 | 2019-09-11 10:40:12 +0200 | [diff] [blame] | 22 | import androidx.preference.Preference; |
| 23 | import androidx.preference.PreferenceViewHolder; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 24 | import android.util.AttributeSet; |
| 25 | import android.util.Log; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.view.ViewParent; |
| 30 | import android.widget.SeekBar; |
| 31 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 32 | import android.widget.TextView; |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 33 | |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame^] | 34 | import omnirom.preference.R; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 35 | |
| 36 | public 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 LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 41 | private static final int DEFAULT_VALUE = 50; |
| 42 | |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 43 | private int mMaxValue = 100; |
| 44 | private int mMinValue = 0; |
| 45 | private int mInterval = 1; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 46 | private int mCurrentValue; |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 47 | private String mUnitsLeft = ""; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 48 | 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 LLanes | c956ca3 | 2018-06-06 20:05:53 +0200 | [diff] [blame] | 63 | setValuesFromXml(context, attrs); |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 64 | setLayoutResource(R.layout.preference_seek_bar); |
| 65 | } |
| 66 | |
Juan Ezquerro LLanes | c956ca3 | 2018-06-06 20:05:53 +0200 | [diff] [blame] | 67 | private void setValuesFromXml(Context context, AttributeSet attrs) { |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 68 | mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100); |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 69 | |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 70 | 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 LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 77 | } |
Juan Ezquerro LLanes | c956ca3 | 2018-06-06 20:05:53 +0200 | [diff] [blame] | 78 | |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 79 | 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 LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 109 | attributes.recycle(); |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 110 | } |
| 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 LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 131 | mStatusText = (TextView) holder.findViewById(R.id.seekBarPrefValue); |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 132 | mStatusText.setText(String.valueOf(mCurrentValue)); |
| 133 | mStatusText.setMinimumWidth(30); |
| 134 | |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 135 | TextView unitsRight = (TextView) holder.findViewById(R.id.seekBarPrefUnitsRight); |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 136 | unitsRight.setText(mUnitsRight); |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 137 | TextView unitsLeft = (TextView) holder.findViewById(R.id.seekBarPrefUnitsLeft); |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 138 | 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 LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 145 | if (newValue > mMaxValue) { |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 146 | newValue = mMaxValue; |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 147 | } else if (newValue < mMinValue) { |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 148 | newValue = mMinValue; |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 149 | } else if (mInterval != 1 && newValue % mInterval != 0) { |
| 150 | newValue = Math.round(((float) newValue) / mInterval) * mInterval; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 151 | } |
| 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 LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 166 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 167 | } |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 168 | |
| 169 | @Override |
| 170 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 171 | notifyChanged(); |
| 172 | } |
| 173 | |
| 174 | @Override |
Juan Ezquerro LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 175 | protected Object onGetDefaultValue(TypedArray ta, int index) { |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 176 | 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 LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 187 | temp = (Integer) defaultValue; |
| 188 | } catch (Exception ex) { |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 189 | 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 LLanes | 4a10d47 | 2018-06-12 20:23:37 +0200 | [diff] [blame] | 215 | public void setEnabled(boolean enabled) { |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 216 | if (mSeekBar != null) { |
| 217 | mSeekBar.setEnabled(enabled); |
| 218 | } |
| 219 | super.setEnabled(enabled); |
| 220 | } |
| 221 | } |