[Application] Refactoring GetBatteryUsageInfo method 34/140334/7
authorSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Wed, 9 Aug 2017 07:21:20 +0000 (09:21 +0200)
committerSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Wed, 9 Aug 2017 07:21:20 +0000 (09:21 +0200)
The refactoring is needed for future features,
which use Context Native API.

[Verification] Code compiles

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

index 8af357a9324637b3d6d1c931d687a5fe4db5607f..25e527552b4d4764ee38e9260d2307f6139f28ff 100755 (executable)
@@ -29,9 +29,6 @@
 #include <bundle_internal.h>
 #include <appsvc.h>
 #include <app_control_internal.h>
-#ifdef TIZEN_MOBILE
-#include <context-service/context_history.h>
-#endif
 
 #include "common/current_application.h"
 #include "common/logger.h"
@@ -99,6 +96,10 @@ const std::map<std::string, std::string> event_map_ = {
   {SYSTEM_EVENT_DATA_ROAMING_STATE, EVENT_KEY_DATA_ROAMING_STATE},
   {SYSTEM_EVENT_FONT_SET, EVENT_KEY_FONT_SET}
 };
+
+#ifdef TIZEN_MOBILE
+const int kMaximumRetrievedObjects = 30;
+#endif
 }
 
 ApplicationManager::ApplicationManager(ApplicationInstance& instance) :
@@ -1162,121 +1163,75 @@ void ApplicationManager::GetBatteryUsageInfo(const picojson::value& args, picojs
   LoggerD("Entered");
 
 #ifdef TIZEN_MOBILE
-  context_history_list_h list = nullptr;
-  context_history_h handle = nullptr;
-  context_history_filter_h filter = nullptr;
-
-  SCOPE_EXIT {
-    context_history_list_destroy(list);
-    context_history_destroy(handle);
-    context_history_filter_destroy(filter);
-  };
-
-  int ret = context_history_create(&handle);
-  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-    LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to create context handle."), out,
-                      ("Failed to create context handle: %d (%s)", ret, get_error_message(ret)));
-    return;
-  }
-
-  ret = context_history_filter_create(&filter);
-  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-    LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to create filter handle."), out,
-                      ("Failed to create filter handle: %d (%s)", ret, get_error_message(ret)));
-    return;
-  }
+  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>());
+    }
 
-  if (args.contains("limit")) {
-    const int limit = static_cast<int>(args.get("limit").get<double>());
     ret = context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_RESULT_SIZE, limit);
 
     if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      LogAndReportError(
-          PlatformResult(ErrorCode::INVALID_VALUES_ERR, "limit given with invalid value."), out,
+      return LogAndCreateResult(
+          ErrorCode::INVALID_VALUES_ERR, "limit given with invalid value.",
           ("limit given with invalid value: %d (%s)", ret, get_error_message(ret)));
-      return;
     }
-  }
 
-  context_history_data_e date_type = CONTEXT_HISTORY_RECENT_BATTERY_USAGE;
-  if (args.contains("days")) {
-    const int days = static_cast<int>(args.get("days").get<double>());
-    date_type = CONTEXT_HISTORY_BATTERY_USAGE;
-    ret = context_history_filter_set_int(filter, CONTEXT_HISTORY_FILTER_TIME_SPAN, days);
+    context_history_data_e data_type_in = CONTEXT_HISTORY_RECENT_BATTERY_USAGE;
 
-    if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      LogAndReportError(
-          PlatformResult(ErrorCode::INVALID_VALUES_ERR, "days given with invalid value."), out,
-          ("days given with invalid value: %d (%s)", ret, get_error_message(ret)));
-      return;
+    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, "days given with invalid value.",
+            ("days given with invalid value: %d (%s)", ret, get_error_message(ret)));
+      }
     }
-  }
 
-  ret = context_history_get_list(handle, date_type, filter, &list);
-  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-    LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to get list."), out,
-                      ("Failed to get list: %d (%s)", ret, get_error_message(ret)));
-    return;
-  }
+    *data_type_out = data_type_in;
 
-  int size = 0;
-  ret = context_history_list_get_count(list, &size);
-  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-    LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to get list size."), out,
-                      ("Failed to get list size: %d (%s)", ret, get_error_message(ret)));
-    return;
-  }
-
-  double amount;
-  char* app_id;
+    return PlatformResult(ErrorCode::NO_ERROR);
+  };
 
-  context_history_record_h record;
-  picojson::value result_array = picojson::value(
-      picojson::array(size, picojson::value(picojson::object())));
-  picojson::array& array_obj = result_array.get<picojson::array>();
+  auto add_attributes_to_object =
+    [](const context_history_record_h record, picojson::object* object) -> PlatformResult {
+    LoggerD("Entered");
 
-  for (int i = 0; i < size; ++i) {
+    int ret = CONTEXT_HISTORY_ERROR_NONE;
+    double amount = 0.0;
+    char* app_id = nullptr;
     SCOPE_EXIT {
       free(app_id);
-      context_history_record_destroy(record);
     };
 
-    app_id = nullptr;
-    record = nullptr;
-
-    picojson::object& object = array_obj[i].get<picojson::object>();
-
-    ret = context_history_list_get_current(list, &record);
-    if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to current record."), out,
-                        ("Failed to get current record: %d (%s)", ret, get_error_message(ret)));
-      return;
-    }
-
     ret = context_history_record_get_string(record, CONTEXT_HISTORY_APP_ID, &app_id);
     if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to get string."), out,
