Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4918f19f authored by Lais Andrade's avatar Lais Andrade
Browse files

Use vibrator HAL getSupportedPrimitives on VibratorService

Use the list provided by the vibrator HAL in the VibratorService public
API arePrimitivesSupported.

Fix: b/163484172
Test: atest FrameworksServicesTests:VibratorServiceTest
Change-Id: I4c567539894a1ff49d94d4bd75225ff9cd254b7b
parent c45394fd
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -127,6 +127,7 @@ public class VibratorService extends IVibratorService.Stub
    private final int mPreviousVibrationsLimit;
    private final boolean mAllowPriorityVibrationsInLowPowerMode;
    private final List<Integer> mSupportedEffects;
    private final List<Integer> mSupportedPrimitives;
    private final long mCapabilities;
    private final int mDefaultVibrationAmplitude;
    private final SparseArray<VibrationEffect> mFallbackEffects;
@@ -184,6 +185,8 @@ public class VibratorService extends IVibratorService.Stub

    static native int[] vibratorGetSupportedEffects(long controllerPtr);

    static native int[] vibratorGetSupportedPrimitives(long controllerPtr);

    static native long vibratorPerformEffect(
            long controllerPtr, long effect, long strength, Vibration vibration);

@@ -397,6 +400,7 @@ public class VibratorService extends IVibratorService.Stub
        mNativeWrapper.vibratorOff();

        mSupportedEffects = asList(mNativeWrapper.vibratorGetSupportedEffects());
        mSupportedPrimitives = asList(mNativeWrapper.vibratorGetSupportedPrimitives());
        mCapabilities = mNativeWrapper.vibratorGetCapabilities();

        mContext = context;
@@ -647,8 +651,11 @@ public class VibratorService extends IVibratorService.Stub
    @Override // Binder call
    public boolean[] arePrimitivesSupported(int[] primitiveIds) {
        boolean[] supported = new boolean[primitiveIds.length];
        if (hasCapability(IVibrator.CAP_COMPOSE_EFFECTS)) {
            Arrays.fill(supported, true);
        if (!hasCapability(IVibrator.CAP_COMPOSE_EFFECTS) || mSupportedPrimitives == null) {
            return supported;
        }
        for (int i = 0; i < primitiveIds.length; i++) {
            supported[i] = mSupportedPrimitives.contains(primitiveIds[i]);
        }
        return supported;
    }
@@ -1501,6 +1508,7 @@ public class VibratorService extends IVibratorService.Stub
            pw.println("  mNotificationIntensity=" + mNotificationIntensity);
            pw.println("  mRingIntensity=" + mRingIntensity);
            pw.println("  mSupportedEffects=" + mSupportedEffects);
            pw.println("  mSupportedPrimitives=" + mSupportedPrimitives);
            pw.println();
            pw.println("  Previous ring vibrations:");
            for (VibrationInfo info : mPreviousRingVibrations) {
@@ -1759,6 +1767,11 @@ public class VibratorService extends IVibratorService.Stub
            return VibratorService.vibratorGetSupportedEffects(mNativeControllerPtr);
        }

        /** Returns all compose primitives supported by the device vibrator. */
        public int[] vibratorGetSupportedPrimitives() {
            return VibratorService.vibratorGetSupportedPrimitives(mNativeControllerPtr);
        }

        /** Turns vibrator on to perform one of the supported effects. */
        public long vibratorPerformEffect(long effect, long strength, Vibration vibration) {
            return VibratorService.vibratorPerformEffect(
+19 −0
Original line number Diff line number Diff line
@@ -181,6 +181,24 @@ static jintArray vibratorGetSupportedEffects(JNIEnv* env, jclass /* clazz */, jl
    return effects;
}

static jintArray vibratorGetSupportedPrimitives(JNIEnv* env, jclass /* clazz */,
                                                jlong controllerPtr) {
    vibrator::HalController* controller = reinterpret_cast<vibrator::HalController*>(controllerPtr);
    if (controller == nullptr) {
        ALOGE("vibratorGetSupportedPrimitives failed because controller was not initialized");
        return nullptr;
    }
    auto result = controller->getSupportedPrimitives();
    if (!result.isOk()) {
        return nullptr;
    }
    std::vector<aidl::CompositePrimitive> supportedPrimitives = result.value();
    jintArray primitives = env->NewIntArray(supportedPrimitives.size());
    env->SetIntArrayRegion(primitives, 0, supportedPrimitives.size(),
                           reinterpret_cast<jint*>(supportedPrimitives.data()));
    return primitives;
}

static jlong vibratorPerformEffect(JNIEnv* env, jclass /* clazz */, jlong controllerPtr,
                                   jlong effect, jlong strength, jobject vibration) {
    vibrator::HalController* controller = reinterpret_cast<vibrator::HalController*>(controllerPtr);
@@ -259,6 +277,7 @@ static const JNINativeMethod method_table[] = {
         "VibratorService$Vibration;)V",
         (void*)vibratorPerformComposedEffect},
        {"vibratorGetSupportedEffects", "(J)[I", (void*)vibratorGetSupportedEffects},
        {"vibratorGetSupportedPrimitives", "(J)[I", (void*)vibratorGetSupportedPrimitives},
        {"vibratorSetExternalControl", "(JZ)V", (void*)vibratorSetExternalControl},
        {"vibratorGetCapabilities", "(J)J", (void*)vibratorGetCapabilities},
        {"vibratorAlwaysOnEnable", "(JJJJ)V", (void*)vibratorAlwaysOnEnable},
+17 −2
Original line number Diff line number Diff line
@@ -223,9 +223,24 @@ public class VibratorServiceTest {
    }

    @Test
    public void arePrimitivesSupported_withComposeCapability_returnsAlwaysTrue() {
    public void arePrimitivesSupported_withNullResultFromNative_returnsAlwaysFalse() {
        mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS);
        assertArrayEquals(new boolean[]{true, true},
        when(mNativeWrapperMock.vibratorGetSupportedPrimitives()).thenReturn(null);

        assertArrayEquals(new boolean[]{false, false},
                createService().arePrimitivesSupported(new int[]{
                        VibrationEffect.Composition.PRIMITIVE_CLICK,
                        VibrationEffect.Composition.PRIMITIVE_QUICK_RISE
                }));
    }

    @Test
    public void arePrimitivesSupported_withSomeSupportedPrimitives_returnsBasedOnNativeResult() {
        mockVibratorCapabilities(IVibrator.CAP_COMPOSE_EFFECTS);
        when(mNativeWrapperMock.vibratorGetSupportedPrimitives())
                .thenReturn(new int[]{VibrationEffect.Composition.PRIMITIVE_CLICK});

        assertArrayEquals(new boolean[]{true, false},
                createService().arePrimitivesSupported(new int[]{
                        VibrationEffect.Composition.PRIMITIVE_CLICK,
                        VibrationEffect.Composition.PRIMITIVE_QUICK_RISE