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) {
65 return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
66 "Not implemented yet",
67 ("Not implemented yet"));
70 PlatformResult NotificationManager::Update(const picojson::object& args) {
72 return StatusNotification::PostStatusNotification(args, true, NULL);
75 PlatformResult NotificationManager::UpdateUserNoti(const picojson::object& args) {
78 return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
79 "Not implemented yet",
80 ("Not implemented yet"));
84 PlatformResult NotificationManager::Remove(const picojson::object& args) {
86 int id = std::stoi(FromJson<std::string>(args, "id"));
88 int ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, id);
89 if (NOTIFICATION_ERROR_NONE != ret) {
90 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
91 "Cannot remove notification error",
92 ("Cannot remove notification error: %d", ret));
95 return PlatformResult(ErrorCode::NO_ERROR);
98 PlatformResult NotificationManager::RemoveAll() {
100 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
101 if (NOTIFICATION_ERROR_NONE != ret) {
102 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
103 "Notification noti remove all failed",
104 ("Notification remove all failed: %d", ret));
107 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
108 if (NOTIFICATION_ERROR_NONE != ret) {
109 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
110 "Notification ongoing remove all failed",
111 ("Notification remove all failed: %d", ret));
114 return PlatformResult(ErrorCode::NO_ERROR);
117 PlatformResult NotificationManager::Get(const picojson::object& args,
118 picojson::object& out, bool is_new_impl) {
122 id = std::stoi(FromJson<std::string>(args, "id"));
124 LoggerE("Failed to convert string to int");
125 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
128 app_control_h app_control = nullptr;
129 notification_h noti_handle = nullptr;
133 app_control_destroy(app_control);
138 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
139 if (status.IsError())
141 LoggerE("Failed: GetNotiHandle");
145 status = StatusNotification::GetAppControl(noti_handle, &app_control);
146 if (status.IsError())
148 LoggerE("Failed: GetAppControl");
153 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
155 status = UserNotification::ToJson(id, noti_handle, app_control, &out);
157 if (status.IsError())
159 LoggerE("Failed: ToJson");
162 return PlatformResult(ErrorCode::NO_ERROR);
165 PlatformResult NotificationManager::GetAll(picojson::array& out, bool is_new_impl) {
167 notification_h noti = nullptr;
168 notification_list_h noti_list = nullptr;
169 notification_list_h noti_list_iter = nullptr;
170 char* package = nullptr;
172 if (APP_ERROR_NONE == app_get_id(&package)) {
173 LoggerD("Package id: %s", package);
175 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
176 "Could not get package id");
178 const std::string package_str = package;
181 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
182 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
183 if (NOTIFICATION_ERROR_NONE != ret) {
184 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
185 "Get notification list error",
186 ("Get notification list error: %d", ret));
189 SCOPE_EXIT { notification_free_list(noti_list); };
191 noti_list_iter = notification_list_get_head(noti_list);
193 while (nullptr != noti_list_iter) {
194 noti = notification_list_get_data(noti_list_iter);
195 if (nullptr != noti) {
197 ret = notification_get_id(noti, NULL, ¬i_priv);
198 if (NOTIFICATION_ERROR_NONE != ret) {
199 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
200 "Cannot get notification id error",
201 ("Cannot get notification id, error: %d", ret));
204 app_control_h app_control = nullptr;
205 PlatformResult status =
206 StatusNotification::GetAppControl(noti, &app_control);
210 app_control_destroy(app_control);
214 if (status.IsError())
217 picojson::object noti_item = picojson::object();
221 StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
224 UserNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
226 if (status.IsError())
229 out.push_back(picojson::value(noti_item));
232 noti_list_iter = notification_list_get_next(noti_list_iter);
235 return PlatformResult(ErrorCode::NO_ERROR);
238 PlatformResult NotificationManager::PlayLEDCustomEffect(
239 const picojson::object& args) {
242 int timeOn = FromJson<double>(args, "timeOn");
243 int timeOff = FromJson<double>(args, "timeOff");
244 unsigned int color = FromJson<double>(args, "color");
246 auto& flags = FromJson<picojson::array>(args, "flags");
247 unsigned int platformFlags = 0;
248 for (auto flag : flags) {
249 std::string flagStr = JsonCast<std::string>(flag);
250 if (flagStr == "LED_CUSTOM_DEFAULT")
251 platformFlags |= LED_CUSTOM_DEFAULT;
252 else if (flagStr == "LED_CUSTOM_DUTY_ON")
253 platformFlags |= LED_CUSTOM_DUTY_ON;
257 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
258 if (DEVICE_ERROR_NONE != ret) {
259 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
260 ("Cannot play LED custom effect: ",ret));
263 return PlatformResult(ErrorCode::NO_ERROR);
266 PlatformResult NotificationManager::StopLEDCustomEffect() {
269 int ret = device_led_stop_custom();
270 if (DEVICE_ERROR_NONE != ret) {
271 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
272 ("Cannot stop LED custom effect: ",ret));
275 return PlatformResult(ErrorCode::NO_ERROR);
278 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
280 std::string name = FromJson<std::string>(args, "name");
281 notification_h noti_handle = nullptr;
284 notification_free(noti_handle);
287 // TODO use UserNotification method
288 PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, ¬i_handle);
290 if (status.IsError()) {
294 ret = notification_save_as_template(noti_handle, name.c_str());
295 if (NOTIFICATION_ERROR_NONE != ret) {
296 LoggerD("Error: %d (%s)", ret, get_error_message(ret));
297 if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) {
298 return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
299 "Maximum number of templates exceeded",
300 ("Maximum number of templates exceeded, error: %d", ret));
302 return LogAndCreateResult(ErrorCode::ABORT_ERR,
303 "Saving template failed",
304 ("Saving template failed, error: %d", ret));
308 return PlatformResult(ErrorCode::NO_ERROR);
312 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
313 picojson::object& out) {
315 std::string name = FromJson<std::string>(args, "name");
317 notification_h noti_handle = nullptr;
318 noti_handle = notification_create_from_template(name.c_str());
320 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
321 "The template with given name not found",
322 ("The template with given name not found, handle is NULL"));
326 notification_free(noti_handle);
329 // TODO use UserNotification method
330 PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out);
331 if (status.IsError())
333 LoggerE("Failed: ToJson");
336 return PlatformResult(ErrorCode::NO_ERROR);
339 } // namespace notification
340 } // namespace extension