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_common.h>
23 #include <app_control_internal.h>
24 #include <device/led.h>
25 #include <notification_internal.h>
26 #include <notification_list.h>
28 #include "common/converter.h"
29 #include "common/logger.h"
30 #include "common/scope_exit.h"
32 #include "notification/common_notification.h"
33 #include "notification/status_notification.h"
34 #include "notification/user_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, picojson::object& out) {
57 return StatusNotification::PostStatusNotification(args, false, &out);
60 PlatformResult NotificationManager::PostUserNoti(const picojson::object& args,
61 picojson::object& out) {
63 return UserNotification::PostUserNotification(args, false, &out);
66 PlatformResult NotificationManager::Update(const picojson::object& args) {
68 return StatusNotification::PostStatusNotification(args, true, nullptr);
71 PlatformResult NotificationManager::UpdateUserNoti(const picojson::object& args) {
73 return UserNotification::PostUserNotification(args, true, nullptr);
76 PlatformResult NotificationManager::Remove(const picojson::object& args) {
78 int id = std::stoi(FromJson<std::string>(args, "id"));
80 int ret = notification_delete_by_priv_id(nullptr, NOTIFICATION_TYPE_NONE, id);
81 if (NOTIFICATION_ERROR_NONE != ret) {
82 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Cannot remove notification error",
83 ("Cannot remove notification error: %d", ret));
86 return PlatformResult(ErrorCode::NO_ERROR);
89 PlatformResult NotificationManager::RemoveAll() {
91 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
92 if (NOTIFICATION_ERROR_NONE != ret) {
93 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Notification noti remove all failed",
94 ("Notification remove all failed: %d", ret));
97 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
98 if (NOTIFICATION_ERROR_NONE != ret) {
99 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Notification ongoing remove all failed",
100 ("Notification remove all failed: %d", ret));
103 return PlatformResult(ErrorCode::NO_ERROR);
106 PlatformResult NotificationManager::Get(const picojson::object& args, picojson::object& out,
111 id = std::stoi(FromJson<std::string>(args, "id"));
113 LoggerE("Failed to convert string to int");
114 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
117 app_control_h app_control = nullptr;
118 notification_h noti_handle = nullptr;
122 app_control_destroy(app_control);
127 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
128 if (status.IsError()) {
129 LoggerE("Failed: GetNotiHandle");
133 status = StatusNotification::GetAppControl(noti_handle, &app_control);
134 if (status.IsError()) {
135 LoggerE("Failed: GetAppControl");
140 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
142 status = UserNotification::ToJson(id, noti_handle, app_control, &out);
144 if (status.IsError()) {
145 LoggerE("Failed: ToJson");
148 return PlatformResult(ErrorCode::NO_ERROR);
151 PlatformResult NotificationManager::GetAll(picojson::array& out, bool is_new_impl) {
153 notification_h noti = nullptr;
154 notification_list_h noti_list = nullptr;
155 notification_list_h noti_list_iter = nullptr;
156 char* package = nullptr;
158 if (APP_ERROR_NONE == app_get_id(&package)) {
159 LoggerD("Package id: %s", package);
161 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not get package id");
163 const std::string package_str = package;
166 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
167 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
168 if (NOTIFICATION_ERROR_NONE != ret) {
169 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification list error",
170 ("Get notification list error: %d", ret));
174 notification_free_list(noti_list);
177 noti_list_iter = notification_list_get_head(noti_list);
179 while (nullptr != noti_list_iter) {
180 noti = notification_list_get_data(noti_list_iter);
181 if (nullptr != noti) {
183 ret = notification_get_id(noti, nullptr, ¬i_priv);
184 if (NOTIFICATION_ERROR_NONE != ret) {
185 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot get notification id error",
186 ("Cannot get notification id, error: %d", ret));
189 app_control_h app_control = nullptr;
190 PlatformResult status = StatusNotification::GetAppControl(noti, &app_control);
194 app_control_destroy(app_control);
198 if (status.IsError()) return status;
200 picojson::object noti_item = picojson::object();
203 status = StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
205 status = UserNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
207 if (status.IsError()) return status;
209 out.push_back(picojson::value(noti_item));
212 noti_list_iter = notification_list_get_next(noti_list_iter);
215 return PlatformResult(ErrorCode::NO_ERROR);
218 PlatformResult NotificationManager::PlayLEDCustomEffect(const picojson::object& args) {
221 int timeOn = FromJson<double>(args, "timeOn");
222 int timeOff = FromJson<double>(args, "timeOff");
223 unsigned int color = FromJson<double>(args, "color");
225 auto& flags = FromJson<picojson::array>(args, "flags");
226 unsigned int platformFlags = 0;
227 for (auto flag : flags) {
228 std::string flagStr = JsonCast<std::string>(flag);
229 if ("LED_CUSTOM_DEFAULT" == flagStr)
230 platformFlags |= LED_CUSTOM_DEFAULT;
231 else if ("LED_CUSTOM_DUTY_ON" == flagStr)
232 platformFlags |= LED_CUSTOM_DUTY_ON;
236 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
237 if (DEVICE_ERROR_NONE != ret) {
238 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
239 ("Cannot play LED custom effect: ", ret));
242 return PlatformResult(ErrorCode::NO_ERROR);
245 PlatformResult NotificationManager::StopLEDCustomEffect() {
248 int ret = device_led_stop_custom();
249 if (DEVICE_ERROR_NONE != ret) {
250 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
251 ("Cannot stop LED custom effect: ", ret));
254 return PlatformResult(ErrorCode::NO_ERROR);
257 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
259 std::string name = FromJson<std::string>(args, "name");
260 notification_h noti_handle = nullptr;
263 notification_free(noti_handle);
266 PlatformResult status = PlatformResult(ErrorCode::NO_ERROR);
267 const auto& new_impl_it = args.find("newImpl");
268 const auto& noti_val_it = args.find("notification");
269 if (args.end() != new_impl_it && args.end() != noti_val_it && new_impl_it->second.get<bool>()) {
270 status = UserNotification::GetNotiHandleFromJson(noti_val_it->second, false, ¬i_handle);
272 status = StatusNotification::GetNotiHandleFromJson(noti_val_it->second, false, ¬i_handle);
275 if (status.IsError()) {
279 ret = notification_save_as_template(noti_handle, name.c_str());
280 if (NOTIFICATION_ERROR_NONE != ret) {
281 LoggerD("Error: %d (%s)", ret, get_error_message(ret));
282 if (NOTIFICATION_ERROR_MAX_EXCEEDED == ret) {
283 return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
284 "Maximum number of templates exceeded",
285 ("Maximum number of templates exceeded, error: %d", ret));
287 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Saving template failed",
288 ("Saving template failed, error: %d", ret));
292 return PlatformResult(ErrorCode::NO_ERROR);
295 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
296 picojson::object& out) {
298 std::string name = FromJson<std::string>(args, "name");
300 notification_h noti_handle = nullptr;
301 noti_handle = notification_create_from_template(name.c_str());
303 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "The template with given name not found",
304 ("The template with given name not found, handle is nullptr"));
308 notification_free(noti_handle);
311 // This method is designed to return only UserNotification objects
312 PlatformResult status = UserNotification::ToJson(0, noti_handle, nullptr, &out);
313 if (status.IsError()) {
314 LoggerE("Failed: ToJson");
317 return PlatformResult(ErrorCode::NO_ERROR);
320 } // namespace notification
321 } // namespace extension