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>
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
139 unsigned int count = 0;
141 const AppId appId = _AppInfo::GetApplicationId();
143 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
145 badge_error_e badgeError = badge_get_count(pAppId.get(), &count);
147 if (badgeError == BADGE_ERROR_NONE)
149 SysLog(NID_APP, "badge_get_count(%d)", count);
153 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
160 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
162 return NotifyImpl(messageText, -1, launchArguments, true);
166 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
169 return NotifyImpl(appId, messageText, -1, launchArguments, true);
173 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
174 const String& launchArguments,
175 bool isOngoing) const
177 result r = E_SUCCESS;
180 char* pkgname = NULL;
183 service_h svc = NULL;
186 ui_notification_h core = NULL;
187 PackageAppInfo* pPackageAppInfo = NULL;
189 if (!messageText.IsEmpty())
191 SysTryReturnResult(NID_APP,
192 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
193 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
195 retcode = ui_notification_create(isOngoing, &core);
196 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
198 pMsg = _StringConverter::CopyToCharArrayN(messageText);
200 int ret = ui_notification_set_content(core, pMsg);
201 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
203 const String& currappId = _AppInfo::GetApplicationId();
205 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(currappId);
206 iconPath = _PackageAppInfoImpl::GetInstance(pPackageAppInfo)->GetAppNotificationIconPath();
208 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
210 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
211 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
212 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
215 app_get_package(&pkgname);
216 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.");
218 r = arg.Construct(launchArguments);
219 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
221 pKb = arg.GetBundle();
222 service_create_request(pKb, &svc);
224 service_set_package(svc, pkgname);
225 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
226 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
228 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
230 r = ConvertNotificationResult(ui_notification_post(core));
231 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
234 if (badgeNumber >= 0)
236 const AppId appId = _AppInfo::GetApplicationId();
237 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
238 badge_error_e badgeError = badge_set_count(pAppId.get(), badgeNumber);
239 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
243 delete pPackageAppInfo;
253 ui_notification_destroy(core);
255 service_destroy(svc);
260 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
261 const String& launchArguments,
262 bool isOngoing) const
264 result r = E_SUCCESS;
270 ui_notification_h core = NULL;
273 service_h svc = NULL;
275 PackageAppInfo* pPackageAppInfo = null;
277 memset(buffer, 0, 256);
279 bool inInstalled = _Aul::IsInstalled(appId);
280 SysTryReturnResult(NID_APP, inInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
282 if (!isOngoing || !messageText.IsEmpty())
284 SysTryReturnResult(NID_APP,
285 messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
286 "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
288 retcode = ui_notification_create(isOngoing, &core);
289 SysTryReturnResult(NID_APP, retcode == UI_NOTIFICATION_ERROR_NONE, E_SYSTEM, "Notification creation error : 0x%x.", retcode);
291 pMsg = _StringConverter::CopyToCharArrayN(messageText);
293 int ret = ui_notification_set_content(core, pMsg);
294 SysTryLog(NID_APP, ret == UI_NOTIFICATION_ERROR_NONE, "Setting notification content failure : %d.", ret);
296 snprintf(buffer, 256, "%ls", appId.GetPointer());
298 pPackageAppInfo = _PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId);
301 iconPath = pPackageAppInfo->GetAppNotificationIconPath();
303 if (!iconPath.IsEmpty() && File::IsFileExist(iconPath))
305 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
306 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
307 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
311 iconPath = pPackageAppInfo->GetAppMenuIconPath();
312 pIcon = _StringConverter::CopyToCharArrayN(iconPath);
313 r = ConvertNotificationResult(ui_notification_set_icon(core, pIcon));
314 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path failed.", GetErrorMessage(r));
317 const String& displayName = pPackageAppInfo->GetAppDisplayName();
318 pName = _StringConverter::CopyToCharArrayN(displayName);
320 r = ConvertNotificationResult(ui_notification_set_title(core, pName));
321 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title failed.", GetErrorMessage(r));
325 SysLog(NID_APP, "No packageInfo found for %ls", appId.GetPointer());
329 r = arg.Construct(launchArguments);
330 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
332 pKb = arg.GetBundle();
333 service_create_request(pKb, &svc);
335 service_set_app_id(svc, buffer);
336 r = ConvertNotificationResult(ui_notification_set_service(core, svc));
337 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set service failed.", GetErrorMessage(r));
339 SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
341 r = ConvertNotificationResult(ui_notification_post(core));
342 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
345 if (badgeNumber >= 0)
347 badge_error_e badgeError = badge_set_count(buffer, badgeNumber);
348 SysTryLog(NID_APP, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
352 delete pPackageAppInfo;
359 ui_notification_destroy(core);
361 service_destroy(svc);
367 _NotificationManagerImpl::RemoveImpl(const char* pAppId, notification_type_e type)
369 result r = E_SUCCESS;
371 notification_error_e err = notification_delete_all_by_type(pAppId, type);
374 case NOTIFICATION_ERROR_NONE:
378 case NOTIFICATION_ERROR_INVALID_DATA:
392 _NotificationManagerImpl::RemoveImpl(const AppId& appId, bool isOngoing)
394 const bool isValidAppId = _Aul::IsInstalled(appId);
395 SysTryReturnResult(NID_APP, isValidAppId, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
397 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
398 const notification_type_e notiType = (isOngoing) ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
400 return RemoveImpl(pAppId.get(), notiType);
404 _NotificationManagerImpl::Notify(int badgeNumber) const
406 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
408 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
410 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
413 String messageText = String(L"");
414 String appMessage = String(L"");
416 return NotifyImpl(messageText, badgeNumber, appMessage, false);
420 _NotificationManagerImpl::Notify(const String& messageText) const
422 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
424 String appMessage = String(L"");
426 return NotifyImpl(messageText, -1, appMessage, false);
430 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
432 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
433 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
435 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
437 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
440 String appMessage = String(L"");
442 return NotifyImpl(messageText, badgeNumber, appMessage, false);
446 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
448 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
449 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
450 SysTryReturnResult(NID_APP,
451 launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
452 "launchArguments is less than 0.");
454 SysTryReturnResult(NID_APP,
455 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
456 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
458 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
460 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
463 return NotifyImpl(messageText, badgeNumber, launchArguments, false);
467 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
469 bool b = _Aul::IsInstalled(appId);
471 SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
475 unsigned int count = 0;
477 memset(buffer, 0, 256);
479 snprintf(buffer, 256, "%ls", appId.GetPointer());
481 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
483 badge_error_e badgeError = badge_get_count(pAppId.get(), &count);
485 if (badgeError == BADGE_ERROR_NONE)
487 SysLog(NID_APP, "badge_get_count(%d)", count);
491 SysLog(NID_APP, "badge_get_count failed(%d).", badgeError);
500 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, int badgeNumber) const
502 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
504 String messageText = String(L"");
505 String appMessage = String(L"");
506 return NotifyImpl(appId, messageText, badgeNumber, appMessage);
510 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText) const
512 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
514 return NotifyImpl(appId, messageText, -1, String(L""));
518 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber) const
520 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
521 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
523 return NotifyImpl(appId, messageText, badgeNumber, String(L""));
527 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, 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 NotifyImpl(appId, messageText, -1, launchArguments);
539 _NotificationManagerImpl::NotifyOnBehalf(const AppId& appId, const String& messageText, int badgeNumber,
540 const String& launchArguments) const
542 SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
543 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
544 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
545 SysTryReturnResult(NID_APP,
546 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
547 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
549 return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
553 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
555 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
557 return OngoingImpl(messageText, String(L""));
561 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
563 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
564 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
565 SysTryReturnResult(NID_APP,
566 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
567 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
569 return OngoingImpl(messageText, launchArguments);
573 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText) const
575 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
577 return OngoingImpl(appId, messageText, String(L""));
581 _NotificationManagerImpl::NotifyOngoingActivityOnBehalf(const AppId& appId, const String& messageText,
582 const String& launchArguments) const
584 SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
585 SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
586 SysTryReturnResult(NID_APP,
587 launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
588 "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
590 return OngoingImpl(appId, messageText, launchArguments);
594 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
596 return RemoveImpl(NULL, NOTIFICATION_TYPE_ONGOING);
600 _NotificationManagerImpl::RemoveOngoingActivityNotificationOnBehalf(const AppId& appId)
602 return RemoveImpl(appId, true);
606 _NotificationManagerImpl::RemoveNotification(void)
608 return RemoveImpl(NULL, NOTIFICATION_TYPE_NOTI);
612 _NotificationManagerImpl::RemoveNotificationOnBehalf(const AppId& appId)
614 return RemoveImpl(appId, false);