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>
26 #include <notification/notification.h>
27 #include <appfw/app.h>
28 #include <appfw/app_ui_notification.h>
30 #include <FBaseSysLog.h>
31 #include <FAppNotificationManager.h>
33 #include <FBaseInternalTypes.h>
34 #include <FBase_StringConverter.h>
35 #include "FApp_NotificationManagerImpl.h"
36 #include "FApp_AppInfo.h"
38 #include "FAppPkg_PackageManagerImpl.h"
40 #include "FApp_AppArg.h"
41 #include "FAppPkgPackageAppInfo.h"
42 #include "FAppPkg_PackageAppInfoImpl.h"
44 using namespace Tizen::Base;
45 using namespace Tizen::App;
46 using namespace Tizen::App::Package;
47 using namespace Tizen::Io;
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:
69 case UI_NOTIFICATION_ERROR_INVALID_STATE:
77 IsPosted(ui_notification_h handle)
79 struct ui_notification_s
99 ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
101 return pStruct->posted;
106 namespace Tizen { namespace App
109 _NotificationManagerImpl::_NotificationManagerImpl(void)
113 _NotificationManagerImpl::~_NotificationManagerImpl(void)
118 _NotificationManagerImpl::Construct(void)
123 const _NotificationManagerImpl*
124 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
126 return notiMgr.__pNotificationManagerImpl;
129 _NotificationManagerImpl*
130 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
132 return notiMgr.__pNotificationManagerImpl;
136 _NotificationManagerImpl::GetBadgeNumber(void) const
139 notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
144 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
146 return NotifyImpl(messageText, -1, launchArguments, true);
150 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
153 return NotifyImpl(appId, messageText, -1, launchArguments, true);
157 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
158 const String& launchArguments,
159 bool isOngoing) const
161 result r = E_SUCCESS;
164 char* pkgname = NULL;
167 service_h svc = NULL;
170 ui_notification_h core = NULL;
171 PackageAppInfo* pPackageAppInfo = NULL;
173 if (!messageText.IsEmpty())
175 SysTryReturnResult(NID_APP,
176 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
177 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
179 retcode = ui_notification_create(isOngoing, &core);
180 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
182 pMsg = _StringConverter::CopyToCharArrayN(messageText);
184 int ret = ui_notification_set_content(core, pMsg);
185 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
187 const String& currappId = _AppInfo::GetApplicationId();
189 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(currappId);
190 iconPath = _PackageAppInfoImpl::GetInstance(pPackageAppInfo)->GetAppNotificationIconPath();
192 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
194 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
195 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
196 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
199 app_get_package(&pkgname);
200 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.");
202 r = arg.Construct(launchArguments);
203 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
205 pKb = arg.GetBundle();
206 service_create_request(pKb, &svc);
208 service_set_package(svc, pkgname);
209 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
210 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
212 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
214 r = ConvertNotificationResult(ui_notification_post(core));
215 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
218 if (badgeNumber >= 0)
220 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
221 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
225 delete pPackageAppInfo;
235 ui_notification_destroy(core);
237 service_destroy(svc);
242 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
243 const String& launchArguments,
244 bool isOngoing) const
246 result r = E_SUCCESS;
252 ui_notification_h core = NULL;
255 service_h svc = NULL;
257 PackageAppInfo* pPackageAppInfo = null;
259 memset(buffer, 0, 256);
261 bool inInstalled = _Aul::IsInstalled(appId);
262 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
264 if (!isOngoing || !messageText.IsEmpty())
266 SysTryReturnResult(NID_APP,
267 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
268 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
270 retcode = ui_notification_create(isOngoing, &core);
271 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
273 pMsg = _StringConverter::CopyToCharArrayN(messageText);
275 int ret = ui_notification_set_content(core, pMsg);
276 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
278 snprintf(buffer, 256, "%ls", appId.GetPointer());
280 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
283 iconPath = pPackageAppInfo->GetAppNotificationIconPath();
285 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
287 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
288 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
289 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
293 iconPath = pPackageAppInfo->GetAppMenuIconPath();
294 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
295 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
296 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
299 const String& displayName = pPackageAppInfo->GetAppDisplayName();
300 pName = _StringConverter::CopyToCharArrayN(displayName);
302 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
303 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
307 SysLog(NID_APP, "No packageInfo found for %ls", appId.GetPointer());
311 r = arg.Construct(launchArguments);
312 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
314 pKb = arg.GetBundle();
315 service_create_request(pKb, &svc);
317 service_set_app_id(svc, buffer);
318 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
319 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
321 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
323 r = ConvertNotificationResult(ui_notification_post(core));
324 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
327 if (badgeNumber >= 0)
329 notification_set_badge(buffer, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
330 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
334 delete pPackageAppInfo;
341 ui_notification_destroy(core);
343 service_destroy(svc);
349 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
351 result r = E_SUCCESS;
353 notification_error_e err = notification_delete_all_by_type(pAppId, type);
356 case NOTIFICATION_ERROR_NONE:
360 case NOTIFICATION_ERROR_INVALID_DATA:
374 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
376 const bool isValidAppId = _Aul::IsInstalled(appId);
377 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
379 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
380 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
382 return RemoveImpl(pAppId.get(), notiType);
386 _NotificationManagerImpl::Notify(int badgeNumber) const
388 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
390 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
392 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
395 String messageText = String(L"");
396 String appMessage = String(L"");
398 return NotifyImpl(messageText, badgeNumber, appMessage, false);
402 _NotificationManagerImpl::Notify(const String& messageText) const
404 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
406 String appMessage = String(L"");
408 return NotifyImpl(messageText, -1, appMessage, false);
412 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
414 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
415 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
417 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
419 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
422 String appMessage = String(L"");
424 return NotifyImpl(messageText, badgeNumber, appMessage, false);
428 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
430 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
431 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
432 SysTryReturnResult(NID_APP,
433 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
434 "launchArguments is less than 0.");
436 SysTryReturnResult(NID_APP,
437 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
438 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
440 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
442 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
445 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
449 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
451 bool b = _Aul::IsInstalled(appId);
453 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
459 memset(buffer, 0, 256);
461 snprintf(buffer, 256, "%ls", appId.GetPointer());
463 notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
470 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
472 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
474 String messageText = String(L"");
475 String appMessage = String(L"");
476 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
480 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
482 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
484 return NotifyImpl(appId, messageText, -1, String(L""));
488 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
490 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
491 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
493 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
497 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
499 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
500 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
501 SysTryReturnResult(NID_APP,
502 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
503 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
505 return NotifyImpl(appId, messageText, -1, launchArguments);
509 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
510 const String& launchArguments) const
512 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
513 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
514 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
515 SysTryReturnResult(NID_APP,
516 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
517 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
519 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
523 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
525 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
527 return OngoingImpl(messageText, String(L""));
531 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
533 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
534 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
535 SysTryReturnResult(NID_APP,
536 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
537 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
539 return OngoingImpl(messageText, launchArguments);
543 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
545 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
547 return OngoingImpl(appId, messageText, String(L""));
551 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
552 const String& launchArguments) const
554 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
555 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
556 SysTryReturnResult(NID_APP,
557 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
558 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
560 return OngoingImpl(appId, messageText, launchArguments);
564 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
566 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
570 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
572 return RemoveImpl(appId, true);
576 _NotificationManagerImpl::RemoveNotification(void)
578 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
582 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
584 return RemoveImpl(appId, false);