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

Commit 3e00ef13 authored by Yeabkal Wubshit's avatar Yeabkal Wubshit
Browse files

Refactor: rename InputDevice specific vibration provider methods

There used to be 2 overloaded methods with the same name: getVibration.
This CL renames one of them to getVibrationForInputDevice. This
simplifies reading usages of this method, and also aligns with how the
getVibrationAttributes and getVibrationAttributesForInputDevice methods
of the same class are named.
Similarly, the InputDevice specific method in
HapticFeedbackCustomization has been renamed to follow the same pattern.

Bug: 397602072
Bug: 408393305
Test: atest HapticFeedbackVibrationProviderTest
Flag: android.os.vibrator.haptic_feedback_with_custom_usage
Change-Id: I46ed5e3e25692b153617c107006333cc42bad209
parent e00a95c7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ final class HapticFeedbackCustomization {
    }

    @Nullable
    VibrationEffect getEffect(int effectId, int inputSource) {
    VibrationEffect getEffectForInputDevice(int effectId, int inputSource) {
        VibrationEffect resultVibration = null;
        if ((InputDevice.SOURCE_ROTARY_ENCODER & inputSource) != 0) {
            resultVibration = mHapticCustomizationsForSourceRotary.get(effectId);
+3 −3
Original line number Diff line number Diff line
@@ -111,12 +111,12 @@ public final class HapticFeedbackVibrationProvider {
     * @return a {@link VibrationEffect} for the given haptic feedback effect ID, or {@code null} if
     * the provided effect ID is not supported.
     */
    @Nullable public VibrationEffect getVibration(int effectId, int inputSource) {
    @Nullable public VibrationEffect getVibrationForInputDevice(int effectId, int inputSource) {
        if (!isFeedbackConstantEnabled(effectId)) {
            return null;
        }
        VibrationEffect customizedVibration = mHapticFeedbackCustomization.getEffect(effectId,
                inputSource);
        VibrationEffect customizedVibration =
                mHapticFeedbackCustomization.getEffectForInputDevice(effectId, inputSource);
        if (customizedVibration != null) {
            return customizedVibration;
        }
+1 −1
Original line number Diff line number Diff line
@@ -568,7 +568,7 @@ public class VibratorManagerService extends IVibratorManagerService.Stub {
            return null;
        }
        return performHapticFeedbackWithEffect(uid, deviceId, opPkg, constant, reason, token,
                hapticVibrationProvider.getVibration(constant, inputSource),
                hapticVibrationProvider.getVibrationForInputDevice(constant, inputSource),
                hapticVibrationProvider.getVibrationAttributesForInputDevice(constant, inputSource,
                        flags, privFlags));
    }
+10 −9
Original line number Diff line number Diff line
@@ -519,14 +519,14 @@ public class HapticFeedbackCustomizationTest {
        // Matching customizations.
        assertThat(customization.getEffect(/* effectId= */ 10)).isEqualTo(COMPOSITION_VIBRATION);
        assertThat(customization.getEffect(/* effectId= */ 14)).isEqualTo(WAVEFORM_VIBRATION);
        assertThat(customization.getEffect(/* effectId= */ 10,
        assertThat(customization.getEffectForInputDevice(/* effectId= */ 10,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PREDEFINED_VIBRATION_CLICK);
        assertThat(customization.getEffect(/* effectId= */ 10,
        assertThat(customization.getEffectForInputDevice(/* effectId= */ 10,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PREDEFINED_VIBRATION_TICK);
        // Missing from input source customization xml. Fallback to base.
        assertThat(customization.getEffect(/* effectId= */ 14,
        assertThat(customization.getEffectForInputDevice(/* effectId= */ 14,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(WAVEFORM_VIBRATION);
        assertThat(customization.getEffect(/* effectId= */ 14,
        assertThat(customization.getEffectForInputDevice(/* effectId= */ 14,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(WAVEFORM_VIBRATION);
    }

@@ -612,11 +612,12 @@ public class HapticFeedbackCustomizationTest {
        return switch (customizationSource) {
            case DEVICE_CONFIG_FILE, DEVICE_RESOURCE -> hapticFeedbackCustomization.getEffect(
                    effectId);
            case DEVICE_RESOURCE_INPUT_ROTARY -> hapticFeedbackCustomization.getEffect(effectId,
                    InputDevice.SOURCE_ROTARY_ENCODER);
            case DEVICE_RESOURCE_INPUT_TOUCHSCREEN -> hapticFeedbackCustomization.getEffect(
                    effectId,
                    InputDevice.SOURCE_TOUCHSCREEN);
            case DEVICE_RESOURCE_INPUT_ROTARY ->
                    hapticFeedbackCustomization.getEffectForInputDevice(
                            effectId, InputDevice.SOURCE_ROTARY_ENCODER);
            case DEVICE_RESOURCE_INPUT_TOUCHSCREEN ->
                    hapticFeedbackCustomization.getEffectForInputDevice(
                            effectId, InputDevice.SOURCE_TOUCHSCREEN);
        };
    }

+74 −54
Original line number Diff line number Diff line
@@ -121,10 +121,10 @@ public class HapticFeedbackVibrationProviderTest {
        // No customization for `CLOCK_TICK`, so the default vibration is used.
        assertThat(provider.getVibration(CLOCK_TICK)).isEqualTo(
                VibrationEffect.get(EFFECT_TEXTURE_TICK));
        assertThat(provider.getVibration(CLOCK_TICK,
        assertThat(provider.getVibrationForInputDevice(CLOCK_TICK,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(
                VibrationEffect.get(EFFECT_TEXTURE_TICK));
        assertThat(provider.getVibration(CLOCK_TICK, InputDevice.SOURCE_TOUCHSCREEN))
        assertThat(provider.getVibrationForInputDevice(CLOCK_TICK, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(VibrationEffect.get(EFFECT_TEXTURE_TICK));
    }

@@ -146,14 +146,14 @@ public class HapticFeedbackVibrationProviderTest {
        // The customization for `CONTEXT_CLICK`.
        assertThat(provider.getVibration(CONTEXT_CLICK))
                .isEqualTo(PRIMITIVE_CLICK_EFFECT);
        assertThat(provider.getVibration(CONTEXT_CLICK,
        assertThat(provider.getVibrationForInputDevice(CONTEXT_CLICK,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_TICK_EFFECT);
        assertThat(provider.getVibration(CONTEXT_CLICK,
        assertThat(provider.getVibrationForInputDevice(CONTEXT_CLICK,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_THUD_EFFECT);
        // The customization for `DRAG_START`.
        assertThat(provider.getVibration(DRAG_START,
        assertThat(provider.getVibrationForInputDevice(DRAG_START,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_QUICK_RISE_EFFECT);
        assertThat(provider.getVibration(DRAG_START,
        assertThat(provider.getVibrationForInputDevice(DRAG_START,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_CLICK_EFFECT);
    }

@@ -189,19 +189,21 @@ public class HapticFeedbackVibrationProviderTest {

        // Test with a customization available for `TEXT_HANDLE_MOVE`.
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE)).isNull();
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE,
        assertThat(provider.getVibrationForInputDevice(TEXT_HANDLE_MOVE,
                InputDevice.SOURCE_ROTARY_ENCODER)).isNull();
        assertThat(
                provider.getVibration(TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN)).isNull();
                provider.getVibrationForInputDevice(
                        TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN)).isNull();

        // Test with no customization available for `TEXT_HANDLE_MOVE`.
        provider = createProviderWithoutCustomizations();

        assertThat(provider.getVibration(TEXT_HANDLE_MOVE)).isNull();
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE,
        assertThat(provider.getVibrationForInputDevice(TEXT_HANDLE_MOVE,
                InputDevice.SOURCE_ROTARY_ENCODER)).isNull();
        assertThat(
                provider.getVibration(TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN)).isNull();
                provider.getVibrationForInputDevice(
                        TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN)).isNull();
    }

    @Test
@@ -219,20 +221,22 @@ public class HapticFeedbackVibrationProviderTest {
                customizationsByRotary, customizationsByTouchScreen);

        assertThat(provider.getVibration(TEXT_HANDLE_MOVE)).isEqualTo(PRIMITIVE_CLICK_EFFECT);
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE,
        assertThat(provider.getVibrationForInputDevice(TEXT_HANDLE_MOVE,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_TICK_EFFECT);
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibrationForInputDevice(TEXT_HANDLE_MOVE,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_THUD_EFFECT);

        // Test with no customization available for `TEXT_HANDLE_MOVE`.
        provider = createProviderWithoutCustomizations();

        assertThat(provider.getVibration(TEXT_HANDLE_MOVE)).isEqualTo(
                VibrationEffect.get(EFFECT_TEXTURE_TICK));
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE,
        assertThat(provider.getVibrationForInputDevice(TEXT_HANDLE_MOVE,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(
                VibrationEffect.get(EFFECT_TEXTURE_TICK));
        assertThat(provider.getVibration(TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN))
        assertThat(
                provider.getVibrationForInputDevice(
                        TEXT_HANDLE_MOVE, InputDevice.SOURCE_TOUCHSCREEN))
                                .isEqualTo(VibrationEffect.get(EFFECT_TEXTURE_TICK));
    }

@@ -250,8 +254,10 @@ public class HapticFeedbackVibrationProviderTest {

        // Whatever customization set to NO_HAPTICS, no vibration happens.
        assertThat(provider.getVibration(NO_HAPTICS)).isNull();
        assertThat(provider.getVibration(NO_HAPTICS, InputDevice.SOURCE_ROTARY_ENCODER)).isNull();
        assertThat(provider.getVibration(NO_HAPTICS, InputDevice.SOURCE_TOUCHSCREEN)).isNull();
        assertThat(provider.getVibrationForInputDevice(NO_HAPTICS,
                InputDevice.SOURCE_ROTARY_ENCODER)).isNull();
        assertThat(provider.getVibrationForInputDevice(NO_HAPTICS,
                InputDevice.SOURCE_TOUCHSCREEN)).isNull();
    }

    @Test
@@ -269,10 +275,10 @@ public class HapticFeedbackVibrationProviderTest {
                        safeModeCustomizationsByTouchScreen);

        assertThat(provider.getVibration(SAFE_MODE_ENABLED)).isEqualTo(PRIMITIVE_CLICK_EFFECT);
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_ROTARY_ENCODER))
                .isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(PRIMITIVE_TICK_EFFECT);
        assertThat(provider.getVibrationForInputDevice(SAFE_MODE_ENABLED,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibrationForInputDevice(SAFE_MODE_ENABLED,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_TICK_EFFECT);

        // Resource changed
        mockSafeModeEnabledVibration(null);
@@ -281,10 +287,10 @@ public class HapticFeedbackVibrationProviderTest {
                        safeModeCustomizationsByTouchScreen);

        assertThat(provider.getVibration(SAFE_MODE_ENABLED)).isEqualTo(PRIMITIVE_CLICK_EFFECT);
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_ROTARY_ENCODER))
                .isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(PRIMITIVE_TICK_EFFECT);
        assertThat(provider.getVibrationForInputDevice(SAFE_MODE_ENABLED,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibrationForInputDevice(SAFE_MODE_ENABLED,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_TICK_EFFECT);
    }

    @Test
@@ -294,10 +300,16 @@ public class HapticFeedbackVibrationProviderTest {

        assertThat(provider.getVibration(SAFE_MODE_ENABLED))
                .isEqualTo(VibrationEffect.createWaveform(new long[]{10, 20, 30, 40}, -1));
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_ROTARY_ENCODER))
                .isEqualTo(VibrationEffect.createWaveform(new long[]{10, 20, 30, 40}, -1));
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(VibrationEffect.createWaveform(new long[]{10, 20, 30, 40}, -1));
        assertThat(
                provider.getVibrationForInputDevice(
                        SAFE_MODE_ENABLED, InputDevice.SOURCE_ROTARY_ENCODER))
                                .isEqualTo(VibrationEffect.createWaveform(
                                        new long[]{10, 20, 30, 40}, -1));
        assertThat(
                provider.getVibrationForInputDevice(
                        SAFE_MODE_ENABLED, InputDevice.SOURCE_TOUCHSCREEN))
                                .isEqualTo(VibrationEffect.createWaveform(
                                        new long[]{10, 20, 30, 40}, -1));
    }

    @Test
@@ -307,10 +319,10 @@ public class HapticFeedbackVibrationProviderTest {

        assertThat(provider.getVibration(SAFE_MODE_ENABLED)).isNull();
        assertThat(provider.getVibration(SAFE_MODE_ENABLED)).isNull();
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_ROTARY_ENCODER))
                .isNull();
        assertThat(provider.getVibration(SAFE_MODE_ENABLED, InputDevice.SOURCE_TOUCHSCREEN))
                .isNull();
        assertThat(provider.getVibrationForInputDevice(SAFE_MODE_ENABLED,
                InputDevice.SOURCE_ROTARY_ENCODER)).isNull();
        assertThat(provider.getVibrationForInputDevice(SAFE_MODE_ENABLED,
                InputDevice.SOURCE_TOUCHSCREEN)).isNull();
    }

    @Test
@@ -333,13 +345,21 @@ public class HapticFeedbackVibrationProviderTest {
                .isEqualTo(VibrationEffect.get(EFFECT_CLICK, true /* fallback */));
        assertThat(provider.getVibration(KEYBOARD_RELEASE))
                .isEqualTo(VibrationEffect.get(EFFECT_TICK, false /* fallback */));
        assertThat(provider.getVibration(KEYBOARD_TAP, InputDevice.SOURCE_ROTARY_ENCODER))
        assertThat(
                provider.getVibrationForInputDevice(
                        KEYBOARD_TAP, InputDevice.SOURCE_ROTARY_ENCODER))
                                .isEqualTo(VibrationEffect.get(EFFECT_CLICK, true /* fallback */));
        assertThat(provider.getVibration(KEYBOARD_RELEASE, InputDevice.SOURCE_ROTARY_ENCODER))
        assertThat(
                provider.getVibrationForInputDevice(
                        KEYBOARD_RELEASE, InputDevice.SOURCE_ROTARY_ENCODER))
                                .isEqualTo(VibrationEffect.get(EFFECT_TICK, false /* fallback */));
        assertThat(provider.getVibration(KEYBOARD_TAP, InputDevice.SOURCE_TOUCHSCREEN))
        assertThat(
                provider.getVibrationForInputDevice(
                        KEYBOARD_TAP, InputDevice.SOURCE_TOUCHSCREEN))
                                .isEqualTo(VibrationEffect.get(EFFECT_CLICK, true /* fallback */));
        assertThat(provider.getVibration(KEYBOARD_RELEASE, InputDevice.SOURCE_TOUCHSCREEN))
        assertThat(
                provider.getVibrationForInputDevice(
                        KEYBOARD_RELEASE, InputDevice.SOURCE_TOUCHSCREEN))
                                .isEqualTo(VibrationEffect.get(EFFECT_TICK, false /* fallback */));
    }

@@ -355,19 +375,19 @@ public class HapticFeedbackVibrationProviderTest {
        assertThat(provider.getVibration(KEYBOARD_RELEASE)).isEqualTo(
                VibrationEffect.startComposition().addPrimitive(PRIMITIVE_TICK,
                        KEYBOARD_VIBRATION_FIXED_AMPLITUDE).compose());
        assertThat(provider.getVibration(KEYBOARD_TAP,
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_TAP,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(
                VibrationEffect.startComposition().addPrimitive(PRIMITIVE_CLICK,
                        KEYBOARD_VIBRATION_FIXED_AMPLITUDE).compose());
        assertThat(provider.getVibration(KEYBOARD_RELEASE,
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_RELEASE,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(
                VibrationEffect.startComposition().addPrimitive(PRIMITIVE_TICK,
                        KEYBOARD_VIBRATION_FIXED_AMPLITUDE).compose());
        assertThat(provider.getVibration(KEYBOARD_TAP,
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_TAP,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(
                VibrationEffect.startComposition().addPrimitive(PRIMITIVE_CLICK,
                        KEYBOARD_VIBRATION_FIXED_AMPLITUDE).compose());
        assertThat(provider.getVibration(KEYBOARD_RELEASE,
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_RELEASE,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(
                VibrationEffect.startComposition().addPrimitive(PRIMITIVE_TICK,
                        KEYBOARD_VIBRATION_FIXED_AMPLITUDE).compose());
@@ -389,14 +409,14 @@ public class HapticFeedbackVibrationProviderTest {
        HapticFeedbackVibrationProvider provider = createProvider(customizations,
                customizationsByRotary, customizationsByTouchScreen);

        assertThat(provider.getVibration(KEYBOARD_TAP, InputDevice.SOURCE_ROTARY_ENCODER))
                .isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibration(KEYBOARD_RELEASE, InputDevice.SOURCE_ROTARY_ENCODER))
                .isEqualTo(PRIMITIVE_QUICK_RISE_EFFECT);
        assertThat(provider.getVibration(KEYBOARD_TAP, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(PRIMITIVE_QUICK_RISE_EFFECT);
        assertThat(provider.getVibration(KEYBOARD_RELEASE, InputDevice.SOURCE_TOUCHSCREEN))
                .isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_TAP,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_THUD_EFFECT);
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_RELEASE,
                InputDevice.SOURCE_ROTARY_ENCODER)).isEqualTo(PRIMITIVE_QUICK_RISE_EFFECT);
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_TAP,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_QUICK_RISE_EFFECT);
        assertThat(provider.getVibrationForInputDevice(KEYBOARD_RELEASE,
                InputDevice.SOURCE_TOUCHSCREEN)).isEqualTo(PRIMITIVE_THUD_EFFECT);
    }

    @Test