[Application] Refactoring getBatteryUsageInfo method into async method 68/140768/8
authorSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Wed, 2 Aug 2017 07:54:57 +0000 (09:54 +0200)
committerSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Wed, 9 Aug 2017 07:22:15 +0000 (09:22 +0200)
[Verification] Code compiles, manually tested with chromium console

Change-Id: I7b891980977b59c799d302d4799a2cc20144a258
Signed-off-by: Szymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
src/application/application_api.js
src/application/application_instance.cc
src/application/application_manager.cc
src/application/application_manager.h

index d1e57ebb3d27b54c60a48fe20d70456186ae0b3e..12bfeef97c7409bb4f7e98c32764515410a89828 100755 (executable)
@@ -498,6 +498,16 @@ ApplicationManager.prototype.getAppMetaData = function() {
 
 ApplicationManager.prototype.getBatteryUsageInfo = function() {
   var args = AV.validateMethod(arguments, [
+  {
+    name: 'successCallback',
+    type: AV.Types.FUNCTION
+  },
+  {
+    name: 'errorCallback',
+    type: AV.Types.FUNCTION,
+    optional: true,
+    nullable: true
+  },
   {
     name: 'days',
     type: AV.Types.LONG,
@@ -521,16 +531,17 @@ ApplicationManager.prototype.getBatteryUsageInfo = function() {
     callArgs.limit = args.limit;
   }
 
-  var result = native.callSync('ApplicationManager_getBatteryUsageInfo', callArgs);
+  var callback = function(result) {
+    if (native.isFailure(result)) {
+      native.callIfPossible(args.errorCallback, native.getErrorObject(result));
+    } else {
+      args.successCallback(native.getResultObject(result));
+    }
+  };
+
+  var result = native.call('ApplicationManager_getBatteryUsageInfo', callArgs, callback);
   if (native.isFailure(result)) {
     throw native.getErrorObject(result);
-  } else {
-    var metaData = native.getResultObject(result);
-    var data = [];
-    metaData.forEach(function(i) {
-      data.push(new ApplicationBatteryUsage(i));
-    });
-    return data;
   }
 };
 
index eb4ee2ddb41d10beb9ad69eff78c865f56c60948..0de967a6f9dd8c633e478d1d64be25f5ab9ad30a 100755 (executable)
@@ -60,7 +60,6 @@ ApplicationInstance::ApplicationInstance() :
   REGISTER_SYNC("ApplicationManager_getAppCerts", GetAppCerts);
   REGISTER_SYNC("ApplicationManager_getAppSharedURI", GetAppSharedURI);
   REGISTER_SYNC("ApplicationManager_getAppMetaData", GetAppMetaData);
-  REGISTER_SYNC("ApplicationManager_getBatteryUsageInfo", GetBatteryUsageInfo);
   REGISTER_SYNC("ApplicationManager_addAppInfoEventListener", AddAppInfoEventListener);
   REGISTER_SYNC("ApplicationManager_removeAppInfoEventListener", RemoveAppInfoEventListener);
   REGISTER_SYNC("ApplicationManager_addAppStatusChangeListener", AddStatusListener);
@@ -90,6 +89,7 @@ ApplicationInstance::ApplicationInstance() :
   REGISTER_ASYNC("ApplicationManager_findAppControl", FindAppControl);
   REGISTER_ASYNC("ApplicationManager_getAppsContext", GetAppsContext);
   REGISTER_ASYNC("ApplicationManager_getAppsInfo", GetAppsInfo);
+  REGISTER_ASYNC("ApplicationManager_getBatteryUsageInfo", GetBatteryUsageInfo);
 #undef REGISTER_ASYNC
 }
 
index 25e527552b4d4764ee38e9260d2307f6139f28ff..354582d0159104f8f1fd5a96beb07ef1f76a6f7b 100755 (executable)
@@ -1159,79 +1159,106 @@ void ApplicationManager::GetAppSharedUri(const std::string& app_id, picojson::ob
   ReportSuccess(result, *out);
 }
 
-void ApplicationManager::GetBatteryUsageInfo(const picojson::value& args, picojson::object* out) {
+#ifdef TIZEN_MOBILE
+PlatformResult ApplicationManager::BatteryUsageFilter(const picojson::value& args, const context_history_filter_h filter,
+                                                      context_history_data_e* data_type_out) {
   LoggerD("Entered");
+  int ret = CONTEXT_HISTORY_ERROR_NONE;
+  int limit = kMaximumRetrievedObjects;
+  if (args.contains("limit")) {
+    limit = static_cast<int>(args.get("limit").get<double>());
+  }
 
-#ifdef TIZEN_MOBILE
-  auto modify_filter_cb = [](const picojson::value& args, const context_history_filter_h filter,
-      context_history_data_e* data_type_out) -> PlatformResult {
-    LoggerD("Entered");
-    int ret = CONTEXT_HISTORY_ERROR_NONE;
-    int limit = kMaximumRetrievedObjects;
-    if (args.contains("limit")) {
-      limit = static_cast<int>(args.get("limit").get<double>());
-    }
+  ret = context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_RESULT_SIZE, limit);
 
-    ret = context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_RESULT_SIZE, limit);
+  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(
+        ErrorCode::INVALID_VALUES_ERR, "limit given with invalid value.",
+        ("limit given with invalid value: %d (%s)", ret, get_error_message(ret)));
+  }
+
+  context_history_data_e data_type_in = CONTEXT_HISTORY_RECENT_BATTERY_USAGE;
+
+  if (args.contains("days")) {
+    const int days = static_cast<int>(args.get("days").get<double>());
+    data_type_in = CONTEXT_HISTORY_BATTERY_USAGE;
+    ret = context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_TIME_SPAN, days);
 
     if (CONTEXT_HISTORY_ERROR_NONE != ret) {
       return LogAndCreateResult(
-          ErrorCode::INVALID_VALUES_ERR, "limit given with invalid value.",
-          ("limit given with invalid value: %d (%s)", ret, get_error_message(ret)));
+          ErrorCode::INVALID_VALUES_ERR, "days given with invalid value.",
+          ("days given with invalid value: %d (%s)", ret, get_error_message(ret)));
     }
+  }
 
-    context_history_data_e data_type_in = CONTEXT_HISTORY_RECENT_BATTERY_USAGE;
-
-    if (args.contains("days")) {
-      const int days = static_cast<int>(args.get("days").get<double>());
-      data_type_in = CONTEXT_HISTORY_BATTERY_USAGE;
-      ret = context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_TIME_SPAN, days);
+  *data_type_out = data_type_in;
 
-      if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-        return LogAndCreateResult(
-            ErrorCode::INVALID_VALUES_ERR, "days given with invalid value.",
-            ("days given with invalid value: %d (%s)", ret, get_error_message(ret)));
-      }
-    }
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
 
