Merge "Default vibration amplitude resolution added" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-04-26 16:39:29 +00:00
committed by Android (Google) Code Review
2 changed files with 46 additions and 0 deletions

View File

@@ -349,6 +349,26 @@ public abstract class VibrationEffect implements Parcelable {
return new OneShot(mDuration, newAmplitude);
}
/**
* Resolve default values into integer amplitude numbers.
*
* @param defaultAmplitude the default amplitude to apply, must be between 0 and
* MAX_AMPLITUDE
* @return A {@link OneShot} effect with same physical meaning but explicitly set amplitude
*
* @hide
*/
public OneShot resolve(int defaultAmplitude) {
if (defaultAmplitude > MAX_AMPLITUDE || defaultAmplitude < 0) {
throw new IllegalArgumentException(
"Amplitude is negative or greater than MAX_AMPLITUDE");
}
if (mAmplitude == DEFAULT_AMPLITUDE) {
return new OneShot(mDuration, defaultAmplitude);
}
return this;
}
@Override
public void validate() {
if (mAmplitude < -1 || mAmplitude == 0 || mAmplitude > 255) {
@@ -470,6 +490,30 @@ public abstract class VibrationEffect implements Parcelable {
return new Waveform(mTimings, scaledAmplitudes, mRepeat);
}
/**
* Resolve default values into integer amplitude numbers.
*
* @param defaultAmplitude the default amplitude to apply, must be between 0 and
* MAX_AMPLITUDE
* @return A {@link Waveform} effect with same physical meaning but explicitly set
* amplitude
*
* @hide
*/
public Waveform resolve(int defaultAmplitude) {
if (defaultAmplitude > MAX_AMPLITUDE || defaultAmplitude < 0) {
throw new IllegalArgumentException(
"Amplitude is negative or greater than MAX_AMPLITUDE");
}
int[] resolvedAmplitudes = Arrays.copyOf(mAmplitudes, mAmplitudes.length);
for (int i = 0; i < resolvedAmplitudes.length; i++) {
if (resolvedAmplitudes[i] == DEFAULT_AMPLITUDE) {
resolvedAmplitudes[i] = defaultAmplitude;
}
}
return new Waveform(mTimings, resolvedAmplitudes, mRepeat);
}
@Override
public void validate() {
if (mTimings.length != mAmplitudes.length) {

View File

@@ -704,9 +704,11 @@ public class VibratorService extends IVibratorService.Stub
VibrationEffect scaledEffect = null;
if (vib.effect instanceof VibrationEffect.OneShot) {
VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) vib.effect;
oneShot = oneShot.resolve(mDefaultVibrationAmplitude);
scaledEffect = oneShot.scale(gamma, maxAmplitude);
} else if (vib.effect instanceof VibrationEffect.Waveform) {
VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) vib.effect;
waveform = waveform.resolve(mDefaultVibrationAmplitude);
scaledEffect = waveform.scale(gamma, maxAmplitude);
} else {
Slog.w(TAG, "Unable to apply intensity scaling, unknown VibrationEffect type");