From: Tomasz Marciniak Date: Mon, 25 Apr 2016 08:04:52 +0000 (+0200) Subject: [HAM] Added error callback to start() function. X-Git-Tag: submit/tizen/20160426.030601^2~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e7ff3a817ddee66b2279e89c7431268aa564fce0;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [HAM] Added error callback to start() function. [Verification] Code compiles. Tested on emulator. When location is off error callback is called. Change-Id: Icd7fc9fe39fe45feb98eec48ec1c7c0c40b10d2a Signed-off-by: Tomasz Marciniak --- diff --git a/src/humanactivitymonitor/humanactivitymonitor_api.js b/src/humanactivitymonitor/humanactivitymonitor_api.js index e71b6a7b..6ba81b7c 100755 --- a/src/humanactivitymonitor/humanactivitymonitor_api.js +++ b/src/humanactivitymonitor/humanactivitymonitor_api.js @@ -225,10 +225,18 @@ function pedometerCallback(result) { } } +var GPSListener = null; +function GPSCallback(result) { + if (GPSListener) { + GPSListener(result); + } +} + HumanActivityMonitorManager.prototype.start = function(type, changedCallback) { var args = validator_.validateArgs(arguments, [ {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}, {name: 'changedCallback', type: types_.FUNCTION, optional: true, nullable: true}, + {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}, {name : 'option', type : types_.DICTIONARY, optional : true, nullable : true} ]); @@ -260,9 +268,19 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) { break; } - var listener = HumanActivityType.PEDOMETER === args.type ? pedometerCallback : function(result) { - native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result)); - }; + var listener = null; + switch (args.type) { + case HumanActivityType.PEDOMETER: + listener = pedometerCallback; + break; + case HumanActivityType.GPS: + listener = GPSCallback; + break; + default: + listener = function(result) { + native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result)); + }; + } console.log("callbackInterval = " + callbackInterval + ", sampleInterval = " + sampleInterval); startListener(listenerId, @@ -278,6 +296,18 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) { if (HumanActivityType.PEDOMETER === args.type) { pedometerListener = args.changedCallback; } + + if (HumanActivityType.GPS === args.type) { + var callback = function(result) { + if (native_.isFailure(result)) { + native_.callIfPossible(args.errorCallback, native_.getErrorObject(result)); + } else { + native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result)); + } + }; + + GPSListener = callback; + } }; HumanActivityMonitorManager.prototype.stop = function(type) { @@ -292,6 +322,10 @@ HumanActivityMonitorManager.prototype.stop = function(type) { if (HumanActivityType.PEDOMETER === args.type) { pedometerListener = null; } + + if (HumanActivityType.GPS === args.type) { + GPSListener = null; + } }; HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function() { diff --git a/src/humanactivitymonitor/humanactivitymonitor_manager.cc b/src/humanactivitymonitor/humanactivitymonitor_manager.cc index 12224d3a..b52282c5 100755 --- a/src/humanactivitymonitor/humanactivitymonitor_manager.cc +++ b/src/humanactivitymonitor/humanactivitymonitor_manager.cc @@ -577,6 +577,15 @@ class HumanActivityMonitorManager::Monitor::GpsMonitor : public HumanActivityMon int sample_interval = static_cast(args.get(kSampleInterval).get() / 1000); LoggerD("callbackInterval: %d, sampleInterval: %d", callback_interval, sample_interval); + ret = location_manager_set_setting_changed_cb(LOCATIONS_METHOD_GPS, + OnGpsSettingEvent, + this); + if (LOCATIONS_ERROR_NONE != ret) { + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Failed to set setting listener", + ("Failed to set setting listener, error: %d (%s)", ret, get_error_message(ret))); + } + ret = location_manager_set_location_batch_cb(handle_, OnGpsEvent, sample_interval, // batch_interval @@ -660,6 +669,34 @@ class HumanActivityMonitorManager::Monitor::GpsMonitor : public HumanActivityMon } private: + static void OnGpsSettingEvent(location_method_e method, bool enable, void *user_data) { + ScopeLogger(); + + if (LOCATIONS_METHOD_GPS != method) { + LoggerD("Location method different from GPS"); + return; + } + + auto monitor = static_cast(user_data); + auto& callback = monitor->event_callback(); + + if (!callback) { + LOGGER(ERROR) << "No GPS event callback registered, skipping."; + return; + } + + if (!enable) { + picojson::value val{picojson::object{}}; + auto& obj = val.get(); + + LogAndReportError( + PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "GPS service is not available"), + &obj, ("GPS service is not available")); + + callback(&val); + } + } + static void OnGpsEvent(int num_of_location, void* user_data) { ScopeLogger();