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

Commit a03eafca authored by Lais Andrade's avatar Lais Andrade
Browse files

Fix regression on VibratorService update settings callback

A regression was introduced by ag/13111039 when the VibratorService
settings updated callback is triggered and any vibration is cancelled.

The fix brings back the check to only cancel the current vibration when
input devices change or while entering low power mode.

Fix: 176466887
Test: VibratorServiceTest
Change-Id: Ic9eb032c61bef446ebdfe265d3b55fe1d6f8bb96
parent 3df00e17
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -679,15 +679,23 @@ public class VibratorService extends IVibratorService.Stub {
        }
    }

    private void updateVibrators() {
    @VisibleForTesting
    void updateVibrators() {
        synchronized (mLock) {
            mInputDeviceDelegate.updateInputDeviceVibrators(
            boolean inputDevicesChanged = mInputDeviceDelegate.updateInputDeviceVibrators(
                    mVibrationSettings.shouldVibrateInputDevices());

            if (mCurrentVibration == null) {
                return;
            }

            if (inputDevicesChanged || !mVibrationSettings.shouldVibrateForPowerMode(
                    mCurrentVibration.attrs.getUsage())) {
                // If the state changes out from under us then just reset.
                doCancelVibrateLocked(Vibration.Status.CANCELLED);
            }
        }
    }

    private void doVibratorOff() {
        Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "doVibratorOff");
+56 −0
Original line number Diff line number Diff line
@@ -403,6 +403,62 @@ public class VibratorServiceTest {
        assertTrue(mVibratorProvider.getEffects().isEmpty());
    }

    @Test
    public void vibrate_enteringLowPowerMode_cancelVibration() throws Exception {
        VibratorService service = createService();

        mRegisteredPowerModeListener.onLowPowerModeChanged(NORMAL_POWER_STATE);
        vibrate(service, VibrationEffect.createOneShot(1000, 100), HAPTIC_FEEDBACK_ATTRS);

        // VibrationThread will start this vibration async, so wait before triggering callbacks.
        Thread.sleep(10);
        assertTrue(service.isVibrating());

        mRegisteredPowerModeListener.onLowPowerModeChanged(LOW_POWER_STATE);

        // Wait for callback to cancel vibration.
        Thread.sleep(10);
        assertFalse(service.isVibrating());
    }

    @Test
    public void vibrate_enteringLowPowerModeAndRingtone_doNotCancelVibration() throws Exception {
        VibratorService service = createService();

        mRegisteredPowerModeListener.onLowPowerModeChanged(NORMAL_POWER_STATE);
        vibrate(service, VibrationEffect.createOneShot(1000, 100), RINGTONE_ATTRS);

        // VibrationThread will start this vibration async, so wait before triggering callbacks.
        Thread.sleep(10);
        assertTrue(service.isVibrating());

        mRegisteredPowerModeListener.onLowPowerModeChanged(LOW_POWER_STATE);

        // Wait for callback to cancel vibration.
        Thread.sleep(10);
        assertTrue(service.isVibrating());
    }

    @Test
    public void vibrate_withSettingsChanged_doNotCancelVibration() throws Exception {
        VibratorService service = createService();
        vibrate(service, VibrationEffect.createOneShot(1000, 100), HAPTIC_FEEDBACK_ATTRS);

        // VibrationThread will start this vibration async, so wait before triggering callbacks.
        Thread.sleep(10);
        assertTrue(service.isVibrating());

        setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
                Vibrator.VIBRATION_INTENSITY_MEDIUM);

        // FakeSettingsProvider don't support testing triggering ContentObserver yet.
        service.updateVibrators();

        // Wait for callback to cancel vibration.
        Thread.sleep(10);
        assertTrue(service.isVibrating());
    }

    @Test
    public void vibrate_withComposed_performsEffect() throws Exception {
        mVibratorProvider.setCapabilities(IVibrator.CAP_COMPOSE_EFFECTS);