
org.robolectric.shadows.ShadowSystemVibrator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shadows-framework Show documentation
Show all versions of shadows-framework Show documentation
An alternative Android testing framework.
The newest version!
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.N_MR1;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import android.media.AudioAttributes;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemVibrator;
import android.os.VibrationEffect;
import android.os.vibrator.VibrationEffectSegment;
import com.google.common.base.Preconditions;
import java.util.List;
import java.util.Optional;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.ClassName;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
/** Shadow for {@link SystemVibrator}. */
@Implements(value = SystemVibrator.class, isInAndroidSdk = false)
public class ShadowSystemVibrator extends ShadowVibrator {
private final Handler handler = new Handler(Looper.getMainLooper());
private final Runnable stopVibratingRunnable = () -> vibrating = false;
@Implementation
protected boolean hasVibrator() {
return hasVibrator;
}
@Implementation(minSdk = O)
protected boolean hasAmplitudeControl() {
return hasAmplitudeControl;
}
@Implementation(maxSdk = N_MR1)
protected void vibrate(
int uid, String opPkg, long[] pattern, int repeat, AudioAttributes attributes) {
recordVibratePattern(pattern, repeat);
}
@Implementation(maxSdk = N_MR1)
protected void vibrate(int uid, String opPkg, long milliseconds, AudioAttributes attributes) {
recordVibrate(milliseconds);
}
@Implementation(minSdk = O, maxSdk = P)
protected void vibrate(
int uid, String opPkg, VibrationEffect effect, AudioAttributes attributes) {
vibrate(uid, opPkg, effect, null, attributes);
}
@Implementation(minSdk = Q, maxSdk = R)
protected void vibrate(
int uid, String opPkg, VibrationEffect effect, String reason, AudioAttributes attributes) {
try {
Class> waveformClass = Class.forName("android.os.VibrationEffect$Waveform");
Class> prebakedClass = Class.forName("android.os.VibrationEffect$Prebaked");
Class> oneShotClass = Class.forName("android.os.VibrationEffect$OneShot");
Optional> composedClass = Optional.empty();
if (RuntimeEnvironment.getApiLevel() == R) {
composedClass = Optional.of(Class.forName("android.os.VibrationEffect$Composed"));
}
if (waveformClass.isInstance(effect)) {
recordVibratePattern(
(long[]) ReflectionHelpers.callInstanceMethod(effect, "getTimings"),
ReflectionHelpers.callInstanceMethod(effect, "getRepeatIndex"));
} else if (prebakedClass.isInstance(effect)) {
recordVibratePredefined(
ReflectionHelpers.callInstanceMethod(effect, "getDuration"),
ReflectionHelpers.callInstanceMethod(effect, "getId"));
} else if (oneShotClass.isInstance(effect)) {
long timing;
if (RuntimeEnvironment.getApiLevel() >= P) {
timing = ReflectionHelpers.callInstanceMethod(effect, "getDuration");
} else {
timing = ReflectionHelpers.callInstanceMethod(effect, "getTiming");
}
recordVibrate(timing);
} else if (composedClass.isPresent() && composedClass.get().isInstance(effect)) {
VibrationEffect.Composed composed = (VibrationEffect.Composed) effect;
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy