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 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
377 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
379 return RemoveImpl(pAppId.get(), notiType);
383 _NotificationManagerImpl::Notify(int badgeNumber) const
385 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
387 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
389 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
392 String messageText = String(L"");
393 String appMessage = String(L"");
395 return NotifyImpl(messageText, badgeNumber, appMessage, false);
399 _NotificationManagerImpl::Notify(const String& messageText) const
401 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
403 String appMessage = String(L"");
405 return NotifyImpl(messageText, -1, appMessage, false);
409 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
411 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
412 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
414 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
416 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
419 String appMessage = String(L"");
421 return NotifyImpl(messageText, badgeNumber, appMessage, false);
425 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
427 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
428 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
429 SysTryReturnResult(NID_APP,
430 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
431 "launchArguments is less than 0.");
433 SysTryReturnResult(NID_APP,
434 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
435 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
437 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
439 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
442 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
446 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
448 bool b = _Aul::IsInstalled(appId);
450 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
456 memset(buffer, 0, 256);
458 snprintf(buffer, 256, "%ls", appId.GetPointer());
460 notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
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);