-                        ("Failed to get string: %d (%s)", ret, get_error_message(ret)));
-      return;
+      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)));
 
     ret = context_history_record_get_double(record, CONTEXT_HISTORY_TOTAL_AMOUNT, &amount);
     if (CONTEXT_HISTORY_ERROR_NONE != ret) {
-      LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to get amount."), out,
-                        ("Failed to get amount: %d (%s)", ret, get_error_message(ret)));
-      return;
+      return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get amount.",
+          ("Failed to get amount: %d (%s)", ret, get_error_message(ret)));
     }
-    object.insert(std::make_pair("batteryUsage", picojson::value(amount)));
 
-    ret = context_history_list_move_next(list);
-    if (CONTEXT_HISTORY_ERROR_NONE != ret && CONTEXT_HISTORY_ERROR_NO_DATA != ret) {
-      LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Failed to move iterator."), out,
-                        ("Failed to move iterator: %d (%s)", ret, get_error_message(ret)));
-      return;
-    }
+    object->insert(std::make_pair("appId", picojson::value(app_id)));
+    object->insert(std::make_pair("batteryUsage", picojson::value(amount)));
+
+    return PlatformResult(ErrorCode::NO_ERROR);
+  };
+
+  PlatformResult result = ApplicationManager::GetContextHistory(args, out, modify_filter_cb, add_attributes_to_object);
+  if (!result) {
+    LogAndReportError(result, out);
   }
-  ReportSuccess(result_array, *out);
 #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,
@@ -1808,6 +1763,90 @@ void ApplicationManager::OnStatusEvent(const char *type, const char *app_id,
   manager->status_callback_(&event);
 }
 
+#ifdef TIZEN_MOBILE
+PlatformResult ApplicationManager::GetContextHistory(const picojson::value& args, picojson::object* out,
+                                                     modifyFilterCb modify_filter_cb,
+                                                     addAttributesToObject add_attributes_to_object) {
+  LoggerD("Entered");
+  context_history_list_h list = nullptr;
+  context_history_h handle = nullptr;
+  context_history_filter_h filter = nullptr;
+
+  SCOPE_EXIT {
+    context_history_list_destroy(list);
+    context_history_destroy(handle);
+    context_history_filter_destroy(filter);
+  };
+
+  int ret = context_history_create(&handle);
+  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to create context handle.",
+        ("Failed to create context handle: %d (%s)", ret, get_error_message(ret)));
+  }
+
+  ret = context_history_filter_create(&filter);
+  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to create filter handle.",
+        ("Failed to create filter handle: %d (%s)", ret, get_error_message(ret)));
+  }
+
+  context_history_data_e data_type;
+
+  PlatformResult result = modify_filter_cb(args, filter, &data_type);
+  if (!result) {
+    return result;
+  }
+
+  picojson::value result_array = picojson::value(picojson::array());
+  picojson::array& array_obj = result_array.get<picojson::array>();
+
+  ret = context_history_get_list(handle, data_type, filter, &list);
+  if (CONTEXT_HISTORY_ERROR_NO_DATA == ret) {
+    ReportSuccess(result_array, *out);
+    return PlatformResult(ErrorCode::NO_ERROR);
+  } else if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get list.",
+        ("Failed to get list: %d (%s)", ret, get_error_message(ret)));
+  }
+
+  int size = 0;
+  ret = context_history_list_get_count(list, &size);
+  if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+    return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get list size.",
+        ("Failed to get list size: %d (%s)", ret, get_error_message(ret)));
+  }
+
+  array_obj.resize(size, picojson::value(picojson::object()));
+
+  for (int i = 0; i < size; ++i) {
+    context_history_record_h record = nullptr;
+    SCOPE_EXIT {
+      context_history_record_destroy(record);
+    };
+
+    ret = context_history_list_get_current(list, &record);
+    if (CONTEXT_HISTORY_ERROR_NONE != ret) {
+      return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to get current record.",
+          ("Failed to get current record: %d (%s)", ret, get_error_message(ret)));
+    }
+
+    result = add_attributes_to_object(record, &array_obj[i].get<picojson::object>());
+    if (!result) {
+      return result;
+    }
+
+    ret = context_history_list_move_next(list);
+    if (CONTEXT_HISTORY_ERROR_NONE != ret && CONTEXT_HISTORY_ERROR_NO_DATA != ret) {
+      return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to move iterator.",
+          ("Failed to move iterator: %d (%s)", ret, get_error_message(ret)));
+    }
+  }
+
+  ReportSuccess(result_array, *out);
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+#endif
+
 PlatformResult ApplicationManager::StartStatusListener(const JsonCallback& callback) {
   LoggerD("Entered");
 
index f80ddc1b2ee2a64cbcdc1ae97f8574b8f83ef40a..195a2166a7d4fab1223a02fce7272e1c39dd2094 100755 (executable)
 #include <package-manager.h>
 #include <string>
 #include <pkgmgr-info.h>
+#include <functional>
+#ifdef TIZEN_MOBILE
+#include <context-service/context_history.h>
+#endif
 
 #include "common/picojson.h"
 #include "common/platform_result.h"
@@ -85,6 +89,15 @@ class ApplicationManager {
                             app_manager_event_state_e event_state,
                             app_manager_event_h handle,
                             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);
+#endif
 };
 
 } // namespace application