Remove non-zero amplitude validation for waveforms

The validation does not exist in Android R and is causing apps to start
crashing in S.

The validation for the static API createOneShot was maintained, as it
already existed in previous releases.

Fix: 184890231
Test: VibrationEffectTest
Change-Id: I606c943a35ddd68ccacab76f0047b9a276b5cf28
This commit is contained in:
Lais Andrade
2021-05-25 11:19:12 +01:00
parent 0def72662f
commit c0c482736b
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.
*/
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="

View File

@@ -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)