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);
188 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
190 pKb = arg.GetBundle();
191 service_create_request(pKb, &svc);
193 service_set_package(svc, pkgname);
194 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
195 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
197 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
199 r = ConvertNotificationResult(ui_notification_post(core));
200 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
203 if (badgeNumber >= 0)
205 badge_error_e badgeError = badge_set_count(appinfo_get_appid(), badgeNumber);
206 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
210 delete pPackageAppInfo;
220 ui_notification_destroy(core);
222 service_destroy(svc);
227 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
228 const String& launchArguments,
229 bool isOngoing) const
231 result r = E_SUCCESS;
237 ui_notification_h core = NULL;
240 service_h svc = NULL;
242 PackageAppInfo* pPackageAppInfo = null;
244 memset(buffer, 0, 256);
246 bool inInstalled = _Aul::IsInstalled(appId);
247 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
249 if (!isOngoing || !messageText.IsEmpty())
251 SysTryReturnResult(NID_APP,
252 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
253 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
255 retcode = ui_notification_create(isOngoing, &core);
256 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
258 pMsg = _StringConverter::CopyToCharArrayN(messageText);
260 int ret = ui_notification_set_content(core, pMsg);
261 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
263 snprintf(buffer, 256, "%ls", appId.GetPointer());
265 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
268 iconPath = pPackageAppInfo->GetAppNotificationIconPath();
270 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
272 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
273 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
274 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
278 iconPath = pPackageAppInfo->GetAppMenuIconPath();
279 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
280 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
281 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
284 const String& displayName = pPackageAppInfo->GetAppDisplayName();
285 pName = _StringConverter::CopyToCharArrayN(displayName);
287 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
288 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
292 SysLog(NID_APP, "No packageInfo found for %ls", appId.GetPointer());
296 r = arg.Construct(launchArguments);
297 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
299 pKb = arg.GetBundle();
300 service_create_request(pKb, &svc);
302 service_set_app_id(svc, buffer);
303 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
304 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
306 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
308 r = ConvertNotificationResult(ui_notification_post(core));
309 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
312 if (badgeNumber >= 0)
314 badge_error_e badgeError = badge_set_count(buffer, badgeNumber);
315 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
319 delete pPackageAppInfo;
326 ui_notification_destroy(core);
328 service_destroy(svc);
334 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
336 result r = E_SUCCESS;
338 notification_error_e err = notification_delete_all_by_type(pAppId, type);
341 case NOTIFICATION_ERROR_NONE:
345 case NOTIFICATION_ERROR_INVALID_DATA:
359 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
361 const bool isValidAppId = _Aul::IsInstalled(appId);
362 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
364 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
365 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
367 return RemoveImpl(pAppId.get(), notiType);
371 _NotificationManagerImpl::Notify(int badgeNumber) const
373 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
375 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
377 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
380 String messageText = String(L"");
381 String appMessage = String(L"");
383 return NotifyImpl(messageText, badgeNumber, appMessage, false);
387 _NotificationManagerImpl::Notify(const String& messageText) const
389 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
391 String appMessage = String(L"");
393 return NotifyImpl(messageText, -1, appMessage, false);
397 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
399 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
400 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
402 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
404 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
407 String appMessage = String(L"");
409 return NotifyImpl(messageText, badgeNumber, appMessage, false);
413 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
415 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
416 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
417 SysTryReturnResult(NID_APP,
418 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
419 "launchArguments is less than 0.");
421 SysTryReturnResult(NID_APP,
422 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
423 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
425 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
427 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
430 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
434 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
436 bool b = _Aul::IsInstalled(appId);
438 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
442 unsigned int count = 0;
444 memset(buffer, 0, 256);
446 snprintf(buffer, 256, "%ls", appId.GetPointer());
448 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
450 badge_error_e badgeError = badge_get_count(pAppId.get(), &count);
452 if (badgeError == BADGE_ERROR_NONE)
454 SysLog(NID_APP, "badge_get_count(%d)", count);
458 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
467 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
469 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
471 String messageText = String(L"");
472 String appMessage = String(L"");
473 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
477 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
479 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
481 return NotifyImpl(appId, messageText, -1, String(L""));
485 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
487 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
488 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
490 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
494 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
496 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
497 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
498 SysTryReturnResult(NID_APP,
499 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
500 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
502 return NotifyImpl(appId, messageText, -1, launchArguments);
506 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
507 const String& launchArguments) const
509 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
510 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
511 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
512 SysTryReturnResult(NID_APP,
513 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
514 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
516 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
520 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
522 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
524 return OngoingImpl(messageText, String(L""));
528 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
530 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
531 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
532 SysTryReturnResult(NID_APP,
533 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
534 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
536 return OngoingImpl(messageText, launchArguments);
540 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
542 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
544 return OngoingImpl(appId, messageText, String(L""));
548 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
549 const String& launchArguments) const
551 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
552 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
553 SysTryReturnResult(NID_APP,
554 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
555 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
557 return OngoingImpl(appId, messageText, launchArguments);
561 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
563 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
567 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
569 return RemoveImpl(appId, true);
573 _NotificationManagerImpl::RemoveNotification(void)
575 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
579 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
581 return RemoveImpl(appId, false);