2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
18 * @file FApp_NotificationManagerImpl.cpp
19 * @brief This is the placeholder for _NotificationManagerImpl class.
22 #include <unique_ptr.h>
24 #include <appsvc/appsvc.h>
27 #include <notification/notification.h>
28 #include <appfw/app.h>
29 #include <appfw/app_ui_notification.h>
32 #include <FBaseSysLog.h>
33 #include <FAppNotificationManager.h>
35 #include <FBaseInternalTypes.h>
36 #include <FBase_StringConverter.h>
37 #include "FApp_NotificationManagerImpl.h"
38 #include "FApp_AppInfo.h"
40 #include "FAppPkg_PackageManagerImpl.h"
42 #include "FApp_AppArg.h"
43 #include "FAppPkgPackageAppInfo.h"
44 #include "FAppPkg_PackageAppInfoImpl.h"
46 using namespace Tizen::Base;
47 using namespace Tizen::App;
48 using namespace Tizen::App::Package;
49 using namespace Tizen::Io;
51 extern "C" int service_create_request(bundle *data, service_h *service);
57 ConvertNotificationResult(int error)
61 case UI_NOTIFICATION_ERROR_NONE:
63 case UI_NOTIFICATION_ERROR_INVALID_PARAMETER:
65 case UI_NOTIFICATION_ERROR_OUT_OF_MEMORY:
66 return E_OUT_OF_MEMORY;
67 case UI_NOTIFICATION_ERROR_DB_FAILED:
69 case UI_NOTIFICATION_ERROR_NO_SUCH_FILE:
71 case UI_NOTIFICATION_ERROR_INVALID_STATE:
79 IsPosted(ui_notification_h handle)
81 struct ui_notification_s
101 ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
103 return pStruct->posted;
108 namespace Tizen { namespace App
111 _NotificationManagerImpl::_NotificationManagerImpl(void)
115 _NotificationManagerImpl::~_NotificationManagerImpl(void)
120 _NotificationManagerImpl::Construct(void)
125 const _NotificationManagerImpl*
126 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
128 return notiMgr.__pNotificationManagerImpl;
131 _NotificationManagerImpl*
132 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
134 return notiMgr.__pNotificationManagerImpl;
138 _NotificationManagerImpl::GetBadgeNumber(void) const
140 unsigned int count = 0;
142 badge_error_e badgeError = badge_get_count(appinfo_get_appid(), &count);
144 if (badgeError == BADGE_ERROR_NONE)
146 SysLog(NID_APP, "badge_get_count(%d)", count);
150 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
157 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
159 return NotifyImpl(messageText, -1, launchArguments, true);
163 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
166 return NotifyImpl(appId, messageText, -1, launchArguments, true);
170 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
171 const String& launchArguments,
172 bool isOngoing) const
174 result r = E_SUCCESS;
177 char* pkgname = NULL;
180 service_h svc = NULL;
183 ui_notification_h core = NULL;
184 PackageAppInfo* pPackageAppInfo = NULL;
186 if (!messageText.IsEmpty())
188 SysTryReturnResult(NID_APP,
189 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
190 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
192 retcode = ui_notification_create(isOngoing, &core);
193 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
195 pMsg = _StringConverter::CopyToCharArrayN(messageText);
197 int ret = ui_notification_set_content(core, pMsg);
198 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
200 const String& currappId = _AppInfo::GetApplicationId();
202 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(currappId);
203 iconPath = _PackageAppInfoImpl::GetInstance(pPackageAppInfo)->GetAppNotificationIconPath();
205 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
207 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
208 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
209 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
212 app_get_package(&pkgname);
213 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.");
215 r = arg.Construct(launchArguments);
216 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
218 pKb = arg.GetBundle();
219 service_create_request(pKb, &svc);
221 service_set_package(svc, pkgname);
222 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
223 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
225 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
227 r = ConvertNotificationResult(ui_notification_post(core));
228 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
231 if (badgeNumber >= 0)
233 badge_error_e badgeError = badge_set_count(appinfo_get_appid(), badgeNumber);
234 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
238 delete pPackageAppInfo;
248 ui_notification_destroy(core);
250 service_destroy(svc);
255 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
256 const String& launchArguments,
257 bool isOngoing) const
259 result r = E_SUCCESS;
265 ui_notification_h core = NULL;
268 service_h svc = NULL;
270 PackageAppInfo* pPackageAppInfo = null;
272 memset(buffer, 0, 256);
274 bool inInstalled = _Aul::IsInstalled(appId);
275 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
277 if (!isOngoing || !messageText.IsEmpty())
279 SysTryReturnResult(NID_APP,
280 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
281 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
283 retcode = ui_notification_create(isOngoing, &core);
284 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
286 pMsg = _StringConverter::CopyToCharArrayN(messageText);
288 int ret = ui_notification_set_content(core, pMsg);
289 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
291 snprintf(buffer, 256, "%ls", appId.GetPointer());
293 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
296 iconPath = pPackageAppInfo->GetAppNotificationIconPath();
298 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
300 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
301 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
302 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
306 iconPath = pPackageAppInfo->GetAppMenuIconPath();
307 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
308 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
309 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
312 const String& displayName = pPackageAppInfo->GetAppDisplayName();
313 pName = _StringConverter::CopyToCharArrayN(displayName);
315 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
316 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
320 SysLog(NID_APP, "No packageInfo found for %ls", appId.GetPointer());
324 r = arg.Construct(launchArguments);
325 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
327 pKb = arg.GetBundle();
328 service_create_request(pKb, &svc);
330 service_set_app_id(svc, buffer);
331 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
332 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
334 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
336 r = ConvertNotificationResult(ui_notification_post(core));
337 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
340 if (badgeNumber >= 0)
342 badge_error_e badgeError = badge_set_count(buffer, badgeNumber);
343 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
347 delete pPackageAppInfo;
354 ui_notification_destroy(core);
356 service_destroy(svc);
362 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
364 result r = E_SUCCESS;
366 notification_error_e err = notification_delete_all_by_type(pAppId, type);
369 case NOTIFICATION_ERROR_NONE:
373 case NOTIFICATION_ERROR_INVALID_DATA:
387 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
389 const bool isValidAppId = _Aul::IsInstalled(appId);
390 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
392 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
393 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
395 return RemoveImpl(pAppId.get(), notiType);
399 _NotificationManagerImpl::Notify(int badgeNumber) const
401 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
403 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
405 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
408 String messageText = String(L"");
409 String appMessage = String(L"");
411 return NotifyImpl(messageText, badgeNumber, appMessage, false);
415 _NotificationManagerImpl::Notify(const String& messageText) const
417 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
419 String appMessage = String(L"");
421 return NotifyImpl(messageText, -1, appMessage, false);
425 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
427 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
428 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
430 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
432 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
435 String appMessage = String(L"");
437 return NotifyImpl(messageText, badgeNumber, appMessage, false);
441 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
443 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
444 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
445 SysTryReturnResult(NID_APP,
446 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
447 "launchArguments is less than 0.");
449 SysTryReturnResult(NID_APP,
450 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
451 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
453 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
455 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
458 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
462 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
464 bool b = _Aul::IsInstalled(appId);
466 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
470 unsigned int count = 0;
472 memset(buffer, 0, 256);
474 snprintf(buffer, 256, "%ls", appId.GetPointer());
476 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
478 badge_error_e badgeError = badge_get_count(pAppId.get(), &count);
480 if (badgeError == BADGE_ERROR_NONE)
482 SysLog(NID_APP, "badge_get_count(%d)", count);
486 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
495 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
497 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
499 String messageText = String(L"");
500 String appMessage = String(L"");
501 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
505 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
507 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
509 return NotifyImpl(appId, messageText, -1, String(L""));
513 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
515 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
516 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
518 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
522 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
524 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
525 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
526 SysTryReturnResult(NID_APP,
527 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
528 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
530 return NotifyImpl(appId, messageText, -1, launchArguments);
534 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
535 const String& launchArguments) const
537 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
538 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
539 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
540 SysTryReturnResult(NID_APP,
541 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
542 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
544 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
548 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
550 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
552 return OngoingImpl(messageText, String(L""));
556 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
558 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
559 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "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 return OngoingImpl(messageText, launchArguments);
568 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
570 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
572 return OngoingImpl(appId, messageText, String(L""));
576 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
577 const String& launchArguments) const
579 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
580 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
581 SysTryReturnResult(NID_APP,
582 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
583 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
585 return OngoingImpl(appId, messageText, launchArguments);
589 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
591 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
595 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
597 return RemoveImpl(appId, true);
601 _NotificationManagerImpl::RemoveNotification(void)
603 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
607 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
609 return RemoveImpl(appId, false);