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>
27 #include "common/converter.h"
28 #include "common/logger.h"
29 #include "common/scope_exit.h"
31 #include "notification/status_notification.h"
34 namespace notification {
36 using namespace common;
38 NotificationManager::NotificationManager() {
42 NotificationManager::~NotificationManager() {
46 NotificationManager* NotificationManager::GetInstance() {
48 static NotificationManager instance;
52 PlatformResult NotificationManager::Post(const picojson::object& args,
53 picojson::object& out) {
55 return StatusNotification::FromJson(args, false, &out);
58 PlatformResult NotificationManager::Update(const picojson::object& args) {
60 return StatusNotification::FromJson(args, true, NULL);
63 PlatformResult NotificationManager::Remove(const picojson::object& args) {
65 int id = std::stoi(FromJson<std::string>(args, "id"));
67 int ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, id);
68 if (ret != NOTIFICATION_ERROR_NONE) {
69 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
70 "Cannot remove notification error",
71 ("Cannot remove notification error: %d", ret));
74 return PlatformResult(ErrorCode::NO_ERROR);
77 PlatformResult NotificationManager::RemoveAll() {
79 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
80 if (ret != NOTIFICATION_ERROR_NONE) {
81 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
82 "Notification noti remove all failed",
83 ("Notification remove all failed: %d", ret));
86 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
87 if (ret != NOTIFICATION_ERROR_NONE) {
88 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
89 "Notification ongoing remove all failed",
90 ("Notification remove all failed: %d", ret));
93 return PlatformResult(ErrorCode::NO_ERROR);
96 PlatformResult NotificationManager::Get(const picojson::object& args,
97 picojson::object& out) {
101 id = std::stoi(FromJson<std::string>(args, "id"));
103 LoggerE("Failed to convert string to int");
104 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
107 app_control_h app_control = nullptr;
108 notification_h noti_handle = nullptr;
112 app_control_destroy(app_control);
117 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
118 if (status.IsError())
120 LoggerE("Failed: GetNotiHandle");
124 status = StatusNotification::GetAppControl(noti_handle, &app_control);
125 if (status.IsError())
127 LoggerE("Failed: GetAppControl");
130 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
131 if (status.IsError())
133 LoggerE("Failed: ToJson");
136 return PlatformResult(ErrorCode::NO_ERROR);
139 PlatformResult NotificationManager::GetAll(picojson::array& out) {
141 notification_h noti = nullptr;
142 notification_list_h noti_list = nullptr;
143 notification_list_h noti_list_iter = nullptr;
144 char* package = nullptr;
146 if (APP_ERROR_NONE == app_get_id(&package)) {
147 LoggerD("Package id: %s", package);
149 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
150 "Could not get package id");
152 const std::string package_str = package;
155 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
156 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
157 if (NOTIFICATION_ERROR_NONE != ret) {
158 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
159 "Get notification list error",
160 ("Get notification list error: %d", ret));
163 SCOPE_EXIT { notification_free_list(noti_list); };
165 noti_list_iter = notification_list_get_head(noti_list);
167 while (nullptr != noti_list_iter) {
168 noti = notification_list_get_data(noti_list_iter);
169 if (nullptr != noti) {
171 ret = notification_get_id(noti, NULL, ¬i_priv);
172 if (NOTIFICATION_ERROR_NONE != ret) {
173 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
174 "Cannot get notification id error",
175 ("Cannot get notification id, error: %d", ret));
178 app_control_h app_control = nullptr;
179 PlatformResult status =
180 StatusNotification::GetAppControl(noti, &app_control);
184 app_control_destroy(app_control);
188 if (status.IsError())
191 picojson::object noti_item = picojson::object();
194 StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
195 if (status.IsError())
198 out.push_back(picojson::value(noti_item));
201 noti_list_iter = notification_list_get_next(noti_list_iter);
204 return PlatformResult(ErrorCode::NO_ERROR);
207 PlatformResult NotificationManager::PlayLEDCustomEffect(
208 const picojson::object& args) {
211 int timeOn = FromJson<double>(args, "timeOn");
212 int timeOff = FromJson<double>(args, "timeOff");
213 unsigned int color = FromJson<double>(args, "color");
215 auto& flags = FromJson<picojson::array>(args, "flags");
216 unsigned int platformFlags = 0;
217 for (auto flag : flags) {
218 std::string flagStr = JsonCast<std::string>(flag);
219 if (flagStr == "LED_CUSTOM_DEFAULT")
220 platformFlags |= LED_CUSTOM_DEFAULT;
221 else if (flagStr == "LED_CUSTOM_DUTY_ON")
222 platformFlags |= LED_CUSTOM_DUTY_ON;
226 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
227 if (ret != DEVICE_ERROR_NONE) {
228 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
229 ("Cannot play LED custom effect: ",ret));
232 return PlatformResult(ErrorCode::NO_ERROR);
235 PlatformResult NotificationManager::StopLEDCustomEffect() {
238 int ret = device_led_stop_custom();
239 if (ret != DEVICE_ERROR_NONE) {
240 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
241 ("Cannot stop LED custom effect: ",ret));
244 return PlatformResult(ErrorCode::NO_ERROR);
247 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
249 std::string name = FromJson<std::string>(args, "name");
250 notification_h noti_handle = nullptr;
253 notification_free(noti_handle);
256 PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, ¬i_handle);
258 if (status.IsError()){
262 ret = notification_save_as_template(noti_handle, name.c_str());
263 if (ret != NOTIFICATION_ERROR_NONE) {
264 LoggerD("Error: %d (%s)", ret, get_error_message(ret));
265 if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) {
266 return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
267 "Maximum number of templates exceeded",
268 ("Maximum number of templates exceeded, error: %d", ret));
270 return LogAndCreateResult(ErrorCode::ABORT_ERR,
271 "Saving template failed",
272 ("Saving template failed, error: %d", ret));
276 return PlatformResult(ErrorCode::NO_ERROR);
280 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
281 picojson::object& out) {
283 std::string name = FromJson<std::string>(args, "name");
285 notification_h noti_handle = nullptr;
286 noti_handle = notification_create_from_template(name.c_str());
288 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
289 "The template with given name not found",
290 ("The template with given name not found, handle is NULL"));
294 notification_free(noti_handle);
297 PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out);
298 if (status.IsError())
300 LoggerE("Failed: ToJson");
303 return PlatformResult(ErrorCode::NO_ERROR);
306 } // namespace notification
307 } // namespace extension