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, picojson::object& out) {
54 return StatusNotification::FromJson(args, false, &out);
57 PlatformResult NotificationManager::Update(const picojson::object& args) {
59 return StatusNotification::FromJson(args, true, NULL);
62 PlatformResult NotificationManager::Remove(const picojson::object& args) {
64 int id = std::stoi(FromJson<std::string>(args, "id"));
66 int ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, id);
67 if (ret != NOTIFICATION_ERROR_NONE) {
68 return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Cannot remove notification error",
69 ("Cannot remove notification error: %d", ret));
72 return PlatformResult(ErrorCode::NO_ERROR);
75 PlatformResult NotificationManager::RemoveAll() {
77 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
78 if (ret != NOTIFICATION_ERROR_NONE) {
79 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Notification noti remove all failed",
80 ("Notification remove all failed: %d", ret));
83 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
84 if (ret != NOTIFICATION_ERROR_NONE) {
85 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Notification ongoing remove all failed",
86 ("Notification remove all failed: %d", ret));
89 return PlatformResult(ErrorCode::NO_ERROR);
92 PlatformResult NotificationManager::Get(const picojson::object& args, picojson::object& out) {
96 id = std::stoi(FromJson<std::string>(args, "id"));
98 LoggerE("Failed to convert string to int");
99 return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
102 app_control_h app_control = nullptr;
103 notification_h noti_handle = nullptr;
107 app_control_destroy(app_control);
112 PlatformResult status = StatusNotification::GetNotiHandle(id, ¬i_handle);
113 if (status.IsError()) {
114 LoggerE("Failed: GetNotiHandle");
118 status = StatusNotification::GetAppControl(noti_handle, &app_control);
119 if (status.IsError()) {
120 LoggerE("Failed: GetAppControl");
123 status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
124 if (status.IsError()) {
125 LoggerE("Failed: ToJson");
128 return PlatformResult(ErrorCode::NO_ERROR);
131 PlatformResult NotificationManager::GetAll(picojson::array& out) {
133 notification_h noti = nullptr;
134 notification_list_h noti_list = nullptr;
135 notification_list_h noti_list_iter = nullptr;
136 char* package = nullptr;
138 if (APP_ERROR_NONE == app_get_id(&package)) {
139 LoggerD("Package id: %s", package);
141 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not get package id");
143 const std::string package_str = package;
146 int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
147 NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
148 if (NOTIFICATION_ERROR_NONE != ret) {
149 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification list error",
150 ("Get notification list error: %d", ret));
154 notification_free_list(noti_list);
157 noti_list_iter = notification_list_get_head(noti_list);
159 while (nullptr != noti_list_iter) {
160 noti = notification_list_get_data(noti_list_iter);
161 if (nullptr != noti) {
163 ret = notification_get_id(noti, NULL, ¬i_priv);
164 if (NOTIFICATION_ERROR_NONE != ret) {
165 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot get notification id error",
166 ("Cannot get notification id, error: %d", ret));
169 app_control_h app_control = nullptr;
170 PlatformResult status = StatusNotification::GetAppControl(noti, &app_control);
174 app_control_destroy(app_control);
178 if (status.IsError()) return status;
180 picojson::object noti_item = picojson::object();
182 status = StatusNotification::ToJson(noti_priv, noti, app_control, ¬i_item);
183 if (status.IsError()) return status;
185 out.push_back(picojson::value(noti_item));
188 noti_list_iter = notification_list_get_next(noti_list_iter);
191 return PlatformResult(ErrorCode::NO_ERROR);
194 PlatformResult NotificationManager::PlayLEDCustomEffect(const picojson::object& args) {
197 int timeOn = FromJson<double>(args, "timeOn");
198 int timeOff = FromJson<double>(args, "timeOff");
199 unsigned int color = FromJson<double>(args, "color");
201 auto& flags = FromJson<picojson::array>(args, "flags");
202 unsigned int platformFlags = 0;
203 for (auto flag : flags) {
204 std::string flagStr = JsonCast<std::string>(flag);
205 if (flagStr == "LED_CUSTOM_DEFAULT")
206 platformFlags |= LED_CUSTOM_DEFAULT;
207 else if (flagStr == "LED_CUSTOM_DUTY_ON")
208 platformFlags |= LED_CUSTOM_DUTY_ON;
212 ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
213 if (ret != DEVICE_ERROR_NONE) {
214 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
215 ("Cannot play LED custom effect: ", ret));
218 return PlatformResult(ErrorCode::NO_ERROR);
221 PlatformResult NotificationManager::StopLEDCustomEffect() {
224 int ret = device_led_stop_custom();
225 if (ret != DEVICE_ERROR_NONE) {
226 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
227 ("Cannot stop LED custom effect: ", ret));
230 return PlatformResult(ErrorCode::NO_ERROR);
233 } // namespace notification
234 } // namespace extension