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

Commit cf02346a authored by Steven Moreland's avatar Steven Moreland Committed by android-build-merger
Browse files

vibrator: add getSupportedEffects am: 2932b225 am: 1168cd40

am: 9708374b

Change-Id: I519a25dd6c07208cde516a90e8e927c7a083376f
parents 92d60be1 9708374b
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -73,6 +73,14 @@ interface IVibrator {
     */
    int perform(in Effect effect, in EffectStrength strength, in IVibratorCallback callback);

    /**
     * List supported effects.
     *
     * Return the effects which are supported (an effect is expected to be supported at every
     * strength level.
     */
    Effect[] getSupportedEffects();

    /**
     * Sets the motor's vibrational amplitude.
     *
+5 −0
Original line number Diff line number Diff line
@@ -78,6 +78,11 @@ ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
    *_aidl_return = {Effect::CLICK, Effect::TICK};
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Vibrator::setAmplitude(int32_t amplitude) {
    LOG(INFO) << "Vibrator set amplitude: " << amplitude;
    if (amplitude <= 0 || amplitude > 255) {
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ class Vibrator : public BnVibrator {
    ndk::ScopedAStatus perform(Effect effect, EffectStrength strength,
                               const std::shared_ptr<IVibratorCallback>& callback,
                               int32_t* _aidl_return) override;
    ndk::ScopedAStatus getSupportedEffects(std::vector<Effect>* _aidl_return) override;
    ndk::ScopedAStatus setAmplitude(int32_t amplitude) override;
    ndk::ScopedAStatus setExternalControl(bool enabled) override;
};
+28 −6
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ using android::hardware::vibrator::Effect;
using android::hardware::vibrator::EffectStrength;
using android::hardware::vibrator::IVibrator;

// TODO(b/143992652): autogenerate
const std::vector<Effect> kEffects = {
        Effect::CLICK,       Effect::DOUBLE_CLICK, Effect::TICK,        Effect::THUD,
        Effect::POP,         Effect::HEAVY_CLICK,  Effect::RINGTONE_1,  Effect::RINGTONE_2,
@@ -40,6 +41,7 @@ const std::vector<Effect> kEffects = {
        Effect::RINGTONE_11, Effect::RINGTONE_12,  Effect::RINGTONE_13, Effect::RINGTONE_14,
        Effect::RINGTONE_15, Effect::TEXTURE_TICK};

// TODO(b/143992652): autogenerate
const std::vector<EffectStrength> kEffectStrengths = {EffectStrength::LIGHT, EffectStrength::MEDIUM,
                                                      EffectStrength::STRONG};

@@ -105,15 +107,22 @@ TEST_P(VibratorAidl, OnCallbackNotSupported) {
}

TEST_P(VibratorAidl, ValidateEffect) {
    std::vector<Effect> supported;
    ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());

    for (Effect effect : kEffects) {
        bool isEffectSupported =
                std::find(supported.begin(), supported.end(), effect) != supported.end();

        for (EffectStrength strength : kEffectStrengths) {
            int32_t lengthMs = 0;
            Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
            EXPECT_TRUE(status.isOk() ||
                        status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION);
            if (status.isOk()) {

            if (isEffectSupported) {
                EXPECT_TRUE(status.isOk());
                EXPECT_GT(lengthMs, 0);
            } else {
                EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
                EXPECT_EQ(lengthMs, 0);
            }
        }
@@ -123,16 +132,29 @@ TEST_P(VibratorAidl, ValidateEffect) {
TEST_P(VibratorAidl, ValidateEffectWithCallback) {
    if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return;

    std::vector<Effect> supported;
    ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());

    for (Effect effect : kEffects) {
        bool isEffectSupported =
                std::find(supported.begin(), supported.end(), effect) != supported.end();

        for (EffectStrength strength : kEffectStrengths) {
            std::promise<void> completionPromise;
            std::future<void> completionFuture{completionPromise.get_future()};
            sp<CompletionCallback> callback =
                    new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
            int lengthMs;
            int lengthMs = 0;
            Status status = vibrator->perform(effect, strength, callback, &lengthMs);
            EXPECT_TRUE(status.isOk() ||
                        status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION);

            if (isEffectSupported) {
                EXPECT_TRUE(status.isOk());
                EXPECT_GT(lengthMs, 0);
            } else {
                EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION);
                EXPECT_EQ(lengthMs, 0);
            }

            if (!status.isOk()) continue;

            std::chrono::milliseconds timeout{lengthMs * 2};