apply new policy for ongoing notification
[platform/framework/native/shell.git] / src / core / FShell_NotificationManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FShell_NotificationManagerImpl.cpp
20  * @brief       This is the implementation for _NotificationManagerImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <notification/notification.h>
25 #include <appfw/app.h>
26 #include <appfw/app_manager.h>
27 #include <badge.h>
28 #include <FBaseSysLog.h>
29 #include <FIoFile.h>
30 #include <FAppApp.h>
31 #include <FShellNotificationManager.h>
32 #include <FShellNotificationRequest.h>
33 #include <FShellIBadgeEventListener.h>
34 #include <FBase_StringConverter.h>
35 #include <FApp_Aul.h>
36 #include <FApp_AppArg.h>
37 #include "FShell_NotificationManagerImpl.h"
38 #include "FShell_NotificationRequestImpl.h"
39
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::App;
43 using namespace Tizen::Io;
44
45 extern "C" int service_create_request(bundle *data, service_h *service);
46 extern "C" int service_to_bundle(service_h service, bundle** data);
47
48 namespace
49 {
50
51 result
52 ConvertNotificationResult(int error)
53 {
54         switch (error)
55         {
56         case NOTIFICATION_ERROR_NONE:
57                 return E_SUCCESS;
58         case NOTIFICATION_ERROR_INVALID_DATA:
59                 return E_INVALID_ARG;
60         case NOTIFICATION_ERROR_NO_MEMORY:
61                 return E_OUT_OF_MEMORY;
62         case NOTIFICATION_ERROR_FROM_DB:
63                 // fall through
64         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
65                 // fall through
66         case NOTIFICATION_ERROR_NOT_EXIST_ID:
67                 return E_OPERATION_FAILED;
68         default:
69                 return E_OPERATION_FAILED;
70         }
71 }
72
73 result
74 ConvertAppManagerResult(int error)
75 {
76         switch (error)
77         {
78         case APP_MANAGER_ERROR_NONE:
79                 return E_SUCCESS;
80         case APP_MANAGER_ERROR_INVALID_PARAMETER:
81                 // fall through
82         case APP_MANAGER_ERROR_INVALID_PACKAGE:
83                 return E_INVALID_ARG;
84         case NOTIFICATION_ERROR_NO_MEMORY:
85                 return E_OUT_OF_MEMORY;
86         case APP_MANAGER_ERROR_DB_FAILED:
87                 return E_OPERATION_FAILED;
88         default:
89                 return E_OPERATION_FAILED;
90         }
91 }
92
93 } // namespace
94
95
96 namespace Tizen { namespace Shell
97 {
98
99 ///////////////////////////////////////////////////////////////////
100 // _BadgeManagerImpl
101 ///////////////////////////////////////////////////////////////////
102 void
103 BadgeChangedCallback(unsigned int action, const char *pkgname, unsigned int count, void *data)
104 {
105         Tizen::Base::Collection::LinkedListT<Tizen::Shell::IBadgeEventListener*>* pBadgeEventListenerList = static_cast<Tizen::Base::Collection::LinkedListT<Tizen::Shell::IBadgeEventListener*>*>(data);
106         SysAssert(pBadgeEventListenerList);
107
108         std::unique_ptr<IEnumeratorT<Tizen::Shell::IBadgeEventListener* > > pEnum(pBadgeEventListenerList->GetEnumeratorN());
109         SysTryReturnVoidResult(NID_SHELL, pEnum.get(), E_OUT_OF_MEMORY, "Failed to GetEnumeratorN()!");
110
111         IBadgeEventListener* pListener;
112         while (pEnum->MoveNext() == E_SUCCESS)
113         {
114                 pListener = null;
115                 pEnum->GetCurrent(pListener);
116                 if (!pListener)
117                 {
118                         SysLog(NID_SHELL, "pListener is null!");
119                         continue;
120                 }
121
122                 pListener->OnBadgeUpdated(pkgname, count);
123         }
124 }
125
126 _BadgeManagerImpl*
127 _BadgeManagerImpl::GetInstance(void)
128 {
129         static _BadgeManagerImpl* pTheInstance = null;
130         if (pTheInstance == null)
131         {
132                 pTheInstance = new (std::nothrow) _BadgeManagerImpl;
133                 SysTryReturn(NID_SHELL, pTheInstance != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
134
135                 pTheInstance->Construct();
136         }
137         return pTheInstance;
138 }
139
140 result
141 _BadgeManagerImpl::Construct(void)
142 {
143         return E_SUCCESS;
144 }
145
146 result
147 _BadgeManagerImpl::AddPrimaryBadgeEventListener(IBadgeEventListener& primaryListener)
148 {
149         if( __primaryBadgeEventListenerList.GetCount() == 0)
150         {
151                 int ret = badge_register_changed_cb(BadgeChangedCallback, &__primaryBadgeEventListenerList);
152                 SysTryReturnResult(NID_SHELL, ret == BADGE_ERROR_NONE, E_SYSTEM, "Failed to badge_register_changed_cb, (%d)", ret);
153         }
154
155         return __primaryBadgeEventListenerList.Add(&primaryListener);
156 }
157
158 result
159 _BadgeManagerImpl::RemovePrimaryBadgeEventListener(IBadgeEventListener& primaryListener)
160 {
161         __primaryBadgeEventListenerList.Remove(&primaryListener);
162         if( __primaryBadgeEventListenerList.GetCount() == 0)
163         {
164                 int ret = badge_unregister_changed_cb(BadgeChangedCallback);
165                 SysTryReturnResult(NID_SHELL, ret == BADGE_ERROR_NONE, E_SYSTEM, "Failed to badge_unregister_changed_cb, (%d)", ret);
166         }
167         return E_SUCCESS;
168 }
169
170
171 ///////////////////////////////////////////////////////////////////
172 // _NotificationManagerImpl
173 ///////////////////////////////////////////////////////////////////
174 _NotificationManagerImpl::_NotificationManagerImpl(void)
175         : __notifyPrivateId(-1)
176         , __notifyPrivateIdForOngoing(-1)
177 {
178 }
179
180 _NotificationManagerImpl::~_NotificationManagerImpl(void)
181 {
182         bool isListeningBadgeEvent = (__badgeEventListenerList.GetCount() > 0)? true : false;
183         if(isListeningBadgeEvent == true)
184         {
185                 _BadgeManagerImpl::GetInstance()->RemovePrimaryBadgeEventListener(*this);
186         }
187 }
188
189 result
190 _NotificationManagerImpl::Construct(void)
191 {
192         return E_SUCCESS;
193 }
194
195 const _NotificationManagerImpl*
196 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
197 {
198         return notiMgr.__pNotificationManagerImpl;
199 }
200
201 _NotificationManagerImpl*
202 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
203 {
204         return notiMgr.__pNotificationManagerImpl;
205 }
206
207 int
208 _NotificationManagerImpl::GetBadgeNumber(void) const
209 {
210         ClearLastResult();
211
212         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
213         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
214
215         int count = GetBadgeCount(pAppId.get());
216         SysTryReturn(NID_SHELL, count != -1, count, E_OPERATION_FAILED, "[E_OPERATION_FAILED] The operation has failed. Badge may not exist.");
217
218         return count;
219 }
220
221 result
222 _NotificationManagerImpl::NotifyMessageImpl(const NotificationRequest& notiMessage, bool isOngoing)
223 {
224         return NotifyMessage(NOTIFY_TYPE_SIMPLE, isOngoing, notiMessage);
225 }
226
227 result
228 _NotificationManagerImpl::NotifyMessageImpl(const AppId& appId, const NotificationRequest& notiMessage, bool isOngoing)
229 {
230         return NotifyMessage(NOTIFY_TYPE_APP_ID, isOngoing, notiMessage, &appId);
231 }
232
233
234 result
235 _NotificationManagerImpl::Notify(int badgeNumber)
236 {
237         SysTryReturnResult(NID_SHELL, badgeNumber >= 0 && badgeNumber <= MAX_NOTIFICATION_BADGE_NUMBER, E_INVALID_ARG,
238                                                 "badgeNumber is less than 0 or greater than MAX_NOTIFICATION_BADGE_NUMBER.");
239
240         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
241         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
242
243         result r = E_SUCCESS;
244
245         if (badgeNumber == 0)
246         {
247                 r = RemoveBadge(pAppId.get());
248                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to remove the badge.");
249         }
250         else
251         if (badgeNumber > 0)
252         {
253                 r = SetBadgeCount(pAppId.get(), badgeNumber);
254                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to set the badge.");
255         }
256
257         return r;
258 }
259
260 result
261 _NotificationManagerImpl::Notify(const String& messageText)
262 {
263         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
264                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
265
266         NotificationRequest request;
267         request.SetAlertText(messageText);
268         request.SetAppMessage(L"");
269
270         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
271 }
272
273 result
274 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber)
275 {
276         SysTryReturnResult(NID_SHELL, badgeNumber >= 0 && badgeNumber <= MAX_NOTIFICATION_BADGE_NUMBER, E_INVALID_ARG,
277                                                 "badgeNumber is less than 0 or greater than MAX_NOTIFICATION_BADGE_NUMBER.");
278         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
279                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
280
281         NotificationRequest request;
282         request.SetAlertText(messageText);
283         request.SetBadgeNumber(badgeNumber);
284         request.SetAppMessage(L"");
285
286         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
287 }
288
289 result
290 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments)
291 {
292         SysTryReturnResult(NID_SHELL, badgeNumber >= 0 && badgeNumber <= MAX_NOTIFICATION_BADGE_NUMBER, E_INVALID_ARG,
293                                                 "badgeNumber is less than 0 or greater than MAX_NOTIFICATION_BADGE_NUMBER.");
294         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
295                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
296         SysTryReturnResult(NID_SHELL, launchArguments.GetLength() > 0 && launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
297                                            "launchArguments is less than 1 or greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
298
299         NotificationRequest request;
300         request.SetAlertText(messageText);
301         request.SetBadgeNumber(badgeNumber);
302         request.SetAppMessage(launchArguments);
303
304         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
305 }
306
307 int
308 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
309 {
310         ClearLastResult();
311
312         bool b = _Aul::IsInstalled(appId);
313         SysTryReturn(NID_SHELL, b == true, -1, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] The application %ls is not installed",
314                                  appId.GetPointer());
315         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
316         int count = GetBadgeCount(pAppId.get());
317
318         SysTryReturn(NID_SHELL, count != -1, count, E_OPERATION_FAILED, "[E_OPERATION_FAILED] The operation has failed. Badge may not exist.");
319
320         return count;
321 }
322
323 result
324 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText)
325 {
326         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
327                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
328
329         NotificationRequest request;
330         request.SetAlertText(messageText);
331
332         return NotifyMessage(NOTIFY_TYPE_SIMPLE, true, request);
333 }
334
335 result
336 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments)
337 {
338         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
339                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
340         SysTryReturnResult(NID_SHELL, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
341         SysTryReturnResult(NID_SHELL, launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
342                                            "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
343
344         NotificationRequest request;
345         request.SetAlertText(messageText);
346         request.SetAppMessage(launchArguments);
347
348         return NotifyMessage(NOTIFY_TYPE_SIMPLE, true, request);
349 }
350
351 result
352 _NotificationManagerImpl::RemoveOngoingActivityNotificationByAppId(const AppId& appId)
353 {
354         return RemoveNotificationByAppId(appId, true);
355 }
356
357
358 result
359 _NotificationManagerImpl::RemoveNotificationByAppId(const AppId& appId)
360 {
361         return RemoveNotificationByAppId(appId, false);
362 }
363
364 result
365 _NotificationManagerImpl::NotifyTextMessage(const String& messageText) const
366 {
367         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
368                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
369
370         std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(messageText));
371         int res = notification_status_message_post(pMsg.get());
372
373         result r = E_SUCCESS;
374         switch (res)
375         {
376         case NOTIFICATION_ERROR_NONE:
377                 // success
378                 break;
379         case NOTIFICATION_ERROR_INVALID_DATA:
380                 r = E_INVALID_ARG;
381                 break;
382         case NOTIFICATION_ERROR_IO:
383                 r = E_OPERATION_FAILED;
384                 break;
385         default:
386                 r = E_OPERATION_FAILED;
387                 break;
388         }
389
390         SysLog(NID_SHELL, "[%s] %ls posted.", GetErrorMessage(r), messageText.GetPointer());
391         return r;
392 }
393
394 result
395 _NotificationManagerImpl::NotifyByAppControl(const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType,
396                                                                                          const Tizen::Base::Collection::IMap* pExtraData, const NotificationRequest& request)
397 {
398         result r = E_SUCCESS;
399
400         r = NotifyMessage(NOTIFY_TYPE_APP_CONTROL, false, request, null, &operationId, pUriData, pDataType, pExtraData);
401         return r;
402 }
403
404 result
405 _NotificationManagerImpl::NotifyOngoingActivityByAppControl(const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType,
406                                                                                                                         const Tizen::Base::Collection::IMap* pExtraData, const NotificationRequest& request)
407 {
408         result r = E_SUCCESS;
409
410         r = NotifyMessage(NOTIFY_TYPE_APP_CONTROL, true, request, null, &operationId, pUriData, pDataType, pExtraData);
411         return r;
412 }
413
414 //
415 // E_SUCCESS
416 // E_INVALID_ARG
417 // E_APP_NOT_INSTALLED - If AppId is invalid, it can occur.
418 // E_OPERATION_FAILED
419 // E_OUT_OF_MEMORY
420 //
421 result
422 _NotificationManagerImpl::NotifyMessage(_NotifyType notifyType, bool isOngoing, const NotificationRequest& notifyRequest, const AppId* pAppId,
423                                                                                 const Tizen::Base::String* pOperationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType, const Tizen::Base::Collection::IMap* pExtraData)
424 {
425         result r = E_SUCCESS;
426         std::unique_ptr<char[]> pAppIdChar(null);
427         const _NotificationRequestImpl* pRequestImpl = _NotificationRequestImpl::GetInstance(notifyRequest);
428         SysTryReturnResult(NID_SHELL, pRequestImpl != null, E_INVALID_ARG, "Invalid argument is used.");
429
430         // Set pAppIdChar
431         if ((notifyType == NOTIFY_TYPE_SIMPLE) || (notifyType == NOTIFY_TYPE_APP_CONTROL))
432         {
433                 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
434                 const String currentAppId = pApp->GetAppId();
435                 std::unique_ptr<char[]> pAppIdTemp(_StringConverter::CopyToCharArrayN(currentAppId));
436                 pAppIdChar = std::move(pAppIdTemp);
437                 SysLog(NID_SHELL, "App ID is %ls.", currentAppId.GetPointer());
438         }
439         else
440         if (notifyType == NOTIFY_TYPE_APP_ID)
441         {
442                 bool isAppInstalled = _Aul::IsInstalled(*pAppId);
443                 SysTryReturnResult(NID_SHELL, isAppInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed.", pAppId->GetPointer());
444                 std::unique_ptr<char[]> pAppIdTemp(_StringConverter::CopyToCharArrayN(*pAppId));
445                 pAppIdChar = std::move(pAppIdTemp);
446         }
447         else
448         {
449                 SysTryReturnResult(NID_SHELL, false, E_INVALID_ARG, "Invalid argument is used.");
450         }
451
452         const int badgeNumber = notifyRequest.GetBadgeNumber();
453         const int badgeOffset = notifyRequest.GetBadgeOffset();
454         const String& contentText = notifyRequest.GetAlertText();
455         // Allow change the badge without other properties.
456         if (badgeNumber != -1 || badgeOffset != 0)
457         {
458                 // Set - Badge
459                 if (badgeOffset != 0)
460                 {
461                         int badgeNumber = GetBadgeCount(pAppIdChar.get());
462                         if (badgeNumber <= 0)
463                         {
464                                 SetBadgeCount(pAppIdChar.get(), badgeOffset);
465                         }
466                         else
467                         {
468                                 SetBadgeCount(pAppIdChar.get(), badgeNumber + badgeOffset);
469                         }
470                 }
471                 else
472                 {
473                         if (badgeNumber == 0)
474                         {
475                                 r = RemoveBadge(pAppIdChar.get());
476                                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to remove the badge.");
477                         }
478                         else
479                         if (badgeNumber > 0)
480                         {
481                                 r = SetBadgeCount(pAppIdChar.get(), badgeNumber);
482                                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to set the badge.");
483                         }
484                 }
485                 if (!(isOngoing || !contentText.IsEmpty()))
486                 {
487                         SysLog(NID_SHELL, "No valid for Notification, just for set a badgeNumber update.");
488                         return E_SUCCESS;
489                 }
490         }
491         SysTryReturnResult(NID_SHELL, (isOngoing || !contentText.IsEmpty()), E_INVALID_ARG, "Invalid argument is used. MessageText is Empty");
492
493         const String& titleText = notifyRequest.GetTitleText();
494         const String& launchArguments = notifyRequest.GetAppMessage();
495         const String& iconPath = notifyRequest.GetIconFilePath();
496         const String& soundPath = notifyRequest.GetSoundFilePath();
497         const notification_type_e notiType = isOngoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
498         int notiPrivateId = isOngoing ? __notifyPrivateIdForOngoing : __notifyPrivateId;
499         notification_h notiHandle = null;
500         bundle* pBundle = null;
501         bool needUpdate = false;
502         bundle* service_data = null;
503         _AppArg arg;
504         service_h hSvc = null;
505         notification_ly_type_e layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
506
507         // Notification creation
508         if (notiPrivateId != -1)
509         {
510                 notiHandle = notification_load(pAppIdChar.get(), notiPrivateId);
511                 SysTryLog(NID_SHELL, notiHandle != null, "Get notiHandle(%d) from notiPrivateId(%d).", notiHandle, notiPrivateId);
512         }
513
514         if (notiHandle == null)
515         {
516                 SysLog(NID_SHELL, "Previous notification(%d) no more valid - create new notification", notiPrivateId);
517                 notiPrivateId = -1;             // reset
518                 notiHandle = notification_create(notiType);
519         }
520         else
521         {
522                 needUpdate = true;              // No need to notification_insert.
523         }
524         SysTryReturnResult(NID_SHELL, notiHandle != null, E_OPERATION_FAILED, "Notification creation or loading failed.");
525
526         // Content text(Alert text)
527         std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(contentText));
528
529         // Title text
530         std::unique_ptr<char[]> pTitleText(null);
531         if (!titleText.IsEmpty())
532         {
533                 std::unique_ptr<char[]> pTitleTextTemp(_StringConverter::CopyToCharArrayN(titleText));
534                 pTitleText = std::move(pTitleTextTemp);
535         }
536         else
537         {
538                 char* pAppName = null;
539                 r = ConvertAppManagerResult(app_manager_get_app_name(pAppIdChar.get(), &pAppName));
540                 if (pAppName)
541                 {
542                         String appName(pAppName);
543                         std::unique_ptr<char[]> pTitleTextTemp(_StringConverter::CopyToCharArrayN(appName));
544                         pTitleText = std::move(pTitleTextTemp);
545                         free(pAppName);
546                         SysLog(NID_SHELL, "App name is %ls.", appName.GetPointer());
547                 }
548                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Failed to get title from app_manager_get_app_name for default setting.", GetErrorMessage(r));
549         }
550         // Icon file path
551         std::unique_ptr<char[]> pIconPath(null);
552         if (!iconPath.IsEmpty())
553         {
554                 std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(iconPath));
555                 pIconPath = std::move(pIconPathTemp);
556         }
557         else
558         {
559                 char* pDefaultIconPath = null;
560                 r = ConvertAppManagerResult(app_manager_get_app_icon_path(pAppIdChar.get(), &pDefaultIconPath));
561                 if (pDefaultIconPath)
562                 {
563                         String iconPath(pDefaultIconPath);
564                         std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(iconPath));
565                         pIconPath = std::move(pIconPathTemp);
566                         free(pDefaultIconPath);
567                 }
568                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the icon path failed.", GetErrorMessage(r));
569         }
570         // Sound file path
571         std::unique_ptr<char[]> pSoundPath(null);
572         if (!soundPath.IsEmpty())
573         {
574                 std::unique_ptr<char[]> pSoundPathTemp(_StringConverter::CopyToCharArrayN(soundPath));
575                 pSoundPath = std::move(pSoundPathTemp);
576         }
577         // Set - AppId
578         if (notifyType == NOTIFY_TYPE_APP_ID)
579         {
580                 r = ConvertNotificationResult(notification_set_pkgname(notiHandle, pAppIdChar.get()));
581                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the package name failed.", GetErrorMessage(r));
582         }
583         // Set - title text
584         if (pTitleText.get())
585         {
586                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_TITLE, pTitleText.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
587                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the title text failed.", GetErrorMessage(r));
588         }
589         // Set - content text
590         if (pMsg.get())
591         {
592                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
593                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting content text failed.", GetErrorMessage(r));
594         }
595         // Set - icon file path
596         r = ConvertNotificationResult(notification_set_image(notiHandle, NOTIFICATION_IMAGE_TYPE_ICON, pIconPath.get()));
597         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the icon path failed.", GetErrorMessage(r));
598         // Set - sound file path
599         if (pSoundPath.get())
600         {
601                 if (isOngoing && needUpdate)
602                 {
603                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_NONE, NULL));
604                 }
605                 else
606                 {
607                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_USER_DATA, pSoundPath.get()));
608                 }
609         }
610         else
611         {
612                 if (isOngoing)
613                 {
614                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_NONE, NULL));
615                 }
616                 else
617                 {
618                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL));
619                 }
620         }
621         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the sound failed.", GetErrorMessage(r));
622
623         // Set extended - text, thumbnail and background image
624         if (!isOngoing)
625         {
626                 // Extended style set
627                 String countText = notifyRequest.GetNotificationCountText();
628                 NotificationStyle notiStyle= notifyRequest.GetNotificationStyle();
629                 if (notiStyle == NOTIFICATION_STYLE_THUMBNAIL)
630                 {
631                         layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
632                 }
633                 else
634                 if (notiStyle == NOTIFICATION_STYLE_NORMAL && !countText.IsEmpty())
635                 {
636                         layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
637                 }
638
639                 if (!countText.IsEmpty())
640                 {
641                         std::unique_ptr<char[]> text(_StringConverter::CopyToCharArrayN(countText));
642                         r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, text.get(),
643                                                                                                                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
644                 }
645
646                 String bgImageFilePath = notifyRequest.GetBackgroundImageFilePath();
647                 if (!bgImageFilePath.IsEmpty() && File::IsFileExist(bgImageFilePath))
648                 {
649                         std::unique_ptr<char[]> pBgImageFilePath(_StringConverter::CopyToCharArrayN(bgImageFilePath));
650                         r = ConvertNotificationResult(notification_set_image(notiHandle, NOTIFICATION_IMAGE_TYPE_BACKGROUND, pBgImageFilePath.get()));
651                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the background image path failed.", GetErrorMessage(r));
652                 }
653
654
655                 if (notiStyle == NOTIFICATION_STYLE_THUMBNAIL)
656                 {
657                         const IList* pThumbnailList = pRequestImpl->GetMessageThumbnailFilePathList();
658                         if (pThumbnailList)
659                         {
660                                 const static notification_image_type_e thumbnailListEnum[] = {NOTIFICATION_IMAGE_TYPE_LIST_1, NOTIFICATION_IMAGE_TYPE_LIST_2,
661                                                 NOTIFICATION_IMAGE_TYPE_LIST_3, NOTIFICATION_IMAGE_TYPE_LIST_4, NOTIFICATION_IMAGE_TYPE_LIST_5 };
662                                 int itemCount = pThumbnailList->GetCount();
663                                 const int maxCount = sizeof(thumbnailListEnum)/sizeof(thumbnailListEnum[0]);
664                                 if (itemCount > maxCount)
665                                 {
666                                         itemCount = maxCount;
667                                 }
668                                 for (int i = 0; i < itemCount; i++)
669                                 {
670                                         const String* pThumbnailPath = static_cast<const String*>(pThumbnailList->GetAt(i));
671                                         std::unique_ptr<char[]> filePath(_StringConverter::CopyToCharArrayN(*pThumbnailPath));
672                                         r = ConvertNotificationResult(notification_set_image(notiHandle, thumbnailListEnum[i], filePath.get()));
673                                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the thumbnail path failed.", GetErrorMessage(r));
674                                 }
675                         }
676                 }
677                 else
678                 {
679                         const IList* pMessageTextList = pRequestImpl->GetMessageTextList();
680                         if (pMessageTextList && pMessageTextList->GetCount())
681                         {
682                                 const String* pText1 = static_cast<const String*>(pMessageTextList->GetAt(0));
683                                 if (pText1)
684                                 {
685                                         int matchIndex;
686                                         if (E_SUCCESS == pText1->IndexOf(L'\t', 0, matchIndex))
687                                         {
688                                                 // Make two token
689                                                 String subText;
690                                                 pText1->SubString(0, matchIndex+1, subText);
691                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(subText));
692                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_1, leftText.get(),
693                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
694                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info1 text failed.", GetErrorMessage(r));
695                                                 subText.Clear();
696                                                 if (E_SUCCESS == pText1->SubString(matchIndex+1, subText))
697                                                 {
698                                                         std::unique_ptr<char[]> rightText(_StringConverter::CopyToCharArrayN(subText));
699                                                         r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, rightText.get(),
700                                                                                                                                                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
701                                                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info1 sub text failed.", GetErrorMessage(r));
702                                                 }
703                                         }
704                                         else
705                                         {
706                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(*pText1));
707                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_1, leftText.get(),
708                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
709                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info1 text failed.", GetErrorMessage(r));
710                                         }
711                                 }
712                                 const String* pText2 = static_cast<const String*>(pMessageTextList->GetAt(1));
713                                 if (pText2)
714                                 {
715                                         int matchIndex;
716                                         // 2.1: Multiple layout has single line text for 2nd information text.
717                                         if ((layout == NOTIFICATION_LY_NOTI_EVENT_MULTIPLE) && (E_SUCCESS == (pText2->IndexOf(L'\t', 0, matchIndex))))
718                                         {
719                                                 // Make two token
720                                                 String subText;
721                                                 pText2->SubString(0, matchIndex+1, subText);
722                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(subText));
723                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_2, leftText.get(),
724                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
725                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info2 text failed.", GetErrorMessage(r));
726                                                 subText.Clear();
727                                                 if (E_SUCCESS == pText2->SubString(matchIndex+1, subText))
728                                                 {
729                                                         std::unique_ptr<char[]> rightText(_StringConverter::CopyToCharArrayN(subText));
730                                                         r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_SUB_2, rightText.get(),
731                                                                                                                                                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
732                                                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info2 sub text failed.", GetErrorMessage(r));
733                                                 }
734                                         }
735                                         else
736                                         {
737                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(*pText2));
738                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_2, leftText.get(),
739                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
740                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info2 text failed.", GetErrorMessage(r));
741                                         }
742                                 }
743                         }
744                         else
745                         if (needUpdate)
746                         {       // Reset text for update case. also must be checked the previous text with get_text
747                                 char* pRetStr = null;
748                                 notification_get_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_1, &pRetStr);
749                                 if (pRetStr)
750                                 {
751                                         const static notification_text_type_e infoTextEnums[] = { NOTIFICATION_TEXT_TYPE_INFO_1, NOTIFICATION_TEXT_TYPE_INFO_SUB_1,
752                                                                                                                                                          NOTIFICATION_TEXT_TYPE_INFO_2, NOTIFICATION_TEXT_TYPE_INFO_SUB_2 };
753                                         for (unsigned int i = 0; i < sizeof(infoTextEnums)/sizeof(infoTextEnums[0]); i++)
754                                         {
755                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, infoTextEnums[i], null, NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
756                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the initial text failed.", GetErrorMessage(r));
757                                         }
758                                 }
759                         }
760                 }
761         }
762
763         // Set - service
764         int retVal = SERVICE_ERROR_NONE;
765         int previousDisplaySet = NOTIFICATION_DISPLAY_APP_ALL;
766         int newDisplaySet = 0;
767
768         r = arg.Construct(launchArguments);
769         SysTryCatch(NID_SHELL, !IsFailed(r), r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] Failed to construct _AppArg.");
770
771         pBundle = arg.GetBundle();
772
773         service_create_request(pBundle, &hSvc);
774         SysTryCatch(NID_SHELL, hSvc != null, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_create_request failed.");
775
776         if (notifyType == NOTIFY_TYPE_SIMPLE || notifyType == NOTIFY_TYPE_APP_ID)
777         {
778                 retVal = service_set_app_id(hSvc, pRequestImpl->IsAppBinding() ? pAppIdChar.get() : null);
779                 SysTryCatch(NID_SHELL, retVal == SERVICE_ERROR_NONE, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_set_app_id failed(%d).", retVal);
780         }
781         else
782         if (notifyType == NOTIFY_TYPE_APP_CONTROL)
783         {
784                 std::unique_ptr<char[]> pOperationIdChar(_StringConverter::CopyToCharArrayN(*pOperationId));
785                 retVal = service_set_operation(hSvc, pOperationIdChar.get());
786                 SysTryCatch(NID_SHELL, retVal == SERVICE_ERROR_NONE, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_set_operation failed(%d).", retVal);
787
788                 if (pUriData)
789                 {
790                         std::unique_ptr<char[]> pUri(_StringConverter::CopyToCharArrayN(*pUriData));
791                         retVal = service_set_uri(hSvc, pUri.get());
792                         SysTryLog(NID_SHELL, retVal == SERVICE_ERROR_NONE, "service_set_uri failed(%d).", retVal);
793                 }
794                 if (pDataType)
795                 {
796                         std::unique_ptr<char[]> pMime(_StringConverter::CopyToCharArrayN(*pDataType));
797                         retVal = service_set_mime(hSvc, pMime.get());
798                         SysTryLog(NID_SHELL, retVal == SERVICE_ERROR_NONE, "service_set_mime failed(%d).", retVal);
799                 }
800                 if (pExtraData)
801                 {
802                         std::unique_ptr<Tizen::Base::Collection::IMapEnumerator> pMapEnum(pExtraData->GetMapEnumeratorN());
803                         if (pMapEnum)
804                         {
805                                 while (pMapEnum->MoveNext() == E_SUCCESS)
806                                 {
807                                         String* pKey = static_cast<String* > (pMapEnum->GetKey());
808                                         String* pValue = static_cast<String* > (pMapEnum->GetValue());
809                                         if (pKey && pValue)
810                                         {
811                                                 std::unique_ptr<char[]> pKeyString(_StringConverter::CopyToCharArrayN(*pKey));
812                                                 std::unique_ptr<char[]> pValueString(_StringConverter::CopyToCharArrayN(*pValue));
813                                                 retVal = service_add_extra_data(hSvc, pKeyString.get(), pValueString.get());
814                                                 SysTryLog(NID_SHELL, retVal == SERVICE_ERROR_NONE, "service_add_extra_data failed(%d).", retVal);
815                                         }
816                                         else
817                                         {
818                                                 SysLog(NID_SHELL, "pKey or pValue is invalid.");
819                                         }
820                                 }
821                         }
822                 }
823         }
824
825         retVal = service_to_bundle(hSvc, &service_data);
826         SysTryCatch(NID_SHELL, retVal == SERVICE_ERROR_NONE, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_to_bundle failed(%d).", retVal);
827
828         r = ConvertNotificationResult(notification_set_property(notiHandle, 0));
829         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification property failed.", GetErrorMessage(r));
830
831         r = ConvertNotificationResult(notification_set_execute_option(notiHandle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data));
832         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the execute option failed.", GetErrorMessage(r));
833
834         // Set layout
835         if (isOngoing)
836         {
837                 OngoingActivityType activityType = notifyRequest.GetOngoingActivityType();
838                 if (activityType == ONGOING_ACTIVITY_TYPE_TEXT)
839                 {
840                         layout = NOTIFICATION_LY_ONGOING_EVENT;
841                 }
842                 else
843                 {
844                         layout = NOTIFICATION_LY_ONGOING_PROGRESS;
845                 }
846         }
847         r = ConvertNotificationResult(notification_set_layout(notiHandle, layout));
848         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification layout failed.", GetErrorMessage(r));
849
850         // For ongoing
851         if (isOngoing)
852         {
853                 OngoingActivityType activityType = notifyRequest.GetOngoingActivityType();
854                 int progress = notifyRequest.GetOngoingActivityProgress();
855
856                 switch (activityType)
857                 {
858                 case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
859                         if (needUpdate)
860                         {
861                                 r = ConvertNotificationResult(notification_set_size(notiHandle, 0));    // Reset for override BYTE type
862                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification size failed.", GetErrorMessage(r));
863                         }
864                         r = ConvertNotificationResult(notification_set_progress(notiHandle, progress/100.));
865                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification progress failed.", GetErrorMessage(r));
866                         break;
867
868                 case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
869                         if (needUpdate)
870                         {
871                                 r = ConvertNotificationResult(notification_set_progress(notiHandle, 0.0));      // Reset for override PERCENTAGE type
872                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification progress failed.", GetErrorMessage(r));
873                         }
874                         r = ConvertNotificationResult(notification_set_size(notiHandle, progress));
875                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification size failed.", GetErrorMessage(r));
876                         break;
877
878                 case ONGOING_ACTIVITY_TYPE_TEXT:
879                         r = ConvertNotificationResult(notification_set_progress(notiHandle, 0.0));      // Reset the progress
880                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification progress failed.", GetErrorMessage(r));
881                         r = ConvertNotificationResult(notification_set_size(notiHandle, 0));
882                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification size failed.", GetErrorMessage(r));
883                         break;
884
885                 default:
886                         r = E_OPERATION_FAILED;
887                         break;
888                 }
889
890                 r = ConvertNotificationResult(notification_get_display_applist(notiHandle, &previousDisplaySet));
891                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification area failed.", GetErrorMessage(r));
892
893                 newDisplaySet = previousDisplaySet & ~NOTIFICATION_DISPLAY_APP_TICKER;
894                 r = ConvertNotificationResult(notification_set_display_applist(notiHandle, newDisplaySet));
895                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification area failed.", GetErrorMessage(r));
896         }
897
898         // insert for new notification
899         if (!needUpdate)
900         {       // new
901                 r = ConvertNotificationResult(notification_insert(notiHandle, &notiPrivateId));
902                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting new notification failed.", GetErrorMessage(r));
903
904                 SysLog(NID_SHELL, "Getting new notiPrivateId(%d)", notiPrivateId);
905
906                 if (isOngoing)
907                 {
908                         __notifyPrivateIdForOngoing  = notiPrivateId;
909                 }
910                 else
911                 {
912                         __notifyPrivateId = notiPrivateId;
913                 }
914         }
915         else
916         {
917                 r = ConvertNotificationResult(notification_update(notiHandle));
918                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Updating the notification failed. notiPrivateId(%d)", GetErrorMessage(r), notiPrivateId);
919         }
920
921         if (isOngoing)
922         {
923                 r = ConvertNotificationResult(notification_set_display_applist(notiHandle, previousDisplaySet));
924                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification area failed.", GetErrorMessage(r));
925         }
926
927 CATCH:
928         service_destroy(hSvc);
929         notification_free(notiHandle);
930         return r;
931 }
932
933 result
934 _NotificationManagerImpl::RemoveNotification(void)
935 {
936         return RemoveNotification(false);
937 }
938
939 result
940 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
941 {
942         return RemoveNotification(true);
943 }
944
945 result
946 _NotificationManagerImpl::RemoveNotification(bool onGoing)
947 {
948         result r = E_SUCCESS;
949         notification_error_e err = NOTIFICATION_ERROR_NONE;
950         const notification_type_e notiType = onGoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
951         const int notiPrivateId = onGoing ? __notifyPrivateIdForOngoing : __notifyPrivateId;
952
953         if (notiPrivateId != -1)
954         {
955                 notification_h notiHandle = notification_load(null, notiPrivateId);
956                 if (notiHandle)
957                 {
958                         err = notification_delete(notiHandle);
959                         SysLog(NID_SHELL, "Notification deleted.");
960                 }
961                 else
962                 {
963                         SysLog(NID_SHELL, "Notification already deleted.");
964                 }
965                 err = notification_free(notiHandle);
966         }
967         else
968         {
969                 err = notification_delete_all_by_type(null, notiType);
970                 SysLog(NID_SHELL, "All [%s] notifications deleted.", onGoing ? "Ongoing" : "Normal");
971         }
972
973         SysTryReturnResult(NID_SHELL, err == NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Failed to RemoveNotification with reason (%d).", err);
974         return r;
975 }
976
977 result
978 _NotificationManagerImpl::RemoveNotificationByAppId(const Tizen::App::AppId& appId, bool onGoing)
979 {
980         result r = E_SUCCESS;
981         notification_error_e err = NOTIFICATION_ERROR_NONE;
982         bool isValidAppId = _Aul::IsInstalled(appId);
983         SysTryReturnResult(NID_SHELL, isValidAppId == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
984
985         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
986         const notification_type_e notiType = onGoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
987         const int notiPrivateId = onGoing ? __notifyPrivateIdForOngoing : __notifyPrivateId;
988
989         if (notiPrivateId != -1)
990         {
991                 notification_h notiHandle = notification_load(pAppId.get(), notiPrivateId);
992                 if (notiHandle)
993                 {
994                         err = notification_delete(notiHandle);
995                         SysLog(NID_SHELL, "Notification deleted.");
996                 }
997                 else
998                 {
999                         SysLog(NID_SHELL, "Notification already deleted.");
1000                 }
1001                 err = notification_free(notiHandle);
1002         }
1003         else
1004         {
1005                 err = notification_delete_all_by_type(pAppId.get(), notiType);
1006                 SysLog(NID_SHELL, "All [%s] notifications deleted.", onGoing ? "Ongoing" : "Normal");
1007         }
1008
1009         SysTryReturnResult(NID_SHELL, err == NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Failed to RemoveNotificationByAppId with reason (%d).", err);
1010         return r;
1011 }
1012
1013 result
1014 _NotificationManagerImpl::RemoveBadge(const char* pkgName) const
1015 {
1016         bool badgeExist = false;
1017         badge_error_e badgeError = badge_is_existing(pkgName, &badgeExist);
1018         SysTryLog(NID_SHELL, badgeError == BADGE_ERROR_NONE, "badge_is_existing failed(%d).", badgeError);
1019
1020         if (badgeExist)
1021         {
1022                 badgeError = badge_remove(pkgName);
1023                 SysTryReturnResult(NID_SHELL, badgeError == BADGE_ERROR_NONE, E_OPERATION_FAILED, "badge_remove failed(%d).", badgeError);
1024         }
1025
1026         SysLog(NID_SHELL, "Badge is removed.");
1027         return E_SUCCESS;
1028 }
1029
1030 result
1031 _NotificationManagerImpl::SetBadgeCount(const char* pkgName, int badgeCount) const
1032 {
1033         bool badgeExist;
1034         badge_error_e badgeError = badge_is_existing(pkgName, &badgeExist);
1035         SysTryLog(NID_SHELL, badgeError == BADGE_ERROR_NONE, "badge_is_existing failed(%d).", badgeError);
1036
1037         if (!badgeExist)
1038         {
1039                 badgeError = badge_create(pkgName, pkgName);
1040                 SysTryReturnResult(NID_SHELL, badgeError == BADGE_ERROR_NONE, E_OPERATION_FAILED, "badge_create failed(%d).", badgeError);
1041         }
1042
1043         badgeError = badge_set_count(pkgName, badgeCount);
1044         SysTryLog(NID_SHELL, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
1045
1046         SysLog(NID_SHELL, "Badge count is %d.", badgeCount);
1047         return E_SUCCESS;
1048 }
1049
1050 int
1051 _NotificationManagerImpl::GetBadgeCount(const char* pkgName) const
1052 {
1053         unsigned int count = 0;
1054
1055         badge_error_e badgeError = badge_get_count(pkgName, &count);
1056         if (badgeError == BADGE_ERROR_NONE)
1057         {
1058                 SysLog(NID_SHELL, "badge_get_count(%d)", count);
1059                 return count;
1060         }
1061         else
1062         {
1063                 SysLog(NID_SHELL, "badge_get_count failed(%d).", badgeError);
1064                 return -1;
1065         }
1066 }
1067
1068 result
1069 _NotificationManagerImpl::AddBadgeEventListener(IBadgeEventListener& listener)
1070 {
1071         SysTryReturnResult(NID_SHELL, !__badgeEventListenerList.Contains(&listener), E_OBJ_ALREADY_EXIST, "The listener is already added.");
1072         SysLog(NID_SHELL, "AddBadgeEventListener (%x)", &listener);
1073
1074         result r = _BadgeManagerImpl::GetInstance()->AddPrimaryBadgeEventListener(*this);
1075         SysTryReturnResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "Failed to AddPrimaryBadgeEventListener with reason (%s)", GetErrorMessage(r) );
1076
1077         return __badgeEventListenerList.Add(&listener);
1078 }
1079
1080 result
1081 _NotificationManagerImpl::RemoveBadgeEventListener(IBadgeEventListener& listener)
1082 {
1083         SysLog(NID_SHELL, "RemoveBadgeEventListener (%x)", &listener);
1084
1085         result r = __badgeEventListenerList.Remove(&listener);  // Remove() can return E_SUCCESS and E_OBJ_NOT_FOUND.
1086         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "Failed to RemoveBadgeEventListener with reason (%s)", GetErrorMessage(r) );
1087
1088         return _BadgeManagerImpl::GetInstance()->RemovePrimaryBadgeEventListener(*this);
1089 }
1090
1091 void
1092 _NotificationManagerImpl::OnBadgeUpdated(const Tizen::App::AppId& appId, int badgeNumber)
1093 {
1094         std::unique_ptr<IEnumeratorT<Tizen::Shell::IBadgeEventListener* > > pEnum(__badgeEventListenerList.GetEnumeratorN());
1095         SysTryReturnVoidResult(NID_SHELL, pEnum.get(), E_OUT_OF_MEMORY, "Failed to GetEnumeratorN()!");
1096
1097         IBadgeEventListener* pListener;
1098         while (pEnum->MoveNext() == E_SUCCESS)
1099         {
1100                 pListener = null;
1101                 pEnum->GetCurrent(pListener);
1102                 if( !pListener)
1103                 {
1104                         SysLog(NID_SHELL, "pListener is null!");
1105                         continue;
1106                 }
1107                 pListener->OnBadgeUpdated(appId, badgeNumber);
1108         }
1109 }
1110
1111 } }    // Tizen::Shell