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");
85 b = AbstractAction::Serialize();
86 b.Add(APPCONTROL_ACTION_KEY,
87 std::string(reinterpret_cast<const char*>(control_raw)));
89 bundle_free(control_b);
94 void AppControlAction::Deserialize(Bundle b) {
95 app_control_h app_control = nullptr;
97 std::string control_str;
100 AbstractAction::Deserialize(b);
102 control_str = b.GetString(APPCONTROL_ACTION_KEY);
104 b_ = bundle_decode((bundle_raw*)control_str.c_str(), control_str.length());
106 LOGE("bundle_decode failed");
110 ret = app_control_create(&app_control);
111 if (ret != APP_CONTROL_ERROR_NONE) {
112 LOGE("failed to create app_control");
117 ret = app_control_import_from_bundle(app_control, b_);
119 if (ret != APP_CONTROL_ERROR_NONE) {
120 LOGE("failed to make app_control from bundle");
121 app_control_destroy(app_control);
125 impl_->control_ = app_control;
128 bool AppControlAction::IsLocal() const {
129 return AbstractAction::IsLocal();
132 void AppControlAction::Execute(std::shared_ptr<AbstractItem> item) {
133 app_control_send_launch_request(impl_->control_, NULL, NULL);
136 std::string AppControlAction::GetExtra() const {
137 return AbstractAction::GetExtra();
140 void AppControlAction::SetAppControl(app_control_h app_control) {
141 if (impl_->control_ != nullptr)
142 app_control_destroy(impl_->control_);
144 app_control_h control_;
145 app_control_clone(&control_, app_control);
147 impl_->control_ = control_;
150 app_control_h AppControlAction::GetAppControl() const {
151 return impl_->control_;
155 } // namespace notification