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_AppInfo.h"
39 #include "FAppPkg_PackageManagerImpl.h"
41 #include "FApp_AppArg.h"
42 #include "FAppPkgPackageAppInfo.h"
43 #include "FAppPkg_PackageAppInfoImpl.h"
45 using namespace Tizen::Base;
46 using namespace Tizen::App;
47 using namespace Tizen::App::Package;
48 using namespace Tizen::Io;
50 extern "C" int service_create_request(bundle *data, service_h *service);
56 ConvertNotificationResult(int error)
60 case UI_NOTIFICATION_ERROR_NONE:
62 case UI_NOTIFICATION_ERROR_INVALID_PARAMETER:
64 case UI_NOTIFICATION_ERROR_OUT_OF_MEMORY:
65 return E_OUT_OF_MEMORY;
66 case UI_NOTIFICATION_ERROR_DB_FAILED:
68 case UI_NOTIFICATION_ERROR_NO_SUCH_FILE:
70 case UI_NOTIFICATION_ERROR_INVALID_STATE:
78 IsPosted(ui_notification_h handle)
80 struct ui_notification_s
100 ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
102 return pStruct->posted;
107 namespace Tizen { namespace App
110 _NotificationManagerImpl::_NotificationManagerImpl(void)
114 _NotificationManagerImpl::~_NotificationManagerImpl(void)
119 _NotificationManagerImpl::Construct(void)
124 const _NotificationManagerImpl*
125 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
127 return notiMgr.__pNotificationManagerImpl;
130 _NotificationManagerImpl*
131 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
133 return notiMgr.__pNotificationManagerImpl;
137 _NotificationManagerImpl::GetBadgeNumber(void) const
140 notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
145 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
147 return NotifyImpl(messageText, -1, launchArguments, true);
151 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
154 return NotifyImpl(appId, messageText, -1, launchArguments, true);
158 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
159 const String& launchArguments,
160 bool isOngoing) const
162 result r = E_SUCCESS;
165 char* pkgname = NULL;
168 service_h svc = NULL;
171 ui_notification_h core = NULL;
172 PackageAppInfo* pPackageAppInfo = NULL;
174 if (!messageText.IsEmpty())
176 SysTryReturnResult(NID_APP,
177 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
178 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
180 retcode = ui_notification_create(isOngoing, &core);
181 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
183 pMsg = _StringConverter::CopyToCharArrayN(messageText);
185 int ret = ui_notification_set_content(core, pMsg);
186 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
188 const String& currappId = _AppInfo::GetApplicationId();
190 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(currappId);
191 iconPath = _PackageAppInfoImpl::GetInstance(pPackageAppInfo)->GetAppNotificationIconPath();
193 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
195 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
196 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
197 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
200 app_get_package(&pkgname);
201 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.");
203 r = arg.Construct(launchArguments);
204 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
206 pKb = arg.GetBundle();
207 service_create_request(pKb, &svc);
209 service_set_package(svc, pkgname);
210 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
211 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
213 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
215 r = ConvertNotificationResult(ui_notification_post(core));
216 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
219 if (badgeNumber >= 0)
221 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
222 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
226 delete pPackageAppInfo;
236 ui_notification_destroy(core);
238 service_destroy(svc);
243 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
244 const String& launchArguments,
245 bool isOngoing) const
247 result r = E_SUCCESS;
252 char* pDefaultIconPath = NULL;
254 ui_notification_h core = NULL;
257 service_h svc = NULL;
259 PackageAppInfo* pPackageAppInfo = null;
261 memset(buffer, 0, 256);
263 bool inInstalled = _Aul::IsInstalled(appId);
264 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
266 if (!isOngoing || !messageText.IsEmpty())
268 SysTryReturnResult(NID_APP,
269 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
270 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
272 retcode = ui_notification_create(isOngoing, &core);
273 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
275 pMsg = _StringConverter::CopyToCharArrayN(messageText);
277 int ret = ui_notification_set_content(core, pMsg);
278 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
280 snprintf(buffer, 256, "%ls", appId.GetPointer());
282 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
283 iconPath = _PackageAppInfoImpl::GetInstance(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 app_manager_get_app_icon_path(buffer, &pDefaultIconPath);
294 r = ConvertNotificationResult(ui_notification_set_icon(core, pDefaultIconPath));
295 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
298 app_manager_get_app_name(buffer, &pName);
299 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
300 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
302 r = arg.Construct(launchArguments);
303 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
305 pKb = arg.GetBundle();
306 service_create_request(pKb, &svc);
308 service_set_app_id(svc, buffer);
309 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
310 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
312 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
314 r = ConvertNotificationResult(ui_notification_post(core));
315 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
318 if (badgeNumber >= 0)
320 notification_set_badge(buffer, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
321 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
325 delete pPackageAppInfo;
328 if (pDefaultIconPath)
330 free(pDefaultIconPath);
334 ui_notification_destroy(core);
340 service_destroy(svc);
346 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
348 result r = E_SUCCESS;
350 notification_error_e err = notification_delete_all_by_type(pAppId, type);
353 case NOTIFICATION_ERROR_NONE:
357 case NOTIFICATION_ERROR_INVALID_DATA:
371 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
373 const bool isValidAppId = _Aul::IsInstalled(appId);
374 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
376 result r = E_SUCCESS;
377 notification_error_e err = NOTIFICATION_ERROR_NONE;
378 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
379 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
381 return RemoveImpl(pAppId.get(), notiType);
385 _NotificationManagerImpl::Notify(int badgeNumber) const
387 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
389 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
391 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
394 String messageText = String(L"");
395 String appMessage = String(L"");
397 return NotifyImpl(messageText, badgeNumber, appMessage, false);
401 _NotificationManagerImpl::Notify(const String& messageText) const
403 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
405 String appMessage = String(L"");
407 return NotifyImpl(messageText, -1, appMessage, false);
411 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
413 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
414 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
416 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
418 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
421 String appMessage = String(L"");
423 return NotifyImpl(messageText, badgeNumber, appMessage, false);
427 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
429 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
430 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
431 SysTryReturnResult(NID_APP,
432 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
433 "launchArguments is less than 0.");
435 SysTryReturnResult(NID_APP,
436 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
437 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
439 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
441 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
444 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
448 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
450 bool b = _Aul::IsInstalled(appId);
452 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
458 memset(buffer, 0, 256);
460 snprintf(buffer, 256, "%ls", appId.GetPointer());
462 notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
469 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
471 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
473 String messageText = String(L"");
474 String appMessage = String(L"");
475 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
479 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
481 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
483 return NotifyImpl(appId, messageText, -1, String(L""));
487 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
489 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
490 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
492 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
496 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
498 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
499 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
500 SysTryReturnResult(NID_APP,
501 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
502 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
504 return NotifyImpl(appId, messageText, -1, launchArguments);
508 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
509 const String& launchArguments) const
511 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
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 NotifyImpl(appId, messageText, badgeNumber, launchArguments);
522 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
524 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
526 return OngoingImpl(messageText, String(L""));
530 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
532 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
533 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
534 SysTryReturnResult(NID_APP,
535 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
536 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
538 return OngoingImpl(messageText, launchArguments);
542 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
544 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
546 return OngoingImpl(appId, messageText, String(L""));
550 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
551 const String& launchArguments) const
553 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
554 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
555 SysTryReturnResult(NID_APP,
556 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
557 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
559 return OngoingImpl(appId, messageText, launchArguments);
563 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
565 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
569 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
571 return RemoveImpl(appId, true);
575 _NotificationManagerImpl::RemoveNotification(void)
577 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
581 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
583 return RemoveImpl(appId, false);