Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f6183631 authored by Juan Sebastian Martinez's avatar Juan Sebastian Martinez
Browse files

Adding support for discrete slider haptics.

A new slider configuration is added to deliver different haptics
depending on the quantization of the slider: continuous or discrete

Test: SliderHapticFeedbackProviderTest
Flag: com.android.systemui.haptics_for_compose_sliders
Bug: 341968766
Change-Id: I935370a1a5610153086a5c20d7004e210dbb13b6
parent 066cfe96
Loading
Loading
Loading
Loading
+67 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ class SliderHapticFeedbackProviderTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope

    private val config = SliderHapticFeedbackConfig()
    private var config = SliderHapticFeedbackConfig()

    private val dragVelocityProvider = SliderDragVelocityProvider { config.maxVelocityToScale }

@@ -226,6 +226,72 @@ class SliderHapticFeedbackProviderTest : SysuiTestCase() {
            assertEquals(/* expected= */ 1, vibratorHelper.timesVibratedWithEffect(ticks.compose()))
        }

    @Test
    @DisableFlags(Flags.FLAG_MSDL_FEEDBACK)
    fun playHapticAtProgress_forDiscreteSlider_playsTick() =
        with(kosmos) {
            config = SliderHapticFeedbackConfig(sliderStepSize = 0.2f)
            sliderHapticFeedbackProvider =
                SliderHapticFeedbackProvider(
                    vibratorHelper,
                    msdlPlayer,
                    dragVelocityProvider,
                    config,
                    kosmos.fakeSystemClock,
                )

            // GIVEN max velocity and slider progress
            val progress = 1f
            val expectedScale =
                sliderHapticFeedbackProvider.scaleOnDragTexture(config.maxVelocityToScale, progress)
            val tick =
                VibrationEffect.startComposition()
                    .addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, expectedScale)
                    .compose()

            // GIVEN system running for 1s
            fakeSystemClock.advanceTime(1000)

            // WHEN called to play haptics
            sliderHapticFeedbackProvider.onProgress(progress)

            // THEN the correct composition only plays once
            assertEquals(expected = 1, vibratorHelper.timesVibratedWithEffect(tick))
        }

    @Test
    @EnableFlags(Flags.FLAG_MSDL_FEEDBACK)
    fun playHapticAtProgress_forDiscreteSlider_playsDiscreteSliderToken() =
        with(kosmos) {
            config = SliderHapticFeedbackConfig(sliderStepSize = 0.2f)
            sliderHapticFeedbackProvider =
                SliderHapticFeedbackProvider(
                    vibratorHelper,
                    msdlPlayer,
                    dragVelocityProvider,
                    config,
                    kosmos.fakeSystemClock,
                )

            // GIVEN max velocity and slider progress
            val progress = 1f
            val expectedScale =
                sliderHapticFeedbackProvider.scaleOnDragTexture(config.maxVelocityToScale, progress)
            val expectedProperties =
                InteractionProperties.DynamicVibrationScale(expectedScale, pipeliningAttributes)

            // GIVEN system running for 1s
            fakeSystemClock.advanceTime(1000)

            // WHEN called to play haptics
            sliderHapticFeedbackProvider.onProgress(progress)

            // THEN the correct token plays once
            assertThat(msdlPlayer.latestTokenPlayed).isEqualTo(MSDLToken.DRAG_INDICATOR_DISCRETE)
            assertThat(msdlPlayer.latestPropertiesPlayed).isEqualTo(expectedProperties)
            assertThat(msdlPlayer.getHistory().size).isEqualTo(1)
        }

    @Test
    @EnableFlags(Flags.FLAG_MSDL_FEEDBACK)
    fun playHapticAtProgress_onQuickSuccession_playsContinuousDragTokenOnce() =
+2 −0
Original line number Diff line number Diff line
@@ -47,4 +47,6 @@ data class SliderHapticFeedbackConfig(
    @FloatRange(from = 0.0, to = 1.0) val lowerBookendScale: Float = 0.05f,
    /** Exponent for power function compensation */
    @FloatRange(from = 0.0, fromInclusive = false) val exponent: Float = 1f / 0.89f,
    /** The step-size that defines the slider quantization. Zero represents a continuous slider */
    @FloatRange(from = 0.0) val sliderStepSize: Float = 0f,
)
+33 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import com.google.android.msdl.domain.MSDLPlayer
import kotlin.math.abs
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.round

/**
 * Listener of slider events that triggers haptic feedback.
@@ -124,14 +125,45 @@ class SliderHapticFeedbackProvider(
        val deltaProgress = abs(normalizedSliderProgress - dragTextureLastProgress)
        if (deltaProgress < config.deltaProgressForDragThreshold) return

        // Check if the progress is a discrete step so haptics can be delivered
        if (
            config.sliderStepSize > 0 &&
                !normalizedSliderProgress.isDiscreteStep(config.sliderStepSize)
        ) {
            return
        }

        val powerScale = scaleOnDragTexture(absoluteVelocity, normalizedSliderProgress)

        // Deliver haptic feedback
        performContinuousSliderDragVibration(powerScale)
        when {
            config.sliderStepSize == 0f -> performContinuousSliderDragVibration(powerScale)
            config.sliderStepSize > 0f -> performDiscreteSliderDragVibration(powerScale)
        }
        dragTextureLastTime = currentTime
        dragTextureLastProgress = normalizedSliderProgress
    }

    private fun Float.isDiscreteStep(stepSize: Float, epsilon: Float = 0.001f): Boolean {
        if (stepSize <= 0f) return false
        val division = this / stepSize
        return abs(division - round(division)) < epsilon
    }

    private fun performDiscreteSliderDragVibration(scale: Float) {
        if (Flags.msdlFeedback()) {
            val properties =
                InteractionProperties.DynamicVibrationScale(scale, VIBRATION_ATTRIBUTES_PIPELINING)
            msdlPlayer.playToken(MSDLToken.DRAG_INDICATOR_DISCRETE, properties)
        } else {
            val effect =
                VibrationEffect.startComposition()
                    .addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, scale)
                    .compose()
            vibratorHelper.vibrate(effect, VIBRATION_ATTRIBUTES_PIPELINING)
        }
    }

    private fun performContinuousSliderDragVibration(scale: Float) {
        if (Flags.msdlFeedback()) {
            val properties =
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.haptics.slider

interface SliderQuantization {
    /** What is the step size between discrete steps of the slider */
    val stepSize: Float

    data class Continuous(override val stepSize: Float = Float.MIN_VALUE) : SliderQuantization

    data class Discrete(override val stepSize: Float) : SliderQuantization
}
+2 −1
Original line number Diff line number Diff line
@@ -2697,7 +2697,8 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
                /* velocityAxis= */ MotionEvent.AXIS_Y,
                /* upperBookendScale= */ 1f,
                /* lowerBookendScale= */ 0.05f,
                /* exponent= */ 1f / 0.89f);
                /* exponent= */ 1f / 0.89f,
                /* sliderStepSize = */ 0f);
        private static final SeekableSliderTrackerConfig sSliderTrackerConfig =
                new SeekableSliderTrackerConfig(
                        /* waitTimeMillis= */100,