-    *data_type_out = data_type_in;
+PlatformResult ApplicationManager::BatteryUsageAttributes(const context_history_record_h record, picojson::object* object) {
+  LoggerD("Entered");
 
-    return PlatformResult(ErrorCode::NO_ERROR);
+  int ret = CONTEXT_HISTORY_ERROR_NONE;
+  double amount = 0.0;
+  char* app_id = nullptr;
+  SCOPE_EXIT {
+    free(app_id);
   };
 
-  auto add_attributes_to_object =
-    [](const context_history_record_h record, picojson::object* object) -> PlatformResult {
-    LoggerD("Entered");
+  ret = context_history_record_get_string(record, CONTEXT_HISTORY_APP_ID, &app_id);
+  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get string.",
+        ("Failed to get string: %d (%s)", ret, get_error_message(ret)));
+  }
 
-    int ret = CONTEXT_HISTORY_ERROR_NONE;
-    double amount = 0.0;
-    char* app_id = nullptr;
-    SCOPE_EXIT {
-      free(app_id);
-    };
+  ret = context_history_record_get_double(record, CONTEXT_HISTORY_TOTAL_AMOUNT, &amount);
+  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get amount.",
+        ("Failed to get amount: %d (%s)", ret, get_error_message(ret)));
+  }
 
-    ret = context_history_record_get_string(record, CONTEXT_HISTORY_APP_ID, &app_id);
-    if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get string.",
-          ("Failed to get string: %d (%s)", ret, get_error_message(ret)));
-    }
+  object->insert(std::make_pair("appId", picojson::value(app_id)));
+  object->insert(std::make_pair("batteryUsage", picojson::value(amount)));
 
