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/common_notification.h"
36 namespace notification {
38 using namespace common;
40 NotificationManager::NotificationManager() {
44 NotificationManager::~NotificationManager() {
48 NotificationManager* NotificationManager::GetInstance() {
50 static NotificationManager instance;
54 PlatformResult NotificationManager::Post(const picojson::object& args,
55 picojson::object& out) {
57 return StatusNotification::FromJson(args, false, &out);
60 PlatformResult NotificationManager::PostUserNoti(const picojson::object& args,
61 picojson::object& out) {
64 return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
65 "Not implemented yet",
66 ("Not implemented yet"));
69 PlatformResult NotificationManager::Update(const picojson::object& args) {
71 return StatusNotification::FromJson(args, true, NULL);
74 PlatformResult NotificationManager::UpdateUserNoti(const picojson::object& args) {
77 return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
78 "Not implemented yet",
79 ("Not implemented yet"));
83 PlatformResult NotificationManager::Remove(const picojson::object& args) {
85 int id = std::stoi(FromJson<std::string>(args, "id"));
87 int ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, id);
88 if (ret != NOTIFICATION_ERROR_NONE) {
89 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
90 "Cannot remove notification error",
91 ("Cannot remove notification error: %d", ret));
94 return PlatformResult(ErrorCode::NO_ERROR);
97 PlatformResult NotificationManager::RemoveAll() {
99 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
100 if (ret != NOTIFICATION_ERROR_NONE) {
101 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
102 "Notification noti remove all failed",
103 ("Notification remove all failed: %d", ret));
106 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
107 if (ret != NOTIFICATION_ERROR_NONE) {
108 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
109 "Notification ongoing remove all failed",
110 ("Notification remove all failed: %d", ret));
113 return PlatformResult(ErrorCode::NO_ERROR);
116 PlatformResult NotificationManager::Get(const picojson::object& args,
117 picojson::object& out) {
121 id = std::stoi(FromJson<std::string>(args, "id"));
123 LoggerE("Failed to convert string to int");
124 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
127 app_control_h app_control = nullptr;
128 notification_h noti_handle = nullptr;
132 app_control_destroy(app_control);
137 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
138 if (status.IsError())
140 LoggerE("Failed: GetNotiHandle");
144 status = StatusNotification::GetAppControl(noti_handle, &app_control);
145 if (status.IsError())
147 LoggerE("Failed: GetAppControl");
150 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
151 if (status.IsError())
153 LoggerE("Failed: ToJson");
156 return PlatformResult(ErrorCode::NO_ERROR);
159 PlatformResult NotificationManager::GetUserNoti(const picojson::object& args,
160 picojson::object& out) {
163 return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
164 "Not implemented yet",
165 ("Not implemented yet"));
168 PlatformResult NotificationManager::GetAll(picojson::array& out) {
170 notification_h noti = nullptr;
171 notification_list_h noti_list = nullptr;
172 notification_list_h noti_list_iter = nullptr;
173 char* package = nullptr;
175 if (APP_ERROR_NONE == app_get_id(&package)) {
176 LoggerD("Package id: %s", package);
178 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
179 "Could not get package id");
181 const std::string package_str = package;
184 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
185 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
186 if (NOTIFICATION_ERROR_NONE != ret) {
187 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
188 "Get notification list error",
189 ("Get notification list error: %d", ret));
192 SCOPE_EXIT { notification_free_list(noti_list); };
194 noti_list_iter = notification_list_get_head(noti_list);
196 while (nullptr != noti_list_iter) {
197 noti = notification_list_get_data(noti_list_iter);
198 if (nullptr != noti) {
200 ret = notification_get_id(noti, NULL, ¬i_priv);
201 if (NOTIFICATION_ERROR_NONE != ret) {
202 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
203 "Cannot get notification id error",
204 ("Cannot get notification id, error: %d", ret));
207 app_control_h app_control = nullptr;
208 PlatformResult status =
209 StatusNotification::GetAppControl(noti, &app_control);
213 app_control_destroy(app_control);
217 if (status.IsError())
220 picojson::object noti_item = picojson::object();
223 StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
224 if (status.IsError())
227 out.push_back(picojson::value(noti_item));
230 noti_list_iter = notification_list_get_next(noti_list_iter);
233 return PlatformResult(ErrorCode::NO_ERROR);
236 PlatformResult NotificationManager::GetAllUserNoti(picojson::array& out) {
239 return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
240 "Not implemented yet",
241 ("Not implemented yet"));
244 PlatformResult NotificationManager::PlayLEDCustomEffect(
245 const picojson::object& args) {
248 int timeOn = FromJson<double>(args, "timeOn");
249 int timeOff = FromJson<double>(args, "timeOff");
250 unsigned int color = FromJson<double>(args, "color");
252 auto& flags = FromJson<picojson::array>(args, "flags");
253 unsigned int platformFlags = 0;
254 for (auto flag : flags) {
255 std::string flagStr = JsonCast<std::string>(flag);
256 if (flagStr == "LED_CUSTOM_DEFAULT")
257 platformFlags |= LED_CUSTOM_DEFAULT;
258 else if (flagStr == "LED_CUSTOM_DUTY_ON")
259 platformFlags |= LED_CUSTOM_DUTY_ON;
263 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
264 if (ret != DEVICE_ERROR_NONE) {
265 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
266 ("Cannot play LED custom effect: ",ret));
269 return PlatformResult(ErrorCode::NO_ERROR);
272 PlatformResult NotificationManager::StopLEDCustomEffect() {
275 int ret = device_led_stop_custom();
276 if (ret != DEVICE_ERROR_NONE) {
277 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
278 ("Cannot stop LED custom effect: ",ret));
281 return PlatformResult(ErrorCode::NO_ERROR);
284 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
286 std::string name = FromJson<std::string>(args, "name");
287 notification_h noti_handle = nullptr;
290 notification_free(noti_handle);
293 PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, ¬i_handle);
295 if (status.IsError()){
299 ret = notification_save_as_template(noti_handle, name.c_str());
300 if (ret != NOTIFICATION_ERROR_NONE) {
301 LoggerD("Error: %d (%s)", ret, get_error_message(ret));
302 if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) {
303 return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
304 "Maximum number of templates exceeded",
305 ("Maximum number of templates exceeded, error: %d", ret));
307 return LogAndCreateResult(ErrorCode::ABORT_ERR,
308 "Saving template failed",
309 ("Saving template failed, error: %d", ret));
313 return PlatformResult(ErrorCode::NO_ERROR);
317 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
318 picojson::object& out) {
320 std::string name = FromJson<std::string>(args, "name");
322 notification_h noti_handle = nullptr;
323 noti_handle = notification_create_from_template(name.c_str());
325 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
326 "The template with given name not found",
327 ("The template with given name not found, handle is NULL"));
331 notification_free(noti_handle);
334 PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out);
335 if (status.IsError())
337 LoggerE("Failed: ToJson");
340 return PlatformResult(ErrorCode::NO_ERROR);
343 } // namespace notification
344 } // namespace extension