Apply [ACR][2/28][Add] Add methods for text message notification and notifies with...
[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 placeholder for _NotificationManagerImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <appsvc/appsvc.h>
25 #include <bundle.h>
26 #include <notification/notification.h>
27 #include <appfw/app.h>
28 #include <appfw/app_manager.h>
29 #include <appfw/app_ui_notification.h>
30 #include <badge.h>
31
32 #include <FBaseSysLog.h>
33 #include <FApp.h>
34 //#include <FAppTypes.h>
35 #include <FShellNotificationManager.h>
36 #include <FShellNotificationRequest.h>
37
38 //#include <FBaseInternalTypes.h>
39 #include <FBase_StringConverter.h>
40 #include "FApp_AppInfo.h"
41 #include "FApp_Aul.h"
42 #include "FIoFile.h"
43 #include "FAppPkg_PackageManagerImpl.h"
44 #include "FApp_AppArg.h"
45 #include "FShell_NotificationManagerImpl.h"
46 #include "FShell_NotificationManagerProxy.h"
47 #include "FAppPkgPackageAppInfo.h"
48 #include "FAppPkg_PackageAppInfoImpl.h"
49
50 using namespace Tizen::Base;
51 using namespace Tizen::App;
52 using namespace Tizen::App::Package;
53 using namespace Tizen::Io;
54 using namespace Tizen::Shell;
55
56 extern "C" int service_create_request(bundle *data, service_h *service);
57 extern "C" int service_to_bundle(service_h service, bundle** data);
58
59 namespace
60 {
61
62 result
63 ConvertNotificationResult(int error)
64 {
65         switch (error)
66         {
67         case NOTIFICATION_ERROR_NONE:
68                 return E_SUCCESS;
69         case NOTIFICATION_ERROR_INVALID_DATA:
70                 return E_INVALID_ARG;
71         case NOTIFICATION_ERROR_NO_MEMORY:
72                 return E_OUT_OF_MEMORY;
73         case NOTIFICATION_ERROR_FROM_DB:
74                 return E_DATABASE;
75         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
76                 return E_OPERATION_FAILED;
77         case NOTIFICATION_ERROR_NOT_EXIST_ID:
78                 return E_OPERATION_FAILED;
79         default:
80                 return E_OPERATION_FAILED;
81         }
82 }
83
84 bool
85 IsPosted(ui_notification_h handle)
86 {
87         struct ui_notification_s
88         {
89                 void* raw_handle;
90                 bool ongoing;
91                 bool posted;
92                 bool removed;
93                 char *icon;
94                 struct tm *time;
95                 char *title;
96                 char *content;
97                 service_h service;
98                 char *sound;
99                 bool vibration;
100         };
101
102         if (handle == NULL)
103         {
104                 return false;
105         }
106
107         ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
108
109         return pStruct->posted;
110 }
111
112 } // namespace
113
114
115 namespace Tizen { namespace Shell
116 {
117
118 _NotificationManagerImpl::_NotificationManagerImpl(void)
119         : __pNotificationManagerProxy(null)
120         , __notifyPrivitId(-1)
121         , __notifyPrivitIdForOngoing(-1)
122 {
123 }
124
125 _NotificationManagerImpl::~_NotificationManagerImpl(void)
126 {
127 }
128
129 result
130 _NotificationManagerImpl::Construct(void)
131 {
132         return E_SUCCESS;
133 }
134
135 const _NotificationManagerImpl*
136 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
137 {
138         return notiMgr.__pNotificationManagerImpl;
139 }
140
141 _NotificationManagerImpl*
142 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
143 {
144         return notiMgr.__pNotificationManagerImpl;
145 }
146
147 int
148 _NotificationManagerImpl::GetBadgeNumber(void) const
149 {
150         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
151         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
152
153         int count = GetBadgeCount(pAppId.get());
154         SysTryReturn(NID_APP, count != -1, count, E_OPERATION_FAILED, "[%s] The operation has failed. Badge may not exist.",
155                                  GetErrorMessage(E_OPERATION_FAILED));
156
157         ClearLastResult();
158         return count;
159 }
160
161 result
162 _NotificationManagerImpl::NotifyMessageImpl(const NotificationRequest& notiMessage, bool isOngoing)
163 {
164         return NotifyMessage(NOTIFY_TYPE_SIMPLE, isOngoing, notiMessage);
165 }
166
167 result
168 _NotificationManagerImpl::NotifyMessageImpl(const AppId& appId, const NotificationRequest& notiMessage, bool isOngoing)
169 {
170         return NotifyMessage(NOTIFY_TYPE_APP_ID, isOngoing, notiMessage, &appId);
171         // No more partner, no need to use IPC.
172         //return __pNotificationManagerProxy->NotifyMessage(appId, notiMessage, isOngoing);
173 }
174
175
176 result
177 _NotificationManagerImpl::Notify(int badgeNumber)
178 {
179         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
180
181         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
182         {
183                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
184         }
185
186         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
187         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
188
189         if (badgeNumber == 0)
190         {
191                 RemoveBadge(pAppId.get());
192         }
193         else
194         if (badgeNumber > 0)
195         {
196                 SetBadgeCount(pAppId.get(), badgeNumber);
197         }
198
199         return E_SUCCESS;
200 }
201
202 result
203 _NotificationManagerImpl::Notify(const String& messageText)
204 {
205         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
206
207         NotificationRequest request;
208         request.SetAlertText(messageText);
209         request.SetAppMessage(L"");
210
211         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
212 }
213
214 result
215 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber)
216 {
217         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
218         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
219
220         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
221         {
222                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
223         }
224
225         NotificationRequest request;
226         request.SetAlertText(messageText);
227         request.SetBadgeNumber(badgeNumber);
228         request.SetAppMessage(L"");
229
230         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
231 }
232
233 result
234 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments)
235 {
236         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
237         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
238         SysTryReturnResult(NID_APP, launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
239                                            "launchArguments is less than 0.");
240
241         SysTryReturnResult(NID_APP, launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
242                                            "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
243
244         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
245         {
246                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
247         }
248
249         NotificationRequest request;
250         request.SetAlertText(messageText);
251         request.SetBadgeNumber(badgeNumber);
252         request.SetAppMessage(launchArguments);
253
254         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
255 }
256
257 int
258 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
259 {
260         bool b = _Aul::IsInstalled(appId);
261         SysTryReturn(NID_APP, b == true, -1, E_APP_NOT_INSTALLED, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
262                                  appId.GetPointer());
263         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
264         int count = GetBadgeCount(pAppId.get());
265
266         SysTryReturn(NID_APP, count != -1, count, E_OPERATION_FAILED, "[%s] The operation has failed. Badge may not exist.",
267                                  GetErrorMessage(E_OPERATION_FAILED));
268         ClearLastResult();
269
270         return count;
271 }
272
273 result
274 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText)
275 {
276         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
277
278         NotificationRequest request;
279         request.SetAlertText(messageText);
280
281         return NotifyMessage(NOTIFY_TYPE_SIMPLE, true, request);
282 }
283
284 result
285 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments)
286 {
287         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
288         SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
289         SysTryReturnResult(NID_APP, launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
290                                            "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
291
292         NotificationRequest request;
293         request.SetAlertText(messageText);
294         request.SetAppMessage(launchArguments);
295
296         return NotifyMessage(NOTIFY_TYPE_SIMPLE, true, request);
297 }
298
299 result
300 _NotificationManagerImpl::RemoveOngoingActivityNotificationByAppId(const AppId& appId)
301 {
302         return RemoveNotificationByAppId(appId, true);
303         // No more partner, no need to use IPC.
304         //return __pNotificationManagerProxy->RemoveNotification(appId, true);
305 }
306
307
308 result
309 _NotificationManagerImpl::RemoveNotificationByAppId(const AppId& appId)
310 {
311         return RemoveNotificationByAppId(appId, false);
312
313         // No more partner, no need to use IPC.
314         //return __pNotificationManagerProxy->RemoveNotification(appId,false);
315 }
316
317 result
318 _NotificationManagerImpl::NotifyTextMessage(const String& messageText) const
319 {
320         SysTryReturnResult(NID_APP, !messageText.IsEmpty(), E_INVALID_ARG, "MessageText is less than 0.");
321
322         std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(messageText));
323         int res = notification_status_message_post(pMsg.get());
324
325         result r = E_SUCCESS;
326         switch (res)
327         {
328         case NOTIFICATION_ERROR_NONE:
329                 // success
330                 break;
331         case NOTIFICATION_ERROR_INVALID_DATA:
332                 r = E_INVALID_ARG;
333                 break;
334         case NOTIFICATION_ERROR_IO:
335                 r = E_SYSTEM;
336                 break;
337         default:
338                 r = E_SYSTEM;
339                 break;
340         }
341
342         SysLog(NID_APP, "[%s] %ls posted.", GetErrorMessage(r), messageText.GetPointer());
343         return r;
344 }
345
346 result
347 _NotificationManagerImpl::NotifyByAppControl(const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType,
348                                                                                          const Tizen::Base::Collection::IMap* pExtraData, const NotificationRequest& request)
349 {
350         result r = E_SUCCESS;
351
352         r = NotifyMessage(NOTIFY_TYPE_APP_CONTROL, false, request, null, &operationId, pUriData, pDataType, pExtraData);
353         return r;
354 }
355
356 result
357 _NotificationManagerImpl::NotifyOngoingActivityByAppControl(const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType,
358                                                                                                                         const Tizen::Base::Collection::IMap* pExtraData, const NotificationRequest& request)
359 {
360         result r = E_SUCCESS;
361
362         r = NotifyMessage(NOTIFY_TYPE_APP_CONTROL, true, request, null, &operationId, pUriData, pDataType, pExtraData);
363         return r;
364 }
365
366
367 result
368 _NotificationManagerImpl::NotifyMessage(_NotifyType notifyType, bool isOngoing, const NotificationRequest& notifyRequest, const AppId* pAppId,
369                                                                                 const Tizen::Base::String* pOperationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType, const Tizen::Base::Collection::IMap* pExtraData)
370 {
371         result r = E_SUCCESS;
372         std::unique_ptr<char[]> pAppIdChar(null);
373
374         // Set pAppIdChar
375         if ((notifyType == NOTIFY_TYPE_SIMPLE) || (notifyType == NOTIFY_TYPE_APP_CONTROL))
376         {
377 //              // Below code not working for service appication context. So app_get_id API should be use for resolve this issue.
378 //              char* pkgname = null;
379 //              app_get_id(&pkgname);   // AppId. (Not package Id)
380 //              SysTryReturnResult(NID_APP, pkgname != NULL, E_OBJ_NOT_FOUND,"Cannot acquire package name for current application.");
381 //              const String currentAppId(pkgname);
382 //              free(pkgname);
383                 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
384                 const String currentAppId = pApp->GetAppId();
385                 std::unique_ptr<char[]> pAppIdTemp(_StringConverter::CopyToCharArrayN(currentAppId));
386                 pAppIdChar = std::move(pAppIdTemp);
387                 SysLog(NID_APP, "app_get_id: %ls", currentAppId.GetPointer());
388         }
389         else
390         if (notifyType == NOTIFY_TYPE_APP_ID)
391         {
392                 bool isAppInstalled = _Aul::IsInstalled(*pAppId);
393                 SysTryReturnResult(NID_APP, isAppInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", pAppId->GetPointer());
394                 std::unique_ptr<char[]> pAppIdTemp(_StringConverter::CopyToCharArrayN(*pAppId));
395                 pAppIdChar = std::move(pAppIdTemp);
396         }
397         else
398         {
399                 SysTryReturnResult(NID_APP, false, E_INVALID_ARG, "Invalid argument is used.");
400         }
401
402         const int badgeNumber = notifyRequest.GetBadgeNumber();
403         const int badgeOffset = notifyRequest.GetBadgeOffset();
404         const String& contentText = notifyRequest.GetAlertText();
405         // Allow change the badge without other properties.
406         if (badgeNumber != -1 || badgeOffset != -1)
407         {
408                 // Set - Badge
409                 if (badgeOffset != 0)
410                 {
411                         int badgeNumber = GetBadgeCount(pAppIdChar.get());
412                         if (badgeNumber <= 0)
413                         {
414                                 SetBadgeCount(pAppIdChar.get(), badgeOffset);
415                         }
416                         else
417                         {
418                                 SetBadgeCount(pAppIdChar.get(), badgeNumber + badgeOffset);
419                         }
420                 }
421                 else
422                 {
423                         if (badgeNumber == 0)
424                         {
425                                 RemoveBadge(pAppIdChar.get());
426                         }
427                         else
428                         if (badgeNumber > 0)
429                         {
430                                 SetBadgeCount(pAppIdChar.get(), badgeNumber);
431                         }
432                 }
433                 if (!(isOngoing || !contentText.IsEmpty()))
434                 {
435                         SysLog(NID_APP, "No valid for Notification, just for set a badgeNumber update.");
436                         return E_SUCCESS;
437                 }
438         }
439         SysTryReturnResult(NID_APP, (isOngoing || !contentText.IsEmpty()), E_INVALID_ARG, "Invalid argument is used. MessageText is Empty");
440
441         const String& titleText = notifyRequest.GetTitleText();
442         const String& launchArguments = notifyRequest.GetAppMessage();
443         const String& iconPath = notifyRequest.GetIconFilePath();
444         const String& soundPath = notifyRequest.GetSoundFilePath();
445         const notification_type_e notiType = isOngoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
446         int notiPrivateId = isOngoing ? __notifyPrivitIdForOngoing : __notifyPrivitId;
447         notification_h notiHandle = null;
448         bundle* pBundle = null;
449         bool needUpdate = false;
450         bundle* service_data = null;
451         _AppArg arg;
452         service_h hSvc = null;
453         notification_ly_type_e layout= NOTIFICATION_LY_NOTI_EVENT_SINGLE;
454
455         // Notification creation
456         if (notiPrivateId != -1)
457         {
458                 notiHandle = notification_load(pAppIdChar.get(), notiPrivateId);
459                 SysTryLog(NID_APP, notiHandle != null, "Get notiHandle(%d) from notiPrivateId(%d).", notiHandle, notiPrivateId);
460         }
461
462         if (notiHandle == null)
463         {
464                 SysLog(NID_APP, "Previous notification(%d) no more valid - create new notification", notiPrivateId);
465                 notiPrivateId = -1;             // reset
466                 notiHandle = notification_create(notiType);
467         }
468         SysTryReturnResult(NID_APP, notiHandle != null , E_SYSTEM, "A system error has been occurred. Notification creation/load failed ");
469
470         // Content text(Alert text)
471         std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(contentText));
472
473         // Title text
474         std::unique_ptr<char[]> pTitleText(null);
475         if (!titleText.IsEmpty())
476         {
477                 std::unique_ptr<char[]> pTitleTextTemp(_StringConverter::CopyToCharArrayN(titleText));
478                 pTitleText = std::move(pTitleTextTemp);
479         }
480         else
481         {
482                 char* pAppName = null;
483                 app_manager_get_app_name(pAppIdChar.get(), &pAppName);
484                 if (pAppName)
485                 {
486                         String appName(pAppName);
487                         std::unique_ptr<char[]> pTitleTextTemp(_StringConverter::CopyToCharArrayN(appName));
488                         pTitleText = std::move(pTitleTextTemp);
489                         free(pAppName);
490                         //SysLog(NID_APP, "Application Id is %ls.", appName.GetPointer());
491                 }
492                 else
493                 {
494                         SysTryLog(NID_APP, pTitleText, "Failed to get title from app_manager_get_app_name for default setting.");
495                 }
496         }
497         // Icon file path
498         std::unique_ptr<char[]> pIconPath(null);
499         if (!iconPath.IsEmpty())
500         {
501                 std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(iconPath));
502                 pIconPath = std::move(pIconPathTemp);
503         }
504         else
505         {
506                 std::unique_ptr<PackageAppInfo> pPackageAppInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(pAppIdChar.get()));
507                 const String& appIconPath(_PackageAppInfoImpl::GetInstance(pPackageAppInfo.get())->GetAppNotificationIconPath());
508
509                 if (!appIconPath.IsEmpty() && File::IsFileExist(appIconPath))
510                 {
511                         std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(appIconPath));
512                         pIconPath = std::move(pIconPathTemp);
513                 }
514                 else
515                 {
516                         char* pDefaultIconPath = null;
517                         app_manager_get_app_icon_path(pAppIdChar.get(), &pDefaultIconPath);
518                         if (pDefaultIconPath)
519                         {
520                                 String iconPath(pDefaultIconPath);
521                                 std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(iconPath));
522                                 pIconPath = std::move(pIconPathTemp);
523                                 free(pDefaultIconPath);
524                         }
525                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path  failed.", GetErrorMessage(r));
526                 }
527         }
528         // Sound file path
529         std::unique_ptr<char[]> pSoundPath(null);
530         if (!soundPath.IsEmpty())
531         {
532                 std::unique_ptr<char[]> pSoundPathTemp(_StringConverter::CopyToCharArrayN(soundPath));
533                 pSoundPath = std::move(pSoundPathTemp);
534         }
535         // Set - AppId
536         if (notifyType == NOTIFY_TYPE_APP_ID)
537         {
538                 r = ConvertNotificationResult(notification_set_pkgname(notiHandle, pAppIdChar.get()));
539                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set Package name failed.", GetErrorMessage(r));
540         }
541         // Set - title text
542         if (pTitleText.get())
543         {
544                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_TITLE, pTitleText.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
545                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set Title Text failed.", GetErrorMessage(r));
546         }
547         // Set - content text
548         if (pMsg.get())
549         {
550                 r = notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
551                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set Title Text failed.", GetErrorMessage(r));
552         }
553         // Set - icon file path
554         r = ConvertNotificationResult(notification_set_image(notiHandle, NOTIFICATION_IMAGE_TYPE_ICON, pIconPath.get()));
555         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path  failed.", GetErrorMessage(r));
556         // Set - sound file path
557         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_USER_DATA, pSoundPath.get()));
558         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set sound  failed.", GetErrorMessage(r));
559
560
561         // Set - service
562         r = arg.Construct(launchArguments);
563         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
564
565         pBundle = arg.GetBundle();
566
567         service_create_request(pBundle, &hSvc);
568         SysTryCatch(NID_APP, hSvc != null, r = E_SYSTEM, r, "[%s] A system error has been occurred. service_create_request failed.", GetErrorMessage(E_SYSTEM));
569
570         if (notifyType == NOTIFY_TYPE_SIMPLE || notifyType == NOTIFY_TYPE_APP_ID)
571         {
572                 service_set_app_id(hSvc, pAppIdChar.get());
573         }
574         else
575         if (notifyType == NOTIFY_TYPE_APP_CONTROL)
576         {
577
578                 std::unique_ptr<char[]> pOperationIdChar(_StringConverter::CopyToCharArrayN(*pOperationId));
579                 service_set_operation(hSvc, pOperationIdChar.get());
580                 if (pUriData)
581                 {
582                         std::unique_ptr<char[]> pUri(_StringConverter::CopyToCharArrayN(*pUriData));
583                         service_set_uri(hSvc, pUri.get());
584                 }
585                 if (pDataType)
586                 {
587                         std::unique_ptr<char[]> pMime(_StringConverter::CopyToCharArrayN(*pDataType));
588                         service_set_mime(hSvc, pMime.get());
589                 }
590                 if (pExtraData)
591                 {
592                         std::unique_ptr<Tizen::Base::Collection::IMapEnumerator> pMapEnum(pExtraData->GetMapEnumeratorN());
593                         if (pMapEnum)
594                         {
595                                 while (pMapEnum->MoveNext() == E_SUCCESS)
596                                 {
597                                         String* pKey = static_cast<String* > (pMapEnum->GetKey());
598                                         String* pValue = static_cast<String* > (pMapEnum->GetValue());
599                                         if (pKey && pValue)
600                                         {
601                                                 std::unique_ptr<char[]> pKeyString(_StringConverter::CopyToCharArrayN(*pKey));
602                                                 std::unique_ptr<char[]> pValueString(_StringConverter::CopyToCharArrayN(*pValue));
603                                                 service_add_extra_data(hSvc, pKeyString.get(), pValueString.get());
604                                         }
605                                         else
606                                         {
607                                                 SysLog(NID_APP, "pKey or pValue is ivalid.");
608                                         }
609                                 }
610                         }
611                 }
612         }
613
614         if (service_to_bundle(hSvc, &service_data) == SERVICE_ERROR_NONE)
615         {
616                 notification_set_property(notiHandle, 0);
617                 notification_set_execute_option(notiHandle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
618         }
619         else
620         {
621                 SysLog(NID_APP, "service_to_bundle failed");
622         }
623
624         // insert for new notification / update for exist one.
625         if (notiPrivateId == -1)
626         {       // new
627                 r = ConvertNotificationResult(notification_insert(notiHandle, &notiPrivateId));
628                 SysLog(NID_APP, "Insert notification and get new notiPrivateId(%d)", notiPrivateId);
629                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
630                 if (isOngoing)
631                 {
632                         __notifyPrivitIdForOngoing  = notiPrivateId;
633                 }
634                 else
635                 {
636                         __notifyPrivitId = notiPrivateId;
637                 }
638         }
639         else
640         {
641                 needUpdate = true;
642         }
643
644         // Set layout
645         if (isOngoing)
646         {
647                 OngoingActivityType activityType = notifyRequest.GetOngoingActivityType();
648                 if (activityType == ONGOING_ACTIVITY_TYPE_TEXT)
649                 {
650                         layout = NOTIFICATION_LY_ONGOING_EVENT;
651                 }
652                 else
653                 {
654                         layout = NOTIFICATION_LY_ONGOING_PROGRESS;
655                 }
656         }
657         else
658         {
659                 // TODO: check and set from noti type.
660         }
661         notification_set_layout(notiHandle, layout);
662
663         // For ongoing
664         if (isOngoing)
665         {
666                 OngoingActivityType activityType = notifyRequest.GetOngoingActivityType();
667                 int progress = notifyRequest.GetOngoingActivityProgress();
668
669                 switch (activityType)
670                 {
671                 case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
672                         r = ConvertNotificationResult(notification_update_progress(notiHandle, notiPrivateId, progress/100.));
673                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
674                         r = ConvertNotificationResult(notification_update_content(notiHandle, notiPrivateId, pMsg.get()));
675                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
676                         break;
677
678                 case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
679                         r = ConvertNotificationResult(notification_update_size(notiHandle, notiPrivateId, progress));
680                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
681                         r = ConvertNotificationResult(notification_update_content(notiHandle, notiPrivateId, pMsg.get()));
682                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
683                         break;
684
685                 case ONGOING_ACTIVITY_TYPE_TEXT:
686                         r = ConvertNotificationResult(notification_update_content(notiHandle, notiPrivateId, pMsg.get()));
687                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
688                         break;
689
690                 default:
691                         r = E_OPERATION_FAILED;
692                         break;
693                 }
694         }
695
696         // notification_update call on Non ongoing or Text type ongoing only.
697         //if (needUpdate && !(isOngoing && (ONGOING_ACTIVITY_TYPE_TEXT != notifyRequest.GetOngoingActivityType())))
698         if (needUpdate && !isOngoing)
699         {
700                 notification_update(notiHandle);
701                 SysLog(NID_APP, "Notification update from notiPrivateId(%d)", notiPrivateId);
702         }
703
704
705 CATCH:
706         service_destroy(hSvc);
707         notification_free(notiHandle);
708         return r;
709 }
710
711 result
712 _NotificationManagerImpl::RemoveNotification(void)
713 {
714         return RemoveNotification(false);
715 //      result r = E_SUCCESS;
716 //      notification_error_e err = NOTIFICATION_ERROR_NONE;
717 //
718 //      if (__notifyPrivitId != -1)
719 //      {
720 //              notification_h notiHandle = notification_load(null, __notifyPrivitId);
721 //              if (notiHandle)
722 //              {
723 //                      notification_delete(notiHandle);
724 //              }
725 //              notification_free(notiHandle);
726 //      }
727 //      else
728 //      {
729 //              err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
730 //      }
731 //
732 //      switch (err)
733 //      {
734 //      case NOTIFICATION_ERROR_NONE:
735 //              r = E_SUCCESS;
736 //              break;
737 //
738 //      case NOTIFICATION_ERROR_INVALID_DATA:
739 //              r = E_INVALID_ARG;
740 //              break;
741 //
742 //      default:
743 //              r = E_OPERATION_FAILED;
744 //              break;
745 //      }
746 //
747 //      return r;
748 }
749
750 result
751 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
752 {
753         return RemoveNotification(true);
754 //      result r = E_SUCCESS;
755 //      notification_error_e err = NOTIFICATION_ERROR_NONE;
756 //
757 //      if (__notifyPrivitIdForOngoing != -1)
758 //      {
759 //              notification_h notiHandle = notification_load(null, __notifyPrivitIdForOngoing);
760 //              if (notiHandle)
761 //              {
762 //                      notification_delete(notiHandle);
763 //              }
764 //              notification_free(notiHandle);
765 //      }
766 //      else
767 //      {
768 //              err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
769 //      }
770 //
771 //      switch (err)
772 //      {
773 //      case NOTIFICATION_ERROR_NONE:
774 //              r = E_SUCCESS;
775 //              break;
776 //
777 //      case NOTIFICATION_ERROR_INVALID_DATA:
778 //              r = E_INVALID_ARG;
779 //              break;
780 //
781 //      default:
782 //              r = E_OPERATION_FAILED;
783 //              break;
784 //      }
785 //
786 //      return r;
787 }
788
789 result
790 _NotificationManagerImpl::RemoveNotification(bool onGoing)
791 {
792         result r = E_SUCCESS;
793         notification_error_e err = NOTIFICATION_ERROR_NONE;
794         const notification_type_e notiType = onGoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
795         const int notiPrivateId = onGoing ? __notifyPrivitIdForOngoing : __notifyPrivitId;
796
797         if (notiPrivateId != -1)
798         {
799                 notification_h notiHandle = notification_load(null, notiPrivateId);
800                 if (notiHandle)
801                 {
802                         err = notification_delete(notiHandle);
803                         SysLog(NID_APP, "Notification deleted.");
804                 }
805                 else
806                 {
807                         SysLog(NID_APP, "Notification already deleted.");
808                 }
809                 notification_free(notiHandle);
810         }
811         else
812         {
813                 err = notification_delete_all_by_type(null, notiType);
814                 SysLog(NID_APP, "All [%s] notification deleted.", onGoing ? "Ongoing" : "Normal");
815         }
816
817         switch (err)
818         {
819         case NOTIFICATION_ERROR_NONE:
820                 r = E_SUCCESS;
821                 break;
822
823         case NOTIFICATION_ERROR_INVALID_DATA:
824                 r = E_INVALID_ARG;
825                 break;
826
827         default:
828                 r = E_SYSTEM;
829                 break;
830         }
831
832         return r;
833 }
834
835 result
836 _NotificationManagerImpl::RemoveNotificationByAppId(const Tizen::App::AppId& appId, bool onGoing)
837 {
838         result r = E_SUCCESS;
839         notification_error_e err = NOTIFICATION_ERROR_NONE;
840         bool isValidAppId = _Aul::IsInstalled(appId);
841         SysTryReturnResult(NID_APP, isValidAppId == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
842
843         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
844         const notification_type_e notiType = onGoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
845         const int notiPrivateId = onGoing ? __notifyPrivitIdForOngoing : __notifyPrivitId;
846
847         if (notiPrivateId != -1)
848         {
849                 notification_h notiHandle = notification_load(pAppId.get(), notiPrivateId);
850                 if (notiHandle)
851                 {
852                         err = notification_delete(notiHandle);
853                         SysLog(NID_APP, "Notification deleted.");
854                 }
855                 else
856                 {
857                         SysLog(NID_APP, "Notification already deleted.");
858                 }
859                 notification_free(notiHandle);
860         }
861         else
862         {
863                 err = notification_delete_all_by_type(pAppId.get(), notiType);
864                 SysLog(NID_APP, "All [%s] notification deleted.", onGoing ? "Ongoing" : "Normal");
865         }
866
867         switch (err)
868         {
869         case NOTIFICATION_ERROR_NONE:
870                 r = E_SUCCESS;
871                 break;
872
873         case NOTIFICATION_ERROR_INVALID_DATA:
874                 r = E_INVALID_ARG;
875                 break;
876
877         default:
878                 r = E_SYSTEM;
879                 break;
880         }
881
882         return r;
883 }
884
885 void
886 _NotificationManagerImpl::RemoveBadge(const char* pkgName) const
887 {
888         bool badgeExist = false;
889         badge_error_e badgeError = badge_is_existing(pkgName, &badgeExist);
890         SysLog(NID_APP, "badge_is_existing: exitst= %d, error= %d.", badgeExist, badgeError);   //#####
891         if (badgeExist)
892         {
893                 badgeError = badge_remove(pkgName);
894                 SysLog(NID_APP, "badge_remove: error= %d.", badgeError);        //#####
895         }
896         SysLog(NID_APP, "Badge removed.");
897 }
898
899 void
900 _NotificationManagerImpl::SetBadgeCount(const char* pkgName, int badgeCount) const
901 {
902         bool badgeExist;
903         badge_error_e badgeError = badge_is_existing(pkgName, &badgeExist);
904         SysLog(NID_APP, "badge_is_existing: error= %d.", badgeError);   //#####
905         if (!badgeExist)
906         {
907                 badgeError = badge_create(pkgName, pkgName);
908                 SysLog(NID_APP, "badge_create: error= %d.", badgeError);        //#####
909         }
910
911         badge_set_count(pkgName, badgeCount);
912         SysLog(NID_APP, "badge_set_count: badgeNumber= %d.", badgeCount);
913 }
914
915 int
916 _NotificationManagerImpl::GetBadgeCount(const char* pkgName) const
917 {
918         unsigned int count = 0;
919
920         badge_error_e badgeError = badge_get_count(pkgName, &count);
921         if (badgeError == BADGE_ERROR_NONE)
922         {
923                 SysLog(NID_APP, "badge_get_count: ret= %d.", count);
924                 return count;
925         }
926         else
927         {
928                 SysLog(NID_APP, "badge_get_count: error= %d.", badgeError);
929                 return -1;
930         }
931 }
932
933 } }    // Tizen::Shell