From: Szymon Jastrzebski Date: Wed, 9 Aug 2017 07:21:20 +0000 (+0200) Subject: [Application] Refactoring GetBatteryUsageInfo method X-Git-Tag: submit/tizen/20170809.093015~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F34%2F140334%2F7;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Application] Refactoring GetBatteryUsageInfo method The refactoring is needed for future features, which use Context Native API. [Verification] Code compiles Change-Id: Iadae3cbf53ef9f4c338e4dc18edd4c1eb26cb001 Signed-off-by: Szymon Jastrzebski --- diff --git a/src/application/application_manager.cc b/src/application/application_manager.cc index 8af357a9..25e52755 100755 --- a/src/application/application_manager.cc +++ b/src/application/application_manager.cc @@ -29,9 +29,6 @@ #include #include #include -#ifdef TIZEN_MOBILE -#include -#endif #include "common/current_application.h" #include "common/logger.h" @@ -99,6 +96,10 @@ const std::map 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(args.get("limit").get()); + } - if (args.contains("limit")) { - const int limit = static_cast(args.get("limit").get()); 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(args.get("days").get()); - 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(args.get("days").get()); + 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(); + 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(); - - 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(); + + 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()); + 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"); diff --git a/src/application/application_manager.h b/src/application/application_manager.h index f80ddc1b..195a2166 100755 --- a/src/application/application_manager.h +++ b/src/application/application_manager.h @@ -25,6 +25,10 @@ #include #include #include +#include +#ifdef TIZEN_MOBILE +#include +#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 modifyFilterCb; + typedef std::function addAttributesToObject; + + static common::PlatformResult GetContextHistory(const picojson::value& args, picojson::object* out, + modifyFilterCb modify_filter_cb, + addAttributesToObject add_attributes_to_object); +#endif }; } // namespace application