[HAM] Added error callback to start() function.
authorTomasz Marciniak <t.marciniak@samsung.com>
Mon, 25 Apr 2016 08:04:52 +0000 (10:04 +0200)
committerTomasz Marciniak <t.marciniak@samsung.com>
Mon, 25 Apr 2016 08:14:57 +0000 (10:14 +0200)
[Verification] Code compiles. Tested on emulator.
When location is off error callback is called.

Change-Id: Icd7fc9fe39fe45feb98eec48ec1c7c0c40b10d2a
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
src/humanactivitymonitor/humanactivitymonitor_api.js
src/humanactivitymonitor/humanactivitymonitor_manager.cc

index e71b6a7b046aad991ca12de7fad7e905a82715aa..6ba81b7c3b21d6d407b8a4dc5dffaa3443c4318a 100755 (executable)
@@ -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() {
index 12224d3a7bba7d2dd230334a28fe13efbb09d014..b52282c50c1e3ca756e14e709b034019f4f14a94 100755 (executable)
@@ -577,6 +577,15 @@ class HumanActivityMonitorManager::Monitor::GpsMonitor : public HumanActivityMon
       int sample_interval = static_cast<int>(args.get(kSampleInterval).get<double>() / 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<GpsMonitor*>(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<picojson::object>();
+
+      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();