RM6785: Switch to common mediatek vibrator service HAL

Change-Id: I7a206fd0d1c83262adab2e36be5eea6fb38d1c35
diff --git a/BoardConfig.mk b/BoardConfig.mk
index a5ee146..d29cd01 100644
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -129,6 +129,9 @@
 # SPL
 VENDOR_SECURITY_PATCH := 2022-05-05
 
+# Vibrator
+TARGET_VIBRATOR_SUPPORTS_EFFECTS := true
+
 # Wi-Fi
 BOARD_HOSTAPD_DRIVER := NL80211
 WIFI_HIDL_FEATURE_DUAL_INTERFACE := true
diff --git a/aidl/vibrator/Android.bp b/aidl/vibrator/Android.bp
deleted file mode 100644
index a9160d6..0000000
--- a/aidl/vibrator/Android.bp
+++ /dev/null
@@ -1,22 +0,0 @@
-//
-// Copyright (C) 2022 The LineageOS Project
-//
-// SPDX-License-Identifier: Apache-2.0
-//
-
-cc_binary {
-    name: "android.hardware.vibrator-service.RM6785",
-    relative_install_path: "hw",
-    init_rc: ["android.hardware.vibrator-service.RM6785.rc"],
-    vintf_fragments: ["android.hardware.vibrator-service.RM6785.xml"],
-    srcs: [
-        "Vibrator.cpp",
-        "service.cpp",
-    ],
-    shared_libs: [
-        "libbase",
-        "libbinder_ndk",
-        "android.hardware.vibrator-V2-ndk",
-    ],
-    vendor: true,
-}
diff --git a/aidl/vibrator/Vibrator.cpp b/aidl/vibrator/Vibrator.cpp
deleted file mode 100644
index 8200522..0000000
--- a/aidl/vibrator/Vibrator.cpp
+++ /dev/null
@@ -1,223 +0,0 @@
-/*
- * Copyright (C) 2022-2023 The LineageOS Project
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-#include <fstream>
-#include <thread>
-#include <android-base/logging.h>
-
-#include "Vibrator.h"
-
-namespace aidl {
-namespace android {
-namespace hardware {
-namespace vibrator {
-
-/*
- * Write value to path and close file.
- */
-template <typename T>
-static void write(const std::string& path, const T& value) {
-    std::ofstream file(path);
-    file << value << std::endl;
-}
-
-static bool fileExists(const std::string& path) {
-    std::ifstream file(path);
-    return (file.good());
-}
-
-Vibrator::Vibrator() {
-    mAmplitudeControl = fileExists(VIBRATOR_INTENSITY);
-    write(VIBRATOR_STATE, 1);
-}
-
-ndk::ScopedAStatus Vibrator::activate(int32_t timeoutMs) {
-    write(VIBRATOR_DURATION, timeoutMs);
-    write(VIBRATOR_ACTIVATE, 1);
-
-    return ndk::ScopedAStatus::ok();
-}
-
-ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
-    *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK;
-
-    if (mAmplitudeControl)
-        *_aidl_return |= IVibrator::CAP_AMPLITUDE_CONTROL;
-
-    return ndk::ScopedAStatus::ok();
-}
-
-ndk::ScopedAStatus Vibrator::off() {
-    write(VIBRATOR_ACTIVATE, 0);
-    return ndk::ScopedAStatus::ok();
-}
-
-ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr<IVibratorCallback>& callback) {
-    ndk::ScopedAStatus status = activate(timeoutMs);
-
-    if (callback != nullptr) {
-        std::thread([=] {
-            LOG(DEBUG) << "Starting on on another thread";
-            usleep(timeoutMs * 1000);
-            LOG(DEBUG) << "Notifying on complete";
-            if (!callback->onComplete().isOk()) {
-                LOG(ERROR) << "Failed to call onComplete";
-            }
-        }).detach();
-    }
-
-    return status;
-}
-
-ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr<IVibratorCallback>& callback, int32_t* _aidl_return) {
-    ndk::ScopedAStatus status;
-    int32_t timeoutMs;
-    float amplitude;
-
-    switch (strength) {
-        case EffectStrength::LIGHT:
-            amplitude = AMPLITUDE_LIGHT;
-            break;
-        case EffectStrength::MEDIUM:
-            amplitude = AMPLITUDE_MEDIUM;
-            break;
-        case EffectStrength::STRONG:
-            amplitude = AMPLITUDE_STRONG;
-            break;
-        default:
-            return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-    }
-
-    setAmplitude(amplitude);
-
-    switch (effect) {
-        case Effect::CLICK:
-            timeoutMs = 70;
-            break;
-        case Effect::TEXTURE_TICK:
-            timeoutMs = 40;
-            break;
-        case Effect::TICK:
-            timeoutMs = 80;
-            break;
-        default:
-            return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-    }
-
-    status = activate(timeoutMs);
-
-    if (callback != nullptr) {
-        std::thread([=] {
-            LOG(DEBUG) << "Starting perform on another thread";
-            usleep(timeoutMs * 1000);
-            LOG(DEBUG) << "Notifying perform complete";
-            callback->onComplete();
-        }).detach();
-    }
-
-    *_aidl_return = timeoutMs;
-    return status;
-}
-
-ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
-    *_aidl_return = {Effect::CLICK, Effect::TEXTURE_TICK, Effect::TICK};
-    return ndk::ScopedAStatus::ok();
-}
-
-ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
-    int32_t intensity;
-
-    if (amplitude <= 0.0f || amplitude > 1.0f)
-        return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
-
-    LOG(DEBUG) << "Setting amplitude: " << amplitude;
-
-    intensity = amplitude * INTENSITY_MAX;
-
-    LOG(DEBUG) << "Setting intensity: " << intensity;
-
-    if (mAmplitudeControl)
-        write(VIBRATOR_INTENSITY, intensity);
-
-    return ndk::ScopedAStatus::ok();
-}
-
-ndk::ScopedAStatus Vibrator::setExternalControl(bool /*enabled*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimitive>* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive /*primitive*/, int32_t* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& /*composite*/, const std::shared_ptr<IVibratorCallback>& /*callback*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t /*id*/, Effect /*effect*/, EffectStrength /*strength*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t /*id*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getResonantFrequency(float* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getQFactor(float* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getFrequencyResolution(float* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getFrequencyMinimum(float* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getBandwidthAmplitudeMap(std::vector<float>* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getPwlePrimitiveDurationMax(int32_t* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getPwleCompositionSizeMax(int32_t* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::getSupportedBraking(std::vector<Braking>* /*_aidl_return*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-ndk::ScopedAStatus Vibrator::composePwle(const std::vector<PrimitivePwle>& /*composite*/, const std::shared_ptr<IVibratorCallback>& /*callback*/) {
-    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
-}
-
-} // namespace vibrator
-} // namespace hardware
-} // namespace android
-} // namespace aidl
diff --git a/aidl/vibrator/Vibrator.h b/aidl/vibrator/Vibrator.h
deleted file mode 100644
index 7872a4f..0000000
--- a/aidl/vibrator/Vibrator.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright (C) 2022-2023 The LineageOS Project
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-#pragma once
-
-#include <aidl/android/hardware/vibrator/BnVibrator.h>
-
-#define VIBRATOR_STATE "/sys/class/leds/vibrator/state"
-#define VIBRATOR_DURATION "/sys/class/leds/vibrator/duration"
-#define VIBRATOR_ACTIVATE "/sys/class/leds/vibrator/activate"
-#define VIBRATOR_INTENSITY "/sys/kernel/thunderquake_engine/level"
-#define INTENSITY_MIN 0
-#define INTENSITY_MAX 9
-
-#define AMPLITUDE_LIGHT 0.3
-#define AMPLITUDE_MEDIUM 0.6
-#define AMPLITUDE_STRONG 1
-
-using ::aidl::android::hardware::vibrator::IVibratorCallback;
-using ::aidl::android::hardware::vibrator::Braking;
-using ::aidl::android::hardware::vibrator::Effect;
-using ::aidl::android::hardware::vibrator::EffectStrength;
-using ::aidl::android::hardware::vibrator::CompositeEffect;
-using ::aidl::android::hardware::vibrator::CompositePrimitive;
-using ::aidl::android::hardware::vibrator::PrimitivePwle;
-
-namespace aidl {
-namespace android {
-namespace hardware {
-namespace vibrator {
-
-class Vibrator : public BnVibrator {
-public:
-    Vibrator();
-    ndk::ScopedAStatus getCapabilities(int32_t* _aidl_return) override;
-    ndk::ScopedAStatus off() override;
-    ndk::ScopedAStatus on(int32_t timeoutMs, const std::shared_ptr<IVibratorCallback>& callback) override;
-    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(float amplitude) override;
-    ndk::ScopedAStatus setExternalControl(bool enabled) override;
-    ndk::ScopedAStatus getCompositionDelayMax(int32_t* _aidl_return) override;
-    ndk::ScopedAStatus getCompositionSizeMax(int32_t* _aidl_return) override;
-    ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive>* _aidl_return) override;
-    ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, int32_t* _aidl_return) override;
-    ndk::ScopedAStatus compose(const std::vector<CompositeEffect>& composite, const std::shared_ptr<IVibratorCallback>& callback) override;
-    ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) override;
-    ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override;
-    ndk::ScopedAStatus alwaysOnDisable(int32_t id) override;
-    ndk::ScopedAStatus getResonantFrequency(float* _aidl_return) override;
-    ndk::ScopedAStatus getQFactor(float* _aidl_return) override;
-    ndk::ScopedAStatus getFrequencyResolution(float* _aidl_return) override;
-    ndk::ScopedAStatus getFrequencyMinimum(float* _aidl_return) override;
-    ndk::ScopedAStatus getBandwidthAmplitudeMap(std::vector<float>* _aidl_return) override;
-    ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t* _aidl_return) override;
-    ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t* _aidl_return) override;
-    ndk::ScopedAStatus getSupportedBraking(std::vector<Braking>* _aidl_return) override;
-    ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle>& composite, const std::shared_ptr<IVibratorCallback>& callback) override;
-private:
-    ndk::ScopedAStatus activate(int32_t timeoutMs);
-    bool mAmplitudeControl;
-};
-
-} // namespace vibrator
-} // namespace hardware
-} // namespace android
-} // namespace aidl
diff --git a/aidl/vibrator/android.hardware.vibrator-service.RM6785.rc b/aidl/vibrator/android.hardware.vibrator-service.RM6785.rc
deleted file mode 100644
index 8fce4bc..0000000
--- a/aidl/vibrator/android.hardware.vibrator-service.RM6785.rc
+++ /dev/null
@@ -1,8 +0,0 @@
-on init
-    chown system system /sys/kernel/thunderquake_engine/level
-
-service vendor.vibrator-default /vendor/bin/hw/android.hardware.vibrator-service.RM6785
-    class hal
-    user system
-    group system
-    shutdown critical
diff --git a/aidl/vibrator/android.hardware.vibrator-service.RM6785.xml b/aidl/vibrator/android.hardware.vibrator-service.RM6785.xml
deleted file mode 100644
index 49b11ec..0000000
--- a/aidl/vibrator/android.hardware.vibrator-service.RM6785.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-<manifest version="1.0" type="device">
-    <hal format="aidl">
-        <name>android.hardware.vibrator</name>
-        <fqname>IVibrator/default</fqname>
-    </hal>
-</manifest>
diff --git a/aidl/vibrator/service.cpp b/aidl/vibrator/service.cpp
deleted file mode 100644
index c345b6e..0000000
--- a/aidl/vibrator/service.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2022 The LineageOS Project
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-#include "Vibrator.h"
-
-#include <android/binder_manager.h>
-#include <android/binder_process.h>
-#include <android-base/logging.h>
-
-using ::aidl::android::hardware::vibrator::Vibrator;
-
-int main() {
-    ABinderProcess_setThreadPoolMaxThreadCount(0);
-    std::shared_ptr<Vibrator> vibrator = ndk::SharedRefBase::make<Vibrator>();
-
-    const std::string instance = std::string() + Vibrator::descriptor + "/default";
-    binder_status_t status = AServiceManager_addService(vibrator->asBinder().get(), instance.c_str());
-    CHECK(status == STATUS_OK);
-
-    ABinderProcess_joinThreadPool();
-    return EXIT_FAILURE; // should not reach
-}
diff --git a/device.mk b/device.mk
index 340b054..1c2c44c 100644
--- a/device.mk
+++ b/device.mk
@@ -313,7 +313,7 @@
 
 # Vibrator
 PRODUCT_PACKAGES += \
