Merge branch 'tizen_3.0' into tizen_4.0
[platform/core/api/webapi-plugins.git] / src / notification / common_notification.cc
1 /*
2  * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "notification/common_notification.h"
18
19 #include <app_control_internal.h>
20 #include <notification_internal.h>
21
22 #include "common/converter.h"
23 #include "common/filesystem/filesystem_provider.h"
24 #include "common/logger.h"
25 #include "common/scope_exit.h"
26
27 namespace extension {
28 namespace notification {
29
30 using namespace common;
31
32 const InformationEnumMap CommonNotification::info_map_ = {{0, NOTIFICATION_TEXT_TYPE_INFO_1},
33                                                           {1, NOTIFICATION_TEXT_TYPE_INFO_2},
34                                                           {2, NOTIFICATION_TEXT_TYPE_INFO_3}};
35
36 const InformationEnumMap CommonNotification::info_sub_map_ = {
37     {0, NOTIFICATION_TEXT_TYPE_INFO_SUB_1},
38     {1, NOTIFICATION_TEXT_TYPE_INFO_SUB_2},
39     {2, NOTIFICATION_TEXT_TYPE_INFO_SUB_3}};
40
41 const ImageEnumMap CommonNotification::thumbnails_map_ = {{0, NOTIFICATION_IMAGE_TYPE_LIST_1},
42                                                           {1, NOTIFICATION_IMAGE_TYPE_LIST_2},
43                                                           {2, NOTIFICATION_IMAGE_TYPE_LIST_3},
44                                                           {3, NOTIFICATION_IMAGE_TYPE_LIST_4}};
45
46 const InformationEnumMap CommonNotification::buttons_texts_map_ = {
47     {0, NOTIFICATION_TEXT_TYPE_BUTTON_1}, {1, NOTIFICATION_TEXT_TYPE_BUTTON_2},
48     {2, NOTIFICATION_TEXT_TYPE_BUTTON_3}, {3, NOTIFICATION_TEXT_TYPE_BUTTON_4},
49     {4, NOTIFICATION_TEXT_TYPE_BUTTON_5}, {5, NOTIFICATION_TEXT_TYPE_BUTTON_6}};
50
51 const ImageEnumMap CommonNotification::buttons_icon_paths_map_ = {
52     {0, NOTIFICATION_IMAGE_TYPE_BUTTON_1}, {1, NOTIFICATION_IMAGE_TYPE_BUTTON_2},
53     {2, NOTIFICATION_IMAGE_TYPE_BUTTON_3}, {3, NOTIFICATION_IMAGE_TYPE_BUTTON_4},
54     {4, NOTIFICATION_IMAGE_TYPE_BUTTON_5}, {5, NOTIFICATION_IMAGE_TYPE_BUTTON_6}};
55
56 CommonNotification::CommonNotification() {
57 }
58
59 CommonNotification::~CommonNotification() {
60 }
61
62 bool CommonNotification::IsColorFormatNumeric(const std::string& color) {
63   LoggerD("Enter");
64   std::string hexCode = "0123456789abcdef";
65   if (7 != color.length() || '#' != color[0]) {
66     return false;
67   }
68
69   for (size_t i = 1; i < color.length(); i++) {
70     if (hexCode.find(color[i]) == std::string::npos) {
71       return false;
72     }
73   }
74
75   return true;
76 }
77
78 PlatformResult CommonNotification::SetLayout(notification_h noti_handle,
79                                              const std::string& noti_type) {
80   LoggerD("Enter");
81   notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE;
82
83   if ("SIMPLE" == noti_type) {
84     long number;
85     PlatformResult status = GetNumber(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &number);
86     if (status.IsError()) {
87       LoggerE("Failed: GetNumber");
88       return status;
89     }
90     if (number > 0)
91       noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
92     else
93       noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
94   } else if ("THUMBNAIL" == noti_type) {
95     noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
96   }
97   if ("ONGOING" == noti_type) {
98     noti_layout = NOTIFICATION_LY_ONGOING_EVENT;
99   } else if ("PROGRESS" == noti_type) {
100     noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS;
101   }
102   int ret = notification_set_layout(noti_handle, noti_layout);
103   if (NOTIFICATION_ERROR_NONE != ret) {
104     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification layout error",
105                               ("Set notification layout error: %d", ret));
106   }
107
108   return PlatformResult(ErrorCode::NO_ERROR);
109 }
110
111 static bool ServiceExtraDataCb(app_control_h service, const char* key, void* user_data) {
112   LoggerD("Enter");
113   if (nullptr == user_data || nullptr == key) {
114     LoggerE("User data or key not exist");
115     return true;
116   }
117
118   picojson::array* control_data = static_cast<picojson::array*>(user_data);
119
120   int length = 0;
121   char** value = nullptr;
122   SCOPE_EXIT {
123     free(value);
124   };
125
126   int ret = app_control_get_extra_data_array(service, key, &value, &length);
127   if (APP_CONTROL_ERROR_NONE != ret) {
128     LoggerE("Get app control extra data error: %d", ret);
129     return true;
130   }
131
132   if (!value || !length) {
133     LoggerE("Get app control extra data value error");
134     return true;
135   }
136
137   picojson::array values = picojson::array();
138   for (int index = 0; index < length; ++index) {
139     values.push_back(picojson::value(value[index]));
140   }
141
142   picojson::object data_control_elem = picojson::object();
143   data_control_elem["key"] = picojson::value(key);
144   data_control_elem["value"] = picojson::value(values);
145
146   control_data->push_back(picojson::value(data_control_elem));
147
148   return true;
149 }
150
151 PlatformResult CommonNotification::Create(notification_type_e noti_type,
152                                           notification_h* noti_handle) {
153   LoggerD("Enter");
154   *noti_handle = notification_create(noti_type);
155   if (!*noti_handle) {
156     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot make new notification object");
157   }
158
159   if (NOTIFICATION_TYPE_ONGOING == noti_type) {
160     int ret =
161         notification_set_display_applist(*noti_handle, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY |
162                                                            NOTIFICATION_DISPLAY_APP_INDICATOR);
163     if (NOTIFICATION_ERROR_NONE != ret) {
164       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot set notification display applist",
165                                 ("Cannot make new notification object: %d", ret));
166     }
167   }
168
169   return PlatformResult(ErrorCode::NO_ERROR);
170 }
171
172 PlatformResult CommonNotification::StatusTypeFromPlatform(notification_type_e noti_type,
173                                                           notification_ly_type_e noti_layout,
174                                                           std::string* type) {
175   LoggerD("Enter");
176   if (NOTIFICATION_TYPE_NOTI == noti_type) {
177     if (NOTIFICATION_LY_NOTI_EVENT_SINGLE == noti_layout ||
178         NOTIFICATION_LY_NOTI_EVENT_MULTIPLE == noti_layout) {
179       *type = "SIMPLE";
180     } else if (NOTIFICATION_LY_NOTI_THUMBNAIL == noti_layout) {
181       *type = "THUMBNAIL";
182     }
183   } else if (NOTIFICATION_TYPE_ONGOING == noti_type) {
184     if (NOTIFICATION_LY_ONGOING_EVENT == noti_layout) {
185       *type = "ONGOING";
186     } else if (NOTIFICATION_LY_ONGOING_PROGRESS == noti_layout) {
187       *type = "PROGRESS";
188     }
189   } else {
190     return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Notification type not found");
191   }
192
193   return PlatformResult(ErrorCode::NO_ERROR);
194 }
195
196 PlatformResult CommonNotification::StatusTypeToPlatform(const std::string& type,
197                                                         notification_type_e* noti_type) {
198   LoggerD("Enter");
199   if ("SIMPLE" == type || "THUMBNAIL" == type) {
200     *noti_type = NOTIFICATION_TYPE_NOTI;
201   } else if ("ONGOING" == type || "PROGRESS" == type) {
202     *noti_type = NOTIFICATION_TYPE_ONGOING;
203   } else {
204     return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, "Invalide notification type",
205                               ("Invalide noti type: %s", type.c_str()));
206   }
207
208   return PlatformResult(ErrorCode::NO_ERROR);
209 }
210
211 PlatformResult CommonNotification::GetImage(notification_h noti_handle,
212                                             notification_image_type_e image_type,
213                                             std::string* image_path) {
214   LoggerD("Enter");
215   char* path = nullptr;
216
217   *image_path = "";
218
219   if (NOTIFICATION_ERROR_NONE != notification_get_image(noti_handle, image_type, &path)) {
220     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification image error",
221                               ("Get notification image error, image_type: %d", image_type));
222   }
223   if (path) {
224     *image_path = path;
225   }
226
227   return PlatformResult(ErrorCode::NO_ERROR);
228 }
229
230 PlatformResult CommonNotification::SetImage(notification_h noti_handle,
231                                             notification_image_type_e image_type,
232                                             const std::string& image_path) {
233   LoggerD("Enter");
234   int ret = notification_set_image(noti_handle, image_type, image_path.c_str());
235   if (NOTIFICATION_ERROR_NONE != ret) {
236     return LogAndCreateResult(
237         ErrorCode::UNKNOWN_ERR, "Set notification image error",
238         ("Set notification image error, image_type: %d, error: %d", image_type, ret));
239   }
240
241   return PlatformResult(ErrorCode::NO_ERROR);
242 }
243
244 PlatformResult CommonNotification::GetText(notification_h noti_handle,
245                                            notification_text_type_e text_type,
246                                            std::string* noti_text) {
247   LoggerD("Enter");
248   char* text = nullptr;
249
250   *noti_text = "";
251
252   if (NOTIFICATION_ERROR_NONE != notification_get_text(noti_handle, text_type, &text)) {
253     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification text error",
254                               ("Get notification text error, text_type: %d", text_type));
255   }
256
257   if (text) *noti_text = text;
258
259   return PlatformResult(ErrorCode::NO_ERROR);
260 }
261
262 PlatformResult CommonNotification::SetText(notification_h noti_handle,
263                                            notification_text_type_e text_type,
264                                            const std::string& noti_text) {
265   LoggerD("Enter");
266   int ret = notification_set_text(noti_handle, text_type, noti_text.c_str(), nullptr,
267                                   NOTIFICATION_VARIABLE_TYPE_NONE);
268   if (NOTIFICATION_ERROR_NONE != ret) {
269     return LogAndCreateResult(
270         ErrorCode::UNKNOWN_ERR, "Set notification text error",
271         ("Set notification text error, text_type: %d, error: %d", text_type, ret));
272   }
273
274   return PlatformResult(ErrorCode::NO_ERROR);
275 }
276
277 PlatformResult CommonNotification::GetNumber(notification_h noti_handle,
278                                              notification_text_type_e text_type, long* number) {
279   LoggerD("Enter");
280   std::string text;
281   PlatformResult status = GetText(noti_handle, text_type, &text);
282   CHECK_ERROR(status);
283
284   if (text.length())
285     *number = std::stol(text);
286   else
287     *number = -1;
288
289   return PlatformResult(ErrorCode::NO_ERROR);
290 }
291
292 PlatformResult CommonNotification::GetLedColor(notification_h noti_handle, std::string* led_color) {
293   LoggerD("Enter");
294   unsigned int color = 0;
295   notification_led_op_e type = NOTIFICATION_LED_OP_ON;
296
297   if (NOTIFICATION_ERROR_NONE != notification_get_led(noti_handle, &type, (int*)&color)) {
298     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
299                               "Get notification led displaying option error");
300   }
301
302   *led_color = "";
303   std::stringstream stream;
304
305   if (NOTIFICATION_LED_OP_OFF != type) {
306     color = 0x00FFFFFF & color;
307     stream << std::hex << color;
308     *led_color = "#" + stream.str();
309
310     while (led_color->length() < 7) {
311       led_color->insert(1, "0");
312     }
313
314     std::transform(led_color->begin(), led_color->end(), led_color->begin(), ::tolower);
315   }
316
317   LoggerD("color:%s", (*led_color).c_str());
318
319   return PlatformResult(ErrorCode::NO_ERROR);
320 }
321
322 PlatformResult CommonNotification::GetLedPeriod(notification_h noti_handle,
323                                                 unsigned long* on_period,
324                                                 unsigned long* off_period) {
325   LoggerD("Enter");
326   int on_time = 0;
327   int off_time = 0;
328
329   if (NOTIFICATION_ERROR_NONE !=
330       notification_get_led_time_period(noti_handle, &on_time, &off_time)) {
331     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification led on/off period error");
332   }
333
334   if (on_period) *on_period = on_time;
335   if (off_period) *off_period = off_time;
336
337   return PlatformResult(ErrorCode::NO_ERROR);
338 }
339
340 PlatformResult CommonNotification::GetSoundPath(notification_h noti_handle,
341                                                 std::string* sound_path) {
342   LoggerD("Enter");
343   *sound_path = "";
344
345   const char* path = nullptr;
346   notification_sound_type_e type = NOTIFICATION_SOUND_TYPE_NONE;
347
348   if (NOTIFICATION_ERROR_NONE != notification_get_sound(noti_handle, &type, &path)) {
349     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification sound error");
350   }
351
352   LoggerD("Sound type = %d", type);
353
354   if (path && (NOTIFICATION_SOUND_TYPE_USER_DATA == type)) {
355     *sound_path = path;
356   }
357
358   LoggerD("Sound path = %s", sound_path->c_str());
359
360   return PlatformResult(ErrorCode::NO_ERROR);
361 }
362
363 PlatformResult CommonNotification::SetSoundPath(notification_h noti_handle,
364                                                 const std::string& sound_path) {
365   LoggerD("Enter");
366   int ret =
367       notification_set_sound(noti_handle, NOTIFICATION_SOUND_TYPE_USER_DATA, sound_path.c_str());
368   if (NOTIFICATION_ERROR_NONE != ret) {
369     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification sound error",
370                               ("Set notification sound error: %d", ret));
371   }
372
373   LoggerD("Sound path = %s", sound_path.c_str());
374
375   return PlatformResult(ErrorCode::NO_ERROR);
376 }
377
378 PlatformResult CommonNotification::GetVibration(notification_h noti_handle, bool* vibration) {
379   LoggerD("Enter");
380   notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE;
381
382   if (NOTIFICATION_ERROR_NONE != notification_get_vibration(noti_handle, &vib_type, nullptr)) {
383     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification vibration error");
384   }
385
386   if (NOTIFICATION_VIBRATION_TYPE_DEFAULT == vib_type ||
387       NOTIFICATION_VIBRATION_TYPE_USER_DATA == vib_type) {
388     *vibration = true;
389   } else {
390     *vibration = false;
391   }
392
393   return PlatformResult(ErrorCode::NO_ERROR);
394 }
395
396 PlatformResult CommonNotification::GetApplicationControl(app_control_h app_handle,
397                                                          picojson::object* out_ptr) {
398   LoggerD("Enter");
399   picojson::object& out = *out_ptr;
400
401   char* operation = nullptr;
402   char* uri = nullptr;
403   char* mime = nullptr;
404   char* category = nullptr;
405   SCOPE_EXIT {
406     free(operation);
407     free(uri);
408     free(mime);
409     free(category);
410   };
411
412   int ret = app_control_get_operation(app_handle, &operation);
413   if (APP_CONTROL_ERROR_NONE != ret) {
414     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get application control operation error",
415                               ("Get application control operation error: %d", ret));
416   }
417   if (operation) {
418     out["operation"] = picojson::value(operation);
419     LoggerD("operation = %s", operation);
420   }
421
422   if (APP_CONTROL_ERROR_NONE != app_control_get_uri(app_handle, &uri)) {
423     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get application control uri error");
424   }
425   if (uri) {
426     out["uri"] = picojson::value(uri);
427     LoggerD("uri = %s", uri);
428   }
429
430   if (APP_CONTROL_ERROR_NONE != app_control_get_mime(app_handle, &mime)) {
431     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get application control mime error");
432   }
433   if (mime) {
434     out["mime"] = picojson::value(mime);
435     LoggerD("mime = %s", mime);
436   }
437
438   if (APP_CONTROL_ERROR_NONE != app_control_get_category(app_handle, &category)) {
439     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get application control category error");
440   }
441   if (category) {
442     out["category"] = picojson::value(category);
443     LoggerD("category = %s", category);
444   }
445
446   picojson::array app_control_data = picojson::array();
447   if (APP_CONTROL_ERROR_NONE !=
448       app_control_foreach_extra_data(app_handle, ServiceExtraDataCb, (void*)&app_control_data)) {
449     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get application control data error");
450   }
451   out["data"] = picojson::value(app_control_data);
452
453   return PlatformResult(ErrorCode::NO_ERROR);
454 }
455
456 PlatformResult CommonNotification::SetApplicationControl(app_control_h app_handle,
457                                                          const picojson::object& app_ctrl) {
458   LoggerD("Enter");
459   picojson::value val(app_ctrl);
460   const std::string& operation = FromJson<std::string>(app_ctrl, "operation");
461
462   int ret;
463   if (operation.length()) {
464     ret = app_control_set_operation(app_handle, operation.c_str());
465   } else {
466     ret = app_control_set_operation(app_handle, APP_CONTROL_OPERATION_DEFAULT);
467   }
468   if (APP_CONTROL_ERROR_NONE != ret) {
469     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set application control operation error",
470                               ("Set application control operation error: %d", ret));
471   }
472
473   if (val.contains("uri") && !IsNull(app_ctrl, "uri")) {
474     const std::string& uri = FromJson<std::string>(app_ctrl, "uri");
475     ret = app_control_set_uri(app_handle, uri.c_str());
476     if (APP_CONTROL_ERROR_NONE != ret) {
477       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set application control uri error",
478                                 ("Set application control uri error: %d", ret));
479     }
480   }
481
482   if (val.contains("mime") && !IsNull(app_ctrl, "mime")) {
483     const std::string& mime = FromJson<std::string>(app_ctrl, "mime");
484     ret = app_control_set_mime(app_handle, mime.c_str());
485     if (APP_CONTROL_ERROR_NONE != ret) {
486       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set application control mime error",
487                                 ("Set application control mime error: %d", ret));
488     }
489   }
490
491   if (val.contains("category") && !IsNull(app_ctrl, "category")) {
492     const std::string& category = FromJson<std::string>(app_ctrl, "category");
493     ret = app_control_set_category(app_handle, category.c_str());
494     if (APP_CONTROL_ERROR_NONE != ret) {
495       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set application control category error",
496                                 ("Set application control category error: %d", ret));
497     }
498   }
499
500   if (!picojson::value(app_ctrl).contains("data") || IsNull(app_ctrl, "data")) {
501     return PlatformResult(ErrorCode::NO_ERROR);
502   }
503
504   auto& items = FromJson<picojson::array>(app_ctrl, "data");
505
506   int idx = 0;
507
508   for (auto item : items) {
509     const picojson::object& obj = JsonCast<picojson::object>(item);
510     const std::string key = FromJson<std::string>(obj, "key");
511     const picojson::array values = FromJson<picojson::array>(obj, "value");
512     const char** arrayValue = (const char**)calloc(sizeof(char*), values.size());
513     SCOPE_EXIT {
514       free(arrayValue);
515     };
516     idx = 0;
517     for (auto& item : values) {
518       arrayValue[idx] = JsonCast<std::string>(item).c_str();
519       ++idx;
520     }
521     ret = app_control_add_extra_data_array(app_handle, key.c_str(), arrayValue, values.size());
522     if (APP_CONTROL_ERROR_NONE != ret) {
523       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set application control extra data error",
524                                 ("Set application control extra data error: %d", ret));
525     }
526   }
527
528   return PlatformResult(ErrorCode::NO_ERROR);
529 }
530
531 PlatformResult CommonNotification::GetApplicationId(app_control_h app_handle, std::string* app_id) {
532   LoggerD("Enter");
533   char* app_id_str = nullptr;
534   SCOPE_EXIT {
535     free(app_id_str);
536   };
537
538   *app_id = "";
539
540   if (APP_CONTROL_ERROR_NONE != app_control_get_app_id(app_handle, &app_id_str)) {
541     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get applicaiton ID failed");
542   }
543
544   if (nullptr != app_id_str) {
545     *app_id = app_id_str;
546   }
547
548   LoggerD("Get appId = %s", /*(*app_id).c_str()*/ app_id_str);
549
550   return PlatformResult(ErrorCode::NO_ERROR);
551 }
552
553 PlatformResult CommonNotification::SetApplicationId(app_control_h app_handle,
554                                                     const std::string& app_id) {
555   LoggerD("Enter");
556   int ret = app_control_set_app_id(app_handle, app_id.c_str());
557   if (APP_CONTROL_ERROR_NONE != ret) {
558     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set applicaiton ID error",
559                               ("Set applicaiton ID error: %d", ret));
560   }
561
562   LoggerD("Set appId = %s", app_id.c_str());
563
564   return PlatformResult(ErrorCode::NO_ERROR);
565 }
566
567 PlatformResult CommonNotification::GetProgressValue(notification_h noti_handle,
568                                                     const std::string& progress_type,
569                                                     double* progress_value) {
570   LoggerD("Enter");
571   double tmp_progress_value = 0.0;
572
573   if (kProgressTypeByte == progress_type) {
574     if (NOTIFICATION_ERROR_NONE != notification_get_size(noti_handle, &tmp_progress_value)) {
575       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification size error");
576     }
577   } else if (kProgressTypePercentage == progress_type) {
578     if (NOTIFICATION_ERROR_NONE != notification_get_progress(noti_handle, &tmp_progress_value)) {
579       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification progress error");
580     }
581     // native api uses range 0-1, but webapi expects 0-100, so we need to multiply result with 100
582     tmp_progress_value *= 100;
583   } else {
584     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown notification progress type",
585                               ("Unknown notification progress type: %s ", progress_type.c_str()));
586   }
587
588   LoggerD("Progress %s = %lf", progress_type.c_str(), tmp_progress_value);
589
590   *progress_value = tmp_progress_value;
591   return PlatformResult(ErrorCode::NO_ERROR);
592 }
593
594 PlatformResult CommonNotification::SetProgressValue(notification_h noti_handle,
595                                                     const std::string& progress_type,
596                                                     double progress_value, bool is_update) {
597   LoggerD("Enter");
598   int ret;
599
600   if (kProgressTypeByte == progress_type) {
601     ret = notification_set_size(noti_handle, progress_value);
602
603     if (is_update) {
604       ret = notification_update_size(noti_handle, NOTIFICATION_PRIV_ID_NONE, progress_value);
605     }
606   } else if (kProgressTypePercentage == progress_type) {
607     // native api uses range 0-1, but webapi expects 0-100, so we need to divide by 100
608     ret = notification_set_progress(noti_handle, progress_value / 100);
609
610     if (is_update) {
611       ret = notification_update_progress(noti_handle, NOTIFICATION_PRIV_ID_NONE, progress_value);
612     }
613   } else {
614     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown notification progress type",
615                               ("Unknown notification progress type: %s ", progress_type.c_str()));
616   }
617
618   if (NOTIFICATION_ERROR_NONE != ret) {
619     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification progress/size error",
620                               ("Set notification progress/size error: %d", ret));
621   }
622
623   return PlatformResult(ErrorCode::NO_ERROR);
624 }
625
626 PlatformResult CommonNotification::GetPostedTime(notification_h noti_handle, time_t* posted_time) {
627   LoggerD("Enter");
628   *posted_time = 0;
629
630   if (NOTIFICATION_ERROR_NONE != notification_get_insert_time(noti_handle, posted_time)) {
631     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get notification posted time error");
632   }
633
634   return PlatformResult(ErrorCode::NO_ERROR);
635 }
636
637 PlatformResult CommonNotification::GetNotiHandle(int id, notification_h* noti_handle) {
638   LoggerD("Enter");
639   *noti_handle = notification_load(nullptr, id);
640   if (nullptr == *noti_handle) {
641     return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Not found or removed notification id");
642   }
643
644   return PlatformResult(ErrorCode::NO_ERROR);
645 }
646
647 PlatformResult CommonNotification::GetAppControl(notification_h noti_handle,
648                                                  app_control_h* app_control) {
649   LoggerD("Enter");
650   int ret = notification_get_launch_option(noti_handle, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL,
651                                            static_cast<void*>(app_control));
652   if (NOTIFICATION_ERROR_NONE != ret) {
653     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Notification get launch option error",
654                               ("Notification get launch option error: %d", ret));
655   }
656
657   return PlatformResult(ErrorCode::NO_ERROR);
658 }
659
660 PlatformResult CommonNotification::CreateAppControl(app_control_h* app_control) {
661   LoggerD("Enter");
662   int ret = app_control_create(app_control);
663   if (APP_CONTROL_ERROR_NONE != ret) {
664     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Application create error",
665                               ("Application create error: %d", ret));
666   }
667
668   return PlatformResult(ErrorCode::NO_ERROR);
669 }
670
671 PlatformResult CommonNotification::SetAppControl(notification_h noti_handle,
672                                                  app_control_h app_control) {
673   LoggerD("Enter");
674   int ret = notification_set_launch_option(noti_handle, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL,
675                                            static_cast<void*>(app_control));
676   if (APP_CONTROL_ERROR_NONE != ret) {
677     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Notification set launch option error",
678                               ("Notification set launch option error: %d", ret));
679   }
680
681   return PlatformResult(ErrorCode::NO_ERROR);
682 }
683
684 PlatformResult CommonNotification::UpdateNotificationAfterPost(notification_h noti_handle, int id,
685                                                                picojson::object* out_ptr) {
686   time_t posted_time;
687   PlatformResult status = GetPostedTime(noti_handle, &posted_time);
688   CHECK_ERROR(status);
689
690   picojson::object& out = *out_ptr;
691   out["postedTime"] = picojson::value(static_cast<double>(posted_time) * 1000.0);
692   out["id"] = picojson::value(std::to_string(id));
693   out["type"] = picojson::value("STATUS");
694
695   return PlatformResult(ErrorCode::NO_ERROR);
696 }
697
698 PlatformResult CommonNotification::PostNotification(const picojson::object& args, bool is_update,
699                                                     picojson::object* out_ptr,
700                                                     GetHandleFromJsonFun getHandle) {
701   LoggerD("Enter");
702   notification_h noti_handle = nullptr;
703   int ret = NOTIFICATION_ERROR_NONE;
704   int id = NOTIFICATION_PRIV_ID_NONE;
705
706   SCOPE_EXIT {
707     notification_free(noti_handle);
708   };
709
710   const auto& noti_val_it = args.find("notification");
711   if (args.end() == noti_val_it) {
712     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot post notification");
713   }
714   PlatformResult status = getHandle(noti_val_it->second, is_update, &noti_handle);
715   CHECK_ERROR(status);
716
717   if (is_update) {
718     ret = notification_update(noti_handle);
719     if (NOTIFICATION_ERROR_NONE != ret) {
720       return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Update notification error",
721                                 ("Update notification error: %d", ret));
722     }
723     return PlatformResult(ErrorCode::NO_ERROR);
724   } else {
725     ret = notification_insert(noti_handle, &id);
726     if (NOTIFICATION_ERROR_NONE != ret) {
727       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot insert notification",
728                                 ("Cannot insert notification: %d", ret));
729     }
730   }
731
732   return UpdateNotificationAfterPost(noti_handle, id, out_ptr);
733 }
734
735 PlatformResult CommonNotification::AddCommonMembersToJson(int id, notification_h noti_handle,
736                                                           picojson::object* out_ptr) {
737   LoggerD("Enter");
738   picojson::object& out = *out_ptr;
739
740   out["id"] = picojson::value(std::to_string(id));
741   out["type"] = picojson::value("STATUS");
742
743   time_t posted_time = 0;
744   PlatformResult ret = GetPostedTime(noti_handle, &posted_time);
745   CHECK_ERROR(ret);
746   out["postedTime"] = picojson::value(static_cast<double>(posted_time) * 1000.0);
747
748   std::string value_str;
749   ret = GetText(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, &value_str);
750   CHECK_ERROR(ret);
751   out["title"] = picojson::value(value_str);
752
753   ret = GetText(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, &value_str);
754   CHECK_ERROR(ret);
755   if (value_str.length()) {
756     out["content"] = picojson::value(value_str);
757   }
758
759   return PlatformResult(ErrorCode::NO_ERROR);
760 }
761
762 PlatformResult CommonNotification::AddTypeToJson(notification_h noti_handle, const std::string& key,
763                                                  picojson::object* out_ptr,
764                                                  std::string* noti_type_str) {
765   LoggerD("Enter");
766   picojson::object& out = *out_ptr;
767   // Notification type
768   notification_type_e noti_type = NOTIFICATION_TYPE_NONE;
769   int ret = notification_get_type(noti_handle, &noti_type);
770   if (NOTIFICATION_ERROR_NONE != ret) {
771     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Notification get type error",
772                               ("Notification get type error: %d", ret));
773   }
774
775   notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE;
776   ret = notification_get_layout(noti_handle, &noti_layout);
777   if (NOTIFICATION_ERROR_NONE != ret) {
778     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Notification get layout error",
779                               ("Notification get layout error: %d", ret));
780   }
781
782   PlatformResult status = StatusTypeFromPlatform(noti_type, noti_layout, noti_type_str);
783   CHECK_ERROR(status);
784   out[key] = picojson::value(*noti_type_str);
785
786   return PlatformResult(ErrorCode::NO_ERROR);
787 }
788
789 PlatformResult CommonNotification::AddProgressTypeAndValueToJson(notification_h noti_handle,
790                                                                  const std::string& noti_type_str,
791                                                                  picojson::object* out_ptr) {
792   LoggerD("Enter");
793
794   picojson::object& out = *out_ptr;
795   std::string progress_type;
796   // native code does not support progress_type value, we are using NOTIFICATION_IMAGE_TYPE_LIST_5
797   // to store this field on native level
798   PlatformResult status = GetImage(noti_handle, NOTIFICATION_IMAGE_TYPE_LIST_5, &progress_type);
799   CHECK_ERROR(status)
800
801   // notification service daemon doesn't set progress type - NOTIFICATION_IMAGE_TYPE_LIST_5 is used
802   // as workaround, so use default if notification type is different from "PROGRESS"
803   if ("PROGRESS" != noti_type_str) {
804     progress_type = progress_type == kProgressTypeByte ? progress_type : kProgressTypePercentage;
805   }
806   out["progressType"] = picojson::value(progress_type);
807
808   double progress_value;
809   status = GetProgressValue(noti_handle, progress_type, &progress_value);
810   CHECK_ERROR(status);
811   out["progressValue"] = picojson::value(progress_value);
812
813   return PlatformResult(ErrorCode::NO_ERROR);
814 }
815
816 PlatformResult CommonNotification::AddEventsNumberToJson(notification_h noti_handle,
817                                                          const std::string& key,
818                                                          picojson::object* out_ptr) {
819   LoggerD("Enter");
820   picojson::object& out = *out_ptr;
821
822   long number = 0;
823   PlatformResult status = GetNumber(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &number);
824   CHECK_ERROR(status);
825   if (number >= 0) {
826     out[key] = picojson::value(static_cast<double>(number));
827   }
828   return PlatformResult(ErrorCode::NO_ERROR);
829 }
830
831 PlatformResult CommonNotification::AddDetailInfosToJson(notification_h noti_handle,
832                                                         picojson::object* out_ptr) {
833   LoggerD("Enter");
834   picojson::object& out = *out_ptr;
835
836   picojson::value result_json = picojson::value(picojson::array());
837   picojson::array& detail_info_array = result_json.get<picojson::array>();
838
839   if (info_map_.size() != info_sub_map_.size()) {
840     return LogAndCreateResult(ErrorCode::VALIDATION_ERR,
841                               "Different notification information types element size");
842   }
843
844   picojson::value detail_info = picojson::value(picojson::object());
845   picojson::object& detail_info_obj = detail_info.get<picojson::object>();
846
847   std::string text;
848   size_t info_map_size = info_map_.size();
849   for (size_t idx = 0; idx < info_map_size; ++idx) {
850     PlatformResult status = GetText(noti_handle, info_map_.at(idx), &text);
851     CHECK_ERROR(status);
852
853     if (text.length()) {
854       detail_info_obj["mainText"] = picojson::value(text);
855
856       status = AddTextToJson(noti_handle, info_sub_map_.at(idx), "subText", &detail_info_obj);
857       CHECK_ERROR(status);
858       detail_info_array.push_back(detail_info);
859     }
860   }
861   out["detailInfo"] = result_json;
862
863   return PlatformResult(ErrorCode::NO_ERROR);
864 }
865
866 PlatformResult CommonNotification::AddPathToJson(notification_h noti_handle,
867                                                  notification_image_type_e type,
868                                                  const std::string& key,
869                                                  picojson::object* out_ptr) {
870   picojson::object& out = *out_ptr;
871
872   std::string value_str;
873   PlatformResult status = GetImage(noti_handle, type, &value_str);
874   CHECK_ERROR(status);
875   if (value_str.length()) {
876     out[key] = picojson::value(FilesystemProvider::Create().GetVirtualPath(value_str));
877   }
878   return PlatformResult(ErrorCode::NO_ERROR);
879 }
880
881 PlatformResult CommonNotification::AddTextToJson(notification_h noti_handle,
882                                                  notification_text_type_e type,
883                                                  const std::string& key,
884                                                  picojson::object* out_ptr) {
885   picojson::object& out = *out_ptr;
886
887   std::string value_str;
888   PlatformResult ret = GetText(noti_handle, type, &value_str);
889   CHECK_ERROR(ret);
890   if (value_str.length()) {
891     out[key] = picojson::value(value_str);
892   }
893   return PlatformResult(ErrorCode::NO_ERROR);
894 }
895
896 PlatformResult CommonNotification::AddLedOnOffPeriodToJson(notification_h noti_handle,
897                                                            picojson::object* out_ptr) {
898   LoggerD("Enter");
899   picojson::object& out = *out_ptr;
900
901   unsigned long on_period = 0;
902   unsigned long off_period = 0;
903   PlatformResult status = GetLedPeriod(noti_handle, &on_period, &off_period);
904   CHECK_ERROR(status);
905
906   out["ledOnPeriod"] = picojson::value(static_cast<double>(on_period));
907   out["ledOffPeriod"] = picojson::value(static_cast<double>(off_period));
908   return PlatformResult(ErrorCode::NO_ERROR);
909 }
910
911 PlatformResult CommonNotification::AddPathsArrayToJson(notification_h noti_handle, ImageEnumMap map,
912                                                        const std::string& key,
913                                                        picojson::object* out_ptr) {
914   LoggerD("Enter");
915   picojson::object& out = *out_ptr;
916
917   picojson::value result_json = picojson::value(picojson::array());
918   picojson::array& result_array = result_json.get<picojson::array>();
919
920   std::string path;
921   size_t map_size = map.size();
922   for (size_t idx = 0; idx < map_size; ++idx) {
923     PlatformResult status = GetImage(noti_handle, map.at(idx), &path);
924     CHECK_ERROR(status);
925
926     if (path.length()) {
927       result_array.push_back(picojson::value(FilesystemProvider::Create().GetVirtualPath(path)));
928     }
929   }
930
931   out[key] = result_json;
932
933   return PlatformResult(ErrorCode::NO_ERROR);
934 }
935
936 PlatformResult CommonNotification::AddTextsArrayToJson(notification_h noti_handle,
937                                                        InformationEnumMap map,
938                                                        const std::string& key,
939                                                        picojson::object* out_ptr) {
940   LoggerD("Enter");
941   picojson::object& out = *out_ptr;
942
943   picojson::value result_json = picojson::value(picojson::array());
944   picojson::array& result_array = result_json.get<picojson::array>();
945
946   std::string text;
947   size_t map_size = map.size();
948   for (size_t idx = 0; idx < map_size; ++idx) {
949     PlatformResult status = GetText(noti_handle, map.at(idx), &text);
950
951     CHECK_ERROR(status);
952     if (text.length()) {
953       result_array.push_back(picojson::value(text));
954     }
955   }
956
957   out[key] = result_json;
958
959   return PlatformResult(ErrorCode::NO_ERROR);
960 }
961
962 PlatformResult CommonNotification::AddLedColorToJson(notification_h noti_handle,
963                                                      picojson::object* out_ptr) {
964   LoggerD("Enter");
965   picojson::object& out = *out_ptr;
966
967   std::string color;
968   PlatformResult status = GetLedColor(noti_handle, &color);
969   CHECK_ERROR(status);
970   if (color.length()) {
971     out["ledColor"] = picojson::value(color);
972   }
973
974   return PlatformResult(ErrorCode::NO_ERROR);
975 }
976
977 PlatformResult CommonNotification::AddSoundPathToJson(notification_h noti_handle,
978                                                       picojson::object* out_ptr) {
979   LoggerD("Enter");
980   picojson::object& out = *out_ptr;
981
982   std::string sound_path;
983   PlatformResult status = GetSoundPath(noti_handle, &sound_path);
984   CHECK_ERROR(status);
985   if (sound_path.length()) {
986     out["soundPath"] = picojson::value(FilesystemProvider::Create().GetVirtualPath(sound_path));
987   }
988
989   return PlatformResult(ErrorCode::NO_ERROR);
990 }
991
992 PlatformResult CommonNotification::AddVibrationToJson(notification_h noti_handle,
993                                                       picojson::object* out_ptr) {
994   LoggerD("Enter");
995   picojson::object& out = *out_ptr;
996
997   bool vibration;
998   PlatformResult status = GetVibration(noti_handle, &vibration);
999   CHECK_ERROR(status);
1000   out["vibration"] = picojson::value(vibration);
1001
1002   return PlatformResult(ErrorCode::NO_ERROR);
1003 }
1004
1005 PlatformResult CommonNotification::AddAppControlInfoToJson(notification_h noti_handle,
1006                                                            app_control_h app_handle,
1007                                                            picojson::object* out_ptr) {
1008   LoggerD("Enter");
1009   picojson::object& out = *out_ptr;
1010
1011   if (app_handle) {
1012     picojson::object app_control = picojson::object();
1013     PlatformResult status = GetApplicationControl(app_handle, &app_control);
1014     CHECK_ERROR(status);
1015     if (app_control.size()) {
1016       out["appControl"] = picojson::value(app_control);
1017     }
1018
1019     std::string app_id;
1020     status = GetApplicationId(app_handle, &app_id);
1021     CHECK_ERROR(status);
1022     if (app_id.length()) {
1023       out["appId"] = picojson::value(app_id);
1024     }
1025   }
1026   return PlatformResult(ErrorCode::NO_ERROR);
1027 }
1028
1029 PlatformResult CommonNotification::InitNotiFromJson(const picojson::object& noti_obj,
1030                                                     const std::string& type_key, bool is_update,
1031                                                     notification_h* noti_handle) {
1032   LoggerD("Enter");
1033   notification_h tmp_noti = nullptr;
1034
1035   const std::string& status_type = FromJson<std::string>(noti_obj, type_key.c_str());
1036
1037   notification_type_e noti_type = NOTIFICATION_TYPE_NONE;
1038   PlatformResult status = StatusTypeToPlatform(status_type, &noti_type);
1039   CHECK_ERROR(status);
1040
1041   if (is_update) {
1042     int id = std::stoi(FromJson<std::string>(noti_obj, "id"));
1043
1044     status = GetNotiHandle(id, &tmp_noti);
1045     CHECK_ERROR(status);
1046   } else {
1047     status = Create(noti_type, &tmp_noti);
1048     CHECK_ERROR(status);
1049   }
1050   std::unique_ptr<std::remove_pointer<notification_h>::type, int (*)(notification_h)> tmp_noti_ptr(
1051       tmp_noti, &notification_free);  // automatically release the memory
1052
1053   status = SetLayout(tmp_noti, status_type);
1054   CHECK_ERROR(status);
1055
1056   *noti_handle = tmp_noti_ptr.release();
1057   return PlatformResult(ErrorCode::NO_ERROR);
1058 }
1059
1060 PlatformResult CommonNotification::SetCommonMembersFromJson(const picojson::value& noti_value,
1061                                                             notification_h noti_handle) {
1062   LoggerD("Enter");
1063   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1064   PlatformResult status =
1065       SetText(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, FromJson<std::string>(noti_obj, "title"));
1066   CHECK_ERROR(status);
1067
1068   SetTextFromJson(noti_value, NOTIFICATION_TEXT_TYPE_CONTENT, "content", noti_handle);
1069   CHECK_ERROR(status);
1070   return PlatformResult(ErrorCode::NO_ERROR);
1071 }
1072
1073 PlatformResult CommonNotification::SetPathFromJson(const picojson::value& noti_value,
1074                                                    notification_image_type_e type,
1075                                                    const std::string& key,
1076                                                    notification_h noti_handle) {
1077   LoggerD("Enter");
1078   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1079   if (noti_value.contains(key) && !IsNull(noti_obj, key.c_str())) {
1080     const std::string& value_str = FromJson<std::string>(noti_obj, key.c_str());
1081     std::string real_path = FilesystemProvider::Create().GetRealPath(value_str);
1082
1083     PlatformResult status = SetImage(noti_handle, type, real_path);
1084     CHECK_ERROR(status);
1085   }
1086   return PlatformResult(ErrorCode::NO_ERROR);
1087 }
1088
1089 PlatformResult CommonNotification::SetLedColorFromJson(const picojson::value& noti_value,
1090                                                        notification_h noti_handle) {
1091   LoggerD("Enter");
1092   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1093   if (noti_value.contains("ledColor") && !IsNull(noti_obj, "ledColor")) {
1094     std::string color_str = FromJson<std::string>(noti_obj, "ledColor");
1095     std::transform(color_str.begin(), color_str.end(), color_str.begin(), ::tolower);
1096
1097     if (!IsColorFormatNumeric(color_str)) {
1098       return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Led color is not numeric value",
1099                                 ("Led color is not numeric value: %s", color_str.c_str()));
1100     }
1101
1102     std::stringstream stream;
1103     unsigned int color = 0;
1104     notification_led_op_e type = NOTIFICATION_LED_OP_ON;
1105     std::string color_code = color_str.substr(1, color_str.length()).insert(0, "ff");
1106
1107     stream << std::hex << color_code;
1108     stream >> color;
1109
1110     if (0 != color)
1111       type = NOTIFICATION_LED_OP_ON_CUSTOM_COLOR;
1112     else
1113       type = NOTIFICATION_LED_OP_OFF;
1114
1115     int ret = notification_set_led(noti_handle, type, static_cast<int>(color));
1116     if (NOTIFICATION_ERROR_NONE != ret) {
1117       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification led color eror",
1118                                 ("Set notification led color eror: %d", ret));
1119     }
1120   }
1121
1122   return PlatformResult(ErrorCode::NO_ERROR);
1123 }
1124
1125 PlatformResult CommonNotification::SetLedOnPeriodFromJson(const picojson::value& noti_value,
1126                                                           notification_h noti_handle) {
1127   LoggerD("Enter");
1128   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1129   unsigned long on_period = static_cast<unsigned long>(FromJson<double>(noti_obj, "ledOnPeriod"));
1130
1131   unsigned long off_period = 0;
1132   PlatformResult status = GetLedPeriod(noti_handle, nullptr, &off_period);
1133   CHECK_ERROR(status);
1134
1135   int ret = notification_set_led_time_period(noti_handle, on_period, off_period);
1136   if (NOTIFICATION_ERROR_NONE != ret) {
1137     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification led on period error",
1138                               ("Set notification led on period error: %d", ret));
1139   }
1140
1141   return PlatformResult(ErrorCode::NO_ERROR);
1142 }
1143
1144 PlatformResult CommonNotification::SetLedOffPeriodFromJson(const picojson::value& noti_value,
1145                                                            notification_h noti_handle) {
1146   LoggerD("Enter");
1147   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1148   unsigned long off_period = static_cast<unsigned long>(FromJson<double>(noti_obj, "ledOffPeriod"));
1149   unsigned long on_period = 0;
1150   PlatformResult status = GetLedPeriod(noti_handle, &on_period, nullptr);
1151   CHECK_ERROR(status);
1152
1153   int ret = notification_set_led_time_period(noti_handle, on_period, off_period);
1154   if (NOTIFICATION_ERROR_NONE != ret) {
1155     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification led off period error",
1156                               ("Set notification led off period error: %d", ret));
1157   }
1158
1159   return PlatformResult(ErrorCode::NO_ERROR);
1160 }
1161
1162 PlatformResult CommonNotification::SetProgressTypeAndValueFromJson(
1163     const picojson::value& noti_value, bool is_update, notification_h noti_handle) {
1164   LoggerD("Enter");
1165   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1166
1167   // progressType
1168   // native code does not support progress_type value, we are using NOTIFICATION_IMAGE_TYPE_LIST_5
1169   // to store this field on native level
1170   const std::string& progress_type = FromJson<std::string>(noti_obj, "progressType");
1171   PlatformResult status = SetImage(noti_handle, NOTIFICATION_IMAGE_TYPE_LIST_5, progress_type);
1172   CHECK_ERROR(status);
1173
1174   // progressValue
1175   double progress_value = -1;
1176   if (noti_value.contains("progressValue") && !IsNull(noti_obj, "progressValue")) {
1177     progress_value = FromJson<double>(noti_obj, "progressValue");
1178   }
1179   status = SetProgressValue(noti_handle, progress_type, progress_value, is_update);
1180   CHECK_ERROR(status);
1181   return PlatformResult(ErrorCode::NO_ERROR);
1182 }
1183
1184 PlatformResult CommonNotification::SetAppControlInfoFromJson(const picojson::value& noti_value,
1185                                                              notification_h noti_handle) {
1186   LoggerD("Enter");
1187   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1188
1189   app_control_h app_control = nullptr;
1190   SCOPE_EXIT {
1191     if (app_control) {
1192       app_control_destroy(app_control);
1193     }
1194   };
1195
1196   PlatformResult status = CreateAppControl(&app_control);
1197   CHECK_ERROR(status);
1198
1199   if (noti_value.contains("appControl") && !IsNull(noti_obj, "appControl")) {
1200     status = SetApplicationControl(app_control, FromJson<picojson::object>(noti_obj, "appControl"));
1201     CHECK_ERROR(status);
1202   }
1203
1204   if (noti_value.contains("appId") && !IsNull(noti_obj, "appId")) {
1205     status = SetApplicationId(app_control, FromJson<std::string>(noti_obj, "appId"));
1206     CHECK_ERROR(status);
1207   }
1208
1209   status = SetAppControl(noti_handle, app_control);
1210   CHECK_ERROR(status);
1211
1212   return PlatformResult(ErrorCode::NO_ERROR);
1213 }
1214
1215 PlatformResult CommonNotification::SetEventsNumberFromJson(const picojson::value& noti_value,
1216                                                            const std::string& key,
1217                                                            notification_h noti_handle) {
1218   LoggerD("Enter");
1219   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1220
1221   if (noti_value.contains(key) && !IsNull(noti_obj, key.c_str())) {
1222     long number = (long)FromJson<double>(noti_obj, key.c_str());
1223     PlatformResult status =
1224         SetText(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, std::to_string(number));
1225     CHECK_ERROR(status);
1226   }
1227   return PlatformResult(ErrorCode::NO_ERROR);
1228 }
1229
1230 PlatformResult CommonNotification::SetDetailInfosFromJson(const picojson::value& noti_value,
1231                                                           notification_h noti_handle) {
1232   LoggerD("Enter");
1233   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1234   if (noti_value.contains("detailInfo") && !IsNull(noti_obj, "detailInfo")) {
1235     const picojson::array& array = FromJson<picojson::array>(noti_obj, "detailInfo");
1236
1237     if (array.size() > info_map_.size()) {
1238       return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR,
1239                                 "Too many values in notification detailInfo array");
1240     }
1241     size_t idx = 0;
1242
1243     for (auto& item : array) {
1244       const picojson::object& obj = JsonCast<picojson::object>(item);
1245
1246       PlatformResult status =
1247           SetText(noti_handle, info_map_.at(idx), FromJson<std::string>(obj, "mainText"));
1248       CHECK_ERROR(status);
1249
1250       SetTextFromJson(picojson::value(obj), info_sub_map_.at(idx), "subText", noti_handle);
1251       CHECK_ERROR(status);
1252       ++idx;
1253     }
1254   }
1255   return PlatformResult(ErrorCode::NO_ERROR);
1256 }
1257
1258 PlatformResult CommonNotification::SetVibrationFromJson(const picojson::value& noti_value,
1259                                                         notification_h noti_handle) {
1260   LoggerD("Enter");
1261   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1262   bool vibration = FromJson<bool>(noti_obj, "vibration");
1263
1264   bool platform_vibration;
1265   PlatformResult status = GetVibration(noti_handle, &platform_vibration);
1266   CHECK_ERROR(status);
1267
1268   if (platform_vibration != vibration) {
1269     notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE;
1270
1271     if (vibration) {
1272       vib_type = NOTIFICATION_VIBRATION_TYPE_DEFAULT;
1273     }
1274
1275     int ret = notification_set_vibration(noti_handle, vib_type, nullptr);
1276     if (NOTIFICATION_ERROR_NONE != ret) {
1277       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set notification vibration error",
1278                                 ("Set notification vibration error: %d", ret));
1279     }
1280   }
1281
1282   return PlatformResult(ErrorCode::NO_ERROR);
1283 }
1284
1285 PlatformResult CommonNotification::SetSoundPathFromJson(const picojson::value& noti_value,
1286                                                         notification_h noti_handle) {
1287   LoggerD("Enter");
1288   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1289
1290   if (noti_value.contains("soundPath") && !IsNull(noti_obj, "soundPath")) {
1291     const std::string& value_str = FromJson<std::string>(noti_obj, "soundPath");
1292     std::string real_path = FilesystemProvider::Create().GetRealPath(value_str);
1293
1294     PlatformResult status = SetSoundPath(noti_handle, real_path);
1295     CHECK_ERROR(status);
1296   }
1297
1298   return PlatformResult(ErrorCode::NO_ERROR);
1299 }
1300
1301 PlatformResult CommonNotification::SetPathsArrayFromJson(const picojson::value& noti_value,
1302                                                          ImageEnumMap map, const std::string& key,
1303                                                          notification_h noti_handle) {
1304   LoggerD("Enter");
1305   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1306
1307   if (noti_value.contains(key) && !IsNull(noti_obj, key.c_str())) {
1308     const picojson::array& array = FromJson<picojson::array>(noti_obj, key.c_str());
1309     if (array.size() > map.size()) {
1310       return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Too many values in array");
1311     }
1312     size_t idx = 0;
1313     for (auto& item : array) {
1314       const std::string& text = JsonCast<std::string>(item);
1315       std::string real_path = FilesystemProvider::Create().GetRealPath(text);
1316
1317       PlatformResult status = SetImage(noti_handle, map.at(idx), real_path);
1318       CHECK_ERROR(status);
1319
1320       ++idx;
1321     }
1322   }
1323
1324   return PlatformResult(ErrorCode::NO_ERROR);
1325 }
1326
1327 PlatformResult CommonNotification::SetTextsArrayFromJson(const picojson::value& noti_value,
1328                                                          InformationEnumMap map,
1329                                                          const std::string& key,
1330                                                          notification_h noti_handle) {
1331   LoggerD("Enter");
1332   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1333
1334   if (noti_value.contains(key) && !IsNull(noti_obj, key.c_str())) {
1335     const picojson::array& array = FromJson<picojson::array>(noti_obj, key.c_str());
1336     if (array.size() > map.size()) {
1337       return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Too many values in array");
1338     }
1339
1340     size_t idx = 0;
1341     for (auto& item : array) {
1342       const std::string& text = JsonCast<std::string>(item);
1343       PlatformResult status = SetText(noti_handle, map.at(idx), text);
1344       CHECK_ERROR(status);
1345
1346       ++idx;
1347     }
1348   }
1349
1350   return PlatformResult(ErrorCode::NO_ERROR);
1351 }
1352
1353 PlatformResult CommonNotification::SetTextFromJson(const picojson::value& noti_value,
1354                                                    notification_text_type_e type,
1355                                                    const std::string& key,
1356                                                    notification_h noti_handle) {
1357   LoggerD("Entered");
1358   const picojson::object& noti_obj = noti_value.get<picojson::object>();
1359
1360   if (noti_value.contains(key) && !IsNull(noti_obj, key.c_str())) {
1361     PlatformResult status =
1362         SetText(noti_handle, type, FromJson<std::string>(noti_obj, key.c_str()));
1363     CHECK_ERROR(status);
1364   }
1365   return PlatformResult(ErrorCode::NO_ERROR);
1366 }
1367
1368 }  // namespace notification
1369 }  // namespace extension