From 71ec70ef09f07f3f1b8c89b62cb4d56271eb77d6 Mon Sep 17 00:00:00 2001 From: Felipe Erias Date: Wed, 11 Dec 2024 17:12:31 +0900 Subject: [PATCH] Prevent a crash in the slider widget The Slider widget will throw an exception if we try to set a value which does not respect the current step size, and min and max values. This can happen accidentally, for example when restoing the app's state from disk. To prevent similar crashes in the future, this change ensures that the value is acceptable, and rounds and clamps it if necessary. --- .../igalia/wolvic/ui/views/settings/SliderSetting.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/common/shared/com/igalia/wolvic/ui/views/settings/SliderSetting.java b/app/src/common/shared/com/igalia/wolvic/ui/views/settings/SliderSetting.java index d573be7a0ea..a66471547ba 100644 --- a/app/src/common/shared/com/igalia/wolvic/ui/views/settings/SliderSetting.java +++ b/app/src/common/shared/com/igalia/wolvic/ui/views/settings/SliderSetting.java @@ -59,9 +59,9 @@ private void initialize(Context aContext) { mSlider = findViewById(R.id.settings_slider); mSlider.setStepSize(mStepSize); - mSlider.setValue(mInitialValue); mSlider.setValueFrom(mValueFrom); mSlider.setValueTo(mValueTo); + mSlider.setValue(adjustValueToSliderRange(mInitialValue)); mSlider.clearOnChangeListeners(); mSlider.addOnChangeListener(mInternalOnChangeListener); @@ -81,9 +81,15 @@ public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) } }; + private float adjustValueToSliderRange(float value) { + // Prevent an exception if the value does not respect the step size, and min and max values. + float roundedValue = Math.round((value - mValueFrom) / mStepSize) * mStepSize + mValueFrom; + return Math.max(mValueFrom, Math.min(mValueTo, roundedValue)); + } + public void setValue(float value, boolean doApply) { mSlider.clearOnChangeListeners(); - mSlider.setValue(value); + mSlider.setValue(adjustValueToSliderRange(value)); mSlider.addOnChangeListener(mInternalOnChangeListener); if (mSliderListener != null && doApply) {