#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"
{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) :
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,
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");