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:
80 namespace Tizen { namespace App
83 _NotificationManagerImpl::_NotificationManagerImpl(void)
87 _NotificationManagerImpl::~_NotificationManagerImpl(void)
92 _NotificationManagerImpl::Construct(void)
97 const _NotificationManagerImpl*
98 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
100 return notiMgr.__pNotificationManagerImpl;
103 _NotificationManagerImpl*
104 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
106 return notiMgr.__pNotificationManagerImpl;
110 _NotificationManagerImpl::GetBadgeNumber(void) const
112 unsigned int count = 0;
114 badge_error_e badgeError = badge_get_count(appinfo_get_appid(), &count);
116 if (badgeError == BADGE_ERROR_NONE)
118 SysLog(NID_APP, "badge_get_count(%d)", count);
122 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
129 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
131 return NotifyImpl(messageText, -1, launchArguments, true);
135 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
138 return NotifyImpl(appId, messageText, -1, launchArguments, true);
142 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
143 const String& launchArguments,
144 bool isOngoing) const
146 result r = E_SUCCESS;
149 char* pkgname = NULL;
152 service_h svc = NULL;
155 ui_notification_h core = NULL;
156 PackageAppInfo* pPackageAppInfo = NULL;
158 if (!messageText.IsEmpty())
160 SysTryReturnResult(NID_APP,
161 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
162 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
164 retcode = ui_notification_create(isOngoing, &core);
165 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
167 pMsg = _StringConverter::CopyToCharArrayN(messageText);
169 int ret = ui_notification_set_content(core, pMsg);
170 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
172 const String& currappId = _AppInfo::GetApplicationId();
174 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(currappId);
175 iconPath = _PackageAppInfoImpl::GetInstance(pPackageAppInfo)->GetAppNotificationIconPath();
177 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
179 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
180 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
181 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
184 app_get_package(&pkgname);
185 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.");
187 r = arg.Construct(launchArguments);
190 SysPropagate(NID_APP, r);
194 pKb = arg.GetBundle();
195 service_create_request(pKb, &svc);
197 service_set_package(svc, pkgname);
198 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
199 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
201 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
203 r = ConvertNotificationResult(ui_notification_post(core));
204 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
207 if (badgeNumber >= 0)
209 badge_error_e badgeError = badge_set_count(appinfo_get_appid(), badgeNumber);
210 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
214 delete pPackageAppInfo;
224 ui_notification_destroy(core);
226 service_destroy(svc);
231 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
232 const String& launchArguments,
233 bool isOngoing) const
235 result r = E_SUCCESS;
241 ui_notification_h core = NULL;
244 service_h svc = NULL;
246 PackageAppInfo* pPackageAppInfo = null;
248 memset(buffer, 0, 256);
250 bool inInstalled = _Aul::IsInstalled(appId);
251 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
253 if (!isOngoing || !messageText.IsEmpty())
255 SysTryReturnResult(NID_APP,
256 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
257 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
259 retcode = ui_notification_create(isOngoing, &core);
260 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
262 pMsg = _StringConverter::CopyToCharArrayN(messageText);
264 int ret = ui_notification_set_content(core, pMsg);
265 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
267 snprintf(buffer, 256, "%ls", appId.GetPointer());
269 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
272 iconPath = pPackageAppInfo->GetAppNotificationIconPath();
274 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
276 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
277 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
278 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
282 iconPath = pPackageAppInfo->GetAppMenuIconPath();
283 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
284 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
285 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
288 const String& displayName = pPackageAppInfo->GetAppDisplayName();
289 pName = _StringConverter::CopyToCharArrayN(displayName);
291 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
292 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
296 SysLog(NID_APP, "No packageInfo found for %ls", appId.GetPointer());
300 r = arg.Construct(launchArguments);
303 SysPropagate(NID_APP, r);
307 pKb = arg.GetBundle();
308 service_create_request(pKb, &svc);
310 service_set_app_id(svc, buffer);
311 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
312 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
314 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
316 r = ConvertNotificationResult(ui_notification_post(core));
317 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
320 if (badgeNumber >= 0)
322 badge_error_e badgeError = badge_set_count(buffer, badgeNumber);
323 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
327 delete pPackageAppInfo;
334 ui_notification_destroy(core);
336 service_destroy(svc);
342 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
344 result r = E_SUCCESS;
346 notification_error_e err = notification_delete_all_by_type(pAppId, type);
349 case NOTIFICATION_ERROR_NONE:
353 case NOTIFICATION_ERROR_INVALID_DATA:
367 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
369 const bool isValidAppId = _Aul::IsInstalled(appId);
370 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
372 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
373 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
375 return RemoveImpl(pAppId.get(), notiType);
379 _NotificationManagerImpl::Notify(int badgeNumber) const
381 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
383 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
385 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
388 String messageText = String(L"");
389 String appMessage = String(L"");
391 return NotifyImpl(messageText, badgeNumber, appMessage, false);
395 _NotificationManagerImpl::Notify(const String& messageText) const
397 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
399 String appMessage = String(L"");
401 return NotifyImpl(messageText, -1, appMessage, false);
405 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
407 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
408 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
410 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
412 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
415 String appMessage = String(L"");
417 return NotifyImpl(messageText, badgeNumber, appMessage, false);
421 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
423 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
424 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
425 SysTryReturnResult(NID_APP,
426 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
427 "launchArguments is less than 0.");
429 SysTryReturnResult(NID_APP,
430 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
431 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
433 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
435 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
438 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
442 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
444 bool b = _Aul::IsInstalled(appId);
446 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
450 unsigned int count = 0;
452 memset(buffer, 0, 256);
454 snprintf(buffer, 256, "%ls", appId.GetPointer());
456 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
458 badge_error_e badgeError = badge_get_count(pAppId.get(), &count);
460 if (badgeError == BADGE_ERROR_NONE)
462 SysLog(NID_APP, "badge_get_count(%d)", count);
466 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
475 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
477 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
479 String messageText = String(L"");
480 String appMessage = String(L"");
481 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
485 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
487 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
489 return NotifyImpl(appId, messageText, -1, String(L""));
493 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
495 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
496 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
498 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
502 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
504 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
505 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
506 SysTryReturnResult(NID_APP,
507 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
508 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
510 return NotifyImpl(appId, messageText, -1, launchArguments);
514 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
515 const String& launchArguments) const
517 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
518 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
519 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
520 SysTryReturnResult(NID_APP,
521 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
522 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
524 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
528 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
530 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
532 return OngoingImpl(messageText, String(L""));
536 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
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 OngoingImpl(messageText, launchArguments);
548 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
550 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
552 return OngoingImpl(appId, messageText, String(L""));
556 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
557 const String& launchArguments) const
559 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
560 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
561 SysTryReturnResult(NID_APP,
562 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
563 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
565 return OngoingImpl(appId, messageText, launchArguments);
569 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
571 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
575 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
577 return RemoveImpl(appId, true);
581 _NotificationManagerImpl::RemoveNotification(void)
583 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
587 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
589 return RemoveImpl(appId, false);