Merge "Use compose duration returned from HAL" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-03-05 21:08:47 +00:00
committed by Android (Google) Code Review
4 changed files with 21 additions and 21 deletions

View File

@@ -87,8 +87,8 @@ final class VibratorController {
static native long vibratorPerformEffect(
long nativePtr, long effect, long strength, long vibrationId);
static native void vibratorPerformComposedEffect(long nativePtr,
VibrationEffect.Composition.PrimitiveEffect[] effect, long vibrationId);
static native long vibratorPerformComposedEffect(
long nativePtr, VibrationEffect.Composition.PrimitiveEffect[] effect, long vibrationId);
static native void vibratorSetExternalControl(long nativePtr, boolean enabled);
@@ -269,13 +269,9 @@ final class VibratorController {
VibrationEffect.Composition.PrimitiveEffect[] primitives =
effect.getPrimitiveEffects().toArray(
new VibrationEffect.Composition.PrimitiveEffect[0]);
mNativeWrapper.compose(primitives, vibrationId);
notifyVibratorOnLocked();
// Compose don't actually give us an estimated duration, so we just guess here.
long duration = 0;
for (VibrationEffect.Composition.PrimitiveEffect primitive : primitives) {
// TODO(b/177807015): use exposed durations from IVibrator here instead
duration += 20 + primitive.delay;
long duration = mNativeWrapper.compose(primitives, vibrationId);
if (duration > 0) {
notifyVibratorOnLocked();
}
return duration;
}
@@ -393,9 +389,9 @@ final class VibratorController {
}
/** Turns vibrator on to perform one of the supported composed effects. */
public void compose(
public long compose(
VibrationEffect.Composition.PrimitiveEffect[] effect, long vibrationId) {
VibratorController.vibratorPerformComposedEffect(mNativePtr, effect,
return VibratorController.vibratorPerformComposedEffect(mNativePtr, effect,
vibrationId);
}

View File

@@ -238,12 +238,12 @@ static jlong vibratorPerformEffect(JNIEnv* env, jclass /* clazz */, jlong ptr, j
return result.isOk() ? result.value().count() : -1;
}
static void vibratorPerformComposedEffect(JNIEnv* env, jclass /* clazz */, jlong ptr,
jobjectArray composition, jlong vibrationId) {
static jlong vibratorPerformComposedEffect(JNIEnv* env, jclass /* clazz */, jlong ptr,
jobjectArray composition, jlong vibrationId) {
VibratorControllerWrapper* wrapper = reinterpret_cast<VibratorControllerWrapper*>(ptr);
if (wrapper == nullptr) {
ALOGE("vibratorPerformComposedEffect failed because native wrapper was not initialized");
return;
return -1;
}
size_t size = env->GetArrayLength(composition);
std::vector<aidl::CompositeEffect> effects;
@@ -252,7 +252,8 @@ static void vibratorPerformComposedEffect(JNIEnv* env, jclass /* clazz */, jlong
effects.push_back(effectFromJavaPrimitive(env, element));
}
auto callback = wrapper->createCallback(vibrationId);
wrapper->hal()->performComposedEffect(effects, callback);
auto result = wrapper->hal()->performComposedEffect(effects, callback);
return result.isOk() ? result.value().count() : -1;
}
static jlong vibratorGetCapabilities(JNIEnv* env, jclass /* clazz */, jlong ptr) {
@@ -296,7 +297,7 @@ static const JNINativeMethod method_table[] = {
{"vibratorSetAmplitude", "(JI)V", (void*)vibratorSetAmplitude},
{"vibratorPerformEffect", "(JJJJ)J", (void*)vibratorPerformEffect},
{"vibratorPerformComposedEffect",
"(J[Landroid/os/VibrationEffect$Composition$PrimitiveEffect;J)V",
"(J[Landroid/os/VibrationEffect$Composition$PrimitiveEffect;J)J",
(void*)vibratorPerformComposedEffect},
{"vibratorGetSupportedEffects", "(J)[I", (void*)vibratorGetSupportedEffects},
{"vibratorGetSupportedPrimitives", "(J)[I", (void*)vibratorGetSupportedPrimitives},

View File

@@ -100,16 +100,17 @@ final class FakeVibratorControllerProvider {
return EFFECT_DURATION;
}
public void compose(VibrationEffect.Composition.PrimitiveEffect[] effect,
public long compose(VibrationEffect.Composition.PrimitiveEffect[] effect,
long vibrationId) {
VibrationEffect.Composed composed = new VibrationEffect.Composed(Arrays.asList(effect));
mEffects.add(composed);
applyLatency();
long duration = EFFECT_DURATION * effect.length;
long duration = 0;
for (VibrationEffect.Composition.PrimitiveEffect e : effect) {
duration += e.delay;
duration += EFFECT_DURATION + e.delay;
}
scheduleListener(duration, vibrationId);
return duration;
}
public void setExternalControl(boolean enabled) {

View File

@@ -19,6 +19,7 @@ package com.android.server.vibrator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
@@ -202,7 +203,7 @@ public class VibratorControllerTest {
VibrationEffect.Prebaked effect = (VibrationEffect.Prebaked)
VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK);
controller.on(effect, 11);
assertEquals(10L, controller.on(effect, 11));
assertTrue(controller.isVibrating());
verify(mNativeWrapperMock).perform(eq((long) VibrationEffect.EFFECT_CLICK),
@@ -212,13 +213,14 @@ public class VibratorControllerTest {
@Test
public void on_withComposed_performsEffect() {
mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS);
when(mNativeWrapperMock.compose(any(), anyLong())).thenReturn(15L);
VibratorController controller = createController();
VibrationEffect.Composed effect = (VibrationEffect.Composed)
VibrationEffect.startComposition()
.addPrimitive(VibrationEffect.Composition.PRIMITIVE_CLICK, 0.5f, 10)
.compose();
controller.on(effect, 12);
assertEquals(15L, controller.on(effect, 12));
ArgumentCaptor<VibrationEffect.Composition.PrimitiveEffect[]> primitivesCaptor =
ArgumentCaptor.forClass(VibrationEffect.Composition.PrimitiveEffect[].class);