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