From 18afa4ce89f133e378f7e0bb0f26b4b4de9d71d3 Mon Sep 17 00:00:00 2001 From: Szymon Jastrzebski Date: Mon, 13 Mar 2017 11:31:34 +0100 Subject: [PATCH] [Alarm] Fixing AlarmRelative/Absolute .period When alarm.period wasn't set by user, tizen.alarm.getAll()/get() should set alarm.period to null, not 0. After adding alarm period is updated. [Verification] TCTs passed 59/1/0/0 Failing test case needs to be fixed Change-Id: I800477b4cefbdfe152b7abb0db750b6713cd0ed3 Signed-off-by: Szymon Jastrzebski --- src/alarm/alarm_api.js | 89 +++++++++++++++++++++++++++++++++++++++++----- src/alarm/alarm_manager.cc | 45 ++++++++++++++++++----- 2 files changed, 117 insertions(+), 17 deletions(-) diff --git a/src/alarm/alarm_api.js b/src/alarm/alarm_api.js index 78f22ad..47725dc 100755 --- a/src/alarm/alarm_api.js +++ b/src/alarm/alarm_api.js @@ -30,6 +30,20 @@ var AlarmManager = function () { }); }; +var EditManager = function() { + this.canEdit = false; +}; + +EditManager.prototype.allow = function() { + this.canEdit = true; +}; + +EditManager.prototype.disallow = function() { + this.canEdit = false; +}; + +var _edit = new EditManager(); + function InternalData_(data) { if (!(this instanceof InternalData_)) { return new InternalData_(data); @@ -90,12 +104,15 @@ AlarmManager.prototype.add = function () { callArgs.type = type; callArgs.seconds = Converter.toString(seconds); + callArgs.isPeriodSet = !T.isNullOrUndefined(args.alarm.period); var result = native.callSync('AlarmManager_add', callArgs); if (native.isFailure(result)) { throw native.getErrorObject(result); } else { + _edit.allow(); UpdateInternalData_(args.alarm, native.getResultObject(result)); + _edit.disallow(); } }; @@ -202,16 +219,38 @@ tizen.AlarmRelative = function(delay, period, internal) { var m_delay = Converter.toLong(delay); if (arguments.length >= 2) { - m_period = Converter.toLong(period, true); + if(!T.isNullOrUndefined(period)){ + m_period = Converter.toLong(period, true); + } } Alarm.call(this, internal); Object.defineProperties(this, { - delay: { value: m_delay, writable: false, enumerable: true}, - period: { value: m_period, writable: false, enumerable: true} + delay: { + get: function() { + return m_delay; + }, + set: function(v) { + if (_edit.canEdit && v) { + m_delay = Converter.toLong(v.delay); + } + }, + enumerable: true + }, + period: { + get: function() { + return m_period; + }, + set: function(v) { + if (_edit.canEdit && v) { + m_period = Converter.toLong(v.period); + } + }, + enumerable: true + } }); -} +}; tizen.AlarmRelative.prototype = new Alarm(); @@ -261,7 +300,9 @@ tizen.AlarmAbsolute = function(date, second, internal) { if(T.isArray(second)){ m_daysOfWeek = second; } else { - m_period = Converter.toLong(second); + if(!T.isNullOrUndefined(second)){ + m_period = Converter.toLong(second); + } } } @@ -271,11 +312,41 @@ tizen.AlarmAbsolute = function(date, second, internal) { } makeDateConst(m_date); Object.defineProperties(this, { - date: { value: m_date, writable: false, enumerable: true}, - period: { value: m_period, writable: false, enumerable: true}, - daysOfTheWeek: { value: m_daysOfWeek, writable: false, enumerable: true} + date: { + get: function() { + return m_date; + }, + set: function(v) { + if (_edit.canEdit && T.isDate(v.date)) { + m_date = v.date; + } + }, + enumerable: true + }, + period: { + get: function() { + return m_period; + }, + set: function(v) { + if (_edit.canEdit && v) { + m_period = Converter.toLong(v.period); + } + }, + enumerable: true + }, + daysOfTheWeek: { + get: function() { + return m_daysOfWeek; + }, + set: function(v) { + if (_edit.canEdit && T.isArray(v.second)) { + m_daysOfWeek = v.second; + } + }, + enumerable: true + } }); -} +}; tizen.AlarmAbsolute.prototype = new Alarm(); diff --git a/src/alarm/alarm_manager.cc b/src/alarm/alarm_manager.cc index 5bc8d84..3dc9f21 100755 --- a/src/alarm/alarm_manager.cc +++ b/src/alarm/alarm_manager.cc @@ -111,6 +111,11 @@ void AlarmManager::Add(const picojson::value& args, picojson::object& out) { app_control_set_app_id(app_control, app_id.c_str()); int alarm_id = 0; + int period = 0; + + // result object + picojson::value result = picojson::value(picojson::object()); + picojson::object& result_obj = result.get(); if (kAlarmRelative == alarm_type) { app_control_add_extra_data(app_control, kAlarmKeyType, kAlarmTypeValueRelative); @@ -124,11 +129,15 @@ void AlarmManager::Add(const picojson::value& args, picojson::object& out) { } int delay = static_cast(it_delay->second.get()); - int period = 0; if (it_period->second.is()) { period = static_cast(it_period->second.get()); } + bool isPeriodSet = false; + if (args.contains("isPeriodSet")) { + isPeriodSet = args.get("isPeriodSet").get(); + } + std::string delay_str = std::to_string(delay); int ret = app_control_add_extra_data(app_control, kAlarmRelativeDelayKey, delay_str.c_str()); if (APP_CONTROL_ERROR_NONE != ret) { @@ -138,7 +147,7 @@ void AlarmManager::Add(const picojson::value& args, picojson::object& out) { return; } - if(period == 0){ + if(!isPeriodSet){ ret = alarm_schedule_once_after_delay(app_control, delay, &alarm_id); }else{ ret = alarm_schedule_after_delay(app_control, delay, period, &alarm_id); @@ -149,6 +158,17 @@ void AlarmManager::Add(const picojson::value& args, picojson::object& out) { ("Error while add alarm to server: %d (%s)", ret, get_error_message(ret))); return; } + + ret = alarm_get_scheduled_period(alarm_id, &period); + if (ALARM_ERROR_NONE != ret) { + LogAndReportError(PlatformResult( + ErrorCode::UNKNOWN_ERR, "Unknown error occurred."), &out, + ("Unknown error occurred: %d (%s)", ret, get_error_message(ret))); + return; + } + if(period != 0){ + result_obj.insert(std::make_pair("period", picojson::value(std::to_string(period)))); + } } else { app_control_add_extra_data(app_control, kAlarmKeyType, kAlarmTypeValueAbsolute); @@ -183,8 +203,17 @@ void AlarmManager::Add(const picojson::value& args, picojson::object& out) { if (alarm.end() != it_period && it_period->second.is()) { app_control_add_extra_data( app_control, kAlarmAbsoluteRecurrenceTypeKey, kAlarmAbsoluteReccurrenceTypeInterval); - int period = static_cast(it_period->second.get()); + period = static_cast(it_period->second.get()); ret = alarm_schedule_at_date(app_control, &start_date, period, &alarm_id); + if (ALARM_ERROR_NONE != ret) { + LogAndReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Adding alarm to server failed."), + &out, + ("Adding alarm to server failed: %d (%s)", ret, get_error_message(ret))); + return; + } + + ret = alarm_get_scheduled_period(alarm_id, &period); + result_obj.insert(std::make_pair("period", picojson::value(std::to_string(period)))); } else if (alarm.end() != it_daysOfTheWeek && it_daysOfTheWeek->second.is() && !(it_daysOfTheWeek->second.get()).empty()) { app_control_add_extra_data( @@ -229,10 +258,6 @@ void AlarmManager::Add(const picojson::value& args, picojson::object& out) { } } - // result object - picojson::value result = picojson::value(picojson::object()); - picojson::object& result_obj = result.get(); - result_obj.insert(std::make_pair("id", picojson::value(std::to_string(alarm_id)))); ReportSuccess(result, out); } @@ -397,7 +422,11 @@ PlatformResult AlarmManager::GetAlarm(int id, picojson::object& obj) { obj.insert(std::make_pair("type", picojson::value(kAlarmRelative))); obj.insert(std::make_pair("delay", picojson::value(delay_string))); - obj.insert(std::make_pair("period", picojson::value(std::to_string(interval)))); + if (interval != 0) { + //according to documentation interval will not be lower than 600, + //thus 0 from native means period wasn't set by user + obj.insert(std::make_pair("period", picojson::value(std::to_string(interval)))); + } } else { return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown error occurred."); } -- 2.7.4