2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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.
17 #include "notification/notification_manager.h"
22 #include <app_control_internal.h>
23 #include <device/led.h>
24 #include <notification_internal.h>
25 #include <notification_list.h>
26 #include <app_common.h>
28 #include "common/converter.h"
29 #include "common/logger.h"
30 #include "common/scope_exit.h"
32 #include "notification/status_notification.h"
33 #include "notification/user_notification.h"
34 #include "notification/common_notification.h"
37 namespace notification {
39 using namespace common;
41 NotificationManager::NotificationManager() {
45 NotificationManager::~NotificationManager() {
49 NotificationManager* NotificationManager::GetInstance() {
51 static NotificationManager instance;
55 PlatformResult NotificationManager::Post(const picojson::object& args,
56 picojson::object& out) {
58 return StatusNotification::PostStatusNotification(args, false, &out);
61 PlatformResult NotificationManager::PostUserNoti(const picojson::object& args,
62 picojson::object& out) {
64 return UserNotification::PostUserNotification(args, false, &out);
67 PlatformResult NotificationManager::Update(const picojson::object& args) {
69 return StatusNotification::PostStatusNotification(args, true, nullptr);
72 PlatformResult NotificationManager::UpdateUserNoti(const picojson::object& args) {
74 return UserNotification::PostUserNotification(args, true, nullptr);
78 PlatformResult NotificationManager::Remove(const picojson::object& args) {
80 int id = std::stoi(FromJson<std::string>(args, "id"));
82 int ret = notification_delete_by_priv_id(nullptr, NOTIFICATION_TYPE_NONE, id);
83 if (NOTIFICATION_ERROR_NONE != ret) {
84 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
85 "Cannot remove notification error",
86 ("Cannot remove notification error: %d", ret));
89 return PlatformResult(ErrorCode::NO_ERROR);
92 PlatformResult NotificationManager::RemoveAll() {
94 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
95 if (NOTIFICATION_ERROR_NONE != ret) {
96 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
97 "Notification noti remove all failed",
98 ("Notification remove all failed: %d", ret));
101 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
102 if (NOTIFICATION_ERROR_NONE != ret) {
103 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
104 "Notification ongoing remove all failed",
105 ("Notification remove all failed: %d", ret));
108 return PlatformResult(ErrorCode::NO_ERROR);
111 PlatformResult NotificationManager::Get(const picojson::object& args,
112 picojson::object& out, bool is_new_impl) {
116 id = std::stoi(FromJson<std::string>(args, "id"));
118 LoggerE("Failed to convert string to int");
119 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
122 app_control_h app_control = nullptr;
123 notification_h noti_handle = nullptr;
127 app_control_destroy(app_control);
132 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
133 if (status.IsError())
135 LoggerE("Failed: GetNotiHandle");
139 status = StatusNotification::GetAppControl(noti_handle, &app_control);
140 if (status.IsError())
142 LoggerE("Failed: GetAppControl");
147 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
149 status = UserNotification::ToJson(id, noti_handle, app_control, &out);
151 if (status.IsError())
153 LoggerE("Failed: ToJson");
156 return PlatformResult(ErrorCode::NO_ERROR);
159 PlatformResult NotificationManager::GetAll(picojson::array& out, bool is_new_impl) {
161 notification_h noti = nullptr;
162 notification_list_h noti_list = nullptr;
163 notification_list_h noti_list_iter = nullptr;
164 char* package = nullptr;
166 if (APP_ERROR_NONE == app_get_id(&package)) {
167 LoggerD("Package id: %s", package);
169 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
170 "Could not get package id");
172 const std::string package_str = package;
175 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
176 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
177 if (NOTIFICATION_ERROR_NONE != ret) {
178 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
179 "Get notification list error",
180 ("Get notification list error: %d", ret));
183 SCOPE_EXIT { notification_free_list(noti_list); };
185 noti_list_iter = notification_list_get_head(noti_list);
187 while (nullptr != noti_list_iter) {
188 noti = notification_list_get_data(noti_list_iter);
189 if (nullptr != noti) {
191 ret = notification_get_id(noti, nullptr, ¬i_priv);
192 if (NOTIFICATION_ERROR_NONE != ret) {
193 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
194 "Cannot get notification id error",
195 ("Cannot get notification id, error: %d", ret));
198 app_control_h app_control = nullptr;
199 PlatformResult status =
200 StatusNotification::GetAppControl(noti, &app_control);
204 app_control_destroy(app_control);
208 if (status.IsError())
211 picojson::object noti_item = picojson::object();
215 StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
218 UserNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
220 if (status.IsError())
223 out.push_back(picojson::value(noti_item));
226 noti_list_iter = notification_list_get_next(noti_list_iter);
229 return PlatformResult(ErrorCode::NO_ERROR);
232 PlatformResult NotificationManager::PlayLEDCustomEffect(
233 const picojson::object& args) {
236 int timeOn = FromJson<double>(args, "timeOn");
237 int timeOff = FromJson<double>(args, "timeOff");
238 unsigned int color = FromJson<double>(args, "color");
240 auto& flags = FromJson<picojson::array>(args, "flags");
241 unsigned int platformFlags = 0;
242 for (auto flag : flags) {
243 std::string flagStr = JsonCast<std::string>(flag);
244 if ("LED_CUSTOM_DEFAULT" == flagStr)
245 platformFlags |= LED_CUSTOM_DEFAULT;
246 else if ("LED_CUSTOM_DUTY_ON" == flagStr)
247 platformFlags |= LED_CUSTOM_DUTY_ON;
251 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
252 if (DEVICE_ERROR_NONE != ret) {
253 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
254 ("Cannot play LED custom effect: ",ret));
257 return PlatformResult(ErrorCode::NO_ERROR);
260 PlatformResult NotificationManager::StopLEDCustomEffect() {
263 int ret = device_led_stop_custom();
264 if (DEVICE_ERROR_NONE != ret) {
265 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
266 ("Cannot stop LED custom effect: ",ret));
269 return PlatformResult(ErrorCode::NO_ERROR);
272 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
274 std::string name = FromJson<std::string>(args, "name");
275 notification_h noti_handle = nullptr;
278 notification_free(noti_handle);
281 PlatformResult status = PlatformResult(ErrorCode::NO_ERROR);
282 const auto& new_impl_it = args.find("newImpl");
283 if (args.end() != new_impl_it && new_impl_it->second.get<bool>()) {
284 status = UserNotification::GetNotiHandleFromJson(args, false, ¬i_handle);
286 status = StatusNotification::GetNotiHandleFromJson(args, false, ¬i_handle);
289 if (status.IsError()) {
293 ret = notification_save_as_template(noti_handle, name.c_str());
294 if (NOTIFICATION_ERROR_NONE != ret) {
295 LoggerD("Error: %d (%s)", ret, get_error_message(ret));
296 if (NOTIFICATION_ERROR_MAX_EXCEEDED == ret) {
297 return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
298 "Maximum number of templates exceeded",
299 ("Maximum number of templates exceeded, error: %d", ret));
301 return LogAndCreateResult(ErrorCode::ABORT_ERR,
302 "Saving template failed",
303 ("Saving template failed, error: %d", ret));
307 return PlatformResult(ErrorCode::NO_ERROR);
311 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
312 picojson::object& out) {
314 std::string name = FromJson<std::string>(args, "name");
316 notification_h noti_handle = nullptr;
317 noti_handle = notification_create_from_template(name.c_str());
319 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
320 "The template with given name not found",
321 ("The template with given name not found, handle is nullptr"));
325 notification_free(noti_handle);
328 //This method is designed to return only UserNotification objects
329 PlatformResult status = UserNotification::ToJson(0, noti_handle, nullptr, &out);
330 if (status.IsError())
332 LoggerE("Failed: ToJson");
335 return PlatformResult(ErrorCode::NO_ERROR);
338 } // namespace notification
339 } // namespace extension