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"
35 namespace notification {
37 using namespace common;
39 NotificationManager::NotificationManager() {
43 NotificationManager::~NotificationManager() {
47 NotificationManager* NotificationManager::GetInstance() {
49 static NotificationManager instance;
53 PlatformResult NotificationManager::Post(const picojson::object& args,
54 picojson::object& out) {
56 return StatusNotification::FromJson(args, false, &out);
59 PlatformResult NotificationManager::Update(const picojson::object& args) {
61 return StatusNotification::FromJson(args, true, NULL);
64 PlatformResult NotificationManager::Remove(const picojson::object& args) {
66 int id = std::stoi(FromJson<std::string>(args, "id"));
68 int ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, id);
69 if (ret != NOTIFICATION_ERROR_NONE) {
70 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
71 "Cannot remove notification error",
72 ("Cannot remove notification error: %d", ret));
75 return PlatformResult(ErrorCode::NO_ERROR);
78 PlatformResult NotificationManager::RemoveAll() {
80 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
81 if (ret != NOTIFICATION_ERROR_NONE) {
82 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
83 "Notification noti remove all failed",
84 ("Notification remove all failed: %d", ret));
87 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
88 if (ret != NOTIFICATION_ERROR_NONE) {
89 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
90 "Notification ongoing remove all failed",
91 ("Notification remove all failed: %d", ret));
94 return PlatformResult(ErrorCode::NO_ERROR);
97 PlatformResult NotificationManager::Get(const picojson::object& args,
98 picojson::object& out) {
102 id = std::stoi(FromJson<std::string>(args, "id"));
104 LoggerE("Failed to convert string to int");
105 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
108 app_control_h app_control = nullptr;
109 notification_h noti_handle = nullptr;
113 app_control_destroy(app_control);
118 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
119 if (status.IsError())
121 LoggerE("Failed: GetNotiHandle");
125 status = StatusNotification::GetAppControl(noti_handle, &app_control);
126 if (status.IsError())
128 LoggerE("Failed: GetAppControl");
131 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
132 if (status.IsError())
134 LoggerE("Failed: ToJson");
137 return PlatformResult(ErrorCode::NO_ERROR);
140 PlatformResult NotificationManager::GetAll(picojson::array& out) {
142 notification_h noti = nullptr;
143 notification_list_h noti_list = nullptr;
144 notification_list_h noti_list_iter = nullptr;
145 char* package = nullptr;
147 if (APP_ERROR_NONE == app_get_id(&package)) {
148 LoggerD("Package id: %s", package);
150 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
151 "Could not get package id");
153 const std::string package_str = package;
156 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
157 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
158 if (NOTIFICATION_ERROR_NONE != ret) {
159 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
160 "Get notification list error",
161 ("Get notification list error: %d", ret));
164 SCOPE_EXIT { notification_free_list(noti_list); };
166 noti_list_iter = notification_list_get_head(noti_list);
168 while (nullptr != noti_list_iter) {
169 noti = notification_list_get_data(noti_list_iter);
170 if (nullptr != noti) {
172 ret = notification_get_id(noti, NULL, ¬i_priv);
173 if (NOTIFICATION_ERROR_NONE != ret) {
174 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
175 "Cannot get notification id error",
176 ("Cannot get notification id, error: %d", ret));
179 app_control_h app_control = nullptr;
180 PlatformResult status =
181 StatusNotification::GetAppControl(noti, &app_control);
185 app_control_destroy(app_control);
189 if (status.IsError())
192 picojson::object noti_item = picojson::object();
195 StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
196 if (status.IsError())
199 out.push_back(picojson::value(noti_item));
202 noti_list_iter = notification_list_get_next(noti_list_iter);
205 return PlatformResult(ErrorCode::NO_ERROR);
208 PlatformResult NotificationManager::PlayLEDCustomEffect(
209 const picojson::object& args) {
212 int timeOn = FromJson<double>(args, "timeOn");
213 int timeOff = FromJson<double>(args, "timeOff");
214 unsigned int color = FromJson<double>(args, "color");
216 auto& flags = FromJson<picojson::array>(args, "flags");
217 unsigned int platformFlags = 0;
218 for (auto flag : flags) {
219 std::string flagStr = JsonCast<std::string>(flag);
220 if (flagStr == "LED_CUSTOM_DEFAULT")
221 platformFlags |= LED_CUSTOM_DEFAULT;
222 else if (flagStr == "LED_CUSTOM_DUTY_ON")
223 platformFlags |= LED_CUSTOM_DUTY_ON;
227 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
228 if (ret != DEVICE_ERROR_NONE) {
229 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
230 ("Cannot play LED custom effect: ",ret));
233 return PlatformResult(ErrorCode::NO_ERROR);
236 PlatformResult NotificationManager::StopLEDCustomEffect() {
239 int ret = device_led_stop_custom();
240 if (ret != DEVICE_ERROR_NONE) {
241 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
242 ("Cannot stop LED custom effect: ",ret));
245 return PlatformResult(ErrorCode::NO_ERROR);
248 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
250 std::string name = FromJson<std::string>(args, "name");
251 notification_h noti_handle = nullptr;
254 notification_free(noti_handle);
257 PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, ¬i_handle);
259 if (status.IsError()){
263 ret = notification_save_as_template(noti_handle, name.c_str());
264 if (ret != NOTIFICATION_ERROR_NONE) {
265 LoggerD("Error: %d (%s)", ret, get_error_message(ret));
266 if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) {
267 return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
268 "Maximum number of templates exceeded",
269 ("Maximum number of templates exceeded, error: %d", ret));
271 return LogAndCreateResult(ErrorCode::ABORT_ERR,
272 "Saving template failed",
273 ("Saving template failed, error: %d", ret));
277 return PlatformResult(ErrorCode::NO_ERROR);
281 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
282 picojson::object& out) {
284 std::string name = FromJson<std::string>(args, "name");
286 notification_h noti_handle = nullptr;
287 noti_handle = notification_create_from_template(name.c_str());
289 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
290 "The template with given name not found",
291 ("The template with given name not found, handle is NULL"));
295 notification_free(noti_handle);
298 PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out);
299 if (status.IsError())
301 LoggerE("Failed: ToJson");
304 return PlatformResult(ErrorCode::NO_ERROR);
307 } // namespace notification
308 } // namespace extension