summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/help_prompt_manager_impl.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/application_manager/src/help_prompt_manager_impl.cc')
-rw-r--r--src/components/application_manager/src/help_prompt_manager_impl.cc143
1 files changed, 67 insertions, 76 deletions
diff --git a/src/components/application_manager/src/help_prompt_manager_impl.cc b/src/components/application_manager/src/help_prompt_manager_impl.cc
index 6581c7edd1..0587327791 100644
--- a/src/components/application_manager/src/help_prompt_manager_impl.cc
+++ b/src/components/application_manager/src/help_prompt_manager_impl.cc
@@ -43,7 +43,7 @@
CREATE_LOGGERPTR_GLOBAL(logger_, "HelpPromptManagerImpl")
namespace {
-const std::size_t kLimitCommand = 30;
+const size_t kLimitCommand = 30;
}
namespace application_manager {
@@ -53,7 +53,6 @@ HelpPromptManagerImpl::HelpPromptManagerImpl(Application& app,
: app_(app)
, app_manager_(app_manager)
, sending_type_(SendingType::kSendBoth)
- , count_vr_commands_(0)
, is_tts_send_(false)
, is_ui_send_(false) {}
@@ -63,66 +62,71 @@ HelpPromptManagerImpl::~HelpPromptManagerImpl() {
bool HelpPromptManagerImpl::AddCommand(
const uint32_t cmd_id, const smart_objects::SmartObject& command) {
+ if (!command.keyExists(strings::vr_commands)) {
+ LOG4CXX_DEBUG(logger_, "vr_commands does`t present");
+ return false;
+ }
+
sync_primitives::AutoLock lock(vr_commands_lock_);
- auto it = vr_commands_.find(cmd_id);
+ auto it = std::find_if(
+ vr_commands_.begin(),
+ vr_commands_.end(),
+ [cmd_id](const VRCommandPair& pair) { return pair.first == cmd_id; });
+
if (vr_commands_.end() != it) {
LOG4CXX_DEBUG(logger_, "Commands with id:" << cmd_id << " already exists");
return false;
}
- if (false == command.keyExists(strings::vr_commands)) {
- LOG4CXX_DEBUG(logger_, "vr_commands does`t present");
- return false;
- }
const smart_objects::SmartObject& commands = command[strings::vr_commands];
- const std::size_t count_new_commands = commands.length();
+ const size_t count_new_commands = commands.length();
+ const bool limit_exceeded =
+ kLimitCommand <= GetCommandsCount(vr_commands_.end());
- LOG4CXX_DEBUG(logger_, "Commands to add: " << count_new_commands);
- if (count_vr_commands_ >= kLimitCommand) {
- LOG4CXX_DEBUG(logger_, "Commands limit is exceeded");
- return false;
- }
-
- const std::size_t available_count = kLimitCommand - count_vr_commands_;
- const std::size_t count_to_add = available_count > count_new_commands
- ? count_new_commands
- : available_count;
- LOG4CXX_DEBUG(logger_, "Will be added " << count_to_add << " commands");
+ LOG4CXX_DEBUG(logger_, "Will be added " << count_new_commands << " commands");
- vr_commands_[cmd_id] = utils::MakeShared<smart_objects::SmartObject>(
- smart_objects::SmartType_Array);
- smart_objects::SmartArray& ar_vr_cmd = *(vr_commands_[cmd_id]->asArray());
+ smart_objects::SmartObjectSPtr vr_item =
+ utils::MakeShared<smart_objects::SmartObject>(
+ smart_objects::SmartType_Array);
+ smart_objects::SmartArray& ar_vr_cmd = *(vr_item->asArray());
smart_objects::SmartArray& ar_cmd = *(commands.asArray());
- ar_vr_cmd.reserve(count_vr_commands_ + count_to_add);
+ ar_vr_cmd.reserve(count_new_commands);
ar_vr_cmd.insert(
- ar_vr_cmd.end(), ar_cmd.begin(), ar_cmd.begin() + count_to_add);
+ ar_vr_cmd.end(), ar_cmd.begin(), ar_cmd.begin() + count_new_commands);
+ vr_commands_.push_back(std::make_pair(cmd_id, vr_item));
LOG4CXX_DEBUG(logger_,
"VR commands with id: " << cmd_id << " added for appID: "
- << app_.app_id());
- count_vr_commands_ += count_to_add;
- return true;
+ << app_.app_id() << ". Total "
+ << vr_commands_.size() << " in cache");
+
+ return !limit_exceeded;
}
bool HelpPromptManagerImpl::DeleteCommand(const uint32_t cmd_id) {
LOG4CXX_AUTO_TRACE(logger_);
+
sync_primitives::AutoLock lock(vr_commands_lock_);
- auto it = vr_commands_.find(cmd_id);
+ auto it = std::find_if(
+ vr_commands_.begin(),
+ vr_commands_.end(),
+ [cmd_id](const VRCommandPair& pair) { return pair.first == cmd_id; });
+
if (vr_commands_.end() == it) {
LOG4CXX_WARN(logger_, "VR command with id: " << cmd_id << " not found");
return false;
}
- count_vr_commands_ -= it->second->length();
-
+ const size_t commands_before_current = GetCommandsCount(it);
vr_commands_.erase(it);
- LOG4CXX_DEBUG(
- logger_,
- "VR command with id: "
- << cmd_id << " deleted for appID: " << app_.app_id()
- << ". Remaining number of commands: " << count_vr_commands_);
- return true;
+ LOG4CXX_DEBUG(logger_,
+ "VR command with id: "
+ << cmd_id << " found after " << commands_before_current
+ << " commands was deleted for appID: " << app_.app_id()
+ << " Cache size after deleting: " << vr_commands_.size());
+
+ return commands_before_current < kLimitCommand;
}
void HelpPromptManagerImpl::OnVrCommandAdded(
@@ -185,27 +189,22 @@ void HelpPromptManagerImpl::OnSetGlobalPropertiesReceived(
HelpPromptManagerImpl::SendingType HelpPromptManagerImpl::GetSendingType()
const {
- if (is_tts_send_ && is_ui_send_) {
- return sending_type_;
- } else if (is_tts_send_) {
- return SendingType::kSendHelpPrompt == sending_type_
- ? SendingType::kNoneSend
- : SendingType::kSendVRHelp;
- } else if (is_ui_send_) {
- return SendingType::kSendVRHelp == sending_type_
- ? SendingType::kNoneSend
- : SendingType::kSendHelpPrompt;
- }
-
return sending_type_;
}
+size_t HelpPromptManagerImpl::GetCommandsCount(
+ VRCommandPairs::const_iterator end_element) const {
+ size_t commands_count = 0;
+ std::for_each(vr_commands_.begin(),
+ end_element,
+ [&commands_count](const VRCommandPair& pair) {
+ commands_count += pair.second->length();
+ });
+ return commands_count;
+}
+
void HelpPromptManagerImpl::SendTTSRequest() {
LOG4CXX_AUTO_TRACE(logger_);
- if (vr_commands_.empty()) {
- LOG4CXX_DEBUG(logger_, "vr_commands_ is empty");
- return;
- }
LOG4CXX_DEBUG(logger_, "TTS request for appID:" << app_.app_id());
smart_objects::SmartObjectSPtr tts_global_properties =
utils::MakeShared<smart_objects::SmartObject>(
@@ -240,10 +239,6 @@ void HelpPromptManagerImpl::SendTTSRequest() {
void HelpPromptManagerImpl::SendUIRequest() {
LOG4CXX_AUTO_TRACE(logger_);
- if (vr_commands_.empty()) {
- LOG4CXX_DEBUG(logger_, "vr_commands_ is empty");
- return;
- }
LOG4CXX_DEBUG(logger_, "UI request for appID:" << app_.app_id());
smart_objects::SmartObjectSPtr ui_global_properties =
utils::MakeShared<smart_objects::SmartObject>(
@@ -286,11 +281,6 @@ void HelpPromptManagerImpl::SendRequests() {
LOG4CXX_AUTO_TRACE(logger_);
sync_primitives::AutoLock lock(vr_commands_lock_);
- if (vr_commands_.empty()) {
- LOG4CXX_DEBUG(logger_, "vr_commands_ is empty");
- return;
- }
-
switch (sending_type_) {
case SendingType::kSendHelpPrompt:
SendTTSRequest();
@@ -312,18 +302,16 @@ void HelpPromptManagerImpl::SendRequests() {
void HelpPromptManagerImpl::CreatePromptMsg(
smart_objects::SmartObject& out_msg_params) {
LOG4CXX_AUTO_TRACE(logger_);
- if (vr_commands_.empty()) {
- LOG4CXX_DEBUG(logger_, "vr_commands_ is empty");
- return;
- }
out_msg_params[strings::help_prompt] =
smart_objects::SmartObject(smart_objects::SmartType_Array);
uint32_t index = 0;
- for (const auto& it : vr_commands_) {
- for (std::size_t i = 0; i < it.second->length(); ++i) {
+ for (size_t i = 0; i < vr_commands_.size(); ++i) {
+ const VRCommandPair& pair = vr_commands_[i];
+ for (size_t j = 0; j < pair.second->length() && index < kLimitCommand;
+ ++j) {
smart_objects::SmartObject item(smart_objects::SmartType_Map);
- item[strings::text] = it.second->getElement(i).asString();
+ item[strings::text] = pair.second->getElement(j).asString();
item[strings::type] = mobile_apis::SpeechCapabilities::SC_TEXT;
out_msg_params[strings::help_prompt][index++] = item;
@@ -335,10 +323,6 @@ void HelpPromptManagerImpl::CreatePromptMsg(
void HelpPromptManagerImpl::CreateVRMsg(
smart_objects::SmartObject& out_msg_params) {
LOG4CXX_AUTO_TRACE(logger_);
- if (vr_commands_.empty()) {
- LOG4CXX_DEBUG(logger_, "vr_commands_ is empty");
- return;
- }
if (false == out_msg_params.keyExists(strings::vr_help_title)) {
if (app_.vr_help_title()) {
out_msg_params[strings::vr_help_title] = (*app_.vr_help_title());
@@ -349,17 +333,24 @@ void HelpPromptManagerImpl::CreateVRMsg(
out_msg_params[strings::vr_help] =
smart_objects::SmartObject(smart_objects::SmartType_Array);
uint32_t index = 0;
- for (const auto& it : vr_commands_) {
- for (std::size_t i = 0; i < it.second->length(); ++i) {
+ for (size_t i = 0; i < vr_commands_.size(); ++i) {
+ const VRCommandPair& pair = vr_commands_[i];
+ for (size_t j = 0; j < pair.second->length() && index < kLimitCommand;
+ ++j) {
smart_objects::SmartObject item(smart_objects::SmartType_Map);
- item[strings::text] = it.second->getElement(i).asString();
+ item[strings::text] = pair.second->getElement(j).asString();
item[strings::position] = index + 1;
out_msg_params[strings::vr_help][index++] = item;
}
}
- app_.set_vr_help(out_msg_params[strings::vr_help]);
+ if (out_msg_params[strings::vr_help].empty()) {
+ out_msg_params.erase(strings::vr_help);
+ app_.reset_vr_help();
+ } else {
+ app_.set_vr_help(out_msg_params[strings::vr_help]);
+ }
}
void HelpPromptManagerImpl::SetSendingType(