fixed prevent issue related to Notification Manager for osp-shell
[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
31 #include <FBaseSysLog.h>
32 #include <FAppTypes.h>
33 #include <FShellNotificationManager.h>
34 #include <FShellNotificationRequest.h>
35
36 #include <FBaseInternalTypes.h>
37 #include <FBase_StringConverter.h>
38 #include "FApp_AppInfo.h"
39 #include "FAppPkg_PackageManagerImpl.h"
40 #include "FApp_AppArg.h"
41 #include "FShell_NotificationManagerImpl.h"
42 #include "FShell_NotificationManagerProxy.h"
43
44 using namespace Tizen::Base;
45 using namespace Tizen::App;
46 using namespace Tizen::App::Package;
47 using namespace Tizen::Shell;
48
49 extern "C" int service_create_request(bundle *data, service_h *service);
50 extern "C" int service_to_bundle(service_h service, bundle** data);
51
52 namespace
53 {
54
55 result
56 ConvertNotificationResult(int error)
57 {
58         switch (error)
59         {
60         case NOTIFICATION_ERROR_NONE:
61                 return E_SUCCESS;
62         case NOTIFICATION_ERROR_INVALID_DATA:
63                 return E_INVALID_ARG;
64         case NOTIFICATION_ERROR_NO_MEMORY:
65                 return E_OUT_OF_MEMORY;
66         case NOTIFICATION_ERROR_FROM_DB:
67                 return E_DATABASE;
68         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
69                 return E_OPERATION_FAILED;
70         case NOTIFICATION_ERROR_NOT_EXIST_ID:
71                 return E_OPERATION_FAILED;
72         default:
73                 return E_OPERATION_FAILED;
74         }
75 }
76
77 static int
78 GetMessageLength()
79 {
80         static int length = (_AppInfo::GetApiVersion() < _API_VERSION_2_1) ? 127 : MAX_NOTIFICATION_MESSAGE_LENGTH;
81
82         return length;
83 }
84
85 bool
86 IsPosted(ui_notification_h handle)
87 {
88         struct ui_notification_s
89         {
90                 void* raw_handle;
91                 bool ongoing;
92                 bool posted;
93                 bool removed;
94                 char *icon;
95                 struct tm *time;
96                 char *title;
97                 char *content;
98                 service_h service;
99                 char *sound;
100                 bool vibration;
101         };
102
103         if (handle == NULL)
104         {
105                 return false;
106         }
107
108         ui_notification_s* pStruct = reinterpret_cast<ui_notification_s*>(handle);
109
110         return pStruct->posted;
111 }
112
113 }
114
115 namespace Tizen { namespace Shell
116 {
117
118 _NotificationManagerImpl::_NotificationManagerImpl(void)
119 : __pNotificationManager(null)
120 {
121 }
122
123 _NotificationManagerImpl::~_NotificationManagerImpl(void)
124 {
125 }
126
127 result
128 _NotificationManagerImpl::Construct(void)
129 {
130         result r = E_SUCCESS;
131
132         __pNotificationManager = new (std::nothrow) _NotificationManagerProxy;
133         SysTryReturnResult(NID_APP, __pNotificationManager != null, E_OUT_OF_MEMORY, "__pNotificationManagerProxy creation failed.");
134
135         r = __pNotificationManager->Construct();
136         SysTryCatch(NID_APP, !IsFailed(r), , r, "__pNotificationManager->Construct() failed [%s].", GetErrorMessage(r));
137
138         return E_SUCCESS;
139
140 CATCH:
141         delete __pNotificationManager;
142         __pNotificationManager = null;
143
144         return r;
145 }
146
147 const _NotificationManagerImpl*
148 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
149 {
150         return notiMgr.__pNotificationManagerImpl;
151 }
152
153 _NotificationManagerImpl*
154 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
155 {
156         return notiMgr.__pNotificationManagerImpl;
157 }
158
159 int
160 _NotificationManagerImpl::GetBadgeNumber(void) const
161 {
162         int count = -1;
163         notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
164         return count;
165 }
166
167 result
168 _NotificationManagerImpl::OngoingImpl(const String& messageText, const String& launchArguments) const
169 {
170         return NotifyImpl(messageText, -1, launchArguments, true);
171 }
172
173 result
174 _NotificationManagerImpl::OngoingImpl(const AppId& appId, const String& messageText, const String& launchArguments) const
175 {
176
177         return NotifyImpl(appId, messageText, -1, launchArguments, true);
178 }
179
180 result
181 _NotificationManagerImpl::NotifyImpl(const String& messageText, int badgeNumber,
182                                                                          const String& launchArguments,
183                                                                          bool isOngoing) const
184 {
185         result r = E_SUCCESS;
186         char* pMsg = null;
187         notification_h core = NULL;
188         char* pkgname = NULL;
189         bundle* pKb = NULL;
190         bundle* service_data = NULL;
191         service_h svc = NULL;
192         _AppArg arg;
193
194         if (!messageText.IsEmpty())
195         {
196                 SysTryReturnResult(NID_APP,
197                                           messageText.GetLength() <= GetMessageLength(), E_INVALID_ARG,
198                                           "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
199
200                 if (isOngoing)
201                 {
202                         core = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
203                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
204                 }
205                 else
206                 {
207                         core = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
208                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
209                 }
210
211                 pMsg = _StringConverter::CopyToCharArrayN(messageText);
212                 notification_set_text(core, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
213
214                 app_get_package(&pkgname);
215                 SysTryCatch(NID_APP, pkgname != NULL, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Cannot acquire package name for current application.");
216
217                 r = arg.Construct(launchArguments);
218                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
219
220                 pKb = arg.GetBundle();
221                 service_create_request(pKb, &svc);
222                 service_set_package(svc, pkgname);
223                 
224                 if (service_to_bundle(svc, &service_data) == SERVICE_ERROR_NONE)
225                 {
226                         notification_set_property(core, 0);
227                         notification_set_execute_option(core, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
228                         SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
229                 }
230
231                 r = ConvertNotificationResult(notification_insert(core, NULL));
232                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
233         }
234
235         if (badgeNumber >= 0)
236         {
237                 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
238                 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
239         }
240
241 CATCH:
242         delete[] pMsg;
243         if (pkgname)
244         {
245                 free(pkgname);
246         }
247
248         if (core)
249         {
250                 notification_free(core);
251         }
252         service_destroy(svc);
253         return r;
254 }
255
256 result
257 _NotificationManagerImpl::NotifyImpl(const AppId& appId, const String& messageText, int badgeNumber,
258                                                                          const String& launchArguments,
259                                                                          bool isOngoing) const
260 {
261         result r = E_SUCCESS;
262         char* pMsg = null;
263         char* pIcon = NULL;
264         char* pName = NULL;
265         notification_h core = NULL;
266         char buffer[256];
267         bundle* pKb = NULL;
268         bundle* service_data = NULL;
269         service_h svc = NULL;
270         _AppArg arg;
271
272         memset(buffer, 0, 256);
273
274         bool b = _PackageManagerImpl::IsAppInstalled(appId);
275
276         SysTryReturnResult(NID_APP, b == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
277
278         if (isOngoing || !messageText.IsEmpty())
279         {
280                 SysTryReturnResult(NID_APP,
281                                           messageText.GetLength() <= GetMessageLength(), E_INVALID_ARG,
282                                           "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
283
284                 if (isOngoing)
285                 {
286                         core = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
287                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
288                 }
289                 else
290                 {
291                         core = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
292                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
293                 }
294
295                 pMsg = _StringConverter::CopyToCharArrayN(messageText);
296                 notification_set_text(core, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
297
298                 snprintf(buffer, 256, "%ls", appId.GetPointer());
299
300                 app_manager_get_app_icon_path(buffer, &pIcon);
301                 r = ConvertNotificationResult(notification_set_image(core, NOTIFICATION_IMAGE_TYPE_ICON, pIcon));
302                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
303
304                 app_manager_get_app_name(buffer, &pName);
305                 r = ConvertNotificationResult(notification_set_text(core, NOTIFICATION_TEXT_TYPE_TITLE, pName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
306                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title text failed.", GetErrorMessage(r));
307
308                 r = arg.Construct(launchArguments);
309                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
310
311                 pKb = arg.GetBundle();
312                 service_create_request(pKb, &svc);
313                 service_set_app_id(svc, buffer);
314                 
315                 if (service_to_bundle(svc, &service_data) == SERVICE_ERROR_NONE)
316                 {
317                         notification_set_property(core, 0);
318                         notification_set_execute_option(core, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
319                         SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer);
320                 }
321
322                 r = ConvertNotificationResult(notification_insert(core,NULL));
323                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
324         }
325
326         if (badgeNumber >= 0)
327         {
328                 notification_set_badge(buffer, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
329                 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
330         }
331
332 CATCH:
333         delete[] pMsg;
334
335         if (core)
336         {
337                 notification_free(core);
338         }
339
340         if (pIcon)
341         {
342                 free(pIcon);
343         }
344
345         if (pName)
346         {
347                 free(pName);
348         }
349         service_destroy(svc);
350         return r;
351 }
352
353
354 result
355 _NotificationManagerImpl::NotifyMessageImpl(const NotificationRequest& notiMessage, bool isOngoing)
356 {
357         result r = E_SUCCESS;
358         int progress = -1;
359         notification_h core = NULL;
360         char* pkgname = NULL;
361         char* pTitleText = NULL;
362         char* pIconPath = NULL;
363         char* pSoundPath = NULL;
364         bundle* pKb = NULL;
365         bundle* service_data = NULL;
366         service_h svc = NULL;
367         _AppArg arg;
368
369         const String& messageText = notiMessage.GetAlertText();
370         const String& launchArguments = notiMessage.GetAppMessage();
371         const String& titleText = notiMessage.GetTitleText();
372         const String& iconPath = notiMessage.GetIconFilePath();
373         const String& soundPath = notiMessage.GetSoundFilePath();
374         const int badgeNumber = notiMessage.GetBadgeNumber();
375         const int badgeOffset = notiMessage.GetBadgeOffset();
376
377         if (isOngoing || !messageText.IsEmpty())
378         {
379                 SysTryReturnResult(NID_APP,
380                                           messageText.GetLength() <= GetMessageLength(), E_INVALID_ARG,
381                                           "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
382
383                 if (isOngoing)
384                 {
385                         core = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
386                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
387                 }
388                 else
389                 {
390                         core = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
391                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
392                 }
393
394                 std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(messageText));
395
396                 if (pMsg)
397                 {
398                         r = ConvertNotificationResult(notification_set_text(core, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
399                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title text failed.", GetErrorMessage(r));
400                 }
401
402                 if (!titleText.IsEmpty())
403                 {
404                         pTitleText = _StringConverter::CopyToCharArrayN(titleText);
405                         r = ConvertNotificationResult(notification_set_text(core, NOTIFICATION_TEXT_TYPE_TITLE, pTitleText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
406                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set title text failed.", GetErrorMessage(r));
407                 }
408
409                 if (!iconPath.IsEmpty())
410                 {
411                         pIconPath = _StringConverter::CopyToCharArrayN(iconPath);
412                         r = ConvertNotificationResult(notification_set_image(core, NOTIFICATION_IMAGE_TYPE_ICON, pIconPath));
413                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set icon failed.", GetErrorMessage(r));
414                 }
415
416                 if (!soundPath.IsEmpty())
417                 {
418                         pSoundPath = _StringConverter::CopyToCharArrayN(soundPath);
419                         r = ConvertNotificationResult(notification_set_sound(core, NOTIFICATION_SOUND_TYPE_USER_DATA, pSoundPath));
420                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification set sound failed.", GetErrorMessage(r));
421                 }
422
423                 app_get_id(&pkgname);
424                 SysTryCatch(NID_APP, pkgname != NULL, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Cannot acquire package name for current application.");
425
426                 r = arg.Construct(launchArguments);
427                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
428
429                 pKb = arg.GetBundle();
430                 service_create_request(pKb, &svc);
431                 service_set_app_id(svc, pkgname);
432
433                 if (service_to_bundle(svc, &service_data) == SERVICE_ERROR_NONE)
434                 {
435                         notification_set_property(core, 0);
436                         notification_set_execute_option(core, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
437                         SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), pkgname);
438                 }
439
440                 if (isOngoing)
441                 {
442                         OngoingActivityType activityType = notiMessage.GetOngoingActivityType();
443                         progress = notiMessage.GetOngoingActivityProgress();
444                         switch (activityType)
445                         {
446                         case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
447                                 r = ConvertNotificationResult(notification_insert(core,NULL));
448                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
449                                 r = ConvertNotificationResult(notification_update_progress(core, NOTIFICATION_PRIV_ID_NONE, progress/100.));
450                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
451                                 break;
452                         case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
453                                 r = ConvertNotificationResult(notification_insert(core,NULL));
454                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
455                                 r = ConvertNotificationResult(notification_update_size(core, NOTIFICATION_PRIV_ID_NONE, progress));
456                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
457                                 break;
458                         case ONGOING_ACTIVITY_TYPE_TEXT:
459                                 r = ConvertNotificationResult(notification_insert(core,NULL));
460                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
461                                 break;
462                         default:
463                                 r = E_OPERATION_FAILED;
464                                 // ui_notification_set_content() is done already
465                                 break;
466                         }
467                 }
468                 else
469                 {
470                         r = ConvertNotificationResult(notification_insert(core,NULL));
471                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
472                 }
473         }
474         else
475         {
476                 SysTryReturnResult(NID_APP, 0, E_INVALID_ARG, "MessageText is Empty");
477         }
478
479         if (badgeNumber >= 0)
480         {
481                 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, badgeNumber);
482                 SysLog(NID_APP, "Badge number is set to %d.", badgeNumber);
483         }
484
485         if (badgeOffset != 0)
486         {
487                 // badge offset is exclusive to badge number
488
489                 int count = 0;
490                 notification_error_e noti_err = notification_get_badge(NULL, NOTIFICATION_GROUP_ID_NONE, &count);
491                 if (noti_err == NOTIFICATION_ERROR_NONE)
492                 {
493                         count += badgeOffset;
494                         if (count > 0)
495                         {
496                                 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, count);
497                                 SysLog(NID_APP, "Badge number is set to %d.", count);
498                         }
499                 }
500         }
501
502 CATCH:
503         delete[] pIconPath;
504         delete[] pTitleText;
505         delete[] pSoundPath;
506         if (pkgname)
507         {
508                 free(pkgname);
509         }
510         if (core)
511         {
512                 notification_free(core);
513         }
514         service_destroy(svc);
515         return r;
516 }
517
518 result
519 _NotificationManagerImpl::NotifyMessageImpl(const AppId& appId, const NotificationRequest& notiMessage, bool isOngoing)
520 {
521         return __pNotificationManager->NotifyMessage(appId, notiMessage, isOngoing);
522 }
523
524
525 result
526 _NotificationManagerImpl::Notify(int badgeNumber) const
527 {
528         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
529
530         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
531         {
532                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
533         }
534
535         String messageText = String(L"");
536         String appMessage = String(L"");
537
538         return NotifyImpl(messageText, badgeNumber, appMessage, false);
539 }
540
541 result
542 _NotificationManagerImpl::Notify(const String& messageText) const
543 {
544         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
545
546         String appMessage = String(L"");
547
548         return NotifyImpl(messageText, -1, appMessage, false);
549 }
550
551 result
552 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber) const
553 {
554         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
555         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
556
557         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
558         {
559                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
560         }
561
562         String appMessage = String(L"");
563
564         return NotifyImpl(messageText, badgeNumber, appMessage, false);
565 }
566
567 result
568 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments) const
569 {
570         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
571         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
572         SysTryReturnResult(NID_APP,
573                                           launchArguments != null && launchArguments.GetLength() > 0, E_INVALID_ARG,
574                                           "launchArguments is less than 0.");
575
576         SysTryReturnResult(NID_APP,
577                                           launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
578                                           "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
579
580         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
581         {
582                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
583         }
584
585         return NotifyImpl(messageText, badgeNumber, launchArguments, false);
586 }
587
588 int
589 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
590 {
591         bool b = _PackageManagerImpl::IsAppInstalled(appId);
592
593         SysTryReturn(NID_APP, b == true, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application %ls is not installed",
594                                 appId.GetPointer());
595
596         char buffer[256];
597         int count = -1;
598
599         memset(buffer, 0, 256);
600
601         snprintf(buffer, 256, "%ls", appId.GetPointer());
602
603         notification_get_badge(buffer, NOTIFICATION_GROUP_ID_NONE, &count);
604
605         return count;
606 }
607
608
609 result
610 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, int badgeNumber) const
611 {
612         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
613
614         String messageText = String(L"");
615         String appMessage = String(L"");
616         return NotifyImpl(appId, messageText, badgeNumber, appMessage);
617 }
618
619 result
620 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText) const
621 {
622         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
623
624         return NotifyImpl(appId, messageText, -1, String(L""));
625 }
626
627 result
628 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText, int badgeNumber) const
629 {
630         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
631         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
632
633         return NotifyImpl(appId, messageText, badgeNumber, String(L""));
634 }
635
636 result
637 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText, const String& launchArguments) const
638 {
639         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
640         SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
641         SysTryReturnResult(NID_APP,
642                                           launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
643                                           "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
644
645         return NotifyImpl(appId, messageText, -1, launchArguments);
646 }
647
648 result
649 _NotificationManagerImpl::NotifyByAppId(const AppId& appId, const String& messageText, int badgeNumber,
650                                                                                  const String& launchArguments) const
651 {
652         SysTryReturnResult(NID_APP, badgeNumber >= 0, E_INVALID_ARG, "BadgeNumber is less than 0.");
653         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
654         SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
655         SysTryReturnResult(NID_APP,
656                                           launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
657                                           "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
658
659         return NotifyImpl(appId, messageText, badgeNumber, launchArguments);
660 }
661
662 result
663 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText) const
664 {
665         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
666
667         return OngoingImpl(messageText, String(L""));
668 }
669
670 result
671 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments) const
672 {
673         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
674         SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
675         SysTryReturnResult(NID_APP,
676                                           launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
677                                           "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
678
679         return OngoingImpl(messageText, launchArguments);
680 }
681
682 result
683 _NotificationManagerImpl::NotifyOngoingActivityByAppId(const AppId& appId, const String& messageText) const
684 {
685         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
686
687         return OngoingImpl(appId, messageText, String(L""));
688 }
689
690 result
691 _NotificationManagerImpl::NotifyOngoingActivityByAppId(const AppId& appId, const String& messageText,
692                                                                                                                 const String& launchArguments) const
693 {
694         SysTryReturnResult(NID_APP, messageText.GetLength() > 0, E_INVALID_ARG, "MessageText is less than 0.");
695         SysTryReturnResult(NID_APP, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
696         SysTryReturnResult(NID_APP,
697                                           launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
698                                           "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
699
700         return OngoingImpl(appId, messageText, launchArguments);
701 }
702
703 result
704 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
705 {
706         result r = E_SUCCESS;
707
708         notification_error_e err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
709         switch (err)
710         {
711         case NOTIFICATION_ERROR_NONE:
712                 r = E_SUCCESS;
713                 break;
714
715         case NOTIFICATION_ERROR_INVALID_DATA:
716                 r = E_INVALID_ARG;
717                 break;
718
719         default:
720                 r = E_OPERATION_FAILED;
721                 break;
722         }
723
724         return r;
725 }
726
727 result
728 _NotificationManagerImpl::RemoveOngoingActivityNotificationByAppId(const AppId& appId)
729 {
730         return __pNotificationManager->RemoveNotification(appId, true);
731 }
732
733 result
734 _NotificationManagerImpl::RemoveNotification(void)
735 {
736         result r = E_SUCCESS;
737
738         notification_error_e err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
739         switch (err)
740         {
741         case NOTIFICATION_ERROR_NONE:
742                 r = E_SUCCESS;
743                 break;
744
745         case NOTIFICATION_ERROR_INVALID_DATA:
746                 r = E_INVALID_ARG;
747                 break;
748
749         default:
750                 r = E_OPERATION_FAILED;
751                 break;
752         }
753
754         return r;
755 }
756
757 result
758 _NotificationManagerImpl::RemoveNotificationByAppId(const AppId& appId)
759 {
760         return __pNotificationManager->RemoveNotification(appId,false);
761 }
762
763 };
764 };    // Tizen::Shell