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

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

Adding support for fallback haptic effects to MSDL Tokens.

In the event that the original haptic effect for a MSDL token is not
supported, the repository will provide a fallback that the player can
use to play less premium (but still consistent) haptics. For faster
operations, the knowledge about which tokens require a fallback has been
cached in the player as a Map.

Test: atest MSDLPlayerImplTest
Flag: NONE use of the player will be flagged but its corresponding use
  case
Bug: 344654090
Change-Id: I0c25741837a284f6052b0c6f3ae0063dc0b29138
parent 1d34c94a
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -16,18 +16,27 @@

package com.google.android.msdl.data.model

/** A haptic composition as a list of [HapticCompositionPrimitive] */
data class HapticComposition(val primitives: List<HapticCompositionPrimitive>? = null)
import android.os.VibrationEffect

/**
 * A haptic composition as a list of [HapticCompositionPrimitive] and a [android.os.VibrationEffect]
 * to use as a fallback.
 */
data class HapticComposition(
    val primitives: List<HapticCompositionPrimitive>,
    val fallbackEffect: VibrationEffect,
)

/**
 * An abstraction of a haptic primitive in a composition that includes:
 *
 * @param[primitiveId] The id of the primitive.
 * @param[scale] The scale of the primitive.
 * @param[delay] The delay of the primitive relative to the end of a previous primitive.
 * @param[delayMillis] The delay of the primitive relative to the end of a previous primitive. Given
 *   in milliseconds.
 */
