Skip to content

Commit acf40d2

Browse files
authored
Add exit application method to system channel (#46)
1 parent 1f13ad1 commit acf40d2

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

flutter/shell/platform/tizen/channels/platform_channel.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <map>
1010

11+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_result_functions.h"
1112
#include "flutter/shell/platform/common/json_method_codec.h"
1213
#include "flutter/shell/platform/tizen/channels/feedback_manager.h"
1314
#ifdef COMMON_PROFILE
@@ -26,6 +27,10 @@ constexpr char kChannelName[] = "flutter/platform";
2627
constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
2728
constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
2829
constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings";
30+
constexpr char kExitApplicationMethod[] = "System.exitApplication";
31+
constexpr char kRequestAppExitMethod[] = "System.requestAppExit";
32+
constexpr char kInitializationCompleteMethod[] =
33+
"System.initializationComplete";
2934
constexpr char kPlaySoundMethod[] = "SystemSound.play";
3035
constexpr char kHapticFeedbackVibrateMethod[] = "HapticFeedback.vibrate";
3136
constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
@@ -42,6 +47,14 @@ constexpr char kSetSystemUIOverlayStyleMethod[] =
4247
constexpr char kIsLiveTextInputAvailable[] =
4348
"LiveText.isLiveTextInputAvailable";
4449

50+
constexpr char kExitTypeKey[] = "type";
51+
constexpr char kExitTypeCancelable[] = "cancelable";
52+
constexpr char kExitTypeRequired[] = "required";
53+
constexpr char kExitRequestError[] = "ExitApplication error";
54+
constexpr char kExitResponseKey[] = "response";
55+
constexpr char kExitResponseCancel[] = "cancel";
56+
constexpr char kExitResponseExit[] = "exit";
57+
4558
constexpr char kTextKey[] = "text";
4659
constexpr char kValueKey[] = "value";
4760
constexpr char kTextPlainFormat[] = "text/plain";
@@ -140,6 +153,33 @@ void PlatformChannel::HandleMethodCall(
140153
document.AddMember(rapidjson::Value(kValueKey, allocator),
141154
rapidjson::Value(ClipboardHasStrings()), allocator);
142155
result->Success(document);
156+
} else if (method == kExitApplicationMethod) {
157+
rapidjson::Value::ConstMemberIterator iter =
158+
arguments->FindMember(kExitTypeKey);
159+
if (iter == arguments->MemberEnd()) {
160+
result->Error(kExitRequestError, "Invalid application exit request.");
161+
return;
162+
}
163+
164+
const std::string& exit_type = iter->value.GetString();
165+
rapidjson::Document document;
166+
document.SetObject();
167+
if (!initialization_complete_ || exit_type == kExitTypeRequired) {
168+
ui_app_exit();
169+
document.AddMember(kExitResponseKey, kExitResponseExit,
170+
document.GetAllocator());
171+
result->Success(document);
172+
} else if (exit_type == kExitTypeCancelable) {
173+
RequestAppExit(exit_type);
174+
document.AddMember(kExitResponseKey, kExitResponseCancel,
175+
document.GetAllocator());
176+
result->Success(document);
177+
} else {
178+
result->Error(kExitRequestError, "Invalid type.");
179+
}
180+
} else if (method == kInitializationCompleteMethod) {
181+
initialization_complete_ = true;
182+
result->Success();
143183
} else if (method == kRestoreSystemUiOverlaysMethod) {
144184
RestoreSystemUiOverlays();
145185
result->Success();
@@ -258,6 +298,37 @@ void PlatformChannel::RestoreSystemUiOverlays() {
258298
}
259299
}
260300

301+
void PlatformChannel::RequestAppExit(const std::string exit_type) {
302+
if (!initialization_complete_) {
303+
ui_app_exit();
304+
return;
305+
}
306+
auto callback = std::make_unique<MethodResultFunctions<rapidjson::Document>>(
307+
[this](const rapidjson::Document* response) {
308+
RequestAppExitSuccess(response);
309+
},
310+
nullptr, nullptr);
311+
auto args = std::make_unique<rapidjson::Document>();
312+
args->SetObject();
313+
args->AddMember(kExitTypeKey, exit_type, args->GetAllocator());
314+
channel_->InvokeMethod(kRequestAppExitMethod, std::move(args),
315+
std::move(callback));
316+
}
317+
318+
void PlatformChannel::RequestAppExitSuccess(const rapidjson::Document* result) {
319+
rapidjson::Value::ConstMemberIterator itr =
320+
result->FindMember(kExitResponseKey);
321+
if (itr == result->MemberEnd() || !itr->value.IsString()) {
322+
FT_LOG(Error) << "Application request response did not contain a valid "
323+
"response value";
324+
return;
325+
}
326+
const std::string& exit_type = itr->value.GetString();
327+
if (exit_type == kExitResponseExit) {
328+
ui_app_exit();
329+
}
330+
}
331+
261332
void PlatformChannel::SetEnabledSystemUiOverlays(
262333
const std::vector<std::string>& overlays) {
263334
if (auto* window = dynamic_cast<TizenWindow*>(view_)) {

flutter/shell/platform/tizen/channels/platform_channel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ class PlatformChannel {
4040
void SetClipboardData(const std::string& data);
4141
bool ClipboardHasStrings();
4242
void RestoreSystemUiOverlays();
43+
void RequestAppExit(const std::string exit_type);
44+
void RequestAppExitSuccess(const rapidjson::Document* result);
4345
void SetEnabledSystemUiOverlays(const std::vector<std::string>& overlays);
4446
void SetPreferredOrientations(const std::vector<std::string>& orientations);
4547

4648
std::unique_ptr<MethodChannel<rapidjson::Document>> channel_;
4749

50+
// Whether or not initialization is complete from the framework.
51+
bool initialization_complete_ = false;
52+
4853
// A reference to the native view managed by FlutterTizenView.
4954
TizenViewBase* view_ = nullptr;
5055

0 commit comments

Comments
 (0)