-    android.hardware.vibrator-service.RM6785
+    android.hardware.vibrator-service.mediatek
 
 # VNDK
 PRODUCT_PACKAGES += \
diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts
index c381d06..92c3065 100644
--- a/sepolicy/vendor/file_contexts
+++ b/sepolicy/vendor/file_contexts
@@ -5,7 +5,7 @@
 /(vendor|system/vendor)/bin/hw/android\.hardware\.light-service.RM6785                                    u:object_r:hal_light_default_exec:s0
 /(vendor|system/vendor)/bin/hw/android\.hardware\.memtrack-service\.RM6785                                u:object_r:hal_memtrack_default_exec:s0
 /(vendor|system/vendor)/bin/hw/android\.hardware\.power-service\.RM6785-libperfmgr                        u:object_r:hal_power_default_exec:s0
-/(vendor|system/vendor)/bin/hw/android\.hardware\.vibrator-service\.RM6785                                u:object_r:hal_vibrator_default_exec:s0
+/(vendor|system/vendor)/bin/hw/android\.hardware\.vibrator-service\.mediatek                               u:object_r:hal_vibrator_default_exec:s0
 /(vendor|system/vendor)/bin/hw/android\.hardware\.wifi@[0-9]\.[0-9]-service\.RM6785                       u:object_r:hal_wifi_default_exec:s0
 /(vendor|system/vendor)/bin/hw/vendor\.lineage\.touch@[0-9]\.[0-9]-service\.RM6785                        u:object_r:hal_lineage_touch_default_exec:s0
 /(vendor|system/vendor)/bin/hw/vendor\.mediatek\.hardware\.mtkpower@1\.2-service\.stub                    u:object_r:mtk_hal_power_exec:s0
diff --git a/vendor.prop b/vendor.prop
index e0e07eb..dbb11c4 100644
--- a/vendor.prop
+++ b/vendor.prop
@@ -182,6 +182,11 @@
 ro.hardware.kmsetkey=trustonic
 ro.vendor.mtk_trustonic_tee_support=1
 
+# Vibrator
+ro.vendor.vibrator.hal.click.duration=70
+ro.vendor.vibrator.hal.tick.duration=60
+ro.vendor.vibrator.hal.texture_tick.duration=40
+
 # VM
 dalvik.vm.heapgrowthlimit=256m
 dalvik.vm.heapmaxfree=8m