Loading core/java/android/os/VibrationEffect.java +9 −4 Original line number Original line Diff line number Diff line Loading @@ -437,7 +437,11 @@ public abstract class VibrationEffect implements Parcelable { * @hide * @hide */ */ protected static int scale(int amplitude, float scaleFactor) { protected static int scale(int amplitude, float scaleFactor) { return (int) (scale((float) amplitude / MAX_AMPLITUDE, scaleFactor) * MAX_AMPLITUDE); if (amplitude == 0) { return 0; } int scaled = (int) (scale((float) amplitude / MAX_AMPLITUDE, scaleFactor) * MAX_AMPLITUDE); return MathUtils.constrain(scaled, 1, MAX_AMPLITUDE); } } /** /** Loading Loading @@ -473,7 +477,7 @@ public abstract class VibrationEffect implements Parcelable { float a = (expMaxX + 1f) / (expMaxX - 1f); float a = (expMaxX + 1f) / (expMaxX - 1f); float fx = (expX - 1f) / (expX + 1f); float fx = (expX - 1f) / (expX + 1f); return a * fx; return MathUtils.constrain(a * fx, 0f, 1f); } } /** @hide */ /** @hide */ Loading Loading @@ -536,9 +540,10 @@ public abstract class VibrationEffect implements Parcelable { /** @hide */ /** @hide */ @Override @Override public OneShot resolve(int defaultAmplitude) { public OneShot resolve(int defaultAmplitude) { if (defaultAmplitude > MAX_AMPLITUDE || defaultAmplitude < 0) { if (defaultAmplitude > MAX_AMPLITUDE || defaultAmplitude <= 0) { throw new IllegalArgumentException( throw new IllegalArgumentException( "Amplitude is negative or greater than MAX_AMPLITUDE"); "amplitude must be between 1 and 255 inclusive (amplitude=" + defaultAmplitude + ")"); } } if (mAmplitude == DEFAULT_AMPLITUDE) { if (mAmplitude == DEFAULT_AMPLITUDE) { return new OneShot(mDuration, defaultAmplitude); return new OneShot(mDuration, defaultAmplitude); Loading core/tests/coretests/src/android/os/VibrationEffectTest.java +17 −0 Original line number Original line Diff line number Diff line Loading @@ -220,6 +220,10 @@ public class VibrationEffectTest { restored = scaledDown.scale(1.25f); restored = scaledDown.scale(1.25f); // Does not restore to the exact original value because scale up is a bit offset. // Does not restore to the exact original value because scale up is a bit offset. assertEquals(101, restored.getAmplitude(), AMPLITUDE_SCALE_TOLERANCE); assertEquals(101, restored.getAmplitude(), AMPLITUDE_SCALE_TOLERANCE); // Does not go below min amplitude while scaling down. VibrationEffect.OneShot minAmplitude = new VibrationEffect.OneShot(TEST_TIMING, 1); assertEquals(1, minAmplitude.scale(0.5f).getAmplitude()); } } @Test @Test Loading @@ -244,6 +248,15 @@ public class VibrationEffectTest { } } } } @Test public void testResolveOneshotFailsWhenAmplitudeNonPositive() { try { TEST_ONE_SHOT.resolve(0); fail("Amplitude is set to zero, should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } @Test @Test public void testScaleWaveform() { public void testScaleWaveform() { VibrationEffect.Waveform initial = (VibrationEffect.Waveform) TEST_WAVEFORM; VibrationEffect.Waveform initial = (VibrationEffect.Waveform) TEST_WAVEFORM; Loading @@ -255,6 +268,10 @@ public class VibrationEffectTest { assertEquals(216, scaled.getAmplitudes()[0], AMPLITUDE_SCALE_TOLERANCE); assertEquals(216, scaled.getAmplitudes()[0], AMPLITUDE_SCALE_TOLERANCE); assertEquals(0, scaled.getAmplitudes()[1]); assertEquals(0, scaled.getAmplitudes()[1]); assertEquals(-1, scaled.getAmplitudes()[2]); assertEquals(-1, scaled.getAmplitudes()[2]); VibrationEffect.Waveform minAmplitude = new VibrationEffect.Waveform( new long[]{100}, new int[] {1}, -1); assertArrayEquals(new int[]{1}, minAmplitude.scale(0.5f).getAmplitudes()); } } @Test @Test Loading Loading
core/java/android/os/VibrationEffect.java +9 −4 Original line number Original line Diff line number Diff line Loading @@ -437,7 +437,11 @@ public abstract class VibrationEffect implements Parcelable { * @hide * @hide */ */ protected static int scale(int amplitude, float scaleFactor) { protected static int scale(int amplitude, float scaleFactor) { return (int) (scale((float) amplitude / MAX_AMPLITUDE, scaleFactor) * MAX_AMPLITUDE); if (amplitude == 0) { return 0; } int scaled = (int) (scale((float) amplitude / MAX_AMPLITUDE, scaleFactor) * MAX_AMPLITUDE); return MathUtils.constrain(scaled, 1, MAX_AMPLITUDE); } } /** /** Loading Loading @@ -473,7 +477,7 @@ public abstract class VibrationEffect implements Parcelable { float a = (expMaxX + 1f) / (expMaxX - 1f); float a = (expMaxX + 1f) / (expMaxX - 1f); float fx = (expX - 1f) / (expX + 1f); float fx = (expX - 1f) / (expX + 1f); return a * fx; return MathUtils.constrain(a * fx, 0f, 1f); } } /** @hide */ /** @hide */ Loading Loading @@ -536,9 +540,10 @@ public abstract class VibrationEffect implements Parcelable { /** @hide */ /** @hide */ @Override @Override public OneShot resolve(int defaultAmplitude) { public OneShot resolve(int defaultAmplitude) { if (defaultAmplitude > MAX_AMPLITUDE || defaultAmplitude < 0) { if (defaultAmplitude > MAX_AMPLITUDE || defaultAmplitude <= 0) { throw new IllegalArgumentException( throw new IllegalArgumentException( "Amplitude is negative or greater than MAX_AMPLITUDE"); "amplitude must be between 1 and 255 inclusive (amplitude=" + defaultAmplitude + ")"); } } if (mAmplitude == DEFAULT_AMPLITUDE) { if (mAmplitude == DEFAULT_AMPLITUDE) { return new OneShot(mDuration, defaultAmplitude); return new OneShot(mDuration, defaultAmplitude); Loading
core/tests/coretests/src/android/os/VibrationEffectTest.java +17 −0 Original line number Original line Diff line number Diff line Loading @@ -220,6 +220,10 @@ public class VibrationEffectTest { restored = scaledDown.scale(1.25f); restored = scaledDown.scale(1.25f); // Does not restore to the exact original value because scale up is a bit offset. // Does not restore to the exact original value because scale up is a bit offset. assertEquals(101, restored.getAmplitude(), AMPLITUDE_SCALE_TOLERANCE); assertEquals(101, restored.getAmplitude(), AMPLITUDE_SCALE_TOLERANCE); // Does not go below min amplitude while scaling down. VibrationEffect.OneShot minAmplitude = new VibrationEffect.OneShot(TEST_TIMING, 1); assertEquals(1, minAmplitude.scale(0.5f).getAmplitude()); } } @Test @Test Loading @@ -244,6 +248,15 @@ public class VibrationEffectTest { } } } } @Test public void testResolveOneshotFailsWhenAmplitudeNonPositive() { try { TEST_ONE_SHOT.resolve(0); fail("Amplitude is set to zero, should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } @Test @Test public void testScaleWaveform() { public void testScaleWaveform() { VibrationEffect.Waveform initial = (VibrationEffect.Waveform) TEST_WAVEFORM; VibrationEffect.Waveform initial = (VibrationEffect.Waveform) TEST_WAVEFORM; Loading @@ -255,6 +268,10 @@ public class VibrationEffectTest { assertEquals(216, scaled.getAmplitudes()[0], AMPLITUDE_SCALE_TOLERANCE); assertEquals(216, scaled.getAmplitudes()[0], AMPLITUDE_SCALE_TOLERANCE); assertEquals(0, scaled.getAmplitudes()[1]); assertEquals(0, scaled.getAmplitudes()[1]); assertEquals(-1, scaled.getAmplitudes()[2]); assertEquals(-1, scaled.getAmplitudes()[2]); VibrationEffect.Waveform minAmplitude = new VibrationEffect.Waveform( new long[]{100}, new int[] {1}, -1); assertArrayEquals(new int[]{1}, minAmplitude.scale(0.5f).getAmplitudes()); } } @Test @Test Loading