2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
25 #include <appsvc/appsvc.h>
27 #include "NotificationManager.h"
28 #include "NotificationUtil.h"
30 #include <PlatformException.h>
34 namespace Notification {
36 extern "C" int app_control_to_bundle(app_control_h service, bundle **data);
38 static bool notification_package_equal(notification_h handle)
41 char* handle_package = NULL;
42 char cmdline[512] = {0,};
45 if (notification_get_pkgname(handle, &handle_package))
50 LOGD("handle package = %s", handle_package);
52 if (app_get_id(&package))
59 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
61 fd = open(buf, O_RDONLY);
66 ret = read(fd, cmdline, sizeof(cmdline) - 1);
75 if (strlen(cmdline) == strlen(handle_package))
77 if (!strncmp(cmdline, handle_package, strlen(cmdline)))
85 LOGD("package = %s", package);
87 if (strlen(package) == strlen(handle_package))
89 if (!strncmp(package, handle_package, strlen(package)))
100 NotificationManager::NotificationManager()
104 NotificationManager::~NotificationManager()
108 int NotificationManager::post(StatusNotification *notification)
113 LOGE("Notification handle is NULL.");
114 throw UnknownException("Notification Handle is NULL.");
117 LOGI("notification id = %d", notification->getID());
118 notification_h handle = (notification_h)notification->getNotificationHandle();
121 LOGD("It doesn't have notification handle.");
122 throw UnknownException("It is Empty Notification.");
128 bundle *service_data = NULL;
129 app_control_h service = notification->getService();
131 LOGI("Service : %p, Flag : %d", service, notification->getLaunchFlag());
133 if (service && notification->getLaunchFlag())
135 ret = app_control_to_bundle(service, &service_data);
136 if (ret != APP_CONTROL_ERROR_NONE)
138 LOGE("Can't create bundle: %d", ret);
139 throw UnknownException("Can't create bundle");
142 LOGI("Notification Launch Flag True");
143 ret = notification_set_property(handle, 0);
144 if (ret != NOTIFICATION_ERROR_NONE) {
145 LOGE("set_property failed: %d, %s", ret,
146 NotificationUtil::getNotificationErrorMessage(ret).c_str());
149 ret = notification_set_execute_option(handle,
150 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
151 if (ret != NOTIFICATION_ERROR_NONE)
153 LOGE("can't set notification option: %d, %s", ret,
154 NotificationUtil::getNotificationErrorMessage(ret).c_str());
159 LOGI("Notification Launch Flag False");
160 ret = notification_set_property(handle,
161 NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
162 if (ret != NOTIFICATION_ERROR_NONE) {
163 LOGE("set_property failed: %d, %s", ret,
164 NotificationUtil::getNotificationErrorMessage(ret).c_str());
169 int type = (int)notification->getNotiType();
170 notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE;
174 case NOTI_TYPE_SIMPLE:
176 if (notification->getNumber()>0)
177 noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
179 noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
182 case NOTI_TYPE_THUMBNAIL:
184 noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
187 case NOTI_TYPE_ONGOING:
189 noti_layout = NOTIFICATION_LY_ONGOING_EVENT;
192 case NOTI_TYPE_PROGRESS:
194 noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS;
199 LOGD("Layout type = %d", noti_layout);
200 ret = notification_set_layout(handle, noti_layout);
201 LOGD("set Layout result = %d", ret);
205 //if noti already exist, it is update.
206 ret = notification_insert(handle, &privID);
208 if (ret != NOTIFICATION_ERROR_NONE) {
209 LOGE("insert failed: %d, %s", ret,
210 NotificationUtil::getNotificationErrorMessage(ret).c_str());
211 NotificationUtil::throwNotificationException(ret,
212 "Notification object post fail");
216 LOGD("private ID : %d", privID);
217 notification->setUpdatedFlag(false);
219 if (type == NOTI_TYPE_PROGRESS)
224 ret = notification_get_size(handle, &value);
225 if (ret != NOTIFICATION_ERROR_NONE)
227 LOGE("get_size failed: %d, %s", ret,
228 NotificationUtil::getNotificationErrorMessage(ret).c_str());
229 NotificationUtil::throwNotificationException(ret,
230 "get notification size error");
232 LOGI("get Size : %d", value);
233 ret = notification_update_size(handle, NOTIFICATION_PRIV_ID_NONE, value);
234 if (ret != NOTIFICATION_ERROR_NONE)
236 LOGE("update_size failed: %d, %s", ret,
237 NotificationUtil::getNotificationErrorMessage(ret).c_str());
238 NotificationUtil::throwNotificationException(ret,
239 "update notification size error");
242 ret = notification_get_progress(handle, &value);
243 if (ret != NOTIFICATION_ERROR_NONE)
245 LOGE("get_progress failed: %d, %s", ret,
246 NotificationUtil::getNotificationErrorMessage(ret).c_str());
247 NotificationUtil::throwNotificationException(ret,
248 "get notification percentage error");
250 LOGI("get Percentage : %lf", value);
252 ret = notification_update_progress(handle, NOTIFICATION_PRIV_ID_NONE,
254 if (ret != NOTIFICATION_ERROR_NONE)
256 LOGE("update_progress failed: %d, %s", ret,
257 NotificationUtil::getNotificationErrorMessage(ret).c_str());
263 void NotificationManager::update(StatusNotification *notification)
266 LOGD("INotification is NULL");
267 throw UnknownException("Notificaton is NULL");
270 LOGD("notification id = %d", notification->getID());
271 notification_h handle = (notification_h)notification->getNotificationHandle();
274 LOGD("it doesn't have notification handle.");
275 throw UnknownException("It is Empty Notification.");
278 LOGD("noti type = %d", notification->getNotiType());
281 if (notification->getNotiType() == NOTI_TYPE_PROGRESS)
283 LOGD("progress type = %d", notification->getProgressType());
284 LOGD("noti id = %d", notification->getID());
285 LOGD("noti progress value = %d", notification->getProgressValue());
287 if (NOTI_PROGRESS_TYPE_PERCENTAGE == notification->getProgressType())
289 LOGD( "Percentage ");
290 ret = notification_update_progress(handle,
291 NOTIFICATION_PRIV_ID_NONE, notification->getProgressValue());
293 else if (NOTI_PROGRESS_TYPE_SIZE == notification->getProgressType())
296 ret = notification_update_size(handle, NOTIFICATION_PRIV_ID_NONE,
297 notification->getProgressValue());
300 LOGD("notification_update_progress = %d", ret);
302 if (NOTIFICATION_ERROR_NONE != ret)
305 NotificationUtil::getNotificationErrorMessage(ret).c_str());
306 NotificationUtil::throwNotificationException(ret,
307 "notification_update_progress failed");
312 LOGD("updated Flag = %d", notification->getUpdatedFlag());
314 if (notification->getUpdatedFlag())
317 int type = (int)notification->getNotiType();
318 notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE;
322 case NOTI_TYPE_SIMPLE:
324 if (notification->getNumber()>0)
325 noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
327 noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
330 case NOTI_TYPE_THUMBNAIL:
332 noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
335 case NOTI_TYPE_ONGOING:
337 noti_layout = NOTIFICATION_LY_ONGOING_EVENT;
340 case NOTI_TYPE_PROGRESS:
342 noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS;
347 LOGD("Layout type = %d", noti_layout);
348 ret = notification_set_layout(handle, noti_layout);
349 LOGD("set Layout result = %d", ret);
352 bundle *service_data = NULL;
353 app_control_h service = notification->getService();
357 ret = app_control_to_bundle(service, &service_data);
358 if (ret != APP_CONTROL_ERROR_NONE)
361 throw UnknownException("Can't create bundle");
364 if (notification->getLaunchFlag())
366 LOGI("Notification Launch Flag True");
367 ret = notification_set_property(handle, 0);
368 if (ret != NOTIFICATION_ERROR_NONE) {
369 LOGE("set_property failed: %d, %s", ret,
370 NotificationUtil::getNotificationErrorMessage(ret).c_str());
373 ret = notification_set_execute_option(handle,
374 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL,
376 if (ret != NOTIFICATION_ERROR_NONE)
378 LOGE("Can't set Service option %d, %s", ret,
379 NotificationUtil::getNotificationErrorMessage(ret).c_str());
384 LOGI("Notification Launch Flag False");
385 notification_set_property(handle, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
389 notification->setLaunchFlag(false);
391 LOGD("get Title = %s", notification->getTitle().c_str());
392 ret = notification_update(handle);
393 LOGD("notification_update = %d", ret);
396 notification->setUpdatedFlag(false);
398 if (ret != NOTIFICATION_ERROR_NONE)
400 LOGE("update failed: %d, %s", ret,
401 NotificationUtil::getNotificationErrorMessage(ret).c_str());
402 throw UnknownException("Notification Object update fail");
407 void NotificationManager::remove(std::string id)
411 bool existFlag = false;
413 LOGD("id : %s", id.c_str());
414 std::istringstream stream(id);
417 LOGE("invalid notification ID");
418 throw InvalidValuesException("Invalid notification ID, it don't match id format");
423 notification_h noti = NULL;
424 notification_list_h noti_list = NULL;
425 notification_list_h noti_list_iter = NULL;
427 ret = notification_get_grouping_list( NOTIFICATION_TYPE_NONE , -1,
429 if (ret != NOTIFICATION_ERROR_NONE) {
430 LOGD("get notification list failed: %d, %s", ret,
431 NotificationUtil::getNotificationErrorMessage(ret).c_str());
432 NotificationUtil::throwNotificationException(ret,
433 "Can't get noti list");
436 noti_list_iter = notification_list_get_head(noti_list);
437 while ( noti_list_iter != NULL)
439 noti = notification_list_get_data(noti_list_iter);
440 if (noti != NULL && notification_package_equal(noti))
443 ret = notification_get_id(noti, NULL, ¬i_priv);
444 if (ret != NOTIFICATION_ERROR_NONE) {
445 LOGE("get_id failed: %d, %s", ret,
446 NotificationUtil::getNotificationErrorMessage(ret).c_str());
449 LOGD("notification id = %d", noti_priv);
450 if (noti_priv == privID)
453 noti_list_iter = notification_list_get_next(noti_list_iter);
457 notification_free_list(noti_list);
459 LOGD("notification ID : %d", privID);
461 LOGE("existFlag is NULL");
462 throw NotFoundException("not exist id");
465 ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, privID);
466 if (ret != NOTIFICATION_ERROR_NONE) {
467 LOGE("delete_by_priv_id failed: %d, %s", ret,
468 NotificationUtil::getNotificationErrorMessage(ret).c_str());
469 NotificationUtil::throwNotificationException(ret,
470 "notification delete failed");
474 void NotificationManager::removeAll()
476 // remove all noti type
477 int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
478 if (ret != NOTIFICATION_ERROR_NONE)
480 LOGE("notification delete failed: %d, %s", ret,
481 NotificationUtil::getNotificationErrorMessage(ret).c_str());
482 NotificationUtil::throwNotificationException(ret,
483 "notification delete failed");
486 ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
487 if (ret != NOTIFICATION_ERROR_NONE)
489 LOGE("notification delete failed: %d, %s", ret,
490 NotificationUtil::getNotificationErrorMessage(ret).c_str());
491 NotificationUtil::throwNotificationException(ret,
492 "notification delete failed");
496 StatusNotification* NotificationManager::get(std::string id)
500 std::istringstream stream(id);
503 LOGE("invalid notification id: %s", id.c_str());
504 throw InvalidValuesException("Invalid notification ID, it don't match id format");
508 LOGI("priv ID : %d", privID);
510 notification_h notification = notification_load(NULL, privID);
512 if (notification != NULL && notification_package_equal(notification))
514 StatusNotification* noti = new StatusNotification(notification);
519 LOGE("It's not notification id or removed notifiation");
520 throw NotFoundException("It is not notification ID or removed notification");
525 std::vector<StatusNotification*> NotificationManager::getAll()
528 std::vector<StatusNotification*> data;
530 notification_h noti = NULL;
531 notification_list_h noti_list = NULL;
532 notification_list_h noti_list_iter = NULL;
535 ret = notification_get_grouping_list(NOTIFICATION_TYPE_NONE, -1,
537 if (ret != NOTIFICATION_ERROR_NONE)
539 LOGE("get notification list failed: %d, %s", ret,
540 NotificationUtil::getNotificationErrorMessage(ret).c_str());
541 NotificationUtil::throwNotificationException(ret,
542 "get notification list failed");
545 noti_list_iter = notification_list_get_head(noti_list);
546 while ( noti_list_iter != NULL)
548 noti = notification_list_get_data(noti_list_iter);
549 if (noti != NULL && notification_package_equal(noti))
552 ret = notification_get_id(noti, NULL, ¬i_priv);
553 if (ret != NOTIFICATION_ERROR_NONE) {
554 LOGE("get_id failed: %d, %s", ret,
555 NotificationUtil::getNotificationErrorMessage(ret).c_str());
557 LOGD("notification id = %d", noti_priv);
559 StatusNotification* notification = new StatusNotification(noti_priv);
561 ret = notification_get_id((notification_h)notification->
562 getNotificationHandle(), NULL, ¬i_priv);
563 if (ret != NOTIFICATION_ERROR_NONE) {
564 LOGE("get_id failed: %d, %s", ret,
565 NotificationUtil::getNotificationErrorMessage(ret).c_str());
568 LOGD("loaded notification id = %d", noti_priv);
569 data.push_back(notification);
572 noti_list_iter = notification_list_get_next(noti_list_iter);
576 notification_free_list(noti_list);