From: Piotr Kosko Date: Tue, 21 Feb 2017 13:12:19 +0000 (+0100) Subject: [Notification] added template feature X-Git-Tag: submit/tizen/20170308.110622~1^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F07%2F116007%2F1;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Notification] added template feature [Feature] Template functionality was added to the notification module. Currently it is possible to save notification as a template and then new notifications could be created basing on the template. [Verification] Code compiles without errors. TCT passrate is 100%. The feature was tested by hand in chrome console. Change-Id: I8fc68f46b82165aa6c1a92750b3d87506611177c Signed-off-by: Piotr Kosko --- diff --git a/src/notification/notification_api.js b/src/notification/notification_api.js index 21bcadf5..5c7ce375 100644 --- a/src/notification/notification_api.js +++ b/src/notification/notification_api.js @@ -242,6 +242,42 @@ 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} + ]); + + var result = native_.callSync('NotificationManager_saveNotificationAsTemplate', args); + + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } +}; + +NotificationManager.prototype.createNotificationFromTemplate = function(name) { + var args = validator_.validateArgs(arguments, [ + {name: 'name', type: types_.STRING} + ]); + + if (!arguments.length) { + throw new WebAPIException(WebAPIException.NOT_FOUND_ERR); + } + + var result = native_.callSync('NotificationManager_createNotificationFromTemplate', args); + + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + var n = native_.getResultObject(result); + + _edit.allow(); + var returnObject = new StatusNotification(n.statusType, n.title, n); + _edit.disallow(); + + return returnObject; +}; + function NotificationInitDict(data) { var _iconPath = null; diff --git a/src/notification/notification_instance.cc b/src/notification/notification_instance.cc index ec92eb59..d4f06dac 100644 --- a/src/notification/notification_instance.cc +++ b/src/notification/notification_instance.cc @@ -52,6 +52,8 @@ NotificationInstance::NotificationInstance() { NotificationManagerPlayLEDCustomEffect); REGISTER_SYNC("NotificationManager_stopLEDCustomEffect", NotificationManagerStopLEDCustomEffect); + REGISTER_SYNC("NotificationManager_saveNotificationAsTemplate", NotificationManagerSaveTemplate); + REGISTER_SYNC("NotificationManager_createNotificationFromTemplate", NotificationManagerCreateFromTemplate); #undef REGISTER_SYNC manager_ = NotificationManager::GetInstance(); @@ -188,6 +190,37 @@ void NotificationInstance::NotificationManagerStopLEDCustomEffect( } } +void NotificationInstance::NotificationManagerSaveTemplate(const picojson::value& args, + picojson::object& out) { + LoggerD("Enter"); + CHECK_PRIVILEGE_ACCESS(kPrivilegeNotification, &out); + + PlatformResult status = manager_->SaveTemplate(args.get()); + + if (status.IsSuccess()) { + ReportSuccess(out); + } else { + LogAndReportError(status, &out); + } +} + + +void NotificationInstance::NotificationManagerCreateFromTemplate(const picojson::value& args, + picojson::object& out) { + LoggerD("Enter"); + CHECK_PRIVILEGE_ACCESS(kPrivilegeNotification, &out); + picojson::value val{picojson::object{}}; + + PlatformResult status = + manager_->CreateFromTemplate(args.get(), val.get()); + + if (status.IsSuccess()) { + ReportSuccess(val, out); + } else { + LogAndReportError(status, &out); + } +} + #undef CHECK_EXIST } // namespace notification diff --git a/src/notification/notification_instance.h b/src/notification/notification_instance.h index 3a1e2e76..4eef4f84 100644 --- a/src/notification/notification_instance.h +++ b/src/notification/notification_instance.h @@ -48,6 +48,10 @@ class NotificationInstance : public common::ParsedInstance { picojson::object& out); void NotificationManagerStopLEDCustomEffect(const picojson::value& args, picojson::object& out); + void NotificationManagerSaveTemplate(const picojson::value& args, + picojson::object& out); + void NotificationManagerCreateFromTemplate(const picojson::value& args, + picojson::object& out); }; } // namespace notification diff --git a/src/notification/notification_manager.cc b/src/notification/notification_manager.cc index 93f0c090..afaae6e5 100644 --- a/src/notification/notification_manager.cc +++ b/src/notification/notification_manager.cc @@ -244,5 +244,64 @@ PlatformResult NotificationManager::StopLEDCustomEffect() { return PlatformResult(ErrorCode::NO_ERROR); } +common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) { + LoggerD("Enter"); + std::string name = FromJson(args, "name"); + notification_h noti_handle = nullptr; + int ret; + SCOPE_EXIT { + notification_free(noti_handle); + }; + + PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, ¬i_handle); + + if (status.IsError()){ + return status; + } + + ret = notification_save_as_template(noti_handle, name.c_str()); + if (ret != NOTIFICATION_ERROR_NONE) { + LoggerD("Error: %d (%s)", ret, get_error_message(ret)); + if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) { + return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR, + "Maximum number of templates exceeded", + ("Maximum number of templates exceeded, error: %d", ret)); + } else { + return LogAndCreateResult(ErrorCode::ABORT_ERR, + "Saving template failed", + ("Saving template failed, error: %d", ret)); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + + +common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args, + picojson::object& out) { + LoggerD("Enter"); + std::string name = FromJson(args, "name"); + + notification_h noti_handle = nullptr; + noti_handle = notification_create_from_template(name.c_str()); + if (!noti_handle) { + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, + "The template with given name not found", + ("The template with given name not found, handle is NULL")); + } + + SCOPE_EXIT { + notification_free(noti_handle); + }; + + PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out); + if (status.IsError()) + { + LoggerE("Failed: ToJson"); + return status; + } + return PlatformResult(ErrorCode::NO_ERROR); +} + } // namespace notification } // namespace extension diff --git a/src/notification/notification_manager.h b/src/notification/notification_manager.h index eaeac9f3..85cd85fa 100644 --- a/src/notification/notification_manager.h +++ b/src/notification/notification_manager.h @@ -41,6 +41,10 @@ class NotificationManager { common::PlatformResult PlayLEDCustomEffect(const picojson::object& args); common::PlatformResult StopLEDCustomEffect(); + common::PlatformResult SaveTemplate(const picojson::object& args); + common::PlatformResult CreateFromTemplate(const picojson::object& args, + picojson::object& out); + private: NotificationManager(); virtual ~NotificationManager(); diff --git a/src/notification/status_notification.cc b/src/notification/status_notification.cc index c6d7bf35..e1ace9d1 100644 --- a/src/notification/status_notification.cc +++ b/src/notification/status_notification.cc @@ -1088,19 +1088,21 @@ PlatformResult StatusNotification::ToJson(int id, return status; out["vibration"] = picojson::value(vibration); - picojson::object app_control = picojson::object(); - status = GetApplicationControl(app_handle, &app_control); - if (status.IsError()) - return status; - if (app_control.size()) { - out["appControl"] = picojson::value(app_control); - } + if (app_handle) { + picojson::object app_control = picojson::object(); + status = GetApplicationControl(app_handle, &app_control); + if (status.IsError()) + return status; + if (app_control.size()) { + out["appControl"] = picojson::value(app_control); + } - status = GetApplicationId(app_handle, &value_str); - if (status.IsError()) - return status; - if (value_str.length()) { - out["appId"] = picojson::value(value_str); + status = GetApplicationId(app_handle, &value_str); + if (status.IsError()) + return status; + if (value_str.length()) { + out["appId"] = picojson::value(value_str); + } } std::string progress_type;