From db959daf3c75d852369f6f2a4d9dc50904565e63 Mon Sep 17 00:00:00 2001 From: Piotr Kosko Date: Wed, 7 Jun 2017 08:39:54 +0200 Subject: [PATCH] [Notification] prepared stubs for UserNotification [Feature] Added stubs for UserNotifcation object, added stubs for new versions of post, update, get, getAll. [Verification] Code compiles without error. TCT passrate didn't change. Checked in chrome console. Change-Id: I138a08e56149090c0c263cec66833aac6a5c65db Signed-off-by: Piotr Kosko --- src/notification/common_notification.cc | 980 +++++++++++++++++++++ src/notification/common_notification.h | 135 +++ src/notification/notification.gyp | 4 +- src/notification/notification_api.js | 178 +++- src/notification/notification_extension.cc | 2 +- src/notification/notification_instance.cc | 52 +- src/notification/notification_manager.cc | 36 + src/notification/notification_manager.h | 7 + src/notification/status_notification.cc | 969 +------------------- src/notification/status_notification.h | 93 +- 10 files changed, 1397 insertions(+), 1059 deletions(-) create mode 100644 src/notification/common_notification.cc create mode 100644 src/notification/common_notification.h diff --git a/src/notification/common_notification.cc b/src/notification/common_notification.cc new file mode 100644 index 00000000..ccaa6c75 --- /dev/null +++ b/src/notification/common_notification.cc @@ -0,0 +1,980 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification/common_notification.h" + +#include +#include +#include + +#include "common/converter.h" +#include "common/logger.h" +#include "common/scope_exit.h" +#include "common/filesystem/filesystem_provider.h" + +namespace extension { +namespace notification { + +using namespace common; + +const InformationEnumMap CommonNotification::info_map_ = { + {0, NOTIFICATION_TEXT_TYPE_INFO_1}, + {1, NOTIFICATION_TEXT_TYPE_INFO_2}, + {2, NOTIFICATION_TEXT_TYPE_INFO_3}}; + +const InformationEnumMap CommonNotification::info_sub_map_ = { + {0, NOTIFICATION_TEXT_TYPE_INFO_SUB_1}, + {1, NOTIFICATION_TEXT_TYPE_INFO_SUB_2}, + {2, NOTIFICATION_TEXT_TYPE_INFO_SUB_3}}; + +const ImageEnumMap CommonNotification::thumbnails_map_ = { + {0, NOTIFICATION_IMAGE_TYPE_LIST_1}, + {1, NOTIFICATION_IMAGE_TYPE_LIST_2}, + {2, NOTIFICATION_IMAGE_TYPE_LIST_3}, + {3, NOTIFICATION_IMAGE_TYPE_LIST_4}}; + +CommonNotification::CommonNotification() { +} + +CommonNotification::~CommonNotification() { +} + +bool CommonNotification::IsColorFormatNumberic(const std::string& color) { + LoggerD("Enter"); + std::string hexCode = "0123456789abcdef"; + if (color.length() != 7 || '#' != color[0]) { + return false; + } + + for (size_t i = 1; i < color.length(); i++) { + if (std::string::npos == hexCode.find(color[i])) { + return false; + } + } + + return true; +} + +PlatformResult CommonNotification::SetLayout(notification_h noti_handle, + const std::string& noti_type) { + + LoggerD("Enter"); + notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE; + + if (noti_type == "SIMPLE") { + long number; + PlatformResult status = + GetNumber(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &number); + if (status.IsError()) + { + LoggerE("Failed: GetNumber"); + return status; + } + if (number > 0) + noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE; + else + noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE; + } else if (noti_type == "THUMBNAIL") { + noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL; + } + if (noti_type == "ONGOING") { + noti_layout = NOTIFICATION_LY_ONGOING_EVENT; + } else if (noti_type == "PROGRESS") { + noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS; + } + int ret = notification_set_layout(noti_handle, noti_layout); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification layout error", + ("Set notification layout error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +static bool ServiceExtraDataCb(app_control_h service, + const char* key, + void* user_data) { + LoggerD("Enter"); + if (nullptr == user_data || nullptr == key) { + LoggerE("User data or key not exist"); + return true; + } + + picojson::array* control_data = static_cast(user_data); + + int length = 0; + char** value = NULL; + SCOPE_EXIT { free(value); }; + + int ret = app_control_get_extra_data_array(service, key, &value, &length); + if (ret != APP_CONTROL_ERROR_NONE) { + LoggerE("Get app control extra data error: %d", ret); + return true; + } + + if (!value || !length) { + LoggerE("Get app control extra data value error"); + return true; + } + + picojson::array values = picojson::array(); + for (int index = 0; index < length; ++index) { + values.push_back(picojson::value(value[index])); + } + + picojson::object data_control_elem = picojson::object(); + data_control_elem["key"] = picojson::value(key); + data_control_elem["value"] = picojson::value(values); + + control_data->push_back(picojson::value(data_control_elem)); + + return true; +} + +PlatformResult CommonNotification::Create(notification_type_e noti_type, + notification_h* noti_handle) { + LoggerD("Enter"); + *noti_handle = notification_create(noti_type); + if (!*noti_handle) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Cannot make new notification object"); + } + + if (NOTIFICATION_TYPE_ONGOING == noti_type) { + int ret = notification_set_display_applist( + *noti_handle, + NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | + NOTIFICATION_DISPLAY_APP_INDICATOR); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Cannot set notification display applist", + ("Cannot make new notification object: %d", ret)); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::StatusTypeFromPlatform( + notification_type_e noti_type, + notification_ly_type_e noti_layout, + std::string* type) { + LoggerD("Enter"); + if (noti_type == NOTIFICATION_TYPE_NOTI) { + if (noti_layout == NOTIFICATION_LY_NOTI_EVENT_SINGLE || + noti_layout == NOTIFICATION_LY_NOTI_EVENT_MULTIPLE) { + *type = "SIMPLE"; + } else if (noti_layout == NOTIFICATION_LY_NOTI_THUMBNAIL) { + *type = "THUMBNAIL"; + } + } else if (noti_type == NOTIFICATION_TYPE_ONGOING) { + if (noti_layout == NOTIFICATION_LY_ONGOING_EVENT) { + *type = "ONGOING"; + } else if (noti_layout == NOTIFICATION_LY_ONGOING_PROGRESS) { + *type = "PROGRESS"; + } + } else { + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, + "Notification type not found"); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::StatusTypeToPlatform( + const std::string& type, + notification_type_e* noti_type) { + LoggerD("Enter"); + if (type == "SIMPLE" || type == "THUMBNAIL") { + *noti_type = NOTIFICATION_TYPE_NOTI; + } else if (type == "ONGOING" || type == "PROGRESS") { + *noti_type = NOTIFICATION_TYPE_ONGOING; + } else { + return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, + "Invalide notification type", + ("Invalide noti type: %s", type.c_str())); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetImage( + notification_h noti_handle, + notification_image_type_e image_type, + std::string* image_path) { + LoggerD("Enter"); + char* path = NULL; + + *image_path = ""; + + if (notification_get_image(noti_handle, image_type, &path) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification image error", + ("Get notification image error, image_type: %d", image_type)); + } + if (path) { + *image_path = path; + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetImage( + notification_h noti_handle, + notification_image_type_e image_type, + const std::string& image_path) { + LoggerD("Enter"); + int ret = notification_set_image(noti_handle, image_type, image_path.c_str()); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification image error", + ("Set notification image error, image_type: %d, error: %d", + image_type, ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetText(notification_h noti_handle, + notification_text_type_e text_type, + std::string* noti_text) { + LoggerD("Enter"); + char* text = NULL; + + *noti_text = ""; + + if (notification_get_text(noti_handle, text_type, &text) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification text error", + ("Get notification text error, text_type: %d", text_type)); + } + + if (text) + *noti_text = text; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetText(notification_h noti_handle, + notification_text_type_e text_type, + const std::string& noti_text) { + LoggerD("Enter"); + int ret = notification_set_text(noti_handle, + text_type, + noti_text.c_str(), + NULL, + NOTIFICATION_VARIABLE_TYPE_NONE); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification text error", + ("Set notification text error, text_type: %d, error: %d", + text_type, ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetNumber(notification_h noti_handle, + notification_text_type_e text_type, + long* number) { + LoggerD("Enter"); + std::string text; + PlatformResult status = GetText(noti_handle, text_type, &text); + if (status.IsError()) + return status; + + if (text.length()) + *number = std::stol(text); + else + *number = -1; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetDetailInfos(notification_h noti_handle, + picojson::array* out) { + LoggerD("Enter"); + if (info_map_.size() != info_sub_map_.size()) { + return LogAndCreateResult( + ErrorCode::VALIDATION_ERR, + "Different notification information types element size"); + } + + picojson::value detail_info = picojson::value(picojson::object()); + picojson::object& detail_info_obj = detail_info.get(); + + std::string text; + size_t info_map_size = info_map_.size(); + for (size_t idx = 0; idx < info_map_size; ++idx) { + PlatformResult status = GetText(noti_handle, info_map_.at(idx), &text); + if (status.IsError()) + return status; + + if (!text.length()) + break; + + detail_info_obj["mainText"] = picojson::value(text); + + status = GetText(noti_handle, info_sub_map_.at(idx), &text); + if (status.IsError()) + return status; + + if (text.length()) { + detail_info_obj["subText"] = picojson::value(text); + } + + out->push_back(detail_info); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetDetailInfos( + notification_h noti_handle, + const picojson::array& value) { + LoggerD("Enter"); + size_t idx = 0; + + size_t info_map_size = info_map_.size(); + for (auto& item : value) { + const picojson::object& obj = JsonCast(item); + + PlatformResult status = + SetText(noti_handle, + info_map_.at(idx), + common::FromJson(obj, "mainText")); + if (status.IsError()) + return status; + + if (picojson::value(obj).contains("subText") && !IsNull(obj, "subText")) { + PlatformResult status = + SetText(noti_handle, + info_sub_map_.at(idx), + common::FromJson(obj, "subText")); + if (status.IsError()) + return status; + } + + ++idx; + + if (idx > info_map_size) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, + "Too many values in notification detailInfo array"); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetLedColor(notification_h noti_handle, + std::string* led_color) { + LoggerD("Enter"); + unsigned int color = 0; + notification_led_op_e type = NOTIFICATION_LED_OP_ON; + + if (notification_get_led(noti_handle, &type, (int*)&color) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification led displaying option error"); + } + + *led_color = ""; + std::stringstream stream; + + if (NOTIFICATION_LED_OP_OFF != type) { + color = 0x00FFFFFF & color; + stream << std::hex << color; + *led_color = "#" + stream.str(); + + while (led_color->length() < 7) { + led_color->insert(1, "0"); + } + + std::transform( + led_color->begin(), led_color->end(), led_color->begin(), ::tolower); + } + + LoggerD("color:%s", (*led_color).c_str()); + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetLedColor(notification_h noti_handle, + const std::string& led_color) { + LoggerD("Enter"); + std::string color_str = led_color; + std::transform( + color_str.begin(), color_str.end(), color_str.begin(), ::tolower); + + if (!IsColorFormatNumberic(color_str)) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, + "Led color is not numeric value", + ("Led color is not numeric value: %s", color_str.c_str())); + } + + std::stringstream stream; + unsigned int color = 0; + notification_led_op_e type = NOTIFICATION_LED_OP_ON; + std::string color_code = + color_str.substr(1, color_str.length()).insert(0, "ff"); + + stream << std::hex << color_code; + stream >> color; + + if (color != 0) + type = NOTIFICATION_LED_OP_ON_CUSTOM_COLOR; + else + type = NOTIFICATION_LED_OP_OFF; + + int ret = notification_set_led(noti_handle, type, static_cast(color)); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification led color eror", + ("Set notification led color eror: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetLedPeriod(notification_h noti_handle, + unsigned long* on_period, + unsigned long* off_period) { + LoggerD("Enter"); + int on_time = 0; + int off_time = 0; + + if (notification_get_led_time_period(noti_handle, &on_time, &off_time) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification led on/off period error"); + } + + if (on_period) + *on_period = on_time; + if (off_period) + *off_period = off_time; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetLedOnPeriod(notification_h noti_handle, + unsigned long on_period) { + LoggerD("Enter"); + unsigned long off_period = 0; + PlatformResult status = GetLedPeriod(noti_handle, nullptr, &off_period); + if (status.IsError()) + return status; + + int ret = + notification_set_led_time_period(noti_handle, on_period, off_period); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification led on period error", + ("Set notification led on period error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetLedOffPeriod(notification_h noti_handle, + unsigned long off_period) { + LoggerD("Enter"); + unsigned long on_period = 0; + PlatformResult status = GetLedPeriod(noti_handle, &on_period, nullptr); + if (status.IsError()) + return status; + + int ret = + notification_set_led_time_period(noti_handle, on_period, off_period); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification led off period error", + ("Set notification led off period error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetThumbnails(notification_h noti_handle, + picojson::array* out) { + LoggerD("Enter"); + std::string text; + size_t thumbnails_map_size = thumbnails_map_.size(); + for (size_t idx = 0; idx < thumbnails_map_size; ++idx) { + PlatformResult status = + GetImage(noti_handle, thumbnails_map_.at(idx), &text); + if (status.IsError()) + return status; + + if (!text.length()) + break; + + //CHECK GetVirtualPath ?? + out->push_back(picojson::value(common::FilesystemProvider::Create().GetVirtualPath(text))); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetThumbnails(notification_h noti_handle, + const picojson::array& value) { + LoggerD("Enter"); + size_t idx = 0; + + size_t thumbnails_map_size = thumbnails_map_.size(); + for (auto& item : value) { + const std::string& text = JsonCast(item); + std::string real_path = common::FilesystemProvider::Create().GetRealPath(text); + + PlatformResult status = + SetImage(noti_handle, thumbnails_map_.at(idx), real_path); + if (status.IsError()) + return status; + + ++idx; + + if (idx > thumbnails_map_size) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, + "Too many values in notification thumbnail array"); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetSoundPath(notification_h noti_handle, + std::string* sound_path) { + LoggerD("Enter"); + *sound_path = ""; + + const char* path = NULL; + notification_sound_type_e type = NOTIFICATION_SOUND_TYPE_NONE; + + if (notification_get_sound(noti_handle, &type, &path) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification sound error"); + } + + LoggerD("Sound type = %d", type); + + if (path && (type == NOTIFICATION_SOUND_TYPE_USER_DATA)) { + *sound_path = path; + } + + LoggerD("Sound path = %s", sound_path->c_str()); + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetSoundPath(notification_h noti_handle, + const std::string& sound_path) { + LoggerD("Enter"); + int ret = notification_set_sound( + noti_handle, NOTIFICATION_SOUND_TYPE_USER_DATA, sound_path.c_str()); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification sound error", + ("Set notification sound error: %d", ret)); + } + + LoggerD("Sound path = %s", sound_path.c_str()); + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetVibration(notification_h noti_handle, + bool* vibration) { + LoggerD("Enter"); + notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE; + + if (notification_get_vibration(noti_handle, &vib_type, NULL) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification vibration error"); + } + + if (NOTIFICATION_VIBRATION_TYPE_DEFAULT == vib_type || + NOTIFICATION_VIBRATION_TYPE_USER_DATA == vib_type) { + *vibration = true; + } else { + *vibration = false; + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetVibration(notification_h noti_handle, + bool vibration) { + LoggerD("Enter"); + bool platform_vibration; + PlatformResult status = GetVibration(noti_handle, &platform_vibration); + if (status.IsError()) + return status; + + if (platform_vibration != vibration) { + notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE; + + if (vibration) { + vib_type = NOTIFICATION_VIBRATION_TYPE_DEFAULT; + } + + int ret = notification_set_vibration(noti_handle, vib_type, NULL); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification vibration error", + ("Set notification vibration error: %d", ret)); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetApplicationControl( + app_control_h app_handle, + picojson::object* out_ptr) { + LoggerD("Enter"); + picojson::object& out = *out_ptr; + + char* operation = NULL; + char* uri = NULL; + char* mime = NULL; + char* category = NULL; + SCOPE_EXIT { + free(operation); + free(uri); + free(mime); + free(category); + }; + + int ret = app_control_get_operation(app_handle, &operation); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get application control operation error", + ("Get application control operation error: %d", ret)); + } + if (operation) { + out["operation"] = picojson::value(operation); + LoggerD("operation = %s", operation); + } + + if (app_control_get_uri(app_handle, &uri) != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get application control uri error"); + } + if (uri) { + out["uri"] = picojson::value(uri); + LoggerD("uri = %s", uri); + } + + if (app_control_get_mime(app_handle, &mime) != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get application control mime error"); + } + if (mime) { + out["mime"] = picojson::value(mime); + LoggerD("mime = %s", mime); + } + + if (app_control_get_category(app_handle, &category) != + APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get application control category error"); + } + if (category) { + out["category"] = picojson::value(category); + LoggerD("category = %s", category); + } + + picojson::array app_control_data = picojson::array(); + if (app_control_foreach_extra_data( + app_handle, ServiceExtraDataCb, (void*)&app_control_data) != + APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get application control data error"); + } + out["data"] = picojson::value(app_control_data); + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetApplicationControl( + app_control_h app_handle, + const picojson::object& app_ctrl) { + LoggerD("Enter"); + picojson::value val(app_ctrl); + const std::string& operation = + common::FromJson(app_ctrl, "operation"); + + int ret; + if (operation.length()) { + ret = app_control_set_operation(app_handle, operation.c_str()); + } else { + ret = app_control_set_operation(app_handle, APP_CONTROL_OPERATION_DEFAULT); + } + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set application control operation error", + ("Set application control operation error: %d", ret)); + } + + if (val.contains("uri") && !IsNull(app_ctrl, "uri")) { + const std::string& uri = common::FromJson(app_ctrl, "uri"); + ret = app_control_set_uri(app_handle, uri.c_str()); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set application control uri error", + ("Set application control uri error: %d", ret)); + } + } + + if (val.contains("mime") && !IsNull(app_ctrl, "mime")) { + const std::string& mime = common::FromJson(app_ctrl, "mime"); + ret = app_control_set_mime(app_handle, mime.c_str()); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set application control mime error", + ("Set application control mime error: %d", ret)); + } + } + + if (val.contains("category") && !IsNull(app_ctrl, "category")) { + const std::string& category = + common::FromJson(app_ctrl, "category"); + ret = app_control_set_category(app_handle, category.c_str()); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set application control category error", + ("Set application control category error: %d", ret)); + } + } + + if (!picojson::value(app_ctrl).contains("data") || IsNull(app_ctrl, "data")) { + return PlatformResult(ErrorCode::NO_ERROR); + } + + auto& items = common::FromJson(app_ctrl, "data"); + + int idx = 0; + + for (auto item : items) { + const picojson::object& obj = JsonCast(item); + const std::string key = common::FromJson(obj, "key"); + const picojson::array values = + common::FromJson(obj, "value"); + const char** arrayValue = + (const char**)calloc(sizeof(char*), values.size()); + SCOPE_EXIT { free(arrayValue); }; + idx = 0; + for (auto& item : values) { + arrayValue[idx] = JsonCast(item).c_str(); + ++idx; + } + ret = app_control_add_extra_data_array( + app_handle, key.c_str(), arrayValue, values.size()); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set application control extra data error", + ("Set application control extra data error: %d", ret)); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetApplicationId(app_control_h app_handle, + std::string* app_id) { + LoggerD("Enter"); + char* app_id_str = NULL; + SCOPE_EXIT { free(app_id_str); }; + + *app_id = ""; + + if (app_control_get_app_id(app_handle, &app_id_str) != + APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get applicaiton ID failed"); + } + + if (app_id_str != NULL) { + *app_id = app_id_str; + } + + LoggerD("Get appId = %s", /*(*app_id).c_str()*/ app_id_str); + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetApplicationId(app_control_h app_handle, + const std::string& app_id) { + LoggerD("Enter"); + int ret = app_control_set_app_id(app_handle, app_id.c_str()); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set applicaiton ID error", + ("Set applicaiton ID error: %d", ret)); + } + + LoggerD("Set appId = %s", app_id.c_str()); + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetProgressValue( + notification_h noti_handle, + const std::string& progess_type, + double* progress_value) { + LoggerD("Enter"); + double tmp_progress_value = 0.0; + + if (progess_type == kProgressTypeByte) { + if (notification_get_size(noti_handle, &tmp_progress_value) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification size error"); + } + } else if (progess_type == kProgressTypePercentage) { + if (notification_get_progress(noti_handle, &tmp_progress_value) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification progress error"); + } + // native api uses range 0-1, but webapi expects 0-100, so we need to multiply result with 100 + tmp_progress_value *= 100; + } else { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Unknown notification progress type", + ("Unknown notification progress type: %s ", progess_type.c_str())); + } + + LOGGER(DEBUG) << "Progress " << progess_type << " = " << tmp_progress_value; + + *progress_value = tmp_progress_value; + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetProgressValue( + notification_h noti_handle, + const std::string& progress_type, + double progress_value, + bool is_update) { + LoggerD("Enter"); + int ret; + + if (progress_type == kProgressTypeByte) { + ret = notification_set_size(noti_handle, progress_value); + + if (is_update) { + ret = notification_update_size(noti_handle, NOTIFICATION_PRIV_ID_NONE, + progress_value); + } + } else if (progress_type == kProgressTypePercentage) { + // native api uses range 0-1, but webapi expects 0-100, so we need to divide by 100 + ret = notification_set_progress(noti_handle, progress_value/100); + + if (is_update) { + ret = notification_update_progress(noti_handle, NOTIFICATION_PRIV_ID_NONE, + progress_value); + } + } else { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Unknown notification progress type", + ("Unknown notification progress type: %s ", progress_type.c_str())); + } + + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Set notification progress/size error", + ("Set notification progress/size error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetPostedTime(notification_h noti_handle, + time_t* posted_time) { + LoggerD("Enter"); + *posted_time = 0; + + if (notification_get_insert_time(noti_handle, posted_time) != + NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Get notification posted time error"); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetNotiHandle(int id, + notification_h* noti_handle) { + LoggerD("Enter"); + *noti_handle = notification_load(NULL, id); + if (NULL == *noti_handle) { + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, + "Not found or removed notification id"); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::GetAppControl(notification_h noti_handle, + app_control_h* app_control) { + LoggerD("Enter"); + int ret = + notification_get_launch_option(noti_handle, + NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, + static_cast(app_control)); + if (ret != NOTIFICATION_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, + "Notification get launch option error", + ("Notification get launch option error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::CreateAppControl( + app_control_h* app_control) { + LoggerD("Enter"); + int ret = app_control_create(app_control); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, + "Application create error", + ("Application create error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult CommonNotification::SetAppControl(notification_h noti_handle, + app_control_h app_control) { + LoggerD("Enter"); + int ret = + notification_set_launch_option(noti_handle, + NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, + static_cast(app_control)); + if (ret != APP_CONTROL_ERROR_NONE) { + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, + "Notification set launch option error", + ("Notification set launch option error: %d", ret)); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + +} // namespace notification +} // namespace extension diff --git a/src/notification/common_notification.h b/src/notification/common_notification.h new file mode 100644 index 00000000..591e9989 --- /dev/null +++ b/src/notification/common_notification.h @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOTIFICATION_COMMON_NOTIFICATION_H_ +#define NOTIFICATION_COMMON_NOTIFICATION_H_ + +#include +#include + +#include "common/picojson.h" +#include "common/platform_result.h" + +#include "common/XW_Extension.h" + +namespace extension { +namespace notification { + +const std::string kProgressTypePercentage = "PERCENTAGE"; +const std::string kProgressTypeByte = "BYTE"; + +typedef std::map InformationEnumMap; +typedef std::map ImageEnumMap; + +class CommonNotification { + public: + XW_EXPORT static common::PlatformResult GetAppControl(notification_h noti_handle, + app_control_h* app_control); + static common::PlatformResult GetNotiHandle(int id, + notification_h* noti_handle); + XW_EXPORT static common::PlatformResult SetAppControl(notification_h noti_handle, + app_control_h app_control); + + static const InformationEnumMap info_map_; + static const InformationEnumMap info_sub_map_; + static const ImageEnumMap thumbnails_map_; + + static common::PlatformResult StatusTypeFromPlatform( + notification_type_e noti_type, + notification_ly_type_e noti_layout, + std::string* type); + static common::PlatformResult StatusTypeToPlatform( + const std::string& type, + notification_type_e* noti_type); + static common::PlatformResult Create(notification_type_e noti_type, + notification_h* noti_handle); + static common::PlatformResult GetImage(notification_h noti_handle, + notification_image_type_e image_type, + std::string* image_path); + static common::PlatformResult SetImage(notification_h noti_handle, + notification_image_type_e image_type, + const std::string& image_path); + static common::PlatformResult GetText(notification_h noti_handle, + notification_text_type_e text_type, + std::string* noti_text); + static common::PlatformResult SetText(notification_h noti_handle, + notification_text_type_e text_type, + const std::string& noti_text); + static common::PlatformResult GetNumber(notification_h noti_handle, + notification_text_type_e text_type, + long* number); + static common::PlatformResult GetDetailInfos(notification_h noti_handle, + picojson::array* out); + static common::PlatformResult SetDetailInfos(notification_h noti_handle, + const picojson::array& value); + static common::PlatformResult GetLedColor(notification_h noti_handle, + std::string* led_color); + static common::PlatformResult SetLedColor(notification_h noti_handle, + const std::string& led_color); + static common::PlatformResult GetLedPeriod(notification_h noti_handle, + unsigned long* on_period, + unsigned long* off_period); + static common::PlatformResult SetLedOnPeriod(notification_h noti_handle, + unsigned long on_period); + static common::PlatformResult SetLedOffPeriod(notification_h noti_handle, + unsigned long off_period); + static common::PlatformResult GetThumbnails(notification_h noti_handle, + picojson::array* out); + static common::PlatformResult SetThumbnails(notification_h noti_handle, + const picojson::array& value); + static common::PlatformResult GetSoundPath(notification_h noti_handle, + std::string* sound_path); + static common::PlatformResult SetSoundPath(notification_h noti_handle, + const std::string& sound_path); + static common::PlatformResult GetVibration(notification_h noti_handle, + bool* vibration); + static common::PlatformResult SetVibration(notification_h noti_handle, + bool vibration); + static common::PlatformResult GetApplicationControl( + app_control_h app_handle, + picojson::object* out_ptr); + static common::PlatformResult SetApplicationControl( + app_control_h app_handle, + const picojson::object& app_ctrl); + static common::PlatformResult GetApplicationId(app_control_h app_handle, + std::string* app_id); + static common::PlatformResult SetApplicationId(app_control_h app_handle, + const std::string& app_id); + static common::PlatformResult GetProgressValue( + notification_h noti_handle, + const std::string& progess_type, + double* progress_value); + static common::PlatformResult SetProgressValue( + notification_h noti_handle, + const std::string& progress_type, + double progress_value, + bool is_update); + static common::PlatformResult GetPostedTime(notification_h noti_handle, + time_t* posted_time); + static common::PlatformResult SetLayout(notification_h noti_handle, + const std::string& noti_type); + static common::PlatformResult CreateAppControl(app_control_h* app_control); + + static bool IsColorFormatNumberic(const std::string& color); + protected: + CommonNotification(); + virtual ~CommonNotification(); +}; + +} // namespace notification +} // namespace extension + +#endif /* NOTIFICATION_COMMON_NOTIFICATION_H_ */ diff --git a/src/notification/notification.gyp b/src/notification/notification.gyp index 766fbb39..6374a5d7 100755 --- a/src/notification/notification.gyp +++ b/src/notification/notification.gyp @@ -18,7 +18,9 @@ 'notification_manager.h', 'notification_manager.cc', 'status_notification.cc', - 'status_notification.h' + 'status_notification.h', + 'common_notification.cc', + 'common_notification.h' ], 'conditions': [ ['tizen == 1', { diff --git a/src/notification/notification_api.js b/src/notification/notification_api.js index 5c7ce375..2b800b16 100644 --- a/src/notification/notification_api.js +++ b/src/notification/notification_api.js @@ -69,6 +69,13 @@ var StatusNotificationType = { PROGRESS: 'PROGRESS' }; +var UserNotificationType = { + SIMPLE: 'SIMPLE', + THUMBNAIL: 'THUMBNAIL', + ONGOING: 'ONGOING', + PROGRESS: 'PROGRESS' +}; + var NotificationProgressType = { PERCENTAGE: 'PERCENTAGE', BYTE: 'BYTE' @@ -84,10 +91,12 @@ function NotificationManager() {} NotificationManager.prototype.post = function(notification) { var args = validator_.validateArgs(arguments, [ - {name: 'notification', type: types_.PLATFORM_OBJECT, values: StatusNotification} + {name: 'notification', type: types_.PLATFORM_OBJECT, values: Notification} ]); var data = { + //add marker for UserNotification implementation + newImpl: (args.notification instanceof tizen.UserNotification), notification: args.notification }; @@ -107,7 +116,7 @@ NotificationManager.prototype.post = function(notification) { NotificationManager.prototype.update = function(notification) { var args = validator_.validateArgs(arguments, [ - {name: 'notification', type: types_.PLATFORM_OBJECT, values: StatusNotification} + {name: 'notification', type: types_.PLATFORM_OBJECT, values: Notification} ]); if (!arguments.length) { @@ -118,6 +127,8 @@ NotificationManager.prototype.update = function(notification) { } var data = { + //add marker for UserNotification implementation + newImpl: (args.notification instanceof tizen.UserNotification), notification: args.notification }; @@ -184,6 +195,36 @@ NotificationManager.prototype.get = function(id) { return returnObject; }; +NotificationManager.prototype.getNotification = function(id) { + var args = validator_.validateArgs(arguments, [ + {name: 'id', type: types_.STRING} + ]); + + if (!arguments.length) { + throw new WebAPIException(WebAPIException.NOT_FOUND_ERR); + } + + var data = { + //add marker for UserNotification implementation + newImpl: (args.notification instanceof tizen.UserNotification), + id: args.id + }; + + var result = native_.callSync('NotificationManager_get', data); + + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + + var n = native_.getResultObject(result); + + _edit.allow(); + var returnObject = new UserNotification(n.statusType, n.title, n); + _edit.disallow(); + + return returnObject; +}; + NotificationManager.prototype.getAll = function() { var result = native_.callSync('NotificationManager_getAll', {}); @@ -203,6 +244,28 @@ NotificationManager.prototype.getAll = function() { return notifications; }; +NotificationManager.prototype.getAllNotifications = function() { + var result = native_.callSync('NotificationManager_getAll', { + //add marker for UserNotification implementation + newImpl: true + }); + + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + + var n = native_.getResultObject(result); + var notifications = []; + + _edit.allow(); + for (var i = 0; i < n.length; i++) { + notifications.push(new UserNotification(n[i].statusType, n[i].title, n[i])); + } + _edit.disallow(); + + return notifications; +}; + /** * Plays the custom effect of the service LED that is located to the front of a device. * @@ -245,9 +308,12 @@ NotificationManager.prototype.stopLEDCustomEffect = function() { NotificationManager.prototype.saveNotificationAsTemplate = function(name, notification) { var args = validator_.validateArgs(arguments, [ {name: 'name', type: types_.STRING}, - {name: 'notification', type: types_.PLATFORM_OBJECT, values: StatusNotification} + {name: 'notification', type: types_.PLATFORM_OBJECT, values: Notification} ]); + //add marker for UserNotification implementation + args.newImpl = (args.notification instanceof tizen.UserNotification); + var result = native_.callSync('NotificationManager_saveNotificationAsTemplate', args); if (native_.isFailure(result)) { @@ -272,13 +338,13 @@ NotificationManager.prototype.createNotificationFromTemplate = function(name) { var n = native_.getResultObject(result); _edit.allow(); + //TODO create UserNotification here var returnObject = new StatusNotification(n.statusType, n.title, n); _edit.disallow(); return returnObject; }; - function NotificationInitDict(data) { var _iconPath = null; var _soundPath = null; @@ -600,7 +666,6 @@ function StatusNotification(statusType, title, notificationInitDict) { StatusNotification.prototype = new Notification(); StatusNotification.prototype.constructor = StatusNotification; - function NotificationDetailInfo(mainText, subText) { validator_.isConstructorCall(this, NotificationDetailInfo); @@ -629,6 +694,109 @@ function NotificationDetailInfo(mainText, subText) { }); } +function UserNotification(statusType, title, notificationGropedInitDict) { + validator_.isConstructorCall(this, UserNotification); + type_.isObject(notificationGropedInitDict) ? + notificationGropedInitDict.title = title : + notificationGropedInitDict = {title: title}; + UserNotificationInitDict.call(this, notificationGropedInitDict); + Notification.call(this, notificationGropedInitDict); + + var _statusType = (Object.keys(UserNotificationType)).indexOf(statusType) >= 0 + ? statusType : StatusNotificationType.SIMPLE; + + Object.defineProperties(this, { + statusType: { + get: function() { + return _statusType; + }, + set: function(v) { + _statusType = (Object.keys(StatusNotificationType)).indexOf(v) >= 0 && _edit.canEdit + ? v : _statusType; + }, + enumerable: true + } + }); +} + +UserNotification.prototype = new Notification(); +UserNotification.prototype.constructor = UserNotification; + +function UserNotificationInitDict(data) { + var _textContents; + var _images; + var _thumbnails; + var _actions; + var _groupContents; + var _leds; + + Object.defineProperties(this, { + textContents: { + get: function() { + return _textContents; + }, + set: function(v) { + _textContents = type_.isObject(v) || type_.isNull(v) ? v : _textContents; + }, + enumerable: true + }, + images: { + get: function() { + return _images; + }, + set: function(v) { + _images = type_.isObject(v) || type_.isNull(v) ? v : _images; + }, + enumerable: true + }, + thumbnails: { + get: function() { + return _thumbnails; + }, + set: function(v) { + _thumbnails = type_.isObject(v) || type_.isNull(v) ? v : _thumbnails; + }, + enumerable: true + }, + actions: { + get: function() { + return _actions; + }, + set: function(v) { + _actions = type_.isObject(v) || type_.isNull(v) ? v : _actions; + }, + enumerable: true + }, + groupContents: { + get: function() { + return _groupContents; + }, + set: function(v) { + _groupContents = type_.isObject(v) || type_.isNull(v) ? v : _groupContents; + }, + enumerable: true + }, + leds: { + get: function() { + return _leds; + }, + set: function(v) { + _leds = type_.isObject(v) || type_.isNull(v) ? v : _leds; + }, + enumerable: true + } + }); + + if (data instanceof _global.Object) { + for (var prop in data) { + if (this.hasOwnProperty(prop)) { + this[prop] = data[prop]; + } + } + } +} + exports = new NotificationManager(); tizen.StatusNotification = StatusNotification; +tizen.UserNotification = UserNotification; tizen.NotificationDetailInfo = NotificationDetailInfo; diff --git a/src/notification/notification_extension.cc b/src/notification/notification_extension.cc index 99a315c6..f2839285 100644 --- a/src/notification/notification_extension.cc +++ b/src/notification/notification_extension.cc @@ -26,7 +26,7 @@ NotificationExtension::NotificationExtension() { SetExtensionName("tizen.notification"); SetJavaScriptAPI(kSource_notification_api); - const char* entry_points[] = {"tizen.StatusNotification", + const char* entry_points[] = {"tizen.StatusNotification", "tizen.UserNotification", "tizen.NotificationDetailInfo", NULL}; SetExtraJSEntryPoints(entry_points); } diff --git a/src/notification/notification_instance.cc b/src/notification/notification_instance.cc index d4f06dac..a4ffee98 100644 --- a/src/notification/notification_instance.cc +++ b/src/notification/notification_instance.cc @@ -75,8 +75,18 @@ void NotificationInstance::NotificationManagerPost(const picojson::value& args, LoggerD("Enter"); picojson::value val{picojson::object{}}; - PlatformResult status = - manager_->Post(args.get(), val.get()); + + using namespace std::placeholders; + std::function impl {}; + if (args.contains("newImpl") && args.get("newImpl").get()) { + LoggerD("New implementation"); + impl = std::bind(&NotificationManager::PostUserNoti, manager_, _1, _2); + } else { + LoggerW("Deprecated object used"); + impl = std::bind(&NotificationManager::Post, manager_, _1, _2); + } + + PlatformResult status = impl(args.get(), val.get()); if (status.IsSuccess()) { ReportSuccess(val, out); @@ -91,7 +101,17 @@ void NotificationInstance::NotificationManagerUpdate( CHECK_PRIVILEGE_ACCESS(kPrivilegeNotification, &out); LoggerD("Enter"); - PlatformResult status = manager_->Update(args.get()); + using namespace std::placeholders; + std::function impl {}; + if (args.contains("newImpl") && args.get("newImpl").get()) { + LoggerD("New implementation"); + impl = std::bind(&NotificationManager::UpdateUserNoti, manager_, _1); + } else { + LoggerW("Deprecated object used"); + impl = std::bind(&NotificationManager::Update, manager_, _1); + } + + PlatformResult status = impl(args.get()); if (status.IsSuccess()) { ReportSuccess(out); @@ -135,8 +155,17 @@ void NotificationInstance::NotificationManagerGet(const picojson::value& args, LoggerD("Enter"); picojson::value val{picojson::object{}}; - PlatformResult status = - manager_->Get(args.get(), val.get()); + using namespace std::placeholders; + std::function impl {}; + if (args.contains("newImpl") && args.get("newImpl").get()) { + LoggerD("New implementation"); + impl = std::bind(&NotificationManager::GetUserNoti, manager_, _1, _2); + } else { + LoggerW("Deprecated object used"); + impl = std::bind(&NotificationManager::Get, manager_, _1, _2); + } + + PlatformResult status = impl(args.get(), val.get()); if (status.IsSuccess()) { ReportSuccess(val, out); @@ -151,7 +180,17 @@ void NotificationInstance::NotificationManagerGetAll( LoggerD("Enter"); picojson::value val{picojson::array{}}; - PlatformResult status = manager_->GetAll(val.get()); + using namespace std::placeholders; + std::function impl {}; + if (args.contains("newImpl") && args.get("newImpl").get()) { + LoggerD("New implementation"); + impl = std::bind(&NotificationManager::GetAllUserNoti, manager_, _1); + } else { + LoggerW("Deprecated object used"); + impl = std::bind(&NotificationManager::GetAll, manager_, _1); + } + + PlatformResult status = impl(val.get()); if (status.IsSuccess()) { ReportSuccess(val, out); @@ -211,6 +250,7 @@ void NotificationInstance::NotificationManagerCreateFromTemplate(const picojson: CHECK_PRIVILEGE_ACCESS(kPrivilegeNotification, &out); picojson::value val{picojson::object{}}; + //TODO return UserNotification here PlatformResult status = manager_->CreateFromTemplate(args.get(), val.get()); diff --git a/src/notification/notification_manager.cc b/src/notification/notification_manager.cc index 84adcd9d..6079a9d4 100644 --- a/src/notification/notification_manager.cc +++ b/src/notification/notification_manager.cc @@ -30,6 +30,7 @@ #include "common/scope_exit.h" #include "notification/status_notification.h" +#include "notification/common_notification.h" namespace extension { namespace notification { @@ -56,11 +57,29 @@ PlatformResult NotificationManager::Post(const picojson::object& args, return StatusNotification::FromJson(args, false, &out); } +PlatformResult NotificationManager::PostUserNoti(const picojson::object& args, + picojson::object& out) { + LoggerD("Enter"); + // TODO implement + return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR, + "Not implemented yet", + ("Not implemented yet")); +} + PlatformResult NotificationManager::Update(const picojson::object& args) { LoggerD("Enter"); return StatusNotification::FromJson(args, true, NULL); } +PlatformResult NotificationManager::UpdateUserNoti(const picojson::object& args) { + LoggerD("Enter"); + // TODO implement + return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR, + "Not implemented yet", + ("Not implemented yet")); +} + + PlatformResult NotificationManager::Remove(const picojson::object& args) { LoggerD("Enter"); int id = std::stoi(FromJson(args, "id")); @@ -137,6 +156,15 @@ PlatformResult NotificationManager::Get(const picojson::object& args, return PlatformResult(ErrorCode::NO_ERROR); } +PlatformResult NotificationManager::GetUserNoti(const picojson::object& args, + picojson::object& out) { + LoggerD("Enter"); + // TODO implement + return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR, + "Not implemented yet", + ("Not implemented yet")); +} + PlatformResult NotificationManager::GetAll(picojson::array& out) { LoggerD("Enter"); notification_h noti = nullptr; @@ -205,6 +233,14 @@ PlatformResult NotificationManager::GetAll(picojson::array& out) { return PlatformResult(ErrorCode::NO_ERROR); } +PlatformResult NotificationManager::GetAllUserNoti(picojson::array& out) { + LoggerD("Enter"); + // TODO implement + return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR, + "Not implemented yet", + ("Not implemented yet")); +} + PlatformResult NotificationManager::PlayLEDCustomEffect( const picojson::object& args) { LoggerD("Enter"); diff --git a/src/notification/notification_manager.h b/src/notification/notification_manager.h index 85cd85fa..770cc7e8 100644 --- a/src/notification/notification_manager.h +++ b/src/notification/notification_manager.h @@ -31,16 +31,23 @@ class NotificationManager { common::PlatformResult Post(const picojson::object& args, picojson::object& out); + common::PlatformResult PostUserNoti(const picojson::object& args, + picojson::object& out); common::PlatformResult Update(const picojson::object& args); + common::PlatformResult UpdateUserNoti(const picojson::object& args); common::PlatformResult Remove(const picojson::object& args); common::PlatformResult RemoveAll(); common::PlatformResult Get(const picojson::object& args, picojson::object& out); + common::PlatformResult GetUserNoti(const picojson::object& args, + picojson::object& out); common::PlatformResult GetAll(picojson::array& out); + common::PlatformResult GetAllUserNoti(picojson::array& out); common::PlatformResult PlayLEDCustomEffect(const picojson::object& args); common::PlatformResult StopLEDCustomEffect(); + // TODO needed also below functions for new design? common::PlatformResult SaveTemplate(const picojson::object& args); common::PlatformResult CreateFromTemplate(const picojson::object& args, picojson::object& out); diff --git a/src/notification/status_notification.cc b/src/notification/status_notification.cc index 279182c3..f2378ca5 100644 --- a/src/notification/status_notification.cc +++ b/src/notification/status_notification.cc @@ -30,955 +30,12 @@ namespace notification { using namespace common; -const std::string kProgressTypePercentage = "PERCENTAGE"; -const std::string kProgressTypeByte = "BYTE"; - -const InformationEnumMap StatusNotification::info_map_ = { - {0, NOTIFICATION_TEXT_TYPE_INFO_1}, - {1, NOTIFICATION_TEXT_TYPE_INFO_2}, - {2, NOTIFICATION_TEXT_TYPE_INFO_3}}; - -const InformationEnumMap StatusNotification::info_sub_map_ = { - {0, NOTIFICATION_TEXT_TYPE_INFO_SUB_1}, - {1, NOTIFICATION_TEXT_TYPE_INFO_SUB_2}, - {2, NOTIFICATION_TEXT_TYPE_INFO_SUB_3}}; - -const ImageEnumMap StatusNotification::thumbnails_map_ = { - {0, NOTIFICATION_IMAGE_TYPE_LIST_1}, - {1, NOTIFICATION_IMAGE_TYPE_LIST_2}, - {2, NOTIFICATION_IMAGE_TYPE_LIST_3}, - {3, NOTIFICATION_IMAGE_TYPE_LIST_4}}; - StatusNotification::StatusNotification() { } StatusNotification::~StatusNotification() { } -bool StatusNotification::IsColorFormatNumberic(const std::string& color) { - LoggerD("Enter"); - std::string hexCode = "0123456789abcdef"; - if (color.length() != 7 || '#' != color[0]) { - return false; - } - - for (size_t i = 1; i < color.length(); i++) { - if (std::string::npos == hexCode.find(color[i])) { - return false; - } - } - - return true; -} - -PlatformResult StatusNotification::SetLayout(notification_h noti_handle, - const std::string& noti_type) { - - LoggerD("Enter"); - notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE; - - if (noti_type == "SIMPLE") { - long number; - PlatformResult status = - GetNumber(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &number); - if (status.IsError()) - { - LoggerE("Failed: GetNumber"); - return status; - } - if (number > 0) - noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE; - else - noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE; - } else if (noti_type == "THUMBNAIL") { - noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL; - } - if (noti_type == "ONGOING") { - noti_layout = NOTIFICATION_LY_ONGOING_EVENT; - } else if (noti_type == "PROGRESS") { - noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS; - } - int ret = notification_set_layout(noti_handle, noti_layout); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification layout error", - ("Set notification layout error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -static bool ServiceExtraDataCb(app_control_h service, - const char* key, - void* user_data) { - LoggerD("Enter"); - if (nullptr == user_data || nullptr == key) { - LoggerE("User data or key not exist"); - return true; - } - - picojson::array* control_data = static_cast(user_data); - - int length = 0; - char** value = NULL; - SCOPE_EXIT { free(value); }; - - int ret = app_control_get_extra_data_array(service, key, &value, &length); - if (ret != APP_CONTROL_ERROR_NONE) { - LoggerE("Get app control extra data error: %d", ret); - return true; - } - - if (!value || !length) { - LoggerE("Get app control extra data value error"); - return true; - } - - picojson::array values = picojson::array(); - for (int index = 0; index < length; ++index) { - values.push_back(picojson::value(value[index])); - } - - picojson::object data_control_elem = picojson::object(); - data_control_elem["key"] = picojson::value(key); - data_control_elem["value"] = picojson::value(values); - - control_data->push_back(picojson::value(data_control_elem)); - - return true; -} - -PlatformResult StatusNotification::Create(notification_type_e noti_type, - notification_h* noti_handle) { - LoggerD("Enter"); - *noti_handle = notification_create(noti_type); - if (!*noti_handle) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Cannot make new notification object"); - } - - if (NOTIFICATION_TYPE_ONGOING == noti_type) { - int ret = notification_set_display_applist( - *noti_handle, - NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | - NOTIFICATION_DISPLAY_APP_INDICATOR); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Cannot set notification display applist", - ("Cannot make new notification object: %d", ret)); - } - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::StatusTypeFromPlatform( - notification_type_e noti_type, - notification_ly_type_e noti_layout, - std::string* type) { - LoggerD("Enter"); - if (noti_type == NOTIFICATION_TYPE_NOTI) { - if (noti_layout == NOTIFICATION_LY_NOTI_EVENT_SINGLE || - noti_layout == NOTIFICATION_LY_NOTI_EVENT_MULTIPLE) { - *type = "SIMPLE"; - } else if (noti_layout == NOTIFICATION_LY_NOTI_THUMBNAIL) { - *type = "THUMBNAIL"; - } - } else if (noti_type == NOTIFICATION_TYPE_ONGOING) { - if (noti_layout == NOTIFICATION_LY_ONGOING_EVENT) { - *type = "ONGOING"; - } else if (noti_layout == NOTIFICATION_LY_ONGOING_PROGRESS) { - *type = "PROGRESS"; - } - } else { - return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, - "Notification type not found"); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::StatusTypeToPlatform( - const std::string& type, - notification_type_e* noti_type) { - LoggerD("Enter"); - if (type == "SIMPLE" || type == "THUMBNAIL") { - *noti_type = NOTIFICATION_TYPE_NOTI; - } else if (type == "ONGOING" || type == "PROGRESS") { - *noti_type = NOTIFICATION_TYPE_ONGOING; - } else { - return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, - "Invalide notification type", - ("Invalide noti type: %s", type.c_str())); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetImage( - notification_h noti_handle, - notification_image_type_e image_type, - std::string* image_path) { - LoggerD("Enter"); - char* path = NULL; - - *image_path = ""; - - if (notification_get_image(noti_handle, image_type, &path) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification image error", - ("Get notification image error, image_type: %d", image_type)); - } - if (path) { - *image_path = path; - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetImage( - notification_h noti_handle, - notification_image_type_e image_type, - const std::string& image_path) { - LoggerD("Enter"); - int ret = notification_set_image(noti_handle, image_type, image_path.c_str()); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification image error", - ("Set notification image error, image_type: %d, error: %d", - image_type, ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetText(notification_h noti_handle, - notification_text_type_e text_type, - std::string* noti_text) { - LoggerD("Enter"); - char* text = NULL; - - *noti_text = ""; - - if (notification_get_text(noti_handle, text_type, &text) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification text error", - ("Get notification text error, text_type: %d", text_type)); - } - - if (text) - *noti_text = text; - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetText(notification_h noti_handle, - notification_text_type_e text_type, - const std::string& noti_text) { - LoggerD("Enter"); - int ret = notification_set_text(noti_handle, - text_type, - noti_text.c_str(), - NULL, - NOTIFICATION_VARIABLE_TYPE_NONE); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification text error", - ("Set notification text error, text_type: %d, error: %d", - text_type, ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetNumber(notification_h noti_handle, - notification_text_type_e text_type, - long* number) { - LoggerD("Enter"); - std::string text; - PlatformResult status = GetText(noti_handle, text_type, &text); - if (status.IsError()) - return status; - - if (text.length()) - *number = std::stol(text); - else - *number = -1; - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetDetailInfos(notification_h noti_handle, - picojson::array* out) { - LoggerD("Enter"); - if (info_map_.size() != info_sub_map_.size()) { - return LogAndCreateResult( - ErrorCode::VALIDATION_ERR, - "Different notification information types element size"); - } - - picojson::value detail_info = picojson::value(picojson::object()); - picojson::object& detail_info_obj = detail_info.get(); - - std::string text; - size_t info_map_size = info_map_.size(); - for (size_t idx = 0; idx < info_map_size; ++idx) { - PlatformResult status = GetText(noti_handle, info_map_.at(idx), &text); - if (status.IsError()) - return status; - - if (!text.length()) - break; - - detail_info_obj["mainText"] = picojson::value(text); - - status = GetText(noti_handle, info_sub_map_.at(idx), &text); - if (status.IsError()) - return status; - - if (text.length()) { - detail_info_obj["subText"] = picojson::value(text); - } - - out->push_back(detail_info); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetDetailInfos( - notification_h noti_handle, - const picojson::array& value) { - LoggerD("Enter"); - size_t idx = 0; - - size_t info_map_size = info_map_.size(); - for (auto& item : value) { - const picojson::object& obj = JsonCast(item); - - PlatformResult status = - SetText(noti_handle, - info_map_.at(idx), - common::FromJson(obj, "mainText")); - if (status.IsError()) - return status; - - if (picojson::value(obj).contains("subText") && !IsNull(obj, "subText")) { - PlatformResult status = - SetText(noti_handle, - info_sub_map_.at(idx), - common::FromJson(obj, "subText")); - if (status.IsError()) - return status; - } - - ++idx; - - if (idx > info_map_size) { - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Too many values in notification detailInfo array"); - } - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetLedColor(notification_h noti_handle, - std::string* led_color) { - LoggerD("Enter"); - unsigned int color = 0; - notification_led_op_e type = NOTIFICATION_LED_OP_ON; - - if (notification_get_led(noti_handle, &type, (int*)&color) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification led displaying option error"); - } - - *led_color = ""; - std::stringstream stream; - - if (NOTIFICATION_LED_OP_OFF != type) { - color = 0x00FFFFFF & color; - stream << std::hex << color; - *led_color = "#" + stream.str(); - - while (led_color->length() < 7) { - led_color->insert(1, "0"); - } - - std::transform( - led_color->begin(), led_color->end(), led_color->begin(), ::tolower); - } - - LoggerD("color:%s", (*led_color).c_str()); - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetLedColor(notification_h noti_handle, - const std::string& led_color) { - LoggerD("Enter"); - std::string color_str = led_color; - std::transform( - color_str.begin(), color_str.end(), color_str.begin(), ::tolower); - - if (!IsColorFormatNumberic(color_str)) { - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Led color is not numeric value", - ("Led color is not numeric value: %s", color_str.c_str())); - } - - std::stringstream stream; - unsigned int color = 0; - notification_led_op_e type = NOTIFICATION_LED_OP_ON; - std::string color_code = - color_str.substr(1, color_str.length()).insert(0, "ff"); - - stream << std::hex << color_code; - stream >> color; - - if (color != 0) - type = NOTIFICATION_LED_OP_ON_CUSTOM_COLOR; - else - type = NOTIFICATION_LED_OP_OFF; - - int ret = notification_set_led(noti_handle, type, static_cast(color)); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification led color eror", - ("Set notification led color eror: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetLedPeriod(notification_h noti_handle, - unsigned long* on_period, - unsigned long* off_period) { - LoggerD("Enter"); - int on_time = 0; - int off_time = 0; - - if (notification_get_led_time_period(noti_handle, &on_time, &off_time) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification led on/off period error"); - } - - if (on_period) - *on_period = on_time; - if (off_period) - *off_period = off_time; - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetLedOnPeriod(notification_h noti_handle, - unsigned long on_period) { - LoggerD("Enter"); - unsigned long off_period = 0; - PlatformResult status = GetLedPeriod(noti_handle, nullptr, &off_period); - if (status.IsError()) - return status; - - int ret = - notification_set_led_time_period(noti_handle, on_period, off_period); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification led on period error", - ("Set notification led on period error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetLedOffPeriod(notification_h noti_handle, - unsigned long off_period) { - LoggerD("Enter"); - unsigned long on_period = 0; - PlatformResult status = GetLedPeriod(noti_handle, &on_period, nullptr); - if (status.IsError()) - return status; - - int ret = - notification_set_led_time_period(noti_handle, on_period, off_period); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification led off period error", - ("Set notification led off period error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetThumbnails(notification_h noti_handle, - picojson::array* out) { - LoggerD("Enter"); - std::string text; - size_t thumbnails_map_size = thumbnails_map_.size(); - for (size_t idx = 0; idx < thumbnails_map_size; ++idx) { - PlatformResult status = - GetImage(noti_handle, thumbnails_map_.at(idx), &text); - if (status.IsError()) - return status; - - if (!text.length()) - break; - - //CHECK GetVirtualPath ?? - out->push_back(picojson::value(common::FilesystemProvider::Create().GetVirtualPath(text))); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetThumbnails(notification_h noti_handle, - const picojson::array& value) { - LoggerD("Enter"); - size_t idx = 0; - - size_t thumbnails_map_size = thumbnails_map_.size(); - for (auto& item : value) { - const std::string& text = JsonCast(item); - std::string real_path = common::FilesystemProvider::Create().GetRealPath(text); - - PlatformResult status = - SetImage(noti_handle, thumbnails_map_.at(idx), real_path); - if (status.IsError()) - return status; - - ++idx; - - if (idx > thumbnails_map_size) { - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Too many values in notification thumbnail array"); - } - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetSoundPath(notification_h noti_handle, - std::string* sound_path) { - LoggerD("Enter"); - *sound_path = ""; - - const char* path = NULL; - notification_sound_type_e type = NOTIFICATION_SOUND_TYPE_NONE; - - if (notification_get_sound(noti_handle, &type, &path) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification sound error"); - } - - LoggerD("Sound type = %d", type); - - if (path && (type == NOTIFICATION_SOUND_TYPE_USER_DATA)) { - *sound_path = path; - } - - LoggerD("Sound path = %s", sound_path->c_str()); - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetSoundPath(notification_h noti_handle, - const std::string& sound_path) { - LoggerD("Enter"); - int ret = notification_set_sound( - noti_handle, NOTIFICATION_SOUND_TYPE_USER_DATA, sound_path.c_str()); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification sound error", - ("Set notification sound error: %d", ret)); - } - - LoggerD("Sound path = %s", sound_path.c_str()); - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetVibration(notification_h noti_handle, - bool* vibration) { - LoggerD("Enter"); - notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE; - - if (notification_get_vibration(noti_handle, &vib_type, NULL) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification vibration error"); - } - - if (NOTIFICATION_VIBRATION_TYPE_DEFAULT == vib_type || - NOTIFICATION_VIBRATION_TYPE_USER_DATA == vib_type) { - *vibration = true; - } else { - *vibration = false; - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetVibration(notification_h noti_handle, - bool vibration) { - LoggerD("Enter"); - bool platform_vibration; - PlatformResult status = GetVibration(noti_handle, &platform_vibration); - if (status.IsError()) - return status; - - if (platform_vibration != vibration) { - notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE; - - if (vibration) { - vib_type = NOTIFICATION_VIBRATION_TYPE_DEFAULT; - } - - int ret = notification_set_vibration(noti_handle, vib_type, NULL); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification vibration error", - ("Set notification vibration error: %d", ret)); - } - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetApplicationControl( - app_control_h app_handle, - picojson::object* out_ptr) { - LoggerD("Enter"); - picojson::object& out = *out_ptr; - - char* operation = NULL; - char* uri = NULL; - char* mime = NULL; - char* category = NULL; - SCOPE_EXIT { - free(operation); - free(uri); - free(mime); - free(category); - }; - - int ret = app_control_get_operation(app_handle, &operation); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get application control operation error", - ("Get application control operation error: %d", ret)); - } - if (operation) { - out["operation"] = picojson::value(operation); - LoggerD("operation = %s", operation); - } - - if (app_control_get_uri(app_handle, &uri) != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get application control uri error"); - } - if (uri) { - out["uri"] = picojson::value(uri); - LoggerD("uri = %s", uri); - } - - if (app_control_get_mime(app_handle, &mime) != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get application control mime error"); - } - if (mime) { - out["mime"] = picojson::value(mime); - LoggerD("mime = %s", mime); - } - - if (app_control_get_category(app_handle, &category) != - APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get application control category error"); - } - if (category) { - out["category"] = picojson::value(category); - LoggerD("category = %s", category); - } - - picojson::array app_control_data = picojson::array(); - if (app_control_foreach_extra_data( - app_handle, ServiceExtraDataCb, (void*)&app_control_data) != - APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get application control data error"); - } - out["data"] = picojson::value(app_control_data); - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetApplicationControl( - app_control_h app_handle, - const picojson::object& app_ctrl) { - LoggerD("Enter"); - picojson::value val(app_ctrl); - const std::string& operation = - common::FromJson(app_ctrl, "operation"); - - int ret; - if (operation.length()) { - ret = app_control_set_operation(app_handle, operation.c_str()); - } else { - ret = app_control_set_operation(app_handle, APP_CONTROL_OPERATION_DEFAULT); - } - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set application control operation error", - ("Set application control operation error: %d", ret)); - } - - if (val.contains("uri") && !IsNull(app_ctrl, "uri")) { - const std::string& uri = common::FromJson(app_ctrl, "uri"); - ret = app_control_set_uri(app_handle, uri.c_str()); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set application control uri error", - ("Set application control uri error: %d", ret)); - } - } - - if (val.contains("mime") && !IsNull(app_ctrl, "mime")) { - const std::string& mime = common::FromJson(app_ctrl, "mime"); - ret = app_control_set_mime(app_handle, mime.c_str()); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set application control mime error", - ("Set application control mime error: %d", ret)); - } - } - - if (val.contains("category") && !IsNull(app_ctrl, "category")) { - const std::string& category = - common::FromJson(app_ctrl, "category"); - ret = app_control_set_category(app_handle, category.c_str()); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set application control category error", - ("Set application control category error: %d", ret)); - } - } - - if (!picojson::value(app_ctrl).contains("data") || IsNull(app_ctrl, "data")) { - return PlatformResult(ErrorCode::NO_ERROR); - } - - auto& items = common::FromJson(app_ctrl, "data"); - - int idx = 0; - - for (auto item : items) { - const picojson::object& obj = JsonCast(item); - const std::string key = common::FromJson(obj, "key"); - const picojson::array values = - common::FromJson(obj, "value"); - const char** arrayValue = - (const char**)calloc(sizeof(char*), values.size()); - SCOPE_EXIT { free(arrayValue); }; - idx = 0; - for (auto& item : values) { - arrayValue[idx] = JsonCast(item).c_str(); - ++idx; - } - ret = app_control_add_extra_data_array( - app_handle, key.c_str(), arrayValue, values.size()); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set application control extra data error", - ("Set application control extra data error: %d", ret)); - } - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetApplicationId(app_control_h app_handle, - std::string* app_id) { - LoggerD("Enter"); - char* app_id_str = NULL; - SCOPE_EXIT { free(app_id_str); }; - - *app_id = ""; - - if (app_control_get_app_id(app_handle, &app_id_str) != - APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Get applicaiton ID failed"); - } - - if (app_id_str != NULL) { - *app_id = app_id_str; - } - - LoggerD("Get appId = %s", /*(*app_id).c_str()*/ app_id_str); - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetApplicationId(app_control_h app_handle, - const std::string& app_id) { - LoggerD("Enter"); - int ret = app_control_set_app_id(app_handle, app_id.c_str()); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Set applicaiton ID error", - ("Set applicaiton ID error: %d", ret)); - } - - LoggerD("Set appId = %s", app_id.c_str()); - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetProgressValue( - notification_h noti_handle, - const std::string& progess_type, - double* progress_value) { - LoggerD("Enter"); - double tmp_progress_value = 0.0; - - if (progess_type == kProgressTypeByte) { - if (notification_get_size(noti_handle, &tmp_progress_value) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification size error"); - } - } else if (progess_type == kProgressTypePercentage) { - if (notification_get_progress(noti_handle, &tmp_progress_value) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification progress error"); - } - // native api uses range 0-1, but webapi expects 0-100, so we need to multiply result with 100 - tmp_progress_value *= 100; - } else { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Unknown notification progress type", - ("Unknown notification progress type: %s ", progess_type.c_str())); - } - - LOGGER(DEBUG) << "Progress " << progess_type << " = " << tmp_progress_value; - - *progress_value = tmp_progress_value; - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetProgressValue( - notification_h noti_handle, - const std::string& progress_type, - double progress_value, - bool is_update) { - LoggerD("Enter"); - int ret; - - if (progress_type == kProgressTypeByte) { - ret = notification_set_size(noti_handle, progress_value); - - if (is_update) { - ret = notification_update_size(noti_handle, NOTIFICATION_PRIV_ID_NONE, - progress_value); - } - } else if (progress_type == kProgressTypePercentage) { - // native api uses range 0-1, but webapi expects 0-100, so we need to divide by 100 - ret = notification_set_progress(noti_handle, progress_value/100); - - if (is_update) { - ret = notification_update_progress(noti_handle, NOTIFICATION_PRIV_ID_NONE, - progress_value); - } - } else { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Unknown notification progress type", - ("Unknown notification progress type: %s ", progress_type.c_str())); - } - - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Set notification progress/size error", - ("Set notification progress/size error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetPostedTime(notification_h noti_handle, - time_t* posted_time) { - LoggerD("Enter"); - *posted_time = 0; - - if (notification_get_insert_time(noti_handle, posted_time) != - NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Get notification posted time error"); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetNotiHandle(int id, - notification_h* noti_handle) { - LoggerD("Enter"); - *noti_handle = notification_load(NULL, id); - if (NULL == *noti_handle) { - return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, - "Not found or removed notification id"); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::GetAppControl(notification_h noti_handle, - app_control_h* app_control) { - LoggerD("Enter"); - int ret = - notification_get_launch_option(noti_handle, - NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, - static_cast(app_control)); - if (ret != NOTIFICATION_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Notification get launch option error", - ("Notification get launch option error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::CreateAppControl( - app_control_h* app_control) { - LoggerD("Enter"); - int ret = app_control_create(app_control); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Application create error", - ("Application create error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - -PlatformResult StatusNotification::SetAppControl(notification_h noti_handle, - app_control_h app_control) { - LoggerD("Enter"); - int ret = - notification_set_launch_option(noti_handle, - NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, - static_cast(app_control)); - if (ret != APP_CONTROL_ERROR_NONE) { - return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Notification set launch option error", - ("Notification set launch option error: %d", ret)); - } - - return PlatformResult(ErrorCode::NO_ERROR); -} - PlatformResult StatusNotification::ToJson(int id, notification_h noti_handle, app_control_h app_handle, @@ -994,16 +51,16 @@ PlatformResult StatusNotification::ToJson(int id, int ret = notification_get_type(noti_handle, ¬i_type); if (ret != NOTIFICATION_ERROR_NONE) { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Notification get type error", - ("Notification get type error: %d", ret)); + "Notification get type error", + ("Notification get type error: %d", ret)); } notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE; ret = notification_get_layout(noti_handle, ¬i_layout); if (ret != NOTIFICATION_ERROR_NONE) { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Notification get layout error", - ("Notification get layout error: %d", ret)); + "Notification get layout error", + ("Notification get layout error: %d", ret)); } std::string noti_type_str; @@ -1064,7 +121,8 @@ PlatformResult StatusNotification::ToJson(int id, if (status.IsError()) return status; if (value_str.length()) { - out["backgroundImagePath"] = picojson::value(common::FilesystemProvider::Create().GetVirtualPath(value_str)); + out["backgroundImagePath"] = + picojson::value(common::FilesystemProvider::Create().GetVirtualPath(value_str)); } picojson::array thumbnails = picojson::array(); @@ -1147,8 +205,8 @@ PlatformResult StatusNotification::ToJson(int id, } PlatformResult StatusNotification::GetNotiHandleFromJson(const picojson::object& args, - bool is_update, - notification_h *noti_handle){ + bool is_update, + notification_h *noti_handle){ LoggerD("Enter"); picojson::object noti_obj = common::FromJson(args, "notification"); @@ -1278,8 +336,7 @@ PlatformResult StatusNotification::GetNotiHandleFromJson(const picojson::object& } } - status = - SetVibration(*noti_handle, common::FromJson(noti_obj, "vibration")); + status = SetVibration(*noti_handle, common::FromJson(noti_obj, "vibration")); if (status.IsError()) { return status; } @@ -1322,7 +379,7 @@ PlatformResult StatusNotification::GetNotiHandleFromJson(const picojson::object& } status = SetProgressValue(*noti_handle, progress_type, progressValue, - is_update); + is_update); if (status.IsError()) { return status; @@ -1373,13 +430,13 @@ PlatformResult StatusNotification::FromJson(const picojson::object& args, ret = notification_insert(noti_handle, &id); if (NOTIFICATION_ERROR_NONE != ret) { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, - "Cannot insert notification"); + "Cannot insert notification"); } } if (ret != NOTIFICATION_ERROR_NONE) { return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, - "Post/Update notification error", - ("Post/Update notification error: %d", ret)); + "Post/Update notification error", + ("Post/Update notification error: %d", ret)); } time_t posted_time; diff --git a/src/notification/status_notification.h b/src/notification/status_notification.h index 896abee2..fccf0029 100644 --- a/src/notification/status_notification.h +++ b/src/notification/status_notification.h @@ -25,13 +25,15 @@ #include "common/XW_Extension.h" +#include "notification/common_notification.h" + namespace extension { namespace notification { typedef std::map InformationEnumMap; typedef std::map ImageEnumMap; -class StatusNotification { +class StatusNotification : public CommonNotification { public: XW_EXPORT static common::PlatformResult ToJson(int id, notification_h noti_handle, @@ -43,98 +45,9 @@ class StatusNotification { static common::PlatformResult FromJson(const picojson::object& args, bool is_update, picojson::object* out_ptr); - XW_EXPORT static common::PlatformResult GetAppControl(notification_h noti_handle, - app_control_h* app_control); - static common::PlatformResult GetNotiHandle(int id, - notification_h* noti_handle); - XW_EXPORT static common::PlatformResult SetAppControl(notification_h noti_handle, - app_control_h app_control); - private: StatusNotification(); virtual ~StatusNotification(); - - static const InformationEnumMap info_map_; - static const InformationEnumMap info_sub_map_; - static const ImageEnumMap thumbnails_map_; - - static common::PlatformResult StatusTypeFromPlatform( - notification_type_e noti_type, - notification_ly_type_e noti_layout, - std::string* type); - static common::PlatformResult StatusTypeToPlatform( - const std::string& type, - notification_type_e* noti_type); - static common::PlatformResult Create(notification_type_e noti_type, - notification_h* noti_handle); - static common::PlatformResult GetImage(notification_h noti_handle, - notification_image_type_e image_type, - std::string* image_path); - static common::PlatformResult SetImage(notification_h noti_handle, - notification_image_type_e image_type, - const std::string& image_path); - static common::PlatformResult GetText(notification_h noti_handle, - notification_text_type_e text_type, - std::string* noti_text); - static common::PlatformResult SetText(notification_h noti_handle, - notification_text_type_e text_type, - const std::string& noti_text); - static common::PlatformResult GetNumber(notification_h noti_handle, - notification_text_type_e text_type, - long* number); - static common::PlatformResult GetDetailInfos(notification_h noti_handle, - picojson::array* out); - static common::PlatformResult SetDetailInfos(notification_h noti_handle, - const picojson::array& value); - static common::PlatformResult GetLedColor(notification_h noti_handle, - std::string* led_color); - static common::PlatformResult SetLedColor(notification_h noti_handle, - const std::string& led_color); - static common::PlatformResult GetLedPeriod(notification_h noti_handle, - unsigned long* on_period, - unsigned long* off_period); - static common::PlatformResult SetLedOnPeriod(notification_h noti_handle, - unsigned long on_period); - static common::PlatformResult SetLedOffPeriod(notification_h noti_handle, - unsigned long off_period); - static common::PlatformResult GetThumbnails(notification_h noti_handle, - picojson::array* out); - static common::PlatformResult SetThumbnails(notification_h noti_handle, - const picojson::array& value); - static common::PlatformResult GetSoundPath(notification_h noti_handle, - std::string* sound_path); - static common::PlatformResult SetSoundPath(notification_h noti_handle, - const std::string& sound_path); - static common::PlatformResult GetVibration(notification_h noti_handle, - bool* vibration); - static common::PlatformResult SetVibration(notification_h noti_handle, - bool vibration); - static common::PlatformResult GetApplicationControl( - app_control_h app_handle, - picojson::object* out_ptr); - static common::PlatformResult SetApplicationControl( - app_control_h app_handle, - const picojson::object& app_ctrl); - static common::PlatformResult GetApplicationId(app_control_h app_handle, - std::string* app_id); - static common::PlatformResult SetApplicationId(app_control_h app_handle, - const std::string& app_id); - static common::PlatformResult GetProgressValue( - notification_h noti_handle, - const std::string& progess_type, - double* progress_value); - static common::PlatformResult SetProgressValue( - notification_h noti_handle, - const std::string& progress_type, - double progress_value, - bool is_update); - static common::PlatformResult GetPostedTime(notification_h noti_handle, - time_t* posted_time); - static common::PlatformResult SetLayout(notification_h noti_handle, - const std::string& noti_type); - static common::PlatformResult CreateAppControl(app_control_h* app_control); - - static bool IsColorFormatNumberic(const std::string& color); }; } // namespace notification -- 2.34.1