throw new WebAPIException(WebAPIException.NOT_SUPPORTED_ERR);
}
- var listenerId ='HumanActivityMonitor_' + args.type;
+ var listenerId = 'HumanActivityMonitor_' + args.type;
if (!native_.isListenerSet(listenerId)) {
throw new WebAPIException(WebAPIException.SERVICE_NOT_AVAILABLE_ERR);
}
convertActivityData(args.type, native_.getResultObject(result)));
};
- native_.call('HumanActivityMonitorManager_getHumanActivityData', data, callback);
-};
-
-HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
- // TODO(r.galka) check access
- // HRM - http://tizen.org/privilege/healthinfo
- // GPS - http://tizen.org/privilege/location
-
- var args = validator_.validateArgs(arguments, [
- {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
- {name: 'changedCallback', type: types_.FUNCTION, optional: true, nullable: true}
- ]);
+ var result = native_.call('HumanActivityMonitorManager_getHumanActivityData', data, callback);
- var listenerId ='HumanActivityMonitor_' + args.type;
-
- var data = {
- type: args.type,
- listenerId: listenerId
- };
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+function startListener(listenerId, listener, method, data) {
if (!native_.isListenerSet(listenerId)) {
- var result = native_.callSync('HumanActivityMonitorManager_start', data);
+ var result = native_.callSync(method, data);
if (native_.isFailure(result)) {
throw native_.getErrorObject(result);
}
}
- var listener = function(result) {
- native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result));
- };
+ // always set the listener, if it's another call to startListener() overwrite the old one
native_.addListener(listenerId, listener);
-};
-
-HumanActivityMonitorManager.prototype.stop = function(type) {
- var args = validator_.validateArgs(arguments, [
- {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
- ]);
-
- var data = {
- type: args.type
- };
+}
- var listenerId ='HumanActivityMonitor_' + args.type;
+function checkPrivilegesForMethod(method, type) {
+ utils_.checkPrivilegeAccess(utils_.privilege.HEALTHINFO);
+ if ('HumanActivityMonitorManager_stop' === method && 'GPS' === type) {
+ utils_.checkPrivilegeAccess(utils_.privilege.LOCATION);
+ }
+}
+function stopListener(listenerId, method, data) {
if (!native_.isListenerSet(listenerId)) {
+ checkPrivilegesForMethod(method, data.type);
return;
}
- var result = native_.callSync('HumanActivityMonitorManager_stop', data);
+ var result = native_.callSync(method, data);
if (native_.isFailure(result)) {
throw native_.getErrorObject(result);
}
native_.removeListener(listenerId);
-};
+}
-HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function(changeCallback) {
+HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
var args = validator_.validateArgs(arguments, [
- {name: 'changeCallback', type: types_.FUNCTION}
+ {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
+ {name: 'changedCallback', type: types_.FUNCTION, optional: true, nullable: true}
]);
- var data = {
- };
+ var listenerId = 'HumanActivityMonitor_' + args.type;
- var callback = function(result) {
- native_.callIfPossible(args.changeCallback);
- };
+ startListener(listenerId,
+ function(result) {
+ native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result));
+ },
+ 'HumanActivityMonitorManager_start',
+ { type: args.type, listenerId: listenerId });
+};
- native_.call('HumanActivityMonitorManager_setAccumulativePedometerListener', data, callback);
+HumanActivityMonitorManager.prototype.stop = function(type) {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
+ ]);
+
+ stopListener('HumanActivityMonitor_' + args.type,
+ 'HumanActivityMonitorManager_stop',
+ { type: args.type });
};
-HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
+var accumulativePedometerListenerId = 'HumanActivityMonitor_AccumulativePedometerListener';
- var data = {
- };
+HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function(changeCallback) {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'changeCallback', type: types_.FUNCTION}
+ ]);
- var result = native_.callSync(
- 'HumanActivityMonitorManager_unsetAccumulativePedometerListener', data);
+ var listenerId = accumulativePedometerListenerId;
- if (native_.isFailure(result)) {
- throw native_.getErrorObject(result);
- }
+ startListener(listenerId,
+ function(result) {
+ args.changeCallback(convertActivityData(HumanActivityType.PEDOMETER, result));
+ },
+ 'HumanActivityMonitorManager_setAccumulativePedometerListener',
+ { listenerId: listenerId });
+};
+
+HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
+ stopListener(accumulativePedometerListenerId,
+ 'HumanActivityMonitorManager_unsetAccumulativePedometerListener',
+ {});
};
function StepDifference() {
#include "common/logger.h"
#include "common/platform_result.h"
#include "common/task-queue.h"
+#include "common/tools.h"
#include "humanactivitymonitor/humanactivitymonitor_manager.h"
namespace extension {
namespace humanactivitymonitor {
+namespace {
+
+const std::string kPrivilegeHealthInfo = "http://tizen.org/privilege/healthinfo";
+const std::string kPrivilegeLocation = "http://tizen.org/privilege/location";
+
+} // namespace
+
using common::PlatformResult;
using common::ErrorCode;
using common::TaskQueue;
LoggerD("Enter");
CHECK_EXIST(args, "type", out)
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeHealthInfo, &out);
+
+ const auto type = args.get("type").get<std::string>();
+
+ if (kActivityTypeGps == type) {
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeLocation, &out);
+ }
+
PlatformResult result = Init();
if (!result) {
LogAndReportError(result, &out, ("Failed: Init()"));
return;
}
- auto get = [this, args]() -> void {
+ const auto callback_id = args.get("callbackId").get<double>();
+
+ auto get = [this, type, callback_id]() -> void {
picojson::value response = picojson::value(picojson::object());
picojson::object& response_obj = response.get<picojson::object>();
- response_obj["callbackId"] = args.get("callbackId");
+ response_obj["callbackId"] = picojson::value(callback_id);
picojson::value data = picojson::value();
- PlatformResult result = manager_->GetHumanActivityData(
- args.get("type").get<std::string>(),
- &data);
+ PlatformResult result = manager_->GetHumanActivityData(type, &data);
if (result) {
ReportSuccess(data, response_obj);
LoggerD("Enter");
CHECK_EXIST(args, "type", out)
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeHealthInfo, &out);
+
+ const auto type = args.get("type").get<std::string>();
+
+ if (kActivityTypeGps == type) {
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeLocation, &out);
+ }
+
PlatformResult result = Init();
if (!result) {
LogAndReportError(result, &out, ("Failed: Init()"));
return;
}
- JsonCallback cb = [this, args](picojson::value* data) -> void {
+ const auto listener_id = args.get("listenerId").get<std::string>();
+
+ JsonCallback cb = [this, listener_id](picojson::value* data) -> void {
if (!data) {
LOGGER(ERROR) << "No data passed to json callback";
return;
}
picojson::object& data_o = data->get<picojson::object>();
- data_o["listenerId"] = args.get("listenerId");
+ data_o["listenerId"] = picojson::value(listener_id);
Instance::PostMessage(this, data->serialize().c_str());
};
- result = manager_->SetListener(args.get("type").get<std::string>(), cb);
+ result = manager_->SetListener(type, cb);
if (result) {
ReportSuccess(out);
} else {
LoggerD("Enter");
CHECK_EXIST(args, "type", out)
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeHealthInfo, &out);
+
+ const auto type = args.get("type").get<std::string>();
+
+ if (kActivityTypeGps == type) {
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeLocation, &out);
+ }
+
PlatformResult result = Init();
if (!result) {
LogAndReportError(result, &out, ("Failed: Init()"));
return;
}
- result = manager_->UnsetListener(args.get("type").get<std::string>());
+ result = manager_->UnsetListener(type);
if (result) {
ReportSuccess(out);
} else {
void HumanActivityMonitorInstance::HumanActivityMonitorManagerSetAccumulativePedometerListener(
const picojson::value& args, picojson::object& out) {
+ LoggerD("Enter");
+
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeHealthInfo, &out);
+
// TODO(r.galka) implement
}
void HumanActivityMonitorInstance::HumanActivityMonitorManagerUnsetAccumulativePedometerListener(
const picojson::value& args, picojson::object& out) {
+ LoggerD("Enter");
+
+ CHECK_PRIVILEGE_ACCESS(kPrivilegeHealthInfo, &out);
+
// TODO(r.galka) implement
}
picojson::value* data);
private:
- std::map<std::string, bool> supported_;
+ // common
common::PlatformResult IsSupported(const std::string& type);
-
// WRIST_UP
- gesture_h gesture_handle_;
- JsonCallback wrist_up_event_callback_;
common::PlatformResult SetWristUpListener(JsonCallback callback);
common::PlatformResult UnsetWristUpListener();
static void OnWristUpEvent(gesture_type_e gesture,
double timestamp,
gesture_error_e error,
void* user_data);
-
// HRM
- sensor_listener_h hrm_sensor_listener_;
- JsonCallback hrm_event_callback_;
common::PlatformResult SetHrmListener(JsonCallback callback);
common::PlatformResult UnsetHrmListener();
static void OnHrmSensorEvent(sensor_h sensor,
sensor_event_s *event,
void *user_data);
common::PlatformResult GetHrmData(picojson::value* data);
-
// GPS
- location_manager_h location_handle_;
- JsonCallback gps_event_callback_;
common::PlatformResult SetGpsListener(JsonCallback callback);
common::PlatformResult UnsetGpsListener();
static void OnGpsEvent(int num_of_location, void *user_data);
common::PlatformResult GetGpsData(picojson::value* data);
+
+ // common
+ std::map<std::string, bool> supported_;
+ // WRIST_UP
+ gesture_h gesture_handle_;
+ JsonCallback wrist_up_event_callback_;
+ // HRM
+ sensor_listener_h hrm_sensor_listener_;
+ JsonCallback hrm_event_callback_;
+ // GPS
+ location_manager_h location_handle_;
+ JsonCallback gps_event_callback_;
};
} // namespace humanactivitymonitor