Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HapticFeedback] Implement vibrate() #75

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ config("tizen_rootstrap_include_dirs") {
"$custom_sysroot/usr/include/elementary-1",
"$custom_sysroot/usr/include/ethumb-1",
"$custom_sysroot/usr/include/ethumb-client-1",
"$custom_sysroot/usr/include/feedback"
pwasowski2 marked this conversation as resolved.
Show resolved Hide resolved
]

lib_dirs = [ "$custom_sysroot/usr/lib" ]
Expand Down Expand Up @@ -121,6 +122,20 @@ template("embedder_for_profile") {
"wayland-client",
]

if (target_name == "mobile") {
libs += [
"capi-base-common",
"feedback"
]
}

if (target_name == "wearable") {
libs += [
"capi-base-common",
"feedback"
]
}
pwasowski2 marked this conversation as resolved.
Show resolved Hide resolved

defines = invoker.defines

if (use_evas_gl_renderer) {
Expand Down
120 changes: 120 additions & 0 deletions shell/platform/tizen/channels/platform_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@

static constexpr char kChannelName[] = "flutter/platform";

#if defined(MOBILE) || defined(WEARABLE)
pwasowski2 marked this conversation as resolved.
Show resolved Hide resolved
static constexpr char kUnsupportedHapticFeedbackError[] =
"HapticFeedback.vibrate() is not supported";
static constexpr char kPermissionDeniedHapticFeedbackError[] =
"No permission to run HapticFeedback.vibrate(). Add "
"\"http://tizen.org/privilege/feedback\" privilege to tizen-manifest.xml "
"to use this method";
static constexpr char kUnknownHapticFeedbackError[] =
"An unknown error on HapticFeedback.vibrate()";
#endif

PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger)
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())) {
Expand All @@ -24,6 +35,94 @@ PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger)

PlatformChannel::~PlatformChannel() {}

#if defined(MOBILE) || defined(WEARABLE)
namespace {

class FeedbackManager {
public:
enum class ResultCode {
OK,
pwasowski2 marked this conversation as resolved.
Show resolved Hide resolved
NOT_SUPPORTED_ERROR,
PERMISSION_DENIED_ERROR,
UNKNOWN_ERROR
};

FeedbackManager() {
FT_LOGD("Enter FeedbackManager::FeedbackManager()");

auto ret = feedback_initialize();
if (FEEDBACK_ERROR_NONE != ret) {
FT_LOGD("feedback_initialize() failed with error: [%d] (%s)", ret,
pwasowski2 marked this conversation as resolved.
Show resolved Hide resolved
get_error_message(ret));
return;
}
FT_LOGD("feedback_initialize() succeeded");
properly_initialized_ = true;

ret = feedback_is_supported_pattern(
FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP, &vibration_supported_);
if (FEEDBACK_ERROR_NONE != ret) {
FT_LOGD("feedback_is_supported_pattern() failed with error: [%d] (%s)",
ret, get_error_message(ret));
return;
}
FT_LOGD("feedback_is_supported_pattern() succeeded");
}

~FeedbackManager() {
FT_LOGD("Enter FeedbackManager::~FeedbackManager");

if (!properly_initialized_) {
return;
}

auto ret = feedback_deinitialize();
if (FEEDBACK_ERROR_NONE != ret) {
FT_LOGD("feedback_deinitialize() failed with error: [%d] (%s)", ret,
get_error_message(ret));
return;
}
FT_LOGD("feedback_deinitialize() succeeded");
}

ResultCode Vibrate() {
FT_LOGD("Enter FeedbackManager::Vibrate()");

if (!properly_initialized_) {
FT_LOGD(
"Cannot run Vibrate(): FeedbackManager.properly_initialized_ is "
"false");
return ResultCode::UNKNOWN_ERROR;
}

if (!vibration_supported_) {
FT_LOGD("HapticFeedback.Vibrate() is not supported");
return ResultCode::NOT_SUPPORTED_ERROR;
}

auto ret =
feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP);
FT_LOGD("feedback_play_type() returned: [%d] (%s)", ret,
get_error_message(ret));
if (FEEDBACK_ERROR_NONE == ret) {
return ResultCode::OK;
} else if (FEEDBACK_ERROR_PERMISSION_DENIED == ret) {
pwasowski2 marked this conversation as resolved.
Show resolved Hide resolved
return ResultCode::UNKNOWN_ERROR;
} else if (FEEDBACK_ERROR_NOT_SUPPORTED) {
return ResultCode::NOT_SUPPORTED_ERROR;
} else {
return ResultCode::UNKNOWN_ERROR;
}
}

private:
bool properly_initialized_ = false;
bool vibration_supported_ = false;
};

} // namespace
#endif

void PlatformChannel::HandleMethodCall(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
Expand All @@ -35,7 +134,28 @@ void PlatformChannel::HandleMethodCall(
} else if (method == "SystemSound.play") {
result->NotImplemented();
} else if (method == "HapticFeedback.vibrate") {
FT_LOGD("HapticFeedback.vibrate() call received");

#if defined(MOBILE) || defined(WEARABLE)
static FeedbackManager feedback_mgr;

auto ret = feedback_mgr.Vibrate();
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
if (FeedbackManager::ResultCode::OK == ret) {
result->Success();
return;
}

const std::string error_message = "Could not vibrate";
if (FeedbackManager::ResultCode::NOT_SUPPORTED_ERROR == ret) {
result->Error(kUnsupportedHapticFeedbackError, error_message);
} else if (FeedbackManager::ResultCode::PERMISSION_DENIED_ERROR == ret) {
result->Error(kPermissionDeniedHapticFeedbackError, error_message);
} else {
result->Error(kUnknownHapticFeedbackError, error_message);
}
#else
result->NotImplemented();
#endif
} else if (method == "Clipboard.getData") {
result->NotImplemented();
} else if (method == "Clipboard.setData") {
Expand Down