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,
#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;