data class HapticCompositionPrimitive(
    val primitiveId: Int,
    var scale: Float = 1f,
    var delay: Int = 0,
    var delayMillis: Int = 0,
)
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ enum class HapticToken {
    POSITIVE_CONFIRMATION_MEDIUM_EMPHASIS,
    POSITIVE_CONFIRMATION_LOW_EMPHASIS,
    NEUTRAL_CONFIRMATION_HIGH_EMPHASIS,
    NEUTRAL_CONFIRMATION_MEDIUM_EMPHASIS,
    LONG_PRESS,
    SWIPE_THRESHOLD_INDICATOR,
    TAP_HIGH_EMPHASIS,
+15 −9
Original line number Diff line number Diff line
@@ -46,12 +46,24 @@ enum class MSDLToken(
        SoundToken.START,
        FeedbackLevel.DEFAULT,
    ),
    /* Inform the user that an ongoing activity has paused */
    PAUSE(
        HapticToken.NEUTRAL_CONFIRMATION_MEDIUM_EMPHASIS,
        SoundToken.PAUSE,
        FeedbackLevel.DEFAULT,
    ),
    /* Inform the user that their previously started activity has stopped SUCCESSFULLY */
    STOP(
        HapticToken.POSITIVE_CONFIRMATION_MEDIUM_EMPHASIS,
        SoundToken.STOP,
        FeedbackLevel.DEFAULT,
    ),
    /* Inform the user that their previously started activity has cancelled SUCCESSFULLY */
    CANCEL(
        HapticToken.POSITIVE_CONFIRMATION_MEDIUM_EMPHASIS,
        SoundToken.CANCEL,
        FeedbackLevel.DEFAULT,
    ),
    /* Inform the user that the state of an interactive component has been switched to on SUCCESSFULLY */
    SWITCH_ON(
        HapticToken.POSITIVE_CONFIRMATION_MEDIUM_EMPHASIS,
@@ -100,16 +112,10 @@ enum class MSDLToken(
        SoundToken.TAP_MEDIUM_EMPHASIS,
        FeedbackLevel.DEFAULT,
    ),
    /* Played when a users drag gesture reaches the maximum value */
    DRAG_THRESHOLD_INDICATOR_CEILING(
        HapticToken.DRAG_THRESHOLD_INDICATOR,
        SoundToken.DRAG_THRESHOLD_INDICATOR_CEILING,
        FeedbackLevel.DEFAULT,
    ),
    /* Played when a users drag gesture reaches the minimum value */
    DRAG_THRESHOLD_INDICATOR_FLOOR(
    /* Played when a users drag gesture reaches the maximum or minimum value */
    DRAG_THRESHOLD_INDICATOR_LIMIT(
        HapticToken.DRAG_THRESHOLD_INDICATOR,
        SoundToken.DRAG_THRESHOLD_INDICATOR_FLOOR,
        SoundToken.DRAG_THRESHOLD_INDICATOR_LIMIT,
        FeedbackLevel.DEFAULT,
    ),
    /* Inform the user that their drag gesture has resulted in an incremental value change.
+3 −2
Original line number Diff line number Diff line
@@ -22,7 +22,9 @@ enum class SoundToken {
    FAILURE,
    SUCCESS,
    START,
    PAUSE,
    STOP,
    CANCEL,
    SWITCH_ON,
    SWITCH_OFF,
    UNLOCK,
@@ -31,8 +33,7 @@ enum class SoundToken {
    SWIPE_THRESHOLD_INDICATOR,
    TAP_HIGH_EMPHASIS,
    TAP_MEDIUM_EMPHASIS,
    DRAG_THRESHOLD_INDICATOR_CEILING,
    DRAG_THRESHOLD_INDICATOR_FLOOR,
    DRAG_THRESHOLD_INDICATOR_LIMIT,
    DRAG_INDICATOR,
    TAP_LOW_EMPHASIS,
    KEYPRESS_STANDARD,
+140 −56
Original line number Diff line number Diff line
@@ -33,29 +33,69 @@ class MSDLRepositoryImpl : MSDLRepository {
    override fun getHapticData(hapticToken: HapticToken): MSDLHapticData? = HAPTIC_DATA[hapticToken]

    companion object {
        // Timings and amplitudes that recreate a composition of three SPIN primitives as a waveform
        private val SPIN_TIMINGS = longArrayOf(20, 20, 3, 43, 20, 20, 3)
        private val SPIN_AMPLITUDES = intArrayOf(40, 80, 40, 0, 40, 80, 40)
        private const val SPIN_DELAY = 56L
        private const val SPIN_BREAK = 10
        private val SPIN_WAVEFORM_TIMINGS =
            SPIN_TIMINGS + SPIN_DELAY + SPIN_TIMINGS + SPIN_DELAY + SPIN_TIMINGS
        private val SPIN_WAVEFORM_AMPLITUDES =
            SPIN_AMPLITUDES + SPIN_BREAK + SPIN_AMPLITUDES + SPIN_BREAK + SPIN_AMPLITUDES

        private val HAPTIC_DATA: Map<HapticToken, MSDLHapticData> =
            mapOf(
                HapticToken.NEGATIVE_CONFIRMATION_HIGH_EMPHASIS to
                    MSDLHapticData { HapticComposition(null) },
                    MSDLHapticData {
                        HapticComposition(
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_SPIN,
                                    scale = 1f,
                                    delayMillis = 0,
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_SPIN,
                                    scale = 1f,
                                    delayMillis = SPIN_DELAY.toInt(),
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_SPIN,
                                    scale = 1f,
                                    delayMillis = SPIN_DELAY.toInt(),
                                )
                            ),
                            VibrationEffect.createWaveform(
                                SPIN_WAVEFORM_TIMINGS,
                                SPIN_WAVEFORM_AMPLITUDES,
                                -1,
                            )
                        )
                    },
                HapticToken.NEGATIVE_CONFIRMATION_MEDIUM_EMPHASIS to
                    MSDLHapticData {
                        HapticComposition(
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    0
                                    scale = 1f,
                                    delayMillis = 0
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    114
                                    scale = 1f,
                                    delayMillis = 114
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    114
                                    scale = 1f,
                                    delayMillis = 114
                                )
                            ),
                            VibrationEffect.createWaveform(
                                longArrayOf(10, 10, 10, 114, 10, 10, 10, 114, 10, 10, 10),
                                intArrayOf(10, 255, 20, 0, 10, 255, 20, 0, 10, 255, 20),
                                -1
                            )
                        )
                    },
@@ -65,14 +105,19 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    0
                                    scale = 1f,
                                    delayMillis = 0
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    114
                                    scale = 1f,
                                    delayMillis = 114
                                )
                            ),
                            VibrationEffect.createWaveform(
                                longArrayOf(10, 10, 10, 114, 10, 10, 10),
                                intArrayOf(10, 255, 20, 0, 10, 255, 20),
                                -1
                            )
                        )
                    },
@@ -82,14 +127,19 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    0
                                    scale = 1f,
                                    delayMillis = 0
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    52
                                    scale = 1f,
                                    delayMillis = 52
                                )
                            ),
                            VibrationEffect.createWaveform(
                                longArrayOf(10, 10, 10, 52, 10, 10, 10),
                                intArrayOf(10, 255, 20, 0, 10, 255, 20),
                                -1
                            )
                        )
                    },
@@ -99,14 +149,19 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_TICK,
                                    1f,
                                    0
                                    scale = 1f,
                                    delayMillis = 0
                                ),
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    52
                                    scale = 1f,
                                    delayMillis = 52
                                )
                            ),
                            VibrationEffect.createWaveform(
                                longArrayOf(5, 52, 10, 10, 10),
                                intArrayOf(100, 0, 10, 255, 20),
                                -1
                            )
                        )
                    },
@@ -116,22 +171,41 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_THUD,
                                    1f,
                                    0
                                    scale = 1f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createWaveform(
                                longArrayOf(50, 100, 100, 50),
                                intArrayOf(5, 50, 20, 10),
                                -1
                            )
                        )
                    },
                HapticToken.LONG_PRESS to
                HapticToken.NEUTRAL_CONFIRMATION_MEDIUM_EMPHASIS to
                    MSDLHapticData {
                        HapticComposition(
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    1f,
                                    0
                                    scale = 1f,
                                    delayMillis = 0,
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.LONG_PRESS to
                    MSDLHapticData {
                        HapticComposition(
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    scale = 1f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.SWIPE_THRESHOLD_INDICATOR to
@@ -140,10 +214,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.7f,
                                    0
                                )
                                    scale = 0.7f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.TAP_HIGH_EMPHASIS to
@@ -152,10 +227,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.7f,
                                    0
                                )
                                    scale = 0.7f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.TAP_MEDIUM_EMPHASIS to
@@ -164,10 +240,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.5f,
                                    0
                                )
                            )
                                    scale = 0.5f,
                                    delayMillis = 0
                                ),
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.DRAG_THRESHOLD_INDICATOR to
@@ -176,10 +253,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_TICK,
                                    1f,
                                    0
                                )
                                    scale = 1f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
                        )
                    },
                HapticToken.DRAG_INDICATOR to
@@ -188,10 +266,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_TICK,
                                    0.5f,
                                    0
                                )
                                    scale = 0.5f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
                        )
                    },
                HapticToken.TAP_LOW_EMPHASIS to
@@ -200,10 +279,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.3f,
                                    0
                                )
                                    scale = 0.3f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.KEYPRESS_STANDARD to
@@ -212,10 +292,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_TICK,
                                    0.7f,
                                    0
                                )
                            )
                                    scale = 0.7f,
                                    delayMillis = 0
                                ),
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
                        )
                    },
                HapticToken.KEYPRESS_SPACEBAR to
@@ -224,10 +305,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.7f,
                                    0
                                )
                                    scale = 0.7f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.KEYPRESS_RETURN to
@@ -236,10 +318,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.7f,
                                    0
                                )
                                    scale = 0.7f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    },
                HapticToken.KEYPRESS_DELETE to
@@ -248,10 +331,11 @@ class MSDLRepositoryImpl : MSDLRepository {
                            listOf(
                                HapticCompositionPrimitive(
                                    VibrationEffect.Composition.PRIMITIVE_CLICK,
                                    0.1f,
                                    0
                                )
                                    scale = 1f,
                                    delayMillis = 0
                                )
                            ),
                            VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
                        )
                    }
            )
Loading