From 8f0fb92919de27f07efb2e641a802b13daa987a5 Mon Sep 17 00:00:00 2001 From: Daniel Solomon Date: Fri, 21 Jan 2022 16:45:49 -0800 Subject: [PATCH] Cancel brightness slider animation before checking its value One of the checks that occurs before initiating a brightness slider animation is a comparison of the current slider value versus the current display brightness value. There is a race condition wherein, 1. There's an outstanding animation that is about to move the slider to a stale brightness value, but hasn't started yet. 2. A new animation to the current display brightness value is being considered. During this time, the slider value (which hasn't been moved yet by the outstanding animation) is being compared against the current display brightness value. They match, and a new animation isn't started. 3. The animation from step #1 proceeds, and moves the slider to a location that no longer matches the current display brightness value. To fix this, we first cancel any outstanding slider animation, and only then compare the slider's position to display brightness. Bug: 202873446 Bug: 214849040 Test: Manual, sliding the brightness slider around and ensuring it stays in place after the user lets go of it. Test: atest QuickQSBrightnessControllerTest BrightnessSliderTest Change-Id: I036638561bef6c5f10a11f5e50957c39cd792705 --- .../settings/brightness/BrightnessController.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessController.java index d7d1de00c82dd..991a68fb056a0 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessController.java +++ b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessController.java @@ -398,6 +398,11 @@ public class BrightnessController implements ToggleSlider.Listener, MirroredBrig min = mBrightnessMin; max = mBrightnessMax; } + + // Ensure the slider is in a fixed position first, then check if we should animate. + if (mSliderAnimator != null && mSliderAnimator.isStarted()) { + mSliderAnimator.cancel(); + } // convertGammaToLinearFloat returns 0-1 if (BrightnessSynchronizer.floatEquals(brightnessValue, convertGammaToLinearFloat(mControl.getValue(), min, max))) { @@ -420,9 +425,6 @@ public class BrightnessController implements ToggleSlider.Listener, MirroredBrig mControl.setValue(target); mControlValueInitialized = true; } - if (mSliderAnimator != null && mSliderAnimator.isStarted()) { - mSliderAnimator.cancel(); - } mSliderAnimator = ValueAnimator.ofInt(mControl.getValue(), target); mSliderAnimator.addUpdateListener((ValueAnimator animation) -> { mExternalChange = true;