[HAM] WRIST_UP implementation
authorRafal Galka <r.galka@samsung.com>
Thu, 14 May 2015 14:01:07 +0000 (16:01 +0200)
committerRafal Galka <r.galka@samsung.com>
Thu, 14 May 2015 14:36:16 +0000 (16:36 +0200)
[Info] Unable to verify. Gesture not supported on emulator.

Change-Id: I2d7751c92f4cb1c8e3dbe566f03004b7f926c8ac
Signed-off-by: Rafal Galka <r.galka@samsung.com>
src/humanactivitymonitor/humanactivitymonitor_api.js
src/humanactivitymonitor/humanactivitymonitor_manager.cc
src/humanactivitymonitor/humanactivitymonitor_manager.h

index 77677f50e40b79ceb86da126070a55a67b0e2a23..daa9a1256c0eb115ddd165bc823885ec60f3cc49 100644 (file)
@@ -87,7 +87,21 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
   }
 
   var listener = function(result) {
-    native_.callIfPossible(args.changedCallback, new HumanActivityHRMData(result));
+    switch (args.type) {
+      case HumanActivityType.PEDOMETER:
+        // TODO(r.galka) Not Supported in current implementation
+        break;
+      case HumanActivityType.WRIST_UP:
+        native_.callIfPossible(args.changedCallback, null);
+        break;
+      case HumanActivityType.HRM:
+        native_.callIfPossible(args.changedCallback, new HumanActivityHRMData(result));
+        break;
+      case HumanActivityType.GPS:
+        // TODO(r.galka) implement
+        break;
+    }
+
   };
   native_.addListener(listenerId, listener);
 };
index e60784fcae9ffaa7709aef6d3797bcb34467108a..450a7cbe18dfc135ed7f8897c4555e45b5409ab5 100644 (file)
@@ -17,6 +17,7 @@ using common::ErrorCode;
 HumanActivityMonitorManager::HumanActivityMonitorManager() {}
 
 HumanActivityMonitorManager::~HumanActivityMonitorManager() {
+  UnsetWristUpListener();
   UnsetHrmListener();
 }
 
@@ -40,7 +41,12 @@ PlatformResult HumanActivityMonitorManager::IsSupported(
     // TODO(r.galka) no native api for pedometer
     // so just pass it for not supported.
   } else if (type == kActivityTypeWristUp) {
-    // TODO(r.galka) implement when available in platform
+    ret = gesture_is_supported(GESTURE_WRIST_UP, &supported);
+    if (ret != SENSOR_ERROR_NONE) {
+      LOGGER(ERROR) << "gesture_is_supported(GESTURE_WRIST_UP), error: " << ret;
+      return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                            "WRIST_UP gesture check failed");
+    }
   } else if (type == kActivityTypeHrm) {
     ret = sensor_is_supported(SENSOR_HRM, &supported);
     if (ret != SENSOR_ERROR_NONE) {
@@ -78,7 +84,7 @@ PlatformResult HumanActivityMonitorManager::SetListener(
 
   // WRIST_UP
   if (type == kActivityTypeWristUp) {
-    // TODO(r.galka) implement
+    return SetWristUpListener(callback);
   }
 
   // HRM
@@ -107,7 +113,7 @@ PlatformResult HumanActivityMonitorManager::UnsetListener(
 
   // WRIST_UP
   if (type == kActivityTypeWristUp) {
-    // TODO(r.galka) implement
+    return UnsetWristUpListener();
   }
 
   // HRM
@@ -121,6 +127,71 @@ PlatformResult HumanActivityMonitorManager::UnsetListener(
   }
 }
 
+// WRIST_UP
+PlatformResult HumanActivityMonitorManager::SetWristUpListener(
+    JsonCallback callback) {
+  int ret;
+
+  ret = gesture_create(&gesture_handle_);
+  if (ret != GESTURE_ERROR_NONE) {
+    LOGGER(ERROR) << "Failed to create WRIST_UP handle, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Failed to create WRIST_UP listener");
+  }
+
+  ret = gesture_start_recognition(gesture_handle_,
+                                  GESTURE_WRIST_UP,
+                                  GESTURE_OPTION_DEFAULT,
+                                  OnWristUpEvent,
+                                  this);
+  if (ret != GESTURE_ERROR_NONE) {
+    LOGGER(ERROR) << "Failed to start WRIST_UP listener, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Failed to start WRIST_UP listener");
+  }
+
+  wrist_up_event_callback_ = callback;
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult HumanActivityMonitorManager::UnsetWristUpListener() {
+  int ret;
+
+  if (gesture_handle_) {
+    ret = gesture_stop_recognition(gesture_handle_);
+    if (ret != GESTURE_ERROR_NONE) {
+      LOGGER(ERROR) << "Failed to stop WRIST_UP detection, error: " << ret;
+    }
+
+    ret = gesture_release(gesture_handle_);
+    if (ret != GESTURE_ERROR_NONE) {
+      LOGGER(ERROR) << "Failed to release WRIST_UP handle, error: " << ret;
+    }
+  }
+
+  wrist_up_event_callback_ = nullptr;
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+void HumanActivityMonitorManager::OnWristUpEvent(gesture_type_e gesture,
+                                                 const gesture_data_h data,
+                                                 double timestamp,
+                                                 gesture_error_e error,
+                                                 void* user_data) {
+  HumanActivityMonitorManager* manager =
+      static_cast<HumanActivityMonitorManager*>(user_data);
+
+  if (!manager->wrist_up_event_callback_) {
+    LOGGER(ERROR) << "No WRIST_UP event callback registered, skipping.";
+    return;
+  }
+
+  picojson::value v = picojson::value(); // null value
+  manager->wrist_up_event_callback_(&v);
+}
+
 // HRM
 PlatformResult HumanActivityMonitorManager::SetHrmListener(
     JsonCallback callback) {
@@ -219,6 +290,11 @@ void HumanActivityMonitorManager::OnHrmSensorEvent(
   HumanActivityMonitorManager* manager =
       static_cast<HumanActivityMonitorManager*>(user_data);
 
+  if (!manager->hrm_event_callback_) {
+    LOGGER(ERROR) << "No HRM event callback registered, skipping.";
+    return;
+  }
+
   picojson::value hrm_data = picojson::value(picojson::object());
   picojson::object& hrm_data_o = hrm_data.get<picojson::object>();
 
index fbf311d1ecfd8cdee5f73b956387d194f9bdbe05..8b32a615a480a96b2fe23fbc5be4c5aa59e56d8c 100644 (file)
@@ -40,6 +40,17 @@ class HumanActivityMonitorManager {
  private:
   std::map<std::string, bool> supported_;
 
+  // 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,
+                             const gesture_data_h data,
+                             double timestamp,
+                             gesture_error_e error,
+                             void* user_data);
+
   // HRM
   sensor_listener_h hrm_sensor_listener_;
   JsonCallback hrm_event_callback_;