Remove non-zero amplitude validation for waveforms am: c0c482736b

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14703097

Change-Id: I91effcc0155767c71fc2c5840279f7f06983b27e
This commit is contained in:
Lais Andrade
2021-06-02 13:18:46 +00:00
committed by Automerger Merge Worker
2 changed files with 9 additions and 7 deletions

View File

@@ -182,6 +182,11 @@ public abstract class VibrationEffect implements Parcelable {
* @return The desired effect. * @return The desired effect.
*/ */
public static VibrationEffect createOneShot(long milliseconds, int amplitude) { 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 */); return createWaveform(new long[]{milliseconds}, new int[]{amplitude}, -1 /* repeat */);
} }
@@ -581,22 +586,16 @@ public abstract class VibrationEffect implements Parcelable {
public void validate() { public void validate() {
int segmentCount = mSegments.size(); int segmentCount = mSegments.size();
boolean hasNonZeroDuration = false; boolean hasNonZeroDuration = false;
boolean hasNonZeroAmplitude = false;
for (int i = 0; i < segmentCount; i++) { for (int i = 0; i < segmentCount; i++) {
VibrationEffectSegment segment = mSegments.get(i); VibrationEffectSegment segment = mSegments.get(i);
segment.validate(); segment.validate();
// A segment with unknown duration = -1 still counts as a non-zero duration. // A segment with unknown duration = -1 still counts as a non-zero duration.
hasNonZeroDuration |= segment.getDuration() != 0; hasNonZeroDuration |= segment.getDuration() != 0;
hasNonZeroAmplitude |= segment.hasNonZeroAmplitude();
} }
if (!hasNonZeroDuration) { if (!hasNonZeroDuration) {
throw new IllegalArgumentException("at least one timing must be non-zero" throw new IllegalArgumentException("at least one timing must be non-zero"
+ " (segments=" + mSegments + ")"); + " (segments=" + mSegments + ")");
} }
if (!hasNonZeroAmplitude) {
throw new IllegalArgumentException("at least one amplitude must be non-zero"
+ " (segments=" + mSegments + ")");
}
if (mRepeatIndex != -1) { if (mRepeatIndex != -1) {
Preconditions.checkArgumentInRange(mRepeatIndex, 0, segmentCount - 1, Preconditions.checkArgumentInRange(mRepeatIndex, 0, segmentCount - 1,
"repeat index must be within the bounds of the segments (segments.length=" "repeat index must be within the bounds of the segments (segments.length="

View File

@@ -102,7 +102,9 @@ public class VibrationEffectTest {
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> VibrationEffect.createOneShot(1, -2).validate()); () -> VibrationEffect.createOneShot(1, -2).validate());
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> VibrationEffect.createOneShot(1, 256).validate()); () -> VibrationEffect.createOneShot(1, 0).validate());
assertThrows(IllegalArgumentException.class,
() -> VibrationEffect.createOneShot(-1, 255).validate());
} }
@Test @Test
@@ -117,6 +119,7 @@ public class VibrationEffectTest {
@Test @Test
public void testValidateWaveform() { public void testValidateWaveform() {
VibrationEffect.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, -1).validate(); 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.createWaveform(TEST_TIMINGS, TEST_AMPLITUDES, 0).validate();
VibrationEffect.startWaveform() VibrationEffect.startWaveform()
.addStep(/* amplitude= */ 1, /* duration= */ 10) .addStep(/* amplitude= */ 1, /* duration= */ 10)