diff options
| -rw-r--r-- | core/java/android/os/VibrationEffect.java | 11 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/os/VibrationEffectTest.java | 5 |
2 files changed, 9 insertions, 7 deletions
diff --git a/core/java/android/os/VibrationEffect.java b/core/java/android/os/VibrationEffect.java index 781800dd49ce..a0cbbfe3327b 100644 --- a/core/java/android/os/VibrationEffect.java +++ b/core/java/android/os/VibrationEffect.java @@ -182,6 +182,11 @@ public abstract class VibrationEffect implements Parcelable { * @return The desired effect. */ public static VibrationEffect createOneShot(long milliseconds, int amplitude) { + if (amplitude == 0) { + throw new IllegalArgumentException( + "amplitude must either be DEFAULT_AMPLITUDE, " + + "or between 1 and 255 inclusive (amplitude=" + amplitude + ")"); + } return createWaveform(new long[]{milliseconds}, new int[]{amplitude}, -1 /* repeat */); } @@ -581,22 +586,16 @@ public abstract class VibrationEffect implements Parcelable { public void validate() { int segmentCount = mSegments.size(); boolean hasNonZeroDuration = false; - boolean hasNonZeroAmplitude = false; for (int i = 0; i < segmentCount; i++) { VibrationEffectSegment segment = mSegments.get(i); segment.validate(); // A segment with unknown duration = -1 still counts as a non-zero duration. hasNonZeroDuration |= segment.getDuration() != 0; - hasNonZeroAmplitude |= segment.hasNonZeroAmplitude(); } if (!hasNonZeroDuration) { throw new IllegalArgumentException("at least one timing must be non-zero" + " (segments=" + mSegments + ")"); } - if (!hasNonZeroAmplitude) { - throw new IllegalArgumentException("at least one amplitude must be non-zero" - + " (segments=" + mSegments + ")"); - } if (mRepeatIndex != -1) { Preconditions.checkArgumentInRange(mRepeatIndex, 0, segmentCount - 1, "repeat index must be within the bounds of the segments (segments.length=" diff --git a/core/tests/coretests/src/android/os/VibrationEffectTest.java b/core/tests/coretests/src/android/os/VibrationEffectTest.java index 009665fa1f53..6cbfffc96116 100644 --- a/core/tests/coretests/src/android/os/VibrationEffectTest.java +++ b/core/tests/coretests/src/android/os/VibrationEffectTest.java @@ -102,7 +102,9 @@ public class VibrationEffectTest { assertThrows(IllegalArgumentException.class, () -> VibrationEffect.createOneShot(1, -2).validate()); assertThrows(IllegalArgumentException.class, - () -> VibrationEffect.createOneShot(1, 256).validate()); + () -> VibrationEffect.createOneShot(1, 0).validate()); + assertThrows(IllegalArgumentException.class, + () -> VibrationEffect.createOneShot(-1, 255).validate()); } @Test @@ -117,6 +119,7 @@ public class VibrationEffectTest { @Test public void testValidateWaveform() { VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1).validate(); + VibrationEffect.createWaveform(new long[]{10, 10}, new int[] {0, 0}, -1).validate(); VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, 0).validate(); VibrationEffect.startWaveform() .addStep(/* amplitude= */ 1, /* duration= */ 10) |