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 all 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
18 changes: 18 additions & 0 deletions shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ config("tizen_rootstrap_include_dirs") {
"$custom_sysroot/usr/include/emile-1",
"$custom_sysroot/usr/include/eo-1",
"$custom_sysroot/usr/include/evas-1",
"$custom_sysroot/usr/include/feedback",
"$custom_sysroot/usr/include/system",
"$custom_sysroot/usr/include/wayland-extension",

# For Evas_GL.
"$custom_sysroot/usr/include/ecore-con-1",
"$custom_sysroot/usr/include/ecore-file-1",
Expand All @@ -77,6 +79,8 @@ config("tizen_rootstrap_include_dirs") {
template("embedder_for_profile") {
forward_variables_from(invoker, [ "use_evas_gl_renderer" ])

profile = target_name

if (!defined(use_evas_gl_renderer)) {
use_evas_gl_renderer = false
}
Expand Down Expand Up @@ -121,6 +125,20 @@ template("embedder_for_profile") {
"wayland-client",
]

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

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

defines = invoker.defines

if (use_evas_gl_renderer) {
Expand Down
175 changes: 174 additions & 1 deletion shell/platform/tizen/channels/platform_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "platform_channel.h"

#include <app.h>
#include <feedback.h>

#include "flutter/shell/platform/common/cpp/json_method_codec.h"
#include "flutter/shell/platform/tizen/tizen_log.h"
Expand All @@ -24,6 +25,146 @@ PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger)

PlatformChannel::~PlatformChannel() {}

namespace {

class FeedbackManager {
public:
enum class ResultCode {
kOk,
kNotSupportedError,
kPermissionDeniedError,
kUnknownError
};

static std::string GetVibrateVariantName(const char* haptic_feedback_type) {
FT_LOGD(
"Enter FeedbackManager::GetVibrateVariantName(): haptic_feedback_type: "
"(%s)",
haptic_feedback_type);

if (!haptic_feedback_type) {
return "HapticFeedback.vibrate";
}

const size_t kPrefixToRemoveLen = strlen("HapticFeedbackType.");

assert(strlen(haptic_feedback_type) >= kPrefixToRemoveLen);

const std::string kHapticFeedbackPrefix = "HapticFeedback.";

return kHapticFeedbackPrefix +
std::string{haptic_feedback_type + kPrefixToRemoveLen};
}

static std::string GetErrorMessage(const std::string& method_name,
ResultCode result_code) {
FT_LOGD(
"Enter FeedbackManager::GetErrorMessage(): method_name: (%s), "
"result_code: [%d]",
method_name.c_str(), static_cast<int>(result_code));

switch (result_code) {
case ResultCode::kNotSupportedError:
return method_name + "() is not supported";
case ResultCode::kPermissionDeniedError:
return std::string{"No permission to run "} + method_name +
"(). Add "
"\"http://tizen.org/privilege/feedback\" privilege to "
"tizen-manifest.xml "
"to use this method";
case ResultCode::kUnknownError:
default:
return std::string{"An unknown error on "} + method_name + "() call";
}
}

#if defined(MOBILE_PROFILE) || defined(WEARABLE_PROFILE)

static FeedbackManager& GetInstance() {
FT_LOGD("Enter FeedbackManager::GetInstance()");

static FeedbackManager instance;
return instance;
}

FeedbackManager(const FeedbackManager&) = delete;
FeedbackManager& operator=(const FeedbackManager&) = delete;

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

if (ResultCode::kOk != initialization_status_) {
FT_LOGE("Cannot run Vibrate(): initialization_status_: [%d]",
static_cast<int>(initialization_status_));
return initialization_status_;
}

auto ret =
feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP);
if (FEEDBACK_ERROR_NONE == ret) {
FT_LOGD("feedback_play_type() succeeded");
return ResultCode::kOk;
}
FT_LOGE("feedback_play_type() failed with error: [%d] (%s)", ret,
get_error_message(ret));

return NativeErrorToResultCode(ret);
}

private:
static ResultCode NativeErrorToResultCode(int native_error_code) {
FT_LOGD("Enter NativeErrorToResultCode: native_error_code: [%d]",
native_error_code);

switch (native_error_code) {
case FEEDBACK_ERROR_NONE:
return ResultCode::kOk;
case FEEDBACK_ERROR_NOT_SUPPORTED:
return ResultCode::kNotSupportedError;
case FEEDBACK_ERROR_PERMISSION_DENIED:
return ResultCode::kPermissionDeniedError;
case FEEDBACK_ERROR_OPERATION_FAILED:
case FEEDBACK_ERROR_INVALID_PARAMETER:
case FEEDBACK_ERROR_NOT_INITIALIZED:
default:
return ResultCode::kUnknownError;
}
}

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

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

initialization_status_ = ResultCode::kOk;
}

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

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

ResultCode initialization_status_ = ResultCode::kUnknownError;

#endif // defined(MOBILE_PROFILE) || defined(WEARABLE_PROFILE)
};

} // namespace

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

const std::string error_message = "Could not vibrate";

#if defined(MOBILE_PROFILE) || defined(WEARABLE_PROFILE)
/*
* We use a single type of vibration (FEEDBACK_PATTERN_SIP) to implement
* HapticFeedback's vibrate, lightImpact, mediumImpact, heavyImpact
* and selectionClick methods, because Tizen's "feedback" module
* has no dedicated vibration types for them.
* Thus, we ignore the "arguments" contents for "HapticFeedback.vibrate"
* calls.
*/

auto ret = FeedbackManager::GetInstance().Vibrate();
if (FeedbackManager::ResultCode::kOk == ret) {
result->Success();
return;
}

const auto vibrate_variant_name =
FeedbackManager::GetVibrateVariantName(call.arguments()[0].GetString());
const auto error_cause =
FeedbackManager::GetErrorMessage(vibrate_variant_name, ret);
FT_LOGE("%s: %s", error_cause.c_str(), error_message.c_str());
#else
const auto vibrate_variant_name =
FeedbackManager::GetVibrateVariantName(call.arguments()[0].GetString());
const auto error_cause = FeedbackManager::GetErrorMessage(
vibrate_variant_name, FeedbackManager::ResultCode::kNotSupportedError);
#endif // defined(MOBILE_PROFILE) || defined(WEARABLE_PROFILE)

result->Error(error_cause, error_message);
} else if (method == "Clipboard.getData") {
result->NotImplemented();
} else if (method == "Clipboard.setData") {
Expand Down