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_common.h>
23 #include "notification-ex/group_item.h"
24 #include "notification-ex/group_item_implementation.h"
25 #include "notification-ex/item_factory.h"
26 #include "notification-ex/exception.h"
32 #define LOG_TAG "NOTIFICATION_EX"
33 #define GROUP_CHILDREN_KEY "__GROUP_CHILDREN_KEY__"
34 #define GROUP_CHILDREN_TYPE_KEY "__GROUP_CHILDREN_TYPE_KEY__"
35 #define GROUP_DIRECTION_KEY "__GROUP_DIRECTION_KEY__"
36 #define GROUP_DIRECTION_VERTICAL "VERTICAL"
37 #define GROUP_DIRECTION_HORIZONTAL "HORIZONTAL"
38 #define GROUP_APP_LABEL_KEY "__GROUP_APP_LABEL_KEY__"
41 namespace notification {
44 GroupItem::GroupItem(string id, shared_ptr<AbstractAction> action)
45 : AbstractItem(id, AbstractItem::Group), impl_(new Impl(this)) {
48 GroupItem::GroupItem(shared_ptr<AbstractAction> action)
49 : AbstractItem(AbstractItem::Group), impl_(new Impl(this)) {
52 GroupItem::Impl::Impl(GroupItem* parent)
54 LOGI("GroupItem created");
57 GroupItem::~GroupItem() {
60 Bundle GroupItem::Serialize() {
62 b = AbstractItem::Serialize();
63 b.Add(GROUP_DIRECTION_KEY, impl_->is_vertical_ ?
64 GROUP_DIRECTION_VERTICAL : GROUP_DIRECTION_HORIZONTAL);
65 if (!impl_->app_label_.empty())
66 b.Add(GROUP_APP_LABEL_KEY, impl_->app_label_);
68 if (impl_->children_list_.size() == 0)
71 vector<std::string> arr;
72 for (auto& i : impl_->children_list_) {
73 Bundle serialized = i.get()->Serialize();
75 GROUP_CHILDREN_TYPE_KEY, to_string((int)i.get()->GetType()));
76 char* str = strdup(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
79 b.Add(GROUP_CHILDREN_KEY, arr);
84 void GroupItem::Deserialize(Bundle b) {
85 AbstractItem::Deserialize(b);
87 (b.GetString(GROUP_DIRECTION_KEY) == GROUP_DIRECTION_VERTICAL);
88 impl_->app_label_ = b.GetString(GROUP_APP_LABEL_KEY);
90 vector<string> str_arr = b.GetStringArray(GROUP_CHILDREN_KEY);
91 if (str_arr.size() == 0)
95 for (string str : str_arr) {
96 Bundle serialized(str);
97 string type_str = serialized.GetString(GROUP_CHILDREN_TYPE_KEY);
98 AbstractItem::Type child_type =
99 static_cast<AbstractItem::Type>(strtol(type_str.c_str(), NULL, 10));
100 shared_ptr<AbstractItem> child = factory.CreateItem(child_type);
101 child.get()->Deserialize(serialized);
106 shared_ptr<AbstractItem> GroupItem::FindByID(std::string id) {
107 for (auto& i : impl_->children_list_) {
108 if (i.get()->GetId() == id)
114 void GroupItem::AddChild(shared_ptr<AbstractItem> child) {
115 impl_->children_list_.emplace_back(child);
118 void GroupItem::RemoveChild(string itemId) {
119 for (auto& i : impl_->children_list_) {
120 if (i.get()->GetId() == itemId) {
121 impl_->children_list_.remove(i);
127 list<shared_ptr<AbstractItem>> GroupItem::GetChildren() {
128 return impl_->children_list_;
131 void GroupItem::SetDirection(bool vertical) {
132 impl_->is_vertical_ = vertical;
135 bool GroupItem::IsVertical() {
136 return impl_->is_vertical_;
139 string GroupItem::GetAppLabel() {
140 if (impl_->app_label_.empty()) {
142 int ret = app_get_name(&name);
143 if (ret != APP_ERROR_NONE)
144 THROW(NOTIFICATION_ERROR_IO_ERROR);
145 impl_->app_label_ = string(name);
147 return impl_->app_label_;
151 } // namespace notification