2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <app_control_internal.h>
22 #include "notification-ex/app_control_action.h"
23 #include "notification-ex/app_control_action_implementation.h"
24 #include "notification-ex/exception.h"
30 #define LOG_TAG "NOTIFICATION_EX"
31 #define APPCONTROL_ACTION_KEY "__APPCONTROL_ACTION_KEY__"
33 using namespace tizen_base;
35 namespace notification {
38 AppControlAction::AppControlAction(app_control_h app_control)
39 : AbstractAction(true), impl_(new Impl(this)) {
40 app_control_h control_;
41 app_control_clone(&control_, app_control);
42 impl_->control_ = control_;
45 AppControlAction::AppControlAction(app_control_h app_control, std::string extra)
46 : AbstractAction(true, extra), impl_(new Impl(this)) {
47 app_control_h control_;
48 app_control_clone(&control_, app_control);
49 impl_->control_ = control_;
52 AppControlAction::Impl::Impl(AppControlAction* parent)
54 LOGI("AppControlAction created");
57 AppControlAction::~AppControlAction() = default;
58 AppControlAction::Impl::~Impl() {
60 app_control_destroy(control_);
63 int AppControlAction::GetType() const {
64 return AbstractAction::AppControl;
67 Bundle AppControlAction::Serialize() const {
69 bundle* control_b = NULL;
70 bundle_raw* control_raw;
73 app_control_export_as_bundle(impl_->control_, &control_b);
74 if (control_b == NULL) {
75 LOGE("failed to get bundle from app_control");
79 bundle_encode(control_b, &control_raw, &len);
81 LOGE("bundle encode failed");
82 bundle_free(control_b);
86 b = AbstractAction::Serialize();
87 b.Add(APPCONTROL_ACTION_KEY,
88 std::string(reinterpret_cast<const char*>(control_raw)));
90 bundle_free(control_b);
95 void AppControlAction::Deserialize(Bundle b) {
96 app_control_h app_control = nullptr;
98 std::string control_str;
101 AbstractAction::Deserialize(b);
103 control_str = b.GetString(APPCONTROL_ACTION_KEY);
105 b_ = bundle_decode((bundle_raw*)control_str.c_str(), control_str.length());
107 LOGE("bundle_decode failed");
111 ret = app_control_create(&app_control);
112 if (ret != APP_CONTROL_ERROR_NONE) {
113 LOGE("failed to create app_control");
118 ret = app_control_import_from_bundle(app_control, b_);
120 if (ret != APP_CONTROL_ERROR_NONE) {
121 LOGE("failed to make app_control from bundle");
122 app_control_destroy(app_control);
126 impl_->control_ = app_control;
129 bool AppControlAction::IsLocal() const {
130 return AbstractAction::IsLocal();
133 void AppControlAction::Execute(std::shared_ptr<AbstractItem> item) {
134 app_control_send_launch_request(impl_->control_, NULL, NULL);
137 std::string AppControlAction::GetExtra() const {
138 return AbstractAction::GetExtra();
141 void AppControlAction::SetAppControl(app_control_h app_control) {
142 if (impl_->control_ != nullptr)
143 app_control_destroy(impl_->control_);
145 app_control_h control_;
146 app_control_clone(&control_, app_control);
148 impl_->control_ = control_;
151 app_control_h AppControlAction::GetAppControl() const {
152 return impl_->control_;
156 } // namespace notification