Loading core/java/android/os/VibrationEffect.java +16 −4 Original line number Diff line number Diff line Loading @@ -340,11 +340,17 @@ public abstract class VibrationEffect implements Parcelable { * Scale the amplitude of this effect. * * @param gamma the gamma adjustment to apply * @param maxAmplitude the new maximum amplitude of the effect * @param maxAmplitude the new maximum amplitude of the effect, must be between 0 and * MAX_AMPLITUDE * @throws IllegalArgumentException if maxAmplitude less than 0 or more than MAX_AMPLITUDE * * @return A {@link OneShot} effect with the same timing but scaled amplitude. */ public VibrationEffect scale(float gamma, int maxAmplitude) { public OneShot scale(float gamma, int maxAmplitude) { if (maxAmplitude > MAX_AMPLITUDE || maxAmplitude < 0) { throw new IllegalArgumentException( "Amplitude is negative or greater than MAX_AMPLITUDE"); } int newAmplitude = scale(mAmplitude, gamma, maxAmplitude); return new OneShot(mDuration, newAmplitude); } Loading Loading @@ -452,12 +458,18 @@ public abstract class VibrationEffect implements Parcelable { * Scale the Waveform with the given gamma and new max amplitude. * * @param gamma the gamma adjustment to apply * @param maxAmplitude the new maximum amplitude of the effect * @param maxAmplitude the new maximum amplitude of the effect, must be between 0 and * MAX_AMPLITUDE * @throws IllegalArgumentException if maxAmplitude less than 0 or more than MAX_AMPLITUDE * * @return A {@link Waveform} effect with the same timings and repeat index * but scaled amplitude. */ public VibrationEffect scale(float gamma, int maxAmplitude) { public Waveform scale(float gamma, int maxAmplitude) { if (maxAmplitude > MAX_AMPLITUDE || maxAmplitude < 0) { throw new IllegalArgumentException( "Amplitude is negative or greater than MAX_AMPLITUDE"); } if (gamma == 1.0f && maxAmplitude == MAX_AMPLITUDE) { // Just return a copy of the original if there's no scaling to be done. return new Waveform(mTimings, mAmplitudes, mRepeat); Loading core/tests/coretests/src/android/os/VibrationEffectTest.java +54 −0 Original line number Diff line number Diff line Loading @@ -19,6 +19,8 @@ package android.os; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; Loading @@ -40,6 +42,17 @@ public class VibrationEffectTest { private static final String RINGTONE_URI_3 = "content://test/system/ringtone_3"; private static final String UNKNOWN_URI = "content://test/system/other_audio"; private static final long TEST_TIMING = 100; private static final int TEST_AMPLITUDE = 100; private static final long[] TEST_TIMINGS = new long[] { 100, 100, 200 }; private static final int[] TEST_AMPLITUDES = new int[] { 255, 0, VibrationEffect.DEFAULT_AMPLITUDE }; private static final VibrationEffect TEST_ONE_SHOT = VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE); private static final VibrationEffect TEST_WAVEFORM = VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1); @Test public void getRingtones_noPrebakedRingtones() { Resources r = mockRingtoneResources(new String[0]); Loading @@ -66,6 +79,47 @@ public class VibrationEffectTest { assertEquals(expectedEffect, effect); } @Test public void testScaleOneShot() { VibrationEffect.OneShot initial = (VibrationEffect.OneShot) TEST_ONE_SHOT; VibrationEffect.OneShot halved = initial.scale(1, 128); assertEquals(50, halved.getAmplitude()); VibrationEffect.OneShot scaledUp = initial.scale(0.5f, 255); assertTrue(scaledUp.getAmplitude() > initial.getAmplitude()); VibrationEffect.OneShot restored2 = scaledUp.scale(2, 255); assertEquals(100, restored2.getAmplitude(), 2); // May differ a bit due to rounding VibrationEffect.OneShot scaledDown = initial.scale(2, 255); assertTrue(scaledDown.getAmplitude() < initial.getAmplitude()); VibrationEffect.OneShot restored3 = scaledDown.scale(0.5f, 255); assertEquals(100, restored3.getAmplitude(), 2); // May differ a bit due to rounding } @Test public void testScaleOneShotFailsWhenMaxAmplitudeAboveThreshold() { try { ((VibrationEffect.OneShot) TEST_ONE_SHOT).scale(1.1f, 1000); fail("Max amplitude above threshold, should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } @Test public void testScaleWaveform() { VibrationEffect.Waveform scaled = ((VibrationEffect.Waveform) TEST_WAVEFORM).scale(1.1f, 200); assertEquals(200, scaled.getAmplitudes()[0]); assertEquals(0, scaled.getAmplitudes()[1]); } @Test public void testScaleWaveformFailsWhenMaxAmplitudeAboveThreshold() { try { ((VibrationEffect.Waveform) TEST_WAVEFORM).scale(1.1f, 1000); fail("Max amplitude above threshold, should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } private Resources mockRingtoneResources() { return mockRingtoneResources(new String[] { Loading Loading
core/java/android/os/VibrationEffect.java +16 −4 Original line number Diff line number Diff line Loading @@ -340,11 +340,17 @@ public abstract class VibrationEffect implements Parcelable { * Scale the amplitude of this effect. * * @param gamma the gamma adjustment to apply * @param maxAmplitude the new maximum amplitude of the effect * @param maxAmplitude the new maximum amplitude of the effect, must be between 0 and * MAX_AMPLITUDE * @throws IllegalArgumentException if maxAmplitude less than 0 or more than MAX_AMPLITUDE * * @return A {@link OneShot} effect with the same timing but scaled amplitude. */ public VibrationEffect scale(float gamma, int maxAmplitude) { public OneShot scale(float gamma, int maxAmplitude) { if (maxAmplitude > MAX_AMPLITUDE || maxAmplitude < 0) { throw new IllegalArgumentException( "Amplitude is negative or greater than MAX_AMPLITUDE"); } int newAmplitude = scale(mAmplitude, gamma, maxAmplitude); return new OneShot(mDuration, newAmplitude); } Loading Loading @@ -452,12 +458,18 @@ public abstract class VibrationEffect implements Parcelable { * Scale the Waveform with the given gamma and new max amplitude. * * @param gamma the gamma adjustment to apply * @param maxAmplitude the new maximum amplitude of the effect * @param maxAmplitude the new maximum amplitude of the effect, must be between 0 and * MAX_AMPLITUDE * @throws IllegalArgumentException if maxAmplitude less than 0 or more than MAX_AMPLITUDE * * @return A {@link Waveform} effect with the same timings and repeat index * but scaled amplitude. */ public VibrationEffect scale(float gamma, int maxAmplitude) { public Waveform scale(float gamma, int maxAmplitude) { if (maxAmplitude > MAX_AMPLITUDE || maxAmplitude < 0) { throw new IllegalArgumentException( "Amplitude is negative or greater than MAX_AMPLITUDE"); } if (gamma == 1.0f && maxAmplitude == MAX_AMPLITUDE) { // Just return a copy of the original if there's no scaling to be done. return new Waveform(mTimings, mAmplitudes, mRepeat); Loading
core/tests/coretests/src/android/os/VibrationEffectTest.java +54 −0 Original line number Diff line number Diff line Loading @@ -19,6 +19,8 @@ package android.os; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; Loading @@ -40,6 +42,17 @@ public class VibrationEffectTest { private static final String RINGTONE_URI_3 = "content://test/system/ringtone_3"; private static final String UNKNOWN_URI = "content://test/system/other_audio"; private static final long TEST_TIMING = 100; private static final int TEST_AMPLITUDE = 100; private static final long[] TEST_TIMINGS = new long[] { 100, 100, 200 }; private static final int[] TEST_AMPLITUDES = new int[] { 255, 0, VibrationEffect.DEFAULT_AMPLITUDE }; private static final VibrationEffect TEST_ONE_SHOT = VibrationEffect.createOneShot(TEST_TIMING, TEST_AMPLITUDE); private static final VibrationEffect TEST_WAVEFORM = VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1); @Test public void getRingtones_noPrebakedRingtones() { Resources r = mockRingtoneResources(new String[0]); Loading @@ -66,6 +79,47 @@ public class VibrationEffectTest { assertEquals(expectedEffect, effect); } @Test public void testScaleOneShot() { VibrationEffect.OneShot initial = (VibrationEffect.OneShot) TEST_ONE_SHOT; VibrationEffect.OneShot halved = initial.scale(1, 128); assertEquals(50, halved.getAmplitude()); VibrationEffect.OneShot scaledUp = initial.scale(0.5f, 255); assertTrue(scaledUp.getAmplitude() > initial.getAmplitude()); VibrationEffect.OneShot restored2 = scaledUp.scale(2, 255); assertEquals(100, restored2.getAmplitude(), 2); // May differ a bit due to rounding VibrationEffect.OneShot scaledDown = initial.scale(2, 255); assertTrue(scaledDown.getAmplitude() < initial.getAmplitude()); VibrationEffect.OneShot restored3 = scaledDown.scale(0.5f, 255); assertEquals(100, restored3.getAmplitude(), 2); // May differ a bit due to rounding } @Test public void testScaleOneShotFailsWhenMaxAmplitudeAboveThreshold() { try { ((VibrationEffect.OneShot) TEST_ONE_SHOT).scale(1.1f, 1000); fail("Max amplitude above threshold, should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } @Test public void testScaleWaveform() { VibrationEffect.Waveform scaled = ((VibrationEffect.Waveform) TEST_WAVEFORM).scale(1.1f, 200); assertEquals(200, scaled.getAmplitudes()[0]); assertEquals(0, scaled.getAmplitudes()[1]); } @Test public void testScaleWaveformFailsWhenMaxAmplitudeAboveThreshold() { try { ((VibrationEffect.Waveform) TEST_WAVEFORM).scale(1.1f, 1000); fail("Max amplitude above threshold, should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } private Resources mockRingtoneResources() { return mockRingtoneResources(new String[] { Loading