2 // Open Service Platform
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.
19 * @file FShell_NotificationManagerImpl.cpp
20 * @brief This is the placeholder for _NotificationManagerImpl class.
23 #include <unique_ptr.h>
24 #include <appsvc/appsvc.h>
26 #include <notification/notification.h>
27 #include <appfw/app.h>
28 #include <appfw/app_manager.h>
29 #include <appfw/app_ui_notification.h>
31 #include <FBaseSysLog.h>
32 #include <FAppTypes.h>
33 #include <FShellNotificationManager.h>
34 #include <FShellNotificationRequest.h>
36 #include <FBaseInternalTypes.h>
37 #include <FBase_StringConverter.h>
38 #include "FApp_AppInfo.h"
39 #include "FApp_PackageManagerImpl.h"
41 #include "FApp_AppArg.h"
42 #include "FShell_NotificationManagerImpl.h"
43 #include "FShell_NotificationManagerProxy.h"
45 using namespace Tizen::Base;
46 using namespace Tizen::App;
47 using namespace Tizen::Shell;
49 extern "C" int service_create_request(bundle *data, service_h *service);
55 ConvertNotificationResult(int error)
59 case UI_NOTIFICATION_ERROR_NONE:
61 case UI_NOTIFICATION_ERROR_INVALID_PARAMETER:
63 case UI_NOTIFICATION_ERROR_OUT_OF_MEMORY:
64 return E_OUT_OF_MEMORY;
65 case UI_NOTIFICATION_ERROR_DB_FAILED:
67 case UI_NOTIFICATION_ERROR_NO_SUCH_FILE:
68 return E_OPERATION_FAILED;
69 case UI_NOTIFICATION_ERROR_INVALID_STATE:
70 return E_OPERATION_FAILED;
72 return E_OPERATION_FAILED;
79 static int length = (_AppInfo::GetApiVersion() < _API_VERSION_2_1) ? 127 : MAX_NOTIFICATION_MESSAGE_LENGTH;
84 ui_notification_progress_type_e
85 ConvertToNotificationProgressType(int progressType)
89 case ONGOING_ACTIVITY_TYPE_TEXT:
90 return UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE;
91 case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
92 return UI_NOTIFICATION_PROGRESS_TYPE_SIZE;
93 case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
94 return UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE;
96 return UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE;
101 IsPosted(ui_notification_h handle)
103 struct ui_notification_s
123 ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
125 return pStruct->posted;
130 namespace Tizen { namespace Shell
133 _NotificationManagerImpl::_NotificationManagerImpl(void)
134 : __pNotificationManager(null)
138 _NotificationManagerImpl::~_NotificationManagerImpl(void)
143 _NotificationManagerImpl::Construct(void)
145 result r = E_SUCCESS;
147 __pNotificationManager = new (std::nothrow) _NotificationManagerProxy;
148 SysTryReturnResult(NID_APP, __pNotificationManager != null, E_OUT_OF_MEMORY, "__pNotificationManagerProxy creation failed.");
150 r = __pNotificationManager->Construct();
151 SysTryCatch(NID_APP, !IsFailed(r), , r, "__pNotificationManager->Construct() failed [%s].", GetErrorMessage(r));
156 delete __pNotificationManager;
157 __pNotificationManager = null;
162 const _NotificationManagerImpl*
163 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
165 return notiMgr.__pNotificationManagerImpl;
168 _NotificationManagerImpl*
169 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
171 return notiMgr.__pNotificationManagerImpl;
175 _NotificationManagerImpl::GetBadgeNumber(void) const
178 notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
183 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
185 return NotifyImpl(messageText, -1, launchArguments, true);
189 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
192 return NotifyImpl(appId, messageText, -1, launchArguments, true);
196 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
197 const String& launchArguments,
198 bool isOngoing) const
200 result r = E_SUCCESS;
203 ui_notification_h core = NULL;
204 char* pkgname = NULL;
206 if (!messageText.IsEmpty())
208 SysTryReturnResult(NID_APP,
209 messageText.GetLength() <= GetMessageLength(), E_INVALID_ARG,
210 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
212 retcode = ui_notification_create(isOngoing, &core);
213 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Notification creation error : 0x%x.", retcode);
215 pMsg = _StringConverter::CopyToCharArrayN(messageText);
217 int ret = ui_notification_set_content(core, pMsg);
218 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
221 service_h svc = NULL;
224 app_get_package(&pkgname);
225 SysTryCatch(NID_APP, pkgname != NULL, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Cannot acquire package name for current application.");
227 r = arg.Construct(launchArguments);
228 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
230 pKb = arg.GetBundle();
231 service_create_request(pKb, &svc);
232 service_set_package(svc, pkgname);
233 ui_notification_set_service(core, svc);
235 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
237 r = ConvertNotificationResult(ui_notification_post(core));
239 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
242 if (badgeNumber >= 0)
244 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
245 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
257 ui_notification_destroy(core);
264 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
265 const String& launchArguments,
266 bool isOngoing) const
268 result r = E_SUCCESS;
273 ui_notification_h core = NULL;
276 memset(buffer, 0, 256);
278 bool b = _Aul::IsInstalled(appId);
280 SysTryReturnResult(NID_APP, b == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
282 if (!isOngoing || !messageText.IsEmpty())
284 SysTryReturnResult(NID_APP,
285 messageText.GetLength() <= GetMessageLength(), E_INVALID_ARG,
286 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
288 retcode = ui_notification_create(isOngoing, &core);
289 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Notification creation error : 0x%x.", retcode);
291 pMsg = _StringConverter::CopyToCharArrayN(messageText);
293 int ret = ui_notification_set_content(core, pMsg);
294 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
297 service_h svc = NULL;
300 snprintf(buffer, 256, "%ls", appId.GetPointer());
302 app_manager_get_app_icon_path(buffer, &pIcon);
303 ui_notification_set_icon(core, pIcon);
305 app_manager_get_app_name(buffer, &pName);
306 ui_notification_set_title(core, pName);
308 r = arg.Construct(launchArguments);
309 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
311 pKb = arg.GetBundle();
312 service_create_request(pKb, &svc);
313 service_set_app_id(svc, buffer);
314 ui_notification_set_service(core, svc);
316 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
318 r = ConvertNotificationResult(ui_notification_post(core));
320 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
323 if (badgeNumber >= 0)
325 notification_set_badge(buffer, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
326 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
334 ui_notification_destroy(core);
352 _NotificationManagerImpl::NotifyMessageImpl(const NotificationRequest& notiMessage, bool isOngoing)
354 result r = E_SUCCESS;
357 ui_notification_h core = NULL;
358 char* pkgname = NULL;
359 char* pTitleText = NULL;
360 char* pIconPath = NULL;
361 char* pSoundPath = NULL;
363 const String& messageText = notiMessage.GetAlertText();
364 const String& launchArguments = notiMessage.GetAppMessage();
365 const String& titleText = notiMessage.GetTitleText();
366 const String& iconPath = notiMessage.GetIconFilePath();
367 const String& soundPath = notiMessage.GetSoundFilePath();
368 const int badgeNumber = notiMessage.GetBadgeNumber();
369 const int badgeOffset = notiMessage.GetBadgeOffset();
371 if (isOngoing || !messageText.IsEmpty())
373 SysTryReturnResult(NID_APP,
374 messageText.GetLength() <= GetMessageLength(), E_INVALID_ARG,
375 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
377 retcode = ui_notification_create(isOngoing, &core);
378 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Notification creation error : 0x%x.", retcode);
380 std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(messageText));
384 int ret = ui_notification_set_content(core, pMsg.get());
385 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
388 if (!titleText.IsEmpty())
390 pTitleText = _StringConverter::CopyToCharArrayN(titleText);
391 ui_notification_set_title(core, pTitleText);
394 if (!iconPath.IsEmpty())
396 pIconPath = _StringConverter::CopyToCharArrayN(iconPath);
397 ui_notification_set_icon(core, pIconPath);
400 if (!soundPath.IsEmpty())
402 pSoundPath = _StringConverter::CopyToCharArrayN(soundPath);
403 ui_notification_set_sound(core, pSoundPath);
407 service_h svc = NULL;
410 app_get_id(&pkgname);
411 SysTryCatch(NID_APP, pkgname != NULL, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Cannot acquire package name for current application.");
413 r = arg.Construct(launchArguments);
414 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
416 pKb = arg.GetBundle();
417 service_create_request(pKb, &svc);
418 service_set_app_id(svc, pkgname);
420 ui_notification_set_service(core, svc);
421 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
425 OngoingActivityType activityType = notiMessage.GetOngoingActivityType();
426 progress = notiMessage.GetOngoingActivityProgress();
427 switch (activityType)
429 case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
432 ui_notification_post(core);
434 r = ConvertNotificationResult(ui_notification_update_progress(core, ConvertToNotificationProgressType(activityType), progress/100.));
436 case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
439 ui_notification_post(core);
441 r = ConvertNotificationResult(ui_notification_update_progress(core, ConvertToNotificationProgressType(activityType), progress));
443 case ONGOING_ACTIVITY_TYPE_TEXT:
444 r = ConvertNotificationResult(ui_notification_post(core));
447 r = E_OPERATION_FAILED;
448 // ui_notification_set_content() is done already
454 r = ConvertNotificationResult(ui_notification_post(core));
455 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
460 SysTryReturnResult(NID_APP, 0, E_INVALID_ARG, "MessageText is Empty");
463 if (badgeNumber >= 0)
465 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
466 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
469 if (badgeOffset != 0)
471 // badge offset is exclusive to badge number
474 notification_error_e noti_err = notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
475 if (noti_err == NOTIFICATION_ERROR_NONE)
477 count += badgeOffset;
480 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, count);
481 SysLog(NID_APP, "Badge number is set to %d.", count);
496 ui_notification_destroy(core);
503 _NotificationManagerImpl::NotifyMessageImpl(const AppId& appId, const NotificationRequest& notiMessage, bool isOngoing)
505 return __pNotificationManager->NotifyMessage(appId, notiMessage, isOngoing);
510 _NotificationManagerImpl::Notify(int badgeNumber) const
512 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
514 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
516 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
519 String messageText = String(L"");
520 String appMessage = String(L"");
522 return NotifyImpl(messageText, badgeNumber, appMessage, false);
526 _NotificationManagerImpl::Notify(const String& messageText) const
528 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
530 String appMessage = String(L"");
532 return NotifyImpl(messageText, -1, appMessage, false);
536 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
538 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
539 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
541 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
543 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
546 String appMessage = String(L"");
548 return NotifyImpl(messageText, badgeNumber, appMessage, false);
552 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
554 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
555 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
556 SysTryReturnResult(NID_APP,
557 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
558 "launchArguments is less than 0.");
560 SysTryReturnResult(NID_APP,
561 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
562 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
564 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
566 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
569 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
573 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
575 bool b = _Aul::IsInstalled(appId);
577 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
583 memset(buffer, 0, 256);
585 snprintf(buffer, 256, "%ls", appId.GetPointer());
587 notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
594 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, int badgeNumber) const
596 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
598 String messageText = String(L"");
599 String appMessage = String(L"");
600 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
604 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText) const
606 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
608 return NotifyImpl(appId, messageText, -1, String(L""));
612 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText, int badgeNumber) const
614 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
615 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
617 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
621 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText, const String& launchArguments) const
623 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
624 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
625 SysTryReturnResult(NID_APP,
626 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
627 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
629 return NotifyImpl(appId, messageText, -1, launchArguments);
633 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText, int badgeNumber,
634 const String& launchArguments) const
636 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
637 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
638 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
639 SysTryReturnResult(NID_APP,
640 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
641 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
643 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
647 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
649 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
651 return OngoingImpl(messageText, String(L""));
655 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
657 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
658 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
659 SysTryReturnResult(NID_APP,
660 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
661 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
663 return OngoingImpl(messageText, launchArguments);
667 _NotificationManagerImpl::NotifyOngoingActivityByAppId(const AppId& appId, const String& messageText) const
669 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
671 return OngoingImpl(appId, messageText, String(L""));
675 _NotificationManagerImpl::NotifyOngoingActivityByAppId(const AppId& appId, const String& messageText,
676 const String& launchArguments) const
678 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
679 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
680 SysTryReturnResult(NID_APP,
681 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
682 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
684 return OngoingImpl(appId, messageText, launchArguments);
688 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
690 result r = E_SUCCESS;
692 notification_error_e err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
695 case NOTIFICATION_ERROR_NONE:
699 case NOTIFICATION_ERROR_INVALID_DATA:
704 r = E_OPERATION_FAILED;
712 _NotificationManagerImpl::RemoveOngoingActivityNotificationByAppId(const AppId& appId)
714 return __pNotificationManager->RemoveNotification(appId, true);
718 _NotificationManagerImpl::RemoveNotification(void)
720 result r = E_SUCCESS;
722 notification_error_e err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
725 case NOTIFICATION_ERROR_NONE:
729 case NOTIFICATION_ERROR_INVALID_DATA:
734 r = E_OPERATION_FAILED;
742 _NotificationManagerImpl::RemoveNotificationByAppId(const AppId& appId)
744 return __pNotificationManager->RemoveNotification(appId,false);