From: Szymon Jastrzebski Date: Wed, 2 Aug 2017 07:54:57 +0000 (+0200) Subject: [Application] Refactoring getBatteryUsageInfo method into async method X-Git-Tag: submit/tizen/20170809.093015~1^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=df3e5eece9dbfdafb851667c4e8bde99b953da8a;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Application] Refactoring getBatteryUsageInfo method into async method [Verification] Code compiles, manually tested with chromium console Change-Id: I7b891980977b59c799d302d4799a2cc20144a258 Signed-off-by: Szymon Jastrzebski --- diff --git a/src/application/application_api.js b/src/application/application_api.js index d1e57ebb..12bfeef9 100755 --- a/src/application/application_api.js +++ b/src/application/application_api.js @@ -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; } }; diff --git a/src/application/application_instance.cc b/src/application/application_instance.cc index eb4ee2dd..0de967a6 100755 --- a/src/application/application_instance.cc +++ b/src/application/application_instance.cc @@ -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 } diff --git a/src/application/application_manager.cc b/src/application/application_manager.cc index 25e52755..354582d0 100755 --- a/src/application/application_manager.cc +++ b/src/application/application_manager.cc @@ -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(args.get("limit").get()); + } -#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(args.get("limit").get()); - } + 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(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, "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(args.get("days").get()); - 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()) { + callback_id = static_cast(callback.get()); + } + + auto get_battery_usage = [args](const std::shared_ptr& response)-> void { + LoggerD("Entered"); + PlatformResult result = ApplicationManager::GetContextHistory(args, &response.get()->get(), + &ApplicationManager::BatteryUsageFilter, + &ApplicationManager::BatteryUsageAttributes); + if (!result) { + LogAndReportError(result, &response.get()->get()); + } }; - 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& response) -> void { + LoggerD("Entered"); + picojson::object& obj = response->get(); + obj.insert(std::make_pair(kCallbackId, picojson::value(static_cast(callback_id)))); + Instance::PostMessage(&this->instance_, response->serialize().c_str()); + }; + + auto data = std::shared_ptr(new picojson::value(picojson::object())); + + TaskQueue::GetInstance().Queue( + 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; diff --git a/src/application/application_manager.h b/src/application/application_manager.h index 195a2166..ed4a8a1c 100755 --- a/src/application/application_manager.h +++ b/src/application/application_manager.h @@ -91,12 +91,12 @@ class ApplicationManager { 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); + 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 };