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 FApp_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 <FAppNotificationManager.h>
34 #include <FBaseInternalTypes.h>
35 #include <FBase_StringConverter.h>
36 #include "FApp_NotificationManagerImpl.h"
37 #include "FApp_NotificationManagerProxy.h"
38 #include "FApp_AppInfo.h"
39 #include "FAppPkg_PackageManagerImpl.h"
41 #include "FApp_AppArg.h"
43 using namespace Tizen::Base;
44 using namespace Tizen::App;
45 using namespace Tizen::App::Package;
47 extern "C" int service_create_request(bundle *data, service_h *service);
53 ConvertNotificationResult(int error)
57 case UI_NOTIFICATION_ERROR_NONE:
59 case UI_NOTIFICATION_ERROR_INVALID_PARAMETER:
61 case UI_NOTIFICATION_ERROR_OUT_OF_MEMORY:
62 return E_OUT_OF_MEMORY;
63 case UI_NOTIFICATION_ERROR_DB_FAILED:
65 case UI_NOTIFICATION_ERROR_NO_SUCH_FILE:
67 case UI_NOTIFICATION_ERROR_INVALID_STATE:
74 ui_notification_progress_type_e
75 ConvertToNotificationProgressType(int progressType)
79 case ONGOING_ACTIVITY_TYPE_TEXT:
80 return UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE;
81 case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
82 return UI_NOTIFICATION_PROGRESS_TYPE_SIZE;
83 case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
84 return UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE;
86 return UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE;
91 IsPosted(ui_notification_h handle)
93 struct ui_notification_s
113 ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
115 return pStruct->posted;
120 namespace Tizen { namespace App
123 _NotificationManagerImpl::_NotificationManagerImpl(void)
124 : __pNotificationManager(null)
128 _NotificationManagerImpl::~_NotificationManagerImpl(void)
133 _NotificationManagerImpl::Construct(void)
135 result r = E_SUCCESS;
137 __pNotificationManager = new (std::nothrow) _NotificationManagerProxy;
138 SysTryReturnResult(NID_APP, __pNotificationManager != null, E_OUT_OF_MEMORY, "__pNotificationManagerProxy creation failed.");
140 r = __pNotificationManager->Construct();
141 SysTryCatch(NID_APP, !IsFailed(r), , r, "__pNotificationManager->Construct() failed [%s].", GetErrorMessage(r));
146 delete __pNotificationManager;
147 __pNotificationManager = null;
152 const _NotificationManagerImpl*
153 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
155 return notiMgr.__pNotificationManagerImpl;
158 _NotificationManagerImpl*
159 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
161 return notiMgr.__pNotificationManagerImpl;
165 _NotificationManagerImpl::GetBadgeNumber(void) const
168 notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
173 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
175 return NotifyImpl(messageText, -1, launchArguments, true);
179 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
182 return NotifyImpl(appId, messageText, -1, launchArguments, true);
186 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
187 const String& launchArguments,
188 bool isOngoing) const
190 result r = E_SUCCESS;
193 ui_notification_h core = NULL;
194 char* pkgname = NULL;
196 service_h svc = NULL;
199 if (!messageText.IsEmpty())
201 SysTryReturnResult(NID_APP,
202 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
203 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
205 retcode = ui_notification_create(isOngoing, &core);
206 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
208 pMsg = _StringConverter::CopyToCharArrayN(messageText);
210 int ret = ui_notification_set_content(core, pMsg);
211 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
213 app_get_package(&pkgname);
214 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.");
216 r = arg.Construct(launchArguments);
217 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
219 pKb = arg.GetBundle();
220 service_create_request(pKb, &svc);
222 service_set_package(svc, pkgname);
223 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
224 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
226 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
228 r = ConvertNotificationResult(ui_notification_post(core));
229 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
232 if (badgeNumber >= 0)
234 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
235 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
247 ui_notification_destroy(core);
249 service_destroy(svc);
254 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
255 const String& launchArguments,
256 bool isOngoing) const
258 result r = E_SUCCESS;
263 ui_notification_h core = NULL;
266 service_h svc = NULL;
269 memset(buffer, 0, 256);
271 bool b = _Aul::IsInstalled(appId);
273 SysTryReturnResult(NID_APP, b == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
275 if (!isOngoing || !messageText.IsEmpty())
277 SysTryReturnResult(NID_APP,
278 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
279 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
281 retcode = ui_notification_create(isOngoing, &core);
282 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
284 pMsg = _StringConverter::CopyToCharArrayN(messageText);
286 int ret = ui_notification_set_content(core, pMsg);
287 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
289 snprintf(buffer, 256, "%ls", appId.GetPointer());
291 app_manager_get_app_icon_path(buffer, &pIcon);
292 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
293 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
295 app_manager_get_app_name(buffer, &pName);
296 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
297 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
299 r = arg.Construct(launchArguments);
300 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
302 pKb = arg.GetBundle();
303 service_create_request(pKb, &svc);
305 service_set_app_id(svc, buffer);
306 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
307 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
309 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
311 r = ConvertNotificationResult(ui_notification_post(core));
312 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
315 if (badgeNumber >= 0)
317 notification_set_badge(buffer, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
318 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
326 ui_notification_destroy(core);
338 service_destroy(svc);
344 _NotificationManagerImpl::Notify(int badgeNumber) const
346 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
348 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
350 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
353 String messageText = String(L"");
354 String appMessage = String(L"");
356 return NotifyImpl(messageText, badgeNumber, appMessage, false);
360 _NotificationManagerImpl::Notify(const String& messageText) const
362 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
364 String appMessage = String(L"");
366 return NotifyImpl(messageText, -1, appMessage, false);
370 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
372 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
373 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
375 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
377 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
380 String appMessage = String(L"");
382 return NotifyImpl(messageText, badgeNumber, appMessage, false);
386 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
388 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
389 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
390 SysTryReturnResult(NID_APP,
391 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
392 "launchArguments is less than 0.");
394 SysTryReturnResult(NID_APP,
395 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
396 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
398 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
400 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
403 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
407 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
409 bool b = _Aul::IsInstalled(appId);
411 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
417 memset(buffer, 0, 256);
419 snprintf(buffer, 256, "%ls", appId.GetPointer());
421 notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
428 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
430 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
432 String messageText = String(L"");
433 String appMessage = String(L"");
434 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
438 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
440 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
442 return NotifyImpl(appId, messageText, -1, String(L""));
446 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
448 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
449 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
451 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
455 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
457 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
458 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
459 SysTryReturnResult(NID_APP,
460 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
461 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
463 return NotifyImpl(appId, messageText, -1, launchArguments);
467 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
468 const String& launchArguments) const
470 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
471 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
472 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
473 SysTryReturnResult(NID_APP,
474 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
475 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
477 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
481 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
483 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
485 return OngoingImpl(messageText, String(L""));
489 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
491 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
492 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
493 SysTryReturnResult(NID_APP,
494 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
495 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
497 return OngoingImpl(messageText, launchArguments);
501 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
503 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
505 return OngoingImpl(appId, messageText, String(L""));
509 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
510 const String& launchArguments) const
512 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
513 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
514 SysTryReturnResult(NID_APP,
515 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
516 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
518 return OngoingImpl(appId, messageText, launchArguments);
522 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
524 result r = E_SUCCESS;
526 notification_error_e err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
529 case NOTIFICATION_ERROR_NONE:
533 case NOTIFICATION_ERROR_INVALID_DATA:
546 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
548 return __pNotificationManager->RemoveNotification(appId, true);
552 _NotificationManagerImpl::RemoveNotification(void)
554 result r = E_SUCCESS;
556 notification_error_e err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
559 case NOTIFICATION_ERROR_NONE:
563 case NOTIFICATION_ERROR_INVALID_DATA:
576 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
578 return __pNotificationManager->RemoveNotification(appId,false);