Skip to content

Commit

Permalink
Refactor PlatformChannel (#251)
Browse files Browse the repository at this point in the history
* Remove platform_channel_tizen.cc

* Extract FeedbackManager into a new file

* Extract TizenShell into a new file

* Minor cleanups
  • Loading branch information
swift-kim authored Mar 3, 2022
1 parent 358b95b commit d36feb5
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 273 deletions.
3 changes: 2 additions & 1 deletion shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ template("embedder") {
sources = [
"channels/app_control.cc",
"channels/app_control_channel.cc",
"channels/feedback_manager.cc",
"channels/key_event_channel.cc",
"channels/lifecycle_channel.cc",
"channels/navigation_channel.cc",
"channels/platform_channel.cc",
"channels/platform_channel_tizen.cc",
"channels/platform_view_channel.cc",
"channels/settings_channel.cc",
"channels/text_input_channel.cc",
"channels/tizen_shell.cc",
"channels/window_channel.cc",
"external_texture_pixel_gl.cc",
"external_texture_surface_gl.cc",
Expand Down
42 changes: 42 additions & 0 deletions shell/platform/tizen/channels/feedback_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "feedback_manager.h"

#include "flutter/shell/platform/tizen/logger.h"

namespace flutter {

FeedbackManager::FeedbackManager() {
int ret = feedback_initialize();
if (ret != FEEDBACK_ERROR_NONE) {
FT_LOG(Error) << "feedback_initialize() failed with error: "
<< get_error_message(ret);
return;
}
initialized_ = true;
}

FeedbackManager::~FeedbackManager() {
if (initialized_) {
feedback_deinitialize();
}
}

void FeedbackManager::Play(feedback_type_e type, feedback_pattern_e pattern) {
if (!initialized_) {
return;
}
int ret = feedback_play_type(type, pattern);
if (ret == FEEDBACK_ERROR_PERMISSION_DENIED) {
FT_LOG(Error)
<< "Permission denied. Add the http://tizen.org/privilege/haptic "
"privilege to tizen-manifest.xml to use haptic feedbacks.";
} else if (ret != FEEDBACK_ERROR_NONE) {
FT_LOG(Error) << "feedback_play_type() failed with error: "
<< get_error_message(ret);
}
}

} // namespace flutter
39 changes: 39 additions & 0 deletions shell/platform/tizen/channels/feedback_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EMBEDDER_FEEDBACK_MANAGER_H_
#define EMBEDDER_FEEDBACK_MANAGER_H_

#include <feedback.h>

namespace flutter {

class FeedbackManager {
public:
static FeedbackManager& GetInstance() {
static FeedbackManager instance;
return instance;
}

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

void PlaySound() { Play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_GENERAL); }

void PlayTapSound() { Play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP); }

void Vibrate() { Play(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP); }

private:
explicit FeedbackManager();
~FeedbackManager();

void Play(feedback_type_e type, feedback_pattern_e pattern);

bool initialized_ = false;
};

} // namespace flutter

#endif // EMBEDDER_FEEDBACK_MANAGER_H_
116 changes: 95 additions & 21 deletions shell/platform/tizen/channels/platform_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

#include "platform_channel.h"

#include <app.h>

#include <map>

#include "flutter/shell/platform/common/json_method_codec.h"
#include "flutter/shell/platform/tizen/channels/feedback_manager.h"
#include "flutter/shell/platform/tizen/channels/tizen_shell.h"
#include "flutter/shell/platform/tizen/logger.h"

namespace flutter {
Expand All @@ -19,18 +25,14 @@ constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings";
constexpr char kPlaySoundMethod[] = "SystemSound.play";
constexpr char kHapticFeedbackVibrateMethod[] = "HapticFeedback.vibrate";
constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
constexpr char kRestoreSystemUIOverlaysMethod[] =
#ifdef COMMON_PROFILE
constexpr char kRestoreSystemUiOverlaysMethod[] =
"SystemChrome.restoreSystemUIOverlays";
constexpr char kSetApplicationSwitcherDescriptionMethod[] =
"SystemChrome.setApplicationSwitcherDescription";
constexpr char kSetEnabledSystemUIModeMethod[] =
"SystemChrome.setEnabledSystemUIMode";
constexpr char kSetEnabledSystemUIOverlaysMethod[] =
constexpr char kSetEnabledSystemUiOverlaysMethod[] =
"SystemChrome.setEnabledSystemUIOverlays";
#endif
constexpr char kSetPreferredOrientationsMethod[] =
"SystemChrome.setPreferredOrientations";
constexpr char kSetSystemUIOverlayStyleMethod[] =
"SystemChrome.setSystemUIOverlayStyle";

constexpr char kTextKey[] = "text";
constexpr char kValueKey[] = "value";
Expand All @@ -40,6 +42,13 @@ constexpr char kUnknownClipboardFormatError[] =
constexpr char kUnknownClipboardError[] =
"Unknown error during clipboard data retrieval";

constexpr char kSoundTypeClick[] = "SystemSoundType.click";
constexpr char kSystemUiOverlayBottom[] = "SystemUiOverlay.bottom";
constexpr char kPortraitUp[] = "DeviceOrientation.portraitUp";
constexpr char kPortraitDown[] = "DeviceOrientation.portraitDown";
constexpr char kLandscapeLeft[] = "DeviceOrientation.landscapeLeft";
constexpr char kLandscapeRight[] = "DeviceOrientation.landscapeRight";

// Naive implementation using std::string as a container of internal clipboard
// data.
std::string text_clipboard = "";
Expand All @@ -63,10 +72,10 @@ PlatformChannel::PlatformChannel(BinaryMessenger* messenger,
PlatformChannel::~PlatformChannel() {}

void PlatformChannel::HandleMethodCall(
const MethodCall<rapidjson::Document>& call,
const MethodCall<rapidjson::Document>& method_call,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
const auto method = call.method_name();
const auto arguments = call.arguments();
const auto& method = method_call.method_name();
const auto* arguments = method_call.arguments();

if (method == kSystemNavigatorPopMethod) {
SystemNavigatorPop();
Expand Down Expand Up @@ -111,17 +120,19 @@ void PlatformChannel::HandleMethodCall(
document.AddMember(rapidjson::Value(kValueKey, allocator),
rapidjson::Value(!text_clipboard.empty()), allocator);
result->Success(document);
} else if (method == kRestoreSystemUIOverlaysMethod) {
RestoreSystemUIOverlays();
#ifdef COMMON_PROFILE
} else if (method == kRestoreSystemUiOverlaysMethod) {
RestoreSystemUiOverlays();
result->Success();
} else if (method == kSetEnabledSystemUIOverlaysMethod) {
} else if (method == kSetEnabledSystemUiOverlaysMethod) {
const auto& list = arguments[0];
std::vector<std::string> overlays;
for (auto iter = list.Begin(); iter != list.End(); ++iter) {
overlays.push_back(iter->GetString());
}
SetEnabledSystemUIOverlays(overlays);
SetEnabledSystemUiOverlays(overlays);
result->Success();
#endif
} else if (method == kSetPreferredOrientationsMethod) {
const auto& list = arguments[0];
std::vector<std::string> orientations;
Expand All @@ -130,16 +141,79 @@ void PlatformChannel::HandleMethodCall(
}
SetPreferredOrientations(orientations);
result->Success();
} else if (method == kSetApplicationSwitcherDescriptionMethod) {
result->NotImplemented();
} else if (method == kSetEnabledSystemUIModeMethod) {
result->NotImplemented();
} else if (method == kSetSystemUIOverlayStyleMethod) {
result->NotImplemented();
} else {
FT_LOG(Info) << "Unimplemented method: " << method;
result->NotImplemented();
}
}

void PlatformChannel::SystemNavigatorPop() {
ui_app_exit();
}

void PlatformChannel::PlaySystemSound(const std::string& sound_type) {
if (sound_type == kSoundTypeClick) {
FeedbackManager::GetInstance().PlayTapSound();
} else {
FeedbackManager::GetInstance().PlaySound();
}
}

void PlatformChannel::HapticFeedbackVibrate(const std::string& feedback_type) {
FeedbackManager::GetInstance().Vibrate();
}

void PlatformChannel::RestoreSystemUiOverlays() {
if (!renderer_) {
return;
}
auto& shell = TizenShell::GetInstance();
shell.InitializeSoftkey(renderer_->GetWindowId());

if (shell.IsSoftkeyShown()) {
shell.ShowSoftkey();
} else {
shell.HideSoftkey();
}
}

void PlatformChannel::SetEnabledSystemUiOverlays(
const std::vector<std::string>& overlays) {
if (!renderer_) {
return;
}
auto& shell = TizenShell::GetInstance();
shell.InitializeSoftkey(renderer_->GetWindowId());

if (std::find(overlays.begin(), overlays.end(), kSystemUiOverlayBottom) !=
overlays.end()) {
shell.ShowSoftkey();
} else {
shell.HideSoftkey();
}
}

void PlatformChannel::SetPreferredOrientations(
const std::vector<std::string>& orientations) {
if (!renderer_) {
return;
}
static const std::map<std::string, int> orientation_mapping = {
{kPortraitUp, 0},
{kLandscapeLeft, 90},
{kPortraitDown, 180},
{kLandscapeRight, 270},
};
std::vector<int> rotations;
for (const auto& orientation : orientations) {
rotations.push_back(orientation_mapping.at(orientation));
}
if (rotations.empty()) {
// The empty list causes the application to defer to the operating system
// default.
rotations = {0, 90, 180, 270};
}
renderer_->SetPreferredOrientations(rotations);
}

} // namespace flutter
4 changes: 2 additions & 2 deletions shell/platform/tizen/channels/platform_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class PlatformChannel {
void SystemNavigatorPop();
void PlaySystemSound(const std::string& sound_type);
void HapticFeedbackVibrate(const std::string& feedback_type);
void RestoreSystemUIOverlays();
void SetEnabledSystemUIOverlays(const std::vector<std::string>& overlays);
void RestoreSystemUiOverlays();
void SetEnabledSystemUiOverlays(const std::vector<std::string>& overlays);
void SetPreferredOrientations(const std::vector<std::string>& orientations);

std::unique_ptr<MethodChannel<rapidjson::Document>> channel_;
Expand Down
Loading

0 comments on commit d36feb5

Please sign in to comment.