Skip to content

Commit d36feb5

Browse files
authored
Refactor PlatformChannel (#251)
* Remove platform_channel_tizen.cc * Extract FeedbackManager into a new file * Extract TizenShell into a new file * Minor cleanups
1 parent 358b95b commit d36feb5

File tree

8 files changed

+299
-273
lines changed

8 files changed

+299
-273
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ template("embedder") {
102102
sources = [
103103
"channels/app_control.cc",
104104
"channels/app_control_channel.cc",
105+
"channels/feedback_manager.cc",
105106
"channels/key_event_channel.cc",
106107
"channels/lifecycle_channel.cc",
107108
"channels/navigation_channel.cc",
108109
"channels/platform_channel.cc",
109-
"channels/platform_channel_tizen.cc",
110110
"channels/platform_view_channel.cc",
111111
"channels/settings_channel.cc",
112112
"channels/text_input_channel.cc",
113+
"channels/tizen_shell.cc",
113114
"channels/window_channel.cc",
114115
"external_texture_pixel_gl.cc",
115116
"external_texture_surface_gl.cc",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "feedback_manager.h"
6+
7+
#include "flutter/shell/platform/tizen/logger.h"
8+
9+
namespace flutter {
10+
11+
FeedbackManager::FeedbackManager() {
12+
int ret = feedback_initialize();
13+
if (ret != FEEDBACK_ERROR_NONE) {
14+
FT_LOG(Error) << "feedback_initialize() failed with error: "
15+
<< get_error_message(ret);
16+
return;
17+
}
18+
initialized_ = true;
19+
}
20+
21+
FeedbackManager::~FeedbackManager() {
22+
if (initialized_) {
23+
feedback_deinitialize();
24+
}
25+
}
26+
27+
void FeedbackManager::Play(feedback_type_e type, feedback_pattern_e pattern) {
28+
if (!initialized_) {
29+
return;
30+
}
31+
int ret = feedback_play_type(type, pattern);
32+
if (ret == FEEDBACK_ERROR_PERMISSION_DENIED) {
33+
FT_LOG(Error)
34+
<< "Permission denied. Add the http://tizen.org/privilege/haptic "
35+
"privilege to tizen-manifest.xml to use haptic feedbacks.";
36+
} else if (ret != FEEDBACK_ERROR_NONE) {
37+
FT_LOG(Error) << "feedback_play_type() failed with error: "
38+
<< get_error_message(ret);
39+
}
40+
}
41+
42+
} // namespace flutter
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef EMBEDDER_FEEDBACK_MANAGER_H_
6+
#define EMBEDDER_FEEDBACK_MANAGER_H_
7+
8+
#include <feedback.h>
9+
10+
namespace flutter {
11+
12+
class FeedbackManager {
13+
public:
14+
static FeedbackManager& GetInstance() {
15+
static FeedbackManager instance;
16+
return instance;
17+
}
18+
19+
FeedbackManager(const FeedbackManager&) = delete;
20+
FeedbackManager& operator=(const FeedbackManager&) = delete;
21+
22+
void PlaySound() { Play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_GENERAL); }
23+
24+
void PlayTapSound() { Play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP); }
25+
26+
void Vibrate() { Play(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP); }
27+
28+
private:
29+
explicit FeedbackManager();
30+
~FeedbackManager();
31+
32+
void Play(feedback_type_e type, feedback_pattern_e pattern);
33+
34+
bool initialized_ = false;
35+
};
36+
37+
} // namespace flutter
38+
39+
#endif // EMBEDDER_FEEDBACK_MANAGER_H_

shell/platform/tizen/channels/platform_channel.cc

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
#include "platform_channel.h"
66

7+
#include <app.h>
8+
9+
#include <map>
10+
711
#include "flutter/shell/platform/common/json_method_codec.h"
12+
#include "flutter/shell/platform/tizen/channels/feedback_manager.h"
13+
#include "flutter/shell/platform/tizen/channels/tizen_shell.h"
814
#include "flutter/shell/platform/tizen/logger.h"
915

1016
namespace flutter {
@@ -19,18 +25,14 @@ constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings";
1925
constexpr char kPlaySoundMethod[] = "SystemSound.play";
2026
constexpr char kHapticFeedbackVibrateMethod[] = "HapticFeedback.vibrate";
2127
constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
22-
constexpr char kRestoreSystemUIOverlaysMethod[] =
28+
#ifdef COMMON_PROFILE
29+
constexpr char kRestoreSystemUiOverlaysMethod[] =
2330
"SystemChrome.restoreSystemUIOverlays";
24-
constexpr char kSetApplicationSwitcherDescriptionMethod[] =
25-
"SystemChrome.setApplicationSwitcherDescription";
26-
constexpr char kSetEnabledSystemUIModeMethod[] =
27-
"SystemChrome.setEnabledSystemUIMode";
28-
constexpr char kSetEnabledSystemUIOverlaysMethod[] =
31+
constexpr char kSetEnabledSystemUiOverlaysMethod[] =
2932
"SystemChrome.setEnabledSystemUIOverlays";
33+
#endif
3034
constexpr char kSetPreferredOrientationsMethod[] =
3135
"SystemChrome.setPreferredOrientations";
32-
constexpr char kSetSystemUIOverlayStyleMethod[] =
33-
"SystemChrome.setSystemUIOverlayStyle";
3436

3537
constexpr char kTextKey[] = "text";
3638
constexpr char kValueKey[] = "value";
@@ -40,6 +42,13 @@ constexpr char kUnknownClipboardFormatError[] =
4042
constexpr char kUnknownClipboardError[] =
4143
"Unknown error during clipboard data retrieval";
4244

45+
constexpr char kSoundTypeClick[] = "SystemSoundType.click";
46+
constexpr char kSystemUiOverlayBottom[] = "SystemUiOverlay.bottom";
47+
constexpr char kPortraitUp[] = "DeviceOrientation.portraitUp";
48+
constexpr char kPortraitDown[] = "DeviceOrientation.portraitDown";
49+
constexpr char kLandscapeLeft[] = "DeviceOrientation.landscapeLeft";
50+
constexpr char kLandscapeRight[] = "DeviceOrientation.landscapeRight";
51+
4352
// Naive implementation using std::string as a container of internal clipboard
4453
// data.
4554
std::string text_clipboard = "";
@@ -63,10 +72,10 @@ PlatformChannel::PlatformChannel(BinaryMessenger* messenger,
6372
PlatformChannel::~PlatformChannel() {}
6473

6574
void PlatformChannel::HandleMethodCall(
66-
const MethodCall<rapidjson::Document>& call,
75+
const MethodCall<rapidjson::Document>& method_call,
6776
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
68-
const auto method = call.method_name();
69-
const auto arguments = call.arguments();
77+
const auto& method = method_call.method_name();
78+
const auto* arguments = method_call.arguments();
7079

7180
if (method == kSystemNavigatorPopMethod) {
7281
SystemNavigatorPop();
@@ -111,17 +120,19 @@ void PlatformChannel::HandleMethodCall(
111120
document.AddMember(rapidjson::Value(kValueKey, allocator),
112121
rapidjson::Value(!text_clipboard.empty()), allocator);
113122
result->Success(document);
114-
} else if (method == kRestoreSystemUIOverlaysMethod) {
115-
RestoreSystemUIOverlays();
123+
#ifdef COMMON_PROFILE
124+
} else if (method == kRestoreSystemUiOverlaysMethod) {
125+
RestoreSystemUiOverlays();
116126
result->Success();
117-
} else if (method == kSetEnabledSystemUIOverlaysMethod) {
127+
} else if (method == kSetEnabledSystemUiOverlaysMethod) {
118128
const auto& list = arguments[0];
119129
std::vector<std::string> overlays;
120130
for (auto iter = list.Begin(); iter != list.End(); ++iter) {
121131
overlays.push_back(iter->GetString());
122132
}
123-
SetEnabledSystemUIOverlays(overlays);
133+
SetEnabledSystemUiOverlays(overlays);
124134
result->Success();
135+
#endif
125136
} else if (method == kSetPreferredOrientationsMethod) {
126137
const auto& list = arguments[0];
127138
std::vector<std::string> orientations;
@@ -130,16 +141,79 @@ void PlatformChannel::HandleMethodCall(
130141
}
131142
SetPreferredOrientations(orientations);
132143
result->Success();
133-
} else if (method == kSetApplicationSwitcherDescriptionMethod) {
134-
result->NotImplemented();
135-
} else if (method == kSetEnabledSystemUIModeMethod) {
136-
result->NotImplemented();
137-
} else if (method == kSetSystemUIOverlayStyleMethod) {
138-
result->NotImplemented();
139144
} else {
140145
FT_LOG(Info) << "Unimplemented method: " << method;
141146
result->NotImplemented();
142147
}
143148
}
144149

150+
void PlatformChannel::SystemNavigatorPop() {
151+
ui_app_exit();
152+
}
153+
154+
void PlatformChannel::PlaySystemSound(const std::string& sound_type) {
155+
if (sound_type == kSoundTypeClick) {
156+
FeedbackManager::GetInstance().PlayTapSound();
157+
} else {
158+
FeedbackManager::GetInstance().PlaySound();
159+
}
160+
}
161+
162+
void PlatformChannel::HapticFeedbackVibrate(const std::string& feedback_type) {
163+
FeedbackManager::GetInstance().Vibrate();
164+
}
165+
166+
void PlatformChannel::RestoreSystemUiOverlays() {
167+
if (!renderer_) {
168+
return;
169+
}
170+
auto& shell = TizenShell::GetInstance();
171+
shell.InitializeSoftkey(renderer_->GetWindowId());
172+
173+
if (shell.IsSoftkeyShown()) {
174+
shell.ShowSoftkey();
175+
} else {
176+
shell.HideSoftkey();
177+
}
178+
}
179+
180+
void PlatformChannel::SetEnabledSystemUiOverlays(
181+
const std::vector<std::string>& overlays) {
182+
if (!renderer_) {
183+
return;
184+
}
185+
auto& shell = TizenShell::GetInstance();
186+
shell.InitializeSoftkey(renderer_->GetWindowId());
187+
188+
if (std::find(overlays.begin(), overlays.end(), kSystemUiOverlayBottom) !=
189+
overlays.end()) {
190+
shell.ShowSoftkey();
191+
} else {
192+
shell.HideSoftkey();
193+
}
194+
}
195+
196+
void PlatformChannel::SetPreferredOrientations(
197+
const std::vector<std::string>& orientations) {
198+
if (!renderer_) {
199+
return;
200+
}
201+
static const std::map<std::string, int> orientation_mapping = {
202+
{kPortraitUp, 0},
203+
{kLandscapeLeft, 90},
204+
{kPortraitDown, 180},
205+
{kLandscapeRight, 270},
206+
};
207+
std::vector<int> rotations;
208+
for (const auto& orientation : orientations) {
209+
rotations.push_back(orientation_mapping.at(orientation));
210+
}
211+
if (rotations.empty()) {
212+
// The empty list causes the application to defer to the operating system
213+
// default.
214+
rotations = {0, 90, 180, 270};
215+
}
216+
renderer_->SetPreferredOrientations(rotations);
217+
}
218+
145219
} // namespace flutter

shell/platform/tizen/channels/platform_channel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class PlatformChannel {
2929
void SystemNavigatorPop();
3030
void PlaySystemSound(const std::string& sound_type);
3131
void HapticFeedbackVibrate(const std::string& feedback_type);
32-
void RestoreSystemUIOverlays();
33-
void SetEnabledSystemUIOverlays(const std::vector<std::string>& overlays);
32+
void RestoreSystemUiOverlays();
33+
void SetEnabledSystemUiOverlays(const std::vector<std::string>& overlays);
3434
void SetPreferredOrientations(const std::vector<std::string>& orientations);
3535

3636
std::unique_ptr<MethodChannel<rapidjson::Document>> channel_;

0 commit comments

Comments
 (0)