diff --git a/services/core/java/com/android/server/VibratorService.java b/services/core/java/com/android/server/VibratorService.java index 63fba25a37854..e662553af7310 100644 --- a/services/core/java/com/android/server/VibratorService.java +++ b/services/core/java/com/android/server/VibratorService.java @@ -1684,6 +1684,10 @@ public class VibratorService extends IVibratorService.Stub // Vibrator is already ON, so just change its amplitude. doVibratorSetAmplitude(amplitude); } + } else { + // Previous vibration should have already finished, but we make sure + // the vibrator will be off for the next step when amplitude is 0. + doVibratorOff(); } // We wait until the time this waveform step was supposed to end, diff --git a/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java b/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java index eae4a0804971c..6152421db6590 100644 --- a/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/VibratorServiceTest.java @@ -512,8 +512,7 @@ public class VibratorServiceTest { InOrder inOrderVerifier = inOrder(mNativeWrapperMock); inOrderVerifier.verify(mNativeWrapperMock).vibratorOff(); - inOrderVerifier.verify(mNativeWrapperMock).vibratorOn(eq(100L), - gt(0L)); + inOrderVerifier.verify(mNativeWrapperMock).vibratorOn(eq(100L), gt(0L)); inOrderVerifier.verify(mNativeWrapperMock).vibratorOff(); } @@ -540,9 +539,13 @@ public class VibratorServiceTest { } @Test - public void vibrate_withWaveformAndNativeCallback_callbackCannotBeTriggeredByNative() + public void vibrate_withWaveformAndNativeCallback_callbackIgnoredAndWaveformPlaysCompletely() throws Exception { VibratorService service = createService(); + doAnswer(invocation -> { + service.onVibrationComplete(invocation.getArgument(1)); + return null; + }).when(mNativeWrapperMock).vibratorOn(anyLong(), anyLong()); Mockito.clearInvocations(mNativeWrapperMock); VibrationEffect effect = VibrationEffect.createWaveform(new long[]{1, 3, 1, 2}, -1); @@ -551,8 +554,9 @@ public class VibratorServiceTest { // Wait for VibrateThread to finish: 1ms OFF, 3ms ON, 1ms OFF, 2ms ON. Thread.sleep(15); InOrder inOrderVerifier = inOrder(mNativeWrapperMock); - inOrderVerifier.verify(mNativeWrapperMock).vibratorOff(); + inOrderVerifier.verify(mNativeWrapperMock, times(2)).vibratorOff(); inOrderVerifier.verify(mNativeWrapperMock).vibratorOn(eq(3L), anyLong()); + inOrderVerifier.verify(mNativeWrapperMock).vibratorOff(); inOrderVerifier.verify(mNativeWrapperMock).vibratorOn(eq(2L), anyLong()); inOrderVerifier.verify(mNativeWrapperMock).vibratorOff(); }