|
|
|
@ -11,6 +11,7 @@ |
|
|
|
|
#include <cmath> |
|
|
|
|
#include <fstream> |
|
|
|
|
#include <iostream> |
|
|
|
|
#include <map> |
|
|
|
|
#include <thread> |
|
|
|
|
|
|
|
|
|
namespace aidl { |
|
|
|
@ -18,6 +19,14 @@ namespace android { |
|
|
|
|
namespace hardware { |
|
|
|
|
namespace vibrator { |
|
|
|
|
|
|
|
|
|
static std::map<Effect, int> CP_TRIGGER_EFFECTS { |
|
|
|
|
{ Effect::CLICK, 10 }, |
|
|
|
|
{ Effect::DOUBLE_CLICK, 14 }, |
|
|
|
|
{ Effect::HEAVY_CLICK, 23 }, |
|
|
|
|
{ Effect::TEXTURE_TICK, 50 }, |
|
|
|
|
{ Effect::TICK, 50 } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Write value to path and close file. |
|
|
|
|
*/ |
|
|
|
@ -48,6 +57,7 @@ static bool nodeExists(const std::string& path) { |
|
|
|
|
Vibrator::Vibrator() { |
|
|
|
|
mIsTimedOutVibrator = nodeExists(VIBRATOR_TIMEOUT_PATH); |
|
|
|
|
mHasTimedOutIntensity = nodeExists(VIBRATOR_INTENSITY_PATH); |
|
|
|
|
mHasTimedOutEffect = nodeExists(VIBRATOR_CP_TRIGGER_PATH); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { |
|
|
|
@ -68,13 +78,18 @@ ndk::ScopedAStatus Vibrator::off() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr<IVibratorCallback>& callback) { |
|
|
|
|
ndk::ScopedAStatus status = activate(timeoutMs); |
|
|
|
|
ndk::ScopedAStatus status; |
|
|
|
|
|
|
|
|
|
if (mHasTimedOutEffect) |
|
|
|
|
writeNode(VIBRATOR_CP_TRIGGER_PATH, 0); // Clear all effects
|
|
|
|
|
|
|
|
|
|
status = activate(timeoutMs); |
|
|
|
|
|
|
|
|
|
if (callback != nullptr) { |
|
|
|
|
std::thread([=] { |
|
|
|
|
LOG(INFO) << "Starting on on another thread"; |
|
|
|
|
LOG(DEBUG) << "Starting on on another thread"; |
|
|
|
|
usleep(timeoutMs * 1000); |
|
|
|
|
LOG(INFO) << "Notifying on complete"; |
|
|
|
|
LOG(DEBUG) << "Notifying on complete"; |
|
|
|
|
if (!callback->onComplete().isOk()) { |
|
|
|
|
LOG(ERROR) << "Failed to call onComplete"; |
|
|
|
|
} |
|
|
|
@ -86,26 +101,34 @@ ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr<IVibrat |
|
|
|
|
|
|
|
|
|
ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr<IVibratorCallback>& callback, int32_t* _aidl_return) { |
|
|
|
|
ndk::ScopedAStatus status; |
|
|
|
|
uint8_t amplitude; |
|
|
|
|
uint32_t ms; |
|
|
|
|
uint32_t amplitude = strengthToAmplitude(strength, &status); |
|
|
|
|
uint32_t ms = 1000; |
|
|
|
|
|
|
|
|
|
amplitude = strengthToAmplitude(strength, &status); |
|
|
|
|
if (!status.isOk()) { |
|
|
|
|
if (!status.isOk()) |
|
|
|
|
return status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
activate(0); |
|
|
|
|
setAmplitude(amplitude); |
|
|
|
|
|
|
|
|
|
ms = effectToMs(effect, &status); |
|
|
|
|
if (!status.isOk()) { |
|
|
|
|
return status; |
|
|
|
|
if (mHasTimedOutEffect && CP_TRIGGER_EFFECTS.find(effect) != CP_TRIGGER_EFFECTS.end()) { |
|
|
|
|
writeNode(VIBRATOR_CP_TRIGGER_PATH, CP_TRIGGER_EFFECTS[effect]); |
|
|
|
|
} else { |
|
|
|
|
if (mHasTimedOutEffect) |
|
|
|
|
writeNode(VIBRATOR_CP_TRIGGER_PATH, 0); // Clear previous effect
|
|
|
|
|
|
|
|
|
|
ms = effectToMs(effect, &status); |
|
|
|
|
|
|
|
|
|
if (!status.isOk()) |
|
|
|
|
return status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
status = activate(ms); |
|
|
|
|
|
|
|
|
|
if (callback != nullptr) { |
|
|
|
|
std::thread([=] { |
|
|
|
|
LOG(INFO) << "Starting perform on another thread"; |
|
|
|
|
LOG(DEBUG) << "Starting perform on another thread"; |
|
|
|
|
usleep(ms * 1000); |
|
|
|
|
LOG(INFO) << "Notifying perform complete"; |
|
|
|
|
LOG(DEBUG) << "Notifying perform complete"; |
|
|
|
|
callback->onComplete(); |
|
|
|
|
}).detach(); |
|
|
|
|
} |
|
|
|
@ -255,19 +278,18 @@ uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, ndk::ScopedAStatu |
|
|
|
|
|
|
|
|
|
uint32_t Vibrator::effectToMs(Effect effect, ndk::ScopedAStatus* status) { |
|
|
|
|
*status = ndk::ScopedAStatus::ok(); |
|
|
|
|
|
|
|
|
|
switch (effect) { |
|
|
|
|
case Effect::CLICK: |
|
|
|
|
return 20; |
|
|
|
|
return 10; |
|
|
|
|
case Effect::DOUBLE_CLICK: |
|
|
|
|
return 25; |
|
|
|
|
case Effect::HEAVY_CLICK: |
|
|
|
|
return 30; |
|
|
|
|
return 15; |
|
|
|
|
case Effect::TICK: |
|
|
|
|
case Effect::TEXTURE_TICK: |
|
|
|
|
case Effect::THUD: |
|
|
|
|
case Effect::POP: |
|
|
|
|
return 15; |
|
|
|
|
return 5; |
|
|
|
|
case Effect::HEAVY_CLICK: |
|
|
|
|
return 10; |
|
|
|
|
case Effect::RINGTONE_1: |
|
|
|
|
case Effect::RINGTONE_2: |
|
|
|
|
case Effect::RINGTONE_3: |
|
|
|
@ -283,9 +305,8 @@ uint32_t Vibrator::effectToMs(Effect effect, ndk::ScopedAStatus* status) { |
|
|
|
|
case Effect::RINGTONE_13: |
|
|
|
|
case Effect::RINGTONE_14: |
|
|
|
|
case Effect::RINGTONE_15: |
|
|
|
|
return 300; |
|
|
|
|
return 30000; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
*status = ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|