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>
23 #include <appsvc/appsvc.h>
25 #include <notification/notification.h>
26 #include <appfw/app.h>
27 #include <appfw/app_manager.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;
251 char* pDefaultIconPath = NULL;
253 ui_notification_h core = NULL;
256 service_h svc = NULL;
258 PackageAppInfo* pPackageAppInfo = null;
260 memset(buffer, 0, 256);
262 bool inInstalled = _Aul::IsInstalled(appId);
263 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
265 if (!isOngoing || !messageText.IsEmpty())
267 SysTryReturnResult(NID_APP,
268 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
269 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
271 retcode = ui_notification_create(isOngoing, &core);
272 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
274 pMsg = _StringConverter::CopyToCharArrayN(messageText);
276 int ret = ui_notification_set_content(core, pMsg);
277 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
279 snprintf(buffer, 256, "%ls", appId.GetPointer());
281 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
282 iconPath = _PackageAppInfoImpl::GetInstance(pPackageAppInfo)->GetAppNotificationIconPath();
284 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
286 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
287 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
288 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
292 app_manager_get_app_icon_path(buffer, &pDefaultIconPath);
293 r = ConvertNotificationResult(ui_notification_set_icon(core, pDefaultIconPath));
294 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
297 app_manager_get_app_name(buffer, &pName);
298 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
299 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
301 r = arg.Construct(launchArguments);
302 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
304 pKb = arg.GetBundle();
305 service_create_request(pKb, &svc);
307 service_set_app_id(svc, buffer);
308 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
309 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
311 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
313 r = ConvertNotificationResult(ui_notification_post(core));
314 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
317 if (badgeNumber >= 0)
319 notification_set_badge(buffer, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
320 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
324 delete pPackageAppInfo;
327 if (pDefaultIconPath)
329 free(pDefaultIconPath);
333 ui_notification_destroy(core);
339 service_destroy(svc);
345 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
347 result r = E_SUCCESS;
349 notification_error_e err = notification_delete_all_by_type(pAppId, type);
352 case NOTIFICATION_ERROR_NONE:
356 case NOTIFICATION_ERROR_INVALID_DATA:
370 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
372 const bool isValidAppId = _Aul::IsInstalled(appId);
373 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
375 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
376 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
378 return RemoveImpl(pAppId.get(), notiType);
382 _NotificationManagerImpl::Notify(int badgeNumber) const
384 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
386 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
388 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
391 String messageText = String(L"");
392 String appMessage = String(L"");
394 return NotifyImpl(messageText, badgeNumber, appMessage, false);
398 _NotificationManagerImpl::Notify(const String& messageText) const
400 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
402 String appMessage = String(L"");
404 return NotifyImpl(messageText, -1, appMessage, false);
408 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
410 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
411 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
413 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
415 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
418 String appMessage = String(L"");
420 return NotifyImpl(messageText, badgeNumber, appMessage, false);
424 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
426 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
427 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
428 SysTryReturnResult(NID_APP,
429 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
430 "launchArguments is less than 0.");
432 SysTryReturnResult(NID_APP,
433 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
434 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
436 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
438 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
441 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
445 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
447 bool b = _Aul::IsInstalled(appId);
449 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
455 memset(buffer, 0, 256);
457 snprintf(buffer, 256, "%ls", appId.GetPointer());
459 notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
466 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
468 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
470 String messageText = String(L"");
471 String appMessage = String(L"");
472 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
476 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
478 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
480 return NotifyImpl(appId, messageText, -1, String(L""));
484 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
486 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
487 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
489 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
493 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, const String& launchArguments) const
495 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
496 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
497 SysTryReturnResult(NID_APP,
498 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
499 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
501 return NotifyImpl(appId, messageText, -1, launchArguments);
505 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
506 const String& launchArguments) const
508 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
509 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
510 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
511 SysTryReturnResult(NID_APP,
512 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
513 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
515 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
519 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
521 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
523 return OngoingImpl(messageText, String(L""));
527 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
529 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
530 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
531 SysTryReturnResult(NID_APP,
532 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
533 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
535 return OngoingImpl(messageText, launchArguments);
539 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
541 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
543 return OngoingImpl(appId, messageText, String(L""));
547 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
548 const String& launchArguments) const
550 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
551 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
552 SysTryReturnResult(NID_APP,
553 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
554 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
556 return OngoingImpl(appId, messageText, launchArguments);
560 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
562 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
566 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
568 return RemoveImpl(appId, true);
572 _NotificationManagerImpl::RemoveNotification(void)
574 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
578 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
580 return RemoveImpl(appId, false);