-    ret = context_history_record_get_double(record, CONTEXT_HISTORY_TOTAL_AMOUNT, &amount);
-    if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get amount.",
-          ("Failed to get amount: %d (%s)", ret, get_error_message(ret)));
-    }
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+#endif
 
-    object->insert(std::make_pair("appId", picojson::value(app_id)));
-    object->insert(std::make_pair("batteryUsage", picojson::value(amount)));
+void ApplicationManager::GetBatteryUsageInfo(const picojson::value& args, picojson::object* out) {
+  LoggerD("Entered");
 
-    return PlatformResult(ErrorCode::NO_ERROR);
+#ifdef TIZEN_MOBILE
+  int callback_id = -1;
+  const auto& callback = args.get(kCallbackId);
+  if (callback.is<double>()) {
+    callback_id = static_cast<int>(callback.get<double>());
+  }
+
+  auto get_battery_usage = [args](const std::shared_ptr<picojson::value>& response)-> void {
+    LoggerD("Entered");
+    PlatformResult result = ApplicationManager::GetContextHistory(args, &response.get()->get<picojson::object>(),
+                                                                  &ApplicationManager::BatteryUsageFilter,
+                                                                  &ApplicationManager::BatteryUsageAttributes);
+    if (!result) {
+      LogAndReportError(result, &response.get()->get<picojson::object>());
+    }
   };
 
-  PlatformResult result = ApplicationManager::GetContextHistory(args, out, modify_filter_cb, add_attributes_to_object);
-  if (!result) {
-    LogAndReportError(result, out);
-  }
+  auto get_battery_usage_response = [this, callback_id](
+      const std::shared_ptr<picojson::value>& response) -> void {
+    LoggerD("Entered");
+    picojson::object& obj = response->get<picojson::object>();
+    obj.insert(std::make_pair(kCallbackId, picojson::value(static_cast<double>(callback_id))));
+    Instance::PostMessage(&this->instance_, response->serialize().c_str());
+  };
+
+  auto data = std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
+
+  TaskQueue::GetInstance().Queue<picojson::value>(
+    get_battery_usage,
+    get_battery_usage_response,
+    data);
 #else
   // 20170510 Context API is supported only for mobile profile, other ones would result with NotSupportedError
   LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "This feature is not supported on this profile."), out,
@@ -1765,8 +1792,8 @@ void ApplicationManager::OnStatusEvent(const char *type, const char *app_id,
 
 #ifdef TIZEN_MOBILE
 PlatformResult ApplicationManager::GetContextHistory(const picojson::value& args, picojson::object* out,
-                                                     modifyFilterCb modify_filter_cb,
-                                                     addAttributesToObject add_attributes_to_object) {
+                                                     common::PlatformResult (*modify_filter_cb)(const picojson::value&, const context_history_filter_h, context_history_data_e* data_type),
+                                                     common::PlatformResult (*add_attributes_to_object)(const context_history_record_h, picojson::object*)) {
   LoggerD("Entered");
   context_history_list_h list = nullptr;
   context_history_h handle = nullptr;
index 195a2166a7d4fab1223a02fce7272e1c39dd2094..ed4a8a1ca46d8d02733bb25ec0536731b8c41775 100755 (executable)
@@ -91,12 +91,12 @@ class ApplicationManager {
                             void *user_data);
 
 #ifdef TIZEN_MOBILE
-  typedef std::function<common::PlatformResult(const picojson::value&, const context_history_filter_h, context_history_data_e* data_type)> modifyFilterCb;
-  typedef std::function<common::PlatformResult(const context_history_record_h, picojson::object*)> addAttributesToObject;
-
   static common::PlatformResult GetContextHistory(const picojson::value& args, picojson::object* out,
-                                                  modifyFilterCb modify_filter_cb,
-                                                  addAttributesToObject add_attributes_to_object);
+                                                  common::PlatformResult (*)(const picojson::value&, const context_history_filter_h, context_history_data_e* data_type),
+                                                  common::PlatformResult (*)(const context_history_record_h, picojson::object*));
+
+  static common::PlatformResult BatteryUsageFilter(const picojson::value&, const context_history_filter_h, context_history_data_e* data_type);
+  static common::PlatformResult BatteryUsageAttributes(const context_history_record_h, picojson::object*);
 #endif
 };